akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
typedef tuple<int, int, int, int> T;
 
int n;
string s[50]; int c[50];
int l[50];
int dp[51][2][21];	//dp[str type(0～n-1)][prefix(0), suffix(1)][len]
 
signed main() {
	int i, j, k;
	
	cin >> n;
	rep(i, n) { cin >> s[i] >> c[i]; l[i] = s[i].length(); }
	
	int INF = 1e+16;
	rep(i, n) rep(j, 2) rep(k, 21) dp[i][j][k] = INF;
	
	priority_queue<T, vector<T>, greater<T>> que;
	
	rep(i, n) {
		que.push(T(c[i], i, 0, l[i]));
	}
	
	while (!que.empty()) {
		T now = que.top(); que.pop();
		int cst = get<0>(now);
		int type = get<1>(now);
		int flag = get<2>(now);
		int len = get<3>(now);
		
		//len文字の部分が既に回文？
		bool heno = true;
		if (flag == 0) {
			rep(i, len) {
				if (s[type][l[type] - len + i] != s[type][l[type] - 1 - i]) {
					heno = false;
					break;
				}
			}
		}
		else {
			rep(i, len) {
				if (s[type][i] != s[type][len - 1 - i]) {
					heno = false;
					break;
				}
			}
		}
		
		if (heno) {
			cout << cst << endl;
			return 0;
		}
		
		if (dp[type][flag][len] <= cst) continue;
		dp[type][flag][len] = cst;
		
		//遷移
		rep(i, n) {
			if (flag == 0) {
				if (len >= l[i]) {
					rep(j, l[i]) {
						if (s[type][l[type] - len + j] != s[i][l[i] - 1 - j]) {
							break;
						}
					}
					if (j == l[i]) {
						que.push(T(cst + c[i], type, flag, len - l[i]));
					}
				}
				else {
					rep(j, len) {
						if (s[type][l[type] - len + j] != s[i][l[i] - 1 - j]) {
							break;
						}
					}
					if (j == len) {
						que.push(T(cst + c[i], i, !flag, l[i] - len));
					}
				}
			}
			else {
				if (len >= l[i]) {
					rep(j, l[i]) {
						if (s[type][len - 1 - j] != s[i][j]) {
							break;
						}
					}
					if (j == l[i]) {
						que.push(T(cst + c[i], type, flag, len - l[i]));
					}
				}
				else {
					rep(j, len) {
						if (s[type][len - 1 - j] != s[i][j]) {
							break;
						}
					}
					if (j == len) {
						que.push(T(cst + c[i], i, !flag, l[i] - len));
					}
				}
			}
		}
	}
	
	cout << -1 << endl;
	return 0;
}

Measured memory_bytes: 3540
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int k, int[] v) {
    int n = cast(int)v.length;
    int ans = 0;

    // 左から a 個とる
    for (int a = 0; a <= n; a++) {
        if (a > k) break;

        int val = 0; // 宝石の合計
        int[] neg; // 負の宝石
        for (int i = 0; i < a; i++) {
            val += v[i];
            if (v[i] < 0) neg ~= v[i];
        }

        // 右から b 個とる
        for (int b = 0; b <= n; b++) {
            if (b > k) break;
            if (a + b > min(n, k)) break;

            int val2 = val;
            auto neg2 = neg.dup;
            for (int i = 0; i < b; i++) {
                val2 += v[n-1-i];
                if (v[n-1-i] < 0) neg2 ~= v[n-1-i];
            }

            // 操作回数が残っているなら、負の宝石を捨てる
            int restK = max(0, k - (a + b));
            if (restK > 0) {
                neg2.sort();
                for (int i = 0; i < min(restK, neg2.length); i++) {
                    val2 += -neg2[i];
                }
            }

            ans = max(ans, val2);
        }
    }
    return ans;
}

void main() {
    int n, k; scan(n, k);
    auto v = readints;
    writeln(calc(k, v));
}


Measured memory_bytes: 636
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;


int main() {
  int N;
  cin >> N;
  vector<long long> vec(N);
  for (int i = 0;i <N;i++) {
    cin >> vec.at(i);
  }
  bool ok = true;
  sort(vec.begin(),vec.end());
  for (int i = 0;i<N-1;i++) {
    if (vec.at(i) == vec.at(i+1)) ok = false;
  }
  if (ok) cout << "YES" << endl;
  else cout << "NO" << endl;

}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;

template <class t>
using vc = vector<t>;
template <class t>
using vvc = vector<vector<t>>;
typedef long long ll;
typedef vc<int> vi;
typedef vvc<int> vvi;
typedef pair<int, int> pi;

#define uset unordered_set
#define umap unordered_map

#define endl "\n"

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()

#define all(a) a.bg, a.ed

template <class t, class u>
ostream &operator<<(ostream &os, const pair<t, u> &p) {
    return os << "( " << p.first << ", " << p.second << " )";
}

template <class t>
ostream &operator<<(ostream &os, const vc<t> &v) {
    os << "[ ";
    for (int i = 0; i < v.size(); i++) {
        os << v[i];
        if (i != v.size() - 1) {
            os << ", ";
        }
    }
    return os << " ]";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    // cout << fixed << setprecision(18);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // Solution
    // C[1] = B[1]
    // C[i] = min(B[i - 1], B[i])
    // C[N] = B[N]
    int n;
    cin >> n;

    vi b(n);

    ll tot = 0;
    for (int i = 1; i < n; i++) {
        cin >> b[i];
        b[0] = b[1];
        tot += min(b[i - 1], b[i]);
    }
    cout << tot + b.back();
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int) x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template<class T> void print(const T& t){ cout << t << endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
  ll n;
  cin >> n;
  ll m = n;
  string s = to_string(m);
  ll sum = 0;
  rep(i,sz(s)){
    sum += s[i] - '0';
  }
  if(n%sum==0){
    print("Yes");
  }
  else{
    print("No");
  }
}


Measured memory_bytes: 256
#include <iostream>
#include <cmath>
using namespace std;

int main(){
	int n,prime[50000] = {0},c;
	bool f = true;
	prime[2] = 1;
	for(int i = 3;i < 50000;++i){
		for(int j = 2;j <= sqrt(i);++j){
			if(i % j == 0) f = false;
		}
		if(f) prime[i] = 1;
		f = true;
	}
	while(cin >> n,n){
		c = 0;
		for(int i = 0;i <= n/2;++i){
			if(prime[i] == 1 && prime[n-i] == 1) ++c;
		}
		cout << c << endl;
	}
	return 0;
}

Measured memory_bytes: 1360
a, b = map(int, input().split())
i = 1
found = False
while True:
    if int(i * 0.08) > a + 1 or int(i * 0.1) > b:
        break
    if int(i * 0.08) == a and int(i * 0.1) == b:
        print(i)
        found = True
        break
    i = i + 1
if not found:
    print(-1)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int n,m;
    cin >>n >>m;

    ll t = 100*(n-m) + 1900*m;
    cout << t*(1<<m) << endl;
    return 0;
}


Measured memory_bytes: 256
import fractions
 
n = int(input())
a = [int(value) for value in input().split(" ")]

from_right_gcd = []
from_left_gcd = []

# 左から
for index in range(len(a)):
    if index == 0:
        from_left_gcd.append(a[index])
        continue
    from_left_gcd.append(fractions.gcd(from_left_gcd[index - 1], a[index]))

# 右から
for index in range(len(a) - 1, -1, -1):
    if index == len(a) - 1:
        from_right_gcd.append(a[index])
        continue
    from_right_gcd.append(fractions.gcd(from_right_gcd[len(a) - index - 1 - 1], a[index]))

maybe_answer_list = []

for index in range(len(a)):
    if index == 0:
        maybe_answer_list.append(from_right_gcd[len(a) - 1 - 1])
        continue
    if index == len(a) - 1:
        maybe_answer_list.append(from_left_gcd[index - 1])
        continue
    maybe_answer_list.append(fractions.gcd(from_left_gcd[index - 1], from_right_gcd[len(a) - index - 1 - 1]))

print(max(maybe_answer_list))

Measured memory_bytes: 16132
#include <stdio.h>
#include <stdlib.h>

#define N 501

int asc(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}

int main(void)
{
	int n, k, a[N], s[N];

	scanf("%d %d", &n, &k);

	int i, j, sum = 0;
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		sum += a[i];
	}

	for (i = sum; i > 0; i--)
	{
		if (sum % i == 0)
		{
			for (j = 0; j < n; j++)
				s[j] = a[j] % i;
			qsort(s, n, sizeof(int), asc);
			for (j = 1; j < n; j++)
				s[j] += s[j - 1];
			for (j = 0; j < n; j++)
				if ((n - j - 1) * i == s[n - 1] && s[j] <= k)
					goto exit;
		}
	}
exit:
	printf("%d\n", i);
	return 0;
}

Measured memory_bytes: 252
import math
count=0
N,D=map(int,input().split())
X = [0] * N
Y = [0] * N
for i in range(N):
    #上から順番に代入していく
    X[i], Y[i] = map(int, input().split())
for i in range(N):
    if math.sqrt(X[i]**2+Y[i]**2)<=D:
        count+=1
print(count)

Measured memory_bytes: 24752
#include<iostream>
using namespace std;


int main(void){
    int a,b,c;
    cin >>a;
    cin >>b;
    cin >>c;

if(a>=b&&a>=c){
    cout <<10*a+b+c<<endl;
} else if(b>=a&&b>=c){
    cout <<10*b+a+c<<endl;
} else {
    cout << 10*c+a+b<<endl;
}


    
  
return 0;
}


Measured memory_bytes: 256
while True:
    a = int(input())
    if a == 0:
        break
    else:
        b = [int(input()) for i in range(a)]
        j = 0
    for j in range(10):
        j = j+1
        c = b.count(j-1)
        if c == 0:
            print("-")
        else:
            print("*"*c)


Measured memory_bytes: 5716
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
typedef long long int ll;

#define PI (acos(-1))
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define dumparr(x) cerr << #x ; for(auto elem: x){cerr << " " << elem;} cerr << endl;
#define dumppairs(x) cerr << #x ; for(auto elem: x){cerr << "(" << elem.first << " " << elem.second << ") ";} cerr << endl;
#define dumpline cerr << "**********************" << endl;
#define dumpwline(x) cerr << "******** " << #x << "=" << (x) << " ********" << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))


ll n;


int main(void){
    cin >> n;
    ll m; cin >> m;
    ll k; cin >> k;
    vector <vector <ll>> G(n, vector<ll>());

    REP(i, m){
        ll a; cin >> a; a--;
        ll b; cin >> b; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<vector <ll>> B(n, vector<ll>());

    REP(i, k){
        ll a; cin >> a; a--;
        ll b; cin >> b; b--;
        B[a].push_back(b);
        B[b].push_back(a);
    }
    vector <bool> visited(n, false);
    vector <ll> ans(n, 0);
    REP(i, n){
        if(visited[i]){
            continue;
        }
        queue <ll> q;
        unordered_set <ll> s;
        vector <int> v;
        q.push(i);
        visited[i] = true;
        s.insert(i);
        v.push_back(i);
        while(!q.empty()){
            ll nxt = q.front();
            q.pop();
            for(auto tugi: G[nxt]){
                if (visited[tugi]){
                    continue;
                }
                visited[tugi] = true;
                s.insert(tugi);
                v.push_back(tugi);
                q.push(tugi);
            }
        }
        //dumparr(v)
        for(auto node: v){
            vector <ll> excluded;
            for(auto suki: G[node]){
                if(s.find(suki) != s.end()){
                    excluded.push_back(suki);
                    s.erase(suki);
                }
            }
            for(auto kirai: B[node]){
                if(s.find(kirai) != s.end()){
                    excluded.push_back(kirai);
                    s.erase(kirai);
                }
            }
            ans[node] = s.size() - 1;

            for(auto e: excluded){
                s.insert(e);
            }
        }
    }
    REP(i, n-1){
        cout << ans[i] << " ";
    }
    cout << ans[n-1] << endl;
    return 0;
}

Measured memory_bytes: 18804
N, K = map(int, input().split())
sum_list = []
for i in range(K):
  d = int(input())
  own_list = list(map(int, input().split()))
  sum_list += own_list
count = 0
# print(sum_list)
for i in range(1, N+1):
  if i not in sum_list:
    count += 1
print(count)

Measured memory_bytes: 9180
#include <cstdio>
using namespace std;
 
long long N, a, an = 1, i, j;
constexpr int p[168] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};
 
int main(){
    scanf("%lld", &N);
    for(i = 0; i < 168 && p[i] <= N; ++i, a = 0){
        for(j = p[i]; j <= N; j *= p[i]){
            a += N / j;
        }
        an = an * (a + 1) % 1000000007;
    }
    printf("%lld", an);
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <map>
using namespace std;

int main(){
  int N,M;
  while(cin >> N >> M && !(N==0 && M==0)){
    map<int,int> hor,ver;
    int h[1500], w[1500];
    for(int i=0;i<N;i++){
      cin >> h[i];
      int s=0;
      for(int j=i;j>=0;j--){
	s+=h[j];
	ver[s]++;
      }
    }
    for(int i=0;i<M;i++){
      cin >> w[i];
      int s=0;
      for(int j=i;j>=0;j--){
	s+=w[j];
	hor[s]++;
      }
    }

    int count=0;
    for(map<int,int>::iterator p=ver.begin();p!=ver.end();++p)
      count+=(p->second)*(hor[p->first]);

    cout << count <<endl;
  }

  return 0;
}

Measured memory_bytes: 34356
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

			String a = sc.next();
			String s = sc.next();
			String b = sc.next();

			char aa = a.charAt(0);
			char ss = s.charAt(0);
			char bb = b.charAt(0);

			System.out.println(aa + "" + ss + "" + bb);
	}
}


Measured memory_bytes: 38856
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int J[1005][1005], O[1005][1005], I[1005][1005];



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

	vector<string> v(m);
	rep(i, m) cin >> v[i];

	memset(J, 0, sizeof(J));
	memset(O, 0, sizeof(O));
	memset(I, 0, sizeof(I));

	rep(i, m) {
		rep(j, n) {
			if(v[i][j] == 'J') J[i][j]++;
			if(v[i][j] == 'O') O[i][j]++;
			if(v[i][j] == 'I') I[i][j]++;
		}
	}

	rep(i, m) {
		REP(j, 1, n) {
			J[i][j] += J[i][j-1];
			O[i][j] += O[i][j-1];
			I[i][j] += I[i][j-1];
		}
	}

	rep(i, n) {
		REP(j, 1, m) {
			J[j][i] += J[j-1][i];
			O[j][i] += O[j-1][i];
			I[j][i] += I[j-1][i];
		}
	}

	rep(i, k) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;

		a--;
		b--;
		c--;
		d--;

		cout << J[c][d] - (a-1 >= 0 ? J[a-1][d] : 0) - (b-1 >= 0 ? J[c][b-1] : 0) + (a-1 >= 0 && b -1 >= 0 ? J[a-1][b-1] : 0) << " ";
		cout << O[c][d] - (a-1 >= 0 ? O[a-1][d] : 0) - (b-1 >= 0 ? O[c][b-1] : 0) + (a-1 >= 0 && b -1 >= 0 ? O[a-1][b-1] : 0) << " ";
		cout << I[c][d] - (a-1 >= 0 ? I[a-1][d] : 0) - (b-1 >= 0 ? I[c][b-1] : 0) + (a-1 >= 0 && b -1 >= 0 ? I[a-1][b-1] : 0) << endl;
	}

	return 0;
}

Measured memory_bytes: 13932
#include <iostream>
#include <vector>
#include <utility>
#include <map>
#include <cstdint>
#include <numeric>

using namespace std;

class Node {
public:
	int start;
	int end;
	int mv;
	Node* left;
	Node* right;
	Node(int s, int e, int m = -1, Node* l = nullptr, Node* r = nullptr) : start(s), end(e), mv(m), left(l), right(r) {}
};

class Tree {
public:
	Node *root;
	Tree() {
		root = nullptr;
	}

	Node* buildTree(int start, int end, vector<int> &v) {
		if (start == end) {
			return new Node(start, end, v[start]);
		}
		int mid = (start + end) / 2;
		Node* res = new Node(start, end);
		res->left = buildTree(start, mid, v);
		res->right = buildTree(mid+1, end, v);
		res->mv = max(res->left->mv, res->right->mv);
		return res;
	}

	void updateNode(int index, int value, Node* node) {
		if (index < node->start || index > node->end) return;
		if (index == node->start && index == node->end) {
			node->mv = value;
			return;
		}
		updateNode(index, value, node->left);
		updateNode(index, value, node->right);
		node->mv = max(node->left->mv, node->right->mv);
	}

	int searchMax(int s, int e, Node* node) {
		if (s == node->start && e == node->end) {
			return node->mv;
		}
		if (s >= node->right->start) {
			return searchMax(s, e, node->right);
		}
		if (e <= node->left->end) {
			return searchMax(s, e, node->left);
		}
		return max(searchMax(s, node->left->end, node->left), searchMax(node->right->start, e, node->right));
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	const int M = 998244353;
	int n;
	cin>>n;
	map<int, int> v;
	for (int i = 0; i < n; i++) {
		int x, d;
		cin>>x>>d;
		v[x] = x + d;
	}

	vector<int> pos, reach;
	for (auto &i : v) {
		pos.push_back(i.first);
		reach.push_back(i.second);
	}

	vector<int> dp(n);
	iota(dp.begin(), dp.end(), 0);

	vector<int64_t> DP(n);
	DP[n-1] = 2;

	Tree T = Tree();
	T.root = T.buildTree(0, n-1, dp);

	for (int i = n-2; i>=0; i--) {
		int index = lower_bound(pos.begin(), pos.end(), reach[i]) - pos.begin() - 1;
		int r = T.searchMax(i, index, T.root);
		T.updateNode(i, r, T.root);
		dp[i] = r;
		if (r == n-1) {
			DP[i] = (DP[i+1] + 1) % M;
		} else {
			DP[i] = (DP[i+1] + DP[r+1]) % M;
		}
	}

	cout<<DP[0]<<endl;
	return 0;
}

Measured memory_bytes: 32364
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

const int MAX_N = 1005, MOD = 1000000007;
typedef long long i64;
typedef pair<int, int> pii;

class Edge {
public:
  int nxt, to;
} e[MAX_N << 1];

int head[MAX_N], cnt, C[MAX_N][MAX_N];
void addedge(int u, int v) {
  e[++cnt] = (Edge){head[u], v}, head[u] = cnt;
  e[++cnt] = (Edge){head[v], u}, head[v] = cnt;
}

int N, M, K, dfn[MAX_N], low[MAX_N], dfs_clock;
int st[MAX_N], top, bcc_cnt;

vector<int> block[MAX_N];

pii edge[MAX_N];

void find_bcc(int u, int v) {
  dfn[u] = ++dfs_clock, low[u] = dfn[u];

  int temp;
  
  for (int i = head[u]; i; i = e[i].nxt) 
    if (!dfn[e[i].to]) {
      temp = top, st[++top] = (i + 1) / 2;
      find_bcc(e[i].to, u), low[u] = min(low[u], low[e[i].to]);

      if (low[e[i].to] >= dfn[u]) {
	++bcc_cnt;
	while (top > temp) block[bcc_cnt].push_back(st[top--]);
      }
      
    } else if (e[i].to != v && dfn[e[i].to] < dfn[u]) {
      low[u] = min(low[u], dfn[e[i].to]);
      st[++top] = (i + 1) / 2;
    }
}

int fa[MAX_N];
int find(int x) {
  return fa[x] == x ? x : fa[x] = find(fa[x]);
}

i64 fpm(i64 x, i64 y) {
  i64 res = 1;
  while (y) {
    if (y & 1) res = res * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return res;
}

i64 work(vector<int> &v) {
  for (int i = 1; i <= N; ++i) fa[i] = i;

  int num = 0, n = v.size();
  for (int x : v) {
    int u = find(edge[x].first), v = find(edge[x].second);
    if (u == v) num++;
    else fa[u] = v;
  }

  if (num == 0) return K;
  else if (num == 1) {
    i64 res = 0;
    for (int i = 1; i <= n; ++i)
      res = (res + fpm(K, __gcd(i, n))) % MOD;
    res = res * fpm(n, MOD - 2) % MOD;
    return res;
  } else {
    return C[n + K - 1][K - 1];
  }
  
}

int main() {

  scanf("%d%d%d", &N, &M, &K);

  for (int i = 0; i < MAX_N; ++i) {
    C[i][0] = 1;
    for (int j = 1; j <= i; ++j)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
  }
  
  for (int i = 1, u, v; i <= M; ++i) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
    edge[i] = make_pair(u, v);
  }

  for (int i = 1; i <= N; ++i)
    if (!dfn[i]) find_bcc(i, 0);

  i64 res = 1;
  for (int i = 1; i <= bcc_cnt; ++i)
    res = res * work(block[i]) % MOD;

  printf("%lld\n", res);
  
  return 0;
}

Measured memory_bytes: 4224
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int out=0,fh=1;
	char jp=getchar();
	while ((jp>'9'||jp<'0')&&jp!='-')
		jp=getchar();
	if (jp=='-')
		fh=-1,jp=getchar();
	while (jp>='0'&&jp<='9')
		out=out*10+jp-'0',jp=getchar();
	return out*fh;
}
const int MAXN=1e5+10;
int head[MAXN],to[MAXN<<1],nx[MAXN<<1],ecnt=0;
void addedge(int u,int v)
{
	++ecnt;
	to[ecnt]=v;
	nx[ecnt]=head[u];
	head[u]=ecnt;
}
const int P=1e9+7;
int add(int a,int b)
{
	return (a + b) % P;
}
int mul(int a,int b)
{
	return 1LL * a * b % P;
}
int fpow(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)
			res=mul(res,a);
		a=mul(a,a);
		b>>=1;
	}
	return res;
}
int fac[MAXN],invfac[MAXN];
int n,k,f[MAXN];
int fa[MAXN];
int A(int N,int M)
{
	if(N>M)
		return 0;
	return mul(fac[M],invfac[M-N]);
}
int dfs(int u,int num,int Fa)
{
	fa[u]=Fa;
	if(num<=0)
		return f[u]=0;
	int deg=0;
	for(int i=head[u];i;i=nx[i])
	{
		int v=to[i];
		if(v==Fa)
			continue;
		++deg;
	}
	if(!deg)
		return f[u]=1;
	if(u==1)
		f[u]=A(deg,k-1);
	else
		f[u]=A(deg,k-2);
	if(!f[u])
		return f[u];
	for(int i=head[u];i;i=nx[i])
	{
		int v=to[i];
		if(v==Fa)
			continue;
		if(u==1)
			dfs(v,k-1,u);
		else
			dfs(v,k-2,u);
		f[u]=mul(f[u],f[v]);
	}
	return f[u];
}
int main()
{
	n=read(),k=read();
	fac[1]=1;
	for(int i=2;i<=k;++i)
		fac[i]=mul(fac[i-1],i);
	invfac[k]=fpow(fac[k],P-2);
	for(int i=k-1;i>=0;--i)
		invfac[i]=mul(invfac[i+1],i+1);
	for(int i=1;i<n;++i)
	{
		int u=read(),v=read();
		addedge(u,v);
		addedge(v,u);
	}
	cout<<mul(dfs(1,k,0),k)<<endl;
	return 0;
}


Measured memory_bytes: 7680
m,d = map(int,input().split())
ans = 0
for i in range(1,m+1):
  for j in range(10,d+1):
    if "1" in list(str(j)):
      continue
    j1 = j//10
    j2 = j%10
    if i == j1*j2:
      ans += 1
print(ans)

Measured memory_bytes: 3060
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
	}
}

class Solver {
	int N;
	int cnt;

	public void init() {
		N = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split(" ");
		// N = Integer.parseInt(strArr[0]);
		// System.out.println(N);
		int a = Integer.parseInt(strArr[0]);
		int b = Integer.parseInt(strArr[1]);
		int c = Integer.parseInt(strArr[2]);
		int max = a;
		int sum = a;
		if (b > max)
			max = b;
		sum += b;
		if (c > max)
			max = c;
		sum += c;
		System.out.println((sum - max));
	}

	public boolean hasNext() {
		return cnt < N;
	}

	public void readBody(String str) {
		// System.out.println(str);
		cnt++;
	}

	public void solve() {

	}
}


Measured memory_bytes: 21204
import Control.Applicative

type Itg = Integer

getItgList :: IO [Itg]
getItgList = map read . words <$> getLine

result :: Itg -> Itg -> Itg -> String
result a b c = show a ++ " " ++ show b ++ " " ++ show c

main :: IO ()
main = do
    [a, b, c] <- getItgList
    putStrLn $ result c a b

Measured memory_bytes: 508
#include <bits/stdc++.h>

using namespace std;
#define int long long
int a[500];
int sum;
int n,L;
int ans = 1e18;
int ans2;
signed main(){
	scanf("%lld%lld",&n,&L);
	for(int i = 1; i <= n; ++i)a[i] = i + L - 1,sum += a[i];
	for(int i = 1; i <= n; ++i){
		int cur = sum - a[i];
		if(llabs(cur - sum) < ans)ans = llabs(cur - sum),ans2 = cur;
	}
	printf("%lld\n",ans2);
	return 0;
}

Measured memory_bytes: 256
#include <queue>
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
ll INF = 9999999999999999;

ll gcd(ll a,ll b){
    if(a < b) return gcd(b,a);
    ll r = INF;
    while(r){
        r = a % b;
        a = b;
        b = r; 
    }
    return a;
}
int main(){
   ll A,B,C,D;
   cin >> A >> B >> C >> D;
   ll LCM = D/gcd(C,D) * C / gcd(C,D) * gcd(C,D);
   ll Cm = B / C - (A-1) / C;
   ll Dm = B / D - (A-1) / D;
   ll CDm = B / LCM - (A-1) / LCM;  
   cout << B - A - (Cm+Dm-CDm) + 1<< endl;
}


Measured memory_bytes: 256
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

pow_rest = 0
defeat = 0

for i in range(n):
    x = min([a[i], b[i]])
    defeat += x
    y = min([a[i+1], b[i]-x])
    defeat += y
    a[i+1] -= y

print(defeat)


Measured memory_bytes: 18624
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 100000;
pair<int, int> sg[N];

int main(){
	int n;
	scanf("%d", &n);
	f(i, 0, n)scanf("%d%d", &sg[i].second, &sg[i].first), sg[i].second *= -1;
	sort(sg, sg + n);
	f(i, 0, n)sg[i].second *= -1;
	f(i, 0, n)swap(sg[i].first, sg[i].second);
	int mxl = 0, an = 0;
	
	int mn = 1e9, mx = 0;
	f(i, 0, n){
		mn = min(mn, sg[i].second);
		mx = max(mx, sg[i].first);
	}
	int ex = max(0, mn - mx + 1);
	f(i, 0, n)an = max(an, sg[i].second - sg[i].first + 1 + ex);
	
	multiset<int> mxr;
	f(i, 0, n)mxr.insert(sg[i].first);
	f(i, 0, n - 1){
		mxr.erase(mxr.find(sg[i].first));
		mxl = max(mxl, sg[i].first);
		an = max(an, max(0, sg[0].second - mxl + 1) + max(0, sg[i + 1].second - *mxr.rbegin() + 1));
	}
	printf("%d\n", an);
}

Measured memory_bytes: 5760
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  vector<ll> a(3);
  cin >> a[0] >> a[1] >> a[2];
  sort(a.begin(), a.end());
  cout << a[0] + a[1] << endl;
  return 0;
}

Measured memory_bytes: 3580
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007 

ll a[26];
int main() {
	ll n;
	cin>>n;
	string s;
	cin>>s;
	for(ll i=0;i<n;i++){
		ll now=s[i]-'a';
		a[now]++;
	}
	ll ans=1;
	for(ll i=0;i<26;i++){
		ans*=a[i]+1;
		ans%=inf;
	}
	cout << ans-1;
	// your code goes here
	return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>

#ifdef _DEBUG
#define debug(x) cerr << "line: " << __LINE__ << ", func: " << __func__ << " ->  " << #x << " = " << x << endl
#else
#define debug(x)
#endif

#define all(s) begin(s), end(s)
#define rall(s) rbegin(s), rend(s)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = ((a)-1); i >= (b); i--)
#define pb push_back
#define sz(a) int((a).size())
#define put(a) ((cout) << (a) << (endl))

using namespace std;

using ll = long long;
using ull = unsigned long long;
using i_i = pair<int, int>;
using ll_ll = pair<ll, ll>;
using d_ll = pair<double, ll>;
using ll_d = pair<ll, double>;
using d_d = pair<double, double>;

static constexpr ll LL_INF = 1LL << 60;
static constexpr int I_INF = 1 << 28;
static constexpr double PI = static_cast<double>(3.14159265358979323846264338327950288);
static constexpr double EPS = numeric_limits<double>::epsilon();
static constexpr const char* YES = "Yes";
static constexpr const char* NO = "No";

static map<type_index, const char* const> scanType = {
    {typeid(int), "%d"}, {typeid(ll), "%lld"}, {typeid(double), "%lf"}, {typeid(char), "%c"}};

template <class T> static void scan(vector<T>& v);
[[maybe_unused]] static void scan(vector<string>& v, bool isWord = true);
template <class T> static inline bool chmax(T& a, T b);
template <class T> static inline bool chmin(T& a, T b);
template <class A, size_t N, class T> static void Fill(A (&arr)[N], const T& val);

int main(int argc, char* argv[]) {
  long long N;
  scanf("%lld", &N);
  vector<ll_ll> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i].second >> v[i].first;
  }

  sort(all(v));

  ll total = 0;

  for (auto p : v) {
    total += p.second;
    if (total > p.first) {
      put(NO);
      return 0;
    }
  }

  put(YES);
  return 0;
}

template <class T> static void scan(vector<T>& v) {
  auto tFormat = scanType[typeid(T)];
  for (T& n : v) {
    scanf(tFormat, &n);
  }
}

static void scan(vector<string>& v, bool isWord) {
  if (isWord) {
    for (auto& n : v) {
      cin >> n;
    }
    return;
  }

  int i = 0, size = v.size();
  string s;
  getline(cin, s);

  if (s.size() != 0) {
    i++;
    v[0] = s;
  }

  for (; i < size; ++i) {
    getline(cin, v[i]);
  }
}

template <class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class A, size_t N, class T> void Fill(A (&arr)[N], const T& val) {
  std::fill((T*)arr, (T*)(arr + N), val);
}


Measured memory_bytes: 3328
#include <bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using ll=long long;
using pll=pair<ll,ll>;

#define fi first
#define se second
#define pb push_back
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define MOD 1000000007

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }


int main() {
	int N,K;
	cin >> N >> K;
	vector<int> A(N, 0);
	int a;
	rep(i,N) {
		cin >> a;
		a--;
		A[a]++;
	}
	sort(A.begin(), A.end());
	//rep(i,N) cout << A[i] << " " << endl;
	ll sum = 0;
	int cnt = 0;
	while(1) {
		if(cnt >= N-K) break;
		if(A[cnt] == 0) {
			cnt++;
			continue;
		}
		sum += A[cnt];
		cnt++;
	}
	cout << sum << endl;


  return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using lint = long long;
using ldouble = long double;

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> sums(n, 0);
    int m = n * (n - 1) / 2;
    while (m--) {
        int i, j, x, y;
        std::cin >> i >> j >> x >> y;
        --i, --j;

        if (x == y) {
            sums[i] += 1;
            sums[j] += 1;
        } else if (x < y) {
            sums[j] += 3;
        } else {
            sums[i] += 3;
        }
    }

    for (int i = 0; i < n; ++i) {
        std::cout << std::count_if(sums.begin(), sums.end(),
                                   [&](int s) { return s > sums[i]; }) +
                         1
                  << "\n";
    }
}

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

    solve();

    return 0;
}



Measured memory_bytes: 3140
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect, string
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

a,b,c=map(int,input().split())
print(c//a*b)

Measured memory_bytes: 39024
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#include<cstring>
#include<iomanip> //cout << fixed << setprecision(15) << x << endl;

using namespace std;
typedef long long ll;
const ll INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1LL<<60;
//#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
/* -- template -- */

int main() {
    ll N, A, B, C, D; cin >> N >> A >> B >> C >> D;
    for(ll i = 0; i < N; ++i) {//i回足す，N - 1 - i回引く
        ll maxv = A + D * i - C * (N - 1 - i);
        ll minv = A + C * i - D * (N - 1 - i);
        if(minv <= B && B <= maxv) {
            cout << "YES" << endl;
            return 0;
        }
    }
    cout << "NO" << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include <math.h>
using namespace std;

int main() {
  cout << fixed << setprecision(9);
  double a, b, h, m;
  cin >> a >> b >> h >> m;
  double deg, pi, deg_r, x, ans, ans_2;
  deg = h*30.0 - m*5.5;
  pi = 2.0 * asin(1.0);
  deg_r = deg*pi/180.0;
  x = cos(deg_r);
  ans = a*a+b*b-2*a*b*x;
  ans_2 = sqrt(ans);
  cout << ans_2 << endl;
}

Measured memory_bytes: 4152
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

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

    ll c = 0;
    for (ll m = 1; m * m <= n; m++) {
        if (n / m == n % m) c += m;
    }
    for (ll a = 1; a * a < n; a++) {
        ll m = (n - a) / a;
        if (n / m == n % m) c += m;
    }

    cout << c << endl;

    //mがnより大きくなると商が0であまりがNになるので、条件を満たすmは有限個しかない（n==mでも満たさない）
    //0でないとしてよい

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
#define ck(a,b) 0<a&&a<=b
using namespace std;
int dy[]={-1,0,1,0},dx[]={0,-1,0,1};
int w,h,xs,ys,xg,yg,n,c,d,x,y,f[101][101];
bool dfs(int y,int x){
	f[y][x]=0;
	if(y==yg&&x==xg)return 1;
	bool r=0;
	rep(d,4){
		int ny=y+dy[d],nx=x+dx[d];
		if(ck(ny,h)&&ck(nx,w)&&f[ny][nx]==c)r=r||dfs(ny,nx);
	}
	return r;
}
int main(){
	while(cin>>w>>h,w){
		rep(i,h)rep(j,w)f[i+1][j+1]=0;
		cin>>xs>>ys>>xg>>yg>>n;
		rep(i,n){
			cin>>c>>d>>x>>y;
			rep(j,(d?2:4))rep(k,(d?4:2))f[y+k][x+j]=c;
		}
		c=f[ys][xs];
		cout<<(c>0&&dfs(ys,xs)?"OK":"NG")<<endl;
	}
	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  if ((A + B) > (C + D)) {
    cout << "Left" << endl;
  }
  else if ((A + B) < (C + D)) {
    cout << "Right" << endl;
  }
  else {
    cout << "Balanced" << endl;
  }
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;

typedef long long 			ll;
typedef unsigned long long 	ull;

const int N = 100 + 20;
const int MAXN = 100 * 1000 + 20;

//ll a[N];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	ll x;
	bool flag = false;
	cin >> x;
	while (!flag) {
		for (int i = 2; i <= x; i++) {
			if (!(x % i) && i < x)
				break;
			if (i == x) {
				cout << x << endl;
				flag = true;
			}
		}
		x++;
	}
	return 0;
}


Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func main() {
	defer writer.Flush()
	var X, t int
	fmt.Fscanf(reader, "%d %d\n", &X, &t)
	if t > X {
		fmt.Fprintf(writer, "%d", 0)
		return
	}
	fmt.Fprintf(writer, "%d\n", X-t)
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
typedef long  l;
typedef pair<int,int> P;
const ll INF=100000000000000001;
const double PI=3.141592653589;


int main(){
  int H,W,h,w; cin>>H>>W>>h>>w;
  int ans=H*W-(h*W+w*H-h*w);
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x,a) for (const auto& x: a)
#define sz(x) (int)(x).size()
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int _i = 0; _i < (n); _i++) ni(a[_i])
#define nal(a, n) for (int _i = 0; _i < (n); _i++) nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
char s[N];

int main() {
  scanf("%s", s);
  int n = strlen(s);
  if (s[0] == s[n-1] && n % 2 == 1)
    printf("Second\n");
  else if (s[0] != s[n-1] && n % 2 == 0)
    printf("Second\n");
  else
    printf("First\n");
  return 0;
}


Measured memory_bytes: 384
N = int(input())
H = list(map(int,input().split()))
var = 0
maxvar = 0
for k in range(N-1):
  if H[k] >= H[k+1]:
    var = var+1
  elif H[k] < H[k+1]:
    if var > maxvar:
      maxvar = var
      var = 0
    elif var <= maxvar:
      var = 0
if var > maxvar: 
  maxvar = var
print(maxvar)
      

Measured memory_bytes: 15020
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

#define ll long long

using namespace std;


int main(){
  string inp;
  int flag ;
  int l,i = 0;

  cin >> inp;

  do {
    i+=2;
    flag = 0;
    l = (inp.size() - i)/2;
    for(int i = 0; i < l ; i++){
      if(inp[i]!=inp[i+l]) flag = 1;
    }
  }while (flag);

  cout << l*2 << endl;
  
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<iomanip>
#include<list>
#include<deque>
using namespace std;
//#define MODE 1
#ifdef MODE
#define DEB(X) cout<< #X <<": "<<X<<" ";
#define DEB2(X) cout<<X<<" ";
#define END cout<<endl;
#else
#define DEB(X) {}
#define DEB2(X) {}
#define END {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
//typedef std::pair<int,int> P;
struct edge{int to,cost;};
const int INF=100000000000000000;
const int INF2=9223372036854775807;
const int MOD=1000000007;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define min(X,Y) (((int)(X)<(int)(Y))?(X):(Y))
#define max(X,Y) (((int)(X)>(int)(Y))?(X):(Y))
#define NP(X,Y) next_permutation(X,Y)
#define setdouble(X,Y) cout<<fixed<<setprecision(X)<<Y
int ceil2(int a,int b){if(a%b==0){return a/b;}else{return a/b+1;}}
int pow2(int a,int b){int r=1;for(int i=1;i<=b;i++){r*=a;}return r;}
int Log2(int a){int t=0;while(1){if(a==0||a==1){break;}a/=2;t++;}return t;}

int N;
int A[100010];
int B[100010];

signed main(){
	cin>>N;
	REP(i,N){
		cin>>A[i]>>B[i];
	}
	int sum=0;
	int ans=0;
	for(int i=N-1;0<=i;i--){
		int R=A[i]+sum;
		ans+=B[i]*ceil2(R,B[i])-R;
		sum+=B[i]*ceil2(R,B[i])-R;
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
void print(int a,int b,int c){cout << a << ' ' << b << ' ' << c << '\n';}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,K; cin >> N >> K;
    if (N+1<2*K){cout << -1 << '\n'; return 0;}
    for (int i=0;i<K;++i) print(K+i,2*N+i,K+2*N+2*i);
    for (int i=0;i<K-1;++i) print(2*K+i,-K+2*N+1+i,K+2*N+1+2*i);
    for (int i=0;i<N-2*K+1;++i) print(3*K-1+i,-K+2*N-i,3*K+2*N-1+i);
}

Measured memory_bytes: 2304
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

using ll=long long;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vpii=vector<pii>;
using vvpii=vector<vpii>;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int a, b, c;
    cin >> a >> b >> c;
    cout << max(0, c-(a-b)) << "\n";
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int,int> pii;
typedef vector< pii > vii;

#define ll          long long
#define ff          first
#define ss          second
#define pb          push_back
#define mkp         make_pair
#define sz          size()

#define forf(a,b,c)    for(int i=a;i<=b;i+=c)
#define forr(a,b,c)    for(int i=a;i>=b;i-=c)
#define all(a)      a.begin(),a.end()
#define mem(a,b)    memset(a,b,sizeof(a))
#define pi          2*acos(0.0)
#define End         return 0;

#define scan1(a)    scanf("%d",&a)
#define scan2(a,b)  scanf("%d %d",&a,&b)
#define scan3(a,b,c)  scanf("%d %d %d",&a,&b,&c)
#define scanl(a)    scanf("%lld",&a)
#define scanll(a,b) scanf("%lld %lld",&a,&b)
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)

#define print1(a)    printf("%d",a)
#define print2(a,b)  printf("%d %d",a,b)
#define print3(a,b,c)  printf("%d %d %d",a,b,c)
#define printl(a)    printf("%lld",a)
#define printll(a,b) printf("%lld %lld",a,b)
#define printlll(a,b,c) printf("%lld %lld %lld",a,b,c)

#define MAXN        100000007
#define INF 0x3f3f3f3f
//int dx[]={+1,-1,+0,+0};///Four Directions
//int dy[]={+0,+0,+1,-1};///Four directions
//int dx [] = {+1,-1,+0,+0,-1,-1,+1,+1};///Eight Directions
//int dy [] = {+0,+0,+1,-1,+1,-1,-1,+1};///Eight Directions

//int mark[10007];vi prime;void seive(){for(int i=2;i<10000;i++){if(mark[i]==0){prime.pb(i);for(int j=i+i;j<=10000;j+=i)mark[j]=1;}}}

//freopen("input.txt", "r", stdin);

int main()
{
   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

   int a,b,c;
   cin>>a>>b>>c;
   cout<<c<<" "<<a<<" "<<b<<endl;
   End;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>

using namespace std;
int main(){
	string s;
	cin>>s;
	int ans1=0,ans2=0;
	for(int i=0;i<s.size();i++){
		if(i%2==1)if(s[i]!='0')ans1++;
		if(i%2==0)if(s[i]!='1')ans1++;
	}
	for(int i=0;i<s.size();i++){
		if(i%2==1)if(s[i]!='1')ans2++;
		if(i%2==0)if(s[i]!='0')ans2++;
	}
	cout<<min(ans1,ans2);
	return 0;
}

Measured memory_bytes: 512
N = input()
a = map(int, raw_input().split( ))

mean = sum(a)/len(a)
upper = mean+1
lower = mean-1

cost = sum(map(lambda x: (x - mean)*(x - mean), a))
costU = sum(map(lambda x: (x - upper)*(x - upper), a))
costL = sum(map(lambda x: (x - lower)*(x - lower), a))

print min(cost,costU,costL)


Measured memory_bytes: 2568
#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define ff first
#define ss second
#define pb push_back
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int mod = 1e9 + 7;
const int cmod = 998244353;
const int N = 1e6 + 5;
const int inf = 1e16 + 2;


void solve(int Case) {
    int n;
    cin >> n;
    map<string , int> m;
    for(int i = 0 ; i < n ; i ++) {
        string s;
        cin >> s;
        m[s] ++;
    }
    cout << "AC x " << m["AC"] << '\n';
    cout << "WA x " << m["WA"] << '\n';
    cout << "TLE x " << m["TLE"] << '\n';
    cout << "RE x " << m["RE"] << '\n';
}
/*
 */

int32_t main()
{
    fastio
    int testcase = 1;
    //cin >> testcase;
    int Case = 1;
    while(testcase --) {
        solve(Case ++);
    }
    return 0;
}
 



Measured memory_bytes: 3600
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String S = sc.next();
        int count = 0;
        int max = 0;


        for(int i = 0; i<S.length(); i++) {
            switch(S.charAt(i)) {
                case 'A':
                case 'T':
                case 'C':
                case 'G':
                    count++;
                    break;
                default:
                    count = 0;
            }
            if (count > max){
                max = count;
            }
        }
        System.out.println(max);
    }
}

Measured memory_bytes: 21588
#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<P,ll> PP;
typedef vector<vector<ll> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e9;

/* BIT: 区間和の更新や計算を行う構造体
    初期値は a_1 = a_2 = ... = a_n = 0
    ・add(i,x): a_i += x とする
    ・sum(i): a_1 + a_2 + ... + a_i を計算する
    計算量は全て O(logn)
*/
template <typename T>
struct BIT {
    int n;          // 配列の要素数(数列の要素数+1)
    vector<T> bit;  // データの格納先
    BIT(int n_) : n(n_ + 1), bit(n, 0) {}
    void add(int i, T x) {
        for (int idx = i; idx < n; idx += (idx & -idx)) {
            bit[idx] += x;
        }
    }
    T sum(int i) {
        T s(0);
        for (int idx = i; idx > 0; idx -= (idx & -idx)) {
            s += bit[idx];
        }
        return s;
    }
    T query(int l, int r) { return sum(r - 1) - sum(l - 1); }

    int lower_bound(T w) { // a_1 + a_2 + ... + a_x >= w となるような最小の x を求める(ただし a_i >= 0)
        if (w <= 0) {
            return 0;
        } else {
            int x = 0, r = 1;
            while (r < n) r = r << 1;
            for (int len = r; len > 0; len = len >> 1) { // 長さlenは1段下るごとに半分に
                if (x + len < n && bit[x + len] < w) { // 採用するとき
                    w -= bit[x + len];
                    x += len;
                }
            }
            return x + 1;
        }
    }
};

//入力
ll n,q;
vector<ll>c;


int main(){
    cin >> n >> q;
    c.resize(n+1);
    rep(i,n)cin>>c[i+1];
    BIT<int>bit(n);
    vector<int>lastAppeared(n+1,0);
    vector<PP>que(q);
    rep(i,q){
        int l,r;
        cin >> l >> r;
        que[i]={{r,l},i};
    }
    sort(que.begin(),que.end());
    int j = 0;
    vector<ll>ans(q);
    rep(i,q){
        int r = que[i].first.first;
        int l = que[i].first.second;
        for(int k=j+1;k<=r;k++){
            ll now = c[k];
            if(lastAppeared[now]==0){
                lastAppeared[now]=k;
                bit.add(k,1);
            }
            else{
                ll last = lastAppeared[now];
                bit.add(last,-1);
                lastAppeared[now]=k;
                bit.add(k,1);
            }
        }
        j = r;
        ans[que[i].second]=bit.query(l,r+1);
    }
    rep(i,q){
        cout << ans[i] << endl;
    }

    return 0;
}


Measured memory_bytes: 27144
n , k = map(int,input().split())
s = input()

if s[k - 1] == "A":
    print(s[:k - 1] + "a" + s[k:])
elif s[k - 1] == "B":
    print(s[:k - 1] + "b" + s[k:])
else:
    print(s[:k - 1] + "c" + s[k:])


Measured memory_bytes: 2940
S=list(input())
S_f=S[:(len(S)-1)//2]
S_l=S[(len(S)+1)//2:]

if S_f!=S_l:
    print("No")
    exit()

for i in range(len(S_f)//2):
    k=-1*i-1
    if S_f[i]!=S_f[k]:
        print("No")
        exit()
print("Yes")

Measured memory_bytes: 3060