Advertisement
vov44k

Untitled

Oct 16th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.  
  8.         int n = in.nextInt(), t = in.nextInt();
  9.         int[] arr = new int[n + 1], arr_v = new int[t];
  10.  
  11.         for (int i = 0; i < t; i++) {
  12.             arr_v[i] = in.nextInt();
  13.         }
  14.  
  15.         for (int i = 1; i < n + 1; i++) {
  16.             int min = 20000000;
  17.             for (int j = t - 1; j >= 0; j--) {
  18.                 if (i - arr_v[j] >= 0) {
  19.                     min = Math.min(min, arr[i - arr_v[j]]);
  20.                 }
  21.             }
  22.             arr[i] = 1 + min;
  23.         }
  24.  
  25.         if (arr[n] > 20000000) System.out.println(-1);
  26.         else System.out.println(arr[n]);
  27.  
  28.  
  29.         int e = n;
  30.         for (int i = arr[n]; i > 0; i--) {
  31.             for (int j = t - 1; j >= 0; j--) {
  32.                 if (e - arr_v[j] >= 0 && arr[e - arr_v[j]] == arr[e] - 1) {
  33.                     System.out.print(arr_v[j] + " ");
  34.                     e -= arr_v[j];
  35.                 }
  36.             }
  37.         }
  38.  
  39.         in.close();
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement