Problem:
Score : 100 points

Problem Statement

Consider an infinite sequence a_1, a_2, … Initially, the values of all the terms are 0, and from this state we will sequentially perform Q operations. The i-th operation (1 ≤ i ≤ Q) is as follows:

For every positive integer j, add x_i to the value of a_{j × m_i}.

Find the value of the largest term after these Q operations.

Constraints

1 ≤ Q ≤ 299

2 ≤ m_i ≤ 300

-10^6 ≤ x_i ≤ 10^6

All m_i are distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
m_1 x_1
:
m_Q x_Q

Output

Print the value of the largest term after the Q operations.

Sample Input 1

3
2 10
3 -20
6 15

Sample Output 1

10

The values of each terms in the sequence a_1, a_2, … change as follows:

Before the operations: 0, 0, 0, 0, 0, 0, …

After the 1-st operation: 0, 10, 0, 10, 0, 10, …

After the 2-nd operation: 0, 10, -20, 10, 0, -10, …

After the 3-rd operation: 0, 10, -20, 10, 0, 5, …

The value of the largest term after all the operations is 10.

Sample Input 2

3
10 -3
50 4
100 -5

Sample Output 2

1

Sample Input 3

5
56 114834
72 -149861
100 190757
192 -132693
240 133108

Sample Output 3

438699

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL<<29;
const ll mod = 1e9+7;
#define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i))
#define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset((m),(v),sizeof(m))
#define chmin(X,Y) ((X)>(Y)?X=(Y),true:false)
#define chmax(X,Y) ((X)<(Y)?X=(Y),true:false)
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)),(x).end())
template<class T> ostream &operator<<(ostream &os, const vector<T> &v){int n=v.size();rep(i,n)os<<v[i]<<(i==n-1?"":" ");return os;}
#define N 300
#define M 7
ll c[N+1], b[N+1], d[N+1];
int p[M] = {2, 3, 5, 7, 11, 13, 17};
int pr[N];
bool isPrime(int x){
	if(pr[x]==1) return true;
	if(pr[x]==-1) return false;
	pr[x] = -1;
	if(x<2) return false;
	for(int i = 2; i*i<=x; i++) if(x%i==0) return false;
	pr[x] = 1;
	return true;
}
int dfs(int ix){
	if(ix<7){
		int res = dfs(ix+1);
		int x = p[ix];
		while(x<=N){
			b[x] = 1;
			res = max(res, dfs(ix+1));
			x *= p[ix];
		}
		x = p[ix];
		while(x<=N){
			b[x] = 0;
			x *= p[ix];
		}
		return res;
	}
	memcpy(d, b, sizeof(b));
	for(int i = 2; i <= N; i++){
		if(!d[i]) continue;
		for(int j = i+1; j <= N/i; j++){
			if(!d[j]) continue;
			if(__gcd(i, j)==1) d[i*j] = 1;
		}
	}
	int res = 0;
	rep(i, N) res += c[i+1]*d[i+1];
	/*if(d[16]==1&&d[7]==1&&d[25]==1&&d[32]==0&&d[49]==0&&d[125]==0&&d[9]==0){
		cerr<<"RES: "<<res<<endl;
		rep(i, N) if(b[i+1]) cerr<<i+1<<" "; cerr<<endl;
		rep(i, N) if(d[i+1]) cerr<<i+1<<" "; cerr<<endl;
		//rep(i, N) cerr<<b[i+1]<<" "; cerr<<endl;
		//rep(i, N) cerr<<d[i+1]<<" "; cerr<<endl;
	}*/
	for(int i = p[M-1]+2; i <= N; i++){
		if(isPrime(i)){
			int s = 0;
			for(int j = 1; i*j <= N; j++){
				s += c[i*j]*d[j];
			}
			if(s>0) res += s;
		}
	}

	return res;
}
int main(){
	int n;
	cin>>n;
	rep(i, n){
		int a, x;
		cin>>a>>x;
		c[a] = x;
	}
	b[1] = 1;
	cout<<dfs(0)<<endl;
	return 0;
}