Submission #991734

#TimeUsernameProblemLanguageResultExecution timeMemory
991734CallieBank (IZhO14_bank)Java
27 / 100
75 ms27584 KiB
import java.io.*;
import java.util.*;

public class bank {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(in.readLine());
		
		int people = Integer.parseInt(st.nextToken());
		int banknotes = Integer.parseInt(st.nextToken());
		int[] plist = new int[people];
		int[] blist = new int[banknotes];
		
		st = new StringTokenizer(in.readLine());
		for(int i = 0; i < people; i++) {
			plist[i] = Integer.parseInt(st.nextToken());
		}
		
		st = new StringTokenizer(in.readLine());
		for(int i = 0; i < banknotes; i++) {
			blist[i] = Integer.parseInt(st.nextToken());
		}
		
		int[][] dp = new int[1 << banknotes][2];
		for(int i = 0; i < (1 << banknotes); i++) {
			for(int j = 0; j < banknotes; j++) {
				if((i & (1 << j)) != 0) continue;
				if(dp[i][0] == people) continue;
				
				int p = dp[i][0];
				int b = dp[i][1];
				
				if(b + blist[j] < plist[p]) {
					b += blist[j];
				}
				else if(b + blist[j] == plist[p]) {
					p++;
					b = 0;
				}
				
				if(p > dp[i ^ (1 << j)][0]) {
					dp[i^ (1 << j)][0] = p;
					dp[i^ (1 << j)][1] = b;
				}
				else if(p == dp[i ^ (1 << j)][0] && b > dp[i ^ (1 << j)][1]) {
					dp[i ^ (1 << j)][1] = b;
				}
			}
		}
		
		for(int i = 0; i < (1 << banknotes); i++) {
			if(dp[i][0] == people - 1 && dp[i][1] == blist[banknotes - 1]) {
				System.out.println("YES");
				return;
			}
			if(dp[i][0] == people) {
				System.out.println("YES");
				return;
			}
		}
		
		System.out.println("NO");
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...