# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
429404 | SansPapyrus683 | 은행 (IZhO14_bank) | Cpython 3 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class bank {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer initial = new StringTokenizer(read.readLine());
int peopleNum = Integer.parseInt(initial.nextToken());
int noteNum = Integer.parseInt(initial.nextToken());
int[] people = Arrays.stream(read.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] banknotes = Arrays.stream(read.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] leftover = new int[1 << noteNum];
int[] peopleCovered = new int[1 << noteNum];
Arrays.fill(leftover, -1);
Arrays.fill(peopleCovered, -1);
leftover[0] = 0;
peopleCovered[0] = 0;
for (int s = 0; s < (1 << noteNum); s++) {
for (int last = 0; last < noteNum; last++) {
if ((s & (1 << last)) == 0) {
continue;
}
int prev = s & ~(1 << last);
if (peopleCovered[prev] == -1) {
continue;
}
int new_amt = leftover[prev] + banknotes[last];
int curr_target = people[peopleCovered[prev]];
if (new_amt < curr_target) {
peopleCovered[s] = peopleCovered[prev];
leftover[s] = new_amt;
} else if (new_amt == curr_target) {
peopleCovered[s] = peopleCovered[prev] + 1;
leftover[s] = 0;
}
}
if (peopleCovered[s] == peopleNum) {
System.out.println("YES");
return;
}
}
System.out.println("NO");
}
}