結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー 37zigen37zigen
提出日時 2019-12-15 01:32:58
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,035 bytes
コンパイル時間 2,352 ms
コンパイル使用メモリ 76,112 KB
実行使用メモリ 465,928 KB
最終ジャッジ日時 2023-09-10 19:04:37
合計ジャッジ時間 9,896 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
60,244 KB
testcase_01 AC 123 ms
57,372 KB
testcase_02 AC 135 ms
56,384 KB
testcase_03 AC 129 ms
56,012 KB
testcase_04 AC 125 ms
55,784 KB
testcase_05 AC 123 ms
55,772 KB
testcase_06 AC 135 ms
55,836 KB
testcase_07 AC 133 ms
56,140 KB
testcase_08 AC 141 ms
55,572 KB
testcase_09 AC 313 ms
68,364 KB
testcase_10 AC 223 ms
65,652 KB
testcase_11 AC 279 ms
67,732 KB
testcase_12 AC 1,084 ms
164,984 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
			}
		}
		SegTree[] seg=new SegTree[K+1];
		for(int i=0;i<=K;++i){
			seg[i]=new SegTree(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]+seg[i-a[j][0]].query(0,j));
				}
				if(j+1<a.length){
					seg[i].update(j+1,dp[i][j]+a[j+1][1]);
				}
			}
		}
		System.out.println(dp[K][N-1]);
	}
	
	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
	
0