fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. class CD
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. int[][] d = new int[210][110];
  9. short[] arr = new short[110];
  10. Scanner in = new Scanner(System.in);
  11. while (in.hasNext()){
  12. short n = in.nextShort();
  13. short k = in.nextShort();
  14. for (int i=1; i<=k; i++) arr[i] = in.nextShort();
  15. for (int i=1; i<=k; i++)
  16. for (int j=0; j<=n; j++)
  17. if (arr[i]<=j){
  18. if (d[j][i-1]< arr[i]+d[j-arr[i]][i-1]) d[j][i]=arr[i]+d[j-arr[i]][i-1];
  19. else d[j][i]=d[j][i-1];
  20. }
  21. else d[j][i]=d[j][i-1];
  22. System.out.printf("sum: " + d[n][k]);
  23. }
  24. }
  25. }
Success #stdin #stdout 0.17s 321344KB
stdin
5 3 1 3 4
stdout
sum: 5