This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> A(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> A[i];
}
// DP array initialized with a very small value (negative infinity)
vector<vector<long long>> dp(N + 1, vector<long long>(K + 1, LLONG_MIN));
dp[0][0] = 0; // Base case
// Iterate over each possible plate
for (int i = 1; i <= N; ++i) {
for (int j = 0; j <= K; ++j) {
// If not taking the i-th plate for the j-th person
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
}
// Iterate over the number of people (from 1 to K)
for (int j = 1; j <= K; ++j) {
// Keep track of the sum for the current segment ending at `i`
long long current_sum = 0;
// Iterate back from `i` to track the best sum ending at `i` for person `j`
for (int x = i; x > 0; --x) {
current_sum += A[x];
dp[i][j] = max(dp[i][j], dp[x - 1][j - 1] + current_sum);
}
}
}
cout << dp[N][K] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |