結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー 37zigen37zigen
提出日時 2019-12-15 01:38:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,138 ms / 2,000 ms
コード長 2,030 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 76,128 KB
実行使用メモリ 354,160 KB
最終ジャッジ日時 2023-09-10 19:14:29
合計ジャッジ時間 36,535 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,908 KB
testcase_01 AC 121 ms
55,860 KB
testcase_02 AC 123 ms
55,796 KB
testcase_03 AC 123 ms
56,068 KB
testcase_04 AC 124 ms
56,072 KB
testcase_05 AC 120 ms
55,804 KB
testcase_06 AC 126 ms
55,764 KB
testcase_07 AC 125 ms
55,444 KB
testcase_08 AC 129 ms
55,848 KB
testcase_09 AC 263 ms
65,964 KB
testcase_10 AC 185 ms
60,960 KB
testcase_11 AC 227 ms
61,752 KB
testcase_12 AC 376 ms
126,220 KB
testcase_13 AC 796 ms
261,944 KB
testcase_14 AC 321 ms
88,260 KB
testcase_15 AC 349 ms
107,020 KB
testcase_16 AC 443 ms
118,180 KB
testcase_17 AC 498 ms
136,948 KB
testcase_18 AC 122 ms
55,676 KB
testcase_19 AC 122 ms
56,092 KB
testcase_20 AC 121 ms
56,116 KB
testcase_21 AC 173 ms
58,524 KB
testcase_22 AC 157 ms
58,172 KB
testcase_23 AC 158 ms
57,936 KB
testcase_24 AC 934 ms
351,968 KB
testcase_25 AC 941 ms
352,556 KB
testcase_26 AC 954 ms
352,208 KB
testcase_27 AC 958 ms
353,220 KB
testcase_28 AC 1,053 ms
354,160 KB
testcase_29 AC 1,048 ms
352,752 KB
testcase_30 AC 121 ms
55,796 KB
testcase_31 AC 121 ms
56,660 KB
testcase_32 AC 154 ms
58,128 KB
testcase_33 AC 164 ms
60,196 KB
testcase_34 AC 181 ms
62,880 KB
testcase_35 AC 955 ms
353,552 KB
testcase_36 AC 933 ms
352,244 KB
testcase_37 AC 934 ms
351,644 KB
testcase_38 AC 929 ms
352,688 KB
testcase_39 AC 931 ms
353,636 KB
testcase_40 AC 959 ms
351,752 KB
testcase_41 AC 962 ms
352,392 KB
testcase_42 AC 979 ms
352,056 KB
testcase_43 AC 962 ms
352,124 KB
testcase_44 AC 961 ms
351,988 KB
testcase_45 AC 984 ms
352,372 KB
testcase_46 AC 1,063 ms
353,924 KB
testcase_47 AC 1,044 ms
352,976 KB
testcase_48 AC 1,138 ms
352,356 KB
testcase_49 AC 1,034 ms
352,324 KB
testcase_50 AC 1,028 ms
353,840 KB
testcase_51 AC 1,099 ms
351,444 KB
testcase_52 AC 847 ms
352,048 KB
testcase_53 AC 951 ms
352,204 KB
testcase_54 AC 868 ms
352,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	class SegTree{
		int n;
		int[] v;
		
		public SegTree(int n){
			this.n=1;
			while(this.n<n)this.n=2*this.n;
			v=new int[2*this.n-1];
			Arrays.fill(v,0);
		}
		
		void update(int k,int val){
			k+=n-1;
			v[k]=Math.max(v[k],val);
			while(k>0){
				k=(k-1)/2;
				v[k]=Math.max(v[2*k+1],v[2*k+2]);
			}
		}
		
		int query(int a,int b){
			return query(0,n,a,b,0);	
		}
		
		int query(int l,int r,int a,int b,int k){
			if(a<=l&&r<=b)
				return v[k];
			else if(r<=a||b<=l)
				return 0;
			else{
				int vl=query(l,(l+r)/2,a,b,2*k+1);
				int vr=query((l+r)/2,r,a,b,2*k+2);
				return Math.max(vl,vr);
			}
		}
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int K=sc.nextInt();
		int[] P=new int[N+1];
		int[] D=new int[N+1];
		int[][] a=new int[N+1][2];
		for(int i=0;i<N;++i){
			P[i]=sc.nextInt();
			D[i]=sc.nextInt();
		}
		P[N]=0;
		D[N]=0;
		for(int i=0;i<=N;++i){
			a[i][0]=P[i];
			a[i][1]=D[i];
		}
		Arrays.sort(a,new Comparator<int[]>(){
			public int compare(int[] a,int[] b){
				if(a[0]!=b[0])
					return Integer.compare(a[0],b[0]);
				else
					return Integer.compare(a[1],b[1]);
			}
		});
		++N;
		int[][] dp=new int[K+1][N];
		int[][] ma=new int[K+1][N];
		for(int i=0;i<=K;++i){
			for(int j=0;j<N;++j){
				dp[i][j]=-Integer.MAX_VALUE/3;
				ma[i][j]=-Integer.MAX_VALUE/3;
			}
		}
		int[][] seg=new int[K+1][N+10];
		for(int i=0;i<=K;++i){
			for(int j=0;j<N;++j){
				dp[i][j]=Math.max((i>0?dp[i-1][j]:0),(j>0?dp[i][j-1]:0));
				ma[i][j]=Math.max((i>0?ma[i-1][j]:0),(j>0?ma[i][j-1]:0));
				if(i-a[j][0]>=0){
					dp[i][j]=Math.max(dp[i][j],a[j][1]+(j>0?seg[i-a[j][0]][j-1]:0));
				}
				if(j+1<N)
					seg[i][j+1]=Math.max(seg[i][j+1],dp[i][j]+a[j+1][1]);
					seg[i][j+1]=Math.max(seg[i][j+1],seg[i][j]);
			}
		}
		System.out.println(dp[K][N-1]);
	}
	
	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
	
0