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<=1000000;j++){
                    for(ll j2=1;j2<=1000000;j2++){
                    //if(j*j==v[i].ss)continue;
                    if(sqrt(j2)*sqrt(j2)==j2)continue;
                    ll o=j*j*j2*v[i].ss;
                    if(o>k)break;
                    if(m[o]){ans-=v[i].ff;}
                    m[o]=1;}
            }
    }
    cout<<ans;

}
/*
3 900
2 10
3 20



*/
Copy
Number of the Beast Wesam
GNU G++17
1103 ms
112.3 MB
Time Limit Exceeded