이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import java.io.*;
import java.util.*;
public class clo {
static int n, m;
static Item[] arr;
static int[][] computer, work;
static long[][] dp;
public static void main(String[] args) throws IOException{
BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader file = new BufferedReader(new FileReader("file.in"));
//PrintWriter outfile = new PrintWriter (new BufferedWriter(new FileWriter("clo.out")));
n = Integer.parseInt(file.readLine());
computer = new int[n][3];
int sumCores = 0;
for (int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(file.readLine());
int c = Integer.parseInt(st.nextToken());
int f = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
computer[i] = new int[] {c, f, e};
}
m = Integer.parseInt(file.readLine());
arr = new Item[n+m];
work = new int[m][3];
for (int i=0; i<m; i++){
StringTokenizer st = new StringTokenizer(file.readLine());
int c = Integer.parseInt(st.nextToken());
int f = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
work[i] = new int[] {c, f, e};
arr[n+i] = new Item(c, f, e, 1); // Set order cores to negative, so we just need to sum all item cores to positive
}
for (int i=0; i<n; i++){
arr[i] = new Item(computer[i][0], computer[i][1], computer[i][2], 0); // Set ernings to negative, so we only take the profit
sumCores += computer[i][0];
}
Arrays.sort(arr);
dp = new long[n+m+1][sumCores+1];
for (int i=0; i<=n+m; i++){
Arrays.fill(dp[i], (int)-1e9);
}
dp[0][0] = 0;
for (int i=1; i<=n+m; i++){
for (int j=0; j<=sumCores; j++){
dp[i][j] = dp[i-1][j];
if (arr[i-1].t == 0){
if (j - arr[i-1].c >= 0){
dp[i][j] = Math.max(dp[i][j], dp[i-1][j - arr[i-1].c] - arr[i-1].earn);
}
}else{
if (j + arr[i-1].c <= sumCores){
dp[i][j] = Math.max(dp[i][j], dp[i-1][j + arr[i-1].c] + arr[i-1].earn);
}
}
}
}
long ans = 0;
for (int i=0; i<=sumCores; i++){
ans = Math.max(ans, dp[n+m][i]);
}
System.out.println(ans);
}
static class Item implements Comparable<Item>{
int c, f, earn, t;
public Item(int c, int f, int earn, int t){
this.c = c;
this.f = f;
this.earn = earn;
this.t = t;
}
public int compareTo(Item o){
if (o.f == this.f){
if (this.t == o.t){
return 0;
}
if (this.t == 0){
return 1;
}else{
return -1;
}
}
return Integer.compare(o.f, this.f);
}
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |