akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
set<int> E[101010];
ll sum;
int D[101010],RD,SP[101010];
int U,V;


int dfs(int cur,int pre,int type) {
	int ret=type+SP[cur];
	FORR(r,E[cur]) if(r!=pre) ret += dfs(r,cur,-type);
	sum += abs(ret);
	return ret;
}
void dfs2(int cur,int pre,int d) {
	D[cur]=d;
	FORR(r,E[cur]) if(r!=pre) {
		
		if(D[r]>=0) {
			if(RD==0) U=cur, V=r, RD=d+1-D[r];
		}
		else dfs2(r,cur,d+1);
	}
}

ll test(int v) {
	sum=abs(v);
	SP[U]=v;
	SP[V]=-v;
	dfs(U,-1,1);
	return sum;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	FOR(i,M) {
		cin>>x>>y;
		E[x-1].insert(y-1);
		E[y-1].insert(x-1);
	}
	
	if(M==N-1) {
		x = dfs(0,-1,1);
		if(x!=0) sum=-1;
	}
	else {
		MINUS(D);
		dfs2(0,-1,0);
		E[U].erase(V);
		E[V].erase(U);
		
		if(RD%2==1) {
			x = dfs(U,-1,1);
			if(abs(x)%2==1) return _P("-1\n");
			sum = abs(x)/2;
			SP[U]-=x/2;
			SP[V]-=x/2;
			dfs(U,-1,1);
		}
		else {
			x = dfs(U,-1,1);
			ll tsum=sum;
			if(x) return _P("-1\n");
			
			ll L=-101010,R=101010;
			while(R-L>5) {
				int M1=(2*L+R)/3;
				int M2=(L+R*2)/3;
				ll sum1=test(M1);
				ll sum2=test(M2);
				if(sum1<sum2) R=M2;
				if(sum1>sum2) L=M1;
				if(sum1==sum2) L=M1,R=M2;
			}
			for(x=L;x<=R;x++) tsum=min(tsum,test(x));
			sum=tsum;
		}
		
	}
	cout<<sum<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


Measured memory_bytes: 20992
#include <iostream>
        #include <vector>
        #include <set>
#include <algorithm>
         
        using namespace std;
         
        int main()
        {
        int M, N;
        multiset< long long int > S;
         
        cin >> M;
        for(int i = 0; i < M; i++) {
        long long int tmp;
        cin >> tmp;
        S.insert(tmp);
        }
        cin >> N;
        multiset< long long int > T;
        vector< long long int > a;
        for(int i = 0; i < N; i++) {
        long long int tmp;
        cin >> tmp;
        T.insert(tmp);
        a.push_back(tmp);
        }
        
        sort(a.begin(), a.end());
        a.erase(unique(a.begin(), a.end()), a.end());
        for(int i = 0; i < a.size(); i++) {
          if(T.count(a[i]) > S.count(a[i])) {cout << "NO" << endl; return 0;}
        }
     
        cout << "YES" << endl;
         
        return 0;
        }

Measured memory_bytes: 20592
// In the name of God

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool smin(T &a, const T &b)   { return a > b ? a = b : a;    }
template<typename T> inline bool smax(T &a, const T &b)   { return a < b ? a = b : a;    }

typedef long long LL;

const int N = (int) 505, oo = 1000 * 1000 * 1000 - 2, mod = (int) 0;
int a[N][N];
int main() {
	int n, m, h, w;
	cin >> n >> m >> h >> w;
	if (n % h == 0 && m % w == 0) {
		cout << "No\n";
		return 0;
	}
	int val = oo / (h * w - 1), neg = -(val * (h * w - 1) + 1);
	cout << "Yes\n";
	for (int x = 0; x < n; ++x)
		for (int y = 0; y < m; ++y) {
			a[x][y] = val;
			if (x % h == h - 1 && y % w == w - 1) {
				a[x][y] = neg;	
			}
		}
	for (int x = 0; x < n; ++x) {
		for (int y = 0; y < m; ++y) {
			cout << a[x][y] << ' ';	
		}
		cout << endl;
	}
}





Measured memory_bytes: 3840
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m+1;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;


int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};


int main(){
  int N;cin >> N;
  vector<int> c(N);REP(i,N)cin>>c[i];
  vector<int> tem(3);
  int t =0;
  int count =0;
  REP(i,N-2){
    REP(j,3){
      tem[j] = c[i+j];
      if(j ==1)t = c[i +j];
    }
    VSORT(tem);
    if(tem[1]==t)count++;
  }
  cout <<count<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (long long i = 0; i < N; i++)
using ll = long long;

void solve(long long N, long long K, std::vector<long long> A) {
    map<ll, ll> cntmap;
    rep(i, N) { cntmap[A[i]]++; }

    if (cntmap.size() <= K) {
        cout << 0 << endl;
        return;
    }
    vector<pair<ll, ll>> pairvec;
    for (auto i = cntmap.begin(); i != cntmap.end(); i++) {
        pairvec.push_back(pair<ll, ll>(i->second, i->first));
    }
    sort(pairvec.begin(), pairvec.end());
    ll ans = 0;
    rep(i, cntmap.size() - K) { ans += pairvec[i].first; }
    cout << ans << endl;
}

int main() {
    long long N;
    scanf("%lld", &N);
    long long K;
    scanf("%lld", &K);
    std::vector<long long> A(N);
    for (int i = 0; i < N; i++) {
        scanf("%lld", &A[i]);
    }
    solve(N, K, std::move(A));
    return 0;
}


Measured memory_bytes: 21280
#include<stdio.h>
int main(){
    double w, h, x, y;
    scanf("%lf %lf %lf %lf", &w, &h, &x, &y);
    if (x*2==w && y*2==h) printf("%lf 1\n", w*h/2);
    else printf("%lf 0\n", w*h/2);
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <string>
#include <ios>
using namespace std;

struct Node
{
    int key;
    struct Node * left;
    struct Node * right;
    Node(int key_) : key(key_), left(nullptr), right(nullptr) {};
};

struct BinarySearchTree
{
    int n_node;
    Node * root;
    
    // Constructor
    BinarySearchTree(): n_node(0), root(nullptr) {};
    
    // Insert
    void insert(int key){
        Node * z = new Node{key};
        Node * y = nullptr;
        Node * x = root;
        while (x != nullptr){
            y = x;
            if (key < x->key){
                x = x->left;
            }else{
                x = x->right;
            }
        }
        if (y == nullptr){
            root = z;
        }else if (key < y->key){
            y->left = z;
        }else{
            y->right = z;
        }
        ++n_node;
    }
    
    // Find
    bool find(int key){
        Node * x = root;
        while (x != nullptr){
            if (key < x->key){
                x = x->left;
            }else if (key > x->key){
                x = x->right;
            }else{
                return true;
            }
        }
        return false;
    }
    
    // print
    void print(){
        _inorder_walk(root);
        cout << endl;
        _preorder_walk(root);
        cout << endl;
    }
    
    //inorder walk
    void _inorder_walk(Node * node){
        if (node == nullptr) return;
        _inorder_walk(node->left);
        cout << ' ' << node->key;
        _inorder_walk(node->right);        
    }
    
    //preorder walk
    void _preorder_walk(Node * node){
        if (node == nullptr) return;
        cout << ' ' << node->key;
        _preorder_walk(node->left);
        _preorder_walk(node->right);
    }
    
    
};

int main()
{
    ios::sync_with_stdio(false);
    int m = 0;
    cin >> m;
    
    BinarySearchTree bst;
    string command;
    int key;
    for (int i = 0; i != m; ++i){
        cin >> command;
        if (command[0] == 'i'){
            cin >> key;
            bst.insert(key);
        }else if (command[0] == 'f'){
            cin >> key;
            cout << (bst.find(key) ? "yes" : "no") << endl;
        }else if (command[0] == 'p'){
            bst.print();
        }else{
            cout << "unexpected command! " << command << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 15520
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

int P[100005];
int main()
{
	memset(P,0x3f3f,sizeof(P)); P[0]=0;
	for(int i=1;i<=100000;i++)
	{
		P[i]=min(P[i],P[i-1]+1);
		for(int j=1;i>=(int)pow(6,j);j++)
			P[i]=min(P[i],P[i-(int)pow(6,j)]+1);
		for(int j=1;i>=pow(9,j);j++)
			P[i]=min(P[i],P[i-(int)pow(9,j)]+1);
	}
	
	int n;
	scanf("%d",&n);
	printf("%d\n",P[n]);
	return 0;
}


Measured memory_bytes: 768
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

int main () {
    int N, M;
    cin >> N >> M;

    vector<int> X(M);
    for(int i = 0; i < M; i++) {
        cin >> X[i];
    }

    
    if(N >= M) {
        cout << 0 << endl;
        return 0;
    }
    
    sort(X.begin(), X.end());
    int total = X[M-1] - X[0];

    vector<int> D(M-1);
    for(int i = 0; i < M-1; i++) {
        D[i] = X[i+1] - X[i];
    }
    sort(D.begin(), D.end(), greater<int>());

    for(int i = 0; i < N - 1; i++) {
        total -= D[i];
    }

    cout << total << endl;
    return 0;
}

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

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

	for(int i=0;i<a;i++){
		if(i==0) c=b;
		if(i>0) c=c*(b-1);
	}

	cout << c << endl;
	return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <complex>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;

typedef complex<double> Point, Vector;
typedef pair<Point, Point> Segment, Line;
typedef vector<Point> Polygon;

#define X real()
#define Y imag()
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

const double pi = acos(-1);

double dot(Vector a, Vector b){
    return a.X * b.X + a.Y * b.Y;
}

double cross(Vector a, Vector b){
    return a.X * b.Y - a.Y * b.X;
}

static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;

int ccw(Point p0, Point p1, Point p2){
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if(cross(a, b) > EPS) return COUNTER_CLOCKWISE;
    if(cross(a, b) < -EPS) return CLOCKWISE;
    if(dot(a, b) < -EPS) return ONLINE_BACK;
    if(norm(a) < norm(b)) return ONLINE_FRONT;

    return ON_SEGMENT;
}

double convexArea(Polygon pol){
    int n = pol.size();
    double S = 0;
    for(int i=0; i<n; i++){
        S += cross(pol[i], pol[(i+1)%n]);
    }

    return S / 2;
}

Point getCrossPointLL(Line l1, Line l2){
    double a = cross(l1.second - l1.first, l2.second - l2.first);
    double b = cross(l1.second - l1.first, l1.second - l2.first);
    if(abs(a) < EPS && abs(b) < EPS) return l2.first;
    return l2.first + (l2.second - l2.first) * (b / a);
}

Polygon convexCut(Polygon pol, Line l){
    Polygon q;
    int n = pol.size();
    for(int i=0; i<n; i++){
        Point a = pol[i];
        Point b = pol[(i+1)%n];
        if(ccw(l.first, l.second, a) != CLOCKWISE) q.emplace_back(a);
        if(ccw(l.first, l.second, a) * ccw(l.first, l.second, b) < -EPS){
            q.emplace_back(getCrossPointLL(Line(a, b), l));
        }
    }

    return q;
}

double solve(Polygon pol){
    double S = convexArea(pol);
    double lb = 0, ub = pi;

    if(convexArea(convexCut(pol, Line(Point(0, 0), Point(1, 0)))) > S / 2){
        lb = pi;
        ub = 0;
    }

    double mid;
    for(int i=0; i<10000; i++){
        mid = (lb + ub) / 2;
        double midS = convexArea(convexCut(pol, Line(Point(0, 0), Point(cos(mid), sin(mid)))));

        if(abs(midS - S / 2) < EPS) break;

        if(midS < S / 2){
            lb = mid;
        }
        else{
            ub = mid;
        }
    }

    return mid;
}

int main(){
    while(1){
        int n; cin>>n;
        if(n == 0) return 0;
        Polygon pol;
        for(int i=0; i<n; i++){
            double x, y; cin>>x>>y;
            pol.emplace_back(x, y);
        }

        double rad = solve(pol);

        printf("%.15f %.15f\n", cos(rad), sin(rad));
    }
}


Measured memory_bytes: 3588
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <string>
#include <utility>
#include <map>
#include <deque>
using namespace std;
#define rep(i,n) for(int i=1;i<=(int)(n);i++)
#define Rep(i,n) for(int i=0;i<=(int)(n-1);i++)
typedef long long ll;
typedef map<int,int> mint;
typedef pair<int,int> pint;
typedef pair<ll,ll> pll;
typedef deque<pair<ll,ll>> dep;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
int inf=1e9+1000;
const int mod=1e9+7;
ll infi=1e18+100;

//x=10111;
//x<<2 = 1011100 (右シフト),x>>2 = 101 (左シフト)
//(x>>2)&1 = 101 & 001 = 1
//iビット目は、(x>>i) & 1 でわかる(x>>i)%2 でもよい


ll N;
ll A[100003];

ll pow2(ll n){
	if(n==0) return 1;
	else if(n%2==0) return (pow2(n/2)*pow2(n/2))%mod;
	else return (2*pow2(n-1))%mod;
}



int main(){
	cin>>N;
	Rep(i,N) cin>>A[i];
	sort(A,A+N);
	ll ans=1;
	if(N%2==0){
		bool f=true;
		Rep(i,N){
			if(A[i]!=(i/2)*2+1){
				f=false;
				break;
			}
		}
		if(f){
			cout<<pow2(N/2)<<endl;
		}
		else cout<<0<<endl;
	}
	else{
		bool f=true;
		Rep(i,N){
			if(A[i]!=((i+1)/2)*2){
				f=false;
				break;
			}
		}
		if(f){
			cout<<pow2(N/2)<<endl;
		}
		else cout<<0<<endl;
	}
	
	  return 0;
  }


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
int main() {
  int a,b,m;
  cin >> a >> b >> m;
  vector<int> vec1(a);
  vector<int> vec2(b);
  for ( int i = 0 ; i < a ; i++ ) {
    cin >> vec1.at(i);
  }
  for ( int i = 0 ; i < b ; i++ ) {
    cin >> vec2.at(i);
  }
  
  vector<int> vec3(m+1);
  for ( int i = 0 ; i < m ; i++ ) {
    int x,y,c;
    cin >> x >> y >> c;
    vec3.at(i) = vec1.at(x-1)+vec2.at(y-1)-c;
  }
  
  sort(vec1.begin(),vec1.end());
  sort(vec2.begin(),vec2.end());
  vec3.at(m) = vec1.at(0) + vec2.at(0);
  
  sort(vec3.begin(),vec3.end());
  
  cout << vec3.at(0) << endl;
}


Measured memory_bytes: 1408
from sys import stdin

N = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())

N = N[::-1]

dp = [[0] * len(N) for i in range(3)]

dp[0][0] = int(N[0])

for i in range(1, len(N)):
    if N[i] == '0':
        dp[0][i] = dp[0][i - 1]
    else:
        dp[0][i] = int(N[i]) + 9 * i

for i in range(1, len(N)):
    if N[i] == '0':
        dp[1][i] = dp[1][i - 1]
    else:
        dp[1][i] = dp[0][i - 1] + 9 * 9 * i * (i - 1)  // 2 + 9 * i * (int(N[i]) - 1)

for i in range(2, len(N)):
    if N[i] == '0':
        dp[2][i] = dp[2][i - 1]
    else:
        dp[2][i] = dp[1][i - 1] + 9 * 9 * 9 * i * (i - 1) * (i - 2) // 6 + 9 * 9 * i * (i - 1) // 2 * (int(N[i]) - 1)

print(dp[K - 1][len(N) - 1])

Measured memory_bytes: 38384
#include <iostream>
#include <vector>
using namespace std;
const int mxn = 1e6;
int isNotPrime[mxn + 10]; //a[i] = 0 means i is prime.
vector <int> primes;
void sieve()
{
    isNotPrime[1] = isNotPrime[0] = 1;
    for(int i = 4; i <= mxn; i += 2)
        isNotPrime[i] = 1;
    for(int i = 3; i * i <= mxn; i += 2)
        if(!isNotPrime[i])
            for(int k = i * i; k <= mxn; k += 2 * i)
                isNotPrime[k] = 1;
    
    primes.push_back(2);
    for(int i = 3; i <= mxn; i += 2)
        if(!isNotPrime[i]) 
            primes.push_back(i);
}


long long gcd(long long a, long long b)
{
    if(b == 0) return a;
    return gcd(b, a % b);
}

int main()
{
    sieve();
    long long a, b;
    cin >> a >> b;
    long long gcd_ab = gcd(a, b);
 //   cout << gcd_ab << "\n";
    int cnt = 0;
    
    for(int x: primes){
        if(gcd_ab % x == 0) ++cnt;
        
        while(gcd_ab % x == 0) gcd_ab /= x;


    }
    if(gcd_ab != 1) cnt++;

    cout << cnt + 1 << "\n";
    return 0;
}

Measured memory_bytes: 4732
#include<bits/stdc++.h>
using namespace std;
#define INF 1e9

int main(){

    int n, m, k; cin >> n >> m >> k;
    int v = n;
    vector<vector<pair<double, int> > > g(v);
    for(int i = 0; i < m; i++){
        int a, b;
        double l;
        cin >> a >> b >> l;
        a--;
        b--;
        //int c = n + i;
        double cost = l;
        g[b].push_back({cost, a});
        g[a].push_back({cost, b});
    }


    //ダイクストラ
    priority_queue<pair<double, int> > wait;
    vector<double> result(v, INF);

    //複数の始点を追加
    for(int i = 0; i < k; i++){
        int s; cin >> s;
        s--;
        result[s] = 0.0;
        wait.push(make_pair(0.0, s));
    }

    while(!wait.empty()){

        int nowpoint = wait.top().second;
        double nowcost = -wait.top().first;
        //cout << nowpoint << " " << nowcost << endl;
        wait.pop();
        
        if(result[nowpoint] < nowcost) continue;

        //今いる頂点と隣接しているすべての頂点をなめる
        for(int i = 0; i < (int)g[nowpoint].size(); i++){

            int nextpoint = g[nowpoint][i].second;
            double nextcost = nowcost + g[nowpoint][i].first;
            //現時点より安く到達できそうであれば、結果を更新して優先度付きキューに格納
            if(result[nextpoint] >= nextcost){
                result[nextpoint] = nextcost;
                wait.push(make_pair(-nextcost, nextpoint));
            }
        }
    }

    double ans = 0.0;
    for(int i = 0; i < n; i++){
        for(auto e : g[i]){
            ans = max(ans, (result[i] + result[e.second] + e.first) / 2);
        }
    }

    cout << round(ans) << endl;
    return 0;
}


Measured memory_bytes: 9028
import sys
import cmath
from itertools import combinations

inf = float('inf')

n, *xy = map(int, sys.stdin.read().split())
xy = zip(*[iter(xy)] * 2)
z = [x + y*1j for x, y in xy]

def circumcenter(z1, z2, z3):
    a = z2 - z1
    b = z3 - z1
    numerator = a * b * (b.conjugate() - a.conjugate())
    denominator = a * b.conjugate() - a.conjugate() * b
    o = numerator / denominator + z1
    return o

def center(z1, z2):
    return (z1 + z2) / 2

def main():
    cand = []
    for comb in combinations(z, 3):
        try: 
            cand.append(circumcenter(*comb))
        except:
            pass
    for comb in combinations(z, 2):
        cand.append(center(*comb))
    
    res = inf
    for o in cand:
        r = 0
        for i in z:
            r = max(r, abs(o - i))
        res = min(res, r)
    
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

Measured memory_bytes: 3812
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#define _USE_MATH_DEFINES
#include<math.h>
#include<queue>
#include<deque>
#include<stack>
#include<cstdio>
#include<utility>
#include<set>
#include<list>
#include<cmath>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const ll dx[4] = { 0,1,0,-1 };
const ll dy[4] = { -1, 0, 1,0 };
const ll INF = 1e9 + 7;
int gcd(int x, int y) {
	if (x < y)swap(x, y);
	if (y == 0)return x;
	return gcd(y, x%y);
}
void mul(ll a, ll b) {
	a = a * b % INF;
}
double mysqrt(double x) {
	double l = 0, r = x;
	for (int i = 0; i < 64; ++i) {
		double m = (l + r) / 2.0;
		if (m*m < x)l = m;
		else r = m;
	}
	return l;
}
///////////////////////////////////////

ll X, Y;
ll fac[1000010], finv[1000010], inv[1000010];
//テーブルをつくる前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;//mod pにおける1,2,,,nの逆元
	for (int i = 2; i < 1000010; ++i) {
		fac[i] = fac[i - 1] * i%INF;
		inv[i] = INF - inv[INF%i] * (INF / i) % INF;
		finv[i] = finv[i - 1] * inv[i] % INF;
	}
}

ll COM(int n, int k) {
	if (n < k)return 0;
	if (n < 0 || k < 0)return 0;
	return fac[n] * (finv[k] * finv[n - k] % INF) % INF;
}

int main() {
    cin >> X >> Y;
	bool ng = true;
	if ((X + Y) % 3 != 0)ng = false;
	if ((X + Y) / 3 < abs(X - Y))ng = false;
	int a, b;
	a = (2 * Y - X) / 3;
	b = (2 * X - Y) / 3;
	COMinit();
	if (!ng) {
		cout << 0 << endl;
	}
	else {
		cout << COM(a + b, a) << endl;
	}
	return 0;
}


Measured memory_bytes: 23680
N = int(input())
a = [int(x) for x in input().split()]

x, y = -1, -1

for i in range(N) :
    for j in range(N) :
        if i != j and abs(a[i] - a[j]) % (N - 1) == 0 :
            x, y = i, j
            
print(a[x], a[y])



Measured memory_bytes: 5624
n, m = map(int, input().split())
s, t = [input() for i in range(2)]

def gcd(a, b):
    if a < b:
        a, b = b, a
    while(a%b!=0):
        a, b = b, a%b
    return b

a = len(s)
b = len(t)

g = gcd(a, b)

if all([s[a*i//g]==t[b*i//g] for i in range(g)]):
    print(a*b//g)
else:
    print(-1)

Measured memory_bytes: 3572
N = int(input())
B = input().split(' ')
B = [int(B[i]) for i in range(N-1)]
SUM = [B[0], B[-1]]
for i in range(N-2):
  if B[i]>=B[i+1]:
    SUM.append(B[i+1])
  else:
    SUM.append(B[i])
print(sum(SUM))

Measured memory_bytes: 9172
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P>IP;
typedef vector<ll> V;
typedef vector<V> V2;
typedef vector<vector<P> > G;
void g_dir(G &graph, ll a, ll b, ll w = 1){graph[a].push_back(P(b, w));}
void g_undir(G &graph, ll a, ll b, ll w = 1){g_dir(graph, a, b, w);g_dir(graph, b, a, w);}
#define rep(i, n) for(ll (i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for(ll (i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for(ll (i) = (n) - 1; (i) >= 0; (i)--)
#define rrep1(i, n) for(ll (i) = (n); (i) >= 1; (i)--)
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}
const ll INF = 1145141919;
const ll MOD = 1000000007;
const ll NUM = 101010;

int main(){
    
    ll n, m, d;
    cin >> n >> m >> d;
    if(d == 0)printf("%.10f\n", 1.0 * (m - 1) / n);
    else printf("%.10f\n", 2.0 * (n - d) * (m - 1) / n / n);
    
    return 0;
}

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

#define fast() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define o(a) { cout << #a << ":" << (a) <<" ";}
#define ov(a) { cout << "["; \
	for(int i = 0; i < (a).size(); ++i){ \
		cout << i <<":" << a[i] <<" "; \
		if(i % 10 == 9) cout <<"\n"; \
		} \
	cout << "]"; \
}
#define ovv(a) { cout << "["; \
	for(int i = 0; i < a.size(); ++i){ \
		for(int j = 0; j < a[i].size(); ++j){ \
			cout << "(" << i <<"," <<j <<")" <<":" << a[i][j] <<" "; \
		} \
		cout << "\n"; \
	} \
	cout << "]"; \
}
#define op(a) { cout <<"[" << #a << ".fi:"<<a.first; \
					cout << " "<< #a << ".se:" << a.second <<"]"; }
//priority_queue <int>a;
#define ll long long
const int mod = 1e9 + 7;
const int nax = 1e5 + 5;
int n, m, k, q;

int main(){
	fast();
	int circle_radius;
   float PI_VALUE=3.14159265359, circle_area, circle_circumf;

   scanf("%d",&circle_radius);

   //Calculate and display Area
   circle_area = PI_VALUE * circle_radius * circle_radius;

   //Caluclate and display Circumference
   circle_circumf = 2 * PI_VALUE * circle_radius;
   printf("%.10f",circle_circumf);

   return(0);

}



Measured memory_bytes: 3852
#include<stdio.h>
#include<string.h>
int main(){
    int no,i,left=0,right=0;
    scanf("%d",&no);
    getchar();
    char s[200];
    gets(s);
    for(i=0;i<no;i++){
        if(s[i]=='(') left++;
        if(s[i]==')'){
            if(left==0) right++;
            else left--;
        }
    }
    for(i=0;i<right;i++) printf("(");
    printf("%s",s);
    for(i=0;i<left;i++) printf(")");
    printf("\n");
    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int n, k;
vector<int> a;

bool f(int m){
    bool dp[5010] = {};
    dp[0] = true;
    rep(i, 0, n){
        if(i == m) continue;
        repb(j, 5000, 0){
            if(j - a[i] >= 0) dp[j] |= dp[j - a[i]];
        }
    }
    rep(i, max(0LL, k - a[m]), k){
        if(dp[i]) return true;
    }
    return false;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    a.resize(n);
    rep(i, 0, n) cin >> a[i];
    sort(all(a));
    int l = -1, r = n;
    rep(i, 0, 30){
        int mid = (l + r) / 2;
        if(f(mid)) r = mid;
        else l = mid;
    }
    cout << r << endl;
}

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

using namespace std;

#define intt long long
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);

const int N = 1005;
const int INF = 1000006;

int n, m;

char a[N][N];
int d[N][N];

int d1x[] = {1, -1, 0, 0};
int d1y[] = {0, 0, 1, -1};

int d2x[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2};
int d2y[] = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2};

bool valid (int x, int y) {
	return (1 <= x && x <= n && 1 <= y && y <= m);
}

void bfs (int vx, int vy) {
	d[vx][vy] = 0;
	deque <pair <int, int>> dq;
	dq.push_front({vx, vy});
	while (!dq.empty()) {
		int fromx = dq.front().first;
		int fromy = dq.front().second;
		dq.pop_front();
		for (int i = 0; i < 4; i ++) {
			int tox = fromx + d1x[i];
			int toy = fromy + d1y[i];
			if (valid(tox, toy) && a[tox][toy] == '.' && d[fromx][fromy] + 0 < d[tox][toy]) {
				d[tox][toy] = d[fromx][fromy] + 0;
				dq.push_front({tox, toy});
			}
		}
		for (int i = 0; i < 20; i ++) {
			int tox = fromx + d2x[i];
			int toy = fromy + d2y[i];
			if (valid(tox, toy) && a[tox][toy] == '.' && d[fromx][fromy] + 1 < d[tox][toy]) {
				d[tox][toy] = d[fromx][fromy] + 1;
				dq.push_back({tox, toy});
			}
		}
	}
}

int main() {

	FAST;


	cin >> n >> m;
	int sx, sy, fx, fy;
	cin >> sx >> sy;
	cin >> fx >> fy;
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			cin >> a[i][j];
			d[i][j] = INF;
		}
	}
	bfs(sx, sy);
	cout << ((d[fx][fy] != INF) ? d[fx][fy] : -1);
	return 0;
}

Measured memory_bytes: 8556
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef long double ld;
typedef std::vector<long double> vld;
typedef std::vector<std::vector<long double>> vvld;
using namespace std;
void yesno(bool state);

void Main(){
    ll A,V,B,W,T;
    cin >> A >> V >> B >> W >> T;
    yesno(abs(A-B) <= (V-W) * T);
    
}

int main(int argc, char const *argv[])
{
    Main();
    return 0;
}

void yesno(bool state){
    if(state)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

Measured memory_bytes: 3596
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int a,b,b1,i=0,con,con2;
cin>>a>>b;
b1=b;
for( i=0;b>0;i++)
{
    b=b/10;
}



a=a*(pow(10,i));

con=a+b1;

con2=sqrt(con);

if(con==(con2*con2))
    cout<<"Yes";
else
    cout<<"No";
    return 0;
}


Measured memory_bytes: 4064
import sys
N = int(input())
t, x, y = [0] * N, [0] * N, [0] * N
for i in range(N):
	t[i],x[i],y[i] = map(int, input().split())

#for i in range(N):
#	print(t[i],x[i],y[i], file=sys.stderr)

def solve(px,py,i,pt):
	global t, x, y
	diff = abs(x[i] - px) + abs(y[i] - py)
	if diff <= t[i] - pt:
		return True
	else:
		return False

ok = True
for i in range(N):
	shortest_time = 0
	if i == 0:
		if solve(0,0,i,0) == True:
			shortest_time = abs(x[i]) + abs(y[i])
		else:
			ok = False
			break
	else:
		if solve(x[i-1],y[i-1],i,shortest_time) == True:
			shortest_time = abs(x[i] - x[i-1]) + abs(y[i] - y[i-1])
		else:
			ok = False
			break
#	print("shortest_time:", shortest_time, file=sys.stderr)

if ok == True:
	for i in range(N):
		if t[i] % 2 != ((x[i] + y[i]) % 2):
			ok = False


print("Yes" if ok == True else "No")

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//==================================

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif

    double r;
    cin >> r;
    cout << setprecision(18) << r * M_PI * 2.0 << endl;

    return 0;
}


Measured memory_bytes: 3800
#!/usr/bin/env python3
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    S = readline().rstrip().decode()
    S_f = S[:len(S)//2]
    S_s = S[-len(S)//2:][::-1]
    ans = 0
    for s1, s2 in zip(S_f, S_s):
        if s1 != s2:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main(){
	int n;
	int k;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	int total=0;
	for(int i=1;i<=k;i++){
		total+=a[i];
	}
	cout<<total<<endl;
}

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

#define ll long long int
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define repm(i, a, n) for (ll i = a; i >= n; i--)
#define INF 1e9
#define LINF 1e17
#define MOD (int)(1e9 + 7)
#define mod (int)(1e9 + 7)
#define pi 3.141592653589
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vs vector<string>
#define vii vector<vector<int>>
#define vllll vector<vector<ll>>
#define ALL(a) (a).begin(), (a).end()

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

void print(bool f)
{
    cout << (f ? "Yes" : "No") << endl;
}

void Print(bool f)
{
    cout << (f ? "YES" : "NO") << endl;
}

vb calc_prime(ll Size)
{
    vb prime(Size + 1, true);
    if (Size >= 0)
        prime[0] = false;
    if (Size >= 1)
        prime[1] = false;

    for (int i = 2; i * i <= Size; i++)
        if (prime[i])
            for (ll j = i + i; j <= Size; j += i)
                prime[j] = false;
    return prime;
}

int main(void)
{
    vb prime = calc_prime(1e5);
    vll sum(1e5 + 1, 0);
    rep(i, 2, 1e5 + 1)
        sum[i] = sum[i - 1] + (prime[i] && prime[(i + 1) / 2] && (i + 1) % 2 == 0);

    ll q;
    cin >> q;
    rep(i, 0, q)
    {
        ll l, r;
        cin >> l >> r;
        cout << sum[r] - sum[l - 1] << endl;
    }
}

Measured memory_bytes: 1408
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long ans = (N + K) % K;

		ans = Math.min(ans, Math.abs(ans - K));

		out.println(ans);

	}

	public static void main(String[] args) {
		new Main()._main(args);
	}
}


Measured memory_bytes: 21716
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        try(Scanner sc = new Scanner(System.in)){
            Set<String> dict = new HashSet<>();
            int n = sc.nextInt();
            for(int i = 0; i < n; i++) {
                String s = sc.next();;
                if(s.equals("insert")) {
                    String str = sc.next();
                    dict.add(str);
                } else {
                    String str = sc.next();
                    if (dict.contains(str)) {
                        System.out.println("yes");
                    } else {
                        System.out.println("no");
                    }
                }
            }
        }
    }
}


Measured memory_bytes: 218788
N,K,Q = map(int,input().split())
A = [0]*Q
score = []
for i in range(Q):
  A[i] = int(input())
for _ in range(N):  
  score.append(K-Q)
for i in range(Q):
  a =  A[i]
  score[a-1] += 1

for j in range(N):
  if score[j] > 0:
    print('Yes')
  else:
    print('No')

Measured memory_bytes: 11780
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
using namespace std;

int N, M;
vector<int> x;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> M;  
  x.resize(M);
  vector<int> diff(M-1);
  for(int i=0; i<M; ++i) cin >> x[i];
    sort(all(x));
  for(int i=1; i<M; ++i)
      diff[i-1] = x[i] - x[i-1];
  sort(all(diff), greater<int>());
  int res = 0;
  if(N >= M){
    res = 0;
  }
  else{
    while(N < M){
       res += diff.back();
       diff.pop_back();
       ++N;
    }
  }
  cout << res << "\n";
  return 0;
}


Measured memory_bytes: 1024
object Main {
  def main(args: Array[String]): Unit = {
    val Array(x, y, _*) = io.StdIn.readLine().split(" ").map(_.toInt)
    println(gcd(x, y))
  }

  @annotation.tailrec
  def gcd(x: Int, y: Int): Int = {
    if (y == 0)
      x
    else
      gcd(y, x % y)
  }
}

Measured memory_bytes: 43872
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <list>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <ctime>
#include <assert.h>
using namespace std;

const unsigned long long BASE = (unsigned long long)(1e9 + 7);

#define FROM_0(i, n) for(int i = 0; i < (n); i++)
#define FROM_S(i, s, e) for(int i = (s); i < (e); i++)
#define ITER_BEGIN_END(iter, vec) for(auto iter = vec.begin(); iter != vec.end(); iter++)

int main()
{
    int n;
    cin >> n;
    vector<long long> add, sub;
    FROM_0(i, n)
    {
        long long x, y;
        cin >> x >> y;
        add.push_back(x + y);
        sub.push_back(x - y);
    }
    std::sort(add.begin(), add.end());
    std::sort(sub.begin(), sub.end());
    cout << max(add.back() - add.front(), sub.back() - sub.front()) << endl;

    return 0;
}


Measured memory_bytes: 6580
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=1e9;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};

int main() {
  int n;
  cin>>n;
  vector<string> s(n);
  rep(i,n) cin>>s[i];
  map<string,int> mp;
  rep(i,n){
    mp[s[i]]++;
  }
  int mx=0;
  for(auto x:mp) mx=max(mx,x.second);
  for(auto x:mp){
    if(x.second==mx) cout<<x.first<<endl;
  }
}

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

using namespace std;

int main()
{
    long int A;
    long int B;
    long int C;
    long int D;
    long int X;
    long int LCM;
    long int po=0;
    cin >> A >> B >> C >> D;
    X=C*D;
    long int calcC;
    long int calcD;
    if(C<D){
        calcC = D;
        calcD = C;
    }else{
        calcC = C;
        calcD = D;
    }
    long int r = calcC%calcD;
    while(r!=0){
        calcC=calcD;
        calcD=r;
        r=calcC%calcD;
    }
    LCM=X/calcD;
    if (A%C==0)
    {
        po = (B/C)-(A/C-1);/* code */
    }else{
        po = (B/C)-(A/C);
    }
    if (A%D==0)
    {
        po = po + (B/D)-(A/D-1);/* code */
    }else{
        po = po + (B/D)-(A/D);
    }
    if (A%LCM==0)
    {
        po = po - ((B/LCM)-(A/LCM-1));/* code */
    }else{
        po = po - ((B/LCM)-(A/LCM));
    }
    
    cout << B-A-po+1;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int ans = n % 1000;
    ans = 1000 - ans;
    if(ans == 1000) ans = 0;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3608
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <random>
#include <fstream>

#define REP(i, N) for(int i = 0; i< N; i++)
using namespace std;
#define ll long long

class Bluedaishou{
public:
    bool operator()(const pair<int, int>& riLeft, const pair<int, int>& riRight) const {
        return riLeft.first < riRight.first; // ここでは、元の < の意味に合わせてあるが、異なる判断も可能。
    }
};
class Reddaishou{
public:
    bool operator()(const pair<int, int>& riLeft, const pair<int, int>& riRight) const {
        return riLeft.second > riRight.second; // ここでは、元の < の意味に合わせてあるが、異なる判断も可能。
    }
};

int main(){
    int N;
    cin >> N;
    vector <pair<int,int>> ab;
    vector <pair<int,int>> cd;
    int a,b,c,d;
    
    REP(i, N){
        cin >> a >> b;
        ab.push_back({a,b});
    }
    REP(i, N){
        cin >> c >> d;
        cd.push_back({c,d});
    }
    sort(ab.begin(),ab.end(),Reddaishou());
    sort(cd.begin(),cd.end(),Bluedaishou());
    REP(i, N){
    //    printf("(%d,%d),(%d,%d)\n",ab[i].first , ab[i].second , cd[i].first ,cd[i].second);
    }
    int pairninatta = 0;
    bool matchsita = false;
    bool mada[100] = {false};
    REP(i, N){
        REP(j, N){
            matchsita = false;
            //printf("(%d,%d),(%d,%d)\n",ab[i].first , ab[i].second , cd[j].first ,cd[j].second);
            if(ab[ i].first < cd[j].first && ab[i].second < cd[j].second && mada[j] == false){
                //printf("A");
    //            printf("(%d,%d),(%d,%d)\n",ab[i].first , ab[i].second , cd[j].first ,cd[j].second);
                mada[j] = true;
                matchsita = true;
                pairninatta++;
                break;
            }
        }
    }
    cout << pairninatta;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <string>


using std::cout;
using std::cin;
using namespace std;

void oneCase () {
    int n;
    cin >> n;
    std::string s;
    std::getline(std::cin, s);
    std::getline(std::cin, s);
    string news = "";
    if (n == 0) {
        std::cout << s << "\n";
        return;
    }
    for (int pos = 0; pos < s.size(); ++pos) {
        news += static_cast<char>((static_cast<int>(s[pos]) - 65 + n) % 26 + 65);
    }
    std::cout << news << "\n";
}




int main() {
    int k = 1;
//    cin >> k;
    for (int i = 0; i < k; ++i) {
        oneCase();
    }

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include<string>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
	int A, B;
	cin >> A >> B;
	int n = A - (B * 2);
	if (n > 0)cout << n;
	else cout << "0";

}

Measured memory_bytes: 384
// 失敗するからこそ そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
// ぜったいあきらめない
#include<bits/stdc++.h>
using namespace std;

#if defined(DAIJOBU)
#include "/home/ichigo/debug.hpp"
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define deb(x...)
#endif
#define FIO {ios::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);}
const int maxn = 1e5 + 50;
pair<int, int> P[maxn];
long long a[maxn];
int main(){
	FIO
	
	int n, m;cin >> n >> m;
	for(int i = 0; i < n;i++)cin >> a[i];
	sort(a, a + n);
	for(int i = 0; i < m;i++){
		cin >> P[i].second >> P[i].first;
	}
	sort(P, P + m);
	reverse(P, P + m);
	int ind = 0;
	for(int i = 0; i < m; i++){
		for(int j = 0; j < P[i].second; j++){
			if(ind >= n || a[ind] > P[i].first)break;
			a[ind++] = P[i].first;
		}
	}
	long long sum = 0;
	for(int i = 0; i < n; i++)sum += a[i];
	cout << sum;
	return 0;
}


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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
 public:
  int n, k, width, numBlock;
  vector<int> a;
  vector<bitset<5000>> dp;
  Solver(LL n) : n(n){};
  void add(bitset<5000>& dp_, int x) {
    dp_ |= dp_ << x;
  }
  bool isUnnecessary(bitset<5000>& dp_, int x) {
    for (int i = k - x; i < k; ++i) {
      if (dp_[i] == 1) return false;
    }
    return true;
  }
  void solve() {
    cin >> k;
    for (int i = 0; i < n; ++i) {
      int tmp;
      cin >> tmp;
      if (tmp < k)
        a.push_back(tmp);
    }
    n = a.size();
    width = n / sqrt(n);
    numBlock = (n + width - 1) / width;
    dp.resize(numBlock);
    for (int i = 0; i < dp.size(); ++i) {
      dp[i][0] = 1;
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < numBlock; ++j) {
        if (i / width == j) continue;
        add(dp[j], a[i]);
      }
    }
    DBG(width, numBlock)
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      auto tmp = dp[i / width];
      for (int j = i / width * width; j < min(n, (i / width + 1) * width); j++) {
        if (j == i) continue;
        add(tmp, a[j]);
      }
      ans += isUnnecessary(tmp, a[i]);
      DBG(i, a[i], ans)
    }
    cout << ans << endl;
  }
};

}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


Measured memory_bytes: 384
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
	int n,a,b;
	while(cin>>n>>a>>b){
		int ans=0;
		for(int i=1;i<=n;i++){
			int tmp=i;
			int sum=0;
			while(tmp>0)
			{
				sum+=tmp%10;
				tmp=tmp/10;	
			}
			if(a<=sum&&sum<=b){
				ans+=i;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>

constexpr long long MOD = 1000000007;

int main()
{
  int n;
  char s[2][53];
  scanf("%d%s%s", &n, s[0], s[1]);
  long long ans = 1;
  int last = 0;
  for (int i = 0; i < n;)
  {
    if (s[0][i] == s[1][i])
    {
      long long p;
      if (last == 0) p = 3;
      else if (last == 1) p = 2;
      else p = 1;
      ans = ans * p % MOD;
      ++i;
      last = 1;
    }
    else
    {
      long long p;
      if (last == 0) p = 3 * 2;
      else if (last == 1) p = 2 * 1;
      else p = 2 + 1;
      ans = ans * p % MOD;
      i += 2;
      last = 2;
    }
  }
  printf("%lld\n", ans);
  return 0;
}

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

int main(){
  int n;
  cin >> n;
  int count =0;
  if(n <=9){
    cout << "Yes"<<endl;
  }
  else if(n <= 81){
    for(int i=2;i<=9;i++){
      if(n%i ==0 && n/i <=9){
        count++;
      }
    }
    if(count > 0){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
  }
  else{
    cout << "No" << endl;
  }
  
  return 0;
}
      

Measured memory_bytes: 256
N = int(input())
A = list(map(int,input().split()))
B = [[i+1,A[i]] for i in range(N)]
B.sort(key=lambda x: x[1])
for j in range(N):
  if j == N-1:
    print(B[j][0])
  else:
    print(B[j][0] ,end=" ")
  

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

using namespace std;

typedef long long int ll;

#define precision(x,d) cout<<fixed<<setprecision(d)<<x

bool check(string s)
{
    int p=0,q=s.length()-1;

    while(p<=q)
    {
        if(s[p]==s[q])
        {
            p++;
            q--;
        }
        else
            return 0;
    }
    return 1;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    string s;
    cin >> s;
    string s1="",s2="";

    int n = s.length();
    for(int i=0 ; i<(n-1)/2 ; i++)
    {
        s1+=s[i];
    }

    for(int i=(n-1)/2 + 1 ; i<n ; i++)
    {
        s2+=s[i];
    }
    if(check(s1) && check(s2) && check(s))
    {
        cout << "Yes";
    }
    else
        cout << "No";


    return 0;
}


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

int main() {
  int N;
  cin >> N;
  
  int s, S, number;
  S = 0;
  number = N;
  while (number >= 10) {
    s = number % 10;
    S += s;
    number = (number - s)/10;
  }
  S += number;
  if (N % S == 0) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;


int main() {
  int N; cin >> N;
  string S, T; cin >> S >> T;
  string result; 
  rep(i,S.length()) {
    result += S[i];
    result += T[i];
  }
  cout << result << endl;
  return 0;
}

Measured memory_bytes: 256
//By Don4ick 
//#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;

#define forn(i, n) for (int i = 1; i <= n; i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define y1 qewr1234

const double PI = acos(-1.0);
const int DIR = 4;
const int X[] = {1, 0, -1, 0};
const int Y[] = {0, 1, 0, -1};

using namespace std;

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

	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);

	int n;
	cin >> n;
	vector < int > a(n);
	vector < bool > used(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	int mx = 0;
	for (int i = 0; i < n; i++)
	{	
		mx = max(mx, a[i]);
	}
	if (mx >= n)
	{
		cout << "Impossible" << endl;
		return 0;
	}
	if (mx == 1)
	{
		if (n == 2)
		{
			cout << "Possible" << endl;
		}
		else
			cout << "Impossible" << endl;
		return 0;
	}
	for (int i = 0; i < n; i++)
	{
		if (a[i] == mx)
		{
			used[i] = true;
			break;
		}	
	}
	for (int i = 1; i <= mx; i++)
	{
		int cur = max(i, mx - i);
		bool found = false;
		for (int j = 0; j < n; j++)
		{
			if (used[j])
				continue;
			if (a[j] == cur)
			{
				found = true;
				used[j] = true;
				break;
			}
		}
		if (!found)
		{
			cout << "Impossible" << endl;
			return 0;
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (used[i])
			continue;
		if (a[i] < (mx + 1) / 2 + 1)
		{
			cout << "Impossible" << endl;
			return 0;
		}
	}
	cout << "Possible" << endl;
		

	return 0;
}



Measured memory_bytes: 256
require "prime"
N, P = gets.split.map &:to_i
gcd = 1
tmp= Math.sqrt(P).to_i
if N >= 2
  if N < P
    Prime.each(tmp) do |p|
      _p = P
      cnt = 0
      while _p % p == 0
        _p /= p
        cnt += 1
      end
      gcd *= p**(cnt/N) if cnt > 0
    end
  end
  puts gcd
else
  puts P
end



Measured memory_bytes: 6808
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; ++i)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;


//dp[i][j]i番目までの商品で価値jを詰め込むのに最小の重さ
int dp[100+1][100*1000+1];

int main()
{
	fill(dp[0],dp[100+1],INF);
	int N,W;
	cin >> N >> W;
	vector<int> w(N),v(N);
	rep(i,N)
	{
		cin >> w[i] >> v[i];
	}
	dp[0][0]=0;
	rep(i,N)
	{
		rep(j,100*1000+1)
		{
			if(j-v[i]<0)
			dp[i+1][j]=dp[i][j];
			else
			dp[i+1][j]= min(dp[i][j],dp[i][j-v[i]]+w[i]);

			//cout<<dp[i+1][j]<<endl;
		}
	}
	int ans = 0;
	rep(i,100*1000+1)if(dp[N][i]!=INF&&dp[N][i]<=W)ans=i;
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 39680
#include<bits/stdc++.h>
using namespace std;
main()
{
	long long  arr[3];
	for(long long  i=0;i<3;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+3);
	long long  sum =0;
	for(long long  i=1;i<3;i++)
	{
		sum += abs(arr[i-1]-arr[i]);
	}
	cout<<sum<<endl;
}

Measured memory_bytes: 256