結果

問題 No.115 遠足のおやつ
ユーザー chiho_miyako
提出日時 2015-04-16 19:10:54
言語 Java
(openjdk 23)
結果
AC  
実行時間 322 ms / 5,000 ms
コード長 1,404 bytes
コンパイル時間 4,938 ms
コンパイル使用メモリ 83,232 KB
実行使用メモリ 106,680 KB
最終ジャッジ日時 2025-01-03 01:04:37
合計ジャッジ時間 12,447 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
   public static void main(String[] args) throws Exception {
       Scanner koko = new Scanner(System.in);
       int n = koko.nextInt();
       int d = koko.nextInt();
       int k = koko.nextInt();
       int mx = n*(n+1)/2;
       boolean[][][] price = new boolean[n+1][Math.max((n+1),k)+1][Math.max(mx,d)+1];
       price[0][0][0]=true;
       for(int x=0; x<n; x++){
           for(int y=0; y<=x; y++){
               for(int z=0; z<mx; z++){
                   if(price[x][y][z]){
                       price[x+1][y][z]=true;
                       price[x+1][y+1][z+x+1]=true;
                   }
               }
           }
       }
       boolean judge = false;
       for(int x=n; x>0; x--){
           if(price[x][k][d]){
               judge=true;
               int[] a = new int[k];
               int b =0;
               while(x>0){
                   if(x<=d&&price[x-1][k-1][d-x]){
                       a[b++]=x;
                       k--;
                       d=d-x;
                       x--;
                   }else{
                       x--;
                   }
               }
               Arrays.sort(a);
               for(int i=0; i<b; i++){
                   System.out.print(a[i]+" ");
               }
               break;
           }
       }
       if(!judge){
           System.out.println(-1);
       }
    }
}
0