Advertisement
informaticage

Pranzo dalla nonna OIS

Mar 7th, 2023
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <limits>
  4.  
  5. using namespace std;
  6.  
  7. int solve(vector<int> &V, int k, int sum, int i) {
  8.   if (sum >= k) return sum;
  9.   if (i == V.size()) return numeric_limits<int>::max();
  10.  
  11.   int eat = solve(V, k, sum + V.at(i), i + 1);
  12.   int no_eat = solve(V, k, sum, i + 1);
  13.  
  14.   return min (eat, no_eat);
  15. }
  16.  
  17. int main() {
  18.   freopen("input.txt", "r", stdin);
  19.   freopen("output.txt", "w", stdout);
  20.  
  21.   int N, K;
  22.  
  23.   cin >> N >> K;
  24.  
  25.   vector<int> V(N);
  26.   for (int &x : V) {
  27.     cin >> x;
  28.   }
  29.  
  30.   cout << solve(V, K, 0, 0);
  31.   return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement