Cod sursa(job #3344576)

Utilizator hrazvanHarsan Razvan hrazvan Data 3 martie 2026 11:13:50
Problema Euro Scor 100
Compilator cpp-64 Status done
Runda Lista lui wefgef Marime 1.6 kb
#include <bits/stdc++.h>
using namespace std;
int main() {
    freopen("euro.in", "r", stdin);
    freopen("euro.out", "w", stdout);
    int n, t;
    int64_t s = 0, r = 0;
    cin >> n >> t;

    std::deque<std::pair<int64_t, int64_t>> dq;
    dq.push_back({0, 0});

    auto eval = [](auto a, int64_t i) -> int64_t {
        return a.first * i + a.second;
    };

    auto intersect = [](auto a, auto b) -> double {
        return 1.0 * (a.second - b.second) / (b.first - a.first);
    };

    auto ever = [&](auto a, auto b, int i) -> bool {
        if (a.first == b.first) 
            return a.second > b.second;
        if (a.first > b.first) 
            return true;
        return a.second > b.second && intersect(a, b) >= i;
    };

    auto redundant = [&](auto a, auto b, auto c, int i) -> bool {
        if (!ever(c, b, i)) 
            return false;
        if (!ever(b, c, i)) 
            return true;
        return intersect(c, a) <= intersect(b, a);
    };

    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        s += x;

        while (dq.size() > 1 && eval(dq[0], i) <= eval(dq[1], i)){
            dq.pop_front();
        }

        r = 1LL * s * i - t + eval(dq[0], i);
        auto new_elem = std::make_pair((int64_t)-s, r);

        while (dq.size() > 1 && redundant(dq[dq.size() - 2], dq.back(), new_elem, i)){
            dq.pop_back();
        }
        if (dq.size() == 1 && !ever(dq.back(), new_elem, i)){
            dq.pop_back();
        }
        if (dq.empty() || ever(new_elem, dq.back(), i)){
            dq.push_back(new_elem);
        }
    }
    cout << r << '\n';
    return 0;
}