제출 #728088

#제출 시각아이디문제언어결과실행 시간메모리
728088OzySafety (NOI18_safety)C++17
100 / 100
228 ms21012 KiB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define lli long long int
#define pll pair<lli,lli>

//para sol
#define pend first
#define ord second

lli n,d,a,b,offset;
pll sol;
multiset<lli> menores,mayores;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> d;
    offset = 0;
    rep(i,1,n) {

        cin >> a;
        if (!menores.empty()) b = (*menores.rbegin());
        else b = 0;
        b -= offset;
        if (a > b) {
            b = a - offset;
            mayores.insert(b);
            mayores.insert(b);

            auto it = mayores.begin();
            b = (*it);
            mayores.erase(it);
            b += offset*2;
            menores.insert(b);
        }
        else {
            b = a + offset;
            menores.insert(b);
            menores.insert(b);

            auto it = menores.end();
            it--;
            b = (*it);
            menores.erase(it);
            b -= offset*2;
            mayores.insert(b);
        }

        sol.pend++;
        sol.ord -= a;
        //ahora recorro
        sol.ord -= sol.pend*d;

        offset += d;
    }

    auto it = mayores.end();
    do {
        it--;

        sol.pend--;
        sol.ord += offset+(*it);
    } while (it != mayores.begin());

    cout << sol.ord;

    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...