Advertisement
Josif_tepe

Untitled

May 11th, 2022
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. import java.util.Collection;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class MAin {
  6.     private static int n;
  7.     private static int[] niza;
  8.  
  9.     static int rec(int x) {
  10.         if(x == 0) {
  11.             return 0;
  12.         }
  13.         int result = (int) 2e9;
  14.         for(int i =0 ; i < n; i++) {
  15.             if(x - niza[i] >= 0) {
  16.                 result = Math.min(result, rec(x - niza[i]) + 1);
  17.             }
  18.         }
  19.         return result;
  20.     }
  21.     public static void main(String[] args) {
  22.         Scanner sc = new Scanner(System.in);
  23.         n = sc.nextInt();
  24. niza = new int [n];
  25.         for(int i = 0; i < n; i++) {
  26.             niza[i] = sc.nextInt();
  27.         }
  28.         int suma = sc.nextInt();
  29.         System.out.println(rec(suma));
  30.     }
  31.  
  32. }
  33. // 50- 10 - 20 = 20
  34. // 60 + 100 +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement