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.StringTokenizer;
public class bank {
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()), m = Integer.parseInt(st.nextToken());
int[] goal = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) goal[i] = Integer.parseInt(st.nextToken());
int[] ar = new int[m];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < m; i++) ar[i] = Integer.parseInt(st.nextToken());
//0 - which salary trying to pay, 1 - how much currently left
int[][] dp = new int[(1 << m)][2];
for (int i = 0; i < (1 << m); i++) for (int j = 0; j < 2; j++) dp[i][j] = -1;
dp[0][0] = 0;
dp[0][1] = 0;
for (int i = 0; i < (1 << m); i++) {
for (int j = 0; j < m; j++) {
if((i & (1 << j)) != 0 && dp[(i ^ (1 << j))][0] != -1){
if(dp[(i ^ (1 << j))][1] + ar[j] == goal[dp[(i ^ (1 << j))][0]]){
dp[i][0] = dp[(i ^ (1 << j))][0] + 1;
dp[i][1] = 0;
}else if(dp[(i ^ (1 << j))][1] + ar[j] < goal[dp[(i ^ (1 << j))][0]]){
dp[i][0] = dp[(i ^ (1 << j))][0];
dp[i][1] = dp[(i ^ (1 << j))][1] + ar[j];
}
if(dp[i][0] == n){
System.out.println("YES");
System.exit(0);
}
}
}
}
System.out.println("NO");
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |