Submission #486554

#TimeUsernameProblemLanguageResultExecution timeMemory
486554john256Cloud Computing (CEOI18_clo)Java
54 / 100
3068 ms14612 KiB
import java.io.*;
import java.util.*;
public class clo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        ArrayList<int[]> logs = new ArrayList<>();
        int totalC = 0;
        for (int i=0; i<N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            logs.add(new int[]{0, 0, 0});
            logs.get(i)[0] = Integer.parseInt(st.nextToken());
            logs.get(i)[1] = Integer.parseInt(st.nextToken());
            logs.get(i)[2] = -Integer.parseInt(st.nextToken());
            totalC += logs.get(i)[0];
        }
        int m = Integer.parseInt(br.readLine());
        for (int i=N; i<m+N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            logs.add(new int[]{0, 0, 0});
            logs.get(i)[0] = -Integer.parseInt(st.nextToken());
            logs.get(i)[1] = Integer.parseInt(st.nextToken());
            logs.get(i)[2] = Integer.parseInt(st.nextToken());
        }
        Collections.sort(logs, (x, y) -> y[1]-x[1]);
        long[][] dp = new long[2][1+totalC];
        Arrays.fill(dp[1], Long.MIN_VALUE);
        dp[1][0] = 0;
        for (int i=0; i<N+m; i++) {
        	//dp[i%2] = dp[1-i%2].clone();
            for(int x=0; x<=totalC; x++)
            	dp[i%2][x] = dp[1-i%2][x];
        	
        	for (int c=0; c<=totalC; c++) {
                if (c-logs.get(i)[0]<0 || c-logs.get(i)[0]>totalC || 
                		dp[1-i%2][c-logs.get(i)[0]]==Long.MIN_VALUE) continue;
                dp[i%2][c] = Math.max(dp[i%2][c], dp[1-i%2][c-logs.get(i)[0]] + logs.get(i)[2]);
            }
        }
        long ans = -1;
        for (int i=0; i<=totalC; i++)
            ans = Math.max(ans, dp[(N+m-1)%2][i]);
        System.out.println(ans);
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...