forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10086.cpp
62 lines (59 loc) · 1.14 KB
/
10086.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
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
int main()
{
ios::sync_with_stdio(false);
int T, S, N, M[35], X[35][35], tot, tmp, prev, O[35];
pair<long long, int> dp[35][305];
while (cin >> T >> S && (T + S))
{
cin >> N;
for (int i = 0; i < N; i += 1)
{
cin >> M[i];
X[i][0] = 0;
for (int j = 0; j < M[i]; j += 1)
{
cin >> X[i][j + 1];
}
for (int j = 1; j <= M[i]; j += 1)
{
cin >> tmp;
X[i][M[i] - j] += tmp;
}
}
tot = 0;
dp[0][0] = make_pair(0, 0);
for (int n = 1; n <= N; n += 1)
{
for (int i = 0; i <= min(T, tot + M[n - 1]); i += 1)
{
dp[n][i].X = 100000000;
for (int j = max(i - M[n - 1], 0); j <= min(i, tot); j += 1)
{
if (dp[n][i].X > dp[n - 1][j].X + X[n - 1][i - j])
{
dp[n][i].X = dp[n - 1][j].X + X[n - 1][i - j];
dp[n][i].Y = i - j;
}
}
}
tot += M[n - 1];
}
cout << dp[N][T].X << '\n';
prev = T;
for (int i = N; i >= 1; i -= 1)
{
O[i] = dp[i][prev].Y;
prev -= dp[i][prev].Y;
}
for (int i = 1; i < N; i += 1)
{
cout << O[i] << ' ';
}
cout << O[N] << "\n\n";
}
return 0;
}