-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABA12C.cpp
53 lines (48 loc) · 933 Bytes
/
ABA12C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
int n,k;
int a[1001];
long long int dp[1010][1010];
long long int price(int packets, int kgs) {
if(kgs == k)
return 0;
if(kgs > k) {
return LLONG_MAX;
}
if(packets >= n && kgs < k) {
return LLONG_MAX;
}
if(dp[packets][kgs] != -1)
return dp[packets][kgs];
dp[packets][kgs] = LLONG_MAX;
for (int i = 1; i <= k; ++i)
{
if((kgs + i) > k)
continue;
long long ans = price(packets+1,kgs+i);
if(ans == LLONG_MAX)
continue;
if(a[i] != -1)
dp[packets][kgs]=min(dp[packets][kgs], a[i]+ans);
}
return dp[packets][kgs];
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
for (int i = 0; i < 1010; ++i)
{
for(int j = 0; j < 1010; ++j)
dp[i][j] = -1;
}
scanf("%d%d",&n,&k);
for (int i = 1; i <= k; ++i)
{
scanf("%d",&a[i]);
}
long long x = price(0,0);
printf("%lld\n", price(0,0) == LLONG_MAX ? -1 : price(0,0));
}
return 0;
}