#include <bits/stdc++.h>
#include <ext/random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pb push_back
#define ppb pop_back
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define vektor vector
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ull Mod = 1e9 + 7;
constexpr ull sqr = 547;
constexpr ld eps = 1e-9;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random(ll l, ll r) {if(l <= r) return uniform_int_distribution <ll> (l, r)(rng); return -1;}
void solve(){
ll n, k;
cin >> n >> k;
vector <ll> a(n + 1);
vector <vector <ll>> dp(n + 1, vector <ll> (2, 0)), cnt(n + 1, vector <ll> (2, 0));
dp[0][1] = -1;
for(int i = 1; i <= n; i ++) cin >> a[i];
ll l = 0, r = 1e9 * n, ans = 0;
while(l <= r){
ll mid = (l + r) >> 1;
dp[0][1] = mid - 1;
for(int i = 1; i <= n; i ++){
if(dp[i - 1][0] > dp[i - 1][1]){
dp[i][0] = dp[i - 1][0];
cnt[i][0] = cnt[i - 1][0];
}
else{
dp[i][0] = dp[i - 1][1];
cnt[i][0] = cnt[i - 1][1];
}
if(dp[i - 1][1] + a[i] == dp[i - 1][0] + a[i] - mid){
dp[i][1] = dp[i - 1][1] + a[i];
cnt[i][1] = max(cnt[i - 1][1], cnt[i - 1][0] + 1);
}
else if(dp[i - 1][1] + a[i] > dp[i - 1][0] + a[i] - mid){
dp[i][1] = dp[i - 1][1] + a[i];
cnt[i][1] = cnt[i - 1][1];
}
else{
dp[i][1] = dp[i - 1][0] + a[i] - mid;
cnt[i][1] = cnt[i - 1][0] + 1;
}
}
if(dp[n][0] < dp[n][1]) swap(cnt[n][0], cnt[n][1]);
if(cnt[n][0] >= k) l = mid + 1;
else r = mid - 1, ans = mid;
}
dp[0][1] = -1e9;
for(int i = 1; i <= n; i ++){
if(dp[i - 1][0] > dp[i - 1][1]){
dp[i][0] = dp[i - 1][0];
cnt[i][0] = cnt[i - 1][0];
}
else{
dp[i][0] = dp[i - 1][1];
cnt[i][0] = cnt[i - 1][1];
}
if(dp[i - 1][1] + a[i] == dp[i - 1][0] + a[i] - ans){
dp[i][1] = dp[i - 1][1] + a[i];
cnt[i][1] = max(cnt[i - 1][1], cnt[i - 1][0] + 1);
}
else if(dp[i - 1][1] + a[i] > dp[i - 1][0] + a[i] - ans){
dp[i][1] = dp[i - 1][1] + a[i];
cnt[i][1] = cnt[i - 1][1];
}
else{
dp[i][1] = dp[i - 1][0] + a[i] - ans;
cnt[i][1] = cnt[i - 1][0] + 1;
}
}
// cout << dp[0][1] + a[1] - ans;
cout << max(dp[n][0] + cnt[n][0] * ans, dp[n][1] + cnt[n][1] * ans);
}
main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if(fopen("COUNTSEQ.inp", "r")){
freopen("COUNTSEQ.inp", "r", stdin);
freopen("COUNTSEQ.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --){
solve();
cout << "\n";
}
}
컴파일 시 표준 에러 (stderr) 메시지
feast.cpp:117:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
117 | main(){
| ^~~~
feast.cpp: In function 'int main()':
feast.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen("COUNTSEQ.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | freopen("COUNTSEQ.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |