Submission #1342590

#TimeUsernameProblemLanguageResultExecution timeMemory
1342590ziad3ssam10Feast (NOI19_feast)C++20
100 / 100
436 ms35648 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using orderedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ll long long
#define ld long double
#define endl '\n'
#define wady                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);

void files() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

int const N = 3e5 + 5;
pair<int, int> dp[N][2];
int vis[N][2], timer;
int n, k, a[N];
int lamda;

pair<int, int> rec(int idx, int lst) {
    if (idx == n) return {0, 0};
    auto &ret = dp[idx][lst];
    if (vis[idx][lst] == timer) return ret;
    vis[idx][lst] = timer;
    auto leave = rec(idx + 1, 0);
    auto take = rec(idx + 1, 1);
    take.first += a[idx];
    if (!lst) {
        take.first -= lamda;
        take.second--;
    }
    return ret = max(leave, take);
}

void solve() {
    cin >> n >> k;
    for (int i = 0; i < n; i++)cin >> a[i];
    int l = 0, r = 1e15;
    int ans = 0;
    while (l <= r) {
        int mid = l + (r - l) / 2;
        lamda = mid;
        timer++;
        auto [cur,cnt] = rec(0, 0);
        int temp = -cnt;
        if (temp > k)l = mid + 1;
        else {
            ans = (cur + (__int128_t)mid * k);
            r = mid - 1;
        }
    }
    cout << ans << endl;
}


signed main() {
    wady
 
    int t = 1;
    int tc = 1;
    while (t--)
        solve();
}

Compilation message (stderr)

feast.cpp: In function 'void files()':
feast.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen("in.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
feast.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     freopen("out.txt", "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...