akhauriyash/Code-Regression huggingface dataset:
import java.io.*;
import java.util.*;

public class Main {
    void run() {
        try {
            for (String line; (line = in.readLine()) != null; ) {
                StringTokenizer tokenizer = new StringTokenizer(line);
                int[] c = new int[8];
                for (int i = 0; i < 8; i++) {
                    c[i] = Integer.parseInt(tokenizer.nextToken());
                }
                List<Integer> cap = Arrays.asList(4, 1, 4, 1, 2, 1, 2, 1);
                int minVal = Integer.MAX_VALUE;
                List<Integer> minCap = null;
                for (int i = 0; i < 8; ++i) {
                    Collections.rotate(cap, -1);
                    int s = 0;
                    for (int j = 0; j < 8; j++) {
                        if (cap.get(j) < c[j]) s += c[j] - cap.get(j);
                    }
                    if (minCap == null || minVal > s || (minVal == s && capToString(minCap) > capToString(cap))) {
                        minVal = s;
                        minCap = new ArrayList<Integer>(cap);
                    }
                }
                sysout.println(join(minCap, " "));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    int capToString(List<Integer> cap) {
        StringBuilder bld = new StringBuilder();
        for (int val : cap) {
            bld.append(val);
        }
        return Integer.parseInt(bld.toString());
    }

    <T> String join(Collection<T> c, String delim) {
        StringBuilder bld = new StringBuilder();
        boolean first = true;
        for (T item : c) {
            if (!first) bld.append(delim);
            first = false;
            bld.append(item);
        }
        return bld.toString();
    }

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

    // flush automatically iff you call `println` or `printf` or `format`.
    PrintWriter sysout = new PrintWriter(System.out, true);

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer buffer = new StringTokenizer("");

    String read() {
        if (!buffer.hasMoreTokens()) {
            try {
                buffer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return buffer.nextToken();
    }

    int readInt() {
        return Integer.parseInt(read());
    }

    long readLong() {
        return Long.parseLong(read());
    }

    double readDouble() {
        return Double.parseDouble(read());
    }

    String readLine() {
        buffer = new StringTokenizer("");
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Measured memory_bytes: 18744
use std::io::prelude::*;
use std::io::stdin;
use std::str::FromStr;

fn main() {
    let n: String = read();
    println!("{}", if n.find('9').is_some() { "Yes" } else { "No" });
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let token: String = stdin
        .lock()
        .bytes()
        .map(|b| b.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


Measured memory_bytes: 4352
#define ENABLE_DEBUG 1
// hidden common codes{{{
#include<bits/stdc++.h>
#define ALL(v) std::begin(v),std::end(v)
#define LOOP(k) for(lint ngtkana_is_a_genius=0; ngtkana_is_a_genius<(lint)k; ngtkana_is_a_genius++)
using lint=long long;
using ld=long double;
template<class T> using numr=std::numeric_limits<T>;
struct input_t {
    template<class T> operator T() {
        T t;
        std::cin>>t;
        return t;
    }
} input;
#ifdef NGTKANA
#include<debug.hpp>
#else
#define DEBUG(...)(void)0
#endif
/*}}}*/

auto cmx=[](auto&x,auto y){if(x<y){x=y;return true;}return false;};

template<std::size_t N,class T,class...Args>auto mkvec(std::size_t sz,Args...args){
    if constexpr(N==1)return std::vector<T>(sz,args...);
    else return std::vector(sz,mkvec<N-1,T>(args...));
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    lint n=input,m=input,q=input;
    std::vector<std::tuple<lint,lint,lint,lint>>data(q);
    auto score=mkvec<2,lint>(m,m);
    for(auto&&t:data){
        lint a=input,b=input,c=input,d=input;
        a--,b--;
        t=std::tuple(a,b,c,d);
    }

    std::vector<std::vector<lint>>box;
    auto dfs=[&](auto&&f, std::vector<lint> now){
        if ((lint)now.size() == n){
            box.push_back(std::move(now));
            return;
        }

        lint last=now.empty()?1:now.back();
        for(lint i=last;i<=m;i++){
            now.push_back(i);
            f(f, now);
            now.pop_back();
        }
    };
    dfs(dfs, std::vector<lint>{});

    lint ans=0;
    for(auto&&v:box){
        lint sum=0;
        for(auto[a,b,c,d]:data){
            if (v.at(b) - v.at(a) == c) {
                sum+=d;
            }
        }
        cmx(ans, sum);
    }
    std::cout<<ans<<'\n';
}


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

using namespace std;
#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()

struct team {
  int id,s,rank;
};

bool comp(const team& a, const team& b){
  if(a.s!=b.s) return a.s > b.s;
  return a.id < b.id;
}

bool comp2(const team& a, const team& b){
  return a.id < b.id;
}

int main(){
  int i,s;
  vector<team> ts(200);
  rep(i,200){
    ts[i].s=0;
    ts[i].id=i;
  }
  
  while(scanf("%d,%d", &i, &s)){
    if(i==0&&s==0) break;
    ts[i].s+=s;
  }

  sort(all(ts),comp);

  rep(i,10){
    //cout << ts[i].id << " " << ts[i].s << endl;
  }
  
  ts[0].rank=1;
  int r=1;
  loop(i,1,200){
    if(ts[i].s==ts[i-1].s){
      ts[i].rank = r;
    } else {
      r++;
      ts[i].rank = r;
    }
  }

  rep(i,10){
    // cout << ts[i].id << " " << ts[i].s << " " << ts[i].rank << endl;
  }
  
  sort(all(ts),comp2);  

  int q;
  while(cin>>q){
    cout << ts[q].rank << endl;
  }

}

Measured memory_bytes: 1264
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<((int)(n));i++)
#define reg(i,a,b) for(int i=((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=((int)(b));i>=((int)(a));i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX


lli gcn(lli p,lli q,lli d){
	return q/d-(p-1)/d;
}

void get(lli p,lli q,lli& rsl,lli& zan){
	lli fbn=gcn(p,q,15);
	lli fn=gcn(p,q,3)-fbn;
	lli bn=gcn(p,q,5)-fbn;

	rsl = (fbn*2+fn+bn)*4;
	zan = q-p+1 -fbn-fn-bn;
}

lli getallen(lli p){ //0..p
	if(p==0)return 0;
	lli b=1,bl=1;
	lli r,z;
	lli res=0;
	while(b*10<=p){
		get(b,b*10-1,r,z);
		res += r+z*bl;
		b*=10;
		bl++;
	}
	get(b,p,r,z);
	res += r+z*bl;
	return res;
}

string tostr(lli p){
	char buf[100];
	sprintf(buf,"%lld",p);
	return string(buf);
}

string solve(lli a){
	lli l=0,r=a+5;
	while(l+1<r){
		lli t=(l+r)/2;
		if(getallen(t)>=a)r=t;
		else l=t;
	}
	
	a-=getallen(l);
	lli st=l+1;
	//printf("st .. %lld, a .. %lld\n",st,a);
	
	string ns="";
	for(;;){
		if(st%3==0){
			if(st%5==0)ns+="FizzBuzz";
			else ns+="Fizz";
		}
		else{
			if(st%5==0)ns+="Buzz";
			else ns+=tostr(st);
		}
		if(ns.size()>a+25)break;
		st++;
	}
	
	return ns.substr(a,20);
}

int main(void){
	
	//printf("%lld %lld %lld\n",getallen(1),getallen(2),getallen(3));
	lli a;
	cin >> a; a--;
	cout << solve(a) << endl;
	return 0;
}

Measured memory_bytes: 1244
from collections import deque
N,M = map(int,input().split())

adj = [list() for _ in range(N)]

for _ in range(M):
    a,b = map(int,input().split())
    adj[a-1].append(b-1)
    adj[b-1].append(a-1)

parent = [None]*N
queue = deque([0])
while queue:
    u = queue.popleft()
    for v in adj[u]:
        if parent[v] is None:
            parent[v] = u
            queue.append(v)

print('Yes')
print(*(x+1 for x in parent[1:]), sep='\n')

Measured memory_bytes: 38720
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

int main()
{
	int N, R;
	cin >> N >> R;
	if (N >= 10) cout << R << endl;
	else cout << R + 100 * (10 - N) << endl;
}


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

int main() {
  
  int A, B;
  cin >> A >> B;
  
  if (A > 12) {
    cout << B << endl;
  }
  else if (A >= 6) {
    cout << B/2 << endl;
  }
  else {
    cout << 0 << endl;
  }
  
  

return 0;
}

Measured memory_bytes: 3616
#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <functional>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <queue>
#include <map>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long ll;
typedef pair<int, int> P;
const vector<int> di = {-1, 0, 1, 0};
const vector<int> dj = { 0, 1, 0, -1 };



int main() {

	ll n;
	cin >> n;
	ll k;
	cin >> k;
	vector<ll> h(n);
	rep(i, n) cin >> h.at(i);

	sort(h.begin(), h.end());
	reverse(h.begin(), h.end());

	ll ans = 0;
	rep(i, n) {
		if (k > 0) {
			k--;
			continue;
		}
		else {
			ans += h.at(i);
		}
	}
	cout << ans << endl;

	
	return 0;
}

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

int main(void)
{
    long N;
    cin >> N;
    long long A[N], t, count=0;
    for(long i=0; i<N; i++)
    {
        cin >> t;
        A[i] = t;
    }

    for(long i=1; i<N; i++){
        if(A[i-1]>A[i])
        {
            long long dif = abs(A[i-1]-A[i]);
            A[i] += dif;
            count += dif;
        }
    }

    cout << count << endl;

    return 0;
}

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

int main(){
    int turu,kame,kazu,asi;
    cin >> kazu >> asi;

    for(int i=0;i<=kazu;i++){
        turu=i*2;
        kame=(kazu-i)*4;
        if(turu+kame == asi){
            cout << "Yes" << endl;
            return 0;
        }
    }

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


Measured memory_bytes: 3632
line = input().split(" ")
a = 0
while a<1:
    line[2] = int(line[2]) - int(line[1])
    if int(line[2])<=0:
        print("Yes")
        break
    line[0] = int(line[0]) - int(line[3])
    if int(line[0])<=0:
        print("No")
        break

Measured memory_bytes: 9132
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
/************************************************/
#define rep(i,n) for(int i=0;i<n;i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i,a,n) for(int i=a;i<n;i++)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(),s.end()
#define zero(x) memset(x,0,sizeof(x))
#define vii vector<pair<int,int> >
#define mpis map<int,string>
#define mpii map<int,int>
#define mpsi map<string,int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get(){
	char c=getchar();
	long long x=0LL;
	while(c<'0'||c>'9')
	c=getchar();
	while(c>='0'&&c<='9'){
		x*=10LL;
		x+=(c-'0');
		c=getchar();
	}
	return x;
}
string i_t_s(int x){
	string t="";
	while(x){
		t+=x%10+'0';
		x/=10;
	}
	reverse(all(t));
	re t;
}
int s_t_i(string t){
	int x=0;
	rep(i,sz(t)){
		x=x*10+(t[i]-'0');
	}
	re x;
}
string s;
int main(){
	ios::sync_with_stdio(0);
	cin>>s;
	if(s[sz(s)-1]=='s')
	s+="es";
	else
	s+="s";
	cout<<s;
	re 0;
}

Measured memory_bytes: 3644
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N,A;
  int T1,T2,T3;

  cin>>N>>A;

  T1=N/500;
  T2=N-T1*500;

  if(A>=T2)
    cout << "Yes" << endl;  
  else
    cout << "No" << endl;

}

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

int cmpnum(const void * n1, const void * n2)
{
	if (*(long *)n1 > *(long *)n2)
	{
		return 1;
	}
	else if (*(long *)n1 < *(long *)n2)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int main(void) {

  long n;
  scanf("%ld", &n);
  long x[n];
  long sort[n];
  for (long i = 0; i < n; i++) {
    scanf("%ld", &x[i]);
    sort[i] = x[i];
  }
  qsort(sort, n, sizeof(long), cmpnum);
  for (long i = 0; i < n; i++) {
    if (x[i] < sort[n/2]) {
      printf("%ld\n", sort[n/2]);
    } else {
      printf("%ld\n", sort[n/2-1]);
    }
  }

  return 0;
}

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

#define     fast()              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
#define     ll                  long long int
#define     test()              ll t; cin>>t; while(t--) 
#define     lp0(i,begin,end)    for(ll i=begin;i<end;i++)
#define     lp1(i,begin,end)    for(ll i=begin;i<=end;i++)
#define     rlp(i,begin,end)    for(ll i=end;i>=begin;i--)
#define     prec(n)             fixed<<setprecision(n)
#define     initial(a,i)        memset(a,i,sizeof(a))
#define     pb                  push_back
#define     PI                  3.1415926535897932384626433832795
#define     MOD                 1000000007
#define     F                   first
#define     S                   second
#define     all(a)              (a).begin(),(a).end()
#define     BPC(x)              __builtin_popcountll(x)
#define     gcd(a,b)            __gcd(a,b)

ll gcd(ll a,ll b) {if (a==0) return b;return gcd(b%a,a);}

ll power(ll x,ll n)
{
    ll result = 1;
    while (n)
    {
        if (n & 1)
            result = result * x;
        n = n / 2;
        x = x * x;
    }
    return result;
}
// Solution Function
void solution()
{
    // cout<<compte<<"\n";
    ll n;
    cin>>n;
    ll cnt=0;
    ll arr[n][2];
    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        arr[i][0]=a;
        arr[i][1]=b;
      
    }

    for(ll i=0;i<n;i++)
    {
        if((i+2)<n &&(arr[i][0] == arr[i][1]) && (arr[i+1][0]==arr[i+1][1]) && (arr[i+2][0]==arr[i+2][1])  )
        {
            puts("Yes");
            cout<<"\n";
            return;
        }

    }
    puts("No");
    cout<<"\n";
}
// Driver Function
int main()
{
    ll compte = 1;
   // test()
    //{
        solution();
        compte++;
    //}
    return 0;
}


Measured memory_bytes: 3632
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<queue>

#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 all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define INF 2e9
#define MOD 1000000007

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
typedef std::pair<ll, ll> P;
int gcd(int a,int b){return b?gcd(b,a%b):a;}


using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    if(a == b && b == c){
        cout << "Yes";
    }else{
        cout << "No";
    }
}

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

using ll = long long;

const int mod = 1000000007;
const int INF = 1000000000;
const double EPS = 1e-9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> p(n);
    for (int i = 0; i < n; ++i) cin >> p[i];
    int ans = 0;
    int mi = p[0];
    for (int i = 0; i < n; ++i) {
        if (mi >= p[i]) ++ans;
        mi = min(p[i], mi);
    }
    cout << ans << endl;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
 
int main(){
  int N;
  cin>>N;
  vector<int> A(N);
  rep(i,N) cin>>A[i];
  ll ans=0;
  for(int i=1;i<N;i++){
    while(A[i-1]>A[i]){
      A[i]++;
      ans++;
    }
  }
  cout<<ans<<endl;
}

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

int main() {
  int a,b,c,k;
  cin>>a>>b>>c>>k;
  int ans;
  if(a>k){
    ans=k;
  }else if(a+b>=k){
    ans=a;
  }else{
    ans=a-(k-a-b);
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3632
N = 9
PRR = [1, 2, 4, 9, 5, 8, 7, 3, 6]

N = 5
PRR = [1, 2, 3, 4, 5]

#
# N = 2
# PRR = [2, 1]
#
# N = 2
# PRR = [1, 2]
#
# N = 5
# PRR = [1, 4, 3, 5, 2]

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


def calculate(n, prr):
    result = 0
    for i in range(n - 1):
        if prr[i] == (i + 1):
            prr[i], prr[i + 1] = prr[i + 1], prr[i]
            result += 1

    if prr[n - 1] == n:
        result += 1
    print(result)


calculate(N, PRR)


Measured memory_bytes: 20620
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstdint>
//#include<>
using namespace std;

typedef int64_t i64;typedef int32_t i32;
typedef int16_t i16;typedef int8_t i8;
typedef uint64_t ui64;typedef uint32_t ui32;
typedef uint16_t ui16;typedef uint8_t ui8;

typedef i32 int__;
#define rep(i,j) for(int__ i=0;i<j;i++)
#define repeat(i,j,k) for(int__ i=(j);i<(k);i++)
#define repeat_eq(i,j,k) for(int__ i=(j);i<=(k);i++)
#define all(v) begin(v),end(v)

const i32 INF=1<<30;//=10E10

int main()
{
  int n,q;cin>>n>>q;
  typedef pair<string,int>psi;
  queue<psi> que;
  int atime=0;
  rep(i,n){
    psi t;
    cin>>t.first>>t.second;
    t.second-=q;
    atime+=q;
    if(t.second>0)
      que.push(t);
    else{
      atime+=t.second;
      cout<<t.first<<" "<<atime<<endl;
    }
  }
  while(!que.empty()){
    psi t=que.front();que.pop();
    t.second-=q;
    atime+=q;
    if(t.second>0)
      que.push(t);
    else{
      atime+=t.second;
      cout<<t.first<<" "<<atime<<endl;
    }
  }
  return 0;
}

Measured memory_bytes: 2296
import java.util.*;

public class Main {

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

	public void run() {
		Scanner sc = new Scanner(System.in);
		int s = sc.nextInt();
		int w = sc.nextInt();
		if(s<=w) {
			System.out.println("unsafe");
		}else {
			System.out.println("safe");
		}
		sc.close();
	}


}

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

class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String str = br.readLine();
			String str1 = br.readLine();
			for(int i = 0;i < (str.length() + str1.length());i++) {
				if(i%2==0) {
					System.out.print(str.charAt(i/2));
				}else {
					System.out.print(str1.charAt(i/2));
				}
			}
//			long max  = 1000000007;
		}catch (Exception e) {
			System.exit(0);
		}
	}
}


Measured memory_bytes: 21204
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)(x.size())
#define double long double 
#define fi first
#define se second
#define pii pair<int, int>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

signed main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  int k;
  cin >> k;
  string s;
  cin >> s;
  if (sz(s) <= k) {
    cout << s << "\n";
  } else {
    cout << s.substr(0, k) + "...\n";
  }
}



Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

  string s, t, u;
  cin >> s >> t;
  int a, b;
  cin >> a >> b >> u;

  int ans = a + b;
  if (u == s) {
    a--;
  } else {
    b--;
  }

  cout << a << " " << b << endl;

  return 0;
}

Measured memory_bytes: 256
n = gets.chomp.to_i

ans = 10**9

def min(a, b)
  a < b ? a : b
end

def max(a, b)
  a > b ? a : b
end

1.upto(Math.sqrt(n).to_i) do |a|
  next unless n % a == 0

  b = n / a

  ans = min(max(a.to_s.length,b.to_s.length), ans)
end

p ans


Measured memory_bytes: 1788
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 3005;

int dp[MAX_N][MAX_N];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s,t;
    cin >> s >> t;
    int n = len(s), m = len(t);
    dp[0][0] = 0;
    srep(i,1,n+1){
        srep(j,1,m+1){
            cmx(dp[i][j], dp[i-1][j]);
            cmx(dp[i][j], dp[i][j-1]);
            if(s[i-1] == t[j-1]){
                cmx(dp[i][j], dp[i-1][j-1]+1);
            }
        }
    }
    string ans;
    int x = n, y = m;
    while(x + y != 0){
        if(x > 0){
            if(dp[x-1][y] == dp[x][y]){
                x--;
                continue;
            }
        }
        if(y > 0){
            if(dp[x][y-1] == dp[x][y]){
                y--;
                continue;
            }
        }
        x--, y--;
        ans.pb(s[x]);
    }
    // cout << dp[n][m] << "\n";
    reverse(all(ans));
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 35456
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace atcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var xyabc = Console.ReadLine().Split(' ').Select(num => int.Parse(num)).ToArray();
            var x = xyabc[0];
            var y = xyabc[1];

            var reds = Console.ReadLine().Split(' ').Select(num => long.Parse(num))
                .OrderByDescending(num => num)
                .Take(x);

            var greens = Console.ReadLine().Split(' ').Select(num => long.Parse(num))
                .OrderByDescending(num => num)
                .Take(y);

            var result = reds.Concat(greens).OrderBy(num => num).ToArray();

            var whites = Console.ReadLine().Split(' ').Select(num => int.Parse(num))
                .OrderByDescending(num => num)
                .ToArray();

            for (var i = 0; i < Math.Min(result.Length, whites.Length); i++)
            {
                if (result[i] >= whites[i]) break;

                result[i] = whites[i];
            }

            Console.WriteLine(result.Sum(num => num));
        }
    }
}

Measured memory_bytes: 46036
#include <stdio.h>
int n;
int Bsearch(int A[],int key){
        int left=0,right=n;
        while(left<right) {
                int mid = (left + right)/2;
                if (A[mid] == key) {
                        return 1;
                }
                if (key > A[mid]) {
                        left = mid+1;
                }
                else if(key < A[mid]) {
                        right = mid;
                }
        }
        return 0;
}
int  main(){
        int A[1000000];
        int i,q,key,sum=0;
        scanf("%d",&n);
        for(i=0; i<n; i++) {
                scanf("%d",&A[i]);
        }
        scanf("%d",&q);
        for(i=0; i<q; i++) {
                scanf("%d",&key);
                if(Bsearch(A,key)) {
                        sum++;
                }
        }
        printf("%d\n",sum);

        return 0;
}



Measured memory_bytes: 2144
N = int(input())
H = list(map(int, input().split()))

if N == 1:
    print("Yes")
    exit()

for i in range(N - 1):
    if H[i] < H[i + 1]:
        H[i + 1] -= 1
        pass
    elif H[i] > H[i + 1]:
        print("No")
        exit()

print("Yes")


Measured memory_bytes: 14396
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>

using namespace std;

int main() {
	int H, W; cin >> H >> W;
	bool** grid = new bool* [H];
	int** a = new int* [H];
	int** b = new int* [H];

	for (int h = 0; h < H; h++) {
		grid[h] = new bool[W];
		a[h] = new int[W];
		b[h] = new int[W];
		for (int w = 0; w < W; w++) {
			char c; cin >> c;
			grid[h][w] = (c == '#');
			a[h][w] = 0;
			b[h][w] = 0;
		}
	}

	for (int h = 0; h < H; h++) {// 横
		int l = -1;
		for (int w = 0; w <= W; w++) {
			if (w==W || grid[h][w] == true) {// 壁だったら
				for (int i = l + 1; i < w; i++) {
					a[h][i] = w - l - 1;
				}
				l = w;
			}
		}
	}
	for (int w = 0; w < W; w++) {// 縦
		int u = -1;
		for (int h = 0; h <= H; h++) {
			if (h == H || grid[h][w] == true) {// 壁だったら
				for (int i = u + 1; i < h; i++) {
					b[i][w] = h - u - 1;
				}
				u = h;
			}
		}
	}

	int m = 0;
	for (int h = 0; h < H; h++) {
		for (int w = 0; w < W; w++) {
			m = max(m, a[h][w] + b[h][w]);
		}
	}

	cout << m - 1 << endl;

	return 0;
}


Measured memory_bytes: 35584
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdlib>

using namespace std;

int main() {
	int N;
	cin >> N;
	string S;
	cin >> S;
	int size = S.size();
	int K;
	cin >> K;
	for (int i = 0; i < size; i++) {
		if (S[i] != S[K - 1]) { S[i] = '*'; }
	}
	cout << S << endl;
}

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

#define int long long
#define ci(m)     for(int i=0;i<m;i++)
#define cj(m)     for(int j=0;j<m;j++)
#define ck(m)     for(int k=0;k<m;k++)
#define gcd            __gcd
#define endl           "\n"
#define pb             emplace_back
#define setbits(x)     __builtin_popcountll(x)
#define zrobits(x)     __builtin_ctzll(x)
#define mod            1000000007
#define mod2           998244353
#define maxe           *max_element
#define mine           *min_element
#define inf            1e18
#define deci(x, y)      fixed<<setprecision(y)<<x
#define w(t)           int t; cin>>t; while(t--)
#define nitin          ios_base::sync_with_stdio(false); cin.tie(NULL)
#define PI             3.141592653589793238
using namespace std;

int32_t main() {
    nitin;
    int n;
    cin>>n;
    map<int,int>m;
    ci(n)
    {
        int a;
        cin>>a;m[a]++;
    }
    if(n%3!=0)
    {
        if(m.size()==1 && m.count(0))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
        return 0;
    }
    bool flag=true;
    if(m.size()>3)
        cout<<"No"<<endl;
    else if(m.size()==3)
    {
        int a=0;
        for(auto c:m)
        {
            a^=c.first;
            if(c.second!=(n/3))
                flag=false;
        }
        if(a!=0)
            flag=false;
        if(flag)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;

    }
    else if(m.size()==2)
    {
        vector<int>v;
        v.reserve(m.size());
for(auto c:m)
            v.push_back(c.first);
        if(v[0]==0 || v[1]==0)
            ;
        else
            flag=false;
        if(m[v[0]]%(n/3)!=0)
            flag=false;
        if(m[v[1]]%(n/3)!=0)
            flag=false;
        if(flag)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;

    }
    else
    {
        if(m.count(0))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

Measured memory_bytes: 8416
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#define maxn 10005
#define INF 2000000005
using namespace std;
int dp[maxn][15];
int line[maxn];
int n;
int DP(int pos,int mod){
	if(pos==n){
		if(mod==0)return 0;
		return -INF;
	}
	if(dp[pos][mod])return dp[pos][mod];
	int& ans=dp[pos][mod];
	ans=max(DP(pos+1,((mod-line[pos])%10+10)%10)+line[pos],DP(pos+1,mod));
	return ans;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&line[i]);
	}
	int ans=0;
	for(int i=1;i<10;i++){
		ans=max(ans,DP(0,i));
	}
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;


char L[] = {'i', 'w', '(', ')', '{', '}', '[', ']'};
char R[] = {'i', 'w', ')', '(', '}', '{', ']', '['};

bool check(string s){
  int sl = s.length();
  if(s[sl / 2] != 'w' || s[sl / 2 - 1] != 'i' || s[sl / 2 + 1] != 'i') return 0;
  int l = sl / 2 - 2, r = sl / 2 + 2;
  while(l >= 0 && r < sl){
    bool f = 0;
    REP(i, 8) if(s[l] == L[i] && s[r] == R[i]) f = 1;
    if(!f) return 0;
    --l; ++r;
  }
  return 1;
}

int main() {
  string s; cin >>s;
  int sl = s.length(), ans = 3;
  REP(b, (1 << sl)){
    stringstream ss;
    REP(i, sl) if(1 & (b >> i)) ss << s[i];
    if(ss.str().length() < 3 || ss.str().length() % 2 == 0) continue;
    if(check(ss.str())) ans = max(ans, (int)ss.str().length());
  }
  cout <<ans <<endl;
  return 0;
}

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

          using namespace std;
          typedef long long ll;
          int n, m, k, t, s;
int dre[3232];
          int main()
          {
            cin >> n >> m >> k >> t;
            if(t > n)
              s += n, t -= n;
            else return cout << t, 0;
            if(t > m)
              t -= m;
            else return cout << s, 0;
            if(t > k)
              s -= k, t -= n;
            else return cout << s-t, 0;
          }

Measured memory_bytes: 3640
#include<bits/stdc++.h>
using namespace std;
double PI=acos(-1);
double eps=1e-7;
typedef complex<double> P;

double modf(double x){
  while(x>2*PI){
    x-=2*PI;
  }
  while(x<0){
    x+=2*PI;
  }
  return x;
}

double x,y,a,b;

double compute(double v,double m,double rate){
  double R=v/b;

  P target=polar(R,m);
  double distA=abs( target - P(x,y) ) / v;
  P base=target-P(x,y);
  P p = P(x,y) + ( base * rate );
  double f = modf(arg(p)) - distA*b*rate;
  
  return f;
}

double check(double v,double m){
  double left=0,right=1,midL,midR;
  
  for(int i=0;i<100;i++){
    double dist=( right-left ) /3.0;
    midL=left+dist;
    midR=right-dist;
    
    double fl = compute(v,m,midL);
    double fr = compute(v,m,midR);
    
    if( fl < fr ){
      right=midR;
    }else{
      left=midL;
    }
  }

  return compute(v,m,left);
}

bool func(double v){
  // R*b == v
  // R==v/b
  double R=v/b;
  if( abs( P(x,y) ) < R )return true;

  double tx=x,ty=y;
  double left=0,right=PI,midL,midR;
  
  if(x<0){
    left=PI*0.5;
    right=PI*1.5;
  }
  
  for(int i=0;i<100;i++){
    double dist=(right-left)/3.0;
    midL=left+dist;
    midR=right-dist;
    
    double fl=check(v,midL);
    double fr=check(v,midR);
    
    if( fl < fr ){
      left=midL;
    }else{
      right=midR;
    }
  }

  bool res=  ( (check(v,midL) >= 0 )|| (check(v,midR) >=0) );
  
  x=tx;
  y=ty;
  return res;
}

double solve(){
  
  P p=P(x,y) / polar(1.0, a);
  x=p.real();
  y=abs(p.imag());

  //  assert( x>=0 || y==0);

  
  double left=0,right=1e5,mid;
  
  for(int i=0;i<100;i++){
    mid=(left+right)*0.5;
    if( func(mid) ) right=mid;
    else left=mid;
  }

  return left;
}

int main(){
  cin>>x>>y>>a>>b;
  a=a/180.0*PI;
  b=b/180.0*PI;
  printf("%.10f\n", (double)solve());
  return 0;
}

Measured memory_bytes: 3696
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define SZ(x) (int)(x).size()
#define endl '\n'
#define FOR(a, b, c) for (auto a = (b); (a) < (c); ++(a))
#define F0R(a, b) FOR (a, 0, (b))
#define CEIL(a, b) ((a) + (b)-1) / (b)
template <class T>
bool ckmin(T& a, const T& b) {
  return a > b ? a = b, true : false;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, true : false;
}
#ifndef DEBUG
#define DEBUG 0
#endif
#define dout \
  if (DEBUG) std::cerr
#define dvar(...) " [" << #__VA_ARGS__ ": " << mt(__VA_ARGS__) << "] "
template <typename T>
struct IsC {
  template <typename C>
  static char test(typename C::const_iterator*);
  template <typename C>
  static int test(...);
  static const bool value = sizeof(test<T>(0)) == sizeof(char);
};
template <>
struct IsC<string> {
  static const bool value = false;
};
template <typename C>
typename enable_if<IsC<C>::value, ostream&>::type operator<<(ostream&, const C&);
template <typename T1, typename T2>
ostream& operator<<(ostream&, const pair<T1, T2>&);
template <typename... T> using TS = tuple_size<tuple<T...>>;
template<size_t idx, typename... T>
typename enable_if<TS<T...>::value == idx + 1, ostream&>::type
operator<<(ostream& o, const tuple<T...>& t) {
  return o << ", " << get<idx>(t) << ')';
}
template<size_t idx, typename... T>
typename enable_if<0 < idx && idx + 1 < TS<T...>::value, ostream&>::type
operator<<(ostream& o, const tuple<T...>& t) {
  return operator<<<idx + 1>(o << ", " << get<idx>(t), t);
}
template<size_t idx = 0, typename... T>
typename enable_if<1 < TS<T...>::value && !idx, ostream&>::type
operator<<(ostream& o, const tuple<T...>& t) {
  return operator<<<idx + 1>(o << '(' << get<idx>(t), t);
}
template<typename T>
ostream& operator<<(ostream& o, const tuple<T>& t) {
  return o << get<0>(t);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& p) {
  return o << mt(p.fi, p.se);
}
template <typename C>
typename enable_if<IsC<C>::value, ostream&>::type operator<<(
    ostream& o, const C& c) {
  o << '[';
  for (auto it = c.cbegin(); it != c.cend(); ++it)
    o << *it << (next(it) != c.cend() ? ", " : "");
  return o << ']';
}
template <typename T>
void tprint(vector<vector<T>>& v, size_t width = 0, ostream& o = cerr) {
  if (!DEBUG) return;
  for (const auto& vv : v) {
    for (const auto& i : vv) o << setw(width) << i;
    o << endl;
  }
}

int main() {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    ll res = a*c;
    ckmax(res, a*d);
    ckmax(res, b*c);
    ckmax(res, b*d);
    if(a <= 0 && b >= 0)
      ckmax(res, 0ll);
    if(c <= 0 && d >= 0)
      ckmax(res, 0ll);
    cout << res << endl;
}



Measured memory_bytes: 3620
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass

MOD = 2  # type: int


def solve(N: int, A: "List[int]"):

    def f(ai: int):
        ans = 0
        while ai & 1 == 0:
            ai //= 2
            ans += 1
        return ans

    print(min(f(ai) for ai in A))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
# cook your dish here
import sys
def file():
	sys.stdin = open('input.py', 'r')
	sys.stdout = open('output.py', 'w') 

def main(N, arr):

	#initialising with positive infinity value
	maxi=float("inf")

	#initialising the answer variable
	ans=0

	#iterating linesrly
	for i in range(N):

		#finding minimum at each step
	    maxi=min(maxi,arr[i])
	    #increasing the final ans
	    ans+=maxi
	    print("dhh")

    #returning the answer
	return ans
        
#file()	
if __name__ == '__main__':

	#length of the array
	'''N = 4

	#Maximum size of each individual bucket
	Arr = [4,3,2,1]

	#passing the values to the main function
	answer = main(N,Arr)

	#printing the result
	print(answer)'''
	n=int(input())
	s=input()

	#l=list(map(int, input().split()))
	ans=0
	for i in range(1,n):
		if(s[i]!=s[i-1]):
			ans+=1
	print(ans+1)		








Measured memory_bytes: 9188
main=getLine>>=putStrLn.unwords.map show.(\s->f s 1)
 where f (a:b:c:d) i = if a==b then [i,i+1] else (if a==c then [i,i+2] else (f (b:c:d) (i+1)))
       f [a,b] i = if a==b then [i,i+1] else [-1,-1]

Measured memory_bytes: 7548
import java.util.*;
public class Main {
	static Scanner sc = new Scanner(System.in);
	static char[][] field;
	static int H, W;
	static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
	static List <int[]> q = new ArrayList<int[]>();
	static List <int[]> next = new ArrayList<int[]>();
	public static void main(String[] args) {
		while(read()){
			solve();
		}
	}
	
	static boolean read(){
		H = sc.nextInt();
		W = sc.nextInt();
		if(H == 0 && W == 0)return false;
		
		field = new char[H][W];
		for(int i = 0; i < field.length; i++){
			field[i] = sc.next().toCharArray();
		}
		return true;
	}

	static void solve(){
		int res = 0;
		for(int i = 0; i < field.length; i++){
			for(int j = 0; j < field[0].length; j++){
				if(field[i][j] != 'x' ){
					bfs(i, j);
					res++;
				}
			}
		}
		System.out.println(res);
	}
	
	static void bfs(int x, int y){
		char mark = field[x][y];
		field[x][y] = 'x';
		q.add(new int[]{x, y});
		while(!q.isEmpty()){
			int u = q.get(0)[0];
			int v = q.get(0)[1];
			q.remove(0);
			for(int i = 0; i < dx.length && i < dy.length; i++){
				int nx = u + dx[i];
				int ny = v + dy[i];
				if(0 <= nx && nx < field.length && 0 <= ny && ny < field[0].length && field[nx][ny] == mark){
					field[nx][ny] = 'x';
					q.add(new int[]{nx, ny});
				}
			}			
		}
	}
}

Measured memory_bytes: 12000
H,W = map(int,input().split())
Ch, Cw = map(int,input().split())
Dh, Dw = map(int,input().split())
S = [list(input()) for _ in range(H)]
 
INF = H * W
 
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]
 
# def steppable(h,w):
#     if not (h in range(H)) or not (w in range(W)):
#         return False
#     if S[h][w] == '#':
#         return False
#     else:
#         return True
 
num_step = [[INF for w in range(W)] for h in range(H)]
num_step[Ch - 1][Cw - 1] = 0
 
# スタート地点を定義
queue = [(Ch - 1, Cw - 1)]
num_warp = 0
 
# 幅優先探索
while not queue == []:
    magic_tiles = []
    while not queue == []:
        h = queue.pop() # here 
        if h[0] == Dh - 1 and h[1] == Dw - 1:
            print(num_warp)
            exit()
        # next_  = [
        #     (h[0] + 1, h[1], 0),
        #     (h[0] - 1, h[1], 0),
        #     (h[0], h[1] + 1, 0),
        #     (h[0], h[1] - 1, 0),]
        for i in range(4):
            if 0 <= h[0]+dy[i] < H and 0 <= h[1]+dx[i] < W and S[h[0]+dy[i]][h[1]+dx[i]] == '.' \
                 and num_step[h[0]+dy[i]][h[1]+dx[i]] > num_warp:
                queue.append((h[0]+dy[i], h[1]+dx[i]))
                num_step[h[0]+dy[i]][h[1]+dx[i]] = num_warp
 
        # 魔法を使っていける場所を検討
        mx = [-2, -1 , 0, 1, 2]
        my = [-2, -1 , 0, 1, 2]
 
        for x in mx:
            for y in my:
                 if 0 <= h[0] + y < H and 0 <= h[1] + x < W and S[h[0] + y][h[1] + x] == '.' \
                     and num_step[h[0] + y][h[1] + x] == INF:
                    magic_tiles.append((h[0] + y, h[1] + x))
 
    num_warp += 1
 
    # キューの追加
    for magic in magic_tiles:
        if num_step[magic[0]][magic[1]] == INF:
            num_step[magic[0]][magic[1]] = num_warp
            queue.append((magic[0],magic[1]))
 
print(-1)


Measured memory_bytes: 141484
//Chandigarh University
//author:- shivamkr21 (pseduo coders)
#include<bits/stdc++.h>
#define lli long long int
#define endl "\n"
using namespace std;
int main()
{
	char c;
	cin>>c;
	lli a=int(c);
	if(a==122)
		a=97;
	else
		a++;
	cout<<char(a);

	return 0;
}


Measured memory_bytes: 384
n,a,b = map(int, input().split())
if abs(a-b)%2 == 0:
    print(abs(a-b)//2)
    exit()
# 左にaを
a,b = sorted([a,b])
ans = a-1 + (b-a+1)//2
# 右にbを
ans2 = n-b + (b-a+1)//2
ans = min(ans, ans2)
print(ans)


Measured memory_bytes: 38384
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int a[100001];
int main(){
    int n;cin>>n;
    long int sum=0;
    for(int i=0;i<n;i++){
        int t;cin>>t;
        a[t]++;
        sum+=t;
    }
    int q;cin>>q;
    for(int i=0;i<q;i++){
        int b,c;cin>>b>>c;
        sum=sum+a[b]*(c-b);
        a[c]+=a[b];
        a[b]=0;
        cout<<sum<<endl;
    }
    
}


Measured memory_bytes: 3932
#include <iostream>
#include <vector>
#include <map>
#define rep(i,n) for(int (i)=0;(i)<(n);++(i))
using namespace std;


int main() {
    int n; cin >> n;
    vector<int> v(n);
    rep(i, n) cin >> v[i];

    map<int, int> even, odd;

    rep(i, n) {
        if (i % 2 == 0) even[v[i]]++;
        else odd[v[i]]++;
    }

    int num_e = -1;
    int cnt_e1 = 0;
    int cnt_e2 = 0;
    for (auto e: even) {
        if (e.second > cnt_e1) {
            num_e = e.first;
            cnt_e2 = cnt_e1;
            cnt_e1 = e.second;
        } else if (e.second > cnt_e2) {
            cnt_e2 = e.second;
        }
    }

    int num_o = -1;
    int cnt_o1 = 0;
    int cnt_o2 = 0;
    for (auto e: odd) {
        if (e.second > cnt_o1) {
            num_o = e.first;
            cnt_o2 = cnt_o1;
            cnt_o1 = e.second;
        } else if (e.second > cnt_o2) {
            cnt_o2 = e.second;
        }
    }

    if (num_e != num_o) cout << n - cnt_e1 - cnt_o1 << endl;
    else cout << min(n- cnt_e1 - cnt_o2, n- cnt_e2 - cnt_o1) << endl;
}

Measured memory_bytes: 5376
import java.util.*;
public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int X[] = new int[N];
		for(int i = 0; i < N; i++) {
			X[i] = sc.nextInt();
		}
		
		int ans_num = Integer.MAX_VALUE;
		for(int i = 1; i <= 100; i++) {
			int num = 0;
			for(int j = 0; j < N; j++) {
				num += Math.pow(i - X[j], 2);
			}
			ans_num = Math.min(num, ans_num);
		}
		
		System.out.println(ans_num);
	}
}


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

int main(){
    int X;
    cin >> X;
    cout << X/500*1000 + (X%500)/5*5 << endl;
    return 0;
} 

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
using llong = long long;

llong n, k;
vector<llong> a;
llong cnt;

llong calc(int i, llong t) {
    llong l, r;

    l = 0;
    r = n;
    if (a[i] < 0) {
        if (a[i] * a[n - 1] > t) return 0;
        if (a[i] * a[0] <= t) return n - 1;
        while (r - l > 1) {
            llong mid = (l + r) / 2;

            if (a[i] * a[mid] <= t) {
                r = mid;
            }
            else {
                l = mid;
            }
        }

        if (i >= r) return n - r - 1;
        else return n - r;
    }
    else {
        if (a[i] * a[0] > t) return 0;
        if (a[i] * a[n - 1] <= t) return n - 1;
        while (r - l > 1) {
            llong mid = (l + r) / 2;

            if (a[i] * a[mid] <= t) {
                l = mid;
            }
            else {
                r = mid;
            }
        }

        if (i <= l) return l;
        else return l + 1;
    }
}

bool f(llong t) {
    llong sum = 0;

    for (int i = 0; i < n; i++) {
        sum += calc(i, t);
    }

    return sum >= k * 2;
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        llong x;
        cin >> x;
        a.push_back(x);
        if (x < 0) cnt++;
    }

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

    long long valid = (long long)1e18 + 308;
    long long invalid = -valid;
    while (valid - invalid > 1) {
        long long mi = (valid + invalid) / 2;

        if (f(mi)) {
            valid = mi;
        }
        else {
            invalid = mi;
        }
    }

    /*
    llong sum = 0;
    for (int i = 0; i < n; i++) {
        sum += calc(i, 448283280358331064);
    }
    cout << sum/2 << endl;
    */

    cout << valid << endl;
    
    return 0;
}


Measured memory_bytes: 2420
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;



int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int r, g;
  cin >> r >> g;
  cout << 2 * g - r << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int fish=1e9+7;
int N,a[505],K;
long long dp[205][100005];
long long Sum[100005];
int main()
{
	scanf("%d%d",&N,&K);
	for (int i=1;i<=N;i++)
	 scanf("%d",&a[i]);
	for (int i=0;i<=N;i++)
	  dp[i][0]=1;
	for (int i=1;i<=N;i++)
	{
	   for (int j=1;j<=K+1;j++)
	      Sum[j]=Sum[j-1]+dp[i-1][j-1];
	    for (int j=1;j<=K;j++)
	      dp[i][j]=dp[i][j]+Sum[j+1]-Sum[max(0ll,j*1ll-a[i])],dp[i][j]%=fish;
	}
	printf("%d",dp[N][K]);
	return 0;
}

Measured memory_bytes: 79488
class UnionFind:
    def __init__(self,n):
        self.par = [i for i in range(n+1)]  #親のノード番号
        self.rank = [0]*(n+1)
    def find(self,x):  #xの根のノード番号
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def same_check(self,x,y):  #x,yが同じグループか否か
        return self.find(x) == self.find(y)
    def union(self,x,y):  #x,yの属するグループの併合
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        elif self.rank[x] > self.rank[y]:
            self.par[y] = x
        else:
            self.par[y] = x
            self.rank[x] += 1

N,M = map(int,input().split())
P = [0] + [int(i) for i in input().split()]
data = [list(map(int,input().split())) for i in range(M)]

A = UnionFind(N)

for i in range(M):
    A.union(data[i][0],data[i][1])
ans = 0
for i in range(1,N+1):
    if A.same_check(i,P[i]):
        ans += 1

print(ans)

Measured memory_bytes: 36116