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;
typedef long long ll;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
pair<ll, int> check(vector<int> &a, ll cost) {
pair<ll, int> n = {0, 0}, y = {LLONG_MIN, 0};
for (int i = 0; i < sz(a); i++) {
pair<ll, int> n2 = n;
if (y.first != LLONG_MIN) n2 = max(n2, y);
pair<ll, int> y2 = {n.first + a[i] - cost, n.second + 1};
if (y.first != LLONG_MIN) {
y2 = max(y2, {y.first + a[i], y.second});
y2 = max(y2, {y.first + a[i] - cost, y.second + 1});
}
swap(n, n2);
swap(y, y2);
}
auto res = max(n, y);
if (cost == 0) res.second = sz(a);
return res;
}
ll search(vector<int> &a, int K, ll l, ll r) {
if (l > r) return LLONG_MIN;
ll mid = (l + r) / 2;
auto p = check(a, mid);
if (p.second >= K) {
ll m = search(a, K, mid + 1, r);
if (m != LLONG_MIN) return m;
return p.first + K * mid;
}
return search(a, K, l, mid - 1);
}
void solve() {
int N, K; cin >> N >> K;
vector<int> a(N);
for (int i = 0; i < a.size(); i++) cin >> a[i];
cout << search(a, K, 0, LLONG_MAX / 2) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
solve();
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'void solve()':
feast.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < a.size(); i++) cin >> a[i];
| ~~^~~~~~~~~~
# | 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... |