Submission #1291828

#TimeUsernameProblemLanguageResultExecution timeMemory
1291828CrabCNHFeast (NOI19_feast)C++20
100 / 100
92 ms12176 KiB
#include <bits/stdc++.h>

#define task   "taskname"

#define pii    pair <ll, ll>
#define fi     first
#define se     second
#define szf    sizeof
#define sz(s)  (int)((s).size())
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef long double ld;

template <class T> void minimize (T &t, T f) {if (t > f) t = f;}
template <class T> void maximize (T &t, T f) {if (t < f) t = f;}

const int maxN = 3e5 + 5;
const ld Eps = 1e-12;
const ll inf = 1e18 + 7;
const int Log = 20;
const int mod = 1e9 + 7;

// mt19937_64 rd (chrono :: steady_clock :: now ().time_since_epoch ().count ());
// int Rand (int l, int r) {return uniform_int_distribution <int> (l, r) (rd);}

int n, k;
ll a[maxN];
pii dp[maxN][2];

pii calc (ll lmd) {
    dp[1][0] = {0, 0};
    dp[1][1] = {a[1] - lmd, 1};
    for (int i = 2; i <= n; i ++) {
        dp[i][0] = max (dp[i - 1][0], dp[i - 1][1]);
        pii t1 = {dp[i - 1][0].fi + a[i] - lmd, dp[i - 1][0].se + 1};
        pii t2 = {dp[i - 1][1].fi + a[i], dp[i - 1][1].se};
        dp[i][1] = max (t1, t2);
    }
    return max (dp[n][0], dp[n][1]);
}

inline void Solve () {
    cin >> n >> k;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    ll l = 0, r = inf, res = 0;
    while (true) {
        if (l > r) {
            break;
        }
        ll mid = (l + r) >> 1;
        //cout << l << ' ' << r << ' ' << calc (mid).se << '\n';
        if (calc (mid).se >= k) {
            res = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    cout << calc (res).fi + res * k;
    return;
}

signed main () {
    cin.tie (nullptr) -> sync_with_stdio (false);
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int t = 1;
    //cin >> t;
    while (t --) {
        Solve ();
    } 
    return 0;
}
// Stress test : 
// Check constant :
// Check taskname :
// Check debug :
// Check subtask :

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:72:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:73:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...