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 <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5+10;
int n, k, a[N];
pair <int, int> dp[N];
pair <int, int> calculate(int c)
{
pair <int, int> Max = {0, 0};
dp[0] = {0, 0};
for (int i=1; i<=n; i++)
{
dp[i]=dp[i-1];
if (Max.first + a[i] - c > dp[i].first || (Max.first + a[i] - c == dp[i].first && Max.second + 1 < dp[i].second))
{
dp[i].first = Max.first + a[i]-c;
dp[i].second = Max.second + 1;
}
if (dp[i].first - a[i] > Max.first || (dp[i].first - a[i] == Max.first && dp[i].second < Max.second))
{
Max.first = dp[i].first - a[i];
Max.second = dp[i].second;
}
}
return dp[n];
}
int solve()
{
int left=1; int right=1e14;
while (left <= right)
{
int mid = (left + right) / 2;
pair <int, int> val = calculate(mid);
if (val.second == k) return val.first + k * mid;
if (val.second < k) right = mid - 1;
else left = mid + 1;
}
pair <int, int> val = calculate(left);
return val.first + val.second * left ;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("BANQUET.inp", "r", stdin);
// freopen("BANQUET.out", "w", stdout);
cin>>n>>k;
for (int i=1; i<=n; i++)
{
cin>>a[i]; a[i]+=a[i-1];
}
cout<<solve();
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... |