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>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 3e5 + 13;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, K;
ll arr[MAXN], pref[MAXN];
ll dp[MAXN], num[MAXN];
void check(ll P) //penalty P
{
int best = 0; ll bestval = 0;
dp[0] = 0;
num[0] = 0;
FOR(i, 0, N)
{
dp[i + 1] = dp[i];
num[i + 1] = num[i];
//or you do like k...i
ll cur = bestval + pref[i + 1] - P;
if (cur > dp[i + 1])
{
dp[i + 1] = cur;
num[i + 1] = num[best] + 1;
}
if (dp[i + 1] - pref[i + 1] > bestval)
{
bestval = dp[i + 1] - pref[i + 1];
best = i + 1;
}
}
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
FOR(i, 0, N)
{
cin >> arr[i];
pref[i + 1] = pref[i] + arr[i];
}
ll lo = 0, hi = 4e14;
while(hi > lo)
{
ll mid = (hi + lo) >> 1;
check(mid);
if (num[N] > K) lo = mid + 1;
else hi = mid;
}
check(lo);
cout << dp[N] + lo * num[N] << '\n';
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... |