akhauriyash/Code-Regression huggingface dataset:
//
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define MOD 1000000007
#define PI 3.14159265
#define EPS 1e-9
#define Pi acos(-1.0)
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
#define forr(i,a,b) for(int i=(a); i<(b); i++)
#define clean(arr,val) memset(arr,val,sizeof(arr))
#define forn(i,n) forr(i,0,n)
#define PB push_back
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vpll;

/*CODE START HERE*/

int w, a, b;

int main(){
    ios::sync_with_stdio(0);
    cin >> w >> a >> b;
    if(a > b) swap(a,b);
    cout << max(0,b-(a+w)) << "\n";
    return 0;
}

Measured memory_bytes: 3612
#include <iostream>

using namespace std;
int main()
{
    int A, B;
    cin >> A >> B;

    if (A <= 5)
    {
        cout << 0;
    }
    else if (A >= 6 && A <= 12)
    {
        cout << B / 2;
    }
    else
    {
        cout << B;
    }

    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#define N NULL

struct node{
  struct node *right;
  struct node *left;
  struct node *parent;
  int key;
};
typedef struct node *Node;



Node root;
Node tMini(Node u);
Node tMax(Node u);
Node tSearch(Node u, int v);
Node tSuccessor(Node u);
void tDelete(Node u);
void insert(int u);
void inorder(Node u);
void preorder(Node u);

int main(){
  int b,n,i;
  char a[20];
  
  scanf("%d", &n);

  for(i = 0; i < n; i++ ){
    
    scanf("%s", a);
    
    if( a[0] == 'f' ){
      
      scanf("%d", &b);
      
      Node t = tSearch(root, b);
      
      if ( t != N ) printf("yes\n");
      
      else printf("no\n");
      
    } else if ( a[0] == 'i' ){
      
      scanf("%d", &b);
      
      insert(b);
      
    } else if ( a[0] == 'p' ){
      
      inorder(root);
      
      printf("\n");
      
      preorder(root);
      
      printf("\n");
      
    } else if ( a[0] == 'd' ){
      
      scanf("%d", &b);
      
      tDelete(tSearch(root,b));
    }
  }

  return 0;
}
Node tMini(Node u){

  while(u->left!=N){
    u=u->left;
  }

  return u;
}

Node tMax(Node u){

  while(u->right!=N){
    u=u->right;
  }

  return u;
  
}

Node tSearch(Node u, int v){
  u=root;

  while(u!=N && u->key!=v){
    if(v < u->key) u=u->left;
    else u=u->right;
  }

  return u;
  
}

Node tSuccessor(Node u){
  Node y;

  if(u->right!=N) return tMini(u->right);

  y=u->parent;

  while(y!=N && u==y->right){
    u=y;
    y=y->parent;
  }

  return y;
}

void tDelete(Node u){
  Node y;
  Node x;

  if(u->left==N || u->right==N) y=u;
  else y=tSuccessor(u);

  if(y->left!=N) x=y->left;
  else x=y->right;

  if(x!=N) x->parent=y->parent;

  if(y->parent==N) root=x;
  else if(y==y->parent->left) y->parent->left=x;
  else y->parent->right=x;

  if(y!=u) u->key=y->key;
  
}

void insert(int u){
  Node y=N;
  Node x=root;
  Node z;

  z=malloc(sizeof(struct node));
  z->key=u;
  z->left=N;
  z->right=N;

  while(x!=N){
    y=x;
    
    if(z->key < x->key) x=x->left;
    else x=x->right;
  }

  z->parent=y;

  if(y==N) root=z;
  else if (z->key < y->key) y->left=z;
  else y->right=z;

}

void inorder(Node u){
  
  if(u==N) return;

  if(u->left!=N) inorder(u->left);
  
  printf(" %d",u->key);
  
  if(u->right!=N) inorder(u->right);
  
}

void preorder(Node u){

  if(u==N) return;
  
  printf(" %d",u->key);
  
  if(u->left!=N) preorder(u->left);
  

  if(u->right!=N) preorder(u->right);
  
  
}



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

using namespace std;

int main(void){
    int d;
    string s;

    cin >> s;

    s.erase(4,1);
    s.erase(6,1);

    d = stoi(s);

    if( d <= 20190430 ) cout << "Heisei" << endl;
    else cout << "TBD" << endl;
    return 0;
}

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

string S;
void solve()
{
	long long sum = 0;
	for(int sign = 0; sign < (1 << S.length()-1); sign++){
		string T;
		T = S[0];	
		for(int i = 0; i < S.length()-1; i++){
			if((sign >> i) & 1){
				T += '+';
				T += S[i+1];
			}
			else{
				T += S[i+1];
			}
		}
		long long  x = 0;
		for(int i = 0; i < T.length(); i++){
			if(T[i] == '+'){
				sum += x;
				x = 0;
			}
			else{
				x  = x * 10;
				x += (int)(T[i] - '0');
			}
		}
		sum += x;
	}
	cout << sum << endl;
}
int main()
{
	cin >> S;
	solve();
	return 0;
}


Measured memory_bytes: 256
//  main.cpp
//  CppTest

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[]) {
    
    /////////////////////
    // Write code below /
    /////////////////////
    
    int N;
    
    int dmin = 100;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int A;
        cin >> A;
        int d = 0;
        while (A % 2 == 0) {
            A /= 2;
            d++;
        }
        dmin = min(dmin, d);
    }
    cout << dmin << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin>>n;
	int ans = 1000 * (n / 500);
	n = n % 500;
	ans = ans + 5 * (n / 5);
	cout<<ans;
}

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

using namespace std;

vector<pair<pair<int, int>, int> >v;

int main() {

	int i;

	int A, B, C;

	scanf("%d %d %d",&A,&B,&C);

	v.push_back(make_pair(make_pair(A, B), C));

	int ans = 0;

	while (1) {
		if (A % 2 == 0 && B % 2 == 0 && C % 2 == 0) {
			int tmpA = (B + C) / 2;
			int tmpB = (A + C) / 2;
			int tmpC = (A + B) / 2;
			A = tmpA;
			B = tmpB;
			C = tmpC;
			for (i = 0; i < v.size(); i++) {
				if (v[i].first.first == A&&v[i].first.second == B&&v[i].second == C) {
					printf("-1\n");
					return 0;
				}
			}
			v.push_back(make_pair(make_pair(A, B), C));
			ans++;
		}
		else {
			break;
		}
	}
	printf("%d\n",ans);

	return 0;
}

Measured memory_bytes: 256
a,b,c,d = input()
for i in range(2**3):
    ch = ["+","+","+"]
    for j in range(3):
        if (i >> j & 1):
            ch[j] = "-"
    if eval(a+ch[0]+b+ch[1]+c+ch[2]+d) == 7:
        print(a+ch[0]+b+ch[1]+c+ch[2]+d,"=7", sep = "")
        break

Measured memory_bytes: 9116
#include<bits/stdc++.h>
using namespace std;
bool p=false;
int main()
{
	int n,x,k,ans=0;
	vector<int>v;
	scanf("%d %d",&n,&x);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&k);
		v.push_back(k);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)
	{
		if(x-v[i]>=0)ans++,x-=v[i];
		else p=true; 
	}
	if(x==0||p)printf("%d",ans);
	else printf("%d",max(ans-1,0));
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cmath>

using namespace std;

typedef long long int ll;

#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;
const int MAXN = 3001;

int main()
{
    string s,t;
    cin >> s >> t;

    int n = s.size(),m = t.size();

    vector<vector<int> > dp(n+1),flag(n+1);
    for(int i = 0; i <= n; i++){
        dp[i].resize(m+1,0);
        flag[i].resize(m+1,0);
    }

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(s[i] == t[j]){
                dp[i+1][j+1] = dp[i][j] + 1;
                flag[i+1][j+1] = 1;
            }else dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]);
        }
    }
    //cout << s << endl;
    int max_len = dp[n][m];
    string ans="";
    for(int i = n; i > 0; i--){
        for(int j = m; j > 0; j--){
            if(flag[i][j] == 1 && dp[i][j] == max_len){
                ans = s[i-1] + ans;
                dp[i][j] = -1;
                max_len--;
                break;
            }
        }
    }

    cout << ans << endl;


    // for(int i = 0; i <= n; i++){
    //     for(int j = 0; j <= m; j++){
    //         if(i == 0 && j != 0){
    //             cout << t[j-1] << " ";
    //             continue;
    //         }
    //         if(i != 0 && j == 0){
    //             cout << s[i-1] << " ";
    //             continue;
    //         }
    //         cout << dp[i][j] << " ";
    //     }printf("\n");
    // }

    return 0;
}

Measured memory_bytes: 70784
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

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

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

	sort(a.begin(), a.end());

	//入力で与えられた符号が正、負でまとめておく
	vector<int> pos;
	vector<int> neg;

	for (int i = 1; i < n - 1; i++) {
		if (a[i] >= 0) {
			pos.push_back(a[i]);
		}
		else {
			neg.push_back(a[i]);
		}
	}

	//手順を記録する配列
	vector<pair<int, int>> res;

	int base=0;
	//右端と左端を核にして、+→-の順でまとめてくっつけたい
	//+があればひっくり返すので、初手が入れ替わる
	if (pos.size() > 0) {
		base =a[0]-a[n-1];
		res.push_back(make_pair(a[0], a[n - 1]));
	}
	else {
		base = a[n - 1] - a[0];
		res.push_back(make_pair(a[n - 1], a[0]));
	}

	//最終的に+にしたい分を一つ残してまとめて-側に突っ込む
	if (pos.size() > 0) {
		for (int i = 0; i < pos.size() - 1; i++) {
			res.push_back(make_pair(base, pos[i]));
			base -= pos[i];
		}

		//残しておいた最後の+で-をまとめてひっくり返す
		res.push_back(make_pair(pos[pos.size() - 1], base));
		base = pos[pos.size() - 1] - base;
	}
	

	//-にしたい分を全部くっつける
	for (int i = 0; i < neg.size(); i++) {
		res.push_back(make_pair(base, neg[i]));
		base -= neg[i];
	}

	//出力
	cout << base << endl;
	for (int i = 0; i < res.size(); i++) {
		cout << res[i].first << " " << res[i].second << endl;
	}

	return 0;
}


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

int main(){
    int n,x;
    cin>>n>>x;
    vector<int> a(n);
    rep(i,n) cin>>a[i];
    sort(a.begin(),a.end());
    int j = 0;
    while(x>0 && j<n){
        x-=a[j];
        j++;
    }
    if(x!=0) j--;
    cout<< j << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e5 + 50;
ll dp[M], dp1[M], mod, n;
vector<ll> graph[M], pre[M], post[M];
void read_graph(){
    for(int i = 0; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        a--, b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
}
void dfs(ll node, ll par){
    dp[node] = 1;
    for(auto child : graph[node]){
        if(child != par){
            dfs(child, node);
            dp[node] *=( dp[child] + 1LL);
            dp[node] %= mod;
        }
    }
    if(par!= -1)pre[par].push_back(dp[node] + 1);   
}
void dfs2(ll node, ll par){
    int c = 0;
    for(auto child : graph[node]){
        if(child != par){
            dp1[child] = (dp1[node]+1)%mod;
            if(c > 0)dp1[child] = (dp1[child]*pre[node][c-1])%mod;
            if(post[node].size() - 1LL  != c)dp1[child] = (dp1[child]*post[node][post[node].size() - 2 - c])%mod;
            dp[child] *= (dp1[child]+1)%mod, dp[child] %= mod;
            dfs2(child, node);
            c++;
        }
    }
}
int main()
{
    cin >> n >> mod;
    read_graph();
    dfs(0LL, -1LL);
    for(ll i = 0; i < n; i++){
        if(pre[i].size() != 0){
            for(ll j = pre[i].size()-1; j >= 0; j--)post[i].push_back(pre[i][j]);
            for(int j = 1; j < pre[i].size(); j++){
                pre[i][j] = (pre[i][j]*pre[i][j-1])%mod;
                post[i][j] = (post[i][j]*post[i][j-1])%mod;
            }
        }
    }
    dfs2(0LL, -1LL);
    for(int i = 0; i < n; i++)cout << dp[i] << endl;
}

Measured memory_bytes: 24192
s = input()
ans = [0 for i in range(len(s)+1)]

for i in range(len(s)):
    if s[i] == '<':
        ans[i+1] = max(ans[i+1],ans[i]+1)
for i in range(len(s)-1,-1,-1):
    if s[i] == '>':
        ans[i] = max(ans[i],ans[i+1]+1)

print(sum(ans))


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

int main(){
    string s;
    cin >> s;
    vector<int> a(10,-1);
    int ans = 1000;
    for(int i=0;i<s.size()-2;i++){
        string s1 = s.substr(i,3);
        //cout << s1 << endl;
        a[i] = atoi(s1.c_str());
        ans = min(ans,abs(753-a[i]));
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
"""


第5回 ドワンゴからの挑戦状 予選　　B - Sum AND Subarrays

N<=1000より美しさの全列挙は可能
K個選んでbitごとの論理積の最大値

K個選ぶ方法はnCk(10**6?, K)とかなので、全探索は間に合わない

K個選んだもののうちの最大、を言い換えを考える。
bitごとの論理積をとるので、選ばれたK個の中で、bitが大きいものについて、K個すべてで1が立っていると嬉しい。
また、2**0 + ... + 2**(N-1) <= 2**N なので、大きいbitが一個立ってる方が、それより小さいもの全てが立っているより嬉しい。
→大きいbitから見ていって、各美しさでそのbitに1が立っているか調べて、K個以上立っていれば、その美しさの中からK個採用される
　この時いずれの美しさも、今見ているbitが立っているので、答えにそのbit分加える。
　で、次のbitをみていって～を繰り返す。
　最下位bitまで見ても、美しさがK個以上のこっているなら、そこから適当にK個選んだら同じ答えになるので気にしなくてよい

bitごとの論理積が大きくなるようにK個選ぶ　→　大きいbitから、そのbitが立っているものがK個以上ある物が選ばれる
の言いかえ
"""

N,K = map(int, input().split())
A = list(map(int, input().split()))

accum = [0] * N
accum[0] = A[0]
for i in range(1,N):
    accum[i] = accum[i-1] + A[i]

beauty = []
for l in range(N):
    for r in range(l, N):
        if l == 0:
            beauty.append(accum[r])
        else:
            beauty.append(accum[r] - accum[l-1])


# 10**9 * 1000 から、だいたい2**40ぐらい
ans = 0
candidate = beauty
for i in range(40, -1, -1):
    nxt_candidate = []
    cnt = 0
    for b in candidate:
        if b & (1 << i):
            cnt += 1
            nxt_candidate.append(b)
    
    if cnt >= K:
        ans += 2**i
        candidate = nxt_candidate

print(ans)

Measured memory_bytes: 65916
n, m = map(int, input().split())
L = list()
R = list()

for a in range(m):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

ans = 0
Lmax = max(L)
Rmin = min(R)

for a in range(n):
    if Lmax <= a+1 and a+1 <= Rmin:
        ans += 1

print(ans)


Measured memory_bytes: 16848
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

ll n,ans;
int main(){
	ll a,b;
	scanf("%lld%lld%lld",&n,&a,&b);
	printf("%lld\n",(n-a+1)*(n-b+1));
	return 0;
}

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

using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n, 0);
  vector<int> c(n, 0);
  for (int i = 0; i < n; i++) {
  	scanf("%d", &v[i]);
  }
  c[1] = abs(v[1] - v[0]);
  for (int i = 2; i < n; i++) {
    c[i] = min(c[i - 2] + abs(v[i] - v[i - 2]), c[i - 1] + abs(v[i] - v[i - 1]));
  }
  printf("%d\n", c[n - 1]);
  return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define rep(i,n) for(int64_t i=0;i < (int64_t)(n);i++)
#define invrep(i,n) for(int64_t i=n-1;i >= 0L;i--)
#define prep(i,n) for(int64_t i=1;i <= (int64_t)(n);i++)
using namespace std;

//////全探索したいときに開けよ////////
vector<vector<int>> fspattern;
vector<int> fspattern_draft;
int fspcnt = 0;

void setfspattern_core(int floor,int number,int degree){
  if(floor==degree){
    fspattern.at(fspcnt) = fspattern_draft;
    fspcnt++;
  }else{
    rep(i,number){
      fspattern_draft.at(floor) = i;
      setfspattern_core(floor+1,number,degree);
    }
  }
}

void setfspattern(int number,int degree){
  int64_t array_num = 1;
  fspcnt = 0;
  rep(i,degree){
    array_num *= number;
  }
  fspattern = vector<vector<int>>(array_num,vector<int>(degree));
  fspattern_draft = vector<int>(degree);
  setfspattern_core(0,number,degree);
}

//////nCrのmodを求めたいときに開けよ/////////
//int64_t univMod = 1000000007;
//int64_t factnum = 100000
//vector<int64_t> modfact(factnum);
//vector<int64_t> modinvfact(factnum);

/*void setmodfact(){
  for(int i=0;i<factnum;i++){
    if(i == 0){
      modfact.at(i) = 1;
    }else{
      modfact.at(i) = (modfact.at(i-1)*i)%univMod;
    }
  }
}

int64_t calcmodpower(int64_t a,int64_t n){
  int64_t res = 1;
  while(n != 0){
    if(n & 1){
      res = (res * a) % univMod;
    }
    a = (a*a) % univMod;
    n = n >> 1;
  }
  return res;
}

int64_t calcinverse(int64_t n){
	return calcmodpower(n,univMod-2);
}

void setmodinvfact(){
  for(int i=0;i<factnum;i++){
    if(i==0){
      modinvfact.at(i) = 1;
    }else{
      modinvfact.at(i) = calcmodpower(modfact.at(i),univMod-2);
    }
  }
}

int64_t calcmodcomb(int64_t n,int64_t r){
  return (((modfact.at(n)*modinvfact.at(r))%univMod)*modinvfact.at(n-r))%univMod;
}

int64_t calcmodpositive(int64_t n){
  return (n + ((abs(n)+univMod-1)/univMod)*univMod)%univMod;
}
  
    */


/*
int64_t calcGCD(int64_t a,int64_t b){
	int64_t p = a;
	int64_t q = b;
	int64_t c = a%b;
	while(c != 0){
		p = q;
		q = c;
		c = p%q;
	}
	return q;
}
*/

int main(){
  int n,a,b;
  cin >> n;
  if(n == 2){
    cin >> a >> b;
    cout << a+b << endl;
  }else{
    cout << "Hello World" << endl;
  }
  
}

Measured memory_bytes: 256
local sx,sy,tx,ty=io.read("n","n","n","n")
local dx=tx-sx
local dy=ty-sy
local path=string.rep("U",dy)..string.rep("R",dx)..
        string.rep("D",dy)..string.rep("L",dx)..
        "L"..string.rep("U",dy+1)..string.rep("R",dx+1).."D"..
        "R"..string.rep("D",dy+1)..string.rep("L",dx+1).."U"
print(path)

Measured memory_bytes: 760
/* ＵＴＦ－８ */
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <array>
#include <queue>
#include <stack>
#include <algorithm>
#include <tuple>
#include <numeric>
#include <type_traits>
#include <limits>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#define USE_COLORED 1

#ifdef USE_COLORED
static const char stdout_pre[]  = "\033[1m"; // bold black
static const char stderr_pre[]  = "\033[31;1m"; // bold red
static const char stdxxx_post[] = "\033[m";

// 当然ながら GCC(g++)/libstdc++ 依存
#include <ext/stdio_filebuf.h>
using stdio_filebuf = __gnu_cxx::stdio_filebuf<char>;
class colored_stdio_filebuf : public stdio_filebuf {
private:
	const char* const pre_;
	const char* const post_;

public:
	colored_stdio_filebuf(FILE* fp, const char* pre, const char* post) : stdio_filebuf(fp, std::ios_base::out), pre_(pre), post_(post)
	{
	}

protected:
	std::streamsize xsputn(const char* s, std::streamsize count) override
	{
		stdio_filebuf::xsputn(pre_, strlen(pre_));
		auto ret = stdio_filebuf::xsputn(s, count);
		stdio_filebuf::xsputn(post_, strlen(post_));
		return ret;
	}
};
#endif

int main()
{
#ifdef USE_COLORED
	if (getenv("DEBUG")) {
		std::cout.rdbuf(new colored_stdio_filebuf(stdout, stdout_pre, stdxxx_post));
		std::cerr.rdbuf(new colored_stdio_filebuf(stderr, stderr_pre, stdxxx_post));
	} else
#endif
	{
		int fd = open("/dev/null", O_WRONLY);
		dup2(fd, 2);
		close(fd);
	}
	
	int A, B;
	std::cin >> A >> B;
	if (A <= 8 && B <= 8) {
		std::cout << "Yay!\n";
	} else {
		std::cout << ":(\n";
	}
}


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

Measured memory_bytes: 128
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)

n = int(input())
x = list(map(int, input().split()))
if n==1:
    print(x[0])
else:
    element = gcd(x[0], x[1])
    if n==2:print(element)
    else:
        for i in range(2,n):
            element=gcd(element,x[i])
        print(element)

Measured memory_bytes: 14252
N, M = map(int, input().split())
H = list(map(int, input().split()))
l = [[0] for _ in range(N+1)]
cnt = 0
for _ in range(M):
    a, b = map(int, input().split())
    l[a].append(H[b-1])
    l[b].append(H[a-1])
for i in range(1, N+1):
    if H[i-1] > max(l[i]):
        cnt += 1
print(cnt)

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

struct Card { char suit,value; };

void bubble(struct Card A[],int N){
    for(int i = 0;i < N;i++){
        for(int j = N-1;j >= i;j--){
            if(A[j].value < A[j - 1].value){
                swap(A[j],A[j-1]);
            }
        }
    }
}
void selection(struct Card A[],int N){
    for(int i = 0;i < N;i++){
        int miji = i;
        for(int j = i;j < N;j++){
            if(A[j].value < A[miji].value)miji = j;
        }
        swap(A[i],A[miji]);
    }
}
void print(struct Card A[],int N){
    for(int i = 0;i < N;i++){
        if(i > 0)cout << " ";
        cout << A[i].suit << A[i].value;
    }
    cout << endl;
}
bool isStable(struct Card C1[],struct Card C2[],int N){
    for(int i = 0;i < N;i++){
        if(C1[i].suit != C2[i].suit) return false;
    }
    return true;
}
int main() {
    Card C1[100],C2[100];
    int N;
    char ch;
    cin >> N;
    for(int i = 0;i < N;i++){
        cin >> C1[i].suit >> C1[i].value;
    }
    for(int i = 0;i < N;i++)C2[i] = C1[i];
    bubble(C1,N);
    selection(C2,N);
    
    print(C1,N);
    cout << "Stable" << endl;
    print(C2,N);
    if(isStable(C1,C2,N)){
        cout << "Stable" << endl;
    }else{
        cout << "Not stable" << endl;
    }
}


Measured memory_bytes: 3116
import math
while True:
    n=int(input())
    if n==0:break
    else:
        s = list(map(int, input().split())) #入力の値が横並びの時使う
        x=sum(s)
        y=x/n
        a=0
        for i in range(n):
             z=(s[i]-y)**2
             a+=z
        b=math.sqrt(a/n)
        print(format(b, '.8f'))


Measured memory_bytes: 5700
#include "bits/stdc++.h"

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define REP(i,a) for(int i = 0; i < (a); ++i)

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int UI;

using namespace std;

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

    int n, k;
    cin >> n >> k;

    int ans = 1;

    REP(i, n)
    {
        if (ans * 2 <= ans + k)
        {
            ans *= 2;
        }
        else
        {
            ans += k;
        }
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
 
int isStable(char in[36][3] ,char out[36][3]);
int N;
 
int main(){
 
  int i,j,min;
  char C[36][3],num[36][3],A[36][3],tmp[36];
 
  scanf("%d",&N);
  
  for(i=0;i<N;i++){
    scanf("%s",C[i]);
    strcpy(num[i],C[i]);
    strcpy(A[i],C[i]);
  }
 
  for(i=0;i<N;i++){
    for(j=N-1;j > i;j-- ){
      if(C[j][1]<C[j-1][1]){
    strcpy(tmp,C[j]);
    strcpy(C[j],C[j-1]);
    strcpy(C[j-1],tmp);
      }
    }
  }
  for(i=0;i<N-1;i++)
    printf("%s ",C[i]);
 
    printf("%s\n",C[i]);
 
     
    if((isStable(num,C))==1)printf("Stable\n");
    else printf("Not stable\n");
 
 
   
for(i=0;i<N;i++){
    min=i;
    for(j=i;j<N;j++){
      if(A[j][1]<A[min][1])min=j;
    }
    strcpy(tmp,A[i]);
    strcpy( A[i],A[min]);
    strcpy( A[min],tmp);
 }
 
  for(i=0;i<N-1;i++)
    printf("%s ",A[i]);
 
  printf("%s\n",A[i]);
   
 
  if((isStable(num,A))==1)printf("Stable\n");
    else printf("Not stable\n");
 
  return 0;
}
 
int isStable(char in[36][3] , char out[36][3]){
 
  int i,j,a,b;
 
  for(i=0;i<N;i++){
    for(j=i+1;j<N;j++){
      for(a=0;a<N;a++){
    for(b=a+1;b<N;b++){
      if((in[i][1]==in[j][1])&&(strcmp(in[i],out[b])==0)&&(strcmp(in[j],out[a])==0))return 0;
    }
      }
    }
  }
  return 1;
  
}

Measured memory_bytes: 624
import java.util.*;

public class Main{

	public static void main(String[] args) throws java.io.IOException{
		int tmp;
		int[] x;
		x = new int[3];
		Scanner in = new Scanner(System.in);
		for(int i = 0; i < 3; i++){
			x[i] = in.nextInt();
		}
		for(int a = 2; a > 0; a--){
			for(int b = 0; b < a; b++){
				if(x[b] > x[b+1]){
					tmp = x[b];
					x[b] = x[b+1];
					x[b+1] = tmp;
				}
			}
		}
		System.out.println(x[0] + " " + x[1] + " " + x[2]);
	}
}

Measured memory_bytes: 17176
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author KharYusuf
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BOrdinaryNumber solver = new BOrdinaryNumber();
        solver.solve(1, in, out);
        out.close();
    }

    static class BOrdinaryNumber {
        public void solve(int testNumber, FastReader s, PrintWriter w) {
            int n = s.nextInt(), a[] = new int[n], c = 0;
            for (int i = 0; i < n; i++) {
                a[i] = s.nextInt();
            }
            for (int i = 1; i < n - 1; i++) {
                if ((a[i] > a[i - 1] && a[i] < a[i + 1]) || (a[i] < a[i - 1] && a[i] > a[i + 1])) c++;
            }
            w.println(c);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {

            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {

                curChar = 0;

                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        public int nextInt() {

            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public boolean isSpaceChar(int c) {

            if (filter != null)
                return filter.isSpaceChar(c);

            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}



Measured memory_bytes: 21328
import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B

main :: IO ()
main = solve <$> f <*> f >>= print
  where
    f = head <$> B.words <$> B.getLine

solve :: ByteString -> ByteString -> Int
solve s t = foldl f lt $ take (ls - lt + 1) $ B.tails s
  where
    ls = B.length s
    lt = B.length t
    f cm bs = min cm $ g t bs
    g xs ys = sum $ B.zipWith (\x y -> if x == y then 0 else 1) xs ys


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

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, a, b) for(int i = (a); i > (b); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define mp make_pair
#define bitcnt __builtin_popcount
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"

typedef long long ll;
typedef unsigned long long ul;
typedef vector<int> V;
typedef vector<ll> VL;
typedef pair<int, int> Pair;
typedef pair<ll, ll> PL;
typedef priority_queue<int> PQ;
typedef priority_queue<int, V, greater<int>> PQG;

const int INF = 1e9;
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1e18;
int N;
V P, tmp;

int main() {
    cin >> N;
    P.resize(N);
    tmp.resize(N + 1);
    VECCIN(P);
    REP(i, N) { tmp[P[i]] = tmp[P[i] - 1] + 1; }
    cout << N - *max_element(All(tmp)) << "\n";
    return 0;
}


Measured memory_bytes: 1792
import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		final int MAX = 100000;
		int[] sums = new int[MAX];
		int idx = 3;
		for (int i = 3; i < MAX; i++) {
		    sums[i] += sums[i - 1];
		    if (isPrime(i) && isPrime((i + 1) /2)) {
		        sums[i]++;
		    }
		}
		int q = sc.nextInt();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < q; i++) {
		    int left = sc.nextInt();
		    int right = sc.nextInt();
		    sb.append(sums[right] - sums[left - 1]).append("\n");
		}
       System.out.print(sb);
   }
   
   static boolean isPrime(int x) {
       if (x == 1) {
           return false;
       }
       for (int i = 2; i <= Math.sqrt(x); i++) {
           if (x % i == 0) {
               return false;
           }
       }
       return true;
   }
}




Measured memory_bytes: 64956
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1e9+7;

#define vec     vector<int>
#define vecll   vector<ll>
#define vec2    vector<vector<int>>
#define vec2ll  vector<vector<ll>>
#define vpair   vector<pair<int, int>>
#define vpairll vector<pair<ll, ll>>

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define f_sum(var)       accumulate(all(var), 0)
#define f_sumll(var)     accumulate(all(var), 0LL)
#define chmin(v1, v2)    v1 = min(v1, v2)
#define chmax(v1, v2)    v1 = max(v1, v2)
#define mpair(v1, v2)    make_pair(v1, v2)
#define pb(var)          push_back(var)
#define prt(var)         cout << var << "\n"
#define scn(var)         cin >> var;
#define pYes()           prt("Yes")
#define pNo()            prt("No")
#define prtd(n, var)     cout << fixed << setprecision(n) << var << "\n"
#define prt2(v1, v2)     cout << v1 << " " << v2 << "\n"
#define prt3(v1, v2, v3) cout << v1 << " " << v2 << " " << v3 << "\n"
#define prtall(var)      rep(i,var.size()){cout << var[i] << " ";} cout << "\n"
//----------------------------------------------------------------

int main(void) {
  int n, k;
  cin >> n >> k;
  arrll(h,n);

  sortall(h);

  ll ans=1e12;
  rep(i,n-k+1){
    chmin(ans, abs(h[i]-h[i+k-1]));
  }
  prt(ans);
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i = 0; i < n; i++)
#define _rep(i,m,n) for(long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
const int N = 1000000;
const ll mod = 1000000007;

int main(){
   ll n,k; cin >> n >> k;
   ll ans = k * (ll)pow(k - 1,n - 1);
   print(ans);
}

Measured memory_bytes: 512
N, K = map(int, input().split())
A = list(map(int, input().split()))
dict = {}
for i in range(N):
    if(A[i] in dict):
        dict[A[i]] += 1
    else:
        dict[A[i]] = 1
if(len(dict) <= K):
    print(0)
else:
    l = sorted(dict.values())
    ans = 0
    for i in range(len(dict) - K):
        ans += l[i]
    print(ans)

Measured memory_bytes: 32184
n = int (input ())
cnt = [[0 for i in range (10)] for i in range (10)]

for i in range (1, n + 1) :
    q = i % 10
    p = int (str (i)[0])
    cnt[p][q] += 1

ans = 0
for i in range (1, 10) :
    for j in range (1, 10) :
        ans += cnt[i][j] * cnt[j][i]

print (ans)


Measured memory_bytes: 3060
#pragma GCC optimize("O3")
#define first x
#define second y
#include <bits/stdc++.h>

using namespace std;

enum {MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009};
typedef unsigned short ushort;typedef unsigned int uint;typedef long long ll;typedef unsigned long long ull;
#ifndef CrafterKolyan
#define endl '\n'
#endif

signed main() {
#ifndef CrafterKolyan
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int n;
    cin >> n;
    cout << (n & 1 ? n << 1 : n) << endl;
    return 0;
}


Measured memory_bytes: 256
#include <cassert>
#include <cstdint>
#include <functional>
#include <vector>

template <typename Abelian> class FenwickTree {
	using uint32 = std::uint_fast32_t;
	using const_reference = const Abelian &;
	using F = std::function<Abelian(const_reference, const_reference)>;
	const Abelian neutral_;
	const F p, m;
	std::vector<Abelian> tree;

public:
	FenwickTree(const uint32 size, const_reference neutral = Abelian(),
		const F &p = std::plus<Abelian>(),
		const F &m = std::minus<Abelian>())
		: neutral_(neutral), p(p), m(m), tree(size + 1, neutral_) {}
	FenwickTree(const uint32 size, const_reference neutral, const F &p,
		const std::function<Abelian(const_reference)> &n)
		: neutral_(neutral), p(p),
		m([&p, &n](const_reference l, const_reference r) { return p(l, n(r)); }),
		tree(size + 1, neutral_) {}
	void update(uint32 index, const std::function<void(Abelian)> &g) {
		assert(index < tree.size() - 1);
		++index;
		const Abelian a = tree[index];
		g(tree[index]);
		const_reference b = tree[index];
		while ((index += index & ~index + 1) < tree.size())
			tree[index] = p(m(tree[index], a), b);
	}
	void update(uint32 index, const_reference data) {
		update(index, [&data](Abelian &d) { d = data; });
	}
	void add(uint32 index, const_reference diff) {
		assert(index < tree.size() - 1);
		++index;
		do
			tree[index] = p(tree[index], diff);
		while ((index += index & ~index + 1) < tree.size());
	}
	Abelian range(uint32 end) const {
		assert(end < tree.size());
		Abelian ret = neutral_;
		do
			ret = p(tree[end], ret);
		while (end &= end - 1);
		return ret;
	}
	Abelian range(uint32 begin, uint32 end) const {
		assert(begin <= end);
		return m(range(end), range(begin));
	}
	Abelian operator[](uint32 index) const {
		assert(index < tree.size() - 1);
		return range(index, index + 1);
	}
};

#include <iostream>
int main(void) {
	int n, q;
	std::cin >> n >> q;
	FenwickTree<int> T(n, 0);
	while (q--) {
		int c, x, y;
		std::cin >> c >> x >> y;
		if (c) {
			std::cout << T.range(x - 1, y) << "\n";
		}
		else {
			T.add(x-1,y);
		}
	}
	return 0;
}


Measured memory_bytes: 3112
import math
ans=0
n=int(input())
yakusu=[]
for num in range(1,math.floor(math.sqrt(n))+1):
    if n%num==0:
        yakusu.append(num)
        if n//num>num:
            yakusu.append(n//num)
# print(yakusu)
for k in yakusu:
    m=n//k-1
    if m>k:
        ans+=m
print(ans)

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

#define int long long
#define endl "\n"
#define REP(i,a,n) for(int i=a;i<n;++i)
#define REPR(i,a,n) for(int i=a;i>n;--i)
#define RUP(a,b) ((a+b-1)/(b))
#define SORTG(v) sort(v.begin(),v.end(),greater<>())
#define SORT(v) sort(v.begin(),v.end())
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define INF LLONG_MAX/3

typedef long long ll;
typedef pair<int,int> Pii;
typedef tuple<int,int,int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<string> Vs;

template<class T> void chmax(T& a, T b) {if(a<b){a=b;}}
template<class T> void chmin(T& a, T b) {if(a>b){a=b;}}
void YesNo(int a){cout<<((a)?"Yes":"No")<<endl;}
void YESNO(int a){cout<<((a)?"YES":"NO")<<endl;}
void AorB(int a,string b,string c){cout<<((a)?b:c)<<endl;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
void start(){cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);}

signed main(){start();
    int h,w,d,q;
    cin>>h>>w>>d;
    VVi a(h,Vi(w));
    vector<Pii> ba(h*w);
    Vi ma(h*w);
    REP(i,0,h) REP(j,0,w){
        cin>>a[i][j];
        a[i][j]--;
        ba[a[i][j]]=mp(i,j);
    }
    cin>>q;
    Vi l(q),r(q);
    REP(i,0,q){
        cin>>l[i]>>r[i];
        l[i]--; r[i]--;
    }
    REPR(i,h*w,-1){
        if(i+d>=h*w) continue;
        ma[i]=ma[i+d]+abs(ba[i+d].first-ba[i].first)+abs(ba[i+d].second-ba[i].second);
    }
    REP(i,0,q) cout<<ma[l[i]]-ma[r[i]]<<endl;
    return 0;
}


Measured memory_bytes: 5504
#include<stdio.h>
int i,j,k,a,b,c[1001],d[1001],e[10001],f=-1;
int main(void)
{
	scanf("%d %d",&a,&b);
	for(i=0;i<=a;i++){
		for(j=0;j<=b;j++){
			e[i]=-1;
		}
	}
	for(i=0;i<a;i++){
		scanf("%d %d",&c[i],&d[i]);
	}
	e[0]=0;
	for(i=0;i<=b;i++){
		if(e[i]!=-1){
			for(j=0;j<=a;j++){
				if(e[i+d[j]]<e[i]+c[j] && i+d[j]<=b){
					e[i+d[j]]=e[i]+c[j];
				}
			}
		}
	}
	for(i=0;i<=b;i++){
			if(f<e[i]){
				f=e[i];
			}
	}
	printf("%d\n",f);
	return 0;
}

Measured memory_bytes: 1068
#include "bits/stdc++.h"

using namespace std;

//using Matrix = vector< vector<int> >;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 10;
//const ll INF = 1e18 + 10LL;
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
const double EPS = 1e-10;
bool cmp(P a, P b) { return a.second < b.second; } //End sort (Interval scheduling problem)
ll MOD = 1e9 + 7;


ll n, m;
pair<ll, ll> drk[100010];

ll ans;

int main(void){
	cin>>n>>m;
	for (int i = 0; i < n; i++) cin>>drk[i].first>>drk[i].second;

	sort(drk, drk + n);

	int i = 0;
	while (0 < m && i < n) {
		ans += drk[i].first * min(drk[i].second, m);
		m -= drk[i++].second;
	}

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

Measured memory_bytes: 1792
use std::collections::HashMap;

fn main() {
    let n: i64 = read();
    let nums: Vec<i64> = read_vec();
    let q: i64 = read();
    let operates: Vec<Vec<i64>> = read_vec2(q);

    let mut num_dict = create_dict(&nums);
    let mut num_sum: i64 = num_dict.iter().map(|(key, value)| key * value).sum();

    for operate in operates.iter() {
        let b = operate[0];
        let c = operate[1];

        if let Some(b_count) = num_dict.remove(&b) {
            let c_count = match num_dict.get(&c) {
                Some(i) => i + b_count,
                None => b_count,
            };
            num_dict.insert(c, c_count);

            num_sum = num_sum + (b * -b_count) + (c * b_count);
        }
        
        println!("{}", num_sum);
    }
}

fn create_dict(nums: &Vec<i64>) -> HashMap<i64, i64> {
    let mut d: HashMap<i64, i64> = HashMap::new();
    for num in nums.iter() {
        let count: i64 = match d.get(num) {
            Some(i) => *i + 1,
            None => 1,
        };
        d.insert(*num, count);
    }
    return d;
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn read_vec2<T: std::str::FromStr>(n: i64) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}


Measured memory_bytes: 12668
#!/usr/bin/env python

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
INF = float("inf")

def main():
    N = int(input())

    ans = INF
    for i in range(1,int(N**.5)+1):
        if N%i == 0:
            ans = min(ans, i + N//i - 2)

    print(ans)

if __name__ == "__main__":
    main()

Measured memory_bytes: 9448
//====== begin of bisma c++ template===========
#include<bits/stdc++.h>
#include <assert.h>
using namespace std;
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define repn(a,b,c) for(int a=b;a>=c;a--)
#define ll long long 
#define sum(a,b) a=a+b
#define mem(a, b) memset(a, (b), sizeof(a))
 
//=======binarysearch===========
int bs(vector<int>&a,int que){
 auto it=lower_bound(a.begin(),a.end(),que);
 if(*it==que){
    return it-a.begin();
 }
 return -1;
}
//========binarysearch=========
 
 
 
//=======SUBSET WITH BRUTEFORCE=============
void subsets(int S[], int n) {
     int nSub = 1<<n; 
     for(int i=0; i<nSub; i++){
          cout<<"{ ";//optional
          for(int k=0; k<n; k++){
               //if the k-th bit is set
               if( (1<<k) & i){
                    cout<<S[k]<<" ";
               } 
          }
          cout<<"}\n"; //optional
     }
}
//======SUBSET WITH BRUTE FORCE================
 
 
//======permuatate all ==================
void permutate(){
int n,x;
cin>>n;
 
vector<int> permutation;
for (int i = 0; i < n; i++) {
    cin>>x;
permutation.push_back(x);
}
do {
 rep(i,0,n-1) cout<<permutation[i];
 cout<<endl;
} while (next_permutation(permutation.begin(),permutation.end()));
 
}
//====permuatate all======================
 
 
 
 
 
//=====check prime========================
bool isprime(int x)
{
  if ( x == 2 ) return true;
  if ( x  < 2 ) return false;
  for ( int i = 2; i * i <= x; i++ )
    if ( x % i == 0 ) return false;
  return true;
}
//======check prime=====================
 
 
 
 
//======= the end of bisma c++ template ============
 

 
int main(){

string a,t;
cin>>a>>t;

int flag=0;

if(a==t){
  flag =1;
}

rep(i,1,a.size()){
  string k ="";
   k = k + a[a.size()-1];

  a.erase(a.begin()+a.size()-1);

  k= k + a;
  a=k;

  if(a==t) flag =1;

}

if(flag==1){
  cout<<"Yes"<<endl;
}else{
  cout<<"No"<<endl;
}
 
  return 0;
}
 

Measured memory_bytes: 256
#include<iostream>
#define rep(i, a) for(int i=0; i<(int)(a); i++)
using namespace std;
int M[100][100];int v, n, c, e,u,r,k;
const  long long int INF = (1<<21);
 void dijkstra() {
     int miv;
     int d[100], co[100];
     rep(i, n) {
         d[i] = INF;
         co[i] = 0;
     }
     d[0] = 0;
     co[0] = 1;
     while (1) {
         miv = INF;
         int u = -1;
         rep(i, n) {
             if (miv > d[i] && co[i] != 2) {
                 u = i;
                 miv = d[i];
             }
         }
         if (u == -1)
             break;
         co[u] = 2;
         rep(v, n) {
             if (co[v] != 2 && M[u][v] != INF) {
                 if (d[v] > d[u] + M[u][v]) {
                     d[v] = d[u] + M[u][v];
                     co[v] = 1;
                 }
             }
         } 
     }
     rep(i, n) {
         cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl;
     }
}
int main()
{
    cin >> n;
    rep(i, n) {
        rep(j, n) {
            M[i][j] = INF;
        }
    }
    rep(i, n) {
        cin >> u >> k;
        rep(j, k) {
            cin >> v >> c;
            M[u][v] = c;
        }
    }
    dijkstra();
}


Measured memory_bytes: 3132
import Control.Monad
import Data.Maybe (fromJust)
import Data.List as List


isVowel :: String -> Bool
isVowel "a" = True
isVowel "i" = True
isVowel "u" = True
isVowel "e" = True
isVowel "o" = True
isVowel _ = False

solve :: String -> String
solve s
    | (isVowel s) = "vowel"
    | otherwise = "consonant"

main :: IO()
main = do
    c <- getLine
    putStrLn $ solve c

Measured memory_bytes: 380
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
  int n;
  cin >> n;
  vector<int> d(n);
  for (int i=0; i<n; i++)
    cin >> d.at(i);
  
  sort(d.begin(), d.end());
  
  int cnt = 1;
  
  for (int i=0; i<n-1; i++)
    if (d.at(i) != d.at(i+1)) cnt++;
  
  cout << cnt << endl;
}

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

int main(){
    int n,d, p = 998244353;
    cin >> n >> d;
    if(d){
        cout << 0 << endl;
        return 0;
    }

    vector<int>  D(n,0);
    for(int i= 1; i< n; i++){
        cin >> D.at(i);
    }
    vector<int> count(n,0);

    long long int answer = 0;

    for(int i = 1; i<n; i++){
        count.at(D.at(i))++;
    }
    if(count.at(0)>0){
        cout << 0 << endl;
        return 0;
    }else{
        answer = 1;
        for(int i = 2; i<=n-1; i++){
           for(int j = 0; j< count.at(i); j++){
               answer *= count.at(i-1);
               answer %= 998244353;
           }
           
        } 
        
    }
    cout << answer<< endl;
}

Measured memory_bytes: 1024
//-----------------------
// author : xyqkoala
// time : 2020-08-22
//-----------------------

#include<iostream>
#include<vector>
#include<string>
#include<deque>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<algorithm>
#include<cmath>
#include<numeric>
using namespace std;

typedef long long LL;
const int MOD = 1e9+7;

void solve();


int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T = 1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


void solve(){
	int h,w,m;
	cin>>h>>w>>m;
	vector<vector<int>> nums(m,vector<int>(2,0));
	unordered_set<string> t;
	vector<int> row(h,0);
	vector<int> col(w,0);
	for(int i=0;i<m;i++){
		cin>>nums[i][0]>>nums[i][1];
		t.insert(to_string(nums[i][0]-1)+" "+to_string(nums[i][1]-1));
		row[nums[i][0]-1]++;
		col[nums[i][1]-1]++;
	}
	int mxrow = -1;
	for(auto item:row){
		mxrow = max(mxrow,item);
	}
	vector<int> rows;
	for(int i=0;i<h;i++){
		if(row[i]==mxrow){
			rows.push_back(i);
		}
	}
	int mxcol = -1;
	for(auto item:col){
		mxcol = max(mxcol,item);
	}
	vector<int> cols;
	for(int i=0;i<w;i++){
		if(col[i]==mxcol){
			cols.push_back(i);
		}
	}

	LL cnt = rows.size();
	cnt = 1ll * cols.size() * cnt;
	if(cnt>m){
		cout<<mxcol+mxrow<<endl;
		return;
	}
	bool f = false;
	for(auto &item:rows){
		for(auto &titem:cols){
			string temp = to_string(item)+" "+to_string(titem);
			if(!t.count(temp)){
				f = true;
				break;
			}
		}
		if(f) break;
	}
	int res = mxcol+mxrow;
	if(!f){
		res--;
	}
	cout<<res<<endl;
}


Measured memory_bytes: 49204