-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathB.cpp
executable file
·37 lines (31 loc) · 853 Bytes
/
B.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int plates(int N, int K, int P, vector<vector<int>>& st) {
int dp[N + 1][P + 1] = {}, prefix[N + 1][K + 1] = {};
// prefix sum array
for (int i = 1; i <= N; i++)
for (int j = 1; j <= K; j++)
prefix[i][j] = st[i - 1][j - 1] + prefix[i][j - 1];
// dynamic programming
for (int i = 1; i <= N; i++)
for (int j = 1; j <= P; j++)
for (int k = 0; k <= min(j, K); k++)
dp[i][j] = max(prefix[i][k] + dp[i - 1][j - k], dp[i][j]);
return dp[N][P];
}
int main() {
int T;
cin >> T;
for (int x = 1; x <= T; x++) {
int N, K, P;
cin >> N >> K >> P;
vector<vector<int>> st(N, vector<int>(K));
for (int i = 0; i < N; i++)
for (int j = 0; j < K; j++)
cin >> st[i][j];
cout << "Case #" << x << ": " << plates(N, K, P, st) << endl;
}
return 0;
}