# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1212393 | nmhung | Feast (NOI19_feast) | C++20 | 108 ms | 11008 KiB |
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define rf if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
//#define in ({int x = 0; int c = getchar(), n = 0; for(; !isdigit(c); c = getchar()) n = (c == '-'); for(; isdigit(c); c = getchar()) x = x * 10 + c - '0'; n ? -x : x;})
#define bit(i, mask) (((mask) >> (i)) & 1)
#define on(i, mask) ((mask) | (1LL << (i)))
#define off(i, mask) ((mask) & (~(1LL << (i))))
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define plx pair<ll, int>
#define vi vector<int>
#define vvi vector<vi>
#define all(a) (a).begin(), (a).end()
#define len(x) ((int)(x).size())
#define pb push_back
#define endl '\n'
#define ub(x, i) upper_bound(all(x), i) - x.begin()
#define lb(x, i) lower_bound(all(x), i) - x.begin()
#define name "feast"
template<typename T1, typename T2> bool mini(T1 &a, T2 b)
{if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b)
{if(a < b) a = b; else return 0; return 1;}
const int mod = 1e9+7;
const int inf = 1e9+9;
const ll oo = 1e18l+7;
const int M = 5e5+6;
const int N = 3e5+6;
const int LOG = 31 - __builtin_clz(N);
int n, k, a[N];
pair<ll, int> dp[N][2];
void inp(){
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
}
pair<ll, int> calc(ll lmb){
dp[1][0] = {0, 0};
dp[1][1] = {a[1] - lmb, 1};
for(int i = 2; i <= n; i++){
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][1].fi + a[i], dp[i - 1][1].se),
make_pair(dp[i - 1][0].fi + a[i] - lmb, dp[i - 1][0].se + 1));
}
return max(dp[n][0], dp[n][1]);
}
void proc(){
ll l = 0, r = 1e18, lmb = 0;
while(l <= r){
ll mid = (l + r) >> 1;
pair<ll, int> tmp = calc(mid);
if(tmp.se >= k){
lmb = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << calc(lmb).fi + lmb * k;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
rf
int test = 1;
//cin >> test;
while(test--){
inp();
proc();
}
cerr << "Time elapsed: " << TIME << "s" << endl;
return 0;
}
Compilation message (stderr)
# | 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... |