Submission #1265527

#TimeUsernameProblemLanguageResultExecution timeMemory
1265527nhphucPotatoes and fertilizers (LMIO19_bulves)C++20
100 / 100
109 ms16196 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 500500;
const long long inf = 1e18;

int n, a[N], b[N];
long long dif[N], c[N], ans = 0;
priority_queue<long long> pq;

int32_t main (){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    const string task = "test";
    if (fopen ((task + ".inp").c_str(), "r")){
        freopen ((task + ".inp").c_str(), "r", stdin);
        freopen ((task + ".out").c_str(), "w", stdout);
    }
    cin >> n;
    for (int i = 1; i <= n; ++i){
        cin >> a[i] >> b[i];
        dif[i] = dif[i - 1] + 1ll * (a[i] - b[i]);
    }
    for (int i = 1; i <= n; ++i){
        pq.push(dif[i]);
        pq.push(dif[i]);
        c[i] = pq.top();
        pq.pop();
    }
    for (int i = n; i >= 1; --i){
        c[i] = max(0ll, min({dif[n], c[i], (i < n ? c[i + 1] : inf)}));
        ans += abs(dif[i] - c[i]);
    }
    cout << ans << "\n";
}

Compilation message (stderr)

bulves.cpp: In function 'int32_t main()':
bulves.cpp:15:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |         freopen ((task + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bulves.cpp:16:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |         freopen ((task + ".out").c_str(), "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...