Submission #213074

#TimeUsernameProblemLanguageResultExecution timeMemory
213074qkxwsmPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
250 ms15336 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

const int MAXN = 500013;
const int INF = 1000000007;

int N;
ll arr[MAXN];
ll net[MAXN];
ll ans;
vl vec;

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N;
    FOR(i, 1, N + 1)
    {
        int a, b;
        cin >> a >> b;
        arr[i] = a - b;
    }
    //you want to make everything >= 0, and you can move smth 1 left or 1 right at a time.
    //+1 -1 => +1 +0
    //-1 +1 => -1 +0
    FOR(i, 1, N + 1)
    {
        arr[i] += arr[i - 1];
    }
    FOR(i, 1, N)
    {
        if (arr[i] < 0)
        {
            ans -= arr[i];
            arr[i] = 0;
        }
        if (arr[i] > arr[N])
        {
            ans += (arr[i] - arr[N]);
            arr[i] = arr[N];
        }
        ans += arr[i];
    }
    priority_queue<ll> pq;
    FOR(i, 1, N)
    {
        pq.push(arr[i]);
        pq.push(arr[i]);
        pq.pop();
    }
    while(!pq.empty())
    {
        ans -= (pq.top());
        pq.pop();
    }
    //make the sequence increasing. or equal. in each move you can change smth by 0 or 1.
    cout << ans << '\n';
    return 0;
    //make all arr's positive, what's the cost?

}
#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...