제출 #1290891

#제출 시각아이디문제언어결과실행 시간메모리
1290891escobrandFeast (NOI19_feast)C++20
0 / 100
62 ms11844 KiB
#include <bits/stdc++.h>

using namespace std;
#define all(v) v.begin(),v.end()
#define pb push_back
#define ll long long
#define ld long double
#define fi first
#define se second
#define mk make_pair
typedef pair<ll,ll> pii;

const int maxn = 3e5 + 10;
const ll inf = 1e17;
ll a[maxn];
pii dp[maxn][2];
int n,k;
pii cal(ll cost)
{
    for(int i = 1;i<=n;i++)
    {
        dp[i][0] = max(dp[i-1][0],dp[i-1][1]);
        dp[i][1] = max( mk(dp[i-1][0].fi + a[i] - cost,dp[i-1][0].se+1),
                        mk(dp[i-1][1].fi + a[i], dp[i-1][1].se ));
    }
    return max(dp[n][0],dp[n][1]);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    dp[0][1] = mk(-inf,0);
    
    cin>>n>>k;
    
    for(int i = 1;i<=n;i++)cin>>a[i];
    
    ll l = 0,r = 1e13;
    
    while(l<r)
    {
        ll mid = (l + r + 1)/2;
        pii ans = cal(mid);
        if(ans.se>k)l=mid;
        else r = mid-1;
    }
    
    pii ans = cal(l+1);
    cout<<ans.fi+ans.se * l;
    
    return 0;
}
#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...