akhauriyash/Code-Regression huggingface dataset:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        map<int,bool>m;
        int ans=0;
        for(int i=1;i<=N;i++){
            int c;
            scanf("%d",&c);
            if(m[c]){
                m[c]=0;
                ans--;
            }else{
                ans++;
                m[c]=1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


Measured memory_bytes: 4992
#include <iostream>
#include <string>
#include <vector>
#define rep(i,s,N) for(int i=s;i<N;i++)
using namespace std;
int main(void)
{
	int H, W; cin >> H >> W;
	string ini(W+2,'.');
	vector<string> s(H+2,ini);
	rep(i, 1, W+1)cin >> s[i];
	bool flag = true;

	rep(i, 1, H+1) {
		rep(j, 1, W+1) {
			if (s[i][j] == '#'&&s[i - 1][j] == '.'&&s[i + 1][j] == '.'&&
				s[i][j - 1] == '.'&&s[i][j + 1] == '.') {
				flag = false;
				break;
			}
		}
	}
	cout << (flag ? "Yes" : "No") << endl;

	return 0;
}

Measured memory_bytes: 256
"""Quick Sort."""
 
def partition(A, p, r):
    """Divide list A into A[p:q] whose elements aren't greater than A[q] and
    A[q+1:r] whose elements are greater than A[q].
 
    Each element of A is a list [x, y].
    x is a character of S, H, C or D (trump suits).
    y is a natural number.
    Sorting is done based on y numbers.
     
    Default value of p is 0.
    The number as a refernce of the division is A[r].
     
    Return the index q.
    """
 
    x = A[r][1]
    i = p - 1
    for j in range(p, r):
        if A[j][1] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1
 
 
def quickSort(A, p, r):
    """Sort list A in ascending order with quick sort algorithm.
 
    Default value of p is 0 (the start index).
    r is 1 less than the length of A (the last index of A).
    """
 
    if p < r:
        q = partition(A, p, r)
        quickSort(A, p, q - 1)
        quickSort(A, q + 1, r)
 
 
def is_stable(A, B):
    """Check the stability of sorted list A.
 
    A is a sorted list.
    B is a original list.
 
    Return True or False.
    """
 
    cA = list(A)
    for x in B:
        i = cA.index(x)
        if i == 0:
            pass
        elif cA[i - 1][1] == x[1]:
            return False
        del cA[i]
    return True
 
 
 
import sys
 
r = int(sys.stdin.readline()) - 1
 
A = []
 
for x in sys.stdin.readlines():
    suit, num = x.split()
    num = int(num)
    A.append([suit, num])
 
B = list (A)
 
quickSort(A, 0, r)
 
if is_stable(A, B):
    print('Stable')
else:
    print('Not stable')
 
for x in A:
    print(*x)

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

int main(){
  ll H,W;
  cin >> H >> W;
  vector<vector<ll>> A(H,vector<ll>(W)),B(H,vector<ll>(W));
  vector<vector<vector<ll>>> dp(H,vector<vector<ll>>(W,vector<ll>(8001,0)));
  rep(i,H)rep(j,W) cin >> A.at(i).at(j);
  rep(i,H)rep(j,W) cin >> B.at(i).at(j);
  dp.at(0).at(0).at(abs(A.at(0).at(0)-B.at(0).at(0)))=1;
  rep(i,H){
    rep(j,W){
      ll t=abs(A.at(i).at(j)-B.at(i).at(j));
      rep(k,8001){
        if(i!=0){
          if(dp.at(i-1).at(j).at(k)==1){
            if(t+k<8001){
              dp.at(i).at(j).at(t+k)=1;
            }
            dp.at(i).at(j).at(abs(t-k))=1;
          }
        }
        if(j!=0){
          if(dp.at(i).at(j-1).at(k)==1){
            if(t+k<8001){
              dp.at(i).at(j).at(t+k)=1;
            }
            dp.at(i).at(j).at(abs(t-k))=1;
          }
        }
      }
    }
  }
  rep(k,8001){
    if(dp.at(H-1).at(W-1).at(k)==1){
      cout << k << endl;
      return 0;
    }
  }
}


Measured memory_bytes: 405632
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
  
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
  
using namespace std;
  
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
  
const int INF=1<<29;
const double EPS=1e-9;
  
const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};


int main() {
  int N;
  cin >> N;
  vector<ll> A(N), B(N), C(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  sort(all(A));
  sort(all(B));
  sort(all(C));

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll b = B[i];
    ll k1 = lower_bound(all(A), b) - A.begin() - 1;
    ll k2 = lower_bound(all(C), b + 1) - C.begin();
    if (k1 < 0 || k2 >= N) continue;
    ans += (k1 + 1) * (N - k2);
  }

  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 2560
N, T = map(int, input().split()) # 1. スペース区切りの入力を受け取る
# 2. 上記の入力に伴ってデータがくるからそれを集めるために配列を宣言
c = []
t = []
for i in range(N): # 3.loop 変数iを用意し、0からN-1までの範囲でループが回る
    tmp = [0, 0] # 4. tempというリストよ用意する
    tmp[0], tmp[1] = map(int, input().split()) # 5. 入力をうけとる
    # 6. tmpリストに入っているデータを0,1の順でc,tに代入
    c.append(tmp[0])
    t.append(tmp[1])
index = -1 # 7. 番兵法を使う
cost = 1000 # 8 costの上限を代入しておく
for i in range(N): # 9.loop. 再びN-1までの範囲でループを回す
    if t[i] <= T and c[i] <= cost: # 10. 最小経路を探す
        index = i
        cost = c[i]
if index == -1: # indexが書き換わっていなければ、エラー
    print('TLE')
else:
    print(cost)

Measured memory_bytes: 3064
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=25,M=(1<<15)+5;
double mn[M];int x[N],y[N],a[N],st[N],top,n,lim;
struct eg{
	int u,v;double w;
	inline bool operator <(const eg &b)const{return w<b.w;}
}E[N*N];int fa[N],tot;
inline int find(R int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline double calc(R int i,R int j){
	return sqrt(1ll*(x[i]-x[j])*(x[i]-x[j])+1ll*(y[i]-y[j])*(y[i]-y[j]));
}
void solve(R int s){
	top=tot=0;fp(i,0,n-1)if(s>>i&1)st[++top]=i,mn[s]+=a[i];
//	if(!top)printf("%d %d\n",s,top);
	fp(i,1,top)fa[i]=i;
	fp(i,1,top)fp(j,i+1,top)E[++tot]={i,j,calc(st[i],st[j])};
	sort(E+1,E+1+tot);
	for(R int i=1,u,v;i<=tot;++i){
		u=find(E[i].u),v=find(E[i].v);
		if(u!=v)mn[s]-=E[i].w,fa[u]=v;
	}
	mn[s]/=top;
}
int main(){
	scanf("%d",&n),lim=(1<<n);
	fp(i,0,n-1)scanf("%d%d%d",&x[i],&y[i],&a[i]);
	fp(i,1,lim-1)solve(i);
	mn[0]=1e18;
	fp(s,1,lim-1)for(R int t=(s-1)&s;t;t=(t-1)&s)cmax(mn[s],min(mn[t],mn[s^t]));
	printf("%.10lf\n",mn[lim-1]);
	return 0;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int main(){
    int year;
    cin >> year;
    if(year < 2) { 
        cout << "Hello World\n";
        }else{
        int a,b;
        cin >> a >> b;
        cout << a+b << "\n";
    }
    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

ll memo[5050][5050];
bool done[5050][5050];

ll solve(int n, int k){
	if(done[n][k]){
		return memo[n][k];
	}
	done[n][k] = true;
	if(n<k){
		return memo[n][k] = 0ll;
	}else if(n==k){
		return memo[n][k] = 1ll;
	}
	add_mod(memo[n][k],solve(n-1,max(k-1,0)));
	add_mod(memo[n][k],(solve(n-1,k+1)*2)%MOD); 
	return memo[n][k];
}

int main(void){
	int n;
	string s;
	cin >> n;
	cin >> s;
	cout << solve(n,s.l_ength()) << endl;
	return 0;
}


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

#define jaldi ios_base::sync_with_stdio(false)
#define fr(i,j,n) for(i=j;i<n;i++)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%ld",&x)
#define sll(x) scanf("%lld",&x)
#define sc(x) scanf("%c",&x)
#define ss(x) scanf("%s",x)
#define pi(x) printf("%d",x)
#define pl(x) printf("%ld",x)
#define pll(x) printf("%lld",x)
#define pc(x) printf("%c",x)
#define ps(x) printf("%s",x)
#define vi vector<int> 
#define vl vector<long> 
#define vll vector<long long>
#define vc vector<char>  
#define vs vector<string>
#define vvi vector<vector<int>> 
#define vvll vector<vector<long long>> 
#define F first
#define S second
#define ll long long
#define pb push_back
#define ppii pair<int,int> 
#define ppll pair<long long,long long>
#define ppcc pair<char,char>
#define ppss pair<string,string> 
#define vpll vector<pair<long long,long long>>
#define PI 3.1415926535897932384626
#define precision(x) cout<<fixed<<setprecision(x) 
#define mod 1000000007

int x_d[8] = {-1,1,0,0,-1,-1,1,1};
int y_d[8] = {0,0,-1,1,-1,1,-1,1};

ll power(ll b,ll e)
{
    if(b==0)
        return 0;
    
    ll r=1;
    while(e>0)
    {
        if(e%2)
            r=(r*b)%mod;
        b=(b*b)%mod;
        e/=2;
    }
    return r;
}

int main()
{
    jaldi;
    precision(20);
    ll i,j;

    ll x;
    cin>>x;
    ll ans=100,y=0;
    double w=1.01;
    while(ans<x)
    {
        ans=ans*1.01;
        y++;
        // cout<<ans<<" ";
    }
    // if(ansx)
    cout<<y;


    return 0;
}

Measured memory_bytes: 3652
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;
#define sim template < class c
#define int long long
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << '\n'; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
typedef tree< pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
const ll MOD = 1e9 + 7 ;
const ll N = 2e5 + 10 ;
const ll INF = 1e18 + 10 ;
signed main(){
    fastio
    int n , k ;
    cin >> n >> k ;
    vector<int> v(n) ;
    for(int i = 0; i < n; i++){
        cin >> v[i] ;
    }
    for(int i = k; i < n; i++){
        if ( v[i] > v[i - k] ){
            cout << "Yes" << endl;
        }
        else cout << "No" << endl;
    }
    return 0; 
}


Measured memory_bytes: 4796
#include<bits/stdc++.h>
#define us using namespace std;
us
#define MAXN [10001400]
#define ll long long
#define cc cin>>
#define st strlen
#define bo bool
#define ch char
#define out cout<<
#define rrr return 0;
#define iC(x) if((x)=='C')flagC=true;
#define iF(x) if((x)=='F'&&flagC)flagF=true;
#define _if(a,b) if((a)&&(b))cout<<"Yes";else cout<<"No";
#define _for(i,a,b); for(int i=(a);i<=(b);i++)
#define IM int main()
ch as MAXN;
bo flagC,flagF; 
IM
{
	cc as
	_for(i,0,strlen(as))
	{
		iC(as[i])
		iF(as[i])
	}
	_if(flagC,flagF)
	rrr
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <cmath>   
#include<cctype>
#include<string>
#include<set>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include <deque>
#include <climits>
#include <typeinfo>
#include <utility> 
#define all(x) (x).begin(),(x).end()
#define rep(i,m,n) for(int i = m;i < n;++i)
using namespace std;
using ll = long long;
using R = double;
const ll inf = 1LL << 50;
const ll MOD = 1e9 + 7;
struct edge { ll from; ll to; ll cost; };

#define INF INT_MAX/2
int n, a[202020];

int main(){
	cin >> n;
	rep(i, 0, n)cin >> a[i];

	ll res = 0;
	int right = 0;
	ll sum = 0;
	rep(left, 0, n) {
		while (right < n && (sum^a[right]) == (sum + a[right])) {
			sum += a[right];
			right++;
		}
		res += right - left;
		if (left == right)right++;
		else {
			sum -= a[left];
		}
	}


	cout << res << endl;
	return 0;
}

Measured memory_bytes: 1024
def isKaibun(str):
    isKaibun = True
    length = len(str)
    for i in range(length):
        if str[i] != str[length - i - 1]:
            isKaibun = False
            break
    return isKaibun

# 整数の入力
S = input()

N = len(S)

output = "No"

flag1 = isKaibun(S)
flag2 = isKaibun(S[0 : int(((N-1)/2))])
flag3 = isKaibun(S[int(((N+3)/2)-1): N])

if flag1 and flag2 and flag3:
    output = "Yes"

###########　出力
print(output)



Measured memory_bytes: 3064
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int N;

const bool IsConsistent(const vector<vector<int>> &states, const int bit)
{
    for (int i = 0; i < N; ++i)
    {
        // iが正直者のとき
        if (bit & (1 << i))
        {
            for (int j = 0; j < N; ++j)
            {
                if (j != i && states[i][j] != -1)
                {
                    // jが正直者なのに「不親切」評価，もしくはjが不親切なのに「正直」評価だと矛盾
                    bool is_j_honest = (bit & (1 << j)) != 0;
                    bool honestState = (states[i][j] == 1);
                    if (is_j_honest != honestState) return false;
                }
            }
        }
    }
    return true;
}

int main()
{
    cin >> N;
    vector<vector<int>> states(N, vector<int>(N, -1));
    for (int i = 0; i < N; ++i)
    {
        int Ai; cin >> Ai;
        for (int j = 0; j < Ai; ++j)
        {
            int xij, yij; cin >> xij >> yij;
            states[i][xij - 1] = yij;
        }
    }
    // bit全探索
    int ans = 0;
    for (int bit = 0, n = (1 << N); bit < n; ++bit)
    {
        // 設定に矛盾がなければ，正直者の数を数える
        if (IsConsistent(states, bit))
        {
            int honest = 0;
            for (int i = 0; i < N; ++i)
            {
                if (bit & (1 << i)) ++honest;
            }
            ans = max(ans, honest);
        }
    }
    cout << ans << endl;
}


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

using lli = int64_t;

using vi = vector<lli>;
using vvi = vector<vi>;

using vm = vector< map<lli, lli> >;

int main()
{
    lli n, w; cin >> n >> w;

    vvi v(n, vi(2));

    for(int i = 0; i < n; i++)
        cin >> v[i][0] >> v[i][1];

    vm dp(n + 1);

    dp[0][0] = 0;

    for(int i = 0; i < n; i++)
    {
        for(auto& pr : dp[i])
        {
            auto& x = dp[i + 1][pr.first];
            auto& y = dp[i + 1][pr.first + v[i][0]];

            x = max(x, dp[i][pr.first]);
            y = max(y, dp[i][pr.first] + v[i][1]);
        }
    }

    lli ans = 0;

    for(auto& pr : dp[n]) if(pr.first <= w)
        ans = max(ans, pr.second);

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 13952
#include <stdio.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <map>
#include <algorithm>
#include <vector>
#include <math.h>
#define MOD 1000000007
using namespace std;

int main(void){
    int n,sum=0;
    cin >> n;
    vector<int> a(n);
    for(int i=0; i<n; i++)cin >> a[i];
    sort(a.begin(),a.end());
    for(int i=0; i<n-1; i++){
        cerr << a[i] << endl;
        sum+=a[i];
    }
    cout << (sum>a[n-1]?"Yes":"No") << endl;
    return 0;
}


Measured memory_bytes: 256
N = int(input())
A = [int(_) for _ in input().split()]

s = sum(A)

x = s - sum([2 * A[i] for i in range(1, N - 1, 2)])
print(x, end='')

for a in A[:-1]:
    x = 2 * a - x
    print('', x, end='')
print()

Measured memory_bytes: 14024
#include <iostream>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

const int mod = 1e9 + 7;

using ll = long long;

int n, m;
int x[100000];
int y[100000];

int add(int a, int b){
  return (a + b) % mod;
}
int mul(int a, int b){
  return ll(a) * b % mod;
}

int solve(int n, int* x){
  --n;
  rep(i, n){
    x[i] = x[i + 1] - x[i];
  }
  int p = n / 2;
  int ans = 0;
  rep(i, p){
    int c = add(mul(i + 1, n - 2 * i), i * ll(i + 1) % mod);
    ans = add(mul(x[i], c), ans);
  }
  rep(i, n - p){
    int c = add(mul(i + 1, n - 2 * i), i * ll(i + 1) % mod);
    ans = add(mul(x[n - i - 1], c), ans);
  }
  return ans;
}

int main(){
  cin >> n >> m;
  rep(i, n){
    cin >> x[i];
  }
  rep(i, m){
    cin >> y[i];
  }
  cout << mul(solve(n, x), solve(m, y)) << endl;
  return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <string.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,a,b) for(int i=a;i<=b;i++)
#define INF 2147483647
#define LLINF 9223372036854775807
using namespace std;
int main(){
	string s,t;
	cin>>s>>t;
	bool flag=0; 
	for(int i=0;i<s.size();i++){
		if(s[i]!=t[i])flag=1;
	}
	if(flag)cout<<"No"<<endl;
	else cout<<"Yes"<<endl;
	return 0;
}

Measured memory_bytes: 3588
a, b, h, m = map(int, input().split())
h_angle = h * 30 + m /2
m_angle = m * 6
angle = h_angle - m_angle
import math
angle_cos = math.cos(math.radians(angle))
c_2 = a ** 2 + b ** 2 - 2 * a * b * angle_cos
c = math.sqrt(c_2)
print(c)

Measured memory_bytes: 9500
a=input()
l=len(a)
ans=0

for i in range(2**(l-1)):
    i=("0"*l+bin(i)[2:])[-l+1:]
    cnt=a[0]
    for j in range(l-1):
        if i[j]=="0":
            cnt+=a[j+1]
        else:
            ans+=int(cnt)
            if j<l-1:
                cnt=a[j+1]
            else:
                cnt=""
    if cnt!="":
        ans+=int(cnt)
print(ans)

Measured memory_bytes: 3188
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;

int main() {
    int n,d;
    scanf("%d%d",&n,&d);
    int ans = (n - 1)  / (2 * d + 1) + 1;
    printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int d[101][100001];
int main() {
    int N, W;
    cin >> N >> W;
    int w[N], v[N];
    for (int i = 0; i < N; i++) {
        cin >> w[i] >> v[i];
    }
    int l = 100001;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j < l; j++) {
            d[i][j] = INT32_MAX;
        }
    }
    d[0][0] = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < l; j++) {
            if (d[i][j] == INT32_MAX) continue;
            d[i + 1][j] = min(d[i + 1][j], d[i][j]);
            if (d[i][j] + w[i] > W) continue;
            d[i + 1][j + v[i]] = min(d[i + 1][j + v[i]], d[i][j] + w[i]);
        }
    }
    int ans = 0;
    for (int i = 0; i < l; i++) {
        if (d[N][i] <= W) {
            ans = i;
        }
    }
    cout << ans << "\n";

    return 0;
}

Measured memory_bytes: 39680
#include<bits/stdc++.h>
using namespace std;
int main(){
	#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    // string s;
    // cin>>s;
   	// int flag=1;
    // for(int i=0;i<s.size();++i){
    // 	for(int j=i+1;j<s.size();++j){
    // 		if(s[i]==s[j]){
    // 			flag=0;
    // 			break;
    // 		}
    // 	}
    // }
    // cout<<((flag)?"yes":"no");



    // string s;
    // cin>>s;
    // set<char>c;
    // for(char k:s) c.insert(k);
    // cout<<((s.size()==c.size())?"yes":"no");
    


    // string s;
    // cin>>s;
    // map<char,int>m;
    // for(int i=0;i<s.size();++i){
    // 	if(m[s[i]]>0){
    // 		cout<<"no";
    // 		return 0;
    // 	}
    // 	++m[s[i]];
    // }
    // cout<<"yes";

    int a,b;
    cin>>a>>b;
    int ans=a+b;
    (ans>=10)?cout<<"error":cout<<ans;
    return 0;
}


Measured memory_bytes: 3628
n,m=map(int,input().split())

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

N=[0,2,5,5,4,5,6,3,7,6]
X=[]
dp=[0]+[-1]*n*9

for i in range(n):
    for j in A:
        if dp[i]>=0:
            dp[i+N[j]]=max(dp[i+N[j]],dp[i]*10+j)

print(dp[n])

Measured memory_bytes: 15268
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <vector>
#include <functional>

template <class Weight>
struct Edge {
  size_t src, dst;
  Weight cost;
  Edge(size_t src, size_t dst, Weight cost=1):
    src(src), dst(dst), cost(cost)
  {}
};

template <class Weight>
using Graph=std::vector<std::vector<Edge<Weight>>>;

template <class Weight>
constexpr Weight inf=(Weight(1)<<(8*sizeof (Weight)-3));

template <class Weight>
size_t bipartite_match(const Graph<Weight> &g, size_t mid) { 
  std::vector<size_t> pair(g.size(), -1);
  std::function<bool (size_t, std::vector<bool> &)> augment;
  augment = [&](size_t u, std::vector<bool> &visited) {
    if (u+1 == 0) return true;

    for (const Edge<Weight> &e: g[u]) {
      if (visited[e.dst]) continue;
      visited[e.dst] = true;
      if (augment(pair[e.dst], visited)) {
        pair[e.src] = e.dst;
        pair[e.dst] = e.src;
        return true;
      }
    }
    return false;
  };

  size_t match=0;
  for (size_t i=0; i<mid; ++i) {
    std::vector<bool> visited(g.size());
    if (augment(i, visited))
      ++match;
  }
  return match;
}

int main() {
  size_t N;
  scanf("%zu", &N);

  std::vector<int> a(N), b(N);
  for (size_t i=0; i<N; ++i)
    scanf("%d %d", &a[i], &b[i]);

  std::vector<int> c(N), d(N);
  for (size_t i=0; i<N; ++i)
    scanf("%d %d", &c[i], &d[i]);

  Graph<int> g(2*N);
  for (size_t i=0; i<N; ++i)
    for (size_t j=0; j<N; ++j)
      if (a[i] < c[j] && b[i] < d[j]) {
        g[i].emplace_back(i, N+j);
        g[N+j].emplace_back(N+j, i);
      }

  printf("%zu\n", bipartite_match(g, N));
}


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

int main(){
	int n,m;
	cin >> n >> m;
	int sdt[n][2],pt[m][2];
	for(int i=0;i<n;++i)
	cin >> sdt[i][0] >> sdt[i][1];
	for(int i=0;i<m;++i)
	cin >> pt[i][0] >> pt[i][1];
	
	for(int i=0;i<n;++i){
		int min=abs(sdt[i][0]-pt[0][0])+abs(sdt[i][1]-pt[0][1]),idx=1,d;
		for(int j=1;j<m;++j){
			d=abs(sdt[i][0]-pt[j][0])+abs(sdt[i][1]-pt[j][1]);
			if(d<min){
				min=d;
				idx=j+1;
			}
		}
		cout << idx << endl;
	}
	return 0;
}

Measured memory_bytes: 256
///
// File:  a.go
// Author: ymiyamoto
//
// Created on Wed Apr 25 08:30:38 2018
//
package main

import (
	"fmt"
)

var s string

func main() {
	fmt.Scan(&s)
	for i := range s {
		fmt.Print(string(s[i]))
		if i == 3 {
			fmt.Print(" ")
		}
	}
	fmt.Println()
}


Measured memory_bytes: 512
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools
import heapq
#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n,k=map(int,input().split())
    a =list(map(int,input().split()))
    town=[-1 for _ in range(n)]
    town[0]=0
    now=0
    for i in range(n):
        if town[a[now]-1]==-1:
            town[a[now]-1]=i+1
            now=a[now]-1
        else:
            sec=a[now]-1
            break
#        if i==n-1:
  #          sec=a[now]-1
    town2 = [-1 for _ in range(n)]
    town2[sec] = 0
    now = sec
    for i in range(n):
        if town2[a[now] - 1] == -1:
            town2[a[now] - 1] = i + 1
            now = a[now] - 1
        else:
            thi = a[now] - 1
            break
#    print(town)
#    print(town2)
    firstloop=max(town)+1
    secondloop=max(town2)+1
    ans=0
    if k<firstloop:
        ans=town.index(k)+1
    else:
        k-=firstloop
        kai=k//secondloop
        k-=kai*secondloop
        ans=town2.index(k)+1
    print(ans)

if __name__ == "__main__":

    main()

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

int pow(int x, int n) {
  int ans = 1;
  for(int i = 0; i < n; i++) {
    ans *= x;
  }
  return ans;
}

int charToInt(char c) {
  return (int)c - (int)'0';
}

int strToInt(string str) {
  int ans = 0;
  for(int i = 0; i < str.length(); i++) {
    ans += charToInt(str[i]) * pow(10, str.length()-i-1);
  }
  return ans;
}

int to_m(string x) {
  return strToInt(x.substr(0,2)) * 60 + strToInt(x.substr(2,2));
}

int main() {
  while(1) {
    int n;
    string p, q;
    cin >> n >> p >> q;
    if(n == 0 && p == "0" && q == "0") break;

    int data[n][to_m("2500")];

    fill(&data[0][0], &data[n-1][to_m("2500")-1] + 1, 0);
    for(int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for(int j = 0; j < k; j++) {
	string a, b;
	cin >> a >> b;
	for(int l = to_m(a); l < to_m(b); l++) {
	  data[i][l] = 1;
	}
      }
    }
    int data2[to_m("2500")];
    for(int i = to_m(p); i < to_m(q); i++) {
      for(int j = 0; j < n; j++) {
	if(data[j][i] == 0) {
	  data2[i] = 0;
	  break;
	}
	if(j+1 == n) data2[i] = 1;
      }
    }

    int s = to_m(p);
    int max = -1;
    for(int i = to_m(p); i <= to_m(q); i++) {
      if(s == -1 && data2[i] == 0) {
	s = i;
      }
      if(s != -1 && (i  == to_m(q) || data2[i] == 1)) {
	int newValue = i - s;
	if(newValue > max) max = newValue;
	s = -1;
      }
    }
    cout << max << endl;
  }
  return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <cstdio>
using namespace std;
int abs(int a){
	return (a<0)?-a:a;
}
int main(){
	int n;
	cin>>n;
	while(n--){
		int h,m,hp,mp;
		scanf("%02d:%02d",&h,&m);
		hp=h*60+m;
		mp=m*12;
		if(abs(mp-hp)%(60*12)*360<(60*12)*30) cout<<"alert\n";
		else if(abs(mp-hp)%(60*12)*360>=(60*12)*90&&abs(mp-hp)%(60*12)*360<=(60*12)*270) cout<<"safe\n";
		else cout<<"warning\n";
	}
	return 0;
}

Measured memory_bytes: 0
#include <vector>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <limits>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <map>
#include <unordered_map>
#include <ctime>
#include <cassert>
#include <cstring>
#include <sys/time.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[]){
  int n, k;
  cin >> n >> k;
  vector<int> s;
  long long int sum = 0;
  s.push_back(0);
  for(int i=0;i<n;i++){
    int p;
    cin >> p;
    sum += p+1;
    s.push_back(sum);
  }
  int max = 0.0;
  for(int i=0;i<n-k+1;i++){
    long long int c = s[i+k]-s[i];
    if(c>max){
      max = c;
    }
  }
  cout << fixed << setprecision(10) << (double)max/2.0 << endl;
  return 0;
}


Measured memory_bytes: 1400
x = int(input())

#引数nが素数かどうかを判定
def is_prime(n):
    for i in range(2, n + 1):
        if i * i > n:
            break
        if n % i == 0:
            return False
    return n != 1

ans = x

for i in range(x,10**10):
    if is_prime(i) == True:
        break
    else:
        ans += 1
print(ans)

Measured memory_bytes: 3060
using System;
using System.Collections.Generic;
using System.Linq;


namespace ConsoleApp15
{
    class Class326
    {
        static void Main()
        {
            var XY = Console.ReadLine().Split().Select(long.Parse).ToArray();
            long X = XY[0];
            long Y = XY[1];

            int count = 0;
            while (X <= Y)
            {
                count++;
                X *= 2;
            }
            Console.WriteLine(count);
        }
    }
}


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

int bubble_sort(int arr[], int n)
{
    int num=0, index=0;
    bool flag = true;

    while (flag) {
        flag = false;
        for (int j=n-1; j>=index+1; --j) {
            if (arr[j-1] > arr[j]) {
                swap(arr[j-1], arr[j]);
                num++;
                flag = true;
            }
        }
        index++;
    }
    return num;
}

int main()
{
    int n, arr[110], result;
    cin >> n;
    for (int i=0; i<n; ++i) {
        cin >> arr[i];
    }
    
    result = bubble_sort(arr, n);
    
    for (int i=0; i<n; ++i) {
        if (i) cout << " ";
        cout << arr[i];
    }
    cout << endl;
    cout << result << endl;
    return 0;
}



Measured memory_bytes: 3112
using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;

namespace AtCoder
{
    public class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static public (T1 a, T2 b) Slice<T1, T2>()
        {
            var t = ReadLine().Split();
            return (
            (T1)Convert.ChangeType(t[0], typeof(T1)),
            (T2)Convert.ChangeType(t[1], typeof(T2)));
        }
        static long Mod = (long)1e9 + 7;
        public static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            //Out.Flush();
            Read();
        }
        static void Solve()
        {
            var dct = new Dictionary<char, string>();
            dct['a'] = ReadLine();
            dct['b'] = ReadLine();
            dct['c'] = ReadLine();
            var n = 'a';
            while(true)
            {
                if(string.IsNullOrEmpty(dct[n]))
                {
                    WriteLine((char)(n + 'A' - 'a'));
                    return;
                }
                var nn = dct[n][0];
                dct[n] = dct[n].Remove(0, 1);
                n = nn;
            }
        }
    }
}

Measured memory_bytes: 26184
from math import factorial
import sys

sys.setrecursionlimit(2000)
mod = 10**9+7
n, k = map(int, input().split())

memo = {}
def combination(n,k):
  if n < k:
    return 0
  return factorial(n)//factorial(k)//factorial(n-k)
  if n / 2 < k:
    k = n - k
  if n in memo and k in memo[n]:
    return memo[n][k]
  v = 0
  if k == 0:
    v = 1
  elif k == 1:
    v = n
  else:
    v = combination(n-1,k) + combination(n-1,k-1)
  return memo.setdefault(n, {}).setdefault(k, v)

for i in range(1, k + 1):
  print(combination(n-k+1,i)*combination(k-1,i-1)%mod)


Measured memory_bytes: 3316
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define INF 1e9
#define MAXN 100005
#define MAXM 100005
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define pii pair<int, int>

ll ans;
int N, M;

void solve(){
    cout << N*(N-1)/2 + M*(M-1)/2 << endl;
}

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

    solve();
}


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

#define o(output) cout << output << endl

using namespace std;

string s;
int a,b,c,d;
int l[13]={0,1,3,1,2,1,2,1,1,2,1,2,1};
char A,B;
priority_queue<int,vector<int>,greater<int> >small;

int main(){
  cin >> a >> b;
  if(l[a]==l[b])o("Yes");
  else o("No");
}

Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<set>
#include<algorithm>
#include<cstring>

using namespace std;
char s[101];
char t[101];
bool cmp(char a,char b){
return a>b;}
/*bool judge(){

char *p1=s;
char *p2=t;
while()

}*/


int main(){
    while(cin>>s>>t){
         int a=strlen(s);
         int b=strlen(t);
         sort(s,s+a);
         sort(t,t+b,cmp);
         if(strcmp(s,t)<0){
            printf("Yes\n");
         }
         else {
            printf("No\n");
         }

    }




return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(true){
            int a = sc.nextInt();
            String op = sc.next();
            int b = sc.nextInt();
            int ans = 0;
            if(op.equals("?")){
                break;
            }if(op.equals("+")){
                ans = a+b;
            }if(op.equals("-")){
                ans = a-b;
            }if(op.equals("*")){
                ans = a*b;
            }if(op.equals("/")){
            	ans = a/b;
            }
            System.out.println(ans);
        }
        sc.close();
    }
}


Measured memory_bytes: 26316
package main

import (
	"fmt"
)

func main() {
	a, b, c := SingleInt(), SingleInt(), SingleInt()
	if a < c && b > c {
		fmt.Println("Yes")
		return
	}
	if a > c && b < c {
		fmt.Println("Yes")
		return
	}

	fmt.Println("No")
}

func SingleInt() int {
	var n int
	fmt.Scan(&n)
	return n
}


Measured memory_bytes: 512
#include <stdio.h>

int main(void) {
  int a, b, c, i;
  scanf ("%d %d %d", &a, &b, &c);
  for ( i = 0 ; a <= b ; a++ ){
    if ( c % a == 0 ) i++ ;
  }
  printf ("%d\n", i);
  return 0;
}



Measured memory_bytes: 2124
from heapq import heappush, heappop
from collections import defaultdict
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]

cand = defaultdict(list)
for a, b in X:
    cand[a].append(b)

pq = []
ans = 0
for i in range(1, M + 1):
    for c in cand[i]:
        heappush(pq, -c)
    if pq:
        ans -= heappop(pq)
    
print(ans)




Measured memory_bytes: 92632
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

s = ins()
n = len(s)
s = '0'+s
if s[1]=='0' or s[n]=='1':
    print(-1)
    exit()
for i in range(1,n):
    if s[i]!=s[n-i]:
        print(-1)
        exit()
e = []
p = 1
for i in range(2,n+1):
    if s[i]=='1' or i==n:
        for j in range(p,i):
            e.append((j,i))
        p = i
for z in e:
    print("{} {}".format(z[0],z[1]))


Measured memory_bytes: 48928
import java.util.*;
public class Main 
{
	public static void main(String[] args) 
	{
		Scanner stdIn = new Scanner(System.in);
		while(stdIn.hasNext())
		{
			int n = stdIn.nextInt();
			if(n == 0)
			{
				break;
			}
			int price = 0;
			while(n-- != 0)
			{
				int x = stdIn.nextInt();
				int y = stdIn.nextInt();
				int h = stdIn.nextInt();
				int w = stdIn.nextInt();
				int size = x + y + h;
				if(size <= 60 && w <= 2)
				{
					price += 600;
				}
				else if(size <= 80 && w <= 5)
				{
					price += 800;
				}
				else if(size <= 100 && w <= 10)
				{
					price += 1000;
				}
				else if(size <= 120 && w <= 15)
				{
					price += 1200;
				}
				else if(size <= 140 && w <= 20)
				{
					price += 1400;
				}
				else if(size <= 160 && w <= 25)
				{
					price += 1600;
				}
				else
				{
					continue;
				}
			}
			System.out.println(price);
		}
	}
}

Measured memory_bytes: 12000
import sys

def input():
  return sys.stdin.readline()


N,K = map(int,input().split())
A=list(map(int,input().split()))
P=[True]*N
P[0]=False
i,a=1,0
while 1:
  a+=1
  if P[A[i-1]-1]:
    P[A[i-1]-1]=False
    i=A[i-1]
  else:
    i=A[i-1]
    break
if a>=K:
  i=1
  for _ in range(K):
    i=A[i-1]
  print(i)
else:
  K-=a
  a,j=0,i
  while 1:
    a+=1
    if A[j-1]==i:
      break
    j=A[j-1]
  K%=a
  for _ in range(K):
    i=A[i-1]
  print(i)

Measured memory_bytes: 100084
#include<iostream>
using namespace std;
int main(){
    int x;
    cin >> x;
    for(int i = 1; i <= x; i++){
        if (i % 3 == 0) cout << " " << i;
        else{
            int j = i;
            while(j > 0){
                if(j % 10 == 3){
                    cout << " " << i;
                    break;
                } else {
                    j /= 10;
                }
            }
        }
    }
    cout << endl;
    return 0;
}



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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}

ll a, b, n;

void read() {
    cin >> a >> b >> n;
}

ll calc(ll x) {
    return a * x / b - a * (x / b);
}

ll ans;

void run() {
    ll l = 1;
    ll r = n / b;
    chkmax(l, r - 1e6);
    ans = -1e18;

    for (ll i = l; i <= r; i++) {
        chkmax(ans, calc(i * b - 1));
    }

    l = 1;
    r = n;
    chkmax(l, n - 1e6);
    for (ll i = l; i <= r; i++) {
        chkmax(ans, calc(i));
    }
}

void write() {
    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    read();
    run();
    write();
    return 0;
}

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

int main() {
    int n, w, c = 0;
    cin >> n;
    vector<int> v(n);
    rep(i, n) {
        cin >> w;
        v.at(i) = w;
    }
    w = 1;
    rep(i, n) {
        c++;
        w = v.at(w-1);
        if(w == 2) break;
    }
    if(w == 2) cout << c << endl;
    else cout << -1 << endl;
}

Measured memory_bytes: 640
import sys

def solve(h, w):
    result = h
    for i in range(1, w):
        area1 = h * i
        area2 = h // 2 * (w - i)
        area3 = h * (w - i) - area2
        diff = max(area1, area2, area3) - min(area1, area2, area3)
        result = min(result, diff)

    return result

H, W = [int(v) for v in input().split()]

if H % 3 == 0 or W % 3 == 0:
    print(0)
    sys.exit(0)

ans = solve(H, W)
if H != W:
    ans = min(ans, solve(W, H))

print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI  3.14159265358979323846
#define lp(i,n) for(int i = 0; i <n;i++)
#define range1(i,a,b,c) for(int i = int(a);i <= int(b);i+=int(c))
#define range(i,a,b) for(int i = int(a);i <= int(b);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define sz(v) ((int)((v).size()))
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1, v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1, v2, v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1, v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1, v2, v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef pair<int, int> pii;
template<class T> using ordered_set=tree<T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;
const ll MOD = 1e9 + 7;
const int OO = (int)1e6;
const int N = (int)1e7;
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};
void init(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
int val[26];
ll a[N];
int main() {
	#   ifdef LOCAL_PROJECT
        freopen("in.txt", "r", stdin);
    #   endif
    // ll num ;
    // cin>>num;
    // for(char a= 'a';a<='z';a++)val[a-'A']=a-'a';
    // string ans ;
    // while(num){
    //             num--;
    //             char c = char('a'+num%26);
    //             ans=c+ans;
    //             num/=26;
    // }

    // cout<<ans<<endl;
    int n;
    scanf("%d",&n);
    map<ll,int>mp;
    ll sum =0;
    lp(i,n){
        scanf("%lld",&a[i]);
        sum+=a[i];
        mp[a[i]]++;
    }
    int q;
    scanf("%d",&q);
    lp(i,q){
        ll f,t;
        scanf("%lld %lld",&f,&t);
        sum-=mp[f]*f;
        sum+=t*mp[f];
        mp[t]+=mp[f];
        mp[f]=0;
        printf("%lld\n",sum);
    }
    return 0;
}

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

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

    int len = (int)s.length();
    long long ans = 0;
    for(int i = 0; i < (1<<(len-1)); i++){
        long long si = s[0] - '0';
        for(int t = 0; t < len-1; t++){
            if(i&(1<<t)){
                ans+=si;
                si = 0;
            }
            si = si * 10 + s[t+1] - '0';
        }
        ans+=si;
    }
    cout<<ans;
}


Measured memory_bytes: 256
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        try (BufferedInputStream in = new BufferedInputStream(System.in);
             PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {

            Scanner sc = new Scanner(in);

            // 15:06-

            int n = sc.nextInt();
            int[] as = new int[n + 1];
            for (int i = 0; i < as.length; i++) as[i] = sc.nextInt();

            int[] vs = new int[n];
            for (int i = 0; i < vs.length; i++) vs[i] = sc.nextInt();

            long killed = 0;
            for (int i = vs.length - 1; i >= 0; i--) {
                for (int j = 1; j >= 0; j--) {
                    int a = as[i + j];
                    int v = vs[i];

                    int m = Math.min(v, a);
                    killed += m;
                    vs[i] -= m;
                    as[i + j] -= m;
                }
            }
            
            out.println(killed);
        }
    }

}


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

public class Main {

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

  public void run() {

    try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

      int n = Integer.parseInt(br.readLine());
      String op;
      String[] words;
      Tree tree = new Tree();

      for(int i=0,k=0; i<n; i++,k=0){
        words = br.readLine().split(" ");
        op  = words[k++];
        if ( op.equals("insert") ) { 
          tree.insert(Integer.parseInt(words[k++]));
        }
        else {
          tree.dispRoundPath();
        }
      }

    }
    catch(IOException e) {
      System.out.println("IOException!");
    }
  }
}

class Tree {

  static final BTreeNode NIL = new BTreeNode(-1);
  BTreeNode root;
  StringBuilder buf;

  public Tree() {
    root = NIL;
    buf = new StringBuilder();
  }

  public void insert(int val) {
    BTreeNode y = NIL;
    BTreeNode x = root;
    BTreeNode z = new BTreeNode(val);

    while( !x.equals(NIL) ) {
      y = x;
      if( z.key < x.key )
        x = x.left;
      else
        x = x.right;
    }
    z.parent = y;

    if( y.equals(NIL) ){
      root = z;
    }
    else if( z.key < y.key )
      y.left = z;
    else
      y.right = z;
  } 
  void preParse(BTreeNode u) {
    if( !u.equals(NIL) ) {
      buf.append(" ").append(u.key);
      preParse(u.left);
      preParse(u.right);
    }
  }
  void inParse(BTreeNode u) {
    if( !u.equals(NIL) ) {
      inParse(u.left);
      buf.append(" ").append(u.key);
      inParse(u.right);
    }
  }

  void dispRoundPath() {

    inParse(root);
    buf.append("\n");
    preParse(root);
    buf.append("\n");

    System.out.print(buf);
    buf.setLength(0);
  }
}

class BTreeNode {
  final int key;
  BTreeNode parent,left,right;

  public BTreeNode(final int key) {
    this.key = key;
    this.parent =
    this.left =
    this.right = Tree.NIL;
  }

  @Override
  public boolean equals(Object o) {
    if(o == this) return true;
    if(o == null) return false;
    if(!(o instanceof BTreeNode)) return false;

    BTreeNode node = (BTreeNode)o;
    if(!(this.key == node.key)) return false;

    return true;
  }
}


Measured memory_bytes: 125560
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vaector<long>
#define vll vector<long long int>
#define vC  vector<char>
#define FastIO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
ll pow(ll x, ll y,ll m) { ll res = 1;x=x%m; while (y > 0) { if (y & 1) res = ((res%m) * (x%m))%m; y = y >> 1; x = ((x%m) * (x%m))%m; } return res%m; }
template <typename Arg1> void ZZ(const char* name, Arg1&& arg1){std::cerr << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr(names + 1, ',');
	std::cerr.write(names, comma - names) << " = " << arg1;
	ZZ(comma, args...);
}
const ll INF = 0xFFFFFFFFFFFFFFFL;
clock_t time_p=clock();
void abhigyan10()
{time_p=clock()-time_p;cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";}
ll seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline ll rnd(ll l=0,ll r=INF){return uniform_int_distribution<ll>(l,r)(rng);}
#define clrbuf cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define preciset(x) cout<<setprecision(x)<<fixed;
const ll mod =1000000007;
int s[6], c[6];
int main()
{
    

        FastIO

        
        int l,h,i,j,flag,n,m;
        cin>>n>>m;
        for(i=0;i<m;i++)
        {
        	cin>>s[i]>>c[i];
        }

        if(n==1)
        	{l=0;h=9;}
        if(n==2)
        {
        	l=10;h=99;
        }
        if(n==3)
        {
        	l=100;h=999;
        }

        for(i=l;i<=h;i++)
        {
        	string stri = to_string(i); 
        	flag=0;
        	for(j=0;j<m;j++)
        	{
        		if((stri[s[j]-1]-'0') == c[j])
        			continue;
        		
        			flag=-1;
        	}
        	if(flag==0)
        		{cout<<i; 
        			return 0;
        		}
        }

        cout<<-1;
        return 0;
    }



Measured memory_bytes: 256