제출 #1340525

#제출 시각아이디문제언어결과실행 시간메모리
1340525iamhereforfunFeast (NOI19_feast)C++20
100 / 100
85 ms1584 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 3e5 + 5;
const int M = 2e5 + 5;
const int LG = 20;
const int INF = 1e9 + 5;
const int K = 2;
const int C = 26;
const int B = 1000;
const int MOD = 998244353;

int n, k, a[N];
pair<long long, long long> dp1[2], dp2[2];

pair<long long, long long> cal(long long lmd)
{
    dp1[0] = {0, 0};
    dp1[1] = {-1e18, -1e18};
    for (int x = 1; x <= n; x++)
    {
        dp2[0] = max(dp1[0], dp1[1]);
        dp2[1] = max(pair<long long, long long>{dp1[0].first - lmd + a[x], dp1[0].second + 1}, {dp1[1].first + a[x], dp1[1].second});
        dp1[0] = dp2[0];
        dp1[1] = dp2[1];
    }
    // cout << lmd << "\n";
    // cout << dp1[0].first << " " << dp1[0].second << "\n";
    // cout << dp2[0].first << " " << dp2[0].second << "\n";
    return max(dp1[0], dp1[1]);
}

inline void solve()
{
    cin >> n >> k;
    for (int x = 1; x <= n; x++)
    {
        cin >> a[x];
    }
    long long l = 1, r = 1e13, b = 0;
    while (l <= r)
    {
        long long m = (l + r) / 2;
        if (cal(m).second >= k)
        {
            l = m + 1;
            b = m;
        }
        else
        {
            r = m - 1;
        }
    }
    cout << cal(b).first + k * b;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    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...