forked from pranavanurag/SPOJSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABA12C.cpp
More file actions
51 lines (41 loc) · 645 Bytes
/
Copy pathABA12C.cpp
File metadata and controls
51 lines (41 loc) · 645 Bytes
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
#include <iostream>
using namespace std;
int L, P[101], n, Memo[101];
#define INF 1000000
void Refresh()
{
for (int i = 0; i <= 100; i++)
Memo[i] = -INF;
}
int DP(int i)
{
if (Memo[i] >= -1)
return Memo[i];
if (i <= 1)
Memo[i] = P[i];
else
{
Memo[i] = INF;
for (int j = 1; j <= n; j++)
if (i >= j && P[j] != -1 && DP(i-j) != -1)
Memo[i] = min(Memo[i], P[j] + DP(i-j));
if (Memo[i] == INF)
Memo[i] = -1;
}
return Memo[i];
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while (t--)
{
Refresh();
cin>>L>>n;
for (int i = 1; i <= n; i++)
cin>>P[i];
cout<<DP(n)<<endl;
}
return 0;
}