結果

問題 No.3761 Moonlit Battle
コンテスト
ユーザー shinchan
提出日時 2026-10-10 00:45:27
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 3,014 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,178 ms
コンパイル使用メモリ 352,544 KB
実行使用メモリ 24,808 KB
最終ジャッジ日時 2026-10-10 00:45:43
合計ジャッジ時間 6,501 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 47
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(),(v).end()
#define pb emplace_back
#define rep(i, n) for(int i=0;i<(n);i++)
#define foa(e, v) for(auto& e : v)
#define dout(a) cout<<fixed<<setprecision(10)<<a<<'\n';
#define Cout(a) cout<<a<<'\n';

using ll = long long;
using ld = long double;
using Int = __int128;
template <class T> using pqr = priority_queue<T, vector<T>, greater<T>>;

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
    bool compare = a < b;
    if(compare) a = b;
    return compare;
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
    bool compare = a > b;
    if(compare) a = b;
    return compare;
}
template <typename T> inline T back(std::set<T> &s) {
    return *s.rbegin();
}
template <typename T> inline T back(std::multiset<T> &s) {
    return *s.rbegin();
}
template <typename T> inline T pop_back(std::set<T> &s) {
    auto it = prev(s.end());
    T val = *it;
    s.erase(it); 
    return val;
}
template <typename T> inline T pop_back(std::multiset<T> &s) {
    auto it = prev(s.end());
    T val = *it;
    s.erase(it); 
    return val;
}

const int dy[8] = {-1, 0, 0, 1, 1, -1, 1, -1};
const int dx[8] = {0, -1, 1, 0, -1, -1, 1, 1};

const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (3LL << 59);
const int inf = 1 << 30;
const char br = '\n';

void solve() {
    ll n, d; cin >> n >> d;
    map<ll, ll> mp;
    vector<ll> a(n), b(n);
    ll ans = 0;
    rep(i, n) {
        cin >> a[i] >> b[i];
    }
    swap(a, b);
    {
        vector<int> ord(n, 0);
        iota(all(ord), 0);
        sort(all(ord), [&](int i, int j) {
            return a[i] < a[j];
        });
        vector<ll> c, d;
        rep(i, n) {
            c.pb(a[ord[i]]);
            d.pb(b[ord[i]]);
        }
        swap(c, a);
        swap(d, b);
    }
    
    {
        ll sum = 0;
        ll base = d;
        for(int i = n - 1; i >= 0; i --) {
            sum += b[i];
            if(sum >= d) {
                base = a[i];
                break;
            }
        }
        priority_queue<pair<ll, ll>> pq;
        rep(i, n) pq.push({a[i], b[i]});

        sum = 0;
        while(1) {
            auto [x, y] = pq.top();
            pq.pop();
            if(x <= 0) {
                chmin(ans, sum);
                break;
            }
            chmin(ans, x + sum);
            if(x > base + d * 2) {
                ll num = max(1LL, (x - (base + d * 2)) / d);
                sum += num * y;
                if(sum >= ans) break;
                x -= num * d;
                pq.push({x, y});
            } else if(x < base - d * 2) break;
            else {
                sum += y;
                if(sum >= ans) break;
                x -= d;
                pq.push({x, y});
            }
        }
    }

    
    cout << ans << endl;
    
    
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int testcase = 1; 
    // cin >> testcase;
    while(testcase --) solve();

    return 0;
}
0