제출 #1053886

#제출 시각아이디문제언어결과실행 시간메모리
1053886SamAndBikeparking (EGOI24_bikeparking)C++17
40 / 100
1070 ms6236 KiB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005;
const int INF = 1000000009;

int n;
int x[N], y[N];
int cx[N], cy[N];

int stg(int p)
{
    for (int i = 1; i <= n; ++i)
    {
        cx[i] = x[i];
        cy[i] = y[i];
    }

    int ans = -p;

    int s = p;
    for (int i = n; i >= 1; --i)
    {
        if (x[i] >= s)
        {
            x[i] -= s;
            s = 0;
        }
        else
        {
            s -= x[i];
            x[i] = 0;
        }
    }
    assert(s == 0);

    s = p;
    for (int i = 1; i <= n; ++i)
    {
        if (y[i] >= s)
        {
            y[i] -= s;
            s = 0;
        }
        else
        {
            s -= y[i];
            y[i] = 0;
        }
    }
    assert(s == 0);

    int i = 1;
    int j = 1;
    while (i <= n && j <= n)
    {
        if (x[i] == 0)
        {
            ++i;
            continue;
        }
        if (y[j] == 0)
        {
            ++j;
            continue;
        }
        if (i > j)
        {
            for (int i = 1; i <= n; ++i)
            {
                x[i] = cx[i];
                y[i] = cy[i];
            }
            return -INF;
        }
        int minu = min(x[i], y[j]);
        if (x[i] >= y[j])
        {
            x[i] -= y[j];
            y[j] = 0;
        }
        else
        {
            y[j] -= x[i];
            x[i] = 0;
        }
        if (i < j)
            ans += minu;
    }

    for (int i = 1; i <= n; ++i)
    {
        x[i] = cx[i];
        y[i] = cy[i];
    }

    return ans;
}

void solv()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> x[i];
    for (int i = 1; i <= n; ++i)
        cin >> y[i];

    int s = 0;
    for (int i = 1; i <= n; ++i)
        s += x[i];
    for (int i = 1; i <= n; ++i)
        s -= y[i];
    assert(s >= 0);

    for (int i = n; i >= 1; --i)
    {
        if (x[i] >= s)
        {
            x[i] -= s;
            s = 0;
        }
        else
        {
            s -= x[i];
            x[i] = 0;
        }
    }
    assert(s == 0);

    for (int i = 1; i <= n; ++i)
        s += x[i];

    int ans = -INF;
    for (int p = 0; p <= s; ++p)
    {
        ans = max(ans, stg(p));
    }

    cout << ans << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    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...