Advertisement
Josif_tepe

Untitled

Nov 14th, 2023
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. const int maxn = 105;
  7. const int INF = 1e8;
  8. int n, k;
  9. int dp[maxn][1101];
  10. int kg[maxn];
  11. int rek(int friends, int left) {
  12.     if(friends == 0 and left > 0) {
  13.         return INF;
  14.     }
  15.     if(left == 0) {
  16.         return 0;
  17.     }
  18.     if(dp[friends][left] != -1) {
  19.         return dp[friends][left];
  20.     }
  21.     int res = INF;
  22.     for(int i = 0; i < n; i++) {
  23.         if(kg[i] != -1) {
  24.             if(left - (i + 1) >= 0) {
  25.                 res = min(res, rek(friends - 1, left - (i + 1)) + kg[i]);
  26.             }
  27.         }
  28.     }
  29.     return dp[friends][left] = res;
  30. }
  31. int main()
  32. {
  33.     int t;
  34.     cin >> t;
  35.    
  36.     while(t--) {
  37.         cin >> k >> n;
  38.         for(int i = 0; i < n; i++) {
  39.             cin >> kg[i];
  40.         }
  41.         for(int i = 0; i <= k; i++) {
  42.             for(int j = 0; j <= 1005; j++) {
  43.                 dp[i][j] = -1;
  44.             }
  45.         }
  46.         int res = rek(k, n);
  47.         if(res >= INF) {
  48.             cout << -1 << endl;
  49.         }
  50.         else {
  51.             cout << res << endl;
  52.         }
  53.     }
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement