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 X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, b, a) for (int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define SQR(x) (1LL * (x) * (x))
#define MASK(x) (1LL << (x))
#define mxx max_element
#define mnn min_element
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 3e5 + 7;
long double dp[N];
int trace[N], n, k;
ll a[N];
pll dynamic_dp(long double cost) {
dp[0] = 0;
FOR(i, 1, n) dp[i] = -1e18;
long double tmp_val = 0;
int idx = 0;
FOR(i, 1, n) {
dp[i] = dp[i - 1];
if (maximize(dp[i], tmp_val + a[i] - cost)) trace[i] = idx;
if (maximize(tmp_val, dp[i - 1] - a[i])) idx = i;
}
ll cnt = 0, res = 0;
int i = n;
while (i > 0) {
if (abs(dp[i] - dp[i - 1]) < 1e-9) {
i--;
continue;
}
cnt++;
res+=a[i] - a[trace[i]];
i = trace[i] - 1;
}
return {cnt, res};
}
int main() {
// freopen("test.INP", "r", stdin);
// freopen("test.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>k;
FOR(i, 1, n) cin>>a[i], a[i]+=a[i - 1];
long double l = -1e12, r = 1e12;
ll res = 0;
FOR(i, 1, 100) {
long double mid = (l + r) / 2.0;
pll tmp = dynamic_dp(mid);
// exit(0);
if (tmp.X <= k) {
maximize(res, tmp.Y);
r = mid;
}
else l = mid;
}
cout<<res;
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... |