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 <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];
}
// Initialize the dp array with negative infinity
vector<long long> dp(K + 1, LLONG_MIN);
dp[0] = 0; // Base case: no plates and 0 people = 0 satisfaction
// Iterate through all plates
for (int i = 1; i <= N; ++i) {
// Start a temporary array for the next iteration
vector<long long> next_dp = dp;
// Maintain a variable to track the best value if starting a segment for person `j`
long long max_so_far = LLONG_MIN;
// Iterate over possible people in reverse to avoid overwriting our current dp values
for (int j = 1; j <= K; ++j) {
max_so_far = max(max_so_far, dp[j - 1]); // Find the best ending segment for `j-1`
next_dp[j] = max(next_dp[j], max_so_far + A[i]); // Update the current segment with the best value + current plate
}
dp = next_dp; // Update dp with our next_dp
}
// The final result is the maximum value for `K` people using up to N plates
cout << dp[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... |