Submission #278803

# Submission time Handle Problem Language Result Execution time Memory
278803 2020-08-21T23:18:04 Z KWang31 Bank (IZhO14_bank) Java 11
71 / 100
1000 ms 67740 KB
import java.io.*; import java.util.*;
public class bank {

    
    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()); 
        } 
    }
    static boolean[] visited;
    static int[] pref;
    static int[]b;
    static int M;
    static boolean[][] dp;
    static ArrayList<Integer> arl[];
    //SOS DP (SEE ANALYSIS)
    public static void main(String[] args) {
        FastReader br=new FastReader();
        int N=br.nextInt();
        M=br.nextInt();
        int[] a=new int[N];
        for (int i = 0; i < N; i++) {
            a[i]=br.nextInt();
        }
        b=new int[M];
        for (int i = 0; i < M; i++) {
            b[i]=br.nextInt();
        }
        dp=new boolean[N+1][1<<M];
        dp[0][0]=true;
        pref=new int[N+1];
        for (int i = 1; i <=N; i++) {
            pref[i]=pref[i-1]+a[i-1];
        }
        
        arl=new ArrayList[N+1];
        for (int i = 0; i <=N; i++) {
            arl[i]=new ArrayList<>();
        }
        arl[0].add(0);
        
        for (int j = 1; j <=N; j++) {
            //Look at arl[j-1]
            visited=new boolean[1<<M];
            for (int i : arl[j-1]) {
                visited[i]=true;
                dfs(i,M-1,j);
            }
            
        }
        
       StringBuilder sb=new StringBuilder();
       if(!arl[N].isEmpty()){
           sb.append("YES"); System.out.println(sb.toString());return;
       }
                                                                                    
        sb.append("NO"); System.out.println(sb.toString());
    }
    public static void dfs(int mask, int k, int i){
        //Finding all supermasks that differ than mask in the last k bits
        if(k>=0){
            dfs(mask,k-1,i);
            if((mask&(1<<k))==0 && !visited[mask|(1<<k)]){//If already visited, I don't need to visit
                
                visited[mask|(1<<k)]=true;
                if(val(mask|(1<<k))==pref[i]){
                    
                    dp[i][mask|(1<<k)]=true;
                    arl[i].add(mask|(1<<k));
                }
                dfs((mask|(1<<k)),k-1,i);
            }
        }
    }
    public static int val(int mask){
        int a=0;
        for (int i = 0; i < M; i++) {
            if((mask&(1<<i))>0){
                a+=b[i];
            }
        }
        return a;
    }
}

Compilation message

Note: bank.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# Verdict Execution time Memory Grader output
1 Correct 78 ms 10236 KB Output is correct
2 Correct 79 ms 10360 KB Output is correct
3 Correct 82 ms 10364 KB Output is correct
4 Correct 100 ms 11592 KB Output is correct
5 Correct 186 ms 14724 KB Output is correct
6 Correct 81 ms 10360 KB Output is correct
7 Correct 85 ms 10392 KB Output is correct
8 Correct 221 ms 15568 KB Output is correct
9 Correct 191 ms 14916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 10488 KB Output is correct
2 Correct 83 ms 10104 KB Output is correct
3 Correct 82 ms 10316 KB Output is correct
4 Correct 84 ms 10600 KB Output is correct
5 Correct 81 ms 10344 KB Output is correct
6 Correct 95 ms 10344 KB Output is correct
7 Correct 77 ms 10472 KB Output is correct
8 Correct 81 ms 10488 KB Output is correct
9 Correct 81 ms 10344 KB Output is correct
10 Correct 80 ms 10360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 11256 KB Output is correct
2 Correct 83 ms 10984 KB Output is correct
3 Correct 86 ms 10616 KB Output is correct
4 Correct 98 ms 12016 KB Output is correct
5 Correct 88 ms 11024 KB Output is correct
6 Correct 85 ms 11212 KB Output is correct
7 Correct 91 ms 10984 KB Output is correct
8 Correct 87 ms 10744 KB Output is correct
9 Correct 97 ms 11020 KB Output is correct
10 Correct 98 ms 11520 KB Output is correct
11 Correct 85 ms 11252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 10236 KB Output is correct
2 Correct 79 ms 10360 KB Output is correct
3 Correct 82 ms 10364 KB Output is correct
4 Correct 100 ms 11592 KB Output is correct
5 Correct 186 ms 14724 KB Output is correct
6 Correct 81 ms 10360 KB Output is correct
7 Correct 85 ms 10392 KB Output is correct
8 Correct 221 ms 15568 KB Output is correct
9 Correct 191 ms 14916 KB Output is correct
10 Correct 83 ms 10488 KB Output is correct
11 Correct 83 ms 10104 KB Output is correct
12 Correct 82 ms 10316 KB Output is correct
13 Correct 84 ms 10600 KB Output is correct
14 Correct 81 ms 10344 KB Output is correct
15 Correct 95 ms 10344 KB Output is correct
16 Correct 77 ms 10472 KB Output is correct
17 Correct 81 ms 10488 KB Output is correct
18 Correct 81 ms 10344 KB Output is correct
19 Correct 80 ms 10360 KB Output is correct
20 Correct 93 ms 11256 KB Output is correct
21 Correct 83 ms 10984 KB Output is correct
22 Correct 86 ms 10616 KB Output is correct
23 Correct 98 ms 12016 KB Output is correct
24 Correct 88 ms 11024 KB Output is correct
25 Correct 85 ms 11212 KB Output is correct
26 Correct 91 ms 10984 KB Output is correct
27 Correct 87 ms 10744 KB Output is correct
28 Correct 97 ms 11020 KB Output is correct
29 Correct 98 ms 11520 KB Output is correct
30 Correct 85 ms 11252 KB Output is correct
31 Correct 288 ms 18356 KB Output is correct
32 Correct 365 ms 21528 KB Output is correct
33 Correct 353 ms 28036 KB Output is correct
34 Correct 352 ms 34308 KB Output is correct
35 Correct 322 ms 36648 KB Output is correct
36 Correct 345 ms 67740 KB Output is correct
37 Correct 294 ms 18448 KB Output is correct
38 Correct 188 ms 21076 KB Output is correct
39 Execution timed out 1090 ms 38880 KB Time limit exceeded
40 Halted 0 ms 0 KB -