src
stringlengths 95
64.6k
| complexity
stringclasses 7
values | problem
stringlengths 6
50
| from
stringclasses 1
value |
---|---|---|---|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class E implements Runnable {
public static void main (String[] args) {new Thread(null, new E(), "_cf", 1 << 28).start();}
int n, m;
char[] str;
int[][] occs, cost;
int[] dp;
public void run() {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
System.err.println("");
//where's my 420???? :(
long tot = 0;
for(int i = 0; i < 19000; i++) tot += i;
System.err.println(tot);
n = fs.nextInt(); m = fs.nextInt();
byte[] str = fs.next().getBytes();
int[] occs = new int[1<<m];
for(int i = 0; i < n-1; i++) {
int l1 = str[i] - 'a';
int l2 = str[i+1] - 'a';
occs[(1<<l1) | (1<<l2)]++;
occs[(1<<l2) | (1<<l1)]++;
}
//cost[mask][v] = numPairs with v for some all bits on in mask
int all = (1<<m)-1;
cost = new int[m][1<<m];
for(int i = 0; i < m; i++) {
for(int mask = 1; mask < all; mask++) {
if(((1<<i)&mask) > 0) continue;
int lb = mask & (-mask);
int trail = Integer.numberOfTrailingZeros(lb);
int nmask = mask ^ lb;
cost[i][mask] = cost[i][nmask]+occs[1<<i | 1<<trail];
}
}
dp = new int[1<<m];
for(int mask = dp.length-2; mask >= 0; mask--) {
int addOn = 0;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
addOn += cost[nxt][mask];
}
int res = oo;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
int ret = addOn+dp[mask | (1<<nxt)];
res = min(res, ret);
}
dp[mask] = res;
}
System.out.println(dp[0]>>1);
out.close();
}
int oo = (int)1e9;
int min(int a, int b) {
if(a < b) return a;
return b;
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class thing {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
String s = in.next();
int[][] count = new int[m][m];
int[] dp = new int[1 << m];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for(int i = 1; i < n; i++) {
int a = s.charAt(i)-'a';
int b = s.charAt(i-1)-'a';
count[a][b]++;
count[b][a]++;
}
for(int i = 1; i < (1 << m); i++) {
int pos = set_bits(i);
for(int j = 0; (i >> j) != 0; j++) {
if(((i >> j) & 1) == 0) continue;
int sum = 0;
for(int mask = i, y = 0; y < m; mask >>= 1, y++) {
if(y == j) continue;
if((mask & 1) == 1) sum += count[j][y];
else sum -= count[j][y];
}
int calc = dp[i-(1<<j)] + pos*sum;
dp[i] = Math.min(dp[i], calc);
}
}
System.out.println(dp[(1 << m)-1]);
}
public static int set_bits(int n) {
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
}
| np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main {
static long mod = 1000000007;
static int INF = 1000000000;
public static void main(String[] args){
FastScanner scanner = new FastScanner();
int n = scanner.nextInt();
int m = scanner.nextInt();
String s = scanner.next();
int[][] cnt = new int[20][20];
for(int i = 0; i < n-1; i++){
cnt[s.charAt(i)-'a'][s.charAt(i+1)-'a']++;
cnt[s.charAt(i+1)-'a'][s.charAt(i)-'a']++;
}
//dp[i]:= 文字列i(この中に同一文字は含まれない)を作った時のコストの最小値
int[] dp = new int[(1<<m)];
for(int i = 0; i < (1<<m); i++){
dp[i] = INF;
}
dp[0] = 0;
for(int i = 0; i < (1<<m); i++){
int cost = 0;
for(int j = 0; j < m; j++){
if((i>>j & 1) == 0){
for(int k = 0; k < m; k++){
if((~i>>k & 1) == 0){
cost += cnt[j][k];
}
}
}
}
for(int j = 0; j < m; j++){
dp[i|1<<j] = Math.min(dp[i|1<<j],dp[i]+cost);
}
}
System.out.println(dp[(1<<m)-1]);
}
static class BIT{
int n;
int[] bit;
public BIT(int n){
this.n = n;
bit = new int[n+1];
}
void add(int idx, int val){
for(int i = idx+1; i <= n; i += i&(-i)) bit[i-1] += val;
}
int sum(int idx){
int res = 0;
for(int i = idx+1; i > 0; i -= i&(-i)) res += bit[i-1];
return res;
}
int sum(int begin, int end){
if(begin == 0) return sum(end);
return sum(end)-sum(begin-1);
}
}
static class Pair implements Comparable<Pair>{
int first, second;
Pair(int a, int b){
first = a;
second = b;
}
@Override
public boolean equals(Object o){
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair p = (Pair) o;
return first == p.first && second == p.second;
}
@Override
public int compareTo(Pair p){
return first == p.first ? second - p.second : first - p.first; //firstで昇順にソート
//return (first == p.first ? second - p.second : first - p.first) * -1; //firstで降順にソート
//return second == p.second ? first - p.first : second - p.second;//secondで昇順にソート
//return (second == p.second ? first - p.first : second - p.second)*-1;//secondで降順にソート
//return first * 1.0 / second > p.first * 1.0 / p.second ? 1 : -1; // first/secondの昇順にソート
//return first * 1.0 / second < p.first * 1.0 / p.second ? 1 : -1; // first/secondの降順にソート
//return second * 1.0 / first > p.second * 1.0 / p.first ? 1 : -1; // second/firstの昇順にソート
//return second * 1.0 / first < p.second * 1.0 / p.first ? 1 : -1; // second/firstの降順にソート
//return Math.atan2(second, first) > Math.atan2(p.second, p.first) ? 1 : -1; // second/firstの昇順にソート
//return first + second > p.first + p.second ? 1 : -1; //first+secondの昇順にソート
//return first + second < p.first + p.second ? 1 : -1; //first+secondの降順にソート
//return first - second < p.first - p.second ? 1 : -1; //first-secondの降順にソート
}
}
private static class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() { return Double.parseDouble(next());}
}
}
| np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.util.*;
import java.io.*;
public class E74 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt(); int m = sc.nextInt();
String s = sc.next();
long time = System.currentTimeMillis();
int [][] a = new int[m][m];
int [][] pre = new int[m][(1 << m)];
for (int i = 0; i < n - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) continue;
a[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a']++;
a[s.charAt(i + 1) - 'a'][s.charAt(i) - 'a']++;
}
//System.out.println("Process Input: " + (System.currentTimeMillis() - time));
for (int i = 0; i < m; i++) {
int b = 0; int stor = 2;
for (int j = 1; j < (1 << m); j++) {
if (j == stor) {
b++;
stor = (1 << (b + 1));
}
pre[i][j] = pre[i][j ^ (1 << b)] + a[i][b];
}
}
//System.out.println("Precomp: " + (System.currentTimeMillis() - time));
long [] dp = new long[1 << m];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int mask = 1; mask < (1 << m); mask++) {
// loop on the last added character in the mask
for (int i = 0; i < m; i++) {
if (((mask >> i) & 1) == 0) continue;
long prev = dp[mask ^ (1 << i)];
long contribution = (pre[i][mask] - pre[i][((1 << m) - 1) ^ mask]) * Integer.bitCount(mask);
dp[mask] = Math.min(dp[mask], prev + contribution);
}
}
//System.out.println("Done: " + (System.currentTimeMillis() - time));
out.println(dp[(1 << m) - 1]);
out.close();
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class E implements Runnable {
public static void main (String[] args) {new Thread(null, new E(), "_cf", 1 << 28).start();}
int n, m;
char[] str;
int[][] occs, cost;
int[] dp;
public void run() {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
System.err.println("");
//where's my 420???? :(
long tot = 0;
for(int i = 0; i < 19999; i++) tot += i;
System.err.println(tot);
n = fs.nextInt(); m = fs.nextInt();
byte[] str = fs.next().getBytes();
int[] occs = new int[1<<m];
for(int i = 0; i < n-1; i++) {
int l1 = str[i] - 'a';
int l2 = str[i+1] - 'a';
occs[(1<<l1) | (1<<l2)]++;
occs[(1<<l2) | (1<<l1)]++;
}
//cost[mask][v] = numPairs with v for some all bits on in mask
int all = (1<<m)-1;
cost = new int[m][1<<m];
for(int i = 0; i < m; i++) {
for(int mask = 1; mask < all; mask++) {
if(((1<<i)&mask) > 0) continue;
int lb = mask & (-mask);
int trail = Integer.numberOfTrailingZeros(lb);
int nmask = mask ^ lb;
cost[i][mask] = cost[i][nmask]+occs[1<<i | 1<<trail];
}
}
dp = new int[1<<m];
for(int mask = dp.length-2; mask >= 0; mask--) {
int addOn = 0;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
addOn += cost[nxt][mask];
}
int res = oo;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
int ret = addOn+dp[mask | (1<<nxt)];
res = min(res, ret);
}
dp[mask] = res;
}
System.out.println(dp[0]>>1);
out.close();
}
int oo = (int)1e9;
int min(int a, int b) {
if(a < b) return a;
return b;
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
// No sorcery shall prevail. //
import java.util.*;
import java.io.*;
public class InVoker {
//Variables
static long mod = 1000000007;
static long mod2 = 998244353;
static FastReader inp= new FastReader();
static PrintWriter out= new PrintWriter(System.out);
public static void main(String args[]) {
InVoker g=new InVoker();
g.main();
out.close();
}
int dp[];
int freq[][];
int m;
//Main
void main() {
int n=inp.nextInt();
m=inp.nextInt();
String s=inp.next();
dp=new int[1<<m];
Arrays.fill(dp, -1);
freq=new int[m][m];
for(int i=0;i<n-1;i++) {
int x=s.charAt(i)-'a';
int y=s.charAt(i+1)-'a';
if(x==y) continue;
freq[x][y]++;
freq[y][x]++;
}
out.println(go(0,0));
}
int go(int pos, int mask) {
if(pos==m) return 0;
if(dp[mask]!=-1) return dp[mask];
int gg=Integer.MAX_VALUE;
for(int i=0;i<m;i++) {
if((mask>>i&1)==1) continue;
int cost=0;
for(int j=0;j<m;j++) {
if((mask>>j&1)==1) cost+=freq[i][j]*pos;
else cost-=freq[i][j]*pos;
}
cost+=go(pos+1,mask|(1<<i));
gg=Math.min(gg, cost);
}
return dp[mask]=gg;
}
/*********************************************************************************************************************************************************************************************************
* ti;. .:,:i: :,;;itt;. fDDEDDEEEEEEKEEEEEKEEEEEEEEEEEEEEEEE###WKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWW#WWWWWKKKKKEE :,:. f::::. .,ijLGDDDDDDDEEEEEEE*
*ti;. .:,:i: .:,;itt;: GLDEEGEEEEEEEEEEEEEEEEEEDEEEEEEEEEEE#W#WEKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKKKKKKG. .::. f:,...,ijLGDDDDDDDDEEEEEE *
*ti;. .:,:i: :,;;iti, :fDDEEEEEEEEEEEEEEEKEEEEDEEEEEEEEEEEW##WEEEKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWWKKKKKKEG .::. .f,::,ijLGDDDDDDDDEEEEEE *
*ti;. .:,:i: .,,;iti;. LDDEEEEEEEEEEKEEEEWEEEDDEEEEEEEEEEE#WWWEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWKKKKKEDj .::. .:L;;ijfGDDDDDDDDDEEEEE *
*ti;. .:,:i: .:,;;iii:LLDEEEEEEEEEEEKEEEEEEEEDEEEEEEEEEEEW#WWEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKK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DDiL.,KDDEEEWEEEEEEEEEEEEWWKEEDDDDDGf: .,itfLGGDDDDDDDDDDDD *
*tti. .....:::.. ...,. fDEEEKKKKKKKKKWKKKKWWWWWWWWWWWWW########WLiiWWWKKKEEjD...G,DDDDi;...EWEEEEEEEEDKDEii..LDDEEEWEEEEEEEEEEEEWWWEEDDDDDGfi .,;tfLGGGDDDDDDDDDDD *
*tti. .....:::.. ...,. iGEEEKKKKKKKKKKWKKKKWWWWWWWWWWWW###########KiWWWKKEEE,.D..D.DDDii:...KKEEEEEEEEEDDj:...tEDEEEWEEEEEEEEEEEEWWWEEEDDDDDLL .,;tjLLGGDDDDDDDDDDD *
*tti. ....::::......:. LEEEKKKKKKKKKKWWKKKWWW#KWWWWWWWW#####W####W##KWWKKEEL..:D.jjDDi;,....KKEEEEEEEDfDDi...:iKDEEEWKEEEEEEEEEEEWWWEEEEDDDDLG .,;tjLLGGDDDDDDDDDDD *
*tti. ...::::::..,. :GEEEKKKKKKKKKKKKWWWWW##WWWWWWWWW##WKWK#W#W####WWKEEK.....G.DDti,.....KKEEEEEEDWGDf.,...iKDEEEWWEEEEEEEEEEEW#WEEEEEDDDGL .,;tjLLGGDDDDDDDDDDD *
*tti. ....::::::,. GDEEKKKKKKKKKKKKKWWWW###WWWWWWWWWW#WWWK###W#####WKEKK.....jDDL;;......KKEEEEEEEEEDi.f...;KDEEEWWEEEEEEEEEEEWWWWEEEEEDDGf .,;tjLLGGDDDDDDDDDDD *
*tti. ....:::,,. .LEEEKKKKKWKKKKKWWWWWW###WWWWWWWWWW#WWKW#WW##W#WWWKEKD:....:DD:;......;KEEEEEEEKiDD..f...,KKEEEWWEEEEEEEEEEEWWWWEEEEEDDDf .:;tjLLGGGDDDDDDDDDD *
*tti. ...::,,,:. GDEEKKKKKKKKKKKKWWWWWWW#WWWWWWWWWWW#KjKWWWWWWWWWWWWEK.j,..;fD.;.......fKEEEEEDKG:Di..,....DKEEEWWEEEEEEKEKKKWWWWEEEEEEDDf .:;tjLLGGDDDDDDDDDDD *
*jti. ...::,,,,:. .fEEEKKKKKWKKKKKKWWWWWWW#WWWWWWWWWWK#KKKWWWWWWWWWWWWWK..f:.:G.,:.......EKEEEEEKK;:E:.......fKEEEWWKEKEKKKKKKKW#WWEEEEEEDDf: .,;tfLLGGDDDDDDDDDDD *
*tti. ...:,,,;;,: iDEEKKKKKWKKKKKKKWWWWWWW#WWWWWWWWWWK#WDKWWKKWWWWWWWWWE..;G:G..,........KKEEEEEKi.Gi..:.....tKEEKWWWKKKKKKKKKKW##WKEEEEEEDfi .,;tfLLGGGDDDDDDDDDD *
*tti. ....::,,;;;,LEEKKKKKKWKKKKKWWWWWWW###WWWWWWWWWWKWWDKWEEEWKKWWWWWKKj.:LG..;.........EKEEEEKG;.G...;.....;KKEKWWWKKKKKKKKKKW##WWKEEEEEDfL .,;tfLGGGDDDDDDDDDDD *
*jti. ...::::,;ijDEEKKKKKWKKKKKKWKWWWWW##WWWWWWWWWWWKK#KKGDGDWEEWKKWKKGE,.i;.:.........:EKEEEKE;.:L...j.....,KWEKWWWKKKKKKKKKK####WKKEEEEDLG .,;tfLGGGGDDDDDDDDDD *
*jti. ...:...,,;GEEKKKKKWWKKKKKWWWWWWWW###WWWWWWWWWKKKWWKiLGGEDEDEKGKKiEG..;...........jKEEEKK;:.G....,.....:KKEWWWWKKKKKKKWKK####WKKKKEEEGL .,;tfLGGGGDDDDDDDDDD *
*jti. ...:. .:,GEEKKKKKWKKKKKWWWWWWWW####WWWWWWWWWKKKWWKii;fDLGDK: EEi:E:.............EKEEKK;;..L...........KKKWWWWKKKKKKKWKK####WKKKWKEEDf .,;tfGGGGDDDDDDDDDDD *
*jti. ...:. ,EEKKKKKWWKKKKKWWWWWWWWW###WWWWWWWWKKKKfWWLt;i,. fi EG..D:.............EKEKK;;..t....:.......KWKWWWWKKKKKKKWKK####WKKKWEEEDf:. .,;tfGGGGDDDDDDDDDDD *
*jti. ...:. GEEKKKKKWKKKKKWWWWWWWWW####WWWWWWWKKKKKt;KKEfff .;t.................KKKKi;:..GtGGfG.......KWWWWWWKKKKKKKWKK###WWWKKKKEEEf,,: .,;tfGGGGDDDDDDDDDDD *
*jti. ...:. GEKKKKKWWKKKKKWWWWWWWWWW##WWWWWWWKKKKKKt;EiKKKK, ...t................jEKKG;;..,.....,LGi....KWWWWWWKKKKKKWKKKW####WKKKKKEEL,,,:. .,;tfGGGDDDDDDDDDDDD *
*jti. ...:. .GEEKKKKKWKKKKKWWWWWWWWWW###WWWWWWWKKKKKKtiE::tGG........................EEEj;;...,.........:D..DKWWWWWWKKKKK#KKW###W#WKKKKKEEfj:,,,:. .,;tfGGGDDDDDDDDDDDD *
*jti. ...:. DEKKKKKWWKKKKKWWWWWWWWW####WWWWWWWKKKKKKiiE:::.::.......................EEi;;...j.....f......:iDKWWWWWWKKKKK#WW######WKKKKKEELG :,,,,:. .,;tfGGGDDDDDDDDDDDD *
*jti. ...:. fEEKKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKK;tE::..........................DD;.;,.::......;........EWWWWWWWKKKKW#WW#####WWKKKWKKELG .:,,,:::,;tfGGGDDDDDDDDDDDD *
*jti. ...:. .DEKEKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKE,iD::..........................D..,;.,;tLffi...........DWDWWWW#KKKWWWWW#####W#KKKWKEEGL .:,;,,,;tfGGGDDDDDDDDDDDD *
*jti. ...:. ;EEKKKKWWKKKKKWWWWWW#WWWW####WWWWWWKKKKKEL:iD:..........................j ..;..;;:.....i,........DKtWWWWWKKWWWWWW#####WWWKKKEKEDf .:,;;;itfGGGDDDDDDDDDDDD *
*jti. ...:. DEKKKKKWWKKKKWWWWWWW#WWWW####WWWWWWKKKKKEj:iG...............................:....................GKiWWWWWKKWW#WWW######WWKKKKKEEf .,;iitfGGGDDDDDDDDDDDD *
*jti. ...:.:EKKKKKWWKKKKKWWWWWWW#WWW#####WWWWWKWKKKKEi:if:.................................iEKEKKKKKKDj......DKiWWWWWKWK##WW#######WWKKK:KEEL .:;itfGGGDDDDDDDDDDDD *
*jji. ...:,DEEKKKWWWKWKKWWWWWWWWWWWW#####WWWWWWWKKKKEi:it..................................j. KKKKKKKKKKKf..DKiWWWWWKWW##WW#######WWKKK,KEEf .,;tfGGGDDDDDDDDDDDD *
*jji. ..L:iDEEKKKWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKKi.i;.................................. . KKKWWWWWWWWK..DGiWWWWWKK##WWW#####W#WWKKKjEKEL, .:;tfGGGDDDDDDDDDDDD *
*jji. .f:::EEEKKKWWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKK;.i,.................................:: KKEKWWWWWWfWK..EiiWWWWWKWW#WW##########KKKD,KELj .:;tfGGDDDDDDDDDDDDD *
*jji. .t::::,DEEKKKWWKKKKWWWWWWWWW#WWWW#####WWWWKKWKKKEK;.i:.................................GDDEEEKKKWWWWWtWWD.E;iWWWWWW###WW#########WWKKK.EEDG .:;tfGGGDDDDDDDDDDDD *
*jji. . j..::::EKEKKKWWWKKKKWWWWWWWWW#WWW######WWWWKKWKKKEK;.t:.................................ELLEDDEEEWWWWEtWK,.KiiWWWWWW###W##########WWKKK:EEEG .;tjfLLGDDDDDDDDDDDDDDD *
*jji. i.::::::,EEEKKWWWKKKKKWWWWWWWWW#WWW#####WWWWWKWKKKKEE,.t..................................DfiEGDDDEEKKKttKWG.KiiWWWWW##WWW##########WWKKK:fEEL ,fGGGDDDDDDDDEEEDDDDDDDDDD *
*jji. .;:..:::::DEEEKKWWWKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKED,.t..................................ifjDDGGEGDKK.ttKKE.DiWWWWW###WW##########WWWKKK:.KELiLGGGGDDDDDDDDDDDDEEEDDDDDDD *
*jji. i.:.::::::,KEEKKWWWKKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKEL:.j..................................GGf,;ifLLED .iiKKi:fWWWWWW##W#W##########WWWKKK:.KKLGGDDDDDDDDDDDDDDDDEDDEEDDDDD *
*jji. .j:.::::::::EEEKKKWWWKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKf:.f..................................:EEfftf .,. ;iE,..jWWWWWWW###W############WWKKK,:KKGDDDDDDDDDDDDDDDDDDDDDDDEDDDD *
*jji. .:.::::::::,,EEEKKWWWKKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKt..G....................................EEELL; .j....tKWWWWWWW################WWWKKtfGKGEDDDDDDDDDDDDDDDDDDDDDDDEEDD *
*jji. :...:::::::,,jEEKKWWWWKKKKKKWWWWWWWWW##KWW#####KWWWWWWKKKKEi..D....................................:jEEE.........;KKWWWWWWWW#WW##W##########WWKKDLGKEKDDDDDDDDDDDDDDDDDDDDDDDDDED *
*jji. i:.::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWW##WWW#####WWWWWWWKKKKKi..D......................................:::::......,KKKWWWWWWWWW#####W########WWWKKKGGKKEGGGGGGGGDDDDDDDDDDDDDDDDDDE *
*jji. i..:::::::::,,tEEKKWWWWKKKKKWWWWWWWWWWW##WW######WWWWWWWKKKKKi..D......................................::::......:EKKKWWWWWWWWWWW##WW########W#WKKWGGKKGGGGGGGGGGGGGGGDDDDDDDDDDDDD *
*jji. .:::::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWWW##WW#####WWWWWWWWKKKKKi..D....................................:::::::::..tELii;KWWWWWWWWWW##WW######WWWWWWKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGDG *
*jjt. :.::::::::,,,,fEEKKWWWWKKKKKKWWWWWWWWWW###WW####WWWWWWW#WKKKKKi..D....................................:::::::.:.,;;;;;;,KKWWWWWWWWW#WW########WWWKKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGGG *
*jji. ;.::::::::,,,,;EEEKWWWWWKKKKKWWWWWWWWWWWW##WW###WKWWWWWK#WKKKKKi..G......................................:::::::,;;;;:...KKKWWWWWWWWWKWW#######WWWWKKGLGKDGGGGGGLLGGGGGGGGGGGGGGGGGGG *
*jjt. f.:::::::::,,,,fEEKKWWWWWKKKKKWWWWWWWWWWW###WW##WKKWWWWWW#WKKKKK;.jt........i.............................:::::::;j;;....:E.KKKWWWWWWWKWW#####W#WWWWKKLLGWEEGGGGGLGGGGGGGGGGGGGGGGGGGG *
*jjt. ...:::::::,,,,,;DEEKWWWWWKKKKKWWWWWWWWWWWW####WWWKKKWWWWWWWWKKKKK;.E;.........t.............................:::::ii;;.....D...KKWWWWWWWKWW#####WWEWWWKKGGGEKKGGGGGLGGGGGGGGGGGGGLGGGGGG *
*fji. ;.:::::::,,,,,;LEEKKWWWWWKKKKKWWWWWWWWWWWW####KWKKKKWWWWWWWWKKKKKi.D;..........j.............................:::tt;,.....:.....KKWWWWWWKWWWW##WWWGWWWKKGGGGKEGGGGGGGGGGGGGGGGGGGLLGGGGL *
*fji. t::::::::,,,,,,;EEEKWWWWWKKKKKKKWWWWWWWWWWW##WKWKKKKKWWWWWWWWKKKKKi:D;............j...........................::LL;,.............KKWWWWWKWWWWWWWWWGWWWKKGGGGKGGGGGGGGGGGGGGGGGGGGLLGGGGL *
*fjt: .:::::::,,,,,,,DEEKWWWWWWKKKKKKKWWWWWWWWWWWWKKWKKKKKKWWWWK#WWKKKKWitE;........... ............................:G;;:...............KKKWWKKWWWWWWWWWGWWWKKGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGL *
*fjji;:. .f:::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWWKKKKKKKKKKKWWKWWWWWKKKKWGKD;........................................L;;..................DKKWKKWWWWWWWWWGWWWKKDGGGKDGGGGGGGGGGGGGGGGGGGGGGGGGG *
*fjjtii;,:. :::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWKKKKKKKKKKKKWWWWWW#WWKKKKWiEj;......................................:i,;....,...............;KKEKWWWWWWWWWGKWWKKDDGGDEGGGDGGGGGDGGGGGGGGGGGGGGGG *
*fjtiiiii;;:. j::::::,,,,,,,;;EEEKWWWWW#KKKKKWWWWWWWWWKKKKKKWKKKKKKKWWWWWWWWWKKKKWtEL;:....................................;;;:...,;j................:KEEWWWWWWWWWDDWWKKDDDDDKDDDDDDDDDDDDDDDGGGGGGGGGGG *
*fjti;;iiii;;,:::::::,,,,,,,,;EEEKWWWWWWWKKKKWWWWWWWWKKKKKKKWKKKKKKKWWWWWWW#W#KKKKWEEii;...................................f;:....,;L...................EEKWWWWWWWWDDWWKKDDDDDKEDDDDDDDDDDDDDDDDDDDDDGGGG *
*fjt,,,;;;;ii;f::::::,,,,,,,;;EEKWWWWWWWKKKKKWWWKWWKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWKEij;:...............................:G;,.....,;f....................:tKKWWWWWWWDDWWKKDDDDDKKDDDDDDDDDDDDDDDDDDDDDDDDD *
*jjt. ..:,;;;;,::::,,,,,,,,;;GEEWWWWWWWWKKKKWKKWKKKKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWEDi;j;............................,Li;L;;;..,;;f........................KKKKWWWKDDWWKKDDDGDKKGGGGGGGGDGDDDDDDDDDDDDDDD *
*fjt. .:,,,:::::,,,,,,,;;;EEKWWWWWWWKKKKKKWKKKKKKKKKKKKKKKKKKKKKWKKKWKW##W#KKKKWEti;;G;........................tEEEL;;;;;;;;;;L..........................DKKKKKEDDWWKEDGftiLE;;;;itjLGGGGGGDDDDDDDDDDD *
*fjt. .j::::,,,,,,,;;;DEEWWWWWWWWKKKKWKKKKKKKKKKKKKKKKKKKKKKKWKKWWWK##W#KKKKKEii;;;L;...................iDEEEEEEKKi;j;;;;jD.....:......................,KKKKDGGEKKE:::::;E::::::::::,tLGGDDDDDDDDDD *
*fjt. .;:::,,,,,,,;;;;EEKWWWWWWWWKWKKKKKKKKKKKKKKKWKKKKKKKKKKWKKWWWW#WW#KKKKKKii;;;;f;.............:tDEEEEEKKKKKKKKEti;;;L...............................EEKf;:iKKE::::::E::::::::::::::ifDDDDDDDDD *
*fjt: :::,,,,,,,,;;;DEEWWWWWWWWWEKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####KKKKKEiii;;;;f,.........iDEEEEKKKKKKKKKKKKKKKf;iG......i..........................fK::::KKE::::::E::::::::::::::::,tGGDDDDD *
*fjt: t:::,,,,,,;;;;iDEKWWWWWWKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####WKKKKLiii;;;;;L,....,Li;EDEEEEKKKKKKKKKKKKKKKKiG......;:...........................:i:::KKE:::::,E,::::::::::::::::::iGDDDD *
*jjt. f::,,,,,,,;;;;GEEWWWWKEEKEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKWWWWW###WWKKKKiii;;;;;;;G,;L;;iiEEEEEEEKKKKKKKKKKKKKWWKE......;t.........:....................j::KEE:,::,,D,,::::,,,,,,:::::::::tDDD *
*fjt:. ,::,,,,,,,;;;;EEWWKEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKKWWWWW#W#KKKKKKiiiiii;;;;;i;;iiiEEKEEKKWKKKKKKKWKKKKKWWWGi;...;t......,;;;;,....................:,EEE,,,,,,D,,,,,,,,,,,,,,,,::,::::tG *
*fjt:. ,::,,,,,,,;;;;DEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWW#W#KKKKKKiiiii;;i;;;;;iiiKKKEKKKKWWKWWWWWWKKKKWWWWW;;;:;L.....;;;;;;;;;....................,KEE,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,; *
*fjt:. f:,,,,,,,;;;;jEDEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWW#W##KKKKKKiiiiiiii;i;;iiiEKKKKKKKKWKWWWWWWWWKKKWWWWWKi;;i.....,jEEfGi;;;;;...................EED,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,, *
*fjt:. .f::,,,,,,;;jEEDEEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWKWWWWW###KKKKKLiiiiiiiiiiiiiiEEKKKKKKKKWWWWWWWWWWWWKWWWWWW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DDEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWDGWWW###KKWWKWKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWW####WWWWWWWWWiiiiiiiiEEEEEEEEEEDi;i;;;;;;;;.....Lj;;;;;;i;iiiiii;;;;;;ii;;;;;;;;;;; *
***********************************************************************************************************************************************************************************************************/
void sort(int a[]) {
ArrayList<Integer> list=new ArrayList<>();
for(int x: a) list.add(x);
Collections.sort(list);
for(int i=0;i<a.length;i++) a[i]=list.get(i);
}
void sort(long a[]) {
ArrayList<Long> list=new ArrayList<>();
for(long x: a) list.add(x);
Collections.sort(list);
for(int i=0;i<a.length;i++) a[i]=list.get(i);
}
void ruffleSort(int a[]) {
Random rand=new Random();
int n=a.length;
for(int i=0;i<n;i++) {
int j=rand.nextInt(n);
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Arrays.sort(a);
}
void ruffleSort(long a[]) {
Random rand=new Random();
int n=a.length;
for(int i=0;i<n;i++) {
int j=rand.nextInt(n);
long temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Arrays.sort(a);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st==null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String s="";
try {
s=br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
long fact[];
long invFact[];
void init(int n) {
fact=new long[n+1];
invFact=new long[n+1];
fact[0]=1;
for(int i=1;i<=n;i++) {
fact[i]=mul(i,fact[i-1]);
}
invFact[n]=power(fact[n],mod-2);
for(int i=n-1;i>=0;i--) {
invFact[i]=mul(invFact[i+1],i+1);
}
}
long nCr(int n, int r) {
if(n<r || r<0) return 0;
return mul(fact[n],mul(invFact[r],invFact[n-r]));
}
long mul(long a, long b) {
return a*b%mod;
}
long add(long a, long b) {
return (a+b)%mod;
}
long power(long x, long y) {
long gg=1;
while(y>0) {
if(y%2==1) gg=mul(gg,x);
x=mul(x,x);
y/=2;
}
return gg;
}
// Functions
static long gcd(long a, long b) {
return b==0?a:gcd(b,a%b);
}
static int gcd(int a, int b) {
return b==0?a:gcd(b,a%b);
}
void print(int a[]) {
int n=a.length;
for(int i=0;i<n;i++) out.print(a[i]+" ");
}
void print(long a[]) {
int n=a.length;
for(int i=0;i<n;i++) out.print(a[i]+" ");
}
//Input Arrays
static void input(long a[], int n) {
for(int i=0;i<n;i++) {
a[i]=inp.nextLong();
}
}
static void input(int a[], int n) {
for(int i=0;i<n;i++) {
a[i]=inp.nextInt();
}
}
static void input(String s[],int n) {
for(int i=0;i<n;i++) {
s[i]=inp.next();
}
}
static void input(int a[][], int n, int m) {
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=inp.nextInt();
}
}
}
static void input(long a[][], int n, int m) {
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=inp.nextLong();
}
}
}
}
| np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.math.*;
public class Main {
public void dfs(ArrayList<Integer>[] graph,int[] visited,int source) {
}
public static void main(String[] args) throws Exception{
Reader.init(System.in);
PrintWriter out = new PrintWriter(System.out);
Main mm =new Main();
int n=Reader.nextInt();
int m=Reader.nextInt();
String s=Reader.next();
int[][] count=new int[m][m];
for(int i=1;i<n;i++) {
count[s.charAt(i)-'a'][s.charAt(i-1)-'a']++;
count[s.charAt(i-1)-'a'][s.charAt(i)-'a']++;
}
int[] dp=new int[1<<m];
Arrays.fill(dp, Integer.MAX_VALUE/10);
for(int i=0;i<m;i++) {
dp[1<<i]=0;
}
for(int i=0;i<(1<<m);i++) {
int extra=0;
for(int j=0;j<m;j++) {
if((i&(1<<j))>0) {
for(int k=0;k<m;k++) {
if(j!=k && (i&(1<<k))==0) {
extra+=count[j][k];
}
}
}
}
for(int j=0;j<m;j++) {
if((i&(1<<j))==0) {
dp[i|(1<<j)]=Math.min(dp[i|(1<<j)], dp[i]+extra);
}
}
}
out.println(dp[(1<<m)-1]);
out.close();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init() throws IOException {
reader = new BufferedReader(
new FileReader("input.txt"));
tokenizer = new StringTokenizer("");
}
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String nextLine() throws IOException{
return reader.readLine();
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class E {
static int g[][];
static int n, m;
static char[] s;
static int dp[], inf = (int) 2e9;
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
n = in.nextInt();
m = in.nextInt();
s = in.next().toCharArray();
g = new int[m][m];
for (int i = 1; i < n; i++) {
int x = s[i - 1] - 'a', y = s[i] - 'a';
if (x != y) {
g[x][y]++;
g[y][x]++;
}
}
dp = new int[1 << m];
Arrays.fill(dp, -1);
pw.println(solve(0, 0));
pw.close();
}
static int solve(int pos, int mask) {
if (pos >= m) return 0;
if (dp[mask] != -1) return dp[mask];
int min = inf;
for (int i = 0; i < m; i++) {
if (!check(mask, i)) {
int res = 0;
for (int j = 0; j < m; j++) {
if (check(mask, j)) res += g[i][j] * pos;
else res -= g[i][j] * pos;
}
res += solve(pos + 1, set(mask, i));
min = min(min, res);
}
}
return dp[mask] = min;
}
static boolean check(int N, int pos) {
return (N & (1 << pos)) != 0;
}
static int set(int N, int pos) {
return N = N | (1 << pos);
}
static int reset(int N, int pos) {
return N = N & ~(1 << pos);
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class E_fast {
static int g[][];
static int n, m;
static char[] s;
static int dp[], inf = (int) 2e9;
static int cost[][];
public static void main(String[] args) throws Exception {
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
n = in.nextInt();
m = in.nextInt();
s = in.next().toCharArray();
g = new int[m][m];
for (int i = 1; i < n; i++) {
int x = s[i - 1] - 'a', y = s[i] - 'a';
if (x != y) {
g[x][y]++;
g[y][x]++;
}
}
cost = new int[m][1 << m];
for (int i = 0; i < m; i++) {
int w = 0;
for (int j = 0; j < m; j++) w += g[i][j];
pre(i, 0, 0, -w);
}
dp = new int[1 << m];
Arrays.fill(dp, -1);
pw.println(solve(0, 0));
pw.close();
}
static void pre(int x, int pos, int mask, int w) {
if (pos >= m) {
cost[x][mask] = w;
return;
}
pre(x, pos + 1, mask, w);
pre(x, pos + 1, set(mask, pos), w + 2 * g[x][pos]);
}
static int solve(int pos, int mask) {
if (pos >= m) return 0;
if (dp[mask] != -1) return dp[mask];
int min = inf;
for (int i = 0; i < m; i++) {
if (!check(mask, i)) {
int res = cost[i][mask] * pos + solve(pos + 1, set(mask, i));
min = min(min, res);
}
}
return dp[mask] = min;
}
static boolean check(int N, int pos) {
return (N & (1 << pos)) != 0;
}
static int set(int N, int pos) {
return N = N | (1 << pos);
}
static int reset(int N, int pos) {
return N = N & ~(1 << pos);
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() throws Exception {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public String nextLine() throws Exception {
String line = null;
tokenizer = null;
line = reader.readLine();
return line;
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class E implements Runnable {
public static void main (String[] args) {new Thread(null, new E(), "_cf", 1 << 28).start();}
int n, m;
char[] str;
int[][] occs, cost;
int[] dp;
public void run() {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
System.err.println("");
n = fs.nextInt(); m = fs.nextInt();
byte[] str = fs.next().getBytes();
int[] occs = new int[1<<m];
for(int i = 0; i < n-1; i++) {
int l1 = str[i] - 'a';
int l2 = str[i+1] - 'a';
occs[(1<<l1) | (1<<l2)]++;
occs[(1<<l2) | (1<<l1)]++;
}
//cost[mask][v] = numPairs with v for some all bits on in mask
int all = (1<<m)-1;
cost = new int[m][1<<m];
for(int i = 0; i < m; i++) {
for(int mask = 1; mask < all; mask++) {
if(((1<<i)&mask) > 0) continue;
int lb = mask & (-mask);
int trail = Integer.numberOfTrailingZeros(lb);
int nmask = mask ^ lb;
cost[i][mask] = cost[i][nmask]+occs[1<<i | 1<<trail];
}
}
dp = new int[1<<m];
for(int mask = dp.length-2; mask >= 0; mask--) {
int addOn = 0;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
addOn += cost[nxt][mask];
}
int res = oo;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
int ret = addOn+dp[mask | (1<<nxt)];
res = min(res, ret);
}
dp[mask] = res;
}
System.out.println(dp[0]>>1);
out.close();
}
int oo = (int)1e9;
int min(int a, int b) {
if(a < b) return a;
return b;
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
/**
* @author egaeus
* @mail [email protected]
* @veredict Accepted
* @url <https://codeforces.com/problemset/problem/1238/E>
* @category dp
* @date 19/10/2019
**/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import static java.lang.Integer.bitCount;
import static java.lang.Integer.parseInt;
public class CF1238E {
static long[][] Q;
static int N;
static int M;
static long[] mem;
static long[] sums;
public static void main(String args[]) throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (String ln; (ln = in.readLine()) != null; ) {
StringTokenizer st = new StringTokenizer(ln);
N = parseInt(st.nextToken());
M = parseInt(st.nextToken());
char[] S = in.readLine().toCharArray();
Q = new long[M][M];
mem = new long[1 << M];
Arrays.fill(mem, -1);
for (int i = 1; i < N; i++)
Q[S[i - 1] - 'a'][S[i] - 'a'] = Q[S[i] - 'a'][S[i - 1] - 'a'] = Q[S[i - 1] - 'a'][S[i] - 'a'] + 1;
calculateSums();
for (int i = (1 << M) - 1; i >= 0; i--)
f(i);
System.out.println(f(0));
}
}
static void calculateSums() {
sums = new long[1 << M];
Arrays.fill(sums, -1);
for (int u = 0; u < (1 << M); u++) {
if (sums[u] == -1) {
sums[u] = 0;
for (int j = 0; j < M; j++)
for (int k = j + 1; k < M; k++)
if (((u & (1 << j)) == 0 && (u & (1 << k)) != 0) ||
((u & (1 << j)) != 0 && (u & (1 << k)) == 0))
sums[u] += Q[j][k];
int neg = (~u) & ((1 << M) - 1);
sums[neg] = sums[u];
}
}
}
static long f(int u) {
if (bitCount(u) == M) return 0;
if (mem[u] >= 0)
return mem[u];
long min = 10000000000L;
for (int i = 0; i < M; i++)
if ((u & (1 << i)) == 0) {
u = u | (1 << i);
min = Math.min(f(u) + sums[u], min);
u = u ^ (1 << i);
}
return mem[u] = min;
}
}
| np | 1238_E. Keyboard Purchase | CODEFORCES |
/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main{
/*********************************************Constants******************************************/
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long mod=(long)1e9+7;
static long mod1=998244353;
static boolean sieve[];
static ArrayList<Integer> primes;
static long factorial[],invFactorial[];
static ArrayList<Pair> graph[];
static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
/****************************************Solutions Begins***************************************/
static int n,m;
static long cost[][],dp[];
public static void main(String[] args) throws Exception{
String st[]=nl();
n=pi(st[0]);
m=pi(st[1]);
st=nl();
String str=st[0];
int mn=10000;
for(int i=0;i<n;i++){
mn=Math.min(mn,str.charAt(i));
}
cost=new long[m][m];
for(int i=1;i<n;i++){
int a1=str.charAt(i-1)-mn;
int a2=str.charAt(i)-mn;
if(a1==a2)continue;
cost[a1][a2]++;
cost[a2][a1]++;
}
int mm=1<<m;
dp=new long[mm];
Arrays.fill(dp,Long.MAX_VALUE/2);
dp[0]=0;
//debug(cost);
long cntbit[]=new long[mm];
int minbit[]=new int[mm];
for(int mask=1;mask<mm;mask++){
cntbit[mask]=1+cntbit[(mask&(mask-1))];
for(int i=0;i<m;i++){
if(((mask>>i)&1)!=0){
minbit[mask]=i;
break;
}
}
}
long cntcost[][]=new long[mm][m];
for(int mask=0;mask<mm;mask++){
for(int i=0;i<m;i++){
int b=minbit[mask];
cntcost[mask][i]=cntcost[mask^(1<<b)][i]+cost[i][b];
}
}
int yy=mm-1;
for(int mask=0;mask<mm;mask++){
long cnt=cntbit[mask];
for(int i=0;i<m;i++){
if(((mask>>i)&1)!=0){
long ans=cnt*(cntcost[mask][i]-cntcost[yy^mask][i]);
dp[mask]=Math.min(dp[mask],dp[mask^(1<<i)]+ans);
}
}
}
out.println(dp[mm-1]);
/****************************************Solutions Ends**************************************************/
out.flush();
out.close();
}
/****************************************Template Begins************************************************/
static String[] nl() throws Exception{
return br.readLine().split(" ");
}
static String[] nls() throws Exception{
return br.readLine().split("");
}
static int pi(String str) {
return Integer.parseInt(str);
}
static long pl(String str){
return Long.parseLong(str);
}
static double pd(String str){
return Double.parseDouble(str);
}
/***************************************Precision Printing**********************************************/
static void printPrecision(double d){
DecimalFormat ft = new DecimalFormat("0.000000000000000000000");
out.println(ft.format(d));
}
/**************************************Bit Manipulation**************************************************/
static void printMask(long mask){
System.out.println(Long.toBinaryString(mask));
}
static int countBit(long mask){
int ans=0;
while(mask!=0){
if(mask%2==1){
ans++;
}
mask/=2;
}
return ans;
}
/******************************************Graph*********************************************************/
static void Makegraph(int n){
graph=new ArrayList[n];
for(int i=0;i<n;i++){
graph[i]=new ArrayList<>();
}
}
static void addEdge(int a,int b){
graph[a].add(new Pair(b,1));
}
static void addEdge(int a,int b,int c){
graph[a].add(new Pair(b,c));
}
/*********************************************PAIR********************************************************/
static class PairComp implements Comparator<Pair>{
public int compare(Pair p1,Pair p2){
return p1.u-p2.u;
}
}
static class Pair implements Comparable<Pair> {
int u;
int v;
int index=-1;
public Pair(int u, int v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
if(index!=other.index)
return Long.compare(index, other.index);
return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/******************************************Long Pair*******************************************************/
static class PairCompL implements Comparator<Pairl>{
public int compare(Pairl p1,Pairl p2){
long a=p1.u*p2.v;
long b=p2.u*p1.v;
if(a>b){
return -1;
}
else if(a<b){
return 1;
}
else{
return 0;
}
}
}
static class Pairl implements Comparable<Pairl> {
long u;
long v;
int index=-1;
public Pairl(long u, long v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pairl other = (Pairl) o;
return u == other.u && v == other.v;
}
public int compareTo(Pairl other) {
if(index!=other.index)
return Long.compare(index, other.index);
return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/*****************************************DEBUG***********************************************************/
public static void debug(Object... o) {
if(!oj)
System.out.println(Arrays.deepToString(o));
}
/************************************MODULAR EXPONENTIATION***********************************************/
static long modulo(long a,long b,long c) {
long x=1;
long y=a%c;
while(b > 0){
if(b%2 == 1){
x=(x*y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return x%c;
}
/********************************************GCD**********************************************************/
static long gcd(long x, long y)
{
if(x==0)
return y;
if(y==0)
return x;
long r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
/******************************************SIEVE**********************************************************/
static void sieveMake(int n){
sieve=new boolean[n];
Arrays.fill(sieve,true);
sieve[0]=false;
sieve[1]=false;
for(int i=2;i*i<n;i++){
if(sieve[i]){
for(int j=i*i;j<n;j+=i){
sieve[j]=false;
}
}
}
primes=new ArrayList<Integer>();
for(int i=0;i<n;i++){
if(sieve[i]){
primes.add(i);
}
}
}
/********************************************End***********************************************************/
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
char[] arr = br.readLine().toCharArray();
int[][] count = new int[m][m];
for (int i = 0; i < n - 1; i++) {
count[arr[i] - 'a'][arr[i + 1] - 'a']++;
count[arr[i + 1] - 'a'][arr[i] - 'a']++;
}
int[] memo = new int[1 << m];
Arrays.fill(memo, (int) 1e9);
memo[0] = 0;
for (int msk = 0; msk < 1 << m; msk++) {
for (int c = 0; c < m; c++) {
if ((msk & 1 << c) != 0)
continue;
int temp = 0;
for (int i = 0; i < m; i++) {
if (i == c)
continue;
if ((msk & 1 << i) != 0) {
temp += count[c][i];
} else
temp -= count[c][i];
}
memo[msk | 1 << c] = Math.min(memo[msk | 1 << c], temp*Integer.bitCount(msk) + memo[msk]);
}
}
System.out.println(memo[(1 << m) - 1]);
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run(){
out.println(work());
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return b==0?a:gcd(b,a%b);
}
long work() {
int n=in.nextInt();
int m=in.nextInt();
String str=in.next();
long[] dp=new long[1<<m];
long[][] cnt=new long[m][m];
long[] rec=new long[1<<m];//记录每次移动的一位怎加的值,减少重复计算
for(int i=1;i<n;i++) {
int n1=str.charAt(i-1)-'a';
int n2=str.charAt(i)-'a';
cnt[n1][n2]++;
cnt[n2][n1]++;
}
for(int i=1;i<1<<m;i++) {
dp[i]=9999999999L;
long v=0;
int b=0;//最低位的1
for(int j=0;j<m;j++) {
if((i&(1<<j))>0) {
b=j;
break;
}
}
for(int j=0;j<m;j++) {
if((i&(1<<j))==0) {
v+=cnt[b][j];
}else {
if(b!=j)v-=cnt[b][j];
}
}
v+=rec[i-(1<<b)];
for(int j=0;j<m;j++) {
if((i&(1<<j))>0) {
dp[i]=Math.min(dp[i], dp[i-(1<<j)]+v);
}
}
rec[i]=v;
}
return dp[(1<<m)-1];
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next()
{
if(st==null || !st.hasMoreElements())
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
/**
* @author Juan Sebastian Beltran Rojas
* @mail [email protected]
* @veredict
* @url https://codeforces.com/problemset/problem/1238/E
* @category dp
* @date 19/10/2019
**/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import static java.lang.Integer.bitCount;
import static java.lang.Integer.parseInt;
public class CF1238E {
static long[][] Q;
static int N;
static int M;
static long[] mem;
static long[] sums;
public static void main(String args[]) throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (String ln; (ln = in.readLine()) != null; ) {
StringTokenizer st = new StringTokenizer(ln);
N = parseInt(st.nextToken());
M = parseInt(st.nextToken());
char[] S = in.readLine().toCharArray();
Q = new long[M][M];
mem = new long[1 << M];
Arrays.fill(mem, -1);
for (int i = 1; i < N; i++)
Q[S[i - 1] - 'a'][S[i] - 'a'] = Q[S[i] - 'a'][S[i - 1] - 'a'] = Q[S[i - 1] - 'a'][S[i] - 'a'] + 1;
calculateSums();
for (int i = (1 << M) - 1; i >= 0; i--)
f(i);
System.out.println(f(0));
}
}
static void calculateSums() {
sums = new long[1 << M];
Arrays.fill(sums, -1);
for (int u = 0; u < (1 << M); u++) {
if (sums[u] == -1) {
sums[u] = 0;
for (int j = 0; j < M; j++)
for (int k = j + 1; k < M; k++)
if (((u & (1 << j)) == 0 && (u & (1 << k)) != 0) ||
((u & (1 << j)) != 0 && (u & (1 << k)) == 0))
sums[u] += Q[j][k];
int neg = (~u) & ((1 << M) - 1);
sums[neg] = sums[u];
}
}
}
static long f(int u) {
if (bitCount(u) == M) return 0;
if (mem[u] >= 0)
return mem[u];
long min = 10000000000L;
for (int i = 0; i < M; i++)
if ((u & (1 << i)) == 0) {
u = u | (1 << i);
min = Math.min(f(u) + sums[u], min);
u = u ^ (1 << i);
}
return mem[u] = min;
}
}
| np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class E implements Runnable {
public static void main (String[] args) {new Thread(null, new E(), "_cf", 1 << 28).start();}
int n, m;
char[] str;
int[][] occs, cost;
int[] dp;
public void run() {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
System.err.println("");
n = fs.nextInt(); m = fs.nextInt();
str = fs.next().toCharArray();
occs = new int[m][m];
for(int i = 0; i < n-1; i++) {
occs[str[i]-'a'][str[i+1]-'a']++;
occs[str[i+1]-'a'][str[i]-'a']++;
}
//cost[mask][v] = numPairs with v for some all bits on in mask
int all = (1<<m)-1;
cost = new int[m][1<<m];
for(int i = 0; i < m; i++) {
for(int mask = 1; mask < all; mask++) {
if(((1<<i)&mask) > 0) continue;
int lb = mask & (-mask);
int trail = Integer.numberOfTrailingZeros(lb);
int nmask = mask ^ lb;
cost[i][mask] = cost[i][nmask]+occs[i][trail];
}
}
dp = new int[1<<m];
for(int mask = dp.length-2; mask >= 0; mask--) {
int addOn = 0;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
addOn += cost[nxt][mask];
}
int res = oo;
for(int nxt = 0; nxt < m; nxt++) {
if(((1<<nxt)&mask) > 0) continue;
int ret = addOn+dp[mask | (1<<nxt)];
res = min(res, ret);
}
dp[mask] = res;
}
System.out.println(dp[0]);
out.close();
}
int oo = (int)1e9;
int min(int a, int b) {
if(a < b) return a;
return b;
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
} | np | 1238_E. Keyboard Purchase | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
// editorial sol
public class cf1238e {
public static void main(String[] args) throws IOException {
int n = rni(), m = ni(), cnt[][] = new int[m][m], dp[] = new int[1 << m], all = (1 << m) - 1;
char[] s = rcha();
for (int i = 1; i < n; ++i) {
++cnt[s[i] - 'a'][s[i - 1] - 'a'];
++cnt[s[i - 1] - 'a'][s[i] - 'a'];
}
fill(dp, IBIG);
dp[0] = 0;
int cnt_bit[] = new int[1 << m], min_bit[] = new int[1 << m], d[][] = new int[1 << m][m];
for (int mask = 1; mask <= all; ++mask) {
cnt_bit[mask] = 1 + cnt_bit[mask & (mask - 1)];
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) > 0) {
min_bit[mask] = i;
break;
}
}
}
for (int mask = 1; mask <= all; ++mask) {
for (int i = 0; i < m; ++i) {
d[mask][i] = d[mask ^ (1 << min_bit[mask])][i] + cnt[i][min_bit[mask]];
}
}
for (int mask = 0; mask <= all; ++mask) {
for (int i = 0; i < m; ++i) {
if ((mask & (1 << i)) > 0) {
continue;
}
int pos = cnt_bit[mask], next = mask | (1 << i);
dp[next] = min(dp[next], dp[mask] + pos * (d[mask][i] - d[all ^ next][i]));
}
}
prln(dp[all]);
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// graph util
static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}
static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}
static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}
static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}
static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}
static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rg(List<List<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdg(List<List<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rsg(List<Set<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdsg(List<Set<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static void pryesno(boolean b) {prln(b ? "yes" : "no");};
static void pryn(boolean b) {prln(b ? "Yes" : "No");}
static void prYN(boolean b) {prln(b ? "YES" : "NO");}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}} | np | 1238_E. Keyboard Purchase | CODEFORCES |