Submission #1249339

#TimeUsernameProblemLanguageResultExecution timeMemory
1249339norman165Self Study (JOI22_ho_t2)C++20
62 / 100
232 ms16292 KiB
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define yes() cout << "YES\n"
#define no() cout << "NO\n"

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1e7 + 5e6;
const int mod1 = 998244353;
const int mod2 = 1e18 + 1;
const int mod3 = 1e9 + 9;
const int mod4 = 333333333;
const int mod5 = 200000;
const int mod6 = 10007;
const int k = 300;
const int w = 1e5;
const ld EPS = 1e-8;

int LOG = 30;

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

    vector<int> a(n), b(n);
    for (int& i : a) cin >> i;
    for (int& i : b) cin >> i;

    int l = 0;
    int r = 1e15;

    auto check = [&](int x) {
        vector<int> prime(n), cnt(n, m);

        for (int i = 0; i < n; i++) {
            int y = max(a[i], b[i]);
            int c = min(m, (x + y - 1) / y);

            cnt[i] -= c;
            prime[i] += c * y;
        }

        vector<int> need;
        vector<int> free;

        for (int i = 0; i < n; i++) if (prime[i] < x) need.push_back(i);
        for (int i = 0; i < n; i++) if (cnt[i] > 0) free.push_back(cnt[i]);
        int res = need.size();

        for (int& i : need) {
            int y = b[i];

            int must = x - prime[i];
            int gol = (must + y - 1) / y;

            while (free.size() && gol > 0) {
                int much = free.back();
                free.pop_back();

                if (gol < much) {
                    much -= gol;
                    gol = 0;
                } else {
                    gol -= much;
                    much = 0;
                }

                if (much > 0) free.push_back(much);
            }

            if (gol == 0) res--;
            else break;
        }

        return !res;
    };

    while (r - l > 1) {
        int mid = (l + r) / 2;

        if (check(mid)) l = mid;
        else r = mid;
    }

    cout << l << "\n";
}

signed main() {
    // cout.precision(16);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }
}
#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...