akhauriyash/Code-Regression huggingface dataset:
// Hail god Yato
 
#include <bits/stdc++.h> 
using namespace std;
 
#define hs ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
void solve(){
    ll a;
    cin>>a;
    cout<<a+a*a+a*a*a;
}
int main(){ 
        hs;
        ll t;
        t=1;
        // cin>>t;
        for (int i=1; i<=t; i++){
                //cout<<"Case #"<<i<<": ";
                solve();
         }
        return 0; 
}

Measured memory_bytes: 3560
#include<cstdio>


int main(void)
{
	int n, y;

	while(scanf("%d%d", &n, &y)==2)
	{
		int is_valid=0;
		int a, b, c;

		for(a=0;a<=n;a++)
		{
			int r=y-10000*a;
			if(r<0) break;
			b=(r-1000*(n-a))/4000;
			c=(y-(a*10000+b*5000))/1000;
			if(b<0 || c<0) continue;
			if(a*10000+b*5000+c*1000==y && a+b+c==n)
			{
				is_valid=1;
				break;
			}
		}

		if(!is_valid) a=b=c=-1;

		printf("%d %d %d\n", a, b, c);
	}

	return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
//#include <numeric> //iota
//#include <sstream>
//#include <cstring>
//#include <tuple>
//#include <random>
//#include <string>
//#include <unordered_set>
//#include <bitset>
//#include <unordered_map>
//#include <stack>
//#include <cmath>
//#include <deque>
//#include <queue>
//#include <list>
#include <array>
using namespace std;
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define vec vector
#define pb push_back
#define pp pop_back
#define fi first
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define se second
#define un_map unordered_map
#define rep(i,a,n) for (int i = a; i < n; ++i) 
#define inc_rep(i,a,n,inc) for (int i = a; i < n; i += inc) 
#define couts(x) cout << x << "\n" 
#define watch(x) cout << #x << " : " << x << endl;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<long long, long long>  PLII;
template<typename T>
void print(vector<T>& v) {rep(i, 0, (int)v.size()) cout << v[i] << " ";cout << endl;}
int nxt() {int x;cin >> x;return x;}
ll nxtl() {ll x;cin >> x;return x;}
string nxts() {string x;cin >> x;return x;}
double nxtd() {double x; cin >> x; return x;}

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int main() {
	IOS;
	int n, m;
	cin >> n >> m;
	int q;
	cin >> q;
	map<int,int> m1;
	map<PII,int> m3;
	map<int,int> m2;
	rep (i, 0, q) {
		int x, y;
		cin >> x >> y;
		m1[x]++;
		m2[y]++;
		m3[{x, y}] = 1;
	}
	int max_x = 0;
	for (auto x: m1) {
		max_x = max(max_x, x.se);
	}
	int max_y = 0;
	for (auto x : m2) {
		max_y = max(max_y, x.se);
	}
	int ans = max_x + max_y;
	
	vector<int> val_y;
	for (auto x : m2) {
		if (x.se == max_y) {
			val_y.push_back(x.fi);
		}
	}
	vector<int> val_x;
	for (auto x : m1) {
		if (x.se == max_x) {
			val_x.push_back(x.fi);
		}
	}
	bool ch = 1;
	for (auto x : val_x) {
		for (auto y : val_y) {
			auto it = m3.find({x,y});
			if (it == m3.end()) {
				ch = 0;
				break;
			}
		}
		if (!ch) {
			break;
		}
	}
	if (ch) {
		ans--;
	}
	cout << ans;
}










Measured memory_bytes: 54264
#include<stdio.h>
    int main(void)
    {
    	int s;
    	scanf("%d",&s);
    	if(s==111){
    		printf("3\n");
    	}
    	else if(s==110){
    		printf("2\n");
    	}
    	else if(s==101){
    		printf("2\n");
    	}
    	else if(s==11){
    		printf("2\n");
    	}
    	else if(s==100){
    		printf("1\n");
    	}
    	else if(s==10){
    		printf("1\n");
    	}
    	else if(s==1){
    		printf("1\n");
    	}
    	else if(s==0){
    		printf("0\n");
    	}
    	else if(s==11){
    		printf("2\n");
    	}
    	else if(s==1){
    		printf("1\n");
    	}
    	return 0;
    }

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

t = [0]*n
for j in range(k):
    t[0] += p[j]
for i in range(1,n-k+1):
    t[i] = t[i-1]-p[i-1]+p[i+k-1]
T = t.index(max(t))
ans = 0
for i in range(k):
    ans += (p[T+i]+1)/2
print(ans)

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

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

int main(){
  int h,w,I=0;
  cin>>h>>w;
  char a[h][w];
  pair<int,int> p[h*w];
  rep(i,h){
    rep(j,w){
      cin>>a[i][j];
      if(a[i][j]=='.'){
	p[I]=make_pair(i,j);
	I++;
	
      }
    }
  }
  map<int,int> H,W;

  rep(i,I){
    H[p[i].first]++;
   W[p[i].second]++;
  }
  rep(i,h){
    bool flag=false;
    rep(j,w){
      if(H[i]!=w&&W[j]!=h){
	cout<<a[i][j];
	flag=true;
      }
    }
    if(flag)
      cout<<endl;
  }
  //  rep(i,5)
  //cout<<"H=="<<H[i]<<"W=="<<W[i]<<endl;


 
  // rep(i,I)
  //cout<<"("<<p[i].first<<","<<p[i].second<<")"<<endl;


  return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
 
/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

int main(){
    int H, W; cin >> H >> W;
    vector<vector<char>> field(H+2, vector<char>(W+2, '.'));
    for(int i=1; i<=H; i++) for(int j=1; j<=W; j++) cin >> field[i][j];

    int dh[4] = {-1, 0, 1, 0};
    int dw[4] = {0, -1, 0, 1};
    for(int i=1; i<=H; i++){
        for(int j=1; j<=W; j++){
            if(field[i][j] == '#'){
                bool flg = false;
                for(int k=0; k<4; k++){
                    if(field[i+dh[k]][j+dw[k]] == '#') flg = true;
                }
                if(!flg){
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
    }

    cout << "Yes" << endl;

}

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

void rei(int n, int y, int three[]);
int main(void);

void rei(int n, int y, int three[]){
     int ii1, ii2, money, cnt;
     for(ii1=0; ii1<=n; ii1++){
        for(ii2=0; ii2<=n-ii1; ii2++){
            money = 10000*ii1 + 5000*ii2 +1000*(n-ii1-ii2);
            if(money == y){
                three[0] = ii1;
                three[1] = ii2;
                three[2] = n-ii1-ii2;
            }
        }
     }
}

int main(void){
    int n, y;
    scanf("%d %d", &n, &y);
    int three[3] = {-1, -1, -1};
    rei(n, y, three);
    printf("%d %d %d", three[0], three[1], three[2]);

    return 0;
}


Measured memory_bytes: 256
import sys

L, R = map(int,input().split())
tmp = 2019
mod = 2019
for i in range(L, R):
  for j in range(i+1, R+1):
    tmp = (i * j) % 2019

    if tmp < mod:
      mod = tmp
    if mod == 0:
      break
  if mod == 0:
    break

print(mod)

Measured memory_bytes: 3060
(N, K) = map(int, input().split())
a = list(map(int, input().split()))

btyarr = [0 for i in range(N*(N+1)//2)]
cnt = 0
for i in range(N):
    bty = 0
    for j in range(i, N):
        bty += a[j]
        btyarr[cnt] = bty
        cnt += 1

#print(cnt)
#print(len(btyarr))

l = len(btyarr)
s = 0
e = l
for i in range(40, -1, -1):
    b = 1 << i
    cnt = 0
    for j in range(s, e):
        if btyarr[j] & b:
            tmp = btyarr[s+cnt]
            btyarr[s+cnt] = btyarr[j]
            btyarr[j] = tmp
            cnt += 1
    if cnt >= K:
        e = s + cnt

#for i in range(len(btyarr)):
#    for j in range(30, -1, -1):
#        b = 1 << j
#        if btyarr[i] & b:
#            chb = 1
#        else:
#            chb = 0
#        print(chb, end="")
#    print("")

ans = btyarr[s]
for i in range(K):
    ans &= btyarr[i]

print(ans)


Measured memory_bytes: 26928
#include<algorithm>
#include<bitset>
#include<climits>
#include<cmath>
#include<cstdlib>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<time.h>
#include<tuple>
#include<utility>
#include<vector>

using namespace std;

int main() {

	long H, W, K; cin >> H >> W >> K;
	vector<vector<bool>> on(H, vector<bool>(W, false));
	vector<vector<long>> res(H, vector<long>(W, -1));
	vector<bool> onrow(H, false);

	for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { char x; cin >> x; on[i][j] = (x == '#'); }

	long cake = 1;
	for (int i = 0; i < H; i++) {
		bool ison = false;
		for (int j = 0; j < W; j++) if (on[i][j]) ison = true;
		onrow[i] = ison;

		if (not ison) for (int j = 0; j < W; j++) res[i][j] = -2;
		else {
			long t = 0;
			bool flag = false;
			while (not on[i][t]) { res[i][t] = cake; t++; }
			for (int j = t; j < W; j++) {
				if (on[i][j]){if (flag) cake++; else flag = true;}
				res[i][j] = cake;
			}
			cake++;
		}
	}
	//今、イチゴのある行には番号が振られていて、そうでない行には-2が与えられている。
	long s = 0;//イチゴのある最初の行
	while (not onrow[s]) s++;

	for (int i = 0; i < s; i++) for (int j = 0; j < W; j++) res[i][j] = res[s][j];

	for (int i = s; i < H; i++) for (int j = 0; j < W; j++) if (res[i][j] == -2) res[i][j] = res[i - 1][j];

	//出力
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W - 1; j++) cout << res[i][j] << " ";
		cout << res[i][W - 1] << endl;
	}
}

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

int x[100];

int main()

{
	int min=100000000,n,j,i,z=0;
	cin>> n;
	for(i=0;i<n;i++)
		cin >> x[i];
	
	for(i=0;i<100;i++)
		{
		z=0;
		for(j=0;j<n;j++)
		z=z+(x[j]-i)*(x[j]-i);
	if(z<min)
	min=z;}
	cout <<min;
	
}


Measured memory_bytes: 256
#include <iostream>
#include<math.h>
#include <limits>
#include <iomanip>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i =0; i < (int)(n); i++)

int main() {
    int A,B;
    cin >> A>>B;

    
    cout << max({ A + B,A - B,A * B }) << endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  long int n,a=0,b=0,c=0,d=0,i;
  cin>>n;
  string s;
  for(i=0;i<n;i++){
    cin>>s;
    if(s=="AC")a++;
    else if(s=="WA")b++;
    else if(s=="TLE")c++;
    else if(s=="RE")d++;
  }
  cout<<"AC x "<<a<<endl;
  cout<<"WA x "<<b<<endl;
  cout<<"TLE x "<<c<<endl;
  cout<<"RE x "<<d<<endl;
  return 0;
}

   

Measured memory_bytes: 3608
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
using ll = long long;

int main(){
  int x,y,a,b,c;
  cin>>x>>y>>a>>b>>c;
  vector<int>p(a),q(b),r(c);
  rep(i,a)cin>>p[i];
  rep(i,b)cin>>q[i];
  rep(i,c)cin>>r[i];
  sort(p.rbegin(),p.rend());
  sort(q.rbegin(),q.rend());
  vector<int>d;
  rep(i,x)d.push_back(p[i]);
  rep(i,y)d.push_back(q[i]);
  rep(i,c)d.push_back(r[i]);
  sort(d.rbegin(),d.rend());
  ll ans=0;
  rep(i,x+y)ans+=d[i];
  cout<<ans<<endl;
  return(0);
}


Measured memory_bytes: 3572
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define fi first
#define se second
#define mp make_pair
#define itrfor(itr,A) for(auto itr = A.begin(); itr !=A.end();itr++)
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long llong;
char moji[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char moji2[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char moji3[10]={'0','1','2','3','4','5','6','7','8','9'};
#define Sort(a) sort(a.begin(),a.end());
#define Reverse(a) reverse(a.begin(),a.end());
#define print(a) cout << a << endl;
#define MOD llong(1e9+7)
#define MAX int(2 * 1e5 +5)
#define debug(x)  cout << #x << " = " << (x) << endl;
#define pi acos(-1.0)
#define int llong
#define INF llong(1e17)
void myprint(int* A,int A_num){
   REP(i,A_num) cout << A[i] << " ";
   cout << endl;
}

int n;
int P[ llong(1e5 + 5)];
int A[ llong(1e5 + 5)];

signed main(){
    cin >> n;

    REP(i,n){
        cin >> P[i];
        A[P[i]] = i;
    }
    pair<int,int> migi[n],hidari[n];
    set<int> iti;
    REPR(i,n){
        if(i == 0) break;
        iti.insert(A[i]);
        auto itr = iti.find(A[i]);
        if( next(itr) != iti.end()){
            migi[i].fi = *(next(itr));
            if( next(next(itr)) != iti.end() ){
                migi[i].se = *(next(next(itr)));
            }
            else{
                migi[i].se = n;
            }
            
        }
        else{
            migi[i].fi = n;
        }

        if( itr != iti.begin() ){
            hidari[i].fi = *(prev(itr));
            if( prev(itr) != iti.begin() ){
                hidari[i].se = *(prev(prev(itr)));
            }
            else{
                hidari[i].se = -1;
            }
        }
        else{
            hidari[i].fi = -1;
        }
    }
    int ans = 0;

    FOR(i,1,n+1){
        if(migi[i].fi != n){
            ans += i * ( migi[i].se - migi[i].fi) * ( A[i] - hidari[i].fi) ;
        }
        if(hidari[i].fi != -1){
            ans += i *  (hidari[i].fi - hidari[i].se ) * (migi[i].fi - A[i]);

        }
    }
    cout << ans << endl;


}

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

  
int main() {
	string S;
  cin >> S;
  bool f = true;
  int n = S.size();
  for(int i=0;i<n;i++){
	if(i %2 == 1 && S[i] == 'R') f = false;
    else if(i%2 == 0 && S[i] == 'L') f=false;
  }
  if(f) puts("Yes");
  else puts("No");
}


Measured memory_bytes: 256
// CPP program to find the minimum number of
// characters to be replaced in string S2, such
// that S1 is a substring of S2

#include <bits/stdc++.h>
using namespace std;

// Function to find the minimum number of
// characters to be replaced in string S2, such
// that S1 is a substring of S2
int minimumChar(string S1, string S2)
{
	// Get the sizes of both strings
	int n = S1.size(), m = S2.size();

	int ans = INT_MAX;

	// Traverse the string S2
	for (int i = 0; i < m - n + 1; i++) {
		int minRemovedChar = 0;

		// From every index in S2, check the number of
		// mis-matching characters in substring of
		// length of S1
		for (int j = 0; j < n; j++) {
			if (S1[j] != S2[i + j]) {
				minRemovedChar++;
			}
		}

		// Take minimum of prev and current mis-match
		ans = min(minRemovedChar, ans);
	}

	// return answer
	return ans;
}

// Driver Code
int main()
{
	string S1, S2;
	cin >> S1 >> S2 ;

	cout << minimumChar(S2, S1);

	return 0;
}


Measured memory_bytes: 3616
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits>

using namespace std;


int main(int argc, char** argv){
    vector<int> a(5);
    for (int i=0; i<5; i++) cin >> a[i];

    int argmin = -1;
    int min = 11;

    for (int i=0; i<5; i++){
        int temp = a[i] % 10;
        if (temp == 0) temp = 10;
        if (temp < min){
            min = temp;
            argmin = i;
        }
    }

    int sum = 0;
    for (int i=0; i<5; i++){
        if (i == argmin) sum += a[i];
        else if (a[i] % 10 == 0) sum += (a[i]/10)*10;
        else sum += (a[i]/10)*10 + 10;
    }

    cout << sum << "\n";

    return 0;
}


Measured memory_bytes: 384
N = int(input())
A = list(map(int,input().split()))
# 今の株価が前の日より上がるなら売る，次の日より下がるなら買う．
max_money = 1000
kabu = 0
money = 1000
for i in range(N):
    if i == 0:
        if A[i]<A[i+1]:
            kabu = int(1000/A[i])
            money -= kabu*A[i]
        continue
    if i == N-1:
        if A[i-1]<A[i]:
            money += kabu * A[i]
            kabu = 0
            max_money = max(max_money,money)
        break

    if A[i-1]<A[i]:
        money += kabu * A[i]
        kabu = 0
        max_money = max(max_money,money)
    if A[i]<A[i+1]:
        kabu += int(money/A[i])
        money -= A[i]*int(money/A[i])

print(max_money)


Measured memory_bytes: 9236
N, MOD = int(input()), 10 ** 9 + 7
memo = [{} for i in range(N+1)]
def ok(last4):
    for i in range(4):
        t = list(last4)
        if i >= 1:
            t[i-1], t[i] = t[i], t[i-1]
        if ''.join(t).count('AGC') >= 1:
            return False
    return True
def dfs(cur, last3):
    if last3 in memo[cur]:
        return memo[cur][last3]
    if cur == N:
        return 1
    ret = 0
    for c in 'ACGT':
        if ok(last3 + c):
            ret = (ret + dfs(cur + 1, last3[1:] + c)) % MOD
    memo[cur][last3] = ret
    return ret
print(dfs(0, 'TTT'))


Measured memory_bytes: 9476
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int N, D, p, q;
	int x = 0;
	cin >> N >> D;
	for (; N != 0; --N)
	{
		
		cin >> p >> q;
		if (sqrt(pow(p,2) + pow(q,2)) <= D)
		{
			++x;
		}
		
	}
	cout << x;
}

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

int digit(ll n) {
  int i = 1;
  ll j = 10;
  while(true) {
    if(n / j == 0) return i;
    i++;
    j *= 10;
  }
}

int main () {
  ll n;
  cin >> n;
  int f = 10;
  for(int i = 1, r = sqrt(n); i <= r; i++) {
    ll d = n / i;
    if(n % i == 0) {
      f = min(f, digit(d));
    }
  }
  cout << f << endl;
  return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
int main()
{
	long long n, k, ans;
	cin >> n >> k;
	vector<long long>x(n), a(n + 1), b(n + 1), temp;
	for(int i = 0;i < n;i++)
	{
		cin>>x[i];
	}
	for(int i = 0;i < n;i++)
	{
		if (x[i] < 0) temp.push_back(x[i]);
	}
	sort(temp.rbegin(), temp.rend());
	long long cou1 = 1, cou2 = 1;
	for(int i = 0;i < n;i++)
	{
		if (x[i] > 0)
		{
			a[cou1] = x[i];
			cou1++;
		}
		if (x[i] == 0)
		{
			k--;
		}
	}
	for(int i = 0;i < temp.size();i++)
	{
		if (temp[i] < 0)
		{
			b[cou2] = -temp[i];
			cou2++;
		}
	}
	if (k == 0)
	{
		cout << 0;
		return 0;
	}
	ans = 11234567890;
	for (int i = 0; i <= min<long long>(cou1 - 1, k); ++i)
	{
		if (k != i && b[k - i] == 0)
		{
		continue;
		}
		long long res = a[i] + b[k - i];
		res += min(a[i], b[k - i]);
		ans = min(ans, res);
	}
	cout << ans ;
	return 0;
}

Measured memory_bytes: 3832
var reader = require ('readline').createInterface({
 input: process.stdin,
 output: process.stdout
});

var lines = []; 
reader.on('line', (line) => {
   lines.push(line) ;
});

reader.on ('close', () => {
  var A = lines[0]
  var S = lines[1]
  
      if (A >= 3200){
      	console.log(S);
}else if (A < 3200){
  console.log("red");
}
});

Measured memory_bytes: 7500
#include <iostream>

int main(){
  std::string s;
  int n[10];
  while(std::cin>>s){
    if(std::cin.eof())break;
    for(int i=10;i;i--){
      for(int j=0;j<i;j++){
	if(i==10){n[j]=static_cast<int>(s[j]-'0');continue;};
	n[j]=(n[j]+n[j+1])%10;
      }
    }
    std::cout<<n[0]<<std::endl;
  }
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long int
#define nl cout << endl
#define pb push_back
#define ff first
#define ss second
#define all(ar) ar.begin(), ar.end()
#define Sort(x) sort(all(x))
#define print_vector(my_vector_name) for(int i=0; i<my_vector_name.size(); i++) cout << my_vector_name[i] << " ";nl
//debugger
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#define hang while (1)

int main() {
	fast;
	int n;
	cin	>> n;
	std::vector<ll> a(n);
	map<ll,ll> m;
	ll sum=0;
	for (int i=0; i<n; i++) {
		cin >> a[i];
		m[a[i]]++;
		sum+=a[i]*1ll;
	}
	int q;
	cin >> q;
	while(q--) {
		ll b,c;
		cin >> b >> c;
		if (m.find(b)!=m.end()) {
			m[c]+=m[b];
			sum-=b*m[b]*1ll;
			sum+=c*m[b]*1ll;
			m.erase(m.find(b));
		}
		cout << sum;nl;
	}
	return 0;
}

//notes
/*
 value of INT_MAX and INT_MIN is fixed and they are
 INT_MAX = 2147483647
 INT_MIN = -2147483648
 use these format of input when number of inputs not provided
 *while(scanf("%lld",&n)!=EOF)
 *while using getline(cin,s) for taking input of a string which is 
 having white space characters just write cin>>ws; before geline(cin,s)
 to make it work properly
*/

/*
 use 2-D vector for representing the pair<int,pair<int,int> > kind of things
*/

Measured memory_bytes: 7880
#include<bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

int main() {
    while (1) {
        int m, n;
        cin >> m >> n;
        if (!m) return 0;
        vector<P> p1(m);
        vector<P> p2(n);
        int c = 105;
        for (int i = 0; i < m; ++i) {
            cin >> p1[i].first >> p1[i].second;
            p1[i].first += c;
        }
        for (int i = 0; i < n; ++i) {
            cin >> p2[i].first >> p2[i].second;
            p2[i].first += c;
        }
        vector<vector<double>> pv1(210);
        vector<vector<double>> pv2(210);
        for (int i = 0; i < m; ++i) {
            pv1[p1[i].first].push_back(p1[i].second);
            int j = (i + 1) % m;
            if (p1[i].first < p1[j].first) {
                double l = p1[j].first - p1[i].first;
                for (int k = p1[i].first + 1; k < p1[j].first; ++k)
                    pv1[k].push_back((p1[i].second * (p1[j].first - k) + p1[j].second * (k - p1[i].first)) / l);
            }
            if (p1[i].first > p1[j].first) {
                double l = p1[i].first - p1[j].first;
                for (int k = p1[i].first - 1; k > p1[j].first; --k)
                    pv1[k].push_back((p1[i].second * (k - p1[j].first) + p1[j].second * (p1[i].first - k)) / l);
            }
        }
        for (int i = 0; i < n; ++i) {
            pv2[p2[i].first].push_back(p2[i].second);
            int j = (i + 1) % n;
            if (p2[i].first < p2[j].first) {
                double l = p2[j].first - p2[i].first;
                for (int k = p2[i].first + 1; k < p2[j].first; ++k)
                    pv2[k].push_back((p2[i].second * (p2[j].first - k) + p2[j].second * (k - p2[i].first)) / l);
            }
            if (p2[i].first > p2[j].first) {
                double l = p2[i].first - p2[j].first;
                for (int k = p2[i].first - 1; k > p2[j].first; --k)
                    pv2[k].push_back((p2[i].second * (k - p2[j].first) + p2[j].second * (p2[i].first - k)) / l);
            }
        }
        vector<double> pd1(210);
        vector<double> pd2(210);
        int l1 = 300, l2 = 300, r1 = -1, r2 = -1;
        for (int i = 0; i < 210; ++i) {
            if (pv1[i].size()) {
                l1 = min(l1, i);
                r1 = max(r1, i);
                sort(pv1[i].begin(), pv1[i].end());
                pd1[i] = pv1[i][(int)pv1[i].size() - 1] - pv1[i][0];
            }
            if (pv2[i].size()) {
                l2 = min(l2, i);
                r2 = max(r2, i);
                sort(pv2[i].begin(), pv2[i].end());
                pd2[i] = pv2[i][(int)pv2[i].size() - 1] - pv2[i][0];
            }
        }
        double ans = 0;
        for (int i = max(l1, l2); i < r1 && i < r2; ++i)
            ans += pd1[i] * pd2[i] / 3 + pd1[i] * pd2[i + 1] / 6 + pd1[i + 1] * pd2[i] / 6 + pd1[i + 1] * pd2[i + 1] / 3;
        printf("%.9lf\n", ans);
    }
}



Measured memory_bytes: 3252
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<ctype.h>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<math.h>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<stdio.h>
#include<stdlib.h>
#include<streambuf>
#include<string.h>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#define MD 1000000007
using namespace std;
typedef long long LL;
LL pwr(LL a,int x){
	if (x==1) return a;
	LL tmp=pwr(a,x>>1);
	tmp*=tmp;
	tmp%=MD;
	if (x&1){
		tmp*=a;
		tmp%=MD;
	}
	return tmp;
}
LL pw[100020],dv[100020];
inline LL c(int a,int b){
	if (a>b) return -1;
	if (b-a<a) a=b-a;
	if (a==0) return 1;
	return pw[b]*dv[a]%MD*dv[b-a]%MD;
}
int a[100020];
int main(void){
	int n,db;
	set<int> s;
	scanf("%d",&n);
	for (int i=1;i<=n+1;i++){
		scanf("%d",&a[i]);
		if (s.find(a[i])!=s.end()){
			db=a[i];
		}
		else{
			s.insert(a[i]);
		}
	}
	int x=0,y=0,z=0;
	int cc=0;
	for (int i=1;i<=n+1;i++){
		if (a[i]==db){
			cc++;
		}
		else{
			if (!cc){
				x++;
			}
			if (cc==1){
				y++;
			}
			if (cc==2){
				z++;
			}
		}
	}
	pw[1]=1;
	dv[1]=1;
	for (int i=2;i<=100010;i++){
		pw[i]=pw[i-1]*i;
		pw[i]%=MD;
	}
	for (int i=2;i<=100010;i++){
		dv[i]=dv[i-1]*pwr(i,MD-2);
		dv[i]%=MD;
	}
	for (int i=1;i<=n+1;i++){
		LL tmp=c(i-1,x+z);
		LL ans=c(i,n+1)-((tmp==-1)?0:tmp);
		if (ans<0) ans+=MD;
		ans%=MD;
		printf("%lld\n",ans);
	}
	return 0;
}

Measured memory_bytes: 7808
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            Scan sc = new Scan();
            write wr = new write();

            long n = sc.intarr[0];
            string[] list = new string[n];

            long count = 0;

            for(int i= 0; i<n; i++)
            {
                char[] tmp = sc.chararr;
                Array.Sort(tmp);
                list[i] = new string(tmp);
            }
            Array.Sort(list);

            int same = 1;
            int index_cst = 0;
            int dif = 1;
            while (true)  //リストの最終要素まで
            {
                if (index_cst + dif < n && list[index_cst].Equals(list[index_cst + dif]))
                {
                    same++;
                    dif++;
                    continue;
                }
                else
                {
                    index_cst = index_cst + dif;
                    count += selffunc(same); 
                    same = 1;
                    dif = 1;

                    if (index_cst + dif > n)
                    {
                        break;
                    }


                }
            }
            //for(int i=0; i<n; i++)
            //{
            //    for(int j=i + 1; j<n; j++)
            //    {
            //        for (int m = 0; m < 10; m++)
            //        {
            //            if (list[i][m] != list[j][m])
            //            {
            //                break;
            //            }
            //            if(m==9)count++;
            //        }
            //    }
            //}

            Console.WriteLine(count);

            
        }

        static bool TwoListSame(string list1,string list2)
        {
            for(int m=0; m<10; m++)
            {
                Console.WriteLine($"{list1[m]}:{list2[m]}");
                if(list1[m] != list2[m])
                {
                    break;
                }
                if (m == 9) return true;
            }

            return false;
        }

        static long selffunc(int n)
        {
            long ans = 0;
            for(int i=n-1; i>0; i--)
            {
                ans += i;
            }
            return ans;
        }
    }

    class Scan
    {
        public string str => Console.ReadLine();

        public string[] strarr => str.Split(' ');

        public long[] longarr => strarr.Select(long.Parse).ToArray();
        public int[] intarr => strarr.Select(int.Parse).ToArray();
        public char[] chararr => str.ToArray();
    }


    //出力クラス
    class write
    {
        public void wri<Type>(Type x)
        {
            Console.WriteLine(x);
        }
    }

    class Method
    {
        static public int max(params int[] nums)
        {

            int maxx = nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
                maxx = Math.Max(maxx, nums[i]);
            }
            return maxx;
        }
    }
}


Measured memory_bytes: 25280
s = int(input())
import sys
list_s=[s]
s_now = s
for i in range (1,100000):
    if s_now%2 == 0:
        s_now = s_now/2
    else:
        s_now = 3*s_now+1
    if s_now in list_s:
        print(i+1)
        sys.exit()
    list_s.append(s_now)

Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define rREP(i,m,n) for(ll i = (m); i >= (n); i--)
#define ALL(c) (c).begin(), (c).end()
#define print(x) cout << (x) << endl;
#define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " ";} cout<<endl;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int ary[9];
    rep(i,9) cin >> ary[i];
    int n;
    cin >> n;
    int ans[n];
    rep(i,n) cin >> ans[i];
    int c[9];
    rep(i,9) c[i]=0;
    rep(i,9)
    {
        rep(j,n)
        {
            if (ary[i]==ans[j])
            {
                c[i] = 1;
            }
        }
    }

    bool flag = false;
    if (c[0]==1)
    {
        if (c[1]==1 && c[2]==1)
        {
            flag = true;
        }
        else if (c[3]==1 && c[6]==1)
        {
            flag = true;
        }
        else if (c[4]==1 && c[8]==1)
        {
            flag = true;
        }
    }
    else if (c[1]==1 && c[4]==1 && c[7]==1)
    {
        flag = true;
    }
    else if (c[2]==1 && c[5]==1 && c[8]==1)
    {
        flag = true;
    }
    else if (c[3]==1 && c[4]==1 && c[5]==1)
    {
        flag = true;
    }
    else if (c[6]==1 && c[7]==1 && c[8]==1)
    {
        flag = true;
    }
    else if (c[2]==1 && c[4]==1 && c[6]==1)
    {
        flag = true;
    }

    // rep(i,9)
    // {
    //     cout << c[i] << " ";
    // }


    if (flag)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    
}





Measured memory_bytes: 256
#include<bits/stdc++.h>
#define fori(j,n) for(ll i=j;i<n;i++)
#define forb(n,j) for(ll i=n-1;i>=0;i--)
#define endl "\n"
#define pb push_back
#define ll long long
#define mp make_pair
#define ff first
#define ss second
#define vec vector<ll>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.141592653589793238
#define mod 1000000007
using namespace std;

void wldd()
{
	ll n;
	cin >> n;
	vector<pair<int, int>> v;
	fori(0, n)
	{
		int x, y;
		cin >> x >> y;
		v.pb(mp(x, y));
	}
	ll c = 0;
	fori(0, v.size())
	{
		if (v[i].ff == v[i].ss)
		{
			c++;
			if (c == 3)
			{
				cout << "Yes" << endl;
				return ;
			}
		}
		else
		{
			c = 0;
		}
	}
	cout << "No" << endl;
}

int32_t main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	IOS;
	// ll t;
	// cin >> t;
	// while (t--)
	wldd();
	return 0;
}

Measured memory_bytes: 3652
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0]
for a in A:
  B.append((B[-1] + a) % M)

ans = 0
d = {}
for b in B:
  if b in d:
    ans += d[b]
    d[b] += 1
  else:
    d[b] = 1

print(ans)

Measured memory_bytes: 16172
/**
* Copyright(c)
* All rights reserved.
* Author : zzy
* Date : 2020-03-10-23.03.00
*/
#include <bits/stdc++.h>

#define eb emplace_back
#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, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) ((int)(x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

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

const int maxn = (int)1e4 + 5;
int n, p, mod[maxn];
string s;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif

    cin >> n >> p;
    cin >> s;
    i64 ans = 0;
    if (p == 2) {
        i64 cnt = 0;
        forn(i, n) {
            int num = int(s[i] - '0');
            ++cnt;
            if (num % 2 == 0) {
                ans += cnt;
            }
        }
        cout << ans << '\n';
    } else if (p == 5) {
        i64 cnt = 0;
        forn(i, n) {
            int num = int(s[i] - '0');
            ++cnt;
            if (num % 5 == 0) {
                ans += cnt;
            }
        }
        cout << ans << '\n';
    } else {
        ms(mod, 0);
        i64 standard = 1;
        i64 now = 0;
        ++mod[0];
        for (int i = n-1; i >= 0; --i) {
            i64 num = i64(s[i]-'0');
            now = (now + (num*standard)) % p;
            ans += mod[now];
            ++mod[now];
            standard = (standard*10)%p;
        }
        cerr << now << ' ' << standard << endl;
        cout << ans << '\n';
    }

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


Measured memory_bytes: 720
#include<iostream>
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
/*---------------------DEBUGGING--------------------------------------------*/
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
/*-------------------------------------------------------------------------------------*/
//#define mp make_pair
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pcc pair<char,char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M  1000000007
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 10000000000000
#define N 200005
#define vi vector<int>
#define all(v) v.begin(),v.end()
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

signed main()
{
   ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int h1,m1,h2,m2,k;
   cin>>h1>>m1>>h2>>m2>>k;
   int time=(h1)*60 + m1;
   int time1=(h2)*60 + m2;
   time1-=time;
   time1-=k;
   cout<<max(0LL,time1)<<endl;
}


Measured memory_bytes: 3656
n, m, x = map(int, input().split())
cost = []
for i in range(n):
    cost.append(list(map(int, input().split())))

ans = float('Inf')
for i in range(2 ** n):
    #print(ans)
    flag = True
    cnt = 0
    s = [0] * m
    for j in range(n):
        if ((i >> j) & 1):
            for k in range(m):
                s[k] += cost[j][k + 1]
            cnt += cost[j][0]
    for j in range(m):
        if s[j] < x:
            flag = False
    if flag:
        ans = min(ans, cnt)
if ans != float('Inf'):
    print(ans)
else:
    print(-1)

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

int main(){
	int n;	//参加者数
	int q;	//イベント数
	cin >> n >> q;

	int a; //参加者番号
	int v; //イベントでの獲得匹数
	int* num; //所持数

	int winner = 0;
	int max = 0;

	num = new int[n+1];
	for (int i = 0; i <= n; i++){
		num[i] = 0;
	}

	for (int i = 0; i < q; i++){
		cin >> a >> v;
		num[a] += v;

		//キャッチの場合
		if (v >= 0){
			//トップの人が獲得した場合
			if (a == winner){
				max = num[a];
			}
			//それ以外の人が獲得した場合
			else{
				if (num[a] > max){
					max = num[a];
					winner = a;
				}
				else if (num[a] == max && a < winner){
					winner = a;

				}
			}
		}
		//リリースの場合
		else if (a == winner){
			max = num[0];
			winner = 0;
			for (int i = 1; i <= n; i++){
				if (num[i] > max){
					max = num[i];
					winner = i;
				}
			}
		}
		cout << winner << " " << max << endl;
	}//各イベント終了
	
	return 0;
}

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

int main() {
	int N,S1=0,S2=0,S3=0,S4,a=10000;
	cin >> N;
	int W[100];

	for (int i = 1; i < N+1; ++i) {
		cin >> W[i];
		S2 = S2 + W[i];
	}

	for (int T = 1; T < N+1; ++T) {
		for (int j = 1; j < T+1; ++j) {
			S1 = S1 + W[j];
			
			
			}
		S4 = S2 - S1;
		//cout << S1 << S2 << endl;
		if (S1 > S4) {
			S3 = S1 - S4;
			}
		else {
			S3 = S4 - S1;
			}
		if (a > S3) {
			a = S3;
			}
		S1 = 0;
		S3 = 0;
		S4 = 0;
	}
	cout << a << endl;
	return 0;
}


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

typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< pair<int,int> > vpii;
typedef vector< pair<ll,ll> > vpll;
typedef vector< vector<int> > matrix;

#define SET(Ar,val) memset(Ar,val,sizeof(Ar))
#define rep(i,val,n)  for(int i=val;i<n;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LMAX LONG_LONG_MAX
#define LMIN LONG_LONG_MIN
#define IMAX INT_MAX
#define IMIN INT_MIN
#define M 1000000007ll
#define md 998244353

/*--------------------------------------------------------------------------------------------------------------------------------*/

template <class T> T modpower(T a,T b,T MOD)
{
    T ans=1;
    while(b!=0)
    {
        if(b%2==1)
        ans=(ans*a)%MOD;
        a=(a*a)%MOD;
        b/=2;
    }
    return ans;
}


void init()
{
    int N;
    ll x;
    vll v0;
    cin >> N;
//    int c0,cn,cp,_cp,_cn;
//    c0 = cn = cp = _cp = _cn = 0;
    rep(i,1,N+1)
    {
        cin >> x;
        x = x - i;
        v0.pb(x);
//        se1.insert(x);
//        if(x < 0)
//        {
//            cn += (x);
//            _cn++;
//        }
//        else if(x > 0)
//        {
//            cp += x;
//            _cp++;
//        }
//        else
//        {
//            c0++;
//        }
    }
    sort(v0.begin(),v0.end());
    if(N%2)
    {
        x = v0[N/2];
    }
    else
    {
        x = (v0[N/2] + v0[N/2 - 1])/2;
    }
    ll ans  = 0;
    for(int i=0;i<N;i++)
    {
        ans += abs(x-v0[i]);
    }
    cout << ans  ;
}


int main()
{
    clock_t time1 = clock();
//	#ifndef ONLINE_JUDGE
//	freopen("input1.txt","r",stdin);
//	#else
//	//ONLINE_JUDGE
//	#endif
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    init();
    clock_t time2 = clock();
    cerr <<"Time: " <<(double)(time2-time1)/CLOCKS_PER_SEC * 100000 <<" seconds" <<endl;
	return 0;
    }


Measured memory_bytes: 2420
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <limits.h>

#define lli long long int
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define	abs(a) ((a)<0?-(a):(a))

#define MOD 1000000007



int main(void){
	/* === input paramater === */
	int n;
	int *a;
	int *b;

	/* === paramter === */
	int i;
	lli *ans;
	lli res;

	/* === input === */
	scanf("%d",&n);
	a = calloc(sizeof(int),n+2);
	b = calloc(sizeof(int),n+2);
	ans = calloc(sizeof(lli),n);
	a++;
	b++;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++){
		scanf("%d",&b[i]);
	}

	/* === prepare === */
	if(a[n-1] != b[0]){
		printf("0\n");
		return 0;
	}
	for(i=0;i<n;i++){
		if(a[i-1] != a[i]){
			ans[i] = 1;
		}else{
			ans[i] = a[i];
		}
	}
	for(i=n-1;i>=0;i--){
		if(b[i] != b[i+1]){
			ans[i] = 1;
		}else{
			ans[i] = min(ans[i],b[i]);
		}
	}
	for(i=0;i<n;i++){
		if(a[i] != b[i] && a[i-1] != a[i] && b[i] != b[i+1]){
			ans[i] = 0;
		}
		if(a[i] != a[i-1] && a[i] > b[i]){
			ans[i] = 0;
		}
		if(b[i] != b[i+1] && b[i] > a[i]){
			ans[i] = 0;
		}
	}

	/* === core === */
	res = 1;
	for(i=0;i<n;i++){
		//printf("%d\n", ans[i]);
		res *= ans[i];
		res %= MOD;
	}

	/* === output === */
	printf("%lld\n", res);


	return 0;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ll long long
#define BIT_FLAG_0 (1<<0) // 0000 0000 0000 0001
#define BIT_FLAG_1 (1<<1) // 0000 0000 0000 0010
#define BIT_FLAG_2 (1<<2) // 0000 0000 0000 0100
#define BIT_FLAG_3 (1<<3) // 0000 0000 0000 1000
#define BIT_FLAG_4 (1<<4) // 0000 0000 0001 0000
#define BIT_FLAG_5 (1<<5) // 0000 0000 0010 0000
#define BIT_FLAG_6 (1<<6) // 0000 0000 0100 0000
#define BIT_FLAG_7 (1<<7) // 0000 0000 1000 0000

int main(){
    int s,w;
    cin >> s >> w;
    if(s>w) cout << "safe" << endl;
    else cout << "unsafe"  << endl;
    return 0;   
}

Measured memory_bytes: 3520
match, _ = [int(item) for item in input().split()]
kind = sorted([int(item) for item in input().split()])

use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1] * int(match + 1)

dp[0] = 0
for i in range (match + 1):
  for k in kind:
    if i < use[k]:
      continue
    dp[i] = max(dp[i], dp[i - use[k]] * 10 + k)
    
print(dp[match])

Measured memory_bytes: 14388
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
s = [STR() for _ in range(n)]
print(len(set(s)))

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

using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;

int main() {
    int total;
    while(cin >> total, total) {
        for(int i = 0; i < 9; ++i) {
            int x; cin >> x;
            total -= x;
        }
        cout << total << endl;
    }
}


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

int main() {
    string s;
    cin >> s;
    int n = s.size();
    if(n%2 != 0) cout << "No" << endl;
    else {
        rep(i,n/2) {
            if(s[i*2] != 'h' || s[i*2+1] != 'i') {
                cout << "No" << endl;
                return 0;
            }
        }
        cout << "Yes" << endl;
    }
}

Measured memory_bytes: 256
import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto rd = readln.split.to!(size_t[]), n = rd[0], k = rd[1];
  auto l = readln.split.to!(int[]);
  l.sort!"a > b";
  writeln(l[0..k].sum);
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		
		int n = scan.nextInt();
		int k = scan.nextInt();
		int x = scan.nextInt();
		int y = scan.nextInt();
		
		if(n < k){
			System.out.println(n * x);
		} else {
			System.out.println((k * x) + ((n - k) * y));
		}
	}
}


Measured memory_bytes: 9552
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define F first
#define S second
#define MEM(i,j) memset(i,j,sizeof i)
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define ET cout << "\n"
#define DB(a,s,e) {for(int i=s;i<e;i++) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int dp[100005],arr[100];

int main()
{jizz
	int n,k;
	cin >> n >> k;
	for(int i=0;i<n;++i)
		cin >> arr[i];
	dp[0]=0;
	for(int i=1;i<=k;++i)
		for(int j=0;j<n;++j)
			if(i>=arr[j]&&!dp[i-arr[j]]) dp[i]=1;
	if(dp[k]) cout << "First\n";
	else cout << "Second\n";
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> E[maxn], G[3], V[2];
int T[maxn], n, Col[maxn], chk[maxn], Res[maxn];

void dfs(int a, int pp) {
    Col[a] = !Col[pp];
    V[Col[a]].push_back(a);
    for (auto &x : E[a]) {
        if (x == pp) continue;
        dfs(x, a);
    }
}

int main() {
    int i, a, b;
    scanf("%d", &n);
    for (i = 1; i <= n; ++i) {
        G[i % 3].push_back(i);
    }
    for (i = 1; i < n; ++i) {
        scanf("%d %d", &a, &b);
        E[a].push_back(b);
        E[b].push_back(a);
    }
    dfs(1, 0);
    int pv = 0;
    if (V[pv].size() > V[!pv].size()) pv = !pv;
    if (V[pv].size() <= G[0].size()) {
        int pp = 0;
        for (auto &x : V[pv]) {
            Res[x] = G[0][pp];
            chk[G[0][pp]] = 1;
            pp++;
        }
    }else {
        int pp = 0, pp2 = 0;
        for (auto &x : V[pv]) {
            if (pp < G[1].size()) {
                Res[x] = G[1][pp];
                chk[G[1][pp]] = 1;
                pp++;
            }else {
                Res[x] = G[0][pp2];
                chk[G[0][pp2]] = 1;
                pp2++;
            }
        }
    }
    vector<int> TP;
    for (i = 1; i <= n; ++i) {
        if (!chk[i]) {
            TP.push_back(i);
        }
    }
    for (i = 0; i < V[!pv].size(); ++i) {
        Res[V[!pv][i]] = TP[i];
    }
    for (i = 1; i <= n; ++i) {
        printf("%d ", Res[i]);
    }
    return 0;
}

Measured memory_bytes: 22168