#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
void input(int & n, int & m, unsigned long long & k, vector<vector<unsigned long long>> & pancakes) {
cin >> n >> m >> k;
pancakes.resize(n+1, vector<unsigned long long>(m+1, 0));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> pancakes[i][j];
pancakes[i][j] += pancakes[i][j-1];
}
}
}
unsigned long long get_result(int const n, int const m, unsigned long long const k, vector<vector<unsigned long long>> const & pancakes) {
vector<vector<unsigned long long>> max_results(n+1, vector<unsigned long long>(k+1, 0));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
max_results[i][j] = max_results[i-1][j];
for(int l = 1; l <= j && l <= m; l++) {
unsigned long long cur_result = pancakes[i][l] + max_results[i-1][j-l];
if(cur_result > max_results[i][j]) {
max_results[i][j] = cur_result;
}
}
}
}
return max_results[n][k];
}
void solve() {
int n, m;
unsigned long long k;
vector<vector<unsigned long long>> pancakes;
input(n, m, k, pancakes);
cout << get_result(n, m, k, pancakes) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
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 | #include <iostream> #include <vector> #include <numeric> using namespace std; void input(int & n, int & m, unsigned long long & k, vector<vector<unsigned long long>> & pancakes) { cin >> n >> m >> k; pancakes.resize(n+1, vector<unsigned long long>(m+1, 0)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin >> pancakes[i][j]; pancakes[i][j] += pancakes[i][j-1]; } } } unsigned long long get_result(int const n, int const m, unsigned long long const k, vector<vector<unsigned long long>> const & pancakes) { vector<vector<unsigned long long>> max_results(n+1, vector<unsigned long long>(k+1, 0)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= k; j++) { max_results[i][j] = max_results[i-1][j]; for(int l = 1; l <= j && l <= m; l++) { unsigned long long cur_result = pancakes[i][l] + max_results[i-1][j-l]; if(cur_result > max_results[i][j]) { max_results[i][j] = cur_result; } } } } return max_results[n][k]; } void solve() { int n, m; unsigned long long k; vector<vector<unsigned long long>> pancakes; input(n, m, k, pancakes); cout << get_result(n, m, k, pancakes) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |
English