제출 #1270460

#제출 시각아이디문제언어결과실행 시간메모리
1270460proplayerFeast (NOI19_feast)C++20
67 / 100
64 ms12108 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int maxN = 1e6 + 5;
const int Mod = 1e9 + 7;
pair<ll, ll> dp[2][maxN];
ll n, k;
ll a[maxN];
void input()
{
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i];
}
pair<ll, ll> check(ll x)
{
    dp[0][0] = {0, 0};
    dp[1][0] = {-x, 1};
    //cerr << x << "\n";
    for (int i = 1; i <= n; ++i)
    {
        dp[0][i] = max(dp[1][i - 1], dp[0][i - 1]);
        dp[1][i] = max(make_pair(dp[1][i - 1].first + a[i], dp[1][i - 1].second), make_pair(dp[0][i - 1].first + a[i] - x, dp[0][i - 1].second + 1));
    }
    return max(dp[0][n], dp[1][n]);
}
void solve()
{
    ll l = -1e18, r = 1e18;
    ll res = 0;
    while (l <= r)
    {
        ll mid = (r + l) / 2;
        if (check(mid).second >= k)
        {
            res = mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    cout << max(check(res).first + k * res, 0ll);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen("C.inp","r",stdin);
    //freopen("C.out","w",stdout);
    input();
    solve();
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...