Source Code
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll N = 1000000;
const ll M = N * N;

ll k;
int n;
vector<pll> a;

int main() {
    cin >> n >> k;

    for (int i = 0; i < n; i++) {
        ll x, y;
        scanf("%lld%lld", &x, &y);
        if (x <= N && x * x <= k) {
            a.emplace_back(y, x);
        }
    }
    sort(a.begin(), a.end());

    auto cmp = [](ll a, ll b) {
        return abs(a) < abs(b);
    };

    ll old = 0;
    ll answer = 0;
    std::set<ll, decltype(cmp)> subs(cmp);
    // set<ll> subs;
    vector<ll> temp;
    for (pll p : a) {
        ll y = p.second;
        ll x = y * y;

        ll current = old;
        if (x <= k) {
            temp.push_back(x);
            current += k / x;
        }

        for (ll z : subs) {
            if (abs(z * y) <= k && abs(z * x) <= k) {
                temp.push_back(-z * x);
                current += k / (-z * x);
            } else {
                break;
            }
        }

        for (ll z : temp) {
            subs.insert(z);
        }

        temp.clear();

        ll now = current - old;
        answer += now * p.first;
        old = current;
    }

    cout << answer << endl;
}
Copy
Number of the Beast Baraa_Armoush
GNU G++17
323 ms
31.9 MB
Accepted