akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define inf 1e7;
 
int main() {
	ll n, a[2] = {}, ans = inf;
	char c[300010];
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> c[i];
		if (c[i] == 'E')a[1]++;
	}
	for (int i = 0; i < n; i++) {
		if (c[i] == 'E')a[1]--;
		ans = min(ans, a[0] + a[1]);
		if (c[i] == 'W')a[0]++;
	}
	cout << ans << endl;
	return 0;
}

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

#define mainspring cout<<"mainspring\n";
#define barrel cout<<"barrel\n";
#define escapement cout<<"escapement\n";

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end() 
#define sz(v) (int)((v).size())
#define INF INT_MAX
#define NINF INT_MIN
#define fs first
#define sd second
#define tik(i,a,b) for(int i = a; i < b; i++)
#define tok(i,a,b) for(int i = a; i >= b; i--)
#define error(args...) {string _s = #args; replace(_s.begin(), _s.end(), ',',' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it,args);}

typedef long long ll;
typedef unsigned long long ull;

typedef pair < int, int > pi;
typedef pair < pi, int > ppi;

typedef vector < int > vi;
typedef vector < vector < int > > vvi;
typedef vector < pi > vpi;
typedef vector < vpi > vvpi;
typedef vector < ppi > vppi;

void fast() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << "\n";
    err(++it, args...);
}
const int  MAX_L = 3000+100;

int dp[MAX_L][MAX_L];
vector<char> res;
string s, t;

void buildLCS(int i, int j) {

	if(i  == 0 || j == 0)
		return;

	if(s[i] == t[j]) {
		res.pb(s[i]);
		buildLCS(i-1,j-1);
	}

	else if(dp[i][j-1] > dp[i-1][j]) {
		buildLCS(i,j-1);
	}

	else
		buildLCS(i-1,j);
}
int main() {

	cin >> s >> t;
	int m = s.length(), n = t.length();
	s = "?" + s;
	t = "?" + t;
	
	tik(i,0,m+1) {
		tik(j,0,n+1) {
			dp[i][j] = {0};
		}
	}

	tik(i,1,m+1) {
		tik(j,1,n+1) {
			if(s[i] == t[j]) {
				//dp[i][j].fs = dp[i-1][j-1].fs + s[i];
				dp[i][j] = dp[i-1][j-1] + 1;
			}
			else if(dp[i-1][j] > dp[i][j-1]) {
				//dp[i][j].fs = dp[i-1][j].fs;
				dp[i][j] = dp[i-1][j];
			}
			else {
				//dp[i][j].fs = dp[i][j-1].fs;
				dp[i][j] = dp[i][j-1];
			}
		}
	}

	buildLCS(m,n);
	reverse(all(res));

	for(auto ch : res)
		cout << ch;
}

Measured memory_bytes: 37120
fun main(args: Array<String>) {
    // Your code here!
    var n = readLine()!!.toInt()
    var s = readLine()!!
    var cnt = 0
    var str = s.replace("ABC",",")!!
    
    if(s != str){
        for(i in 0..str.length-1){
            if(str[i].toString() == ","){
                cnt += 1 
            } 
        }
    }
    println(cnt)
}


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

int main(){
    int N;
    int a[110];
    cin >> N;
    for (int i = 0; i < N; ++i) cin >> a[i];

    sort(a, a + N, greater<int>() );
    int Alice = 0;
    int Bob = 0;
    for (int i = 0; i < N; ++i){
        if (i % 2 == 0){
            Alice += a[i]; 
        }
        else{
            Bob += a[i];
        }

    }
    cout << Alice - Bob << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		sc.close();


		int menseki = 0,nagasa = 0;
		// 開始



		// 終了

		System.out.println(a*b+ " " +((a+b)*2) );
	}
}


Measured memory_bytes: 26312
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] s = sc.next().split("");
        Arrays.sort(s);
        for (int i = 0; i < s.length - 1; i++) {
            if (!s[i].equals(s[i + 1])) {
                continue;
            }
            System.out.println("no");
            return;
        }
        System.out.println("yes");
    }

}


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

int main(){
    string s;
    cin>>s;
    int ans=0;
    int count=0;
    int counta=0;
rep(i,3){
    if(s[i]=='S'){
        break;
    }
    count++;
}
for(int i=1;i<3;i++){
if(s[i]=='S'){
    break;
}
counta++;
}
if(count==0&&counta==0&&s[2]=='R'){
    cout<<1;
    return 0;
}
cout<<max(count,counta);

}

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

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

int64 dp[4];

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

  dp[3] = 1;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'A') {
      (dp[0] += dp[3]) %= MOD;
    } else if (s[i] == 'B') {
      (dp[1] += dp[0]) %= MOD;
    } else if (s[i] == 'C') {
      (dp[2] += dp[1]) %= MOD;
    } else {
      (dp[2] = dp[2] * 3 + dp[1]) %= MOD;
      (dp[1] = dp[1] * 3 + dp[0]) %= MOD;
      (dp[0] = dp[0] * 3 + dp[3]) %= MOD;
      (dp[3] = dp[3] * 3) %= MOD;
    }
  }

  int64 ans = dp[2];
  cout << ans << endl;
  
  return 0;
}


Measured memory_bytes: 512
#include<map>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  map<string,vector<int> > index;
  string m;
  int d;
  while(cin>>m>>d){
    index[m].push_back(d);
  }
  map<string,vector<int> >::iterator it;
  for(it = index.begin() ; it!=index.end() ; it++){
    sort( (*it).second.begin() , (*it).second.end() );
    cout<<(*it).first<<endl;
    for(int i = 0 ; i < (*it).second.size() ; i++){
      cout<<(*it).second[i]<<(i == (*it).second.size()-1?'\n':' ');
    }
  }
}

Measured memory_bytes: 0
#include <iostream>

int main(){

	int R, G, B, N;

	std::cin >> R >> G >> B >> N;

	int ans = 0;

	for(int i = 0; true; ++i){

		int r_amount = R * i;

		if(r_amount > N){
			break;
		}

		for(int j = 0; true; ++j){

			int rg_amount = r_amount + G * j;

			if(rg_amount > N){
				break;
			}

			if((N - rg_amount) % B == 0){
				++ans;
			}
		}
	}

	std::cout << ans << std::endl;

	return 0;
}


Measured memory_bytes: 888
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>
#include<unordered_map>
#include<deque>
#include<cmath>





using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double eps = 1e-5;
const ll INF = numeric_limits<int>::max();
const ll LINF = numeric_limits<ll>::max();
const ll mod = 1000 * 1000 * 1000 + 7;


void solve() {
    int k, s;
    cin >> k >> s;
    int res = 0;
    for (int x = 0; x <= k; ++x) {
        for (int y = 0; y <= k; ++y) {
            int z = s - x - y;
            if ((z >= 0) && (z <= k))
                ++res;
        }
    }
    cout << res << endl;
}


//#define LOCAL

int main() {
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    solve();



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


Measured memory_bytes: 256
#include <iostream>
#include <queue>
#include <stdlib.h>
#define INF 123456789
using namespace std;
typedef pair<int,int> P;//first:???????????¢/second:????????????
struct edge {int to,cost;};
int main() {
// AOJ0189 Dijkstra
	for (;;)
	{
		int city = 0;//?????¨???????????????????°????(0~7?????§????????????city == 8??§??????)
		int n;
		cin >> n;
		vector<edge> es[11];
		if (n == 0) break;
		for (int i = 0;i < n;i++)
		{
			int a,b,c;
			cin >> a >> b >> c;
			es[a].push_back(edge{b,c});
			es[b].push_back(edge{a,c});//????????¨???
 			city = max (max(a,b)+1,city);
		}
		int min = INF;
		int mincity;
		for (int target_city = 0;target_city < city;target_city++)
		{
			//target_city?????????????°???????????¨??????????????????????
			int d[city];
			priority_queue <P,vector<P>,greater<P> > que;
			for (int i = 0;i < city;i++)
			{
				d[i] = INF;
			}
			d[target_city] = 0;
			que.push(P(0,target_city));
			while(!que.empty())
			{
				P pp = que.top();
				que.pop();
				if(d[pp.second] < pp.first) continue;
				d[pp.second] = pp.first;
				for (int i = 0;i < es[pp.second].size();i++)
				{
					//?????????????????????????????????????????????
					if (es[pp.second][i].cost + pp.first < d[es[pp.second][i].to])
					{
						d[es[pp.second][i].to] = es[pp.second][i].cost + pp.first;
						que.push(P(d[es[pp.second][i].to],es[pp.second][i].to));
					}
				}
			}
			int sum = 0;
			for (int i = 0;i < city;i++)
			{
				sum += d[i];
			}
			if (sum < min)
			{
				min = sum;
				mincity = target_city;
			}
		}
		cout << mincity << " " << min << endl;
	}
	return 0;
}

Measured memory_bytes: 1212
import java.util.Scanner;

class Main{
	public static void main(String[] args) {



		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int[] xdata = new int[N] ;
		int[] ydata = new int[N] ;
		int[] hdata = new int[N] ;



		for(int i = 0;i < N;i++){
			xdata[i] =  scanner.nextInt();
			ydata[i] =  scanner.nextInt();
			hdata[i] =  scanner.nextInt();


		}
		for(int i= 0;i < N;i++){
			int H = 0;
			for(int x = 0;x <= 100;x++){
				for(int y = 0;y <= 100;y++){
					 H = hdata[i] + Math.abs(x-xdata[i]) + Math.abs(y - ydata[i]);
					 for(int j = i+1;j < N;j++){
						 if(hdata[j] != Math.max(H -Math.abs(x-xdata[j]) - Math.abs(y - ydata[j]) , 0)){
							 break;
						 }else if(j == N -1){
							 System.out.println(x+" "+ y+" "+H);

							 return;
						 }
						 
					 }
				}
			}
		}

	}


}

class Pair implements Comparable{
	int from;
	int end;
	@Override
	public int compareTo(Object other) {
		Pair otherpair = (Pair)other;

		return end - otherpair.end;
	}
}



Measured memory_bytes: 22100
#include <iostream>
#include <map>
#include <bitset>
#include <string>
#include <algorithm>
#include <vector>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define ALL(c) c.begin(),c.end()
#define PB push_back
#define INF 1100000000
#define LLINF 1100000000000000000
#define MOD 1000000007
#define FI first
#define SE second
#define MP make_pair

using namespace std;
typedef long long ll;

int main()
{
  ll N, x; cin >> N >> x;
  vector<ll> a(N + 1); REP(i, N) cin >> a[i + 1];
  ll ans = 0;
  REP(i, N) {
    ll d = (a[i] + a[i + 1]) - x;
    if (d <= 0) continue;
    if (a[i + 1] >= d) {
      ans += d;
      a[i + 1] -= d;
    } else {
      ans += a[i + 1];
      a[i + 1] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 1024
N, M = map(int, input().split())
A = list(map(int, input().split()))

check = ((1 / (4 * M)) * sum(A))
x = 0
for item in A:
    if item < check:
        x += 1

ans = N - x

if ans >= M :
    print('Yes')
else:
    print('No')

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

int main() {
	int A,B,X;
	cin >> X >> A >> B;
	if (B - A <= 0){
		cout << "delicious" << endl;
	}
	else if (X >= B - A){
		cout << "safe" << endl;
	}
	else{
		cout << "dangerous" << endl;
	return 0;
	}
}

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

int main(){
	
	int a,b,c;
	cin>>a>>b>>c;
	if(a>b)swap(a,b);
	if(b>c)swap(b,c);
	if(a>b)swap(a,b);
	cout<<a<<" "<<b<<" "<<c<<endl;
	
	return 0;
}


Measured memory_bytes: 1164
#include <bits/stdc++.h>
#include <fstream>
#include <vector>
#include <bitset>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <math.h>
#include <iomanip>

using namespace std;

/********** TEMPLATE STARTS HERE ***********/

#define IOS ios::sync_with_stdio(false), cin.tie(0);
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define gcd __gcd 

typedef unsigned long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <long long> vl;
typedef vector <pll> vll;
typedef vector <pii> vii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

template <typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template <typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

/*********** TEMPLATE ENDS HERE *************/

main()
{
	IOS
	
	int n;
	cin >> n;
	
	cout << n / 3;
}

Measured memory_bytes: 256
n=int(input())#ｎはデータセットの数
for i in range(n):#ｎ回繰り返す
  edge=input().split()#入力
  edge=[int(j) for j in edge]#入力
  edge.sort()#ここでedge[2]が最大になる
  if(edge[0]**2+edge[1]**2==edge[2]**2):#直角三角形かどうか
    print("YES")
  else:#そうでないなら
    print("NO")

Measured memory_bytes: 6716
#include <iostream>
using namespace std;
int main(){
int n,x,z,m,v;
cin>>n;
x=n/100;
z=n%100;
m=z/10;
v=n%10;
if(x==7)
cout<<"Yes"<<endl;
else if(m==7)
    cout<<"Yes"<<endl;
else if(v==7)
    cout<<"Yes"<<endl;
else
    cout<<"No"<<endl;


return 0;
}

Measured memory_bytes: 3100
#include <stdio.h>
int main(){
    int now=0;
    char a[10000];
    for(int i=0;i<10000;i++)a[i]='\0';
    scanf("%s",a);
    while(a[now]!='\0')now++;
    for(int i=0;i<now-8;i++)printf("%c",a[i]);
    printf("\n");
}

Measured memory_bytes: 128
//-----------------------
// author : xyqkoala
// time : 2020-06-16
//-----------------------

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

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

void solve();


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


void solve(){
	int x,n;
	cin>>x>>n;
	vector<int> nums(n);
	for(int i=0;i<n;i++) cin>>nums[i];
	unordered_set<int> t;
	for(auto &item:nums){
		t.insert(item);
	}
	
	// unordered_set<int> vis;
	for(int i=0;i<(n+1);i++){
		if(!t.count(x-i)) {
			cout<<x-i<<endl;
			return;
		}else if(!t.count(x+i)){
			cout<<x+i<<endl;
			return;
		}
	}
}


Measured memory_bytes: 3668
#include <bits/stdc++.h>
using namespace std;
    #define ll long long
int main(){
   ll n,m,s=0;
   cin>>n>>m;
   ll a[m];
   for(ll i=0;i<m;i++){
            cin>>a[i];s+=a[i];
   }
  if(n<s){
    cout<<-1<<endl;
  }
  else {
      cout<<n-s<<endl;
  }
} 

Measured memory_bytes: 3644
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <map>

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

using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main() {
    int n;
    cin >> n;
    int n2 = 1 << n;
    vector<int> s(n2);
    rep(i, n2) cin >> s[i];
    
    sort(s.begin(), s.end());
    vector<int> bs;
    bs.push_back(s.back());
    s[n2-1] = -1;
    //rep(i, n2) cout << s[i] << " ";
    //cout << endl;

    rep(i, n) {
        int news = (1 << i);
        int si = n2-1;
        vector<int> ps = bs;
        sort(ps.begin(), ps.end());
        while(si >= 0 and !ps.empty()) {
            //cout << "now si: " << si << endl;
            //rep(j, n2) {
            //    cout << "s[" << j << "]: " << s[j] << endl;
            //}
            //cout << "parents:" << endl;
            //for (auto x: ps) {
            //    cout << x << " ";
            //}
            //cout << endl;
            if (s[si] == -1) {
                --si;
                continue;
            }
            if (s[si] < ps.back()) {
                bs.push_back(s[si]);
                ps.pop_back();
                s[si] = -1;
            }
            --si;
        }
        if (!ps.empty()) {
            cout << "No" << endl;
            return 0;
        }

    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 2932
def selectionSort(A, N):
	for i in range(N):
		minj = i
		for j in range(i, N):
			if A[j][1:] < A[minj][1:]:
				minj = j
		if i != minj:
			tmp = A[i]
			A[i] = A[minj]
			A[minj] = tmp

	return A

def bubbleSort(A, N):
	for i in range(N):
		for j in range(N - 1, i, -1):
			if A[j][1:] < A[j - 1][1:]:
				tmp = A[j]
				A[j] = A[j - 1]
				A[j - 1] = tmp

	return A

if __name__ == '__main__':
	n = int(input())
	R = list(map(str, input().split()))
	C = R[:]
	SR = selectionSort(R, n)
	BR = bubbleSort(C, n)

	fStable = SR == BR

	print(" ".join(map(str, BR)))
	print("Stable")
	print(" ".join(map(str, SR)))
	if (fStable == True):
		print("Stable")
	else:
		print("Not stable")


Measured memory_bytes: 5608
# 頂点数がN(N-1)/2, 辺がN(N-2)本できる
from collections import deque

N = int(input())
matches = [[a-1 for a in map(int, input().split())] for line in range(N)]
q = deque(range(N))
depth = [0]*N
waiting = [-1]*N

while q:
  a = q.popleft()
  b = matches[a].pop()
  if waiting[b] == a:
    depth[a] = depth[b] = max(depth[a], depth[b]) + 1
    if matches[a]:
      q.append(a)
    if matches[b]:
      q.append(b)
  else:
    waiting[a] = b
    
if any(matches):
  print(-1)
else:
  print(max(depth))

Measured memory_bytes: 35848
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int num=0;
	string a,b;
	cin>>a>>b; a+=b;
	for(int i=0;i<a.size();i++)
		num=num*10+(a[i]-'0');
	int Ans=sqrt(num);
	if(Ans*Ans==num)
		cout<<"Yes";
	else cout<<"No";
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>

int N;

int p1=4;
int p2=7;

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

    int ret = N % 28;

    switch(ret) {
        case 1:
        case 2:
        case 3:
        case 5:
        case 6:
        case 9:
        case 10:
        case 13:
        case 17:
            printf("No\n");
            break;
        default:
            printf("Yes\n");
            break;
    }
}


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

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

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

PII dp[205][205];
signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;

  // [l,r]
  function<PII(int,int)> dfs = [&](int l, int r) -> PII {
    if(dp[l][r] != PII{-INF, INF}) return dp[l][r];
    if(l==r) return dp[l][r] = {s[l]-'0', s[l]-'0'};
    if(s[l]=='(' && r-l>=3) {
      PII p = dfs(l+1, r);
      chmax(dp[l][r].first, p.first);
      chmin(dp[l][r].second, p.second);
    }
    if(s[r]==')' && r-l>=3) {
      PII p = dfs(l, r-1);
      chmax(dp[l][r].first, p.first);
      chmin(dp[l][r].second, p.second);
    }
    FOR(i, l+1, r) {
      if(s[i]=='+' || s[i]=='-') {
        PII vl = dfs(l, i-1);
        PII vr = dfs(i+1, r);
        if(s[i]=='+') {
          chmax(dp[l][r].first, vl.first+vr.first);
          chmin(dp[l][r].second, vl.second+vr.second);
        } else {
          chmax(dp[l][r].first, vl.first-vr.second);
          chmin(dp[l][r].second, vl.second-vr.first);
        }
      }
    }
    return dp[l][r];
  };

  REP(i, s.size()) REP(j, s.size()) dp[i][j] = PII{-INF, INF};
  cout << dfs(0, s.size()-1).first << endl;

  // REP(i, s.size()) {
  //   FOR(j, i, s.size()) cout << dp[i][j] << " ";
  //   cout << endl;
  // }

  return 0;
}



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

using namespace std;

int main(){
    string S;
    string divide[4]={"dream","dreamer","erase","eraser"};
    cin>>S;
    reverse(S.begin(),S.end());
    for(int i=0;i<4;i++)reverse(divide[i].begin(),divide[i].end());
    bool can=true;
    for(int i=0;i<S.size();){
        bool can2=false;
        for(int j=0;j<4;j++){
            if(S.substr(i,divide[j].size())==divide[j]){
                can2=true;
                i+=divide[j].size();
            }
        }
        if(!can2){
            can=false;
            break;
        }
    }
    cout<<(can?"YES":"NO")<<endl;
}

Measured memory_bytes: 3736
#include<bits/stdc++.h>
using namespace std;
int H1,H2,M1,M2,K;
int main(){
	scanf("%d%d%d%d%d",&H1,&M1,&H2,&M2,&K);
	printf("%d\n",(H2-H1)*60+(M2-M1)-K);
}


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

int main()
{
    char b;
    cin>>b;
    if(b=='A')
        cout<<"T";
    else if(b=='T')
        cout<<"A";
        else
            if(b=='G')
            cout<<"C";
        else
            cout<<"G";
    return 0;
}


Measured memory_bytes: 256
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

# まずA, Bで総当たり、上位K個をスタック
tmp = [a+b for a in A for b in B]
tmp.sort(reverse=True)
tmp = tmp[:K]

# 残りのc個に対しても総当たり
ans = [t+c for t in tmp for c in C]
ans.sort(reverse=True)
ans = ans[:K]
for s in ans:
    print(s)


Measured memory_bytes: 161220
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,k,i,a[10005],sum=0;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<k;i++)
    {
        sum=sum+a[i];
    }

    cout<<sum<<endl;

    return 0;
}


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

int main(){
	// sengen
	string s;
	int count =0;
	
	
	// nyuryoku
	cin >> s;
    int n = s.size();
	// keisan
	if(s.size()%2 == 0){
		for(int i=0;i<n/2;i++){
			if(s.at(i) != s.at(n-1-i)){
				count++;
			}
		}
	}else{
		for(int i=0;i<n/2-1/2;i++){
			if(s.at(i) != s.at(n-1-i)){
				count++;
			}
		}
	}
	cout << count << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n, m;
    cin >> n >> m;
    int a[n];
    int total = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }
    int count = 0;
 
    int x;
    if(total%(4*m) == 0) x = total/(4*m);
    else x = total/(4*m) + 1;
 
    for(int i = 0; i < n; i++){
        if(a[i] >= x) count++;
    }
 
    //cout << x << " " << count << " " << total << endl;
 
    if(count >= m) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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

using namespace std;

#define ll long long

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

  int maxr = r[n-1];
  int ans = -1000000000;
  for (int i = n-2; i >= 0; i--) {
    int d = maxr - r[i];
    ans = max(ans, d);
    maxr = max(maxr, r[i]);
  }

  cout << ans << endl;
}

Measured memory_bytes: 3864
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;


void main() {
    int n;
    scan(n);
    auto h = readln.split.to!(int[]);

    int a = 0, b = abs(h[1] - h[0]);

    foreach (i ; 2 .. n) {
        int t = b;
        b = min(b + abs(h[i] - h[i-1]), a + abs(h[i] - h[i-2]));
        a = t;
    }

    writeln(b);
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

Measured memory_bytes: 4256
from collections import Counter
ls=[]
def rec(n):
    if len(str(n))>9: return
    ls.append(n)
    rec(n*10+3)
    rec(n*10+5)
    rec(n*10+7)
n=int(input())
rec(3)
rec(5)
rec(7)
cnt=0
for l in ls:
    temp=Counter(str(l))
    if l<=n and len(temp.keys())==3:
        cnt+=1
print(cnt)

Measured memory_bytes: 10372
#include <vector>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>


using namespace std;


int main(void){
  
  vector<int>A;
  int K,N,ans=0,max=0;
  
  
  cin >> K >> N;

  for(int i = 0; i< N;i++){
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
  }

  for(int i = 0; i< N-1;i++){
    int tmp;
    tmp=A.at(i+1) - A.at(i);
    if(tmp > max){
      max = tmp;
    }
  }
  
  if(max < K -A.at(N-1) + A.at(0)){
      max = K -A.at(N-1) + A.at(0);
    }
    ans  = K - max;

   
  cout << ans << endl;
  

  

  return 0;
}




Measured memory_bytes: 1528
#include<bits/stdc++.h>
using namespace std;
long long n,m;
long long t[100010];
long long p;
struct P
{
    long long x,y;
}He[100010];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>t[i];
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    
{
	
        cin>>He[i].x>>He[i].y;
    }
    for(int i=1;i<=m;i++)
    {
        long long sum=0;
        for(int j=1;j<He[i].x;j++)
        {
            sum+=t[j];
         //   cout<<sum<<"A"<<endl;
        }
        sum+=He[i].y;
       // cout<<sum<<"B"<<endl;
        for(int j=He[i].x+1;j<=n;j++)
        {
            sum+=t[j];
         //   cout<<sum<<"C"<<endl;
        }
        cout<<sum<<endl;
    }
    return 0;
}

Measured memory_bytes: 256
l, r = map(int, input().split())
ans = 2020
if l <= (l // 2019 + 1) * 2019 <= r:
    ans = 0
else:
    for i in range(l, r):
        for j in range(i + 1, r + 1):
            tmp = i * j % 2019
            ans = tmp if tmp < ans else ans
            if ans == 0:
                break
print(ans)


Measured memory_bytes: 2940
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string Shf(string s, int n){
    string t = s.substr(0,n);
    s.erase(0,n);
    s += t;
    return s;
}
int main(){
    string s;
    int m,n;
    while(1){
        cin >> s;
        if (s == "-") break;
        cin >> m;
        for(int i = 0;i < m; i++){
            cin >> n;
            s = Shf(s,n);
        }
        cout << s << endl;
    }
    return 0;
}


Measured memory_bytes: 3172
def gcd(x,y):
    while y:
        x, y = y, x % y
    return x


def lcm(x,y):
    return x * y // gcd(x,y)


while True:
    try:
        a = list(map(int,input().split()))
        print(gcd(a[0],a[1]),lcm(a[0],a[1]))
    except:
        break


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

typedef long long ll;
#define reg register
#define rep(i,a,b) for(reg int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(reg int i=a,i##end=b;i>=i##end;--i)

char IO;
int rd(){
	int s=0,f=0;
	while(!isdigit(IO=getchar())) if(IO=='-') f=1;
	do s=(s<<1)+(s<<3)+(IO^'0');
	while(isdigit(IO=getchar()));
	return f?-s:s;
}

const int N=52,P=1e9+7;

int n,k;
ll dp[N][N][N*N+100];

int main(){
	n=rd(),k=rd();
	if((k&1)||(k>n*n/2)) return puts("0"),0;
	dp[0][0][1250]=1;
	rep(i,0,n-1) rep(j,0,i) rep(s,1250-n*n/2,1250+n*n/2) {
		(dp[i+1][j][s]+=(2*j+1)*dp[i][j][s])%=P;
		if(s>=2*i+2) (dp[i+1][j+1][s-2*i-2]+=dp[i][j][s])%=P;
		if(j&&s+2*i+2<=2500) (dp[i+1][j-1][s+2*i+2]+=dp[i][j][s]*j*j%P)%=P;
		//cout<<i<<' '<<j<<' '<<s<<' '<<dp[i][j][s]<<endl;
	}
	//rep(i,0,8) cout<<dp[4][0][i+1250]<<endl;
	printf("%lld\n",dp[n][0][k+1250]);
}



















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

public class Main {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        int n = sc.nextInt();
        int wmax = sc.nextInt();
        int[] w = new int[n];
        long[] v = new long[n];
        for (int i = 0; i < n; i++) {
            w[i] = sc.nextInt();
            v[i] = sc.nextLong();
        }
        long[][] dp = new long[n + 1][wmax + 1];

        dp[0][0] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= wmax; j++) {
                if (j - w[i - 1] < 0) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
                }
            }
        }
        System.out.println(dp[n][wmax]);
    }
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte())
            return buffer[ptr++];
        else
            return -1;
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr]))
            ptr++;
        return hasNextByte();
    }

    public String next() {
        if (!hasNext())
            throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext())
            throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
            throw new NumberFormatException();
        return (int) nl;
    }

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


Measured memory_bytes: 127056
#include <bits/stdc++.h>
        using namespace std;
   #define int long long
const int MAX = 510000;
const int MOD = 1000000007;

     signed main(){
        int h,w,k2;
        cin>>h>>w>>k2;
        int x1,y1,x2,y2;
        cin>>x1>>y1;x1--;y1--;
        cin>>x2>>y2;x2--;y2--;
        queue<pair<int,int>> que,que2;
        bool used[h][w],used2[h][w];
           for(int i=0;i<h;i++){fill(used[i],used[i]+w,false);fill(used2[i],used2[i]+w,false);}
        string c[h];
        for(int i=0;i<h;i++)cin>>c[i];
        que.push({x1,y1});
        used[x1][y1]=true;
        int t=0;
        while(!que.empty()){
             t++;
             while(!que.empty()){
                  int l=que.front().first,r=que.front().second,k;
                  que.pop();
                  used2[l][r]=true;
                  k=1;
                  //cerr<<l<<' '<<r<<endl;
               while(l-k>=0&&c[l-k][r]!='@'&&!used2[l-k][r]&&k<=k2){
                    if(l-k==x2&&r==y2){
                         cout<<t<<endl;
                         return 0;
                    }
                    if(!used[l-k][r]){
                           used[l-k][r]=true;
                    que2.push({l-k,r});
                    }
                    used[l-k][r]=true;
                    que2.push({l-k,r});
                    k++;
               }
               k=1;
               while(l+k<h&&c[l+k][r]!='@'&&!used2[l+k][r]&&k<=k2){
                 //   cerr<<l+k<<' '<<r<<' '<<c[l+k][r]<<endl;
                     if(l+k==x2&&r==y2){
                         cout<<t<<endl;
                  
                         return 0;
                    }
                     if(!used[l+k][r]){
                    used[l+k][r]=true;
                    que2.push({l+k,r});
                     }
                    k++;
               }
               k=1;
               while(r+k<w&&c[l][r+k]!='@'&&!used2[l][r+k]&&k<=k2){
                     if(l==x2&&r+k==y2){
                         cout<<t<<endl;
                         return 0;
                    }
                     if(!used[l][r+k]){
                    used[l][r+k]=true;
                    que2.push({l,r+k});
                     }
                    k++;
               }
               k=1;
               while(r-k>=0&&c[l][r-k]!='@'&&!used2[l][r-k]&&k<=k2){
                     if(l==x2&&r-k==y2){
                         cout<<t<<endl;
                         return 0;
                    }
                     if(!used[l][r-k]){
                    used[l][r-k]=true;
                    que2.push({l,r-k});
                     }
                    k++;
               }
             }
            // cerr<<que2.size()<<endl;
             swap(que,que2);

        }
        cout<<-1<<endl;
     }


 


 






    





      

        


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

int main(){
    string ss;
    int num[128];
    num['M'] = 1000;
    num['D'] = 500;
    num['C'] = 100;
    num['L'] = 50;
    num['X'] = 10;
    num['V'] = 5;
    num['I'] = 1;
    while( cin >> ss ){
        string::iterator it = ss.begin();
        int answer = 0;
        while( it != ss.end() ){
            if( it+1 == ss.end() ){
                answer += num[ *it ];
            }else if( num[ *it ] < num[ *(it+1) ] ){
                answer += num[ *(it+1) ] - num[ *it ];
                it++;
            }else{
                answer += num[ *it ];
            }
            it++;
        }
        cout << answer << endl;
    }
    return 0;
}

Measured memory_bytes: 1180
h, w = map(int, input().split())
s = [['.'] * (w + 2)]
s += [['.'] + list(input()) + ['.'] for _ in range(h)]
s += [['.'] * (w + 2)]
t = s
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == '.':
            n = 0
            for k in range(3):
                n += s[i - 1 + k][j - 1:j + 2].count('#')
            t[i][j] = str(n)
for i in range(1, h + 1):
    tt =''
    for j in range(1, w + 1):
        tt += t[i][j]
    print(tt)


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

long long arr[200005];

int main(){
    long long n,t;
    long long now;
    while(cin>>n>>t){
        for(long long i=0;i<n;i++){
            cin>>arr[i];
        }
        now=arr[0];
        long long last=now+t;
        long long ans=0;
        for(long long i=1;i<n;i++){
            if(last<=arr[i]){
                ans+=last-now;
                now=arr[i];
                last=arr[i]+t;
            }
            else{
                last=arr[i]+t;
            }
        }
        cout<<ans+last-now<<endl;
    }
}


Measured memory_bytes: 1792
while True:
    a, op, b = input().split()
    a, b = map(int, [a, b])
    if op == '-':
        rs = a - b
    elif op == '*':
        rs = a * b
    elif op == '/':
        rs = a // b
    elif op == '+':
        rs = a + b
    elif op == '?':
        break
    print(rs)


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

int main(){
  int H,W,K;
  cin >> H >> W >> K;
  vector<string> vs(H);
  for(int i=0;i<H;i++){
    cin >> vs.at(i);
  }
  int ans=0;
  for(int i=0;i<(1<<H);i++){
    bitset<6> th(i);
    for(int j=0;j<(1<<W);j++){
      bitset<6> tw(j);
      int count=0;
      for(int k=0;k<H;k++){
        if(th.test(k)){
          continue;
        }
        for(int l=0;l<W;l++){
          if((vs.at(k).at(l)=='#')&&(!tw.test(l))){
            count++;
          }
        }
      }
      if(count==K){
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
#define debug(x) cout<<#x<<"::"<<x<<endl;
using namespace std;
typedef long long ll;

void solve()
{
	string s1;
	string s2;
	cin>>s1;
	cin>>s2;
	//debug(s1);debug(s2);
	ll n1=s1.length();ll n2=s2.length();
	vector<vector<ll> >dp(n1+1,vector<ll>(n2+1));
	for(ll i=1;i<=n1;i++)
	{
		for(ll j=1;j<=n2;j++)
		{
			if(s1[i-1]==s2[j-1])
				dp[i][j]=dp[i-1][j-1]+1;
			else
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
		}
	}
	//debug(dp[n1][n2]);
	string ans;
	ll i=n1,j=n2;
	while(ans.size()!=dp[n1][n2])
	{
		if(s1[i-1]==s2[j-1])
		{
			ans+=s1[i-1];
			i--;
			j--;
		}
		else
		{
			ll x=dp[i][j];
			if(dp[i][j-1]==x)
			{
				j--;
			}
			else
			{
				i--;
			}
		}
	}
	reverse(ans.begin(),ans.end());
	cout<<ans<<'\n';

}

int main()
{
	int t;
	t=1;
	while(t--)
		solve();
	return 0;
}

Measured memory_bytes: 70784
n,m=map(int,input().split())
a=[int(input()) for _ in range(m)]
if n==1:
    print(1)
    exit()
for i in range(m-1):
    if a[i+1]-a[i]==1:
        print(0)
        exit()
ans=1
a.append(n+1)
for i in range(m+1):
    if i==0:
        l=[1,1]
        for j in range(1,a[i]-1):
            l.append(l[-1]+l[-2])
        ans*=l[-1]
        ans%=(10**9 +7)
        #print(l)
    else:
        l=[1,1]
        for j in range(a[i-1]+2,a[i]-1):
            l.append(l[-1]+l[-2])
        ans*=l[-1]
        ans%=(10**9 +7)
        #print(l)
print(ans)

Measured memory_bytes: 466056
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<utility>
#include<functional>
#include<cfenv>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define vint vector<int>
#define vvint vector<vint>
#define P pair<int,int>
#define INT_MAX 2147483647
#define MOD 1000000007
#define PI 3.14159265358979323846
using namespace std;
typedef long long ll;

#define MAX 1000000000000000000

int main(void) {
	ll n; cin >> n;
	ll ans = 1;
	bool flag=0;
	for (int i = 0; i < n;i++) {
		ll x; cin >> x;
		if (x == 0) {
			cout << 0 << endl;
			return 0;
		}
		if (double(MAX / x) >= ans) { ans *= x; }
		else {
			flag = 1;
		}
	}
	if (flag == 1) {
		cout << -1 << endl;
	}
	else {
		cout << ans << endl;
	}

	return 0;
}

Measured memory_bytes: 3672
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
using namespace std;

#define pb push_back
#define All(a) a.begin(), a.end()
#define met(a, b) memset(a, b, sizeof(a))
#define rep1(i, l, r) for(int i = l; i <= r; ++i)
#define rep2(i, l, r) for(int i = l; i < r; ++i) 
#define per(i, r, l) for(int i = r; i >= l; --i)
typedef long long ll;
typedef double db;
typedef long double ldb;
const int NIL = -1;
const int oo = 0x3f3f3f3f;
const ll INF = 2e18;
const db eps = 1e-8;
const db PI = acos(-1.0);

inline int read(void)
{
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9')	{ if(ch == '-') f = -1; ch = getchar(); } 
	while(ch >= '0' && ch <= '9') {	x = (x << 1) + (x << 3) + ch - '0';	ch = getchar();	}
	return x * f;
}

inline void write(int x)
{
	if(x < 0) { putchar('-'); x = -x; }
    if(x>=10) write(x/10); 
	putchar(x%10+'0'); 
}

int T[10];

int main(void)
{
	T[0] = read();
	T[1] = read();
	T[2] = read();
	sort(T, T+3);
	if(T[0] == 5 && T[1] == 5 && T[2] == 7) cout<<"YES";
	else cout<<"NO"; 
	
	
	return 0;
} 

Measured memory_bytes: 256
N = int(input())
A = list(map(int, input().split(" ")))

if 0 in A:
        print(0)
        exit()

x = 1
for i in range(N):
    x = x * A[i]
        
    if x > 10 ** 18:
        print(-1)
        exit()

print(x)


Measured memory_bytes: 21596
#include <bits/stdc++.h>
#include <ext/rope>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
 
#define file ""
 
#define all(x) x.begin(), x.end()
 
#define sc second
#define fr first
 
#define pb push_back
#define mp make_pair
 
#define pss pair<line*, line*>
 
using namespace std;
using namespace __gnu_cxx;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
                                 
const ll inf = 1e18;
const int MOD = 1e9 + 7;
 
const int dx[] = {-1, +1, -2, +2, -2, +2, -1, +1};
const int dy[] = {-2, -2, -1, -1, +1, +1, +2, +2};
 
int const N = 3e5 + 5;
int const M = 1e8	;

int main() {                 
#ifdef Mask                                                         
  freopen(".in", 	"r", stdin);
  freopen(".out", "w", stdout);  
#endif
	ios_base :: sync_with_stdio(false);               
  cin.tie(nullptr);
  srand(time(nullptr));	
  ll n;
  cin >> n;
  ll a[n + 1];
  map<ll,  ll> cnt;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
  	cin >> a[i];
  	cnt[a[i]]++;
  	sum += a[i];
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
   	int a, b;
   	cin >> a >> b;
   	sum -= cnt[a] * a;
   	cnt[b] += cnt[a];
   	sum += cnt[a] * b;
   	cnt[a] = 0;
   	cout << sum << endl;
  }
	return 0;                    
 
}

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

using namespace std;

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

    int h[n];
    for(int i = 0; i < n; i++)
    {
        cin >> h[i];
    }
    int a[n] = {};
    a[1] = abs(h[1] - h[0]);
    for(int i = 2; i < n; i++)
    {
        a[i] = min((a[i - 1] + abs(h[i] - h[i - 1])), (a[i - 2] + abs(h[i] - h[i - 2])));
    }
    cout << a[n - 1];
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int H, W;
  cin >> H >> W;
  for ( int i = 0 ; i < W + 2 ; i++ ) {
    cout << "#" ;
  }
  cout << endl;
  for ( int i = 0 ; i < H ; i++ ) {
    cout << "#" ;
    for ( int k = 0 ; k < W ; k++ ) {
      char A;
      cin >> A;
      cout << A;
    }
    cout << "#" << endl;
  }
  for ( int i = 0 ; i < W + 2 ; i++ ) {
    cout << "#" ;
  }  
  cout << endl;
}

Measured memory_bytes: 256
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    x, y = map(int, input().split())

    if y % 2 == 1:
        print('No')
    elif x * 2 > y:
        print('No')
    elif x * 4 < y:
        print('No')
    else:
        print('Yes')


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()


Measured memory_bytes: 65724
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
//using static System.Globalization.CultureInfo;

class Program
{
    static void Main(string[] args)
    {
        var hwd = Input.ar;
        var ar = Enumerable.Repeat(0, hwd[0]).Select(v => Input.ar).ToArray();
        var pair = new KeyValuePair<int, int>[hwd[0] * hwd[1] + 1];
        for (var i = 0; i < hwd[0]; i++)
            for (var j = 0; j < hwd[1]; j++)
                pair[ar[i][j]] = new KeyValuePair<int, int>(i, j);
        var sum = new long[hwd[0] * hwd[1] + 1];
        for(var i = hwd[2]+1; i <= hwd[0] * hwd[1]; i++)
            sum[i] = sum[i - hwd[2]] + Abs(pair[i].Key - pair[i - hwd[2]].Key) + Abs(pair[i].Value - pair[i - hwd[2]].Value);
        var qur = Input.num;
        var res = new long[qur];
        for(var i = 0; i < qur; i++)
        {
            var lr = Input.ar;
            res[i] = sum[lr[1]] - sum[lr[0]];
        }
        WriteLine(string.Join("\n", res));
    }
}

public class Input
{
    public static string read => ReadLine();
    public  static int[] ar => Array.ConvertAll(read.Split(' '), int.Parse);
    public  static int num => ToInt32(read);
    public  
static long[] arL => Array.ConvertAll(read.Split(' '), long.Parse);
    public  static long numL => ToInt64(read);
    public static int[][] twodim(int num)
        => Enumerable.Repeat(0, num).Select(v => ar).ToArray();
    public static long[][] twodimL(int num)
        => Enumerable.Repeat(0, num).Select(v => arL).ToArray();
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}


Measured memory_bytes: 24120
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Main {
    string S, T;
    void input() { cin >> S >> T; }
    void output() {
        int s = S.size(), t = T.size();
        int res = 1e9;
        REP(i, s - t + 1) {
            int cnt = 0;
            REP(j, t) { cnt += (S[i + j] != T[j]); }
            res = min(res, cnt);
        }
        cout << res << endl;
    }

public:
    Main() {
        input();
        output();
    }
};

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

    Main();

    return 0;
}


Measured memory_bytes: 3660