akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cctype>
using namespace std;

int n, m, p;
string s;
bool error = false;
vector<pair<string, vector<int> > > unit;

int getUnitIndex(const vector<int>& d)
{
  int ret = -1;
  for (int i = 0; i < unit.size(); ++i) {
    bool same = true;
    for (int j = 0; j < unit[i].second.size(); ++j) {
      if (unit[i].second[j] != d[j])
	same = false;
    }
    if (same)
      ret = i;
  }
  return ret;
}

int getUnitIndex(const string& u)
{
  int ret = -1;
  for (int i = 0; i < unit.size(); ++i) {
    if (unit[i].first == u)
      ret = i;
  }
  return ret;
}

vector<int> p_formula();

vector<int> p_variable()
{
  string ret = "";
  while (isalpha(s[p]))
    ret += s[p++];
  return unit[getUnitIndex(ret)].second;
}

vector<int> p_factor()
{
  if (s[p] == '(') {
    ++p;
    vector<int> ret =  p_formula();
    ++p;
    return ret;
  } else {
    return p_variable();
  }
}

vector<int> p_term()
{
  vector<int> lhs = p_factor();
  for ( ; ; ) {
    if (s[p] == '*' || s[p] == '/') {
      char op = s[p];
      ++p;
      vector<int> rhs = p_factor();
      for (int i = 0; i < lhs.size(); ++i) {
	if (op == '*')
	  lhs[i] += rhs[i];
	else
	  lhs[i] -= rhs[i];
      }

    } else {
      break;
    }
  }
  return lhs;
}

vector<int> p_formula()
{
  vector<int> lhs = p_term();
  for ( ; ; ) {
    if (s[p] == '+' || s[p] == '-') {
      ++p;
      vector<int> rhs = p_term();
      bool same = true;
      for (int i = 0; i < lhs.size(); ++i) {
	if (lhs[i] != rhs[i])
	  same = false;
      }
      if (!same)
	error = true;
    } else {
      break;
    }
  }
  return lhs;
}

int main()
{
  while (cin >> n >> m >> p) {
    if ((n|m|p) == 0)
      break;

    unit.clear();

    for (int i = 0; i < m; ++i) {
      string derived;
      cin >> derived;
      vector<int> d(n);
      for (int j = 0; j < n; ++j)
	cin >> d[j];
      unit.push_back(make_pair(derived, d));
    }

    string t;
    cin >> t;
    t += '@';

    map<string, string> var;
    for (int i = 0; i < p; ++i) {
      string v, D;
      cin >> v >> D;
      var[v] = D;
    }

    s = "";
    for (int i = 0; i < t.size(); ++i) {
      string v = "";
      while (isalpha(t[i]))
	v += t[i++];
      if (v != "")
	s += var[v];
      s += t[i];
    }

    p = 0;
    error = false;
    vector<int> ans = p_formula();

    if (error) {
      cout << "error" << endl;
    } else {
      int ansIndex = getUnitIndex(ans);
      if (ansIndex == -1)
	cout << "undefined" << endl;
      else
	cout << unit[ansIndex].first << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 952
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
#include <math.h>
using namespace std;
int main(){
    long long int n,ans=0;
    cin>>n;
    vector<long long int>a(1000000);
    for(long long int i=0;i<n;i++){
        cin>>a[i];
    }
    for(long long int i=0;i<n;i++){
        for(long long int j=i+1;j<n;j++){
            for(long long int k=j+1;k<n;k++){
                if(a[i]!=a[j]&&a[j]!=a[k]&&a[k]!=a[i]&&a[i]+a[j]>a[k]&&a[j]+a[k]>a[i]&&a[k]+a[i]>a[j]){
                    ans++;
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


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

int N;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B){
    mat C(A.size(), vec(B[0].size()));
    for(int i=0; i<A.size(); i++){
        for(int k=0; k<B.size(); k++){
            for(int j=0; j<B[0].size(); j++){
                C[i][j] = C[i][j] + A[i][k] * B[k][j];
            }
        }
    }
    return C;
}

mat pow(mat A, int n){
    mat B(A.size(), vec(A.size()));
    for(int i=0; i<(int)A.size(); i++){
        B[i][i] = 1;
    }
    while(n>0){
        if(n&1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

int main(){
    scanf("%d", &N);
    mat A(2, vec(2));
    mat B(2, vec(1));
    A[0][0]=1;A[0][1]=1;
    A[1][0]=1;A[1][1]=0;
    B[0][0]=1;B[1][0]=2;
    A = pow(A, N-1);
    B = mul(A, B);
    printf("%lld", B[0][0]);
}


Measured memory_bytes: 256
from sys import stdin 
n = int(stdin.readline())
ans = 10**18 
i = 1 
while i*i <=n:
    if n%i==0:
        j = n/i 
        cur = max(len(str(i)) , len(str(j)))
        ans = min(ans,cur)
    i+=1 
print ans 

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

int main(void){
    char s[4];
    cin >> s;
    
    string ans;
    
    bool YY=true;
    bool MM=true;
    
    if((((static_cast<int>(s[0]))=='0')&&((static_cast<int>(s[1]))!='0'))||(((static_cast<int>(s[0]))=='1')&&((static_cast<int>(s[1]))<='2'))){
        MM=true;
    } else {
        MM=false;
    }
    
    if((((static_cast<int>(s[2]))=='0')&&((static_cast<int>(s[3]))!='0'))||(((static_cast<int>(s[2]))=='1')&&((static_cast<int>(s[3]))<='2'))){
        YY=true;
    } else {
        YY=false;
    }
    
    if(YY==true&&MM==false){
        ans="YYMM";
    } else if(YY==false&&MM==true){
        ans="MMYY";
    } else if(YY==true&&MM==true){
        ans="AMBIGUOUS";
    } else if(YY==false&&MM==false){
        ans="NA";
    }
    
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
	int h, w, n, a;
	cin >> h >> w >> n;

	VI nums;
	for (int i = 0; i < n; i++) {
		cin >> a;

		for (int j = 0; j < a; j++) {
			nums.emplace_back(i+1);
		}
	}

// cout << endl;
// for (auto e : nums) {
// 	cout << e << " ";
// }
// cout << endl;

	VVI board(h, VI(w, -1));


	int x = 0;
	int y = 0;
	int nx, ny;

	int dir = 0;
	const VI dx = {1, 0, -1, 0};
	const VI dy = {0, 1, 0, -1};
	for (int i = 0; i < h*w; i++) {
		board[y][x] = nums[i];

		nx = x + dx[dir];
		ny = y + dy[dir];

		if (0 <= nx && nx < w &&
			0 <= ny && ny < h &&
			(board[ny][nx] == -1 || i == n*w - 1))
		{
			x = nx;
			y = ny;
		} else {
			dir = (dir + 1) % 4;
			x = x + dx[dir];
			y = y + dy[dir];
		}


	}

	for (auto v : board) {
		cout << v[0];
		for (int i = 1; i < v.size(); i++) {
			cout << " " << v[i];
		}
		cout << endl;
	}
	return 0;
}


Measured memory_bytes: 384
redBlue n a b = divs + mods'
                where
                    ab = a + b
                    divs = (n `div` ab) * a
                    mods = (n `mod` ab)
                    mods' = if mods > a then a
                                        else mods

toInt x = read x :: Integer

main = do
    nab <- getLine
    let nab' = map toInt $ words nab
        n = head nab'
        a = head $ tail nab'
        b = last nab'
    print $ redBlue n a b

Measured memory_bytes: 508
<?php
list($N, $T) = ints();
$t = ints();
$ans = 0;
$p = -1;
for ($i = 0; $i < $N; ++$i) {
    $ans += $T;
    if ($t[$i] < $p) $ans -= $p - $t[$i];
    $p = $t[$i] + $T;
}
echo $ans;
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}


Measured memory_bytes: 31220
from collections import deque
inf=float("inf")


def BFS():
    color=[["white" for _ in range(w)] for _ in range(h)]
    D=[[inf for _ in range(w)] for _ in range(h)]
    queue=deque([])

    x,y=C
    x -=1
    y -=1
    queue.append([x,y])
    D[x][y]=0

    for i in range(h):
        for j in range(w):
            if S[i][j]=="#":
                color[i][j]="black"

    while len(queue)>0:
        x,y=queue.popleft()
        for i,j in [[1,0],[0,1],[-1,0],[0,-1]]:
            X,Y=x+i,y+j
            if 0<=X<h and 0<=Y<w:
                if color[X][Y]!="black" and D[X][Y]>D[x][y]:
                    D[X][Y]=D[x][y]
                    color[X][Y]="gray"
                    queue.appendleft([X,Y])

        for i in range(max(0,x-2),min(h,x+3)):
            for j in range(max(0,y-2),min(w,y+3)):
                X,Y=i,j
                if 0<=X<h and 0<=Y<w:
                    if color[X][Y]!="black" and D[X][Y]>D[x][y]+1:
                        D[X][Y]=D[x][y]+1
                        color[X][Y]="gray"
                        queue.append([X,Y])
        color[x][y]="black"

    return D[G[0]-1][G[1]-1]


h,w=map(int,input().split())
C=list(map(int,input().split()))
G=list(map(int,input().split()))
S=[list(input()) for _ in range(h)]

D=BFS()
print(D if D!=inf else -1)

Measured memory_bytes: 170952
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE BangPatterns #-}

module Main where

import Debug.Trace
--import Data.List
--import qualified Data.Set.Strict as Set
--import qualified Data.Map.Strict as Map

main :: IO ()
main = do
  a <- read <$> getLine
  --a `traceShow`
  putStrLn $ solve a

solve :: Int -> String
solve a = build a ""

char_range :: Int
char_range = fromEnum 'z' - fromEnum 'a' + 1

char_start :: Int
char_start = fromEnum 'a'

build :: Int -> String -> String
build a s
  -- | a <= 0 = s `traceShow` s
  | a <= 0 = s
  -- | otherwise = (a, m, d, s) `traceShow` build d ((toEnum (char_start + m)):s)
  | otherwise = build d ((toEnum (char_start + m)):s)
  where
    m = (a - 1) `mod` char_range
    d = (a - 1 - char_range) `div` char_range + 1


--toEnum (fromEnum 'a' + 2) :: Char


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

int main()
{
    string s, t, u;
    int a, b;
    cin >> s >> t >> a >> b >> u;
    if(u == s){
        a--;
    }else b--;
    cout << a << " " << b << endl;
    system("pause");
}

Measured memory_bytes: 504
#include <iostream>
#include <stdio.h>

#include <vector>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>

using namespace std;


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

    int k= (n+1000-1)/1000;

    cout << 1000*k-n  << endl;

    return 0;
}

Measured memory_bytes: 3632
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

public class Main{
	public static void main(String[] args){
		//変数
		ArrayList<Integer> cards = new ArrayList<>();	//カードの格納
		int n = -1;																//カードの枚数
		int r = -1;																//シャッフルの回数
		Scanner sc = new Scanner(System.in);						//標準入力
		
		while (true){
			//カードの枚数と、シャッフルの回数を読み取り
			n = sc.nextInt();
			r = sc.nextInt();
		
			//終了条件の判定
			if (n == 0 && r == 0){
				break;	
			}
			
			//データの初期化
			for (int i = 1; i <= n; i++){
				cards.add(i);
			}
		
			//データのシャッフル
			for (int i = 0; i < r; i++){
				int end = n - sc.nextInt();
				int start = end - (sc.nextInt() - 1);
				cards = shuffle(cards, start, end);
			}
		
			System.out.println(cards.get(n - 1));
			
			//カード情報の削除
			cards.clear();
		}
	}
	
	private static ArrayList<Integer> shuffle(ArrayList<Integer> cards,int start, int end){
		for (int i = start; i <= end; i++){
			int temp = cards.get(start);
			cards.remove(start);
			cards.add(temp);
		}
		return cards;
	}
}


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

int main(){
	int h[10000], w[10000], i, j, k;
	for(i = 0; i <= 9999; i++){
		cin >> h[i] >> w[i];
		if((h[i] == 0) and (w[i] == 0)){
			break;
		}
	}
	
	for(i = 0; i <= 9999; i++){
		if((h[i] == 0) and (w[i] == 0)){
			break;
		}
		for(j = 1; j <= h[i]; j++){
			cout.width(w[i]);
			cout.fill('#');
			cout << '#' << endl;
		}
		cout << endl;
	}
	
	return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<pdd> vdd;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mem(a,b) memset(a, b, sizeof(a) )
#define all(x) (x).begin(),(x).end()
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
    ll n,m;cin>>n>>m;
    vl a(m),b(m),c(m);
    rep(i,m){
        cin>>a[i]>>b[i];
        rep(j,b[i]){
            ll x;cin>>x;x--;
            ll y=1;
            while(x>0){
                x--;
                y*=2;
            }
            c[i]+=y;
        }
    }
    vl dp((1<<n)+10,INF);
    dp[0]=0;
    rep(i,(1<<n)){
        if(dp[i]==INF)continue;
        rep(j,m){
            dp[i|c[j]]=min(dp[i|c[j]],dp[i]+a[j]);
        }
    }
    if(dp[(1<<n)-1]==INF)cout<<-1<<endl;
    else cout<<dp[(1<<n)-1]<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define show(x) {for(auto i: x){cout << i << " ";} cout<<endl;}
#define showm(m) {for(auto i: m){cout << m.x << " ";} cout<<endl;}
typedef long long ll;
typedef pair<int, int> P;
ll gcd(int x, int y){ return y?gcd(y, x%y):x;}
ll lcm(ll x, ll y){ return (x*y)/gcd(x,y);}


int main()
{
    string day[7] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

    string s; cin >> s;

    rep(i, 7){
        if (s == day[i]){
            cout << 7-i << endl;
            return 0;
        }
    }
    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <functional>

struct edge
{
  int to;
  long long cost;
};

int main()
{
  int n;
  scanf("%d", &n);
  std::vector<edge> es[100000];
  for (int i = 0; i < n - 1; ++i)
  {
    int a, b; long long c;
    scanf("%d%d%lld", &a, &b, &c);
    --a, --b;
    es[a].emplace_back(edge{b, c});
    es[b].emplace_back(edge{a, c});
  }
  int q, k;
  scanf("%d%d", &q, &k);
  --k;

  long long dist[100000];
  std::fill(dist, dist + n, (long long)1e18);

  using pair = std::pair<long long, int>;
  std::priority_queue<pair, std::vector<pair>, std::greater<pair>> que;
  dist[k] = 0;
  que.emplace(0, k);
  while (que.size() > 0)
  {
    auto p = que.top(); que.pop();
    if (dist[p.second] < p.first) continue;
    for (auto &e : es[p.second])
    {
      long long nc = p.first + e.cost;
      if (dist[e.to] <= nc) continue;
      dist[e.to] = nc;
      que.emplace(dist[e.to], e.to);
    }
  }

  for (int i = 0; i < q; ++i)
  {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%lld\n", dist[x - 1] + dist[y - 1]);
  }
  return 0;
}

Measured memory_bytes: 11120
#include <iostream>
#include <string>

using namespace std;
int main(){
  int n;
  cin >> n;
  
  int list[n];
  int ok = 0;
  
  for(int i = 0; i < n; i++){
    int num; 
    cin >> num;
    list[i] = num;
  }
  
  for (int i = 1; i < n - 1; i++){
    int a = list[i - 1];
    int b = list[i];
    int c = list[i + 1];
    if(a < b && b < c){
      ok++;
    }
    else if(a > b && b > c){
      ok++;
    }
  }
  
    cout << ok << endl;
  
}


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

int main(){
	string s;
	cin>>s;
	for(int i=0;i<s.size()-1;++i) if(s[i]==s[i+1]){
		printf("%d %d\n",i+1,i+2);
		return 0;
	}
	for(int i=0;i<s.size()-2;++i) if(s[i]==s[i+2]){
		printf("%d %d\n",i+1,i+3);
		return 0;
	}
	printf("-1 -1\n");
	return 0;
}

Measured memory_bytes: 512
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

struct timer{
	time_t start;
	timer(){start=clock();}
	~timer(){cerr<<1.*(clock()-start)/CLOCKS_PER_SEC<<" secs"<<endl;}
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int N,p[100010];
int to[100010],vis[100010],dist[100010];

int f(int cur){
	if(to[cur]!=-1)return to[cur];
	if(vis[cur] && to[cur]==-1)return to[cur]=-2;
	vis[cur]=1;
	if(p[cur]==0)return to[cur]=cur;
	return to[cur]=f(cur+p[cur]);
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	
	cin>>N;
	rep(i,N)cin>>p[i];
	memset(to,-1,sizeof(to));
	rep(i,N)f(i);
	fill(dist,dist+N,INF);
	dist[0]=0;
	
	queue<int> Q;
	Q.push(0);
	while(sz(Q)){
		int x=Q.front();Q.pop();
		rep2(i,1,7){
			int nx=min(x+i,N-1);
			if(dist[to[nx]]==INF){
				dist[to[nx]]=dist[x]+1;
				Q.push(to[nx]);
			}
		}
	}
	cout<<dist[N-1]<<endl;
}

Measured memory_bytes: 4088
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String t = sc.next();
        char[] sL = s.toCharArray();
        char[] tL = t.toCharArray();
        Arrays.sort(sL);
        Arrays.sort(tL);

        String ss = new String(sL);
        String tt = new String(tL);
        String ts = new StringBuffer(tt).reverse().toString();
        if (ss.compareTo(ts) < 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }

}

Measured memory_bytes: 21844
#include<iostream>
using namespace std;
int main() {
    string num;
    cin >> num;
    
    if (num == "SUN") {
        cout << "7";
    }
    else if (num == "MON") {
        cout << "6";
    }
    else if (num == "TUE") {
        cout << "5";
    }
    else if (num == "WED") {
        cout << "4";
    }
    else if (num == "THU") {
        cout << "3";
    }
    else if (num == "FRI") {
        cout << "2";
    }
    else if (num == "SAT") {
        cout << "1";
    }


}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int> vec;
int check(int n){
    int a=0,b=0,c=0;
    while(n){
        if(n%10==7) a=1;
        else if(n%10==5) b=1;
        else if(n%10==3) c=1;
        n/=10;
    }
    if(a+b+c==3) return 1;
    else return 0;
}
void eiei(int n){
    if(n>=1000000000) return ;
    else{
        if(check(n)==1){
            vec.push_back(n);
        }
        eiei(n*10+7);
        eiei(n*10+5);
        eiei(n*10+3);
    }
}
signed main(){
    int n;
    cin>>n;
    eiei(0);
    sort(vec.begin(),vec.end());
    int ans=0;
    for(int i=0;i<vec.size();i++){
        if(vec[i]<=n) ans++;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#define forn(i, n) for (long long i = 0; i < (long long)(n); ++i)
#define for1(i, n) for (long long i = 1; i <= (long long)(n); ++i)
#define fore(i, l, r) for (long long i = (long long)(l); i <= (long long)(r); ++i)
#define fordn(i, n) for (long long i = (long long)(n) - 1; i >= 0; --i)
#define ford1(i, n) for (long long i = (long long)(n); i >= 1; --i)
#define all(x) (x).begin(), (x).end()
#define PI 3.141592653589793
using namespace std;
using ll = long long;
using ld = long double;

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int p, q, r;
    cin >> p >> q >> r;
    int p1 = p + q;
    int p2 = r + min(p, q);
    cout << min(p1, p2) << endl;

}

Measured memory_bytes: 256
#include <stdio.h>
#include <limits.h>

int main(int argc, char *argv[])
{
    int list[50][26]={{0}}, i, j, n, min;
    char buf[64];
    scanf("%d\n", &n);
    for ( i = 0; i < n; i++ ){
        fgets(buf, sizeof(buf), stdin);
        for ( j = 0; buf[j] != '\n'; j++ ){
            list[i][buf[j]-'a']++;
        }
    }
    for ( i = 0; i < 26; i++ ){
        for ( j = 0, min = INT_MAX; j < n; j++ ){
            if ( min > list[j][i] ){
                min = list[j][i];
            }
        }
        for ( j = 0; j < min; j++ ){
            printf("%c", i+'a');
        }
    }
    printf("\n");
    return 0;
}

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

int main() {
  int N;
  cin >> N;
  int count =0;
  int a;
  for (int i=0; i<N ; i++){
    cin >> a;
    if ( i%2 == 0 & a%2 == 1){
      count++;
    }
  }
  cout << count << endl;
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue; 
#define LINF 1e18
#define INF 1e9
using namespace std;

#define fs first
#define sc second

using P = pair<ll,ll>;
using Pll = pair<P,ll>;
using PQ = priority_queue<P,vector<P>,greater<P>>;

signed main(){
    ll n,x;
    cin >> n >> x;
    ll l[n] = {};
    ll cnt = 1,pos = 0;
    rep(i,0,n){
        cin >> l[i];
        if(pos+l[i] <= x){
            cnt++;
            pos += l[i];
        }
        else{
            break;
        }
    }
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 3600
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read() {
	int x(0),neg(1);char ch(getchar());
	while(ch<'0'||ch>'9') {
		if (ch=='-') neg=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*neg;
}
deque<int> num[10005],now,a,ans;
int used[10005];
int cnt,tot=1;
inline void solve() {
	while(cnt) {
		if (!now.size()) {
			puts("-1");
			exit(0);
		}
		++tot;
		while(a.size()) {
			used[a[0]]=0;
			a.pop_front();
		}
		while(now.size()) {
			int t=now[0];now.pop_front();
			if(!num[t].size())continue;
			int to=num[t][0];
			if(!num[to].size()) {
				puts("-1");
				exit(0);
			}
			if(used[t]||used[to]||num[to][0]!=t) {
				continue;
			}
			--cnt;
			a.push_back(t),a.push_back(to);
			ans.push_back(t),ans.push_back(to);
			num[t].pop_front(),num[to].pop_front();
			used[t]=used[to]=1;
		}
		while(ans.size()) now.push_back(ans[0]),ans.pop_front();
	}
}
int n;
inline void init() {
	n=read();
	for (int i=1;i<=n;++i) {
		for (int j=1;j<n;++j) {
			num[i].push_back(read());
			++cnt;
		}
	}
	cnt/=2;
}
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);	
	init();
	
	for (int i=1;i<=n;++i) {
		if (!used[i]&&!used[num[i][0]]&&num[i][0]!=i&&num[num[i][0]][0]==i) {
			a.push_back(i),a.push_back(num[i][0]);
			now.push_back(i),now.push_back(num[i][0]);
			used[i]=used[num[i][0]]=1;
			num[num[i][0]].pop_front(),num[i].pop_front();
			--cnt;
		}
	}
	solve();
	
	printf("%lld\n",tot);
	return 0;
}

Measured memory_bytes: 15104
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define int long long
#define _CRT_SECURE_NO_WARNINGS
#define rep(i, n) for (int i = 0; i < n; i++)
#define _GLIBCXX_DEBUG
const int MOD = 1e9 + 7;
const int INF = 1e18 + 9;
constexpr long double pi = 3.141592653589793238462643383279; //円周率
/*---------------------便利な関数--------------------------------------*/
int fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i;
}

int gcd(int a, int b) {        //最大公約数
    if (b == 0) return a;
    return gcd(b, a % b);
}

int lcm(int a, int b) {      //最小公倍数
    return a * b / gcd(a, b);
}

int keta(int n) {        //桁数を求める
    if (n == 0) return 1;
    int count = 0;
    while (n != 0) {
        n /= 10;
        count++;
    }
    return count;
}

int ketasum(int n) {    //各桁の和
    int sum = 0;
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/*-------------ここまで---------------------------------------------*/

//C++(GCC9.2.1)

signed main() {
    //宣言
    int n, m;
    cin >> n >> m;
    vector<int> vec(m);
    int sum = 0;
    rep(i, m) {
        cin >> vec[i];
        sum += vec[i];
    }
    if (sum > n) {
        cout << "-1" << endl;
    }
    else cout << n - sum << endl;
    return 0;
}

Measured memory_bytes: 3676
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

n,m = map(int,input().split())

class UnionFind():
    def __init__(self, N):
        self.rank = [0] * N
        self.par = [i for i in range(N)]
        self.counter = [1] * N

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            z = self.counter[x] + self.counter[y]
            self.counter[x], self.counter[y] = z, z
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def size(self, x):
        x = self.find(x)
        return self.counter[x]

    def same(self, x, y):
        return self.find(x) == self.find(y)
UF = UnionFind(n)
ans = n
for i in range(m):
    x,y,z = map(int,input().split())
    if  UF.same(x-1,y-1):
        continue
    else:
        UF.unite(x-1,y-1)
        ans -=1
print(ans)

Measured memory_bytes: 84768
#include "bits/stdc++.h"
using namespace std;

#define ll long long int
#define str string
#define ALL(a)  (a).begin(),(a).end()
#define P pair<ll,ll>

using Graph = vector<vector<int>>;

const int dy[] = { 0, 0, 1, -1 }, dx[] = { 1, -1, 0, 0 };
ll INFL = 1ll << 60, INF = 1 << 30, mod = 1e9 + 7, Min = INFL, Max = -1;
ll a, b, c, n, sum, A[500010], B[500000], rui[500000], dp[5000];
str S; bool check = true;	vector<ll> v;



int main() {
	//Code Line
	cin >> n;
	b = n;
	a = 1;
	while (n > 1) {
		
		c += a;
		a *= 2;
		
		n /= 2;
		
		
	}
	c += a;
	cout << c << endl;

	return 0;
}


Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

 class Main {
	public static void main(String[]args) {
		Scanner scan = new Scanner(System.in) ;
		int n = scan.nextInt();
		int m = scan.nextInt();
		ArrayList<Integer> L = new ArrayList<>();
		ArrayList<Integer> R = new ArrayList<>();
		for(int i = 0 ; i<m ; i++) {
			L.add(scan.nextInt());
			R.add(scan.nextInt());
		}
	    int a = Collections.max(L) ;
	    int b = Collections.min(R) ;
		int result = (b-a) +1 ;
		if(result > 0) {
			System.out.println(result);
		}else {
			System.out.println(0);
		}
	}
}


Measured memory_bytes: 84340
import java.io.*;
import java.util.*;

public class Main {

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tok;

        public InputReader(InputStream inputStream) {
            reader = new BufferedReader(new InputStreamReader(inputStream));
            tok = null;
        }

        public InputReader(String inputFile) throws FileNotFoundException {
            reader = new BufferedReader(new FileReader(inputFile));
            tok = null;
        }

        public String nextLine() {
            String c = "";
            try {
                c = reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            return c;
        }

        public String next() {
            while (tok == null || !tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(nextLine());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            return tok.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }

    public static void main(String args[]) {
        InputStream inputstream = System.in;
        OutputStream outputstream = System.out;
        InputReader in = new InputReader(inputstream);
        PrintWriter out = new PrintWriter(outputstream);
        Task solver = new Task();
        solver.solve(in, out);
        out.flush();
    }

    static class Task {
        public void solve(InputReader in, PrintWriter out) {
            int n=in.nextInt();
            int[][] dice=new int[n][2];
            for(int i=0;i<n;i++){
                dice[i][0]=in.nextInt();
                dice[i][1]=in.nextInt();
            }
            int ans=0;
            boolean flag=false;
            for(int i=0;i<n;i++){
                if(dice[i][0]==dice[i][1])
                ans++;
                else
                ans=0;
                if(ans>=3){
                    flag=true;
                    break;
                }
            }
            if(flag)
            out.println("Yes");
            else
            out.println("No");
        }
    }
}


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

int main() {
  int A, B, N = 0;
  cin >> A >> B;
  
  for (int i = A; i < B + 1; i++) {
    if (i / 10000 == i % 10 && (i / 1000) % 10 == (i / 10) % 10) {
      N++;
    }
  }
  cout << N << endl;
}

Measured memory_bytes: 3628
#include<iostream>
using namespace std;
int main(){
    int n, j=0, k=0, tmp, x;
    cin >> n;
    int a[n];
    for (int i=0; i<n; i++) {
        cin >> a[i];
    }
    x = a[n-1];
    for (int i=0; i<n-1; i++) {
        if (a[i]<=x) {
            tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
            j++;
        }
    }
    tmp = a[j];
    a[j] = a[n-1];
    a[n-1] = tmp;
    for (int i=0; i<n; i++) {
        if (i == j) {
            cout << "[" << a[i] << "]";
        } else {
            cout << a[i];
        }
        if (i==n-1) {
            cout << endl;
            break;
        }
        cout << " ";
    }
}


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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9+7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define pii pair<int32,int32>
#define pll pair<int64,int64>
#define pb(a) push_back(a)

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n,m;
  cin >> n >> m;
  cout << (n - 1) * (m - 1) << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define N (int)1e5+5
using namespace std;
int n;
ll a[N];
ll ans;
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) {
		scanf("%lld", &a[i]);
	}
	for (int i = 1; i < n; i ++) {
		ans += a[i] >> 1;
		a[i] = a[i] % 2;
		a[i+1] = max(1ll*0, a[i+1] - a[i]);
		ans += min(a[i], a[i+1]);
	}
	ans += a[n] >> 1;
	printf("%lld", ans);
}

Measured memory_bytes: 1024
A, B, C = map(int,input().split())
K = int(input())
flag = False

for p in range(8):
    for q in range(8):
        for r in range(8):
            if A * 2 ** p < B * 2 ** q < C * 2 ** r and p + q + r <= K:
                flag = True

if flag:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 9188
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace C
{
    public class Program
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var x = int.Parse(Console.ReadLine());

            while (true)
            {
                if (IsPrime(x))
                {
                    Console.WriteLine(x);
                    break;
                }
                x++;
            }
        }

        static bool IsPrime(int x)
        {
            if (x < 2) return false;
            if (x == 2) return true;
            if (x % 2 == 0) return false;
            var sqrt = Math.Sqrt(x);
            for (var i = 3; i < sqrt; i++) if (x % i == 0) return false;
            return true;
        }
    }
}


Measured memory_bytes: 11092
#####################################################################################################
##### トポロジカルソート
#####################################################################################################

import sys
input = sys.stdin.buffer.readline


class DiGraph:
    def __init__(self, n, decrement=True, destroy=False, edges=[]):
        """
        incs[p]: 頂点 p に流入するリンク数
        """
        self.n = n
        self.decrement = decrement
        self.destroy = destroy
        self.edges = [set() for _ in range(self.n)]
        self.incs = [0]*self.n
        for x, y in edges:
            self.add_edge(x,y)

    def add_edge(self, x, y):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].add(y)
        self.incs[y] += 1

    def topological_sort(self):
        next_set = {p for p, inc in enumerate(self.incs) if inc == 0}
        while next_set:
            p = next_set.pop()
            yield p+self.decrement
            for q in self.edges[p]:
                self.incs[q] -= 1
                if self.incs[q] == 0:
                    next_set.add(q)

    def draw(self):
        """
        :return: グラフを可視化
        """
        import matplotlib.pyplot as plt
        import networkx as nx

        G = nx.DiGraph()
        for x in range(self.n):
            for y in self.edges[x]:
                G.add_edge(x + self.decrement, y + self.decrement)

        pos = nx.spring_layout(G)
        nx.draw_networkx(G, pos)
        plt.axis("off")
        plt.show()


################################################################################

def main():
    N, M = map(int, input().split())

    M = N - 1 + M
    graph = DiGraph(N, decrement=True, destroy=True)
    for _ in range(M):
        A, B = map(int, input().split())
        graph.add_edge(A, B)

    res = [0]*N
    for p in graph.topological_sort():
        for q in graph.edges[p-1]:
            res[q] = p
    print(*res, sep="\n")

if __name__ == '__main__':
    main()


Measured memory_bytes: 107896
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>

using namespace std;
struct edge
{
	int u,v;
	edge *next;
}*h[2333],pool[1111111];
int top;
inline void addedge(int u,int v)
{
//	cout<<"addedge "<<u<<' '<<v<<endl;
	edge *tmp=&pool[top++];tmp->v=v;tmp->next=h[u];h[u]=tmp;
	edge *pmt=&pool[top++];pmt->v=u;pmt->next=h[v];h[v]=pmt;
}
int n,m,x,y;
int mat[777][777];
int dp[777][2333];
vector<pair<int,int> > siz;
int col[777],c[3];
bool dfs(int u)
{
//	cout<<"dfs "<<u<<' '<<col[u]<<endl;
	for(edge *tmp=h[u];tmp;tmp=tmp->next)
	{
//		cout<<u<<"->"<<tmp->v<<' '<<col[u]<<' '<<col[tmp->v]<<endl;
		if(!col[tmp->v])
		{
			col[tmp->v]=3-col[u];
			c[col[tmp->v]]++;
			if(!dfs(tmp->v))return 0;
		}
		else if(col[tmp->v]==col[u])return 0;
	}
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		mat[x][y]=mat[y][x]=1;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			if(mat[i][j]==0)
			{
				addedge(i,j);
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(!col[i])
		{
			c[1]=1;c[2]=0;
			col[i]=1;
			if(!dfs(i))return printf("-1\n"),0;
			siz.push_back(make_pair(c[1],c[2]));
		}
	}
	dp[0][0]=1;
	for(int i=0;i<siz.size();i++)
	{
		for(int j=0;j<=n;j++)
		{
			if(dp[i][j]==1)
				dp[i+1][j+siz[i].first]=1,
				dp[i+1][j+siz[i].second]=1;
		}
	}
	for(int i=n/2;i>=0;i--)
	{
		if(dp[siz.size()][i])
		{
			printf("%d\n",i*(i-1)/2+(n-i)*(n-i-1)/2);
			return 0;
		}
	}
	return 0;
}

Measured memory_bytes: 10496
#include<stdio.h>
int aa[110];
main()
{
	int n,m;
	while(~scanf("%d %d",&n,&m))
	{
		int i;
		for(i=0;i<n;i++)
		{
			scanf("%d",&aa[i]);
		}
		int sum=0;
		int num=1;
		for(i=0;i<n;i++)
		{	
			sum=sum+aa[i];
			if(sum>m)
			break;
			else
			num++;
			
		}
		printf("%d\n",num);
	}
}

Measured memory_bytes: 128
a,b=map(int,input().split())
c=0
for l in range(a+1):
    for m in range(l,a+1):
        r=b-l-m
        if r >a:
            continue
        elif r<m:
            continue
        elif r==l==m:
            c+=1
        elif r==m:
            c+=3
        elif m==l:
            c+=3
        else:
            c+=6
print(c)

Measured memory_bytes: 3060
#include <stdio.h>
int main(){
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    if(a==b) printf("%d\n",c);
    else if(a==c) printf("%d\n",b);
    else if(b==c) printf("%d\n",a);
    return 0;
}


Measured memory_bytes: 128
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

a = 0
b = 0
import itertools
for r in list(itertools.permutations(range(1, n+1))):
  for i in range(n):
    if r[i] < p[i]:
      a += 1
      break
    elif r[i] > p[i]:
      break
    else:
      continue
  for i in range(n):
    if r[i] < q[i]:
      b += 1
      break
    elif r[i] > q[i]:
      break
    else:
      continue
      
print(abs(a-b))

Measured memory_bytes: 8052
#include"bits/stdc++.h"
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;

void Main(){
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	int dx = c-a;
	int dy = d-b;
	int dx2 = -dy;
	int dy2 = dx;
	cout << c + dx2 << " " << d + dy2 << " " << a + dx2 << " " << b + dy2 << endl;
	return;
}

int main(){
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
main()
{
	int a, b, x;
	scanf("%d%d%d", &a, &b, &x);
	if (a==x)
	printf("YES");
else if (a>x)
printf("NO");	 
	else if (a<x)
	{
		if (a+b>=x)
		printf("YES");
		else printf("NO");
	}
	return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>
#include<cstdint>
#include<bits/stdc++.h>
#include<queue>
#include<set>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for(int i = n-1; i >= 0; i--)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define INF bit(60)
#define pb push_back
#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; }

using namespace std;
using uif=uint_fast64_t;
using ll=long long int;

int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mod;a=1ll*a*a%mod;}while(b>>=1);return ans;}
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};


int main(void){

  ll n;
  cin>>n;

  double bty=380000.0;

  double ans=0.0;

  rep(i,n){
    double x;
    string u;

    cin >> x;
    cin >> u;

    if(u=="JPY"){
      ans+=x;
    }
    else{
      ans+=x*bty;
    }

  }

  cout << ans << endl;

  return 0;

}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<climits>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define INF 1e9+7
#define LLINF 1e18
// #define int ll
using namespace std;
const int MOD = 1e9 +7;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,m,b[100010],c[100010],ans=0,sum=0;
    cin>>n>>m;
    vector<ll> a;
    vector<pll>p;
    rep(i,n){
        ll x;
        cin>>x;
        a.pb(x);
    }
    rep(i,m){
        cin>>b[i]>>c[i];
        p.pb(pll(c[i],b[i]));
    }
    sort(all(p));
    reverse(all(p));
    sort(all(a));
    ll it;
    rep(i,m){
        it = lower_bound(all(a),p[i].first)-a.begin();
        if(it >sum){
            ans += p[i].first *min(it-sum,p[i].second);
            sum +=min(it-sum,p[i].second);
        }else{
            break;
        }
    }
    for(int i=sum;i<n;i++) ans+=a[i];
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 4336
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
#define it vector<int>::iterator
bool next_combination(it first1,it last1,it first2,it last2){
	if((first1==last1)||(first2==last2))return false;
	it m1=last1,m2=last2;
	m2--;
	while(--m1!=first1&&!(*m1<*m2));
	bool result=(m1==first1)&&!(*first1<*m2);
	if(!result){
		while(first2!=m2&&!(*m1<*first2))first2++;
		first1=m1;
		iter_swap(first1,first2);
		first1++;first2++;
	}
	if((first1!=last1)&&(first2!=last2)){
		m1=last1;m2=first2;
		while((m1!=first1)&&(m2!=last2))iter_swap(--m1,m2++);
		reverse(first1,m1);
		reverse(first1,last1);
		reverse(m2,last2);
		reverse(first2,last2);
	}
	return !result;
}
bool next_combination(it first,it middle,it last){
  return next_combination (first , middle , middle , last );
}
int main(){
	int n;
	while(cin>>n,n){
		vs in(n);
		rep(i,n)cin>>in[i];
		map<int,int>ma;
		rep(i,n)rep(j,in[i].size())ma[in[i][j]]=2;
		int N=ma.size();
		int t=0;
		for(auto &ite:ma)ite.second=t++;

		vi w(N);
		rep(i,n){
			int t=1;
			for(int j=in[i].size()-1;j>=0;j--)
				w[ma[in[i][j]]]+=t*(i==n-1?-1:1),t*=10;
		}
		vi dame(N);
		rep(i,n)if(in[i].size()>1)dame[ma[in[i][0]]]=1;

		vi pr(10);
		rep(i,10)pr[i]=i;
		int co=0;
		do{
			do{
				bool h=true;
				rep(i,N)if(pr[i]==0&&dame[i])h=false;
				if(!h)continue;
				
				int sum=0;
				rep(i,N)sum+=pr[i]*w[i];
				if(sum==0)co++;
			}while(next_permutation(pr.begin(),pr.begin()+N));
		}while(next_combination(pr.begin(),pr.begin()+N,pr.end()));
		cout<<co<<endl;
	}
}



Measured memory_bytes: 3200
import java.util.Scanner;

public class Main{
	static Scanner s=new Scanner(System.in);
	static int gInt(){return Integer.parseInt(s.next());}

	public static void main(String[] $){
		String a=s.next().replaceAll("\\?","."),b=s.next();
		for(int i=a.length()-b.length();i>=0;--i) {
			String regex=a.substring(i,i+b.length());
			if(b.matches(regex)) {
				//System.out.println(regex);
				System.out.println(a.replaceFirst(a.substring(0,i)+regex,a.substring(0,i)+b).replaceAll("\\.","a"));
				return;
			}
		}
		System.out.println("UNRESTORABLE");
	}
}

Measured memory_bytes: 23124
// 失敗するからこそ そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
// ぜったいあきらめない
#if defined(DAIJOBU)
#include "/home/ichigo/includes.hpp"
#include "/home/ichigo/debug.hpp"
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#include <bits/stdc++.h>
#define deb(x...)
#endif
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long lint;
typedef vector<lint> vlint;
typedef vector<vlint> vvlint;
typedef pair<lint, lint> plint;
typedef double ld;

template<class T> bool chmin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool chmax(T &a, T b) { return a < b ? (a = b, true) : false; }

int mod = 1000000007;
struct mint {
	lint x;
	mint(lint x=0):x((x%mod+mod)%mod){}
	mint operator-() const { return mint(-x);}
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod-a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res+=a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res-=a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res*=a;
	}
	mint pow(lint t) const {
		if (!t) return 1;
		mint a = pow(t>>1);
		a *= a;
		if (t&1) a *= *this;
		return a;
	}
	mint inv() const {
		return pow(mod-2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res/=a;
	}
};

int main() {	
    ios::sync_with_stdio(false);
	cin.tie(0);
	
	int a, b, c;
	cin >> a >> b >> c;
	swap(a, b);swap(a, c);
	cout << a << ' ' << b << ' ' << c;
	return 0;
}


Measured memory_bytes: 256