Submission #569840

#TimeUsernameProblemLanguageResultExecution timeMemory
569840four_specksBuilding Bridges (CEOI17_building)C++17
100 / 100
57 ms9772 KiB
#include <bits/stdc++.h>

using namespace std;

inline namespace
{
    struct Line
    {
        mutable long k, m, p;
        bool operator<(const Line &o) const { return k < o.k; }
        bool operator<(long x) const { return p < x; }
    };

    struct LineContainer : multiset<Line, less<>>
    {
        // (for doubles, use inf = 1/.0, div(a,b) = a/b)
        static const long inf = LONG_MAX;
        long div(long a, long b)
        { // floored division
            return a / b - ((a ^ b) < 0 && a % b);
        }
        bool isect(iterator x, iterator y)
        {
            if (y == end())
                return x->p = inf, 0;
            if (x->k == y->k)
                x->p = x->m > y->m ? inf : -inf;
            else
                x->p = div(y->m - x->m, x->k - y->k);
            return x->p >= y->p;
        }
        void add(long k, long m)
        {
            auto z = insert({k, m, 0}), y = z++, x = y;
            while (isect(y, z))
                z = erase(z);
            if (x != begin() && isect(--x, y))
                isect(x, y = erase(y));
            while ((y = x) != begin() && (--x)->p >= y->p)
                isect(x, erase(y));
        }
        long query(long x)
        {
            assert(!empty());
            auto l = *lower_bound(x);
            return l.k * x + l.m;
        }
    };

} // namespace

void solve()
{
    int n;
    cin >> n;

    vector<long> h(n), w(n);
    for (long &x : h)
        cin >> x;
    for (long &x : w)
        cin >> x;

    long add = 0;
    for (long x : w)
        add += x;

    vector<long> dp(n);
    LineContainer cht;
    dp[0] = w[0];
    cht.add(2 * h[0], dp[0] - h[0] * h[0]);
    for (int i = 1; i < n; i++)
    {
        dp[i] = cht.query(h[i]) - h[i] * h[i] + w[i];
        cht.add(2 * h[i], dp[i] - h[i] * h[i]);
    }

    cout << -dp[n - 1] + add << '\n';
}

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

    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...