akhauriyash/Code-Regression huggingface dataset:
#include <iostream>

using namespace std;


int main(void){
	int s,e,n;
	
	cin >> n;
	
	for(int i=0;i<n;i++){
		cin >> s >> e;
		cout << s;
		if(s <= 5){
			if(e < s){
				for(int j=s-1;j>=e;j--){
					cout << " " << j;
				}
				cout << endl;
			}else{
				for(int j=s+1;j<=e;j++){
					cout << " " << j;
 				}
 				cout << endl;
			}
		}else{
			if(e >= 6){
				if(s < e){
					for(int j=s+1;j<=e;j++){
						cout << " " << j;
					}
					cout << endl;
				}else{
					for(int j=s+1;j<=9;j++){
						cout << " " << j;
					}
					for(int j=5;j>=0;j--){
						cout << " " << j;
					}
					for(int j=1;j<=5;j++){
						cout << " " << j;
					}					
					for(int j=6;j<=e;j++){
						cout << " " << j;
					}
					cout << endl;
				}
			}else{
				for(int j=s+1;j<=9;j++){
					cout << " " << j;
				}
				for(int j=5;j>=e;j--){
					cout << " " << j;
				}
				cout << endl;
			}
		}
	}

}

Measured memory_bytes: 0
def call(n)
 ii=1
 ii.upto(n){|i|
  x=i
  if x%3==0 
   print(' ' + i.to_s)
   next
  end
  while x>0
   if x%10==3
    print(' '+i.to_s)
    break
   end
   x/=10
  end
 }
 puts
end
call(gets.to_i)

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

// https://ei1333.hateblo.jp/entry/2017/04/10/224413

vll G[150001];
double ee[150001], ans[150001];

void dfs1(int v, int p){
  double res=0;
  int child=0;
  for(auto &nv: G[v]){
    if(nv==p) continue;
    dfs1(nv,v);
    res+=ee[nv]+1.0;
    child++;
  }
  ee[v]=0;
  if(child>=1) ee[v]+=res/child;
}

void dfs2(int v, double d_par, int p){
  double res=0;
  for(auto &nv: G[v]){
    if(nv==p) res+=d_par+1.0;
    else res+=ee[nv]+1.0;
  }
  ans[v]=res/G[v].size();
  for(auto &nv: G[v]){
    if(nv==p) continue;
    dfs2(nv, (res-ee[nv]-1.0)/max(1, (int)G[v].size()-1) , v); // vにおける期待値から部分木nv以降の期待値を除外して渡す
  }
}

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  int n; cin>>n;
  rep(i,n-1){
    ll a,b; cin>>a>>b;
    a--,b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  dfs1(0,-1);
  dfs2(0,0,-1);
  rep(i,n){
    cout<<std::setprecision(10)<<ans[i]<<endl;
  }
  
}

Measured memory_bytes: 21336
import sys

def main():
    N = int(input())
    A = list(map(int, input().split()))
    cnt = 0
    for i in range(N):
        if A[i] % 2 != 0:
            cnt += 1
    
    print('YES' if cnt % 2 == 0 else 'NO')
if __name__ == '__main__':
    main()

Measured memory_bytes: 14112
#include<iostream>
#include<string>
#include<stdio.h>
#include <algorithm>
#include <set>
#include<math.h>
#include<vector>

using namespace std;

int main(){
    int a,b;cin >> a >> b;
    int t = 0;
    int cnt = 1;
    for(int i = 0;i < a;i++){
        int tmp = 0;
        cin >> tmp;
        if(t + tmp <= b)cnt++;
        t+=tmp;
    }
    cout << cnt << endl;
}

Measured memory_bytes: 3644
#include<bits/stdc++.h>
#define rep(i,a,n) for (int i = a;i < n;i++)
#define per(i,n,a) for (int i = n-1;i >= a;i--)
using namespace std;

int main() {
    int N;
    unsigned long long S = 1;
    long long P[1001] = {0};
    scanf("%d", &N);
    int d;
    for (int i = 2; i <= N; i++) {//iの約数を求める
        d = i;
        for (int j = 2; d > 1; j++) {//素因数分解
            while (d % j == 0) {
                P[j] += 1;
                d /= j;
            }
        }
    }
    for (int i = 2; i <= N; i++) {
        S = (S * (P[i] + 1)) % 1000000007;
        //printf("%llu\n",S);
    }
    printf("%llu", S);
    return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n; cin >> n;
  vector<int> v(n);
  for(int i = 0; i < n; ++i) cin >> v[i];
  for(int i = 0; i + 1 < n; ++i) {
    if(v[i] < 0) {
      v[i] = -v[i];
      v[i + 1] = -v[i + 1];
    }
  }

  long long sum = 0;
  bool was = false;
  int best = 2e9;
  for(int i = 0; i < n; ++i) {
    if(v[i] < 0) was = true;
    best = min(best, abs(v[i]));
    sum += abs(v[i]);
  }
  
  if(was) sum -= (best << 1);
  cout << sum << endl;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

class UnionFind {
public:
	vector < ll > par;
	vector < ll > siz;
	UnionFind(ll sz_) : par(sz_), siz(sz_, 1) {
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	void init(ll sz_) {
		par.resize(sz_);
		siz.assign(sz_, 1);
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	ll root(ll x) {
		while (par[x] != x) {
			x = par[x] = par[par[x]];
		}
		return x;
	}
	bool unite(ll x, ll y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		par[y] = x;
		return true;
	}
	bool same(ll x, ll y) {
		return root(x) == root(y);
	}
	ll size(ll x) {
		return siz[root(x)];
	}
};

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(m), b(m), ans(m);
    UnionFind UF(n);
    rep(i, m)cin >> a[i] >> b[i];
    reverse(all(a));
    reverse(all(b));
    ans[0] = n * (n-1) / 2;
    REP(i, m-1){
        int A = a[i-1]-1, B = b[i-1]-1;
        if(!UF.same(A, B)){
            ans[i] = ans[i-1]-UF.size(A)*UF.size(B);
        }
        else ans[i] = ans[i-1];
        UF.unite(A, B);
    } 
    reverse(all(ans));
    rep(i, m)cout << ans[i] << endl;
}


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

#define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define SIZE(x) ll((x).size())
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

/* REPmacro*/
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define RREP(i, n) RFOR(i, n, 0)
#define EACH(i, n) for (auto&& i : n)

/* alias */
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using str = string;


/* const */
const int INF = 1001001001;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

/* func */
template <typename T = int> inline T in(){T x; cin >> x; return x;}
template <typename T> inline void print(const T& x){cout << x << "\n";}
inline void printvec(const vi& vec){REP(i, SIZE(vec)) cout << vec.at(i) << ((i == SIZE(vec)) ? "\n" : " ");}
inline int lcm(int a, int b){return a / gcd(a, b) * b;}
bool is_prime(int n){if (n < 2) return false;FOR(i, 2, (int)(sqrt(n) + 1)){if (n % i == 0) return false;}return true;}


signed main(){
  // 人数, 初期の点数, 問題数
  int N = in(), K = in(), Q = in();
  // A[i] = (第i問の正解者)
  vi A(Q); EACH(x, A) x = (in() - 1);
  
  vi P(N, 0);
  REP(i, Q){
    ++P.at(A.at(i));
  }
  
  REP(i, N){
    if (Q < K) {print("Yes");}
    else if (P.at(i) > max((int)0, (Q - K))){print("Yes");}
    else {print("No");}
  }
}

Measured memory_bytes: 2176
while True :
    n, x = map(int, input().split())
    
    if n == 0 :
        break
    else :
        cnt = 0
        
        for i in range(1, n - 1) :
            for j in range(i + 1, n) :
                for k in range(j + 1, n + 1) :
                    if i + j + k == x :
                        cnt += 1
                        
        print(cnt)



Measured memory_bytes: 5596
#include<bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define lper(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9;
const ll INF=4e18;
const ll dy[4]={1,0,-1,0};
const ll dx[4]={0,-1,0,1};
int main() {
  int X;
  int Y;
  cin >> X>> Y;
  int x=X+Y/2;
  cout << x << endl;
}
  

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int h[500001],w[500001],o,q[500001],f[500001],c[500001],v[500001],qw,wq,a,b,qq=1,ww=1,ans=0;
int get(int y)
{
    if(y==f[y]) return y ; 
    return f[y]=get(f[y]);
}
void dfs(int n, int m)
{
    c[n]=m;
    for (int i=h[n]; i; i=w[i])
    {
        if (q[i]!=f[n])
        {
            f[q[i]]=n;
            dfs(q[i], -m);
            c[n] += c[q[i]];
        }
    }
}
int main()
{
    for (int i = 1; i <= 500000; ++i)
    {
        f[i] = i;
    }
    cin>>a>>b;
    for (int i = 1; i <= b; ++i)
    {
        cin>>qw>>wq;
        if (get(qw) != get(wq))
        {
            f[f[qw]] = f[wq];
        }
        else
        {
            qq = qw;
             ww = wq;
            continue;
        }
        o++;
        w[o] = h[qw];
        q[o] = wq;
        h[qw]=o;
        o++;
        w[o] = h[wq];
        q[o] = qw;
        h[wq]=o;
    }
    f[qq]=0;
    dfs(qq,1);
    if(a==b)
    {
        long long u=0;
        for(int i=ww;i;i=f[i])
        {
            u++;
            v[u]=c[i];
        }
        if(u%2)
        {
            if(c[qq]%2)
            {
                cout<<"-1";
                return 0;
            }
            for (int i = ww; i; i = f[i])
            {
                c[i] -= c[qq] / 2;
            }
        }
        else
        {
            if (c[qq])
            {
                cout<<"-1";
                return 0;
            }
            sort(v + 1, v + u + 1);
            for (int i = ww; i; i = f[i])
            {
                c[i] -= v[u / 2];
            }
        }
    }
    else if (c[qq])
    {
        cout<<"-1";
        return 0;
    }
    for (int i = 1; i <= a; ++i)
    {
        ans += abs(c[i]);
    }
    cout<<ans;
    return 0;
}

Measured memory_bytes: 16068
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll zero = 0;
ll INF = 10000000000;

int main(void){
    ll X,Y,A,B,C;
    cin >> X >> Y >> A >> B >> C;
    vector<ll> p(A+2,0);
    vector<ll> q(B+2,0);
    vector<ll> r(C+2,0);
    for(ll i = 1; i < A+1; i++){
        cin >> p.at(i);
    }
    for(ll i = 1; i < B+1; i++){
        cin >> q.at(i);
    }
    for(ll i = 1; i < C+1; i++){
        cin >> r.at(i);
    }
    sort(p.begin() + 1, p.end(), greater<ll>());
    sort(q.begin() + 1, q.end(), greater<ll>());
    sort(r.begin() + 1, r.end(), greater<ll>());
    p.at(0) = INF;
    q.at(0) = INF;
    
    vector<ll> count(3);
    count = {X, Y, 0};
    
    while( p.at(count.at(0)) < r.at(count.at(2) + 1) || q.at(count.at(1)) < r.at(count.at(2) + 1) ){
        if(q.at(count.at(1)) >= r.at(count.at(2) + 1)){
            count.at(0)--;
            count.at(2)++;
        }
        else if(p.at(count.at(0)) >= r.at(count.at(2) + 1)){
            count.at(1)--;
            count.at(2)++;
        }
        else{
            if(p.at(count.at(0)) < q.at(count.at(1))){
                count.at(0)--;
                count.at(2)++;
            }
            else{
                count.at(1)--;
                count.at(2)++;
                
            }
        }
    }
    
    ll ans = 0;
    for(int i = 1; i <= count.at(0); i++){
        ans += p.at(i);
    }
    for(int i = 1; i <= count.at(1); i++){
        ans += q.at(i);
    }    
    for(int i = 1; i <= count.at(2); i++){
        ans += r.at(i);
    }
    
    printf("%lld\n", ans);
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
int main(){
  vector<int> data(5);
  int k;
  for(int i=0;i<5;i++){
  cin >>data.at(i);}
  cin >>k;
  bool check=true;
  for(int i=0;i<5;i++){
    for(int j=i+1;j<5;j++){
      if(max(data.at(i),data.at(j))-min(data.at(i),data.at(j))>k){check=false;break;}
    }
  }
  if(check==true){cout <<"Yay!"<<endl;}
  else{cout <<":("<<endl;}
}

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

using namespace std;

void func(int n, int m, vector<int>res){

  if(n==0){
    for(int i=0;i<res.size();i++){
      cout << res[i];
      if(i==res.size()-1)cout << endl;
      else cout << " ";
    }
    return;
  }
  

  for(int i=0;i<m;i++){
    if(m-i>n)continue;
    res.push_back(m-i);
    func(n-m+i,m-i,res);
    res.pop_back();
  }
}

int main(void){

  int n;
  vector<int>res;

  while(cin >> n,n)
    func(n,n,res);

  return 0;
}

Measured memory_bytes: 1224
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>

using namespace std;
#define vsort(v) sort(v.begin(), v.end());

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

	int N;
	cin >> N;
	int l[N];
	int r[N];
	for(int i = 0; i < N; i++) {
		cin >> l[i] >> r[i];
	}
	int rslt = 0;
	for(int i = 0;i < N; i++) {
		rslt += r[i] - l[i] + 1;
	}
	cout << rslt << endl;
}


Measured memory_bytes: 256
using namespace std;

//#define fileio

#ifndef fileio
#include <iostream>
#endif
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <assert.h>
#include <functional>
#include <math.h>
#include <string>
#include <ctime>

typedef long long ll;

#ifdef fileio
ifstream cin;
ofstream cout;
#endif



bool grid[1005][1005];
bool seen[1005][1005];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifdef fileio
    cin.open("Text.txt");
    cout.open("Output.txt");
    #endif
    
    int h, w;
    cin >> h >> w;
    int m;
    cin >> m;
    set<pair<int, int>> ms;
    vector<int> xs(h, 0);
    vector<int> ys(w, 0);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        xs[x]++;
        ys[y]++;
        ms.insert({ x, y });
    }
    int mxx = 0;
    int mxy = 0;
    for (int i = 0; i < h; i++) {
        mxx = max(mxx, xs[i]);
    }
    for (int i = 0; i < w; i++) {
        mxy = max(mxy, ys[i]);
    }
    vector<int> xis;
    vector<int> yis;
    for (int i = 0; i < h; i++) {
        if (xs[i] == mxx) xis.push_back(i);
    }
    for (int i = 0; i < w; i++) {
        if (ys[i] == mxy) yis.push_back(i);
    }
    if ((ll)xis.size() * (ll)yis.size() > m) {
        cout << mxx + mxy << endl;
        return 0;
    }
    for (int i = 0; i < xis.size(); i++) {
        for (int j = 0; j < yis.size(); j++) {
            if (!ms.count({ xis[i], yis[j] })) {
                cout << mxx + mxy << endl;
                return 0;
            }
        }
    }
    cout << mxx + mxy - 1 << endl;
    return 0;
}

Measured memory_bytes: 23824
import java.util.*;

class Main{
  public static void main(String[] args){
    //declare
    Scanner in = new Scanner(System.in);
    ArrayList<Integer> array = new ArrayList<Integer>();
    int a,b;
    //imput
    b = 0;
    a = in.nextInt();
    for (int i=0;i<a;i++){
      b += Integer.parseInt(in.next());
    }
    //calculaate
    if(b % 2 == 0){System.out.println("YES");}
    else{System.out.println("NO");}
  }
}

Measured memory_bytes: 48324
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

class Graph{
	private:
	int V;
	public:
	vvi Pro;
	Graph(int v){
		V=v;
		Pro=vvi(v,vi(v,inf));
		for(int i=0;i<v;i++) Pro[i][i]=0;
	}
	void add_edge(int x,int y,int c){
		Pro[x][y]=c;
		Pro[y][x]=c;
	}
	void Warshall(){
		for(int k=0;k<V;k++){
			for(int i=0;i<V;i++){
				for(int j=0;j<V;j++){
					Pro[i][j]=min(Pro[i][j],Pro[i][k]+Pro[k][j]);
				}
			}
		}
	}
};

int V,E,N;

int main(){
	while(1){
		cin>>E>>V;
		if(!V&&!E) break;
		Graph cost(V),time(V);
		int s,t,c,l;
		for(int i=0;i<E;i++){
			cin>>s>>t>>c>>l;
			s--;
			t--;
			cost.add_edge(s,t,c);
			time.add_edge(s,t,l);
		}
		cost.Warshall();
		time.Warshall();
		cin>>N;
		bool r;
		for(int i=0;i<N;i++){
			cin>>s>>t>>r;
			s--;
			t--;
			if(r) cout<<time.Pro[s][t]<<endl;
			else cout<<cost.Pro[s][t]<<endl;
		}
	}
}

Measured memory_bytes: 1348
#include<iostream>
using namespace std;
int main(){
int n[4], f[4];
for(int i=0; i<4; i++){
cin >> n[i];
f[i] = 0;
}

for(int i=0; i<4; i++){
if(n[i]==1)
f[0]=1;
if(n[i]==4)
f[1]=1;
if(n[i]==7)
f[2]=1;
if(n[i]==9)
f[3]=1;
}

if(f[0]==1&&f[1]==1&&f[2]==1&&f[3]==1)
cout << "YES";
else
cout << "NO";
  return 0;
}

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

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

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

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

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int H, W; cin >> H >> W;
    int N; cin >> N;
    int A[N]; rep(i, N) cin >> A[i];
    int ans[H+2][W+2];
    rep(i, H+2) rep(j, W+2) ans[i][j] = -1;
    for(int i = 1; i <= H; i++) for(int j = 1; j <= W; j++) ans[i][j] = 0;
    int idx = 0;
    int delta = 0;
    int sx = 1, sy = 1;
    int loop = 0;
    while(loop < H*W){
        ans[sx][sy] = idx+1;
        A[idx]--;
        if(A[idx] == 0) idx++;

        int nx = sx + dx[delta], ny = sy + dy[delta];
        if(ans[nx][ny] == -1){
            delta = (delta+1) % 4;
            sx += dx[delta]; sy += dy[delta];
        }
        else if(ans[nx][ny] != 0){
            delta = (delta+1) % 4;
            sx += dx[delta]; sy += dy[delta];
        }
        else{
            sx = nx; sy = ny;
        }

        loop++;
    }
    for(int i = 1; i <= H; i++){
        for(int j = 1; j <= W; j++){
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> grh;
vector<long long> dp;
void dfs (int crr, long long far) {
  dp[crr] = far;
  for (auto p : grh[crr]) {
    int wt = p.first;
    int nxt = p.second;
    if (dp[nxt] == -1) {
      dfs(nxt, far + wt);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  grh.resize(n);
  dp.resize(n, -1);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    grh[--a].push_back({c, --b});
    grh[b].push_back({c, a});
  }
  int q, k;
  cin >> q >> k;
  dfs(--k, 0);
  while (q--) {
    int x, y;
    cin >> x >> y;
    long long ret = dp[--x] + dp[--y];
    cout << ret << '\n';
  }
  return 0;
}

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

public class Main {
	static FastReader in = new FastReader();

	public static void main(String[] args) {
		solve();
	}

	static void solve() {
		int n = in.nextInt();
		int w = in.nextInt();
		
		int[] we = new int[n];
		int[] val = new int[n];
		int sum = 0;
		for(int i = 0; i < n; i++)
		{
			we[i] = in.nextInt();
			val[i] = in.nextInt();
			sum += val[i];
		}
		long ans = 0;
		int max = sum + 10;
		long[][] dp = new long[n + 1][sum + 1];
		for(int i = 0; i <= n; i++)
		{
			Arrays.fill(dp[i], Integer.MAX_VALUE);
			dp[i][0] = 0;
		}
		
		
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= sum; j++)
			{
				dp[i][j] = Math.min(dp[i - 1][Math.max(0, j - val[i - 1])] + we[i - 1], dp[i - 1][j]);
				if(dp[i][j] <= w) ans = Math.max(ans, j);
			}
		}
		System.out.println(ans);
		
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

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

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

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

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		}
	}
}


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

long long int dp[3005][3005];

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

    vector<long long int> a(n);

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

    for(int L=n-1; L >= 0; L--)
    {
        for(int R=L; R<n; R++)
        {
            if(L==R)
                dp[L][R] = a[L];
            else
                dp[L][R] = max(a[L] - dp[L+1][R], a[R] - dp[L][R-1]);
        }
    }

    cout<<dp[0][n-1]<<endl;

    return 0;
} 


Measured memory_bytes: 49748
#!/mnt/c/Users/moiki/bash/env/bin/python
# FROM : https://atcoder.jp/contests/dp/tasks/dp_l
# N,M = map(int, input().split())
N = int(input())
# A = list(map(int, input().split()))
A = tuple(map(int, input().split()))

dp = [ [0]*(N+1) for _ in range(N+1)]
for d in range(1,N+1):
    for l in range(N+1-d):
        r = l+d
        dp[l][r] = \
        max(
            A[l] - dp[l+1][r],
            A[r-1] - dp[l][r-1]
                )
print(dp[0][N])


Measured memory_bytes: 112220
#include <iostream>
#include <string>
#include <vector>

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
  string S, T;
  cin >> S >> T;
  S += S;

  bool found = false;
  rep(i, S.size() - T.size() + 1)
  {
    if (S.substr(i, T.size()) == T)
    {
      found = true;
      break;
    }
  }

  if (found)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}


Measured memory_bytes: 256
import sys
import os
import itertools
import numpy as np
from collections import deque

def main():
    if os.getenv("ENV")=='LOCAL':
        sys.stdin = open(os.path.dirname(__file__)+'/input.txt', 'r')

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

    A.sort(reverse=True)
    # print(A)
    # s=sum(A)-min(A)
    A_set=list(set(A))
    A_set.sort(reverse=True)
    set_p=0
    # print(A_set)

    ans=0
    # print(A_set)
    # print([0]*len(A_set))
    arc={k: 0 for k in A_set}
    # print(type(arc))
    # print(A[0])
    arc[A_set[0]]=arc[A_set[0]]+1
    max=A_set[0]    #取得できる最大の心地よさ
    for a in A[1:]:
        # print(arc)
        arc[a]+=2

        ans+=max
        arc[max]-=1
        if arc[max]==0:
            set_p+=1
            max=A_set[set_p]


    print(ans)
    # comfort

if __name__ == '__main__':
    main()


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

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

typedef vector<int> vi;
typedef vector<vi> vvi;

struct Edge {
	int src, dst, cost;
	Edge(int s, int d, int c) : src(s), dst(d), cost(c) {}
	Edge(){}
	bool operator< (const Edge& e)const {
		return cost < e.cost;
	}
};

struct UnionFind{
	vi par, rank;
	UnionFind(int n) : par(n), rank(n, 0) {
		rep(i, n)
			par[i] = i;
	}

	int find(int a){
		return par[a] == a ? a : (par[a] = find(par[a]));
	}

	bool same(int a, int b){
		return find(a) == find(b);
	}

	void unite(int a, int b){
		a = find(a);
		b = find(b);
		if(a == b)
			return;

		if(rank[a] < rank[b])
			par[a] = b;
		else
			par[b] = a;
		rank[a] += rank[a] == rank[b];
	}
};

int main()
{
	int n, m;
	while(scanf("%d%d", &n, &m), n){
		vector<Edge> E(m);
		rep(i, m){
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			E[i] = Edge(a, b, c);
		}
		sort(E.begin(), E.end());

		int cost = 0;
		UnionFind uf(n);
		rep(i, m){
			if(!uf.same(E[i].src, E[i].dst)){
				uf.unite(E[i].src, E[i].dst);
				cost += E[i].cost;
			}
		}
		printf("%d\n", cost);
	}

	return 0;
}

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

int main() {
  int n;
  cin >> n;
  deque<int> q;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (i % 2 == n % 2) {
      q.push_back(a);
    } else {
      q.push_front(a);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << q.at(i);
    if (i == n - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}


Measured memory_bytes: 2944
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a, b, c, k;
    cin >> a >> b >> c >> k;
    while(k--){
        if((c<=b && c<=a) || (c<=a && c>=b) || (c>=a && c<=b)){
            c *= 2;
        }else if(b<=a){
            b *= 2;
        }
    }
    if(a<b && b<c) cout << "Yes\n";
    else cout << "No\n";
    return 0;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c;
  c = 1;
  cin >> a >> b;
  for (int i = 0; i < a; i++){
    if (c+b<c*2){
      c = c+b;
    }
    else {
      c = c*2;
    }
  }
  cout << c <<endl;
}

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

#define sc(x) cin >>x
#define sc2(x,y) cin >> x >> y ;
#define sc3(x,y,z) cin >> x >> y >> z ;
#define pb push_back
#define mp make_pair
#define fi first
#define se second 

#define M 1000000007
#define inf 99999999999999999LL	//long long inf

#define debug(x) cerr<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define debug4(x,y,z,a) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#a<<" :: "<<a<<"\n";

#define LIM 100020
int main()
{
	int i,j,t;
	int n ;
	sc(n) ;
	if(n==1){
		printf("Hello World\n") ;
	}	
	else
	{
		int x,y;
		sc2(x,y) ;
		printf("%d\n",x+y);
	}
	return 0;
}







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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main() {
    map<ll, ll> m;
    int N;
    cin >> N;
    for(int i = 0; i < N; ++i) {
        int a;
        cin >> a;
        m[a] += 1;
    }
    ll x = -1, y = -1;
    for(auto& p : m) {
        if(p.second >= 2) {
            if(x < p.first) {
                y = x;
                x = p.first;
                p.second -= 2;
            } else if(y < p.first) {
                y = p.first;
                p.second -= 2;
            }
        }
        if(p.second >= 2) {
            if(x < p.first) {
                y = x;
                x = p.first;
                p.second -= 2;
            } else if(y < p.first) {
                y = p.first;
                p.second -= 2;
            }
        }
    }
    if(x == -1 || y == -1) {
        cout << 0 << endl;
    } else {
        cout << x * y << endl;
    }
}


Measured memory_bytes: 6528
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define all(a)  begin((a)),end((a))
#define uniq(c) (c).erase(unique(all((c))), end((c)))

void print_arr(vector<int> &A)
{
    rep (i, 0, A.size())
        if (i != A.size()-1)
            printf("%d ", A[i]);
        else
            printf("%d\n", A[i]);
}

int selection_sort(vector<int> &A)
{
    int cnt = 0;
    rep (i, 0, A.size()) {
        int min_j = i;
        rep (j, i, A.size()) {
            if (A[j] < A[min_j]) {
                min_j = j;
            }
        }
        if (A[i] > A[min_j]) ++cnt;
        swap(A[i], A[min_j]);
    }
    return cnt;
}

int main()
{
    int N; cin >> N;
    vector<int> A(N);
    rep (i, 0, N) cin >> A[i];

    int cnt = selection_sort(A);
    print_arr(A);
    cout << cnt << endl;
}


Measured memory_bytes: 3232
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 2
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	h := getNextInt(scanner)
	w := getNextInt(scanner)
	n := getNextInt(scanner)

	if h < w {
		h, w = w, h
	}

	fmt.Fprintln(writer, (n-1)/h+1)
}


Measured memory_bytes: 640
#include <iostream>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    int n;
    std::cin >> n;

    int m = (n % 2 == 0 ? 1 : 2);

    auto dp = vec(m + 1, vec(n + 2, -INF));
    dp[0][0] = 0;

    for (int i = 0; i <= n; ++i) {
        lint x;
        if (i >= n) {
            x = 0;
        } else {
            std::cin >> x;
        }

        for (int k = 0; k + 1 <= m; ++k) {
            dp[k + 1][i + 1] = std::max(dp[k + 1][i + 1], dp[k][i]);
        }
        if (i == n) break;

        for (int k = 0; k <= m; ++k) {
            dp[k][i + 2] = std::max(dp[k][i + 2], dp[k][i] + x);
        }
    }

    std::cout << dp[m][n + 1] << std::endl;
}

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

    solve();

    return 0;
}


Measured memory_bytes: 9536
#include <bits/stdc++.h>
using namespace std;
int dist[1005],par[1005];
vector<int> v[1005];
pair<int,pair<int,int> > ans(1e9,{0,0});
void bfs(int i)
{
	memset(dist,0,sizeof(dist));
	dist[i]=1;
	par[i]=0;
	queue<int> q;
	q.push(i);
	while (!q.empty())
	{
		int node=q.front();
		q.pop();
		for (int u:v[node])
		{
			if (u==i)
			ans=min(ans,make_pair(dist[node],make_pair(node,i)));
			if (!dist[u])
			{
				q.push(u);
				dist[u]=dist[node]+1;
				par[u]=node;
			}
		}
	}
}
void go(int node)
{
	if (par[node])
	go(par[node]);
	printf("%d\n",node);
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	while (m--)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		v[a].push_back(b);
	}
	for (int i=1;i<=n;i++)
	bfs(i);
	if (ans.first==1e9)
	{
		printf("-1");
		return 0;
	}
	bfs(ans.second.second);
	printf("%d\n",ans.first);
	go(ans.second.first);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
using namespace std;
 
signed main(){
  int N,K;
  cin>>N>>K;
  int h[100010];
  for(int i=1;i<=N;i++){
    cin>>h[i];
  }
  int dp[100010];
  for(int i=1;i<=N;i++){
    dp[i]=1e18;
  }
  dp[1]=0;
  for(int i=1;i<N;i++){
    for(int j=1;j<=K;j++){
      if(i+j>N){
        break;
      }else{
      dp[i+j]=min(dp[i+j],dp[i]+abs(h[i]-h[i+j]));
      }
    }
  }
  cout<<dp[N]<<endl;
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define rep(N) for (int i = 0; i < N; ++i)
#define Rep(a, b) for (int i = a; i < b; ++i)
#define For(i, N) for (int i = 0; i < N; ++i)
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()
#define makei(N) int N; cin >> N;
#define makes(s) string s; cin >> s;
#define maked(d) double d; cin >> d;
#define makev(v, N) vi v(N); rep(N)cin >> v[i];
#define mod 1000000007

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vs = vector<string>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using msi = map<string, int>;

template<typename T>
void say(T s) {
	cout << s << "\n";
}

template<typename T>
void say(vector<T> s) {
	auto itr = s.begin();
	cout << *(itr++);
	while (itr != s.end()) {
		cout << " " << *(itr++);
	}
	cout << "\n";
}

template<typename T>
void say(vector<T> s, char r) {
	auto itr = s.begin();
	cout << *(itr++);
	while (itr != s.end()) {
		cout << r << *(itr++);
	}
	cout << "\n";
}

void yn(bool b) {
	if (b)say("yes");
	else say("no");
}

void Yn(bool b) {
	if (b)say("Yes");
	else say("No");
}

void YN(bool b) {
	if (b)say("YES");
	else say("NO");
}

template<typename T>
void maxi(T& a, T b) {
	a = max(a, b);
}

template<typename T>
void mini(T& a, T b) {
	a = min(a, b);
}

void exact_say(double x) {
	cout << setprecision(numeric_limits<double>::max_digits10) << x << endl;
}

template<class T> void getVector(vector<T>& v) {
	for (int i = 0; i < v.size(); ++i)
		cin >> v[i];
}

std::string revString(std::string s) {
	string ret;
	auto it = s.rend();
	while (it != s.rbegin()) {
		ret.push_back(*it);
		++it;
	}
	return ret;
}

vector<vvi> init() {
	vi v;
	rep(10)v.push_back(0);
	vvi vv;
	rep(3)vv.push_back(v);
	vector<vvi> vvv;
	rep(4)vvv.push_back(vv);
	return vvv;
}

signed main() {
	makei(n);
	makei(m);
	vvi A;
	rep(n) {
		vi v;
		rep(m) {
			makei(a);
			v.push_back(a);
		}
		A.push_back(v);
	}
	makev(b, m);
	For(i, n) {
		int ans = 0;
		For(j, m) {
			ans += A[i][j] * b[j];
		}
		say(ans);
	}
}


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

int N;
int a[310];
int cnt[4];
double memo[310][310][310];

double f(int i, int j, int k) {
    if (memo[i][j][k]!=-1) return memo[i][j][k];
    if (i==0 && j==0 && k==0) return memo[i][j][k] = 0;
    double res = N;
    if (i>0) res += i*f(i-1, j, k);
    if (j>0) res += j*f(i+1, j-1, k);
    if (k>0) res += k*f(i, j+1, k-1);
    res /= i+j+k;
    return memo[i][j][k] = res;
}
 
int main() {
    cin >> N;
    rep(i, N) cin >> a[i];
    rep(i, N) cnt[a[i]]++;
    rep(i, 310) rep(j, 310) rep(k, 310) memo[i][j][k] = -1;
    cout << fixed << setprecision(12) << f(cnt[1], cnt[2], cnt[3]) << endl;
}

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

int main()
{
    int N,A,B,C,D;
    cin >> N >> A >> B >> C >> D;
    int a,b = 0;
    if(N % A == 0){
        a = (N - N % A) / A;
    } else {
        a = (N - N % A) / A + 1;
    }
    if(N % C == 0){
        b = (N - N % C) / C;
    } else {
        b = (N - N % C) / C + 1;
    }
    if(a * B < b * D){
        cout << a * B << endl;
    } else {
        cout << b * D << endl;
    }
    
    return 0;
 
}



Measured memory_bytes: 3108
#include<bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;

int main(){
  int N, W;
  cin >> N >> W;
  long long w[N], v[N];
  long long dp[N + 1][W + 1];
  for(int i = 0; i < N; i++) cin >> w[i] >> v[i];
  for(int i = 0; i <= W; i++) dp[0][i] = 0;
  for(int i = 0; i < N; i++){
    for(int j = 0; j <= W; j++){
      if(j >= w[i]) dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
      else dp[i + 1][j] = dp[i][j];
    }
  }
  cout << dp[N][W] << endl;
}

Measured memory_bytes: 82352
a_ = [[int(x) for x in input().split()] for _ in range(3)]
n = int(input())
b = []
for _ in range(n): b.append(int(input()))
a = []
for i in range(3):
    for a_i in a_[i]:
        a.append(a_i)
for b_i in b:
    if b_i not in a: continue
    else:
        a[a.index(b_i)] = 0
ans = "No"
for i in range(3):
    if a[i*3] == 0 and a[i*3+1] == 0 and a[i*3+2] == 0:
        ans = "Yes"
        break
for i in range(3):
    if a[i] == 0 and a[i+3] == 0 and a[i+6] == 0:
        ans = "Yes"
        break
if a[2] == 0 and a[4] == 0 and a[6] == 0: ans = "Yes"
if a[0] == 0 and a[4] == 0 and a[8] == 0: ans = "Yes"
print(ans)


Measured memory_bytes: 3064
#include<stdio.h>
#include<ctype.h>

int main(void){
char str[205],*temp;

gets(str);
temp=&str[0];
while(*temp!='\0'){
	if(islower(*temp)){*temp=toupper(*temp);}
	temp++;
}
printf("%s\n",str);
return 0;
}

Measured memory_bytes: 524
object Main extends App {
  val src = io.Source.stdin.getLines.map(_.toInt).toList

  def func(stack: List[Int], num: Int): List[Int] = {
      if (num == 0) { println(stack.head); stack.tail }
      else num :: stack
  }

  (Nil /:[List[Int]] src)(func)
}



Measured memory_bytes: 43956
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
long long n,i,s,a,b,q;
vector<long long> cnt;
int main() {
  /*  cin >> n;
    s = "";
    while(n > 1) {
        if(n%26 == 0) s += 'z';
        else s += (char)('a'+n%26-1);
        n /= 26;
    }
    
    if(n == 1) s = "a" + s;
    cout << s;*/
    cin >> n;
    cnt.resize(100001);
    s = 0;
    for(i = 0; i < n; i++) {
        cin >> a;
        cnt[a]++;
        s += a;
    }
    cin >> q;
    for(i = 0; i < q; i++) {
        cin >> a >> b;
        s = s-cnt[a]*a+cnt[a]*b;
        cnt[b] += cnt[a];
        cnt[a] = 0;
        cout << s << endl;
    }
    return 0;
}

Measured memory_bytes: 3960
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ft first
#define sd second
#define fr(i,n) for(int i=0;i<(n);++i)
#define Fr(i,n) for(int i=1;i<=(n);++i)
#define ifr(i,n) for(int i=(n)-1;i>=0;--i)
#define iFr(i,n) for(int i=(n);i>0;--i)

struct modint{
    ll a;
    const ll MOD=1e9+7;
    modint(ll a_=0){
        a=((a_%MOD)+MOD)%MOD;
    }
    modint inv()const{
        ll n=1,m=MOD-2,A=a;
        while(m){
            if(m&1)(n*=A)%=MOD;
            (A*=A)%=MOD;
            m>>=1;
        }
        modint y(n);
        return y;
    }
    bool operator==(const modint& x){
        return a==x.a;
    }
    bool operator!=(const modint& x){
        return a!=x.a;
    }
    modint& operator=(const modint& x){
        a=x.a;
        return *this;
    }
    modint operator+(const modint& x){
        modint y(a+x.a);
        return y;
    }
    modint operator-(const modint& x){
        modint y(a-x.a);
        return y;
    }
    modint operator*(const modint& x){
        modint y(a*x.a);
        return y;
    }
    modint operator/(const modint& x){
        return *this * x.inv();
    }
    modint& operator+=(const modint& x){
        *this=*this+x;
        return *this;
    }
    modint& operator-=(const modint& x){
        *this=*this-x;
        return *this;
    }
    modint& operator*=(const modint& x){
        *this=*this*x;
        return *this;
    }
    modint& operator/=(const modint& x){
        *this=*this/x;
        return *this;
    }
};
istream& operator>>(istream &in,modint& x){
    ll a_;
    in>>a_;
    modint y(a_);
    x=y;
    return in;
}
ostream& operator<<(ostream &out,const modint& x){
    out<<x.a;
    return out;
}
modint pwr(ll a,ll b){
    modint n(1),A(a);
    while(b){
        if(b&1) n*=A;
        A*=A;
        b>>=1;
    }
    return n;
}

class DDigitsParade {
public:
    void solve(istream& in, ostream& out) {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        string s;
        in>>s;
        int n=s.length();
        reverse(s.begin(),s.end());
        vector<vector<modint>> dp(n+1,vector<modint>(13));
        dp[0][0]=1;
        int c=1;
        fr(i,n){
            if(s[i]=='?'){
                fr(j,13){
                    fr(k,10){
                        dp[i+1][(j+k*c)%13]+=dp[i][j];
                    }
                }
            }
            else{
                fr(j,13){
                    dp[i+1][(j+(s[i]-'0')*c)%13]+=dp[i][j];
                }
            }
            c*=10;c%=13;
        }
        out<<dp[n][5]<<endl;
    }
};

int main() {
    DDigitsParade solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 24656
#include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c;
    cin >> a >> b >> c;
    bool ans=false;
    if((a==b && a!=c)||(b==c && a!=b)||(a==c && a!=b)) ans=true;
    puts(ans?"Yes":"No");
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
int i,j,n,a,b,c,d,k;
string s;
int main(){
  cin>>a>>b>>c>>d>>k;
  a*=60;c*=60;
  a=a+b;c=c+d-k;
  cout<<c-a;
}

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

#define ll long long 

#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif

template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
	cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << ": " << arg1 << " |";
	__f(comma + 1, args...);
}

const int N = 2e6+1;

ll int a[N] = {0};

int main()
{
	ios_base::sync_with_stdio(NULL);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	ll int n, k, r;
	cin >> n >> k;
	
	for(int i = 1; i <= n; ++i)
	{
		cin >> a[i];
		r = max(r, a[i]);
	}
	ll int l = 1, total ;
	while(r != l)
	{
		ll int mid;
		mid = (r + l)/2;
		ll cnt = 0;
		for(int i = 1; i<=n; ++i)
			cnt += (a[i] - 1) /mid;
			
	
		
		if(cnt <= k) r = mid;
		else l = mid + 1;
		
		
//		trace(r, mid, cnt, l);
	}
	cout << r << "\n";
	
	return 0;
}

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

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

typedef long long ll;

const ll INF=1e9+7;
const int MAX_N=1e5+4;
ll h[MAX_N];
ll A,B,n;
ll max(ll x,ll y){
	if(x>y)return x;
	else return y;
}
bool check(ll t){
	ll tmp=0;
	rep(i,n)if(h[i]-t*B>=0)tmp+=(h[i]-t*B)/(A-B)+(((h[i]-t*B)%(A-B))!=0);
	return tmp<=t;
}
ll bin(){
	ll l=0,r=INF;
	while(r>l){
		ll m=(r+l)/2;
		if(check(m))r=m;
		else l=m+1;
	}
	return r;
}

int main(){
	cin>>n>>A>>B;
	rep(i,n)cin>>h[i];
	cout<<bin()<<endl;
}


Measured memory_bytes: 1024
#include <string>
#include <vector>
#include <map>
#include <deque>
#include <iostream>
#include <queue>
#include <math.h>
#include <algorithm>
#include <fstream>
#include <regex>
using namespace std;
typedef long long ll;


int main(void) {
	ll n;
	string s;
	cin >> n;
	cin >> s;
	s = regex_replace(s, regex("(.)\\1{1,}"), "1");
	cout << s.length();
	return 0;
}

Measured memory_bytes: 44160
import sys


def foo(n):
    ts = [(a,b,c,d) for a in range(10) for b in range(10) for c in range(10) for d in range(10) if a+b+c+d == n]
    return len(ts)


#input_file = open(sys.argv[1], "r")

#for line in input_file:
for line in sys.stdin:
    n = int(line)
    print foo(n)

Measured memory_bytes: 4248
import sys
n=int(input())
l=sorted([int(input()) for _ in range(n)])
sm=sum(l)

if sm%10!=0:
    print(sm)
    sys.exit()
    
else:
    for i in range(n):
        if l[i]%10!=0:
            print(sm-l[i])
            sys.exit()
print(0)


Measured memory_bytes: 2940
#include<iostream>
#include<string>
#include<stdio.h>
#include<vector>
#include<algorithm>

using namespace std;
typedef long long ll;

int main(){
  ll N;
  ll ans=0;
  cin >> N;
  ll a[200001],b[200001];
  for(ll i=0;i<N;i++){
    cin >> a[i];
  }
  for(ll i=0;i<N;i++){
    cin >> b[i];
  }
  for(ll i=0;i<=28;i++){
    vector<ll>v;
    for(ll j=0;j<N;j++)v.push_back(b[j] % (1 << (i + 1)));
    sort(v.begin(),v.end());
    ll t=0;
    for(ll j=0;j<N;j++){
      ll low1 = lower_bound(v.begin(), v.end(), (1 << i) - a[j] % (1 << (i + 1))) - v.begin();
      ll low2 = lower_bound(v.begin(), v.end(), (2 << i) - a[j] % (1 << (i + 1))) - v.begin();
      ll low3 = lower_bound(v.begin(), v.end(), (3 << i) - a[j] % (1 << (i + 1))) - v.begin();
      ll low4 = lower_bound(v.begin(), v.end(), (4 << i) - a[j] % (1 << (i + 1))) - v.begin();
      t += low2 - low1 - low3 + low4;
      t %= 2;
    }
    // cout << t <<endl;
    ans += t << i;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 7204
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

signed main() {
    cin >> A >> B >> K;
    if (K < A) {
        cout << A - K << " " << B;
    } else if (A <= K && K < A + B) {
        cout << 0 << " " << A + B - K << endl;
    } else {
        cout << 0 << " " << 0 << endl;
    }
}


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

using namespace std;

#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, m, n) for (int i = m; i <= n; ++i)

using i64 = long long;
using pii = pair<i64, i64>;

template<class A, class B>inline bool chmax(A &a, const B &b){return b > a ? a = b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b < a ? a = b,1 : 0;}

constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int MOD = int(1e9) + 7;

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int a, b;
  cin >> a >> b;

  int ans = 0;
  for (int i = a; i <= b; ++i) {
    string S = to_string(i);
    reverse(all(S));
    if (S == to_string(i)) ++ans;
  }

  cout << ans << "\n";

  return 0;
}

Measured memory_bytes: 256