이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
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... |