Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ss second
#define ff first
#define pb push_back
#define mp make_pair
ll n,k;
vector<pair<ll,ll> >v;
map<ll,bool> m;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    ll ans=0;
    for(int i=0;i<n;i++){
        ll x,y;
        cin>>x>>y;
        ll o=k/x;
        if(o<x)continue;
        x=x*x;
        v.pb(mp(y,x));
        ans+=(k/x)*y;
    }
    sort(v.begin(),v.end());
    for(ll i=0;i<v.size();i++){
            for(ll j=1;j<=1e6;j++){
                    //if(j*j==v[i].ss)continue;
                    ll o=j*j*v[i].ss;
                    if(o>k)break;
                    if(m[o]){ans-=v[i].ff;}
                    m[o]=1;
            }
    }
    cout<<ans;

}
/*
5 3
1 3 3 2 4 6
-1 -5 -10 4 -3 5
1 3 2

*/
Copy
Number of the Beast Wesam
GNU G++17
25 ms
5.4 MB
Wrong Answer