Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- const int maxn = 105;
- const int INF = 1e8;
- int n, k;
- int dp[maxn][1101];
- int kg[maxn];
- int rek(int friends, int left) {
- if(friends == 0 and left > 0) {
- return INF;
- }
- if(left == 0) {
- return 0;
- }
- if(dp[friends][left] != -1) {
- return dp[friends][left];
- }
- int res = INF;
- for(int i = 0; i < n; i++) {
- if(kg[i] != -1) {
- if(left - (i + 1) >= 0) {
- res = min(res, rek(friends - 1, left - (i + 1)) + kg[i]);
- }
- }
- }
- return dp[friends][left] = res;
- }
- int main()
- {
- int t;
- cin >> t;
- while(t--) {
- cin >> k >> n;
- for(int i = 0; i < n; i++) {
- cin >> kg[i];
- }
- for(int i = 0; i <= k; i++) {
- for(int j = 0; j <= 1005; j++) {
- dp[i][j] = -1;
- }
- }
- int res = rek(k, n);
- if(res >= INF) {
- cout << -1 << endl;
- }
- else {
- cout << res << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement