akhauriyash/Code-Regression huggingface dataset:
def li():
    return list(map(int, input().split()))


def mi():
    return map(int, input().split())


def ii():
    return int(input())


N = ii()

X = N / 1.08

if int((int(X) * 1.08)) == N:
    print(int(X))
elif int(((int(X) + 1) * 1.08)) == N:
    print(int(X) + 1)
else:
    print(":(")


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1000000000000000000;

struct edge{ll to, ca, cb;};
typedef pair<ll, ll> P;//(sからの距離、頂点の番号)

ll N, M;
ll s, t;
//辺
vector<edge> G[100000];
//cost a で定まるsからvまでの距離、cost b で定まるvからtまでの距離
ll da[100000], db[100000];

void dijka(ll s) {
  //greater<P>を指定することでfirstが小さい順に取り出せるようにできる
  priority_queue<P, vector<P>, greater<P>> que;
  fill(da, da+100000, INF);
  da[s] = 0;
  que.push(P(0, s));
  
  while(!que.empty()) {
    P p = que.top(); que.pop();
    ll v = p.second;
    if (da[v] < p.first) continue;
    for (ll i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (da[e.to] > da[v] + e.ca) {
        da[e.to] = da[v] + e.ca;
        que.push(P(da[e.to], e.to));
      }
    }
  }
}

void dijkb(ll t) {
  //greater<P>を指定することでfirstが小さい順に取り出せるようにできる
  priority_queue<P, vector<P>, greater<P>> que;
  fill(db, db+100000, INF);
  db[t] = 0;
  que.push(P(0, t));
  
  while(!que.empty()) {
    P p = que.top(); que.pop();
    ll v = p.second;
    if (db[v] < p.first) continue;
    for (ll i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (db[e.to] > db[v] + e.cb) {
        db[e.to] = db[v] + e.cb;
        que.push(P(db[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> N >> M;
  cin >> s >> t;
  s--; t--;
  for (ll i = 0; i < M; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--; v--;
    G[u].push_back({v,a,b});
    G[v].push_back({u,a,b});
  }
  
  dijka(s); 
  dijkb(t);
  
  ll d[N];
  
  for (ll i = 0; i < N; i++) {
    d[i] = da[i]+db[i];
  }
  
  
  priority_queue<ll, vector<ll>, greater<ll>> que;
  vector<ll> ans;
  
  for(ll i = N-1; i >= 0; i--) {
    que.push(d[i]);
    ans.push_back(que.top());
  }
  
  reverse(ans.begin(), ans.end());
  
  for (ll i = 0; i < N; i++) {
    cout << 1000000000000000 - ans.at(i) << endl;
  }
}

Measured memory_bytes: 15872
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

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

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

vector<int> as;

int solve(const int N) {
    int cur = 1;
    for (int i = 0; i < N; i++) {
        if (as[i] == cur) {
            cur++;
        }
    }
    if (cur == 1) {
        return -1;
    } else {
        return N - (cur - 1);
    }
}

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

    as.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> as[i];
    }

    cout << solve(N) << endl;
    return 0;
}


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

namespace abc152a{
    class Program{
        static void Main (string[] args){
            var ans = Console.ReadLine().Split().Select(s=>int.Parse(s)).ToArray();
            
            Console.Write(ans[0]==ans[1]?"Yes":"No");
        }
    }
}

Measured memory_bytes: 13268
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <deque>
#include <vector>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
#define x first
#define y second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ll long long
#define ld long double
#define ii pair<int,int>
#define heap priority_queue
#define all(k) k.begin(),k.end()
#define rall(k) k.rbegin(),k.rend()
#define len(v) ((int)v.size())
#define fa(a,c) for(auto &a:c)
#define endl '\n'





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

  int n;
  cin>>n;
  vector<int> a(n), b(n), c(n-1);
  fa(v,a){cin>>v;v--;}
  fa(v,b)cin>>v;
  fa(v,c)cin>>v;

  int last = -2;
  int ans=0;
  fa(v,a){
    ans+=b[v];
    if(last+1==v){
      ans+=c[last];
    }
    last = v;
  }

  cout<<ans<<endl;

}


Measured memory_bytes: 256
import heapq
def main():
    H, W = map(int, input().split())
    c = []
    for _ in range(10):
        c.append(list(map(int, input().split())))
    A = []
    for _ in range(H):
        A.append(list(map(int, input().split())))

    d1 = dict()
    queue = [[0, 1]]
    while queue:
        m, j = heapq.heappop(queue)
        if j not in d1:
            d1[j] = m
            for k in range(10):
                if k not in d1:
                    heapq.heappush(queue, [m+c[k][j], k])

    d = dict()
    for i in range(H):
        for j in range(W):
            if A[i][j] != -1:
                d[A[i][j]] = d.get(A[i][j], 0) + 1

    # print(d1)
    # print(d)
    mag = 0
    for v in d:
        mag += d[v] * d1[v]
    return mag


if __name__ == '__main__':
    print(main())

Measured memory_bytes: 40412
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define INF 1e+9
#define mp make_pair
#define pb push_back
#define fi first
#define fs first
#define se second
#define i64 long long
#define li long long
#define lint long long
#define pii pair<int, int>
#define vi vector<int>

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

const int maxn = 55;

i64 dp[maxn][2];

int main() {
#ifdef LOCAL
    freopen("inp", "r", stdin);
    //freopen("outp", "w", stdout);
#else
    // freopen(TASKNAME ".in", "r", stdin);
    // freopen(TASKNAME ".out", "w", stdout);
#endif
    int n, p;
    scanf("%d%d", &n, &p);
    dp[0][0] = 1;
    forn(i, n) {
        int x;
        scanf("%d", &x);
        x = x % 2;
        forn(prev, 2)
            dp[i + 1][prev] = dp[i][prev];
        forn(prev, 2)
            dp[i + 1][(prev + x) % 2] += dp[i][prev];
    }
    cout << dp[n][p];
}



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


int main()
{
	string str1,str2;
	cin>>str1;
	cin>>str2;
	string str3=str2.substr(0,str1.length());
	if(str1==str3)
		cout<<"Yes\n";
	else
		cout<<"No\n";
	return 0;
}

Measured memory_bytes: 3604
n = int(input())


def make_divisors(n):
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    # divisors.sort()
    return divisors



ansl = make_divisors(n-1)
ansl.remove(1)
for i in make_divisors(n):
    if i == 1:
        continue
    k = n
    while k:
        if k % i == 0:
            k //= i
        else:
            if k % i == 1:
                ansl.append(i)
            break

print(len(set(ansl)))




Measured memory_bytes: 39024
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <climits>
#include <iomanip>
#include <cmath>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << std::fixed << std::setprecision(10);

	string s;
	cin >> s;

	if (s == "Sunny") {
		cout << "Cloudy" << endl;
	}
	else if(s == "Cloudy") {
		cout << "Rainy" << endl;
	}
	else {
		cout << "Sunny" << endl;
	}

	return 0;
}

Measured memory_bytes: 256
# coding: utf-8
# Your code here!
import math
a, b, h, m = map(float, input().split())
long_deg = m * 6
short_deg = (h * 60 + m) * 0.5
temp_deg = abs(long_deg - short_deg)
deg = min(temp_deg, 360 - temp_deg)
temp_c = a * a + b * b - 2 * a * b * math.cos(math.radians(deg))
c = math.sqrt(temp_c)
print(c)

Measured memory_bytes: 9500
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
// #define endl "\n"

ll recurse(ll i, vector<ll>& height, vector<ll>& dp) {
    ll n = dp.size();

    if (i == n-1) {
        return 0;
    }

    if (i == n-2) {
        return abs(height[n-1] - height[n-2]);
    }

    if (dp[i] != -1) {
        return dp[i];
    }

    int one = abs(height[i+1] - height[i]) +recurse(i+1, height, dp);
    int two = abs(height[i+2] - height[i]) + recurse(i+2, height, dp);

    dp[i] = min(one,two);
    return dp[i];
}

int main(void) {
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.tie(NULL);

    ll n;
    cin >> n;
    vector<ll> arr(n);
    for (ll i = 0; i < n; i++) {
        cin >> arr[i];
    }

    vector<ll> dp (n, -1);
    cout << recurse(0, arr, dp);

    
    return 0;
}

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

#define ll long long
#define ld long double
#define fi first
#define endl "\n"
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define initialize(array,size,value) for(ll i=0;i<size;i++) array[i]=value
#define couta(array,size) for(ll i=0;i<size;i++) cout<<array[i]<<" "
#define inf (long long int) 1e18
#define eps 0.000001
#define vl vector<ll> 
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll> 
#define pq priority_queue<ll>
#define mod 1000000007
#define MAXN 100001
ll spf[MAXN]; 
ll gcd(ll a,ll b);
ll palindrome(string s);
ll modexp(ll a,ll b,ll m);
void sieve();
ll ceil(ll a, ll b);
vl getFactorization(ll x);
void getZarr(string str, ll Z[]);
vector<ll> prefix_function(string s);

ll n;
string a[5205];
bool f(ll x){
    //cout<<"llo"<<endl;
    for(ll i=0; i<n; i+=x){
        //cout<<"lolppl"<<endl;
        for(ll j=0; j<n; j+=x){
            bool done=true;
            char ch=a[i][j];
            for(ll k=i; k<i+x; k++){
                for(ll o=j; o<j+x; o++){
                    if(a[k][o]!=ch){
                        return 0;
                    }
                }
            }
        }
    }
    return 1;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/

   ll n, a, b;
   cin>>n>>a>>b;
   ll ubo=min(a, b);
   ll lbo=max((ll)0, a+b-n);

   cout<<ubo<<" "<<lbo<<endl;



}

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

ll palindrome(string s){
    ll l = 0;
    ll h = s.length() - 1;
    while (h > l){
        if (s[l++] != s[h--]){
            return 0;
        }
    }
    return 1;
}

ll modexp(ll a, ll b, ll m){
    if (b==0)
        return 1;
    ll temp = modexp(a,b/2,m);
    temp = (temp*temp)%m;
    if (b&1)
        return (temp*(a%m))%m; // if b is odd a^b = a^(b/2)*a^(b/2)*a
    return temp;
}

Measured memory_bytes: 256
A, B, C, X, Y = map(int, input().split())
faceAB = (A + B) / 2
sumPizza = 0

if faceAB <= C:
    ans = X * A + Y * B
else:
    def2 = 2 * C * max(X, Y)
    if X <= Y:
        tmp = 2 * C * min(X, Y) + ((Y - X) * B)
    else:
        tmp = 2 * C * min(X, Y) + ((X - Y) * A)
    ans = min(def2, tmp)
print(ans)


Measured memory_bytes: 38416
def gcd(a, b):
    if(a == 0):
        return b
    return gcd(b % a, a)
def lcm(a, b):
    return a // gcd(a, b) * b
t = int(input())
ans = 1
while(t != 0):
    t -= 1
    n = int(input())
    ans = lcm(ans, n)
print(int(ans))


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


using namespace std;



int main()
{
	int n, k, h[200005];
	long long sm = 0;
	cin >> n >> k;
	for(int i = 0; i < n; i++)
	{
		cin >> h[i];
		//sm += (long long)h[i];
	}
	sort(h, h + n);
	if (k >= n)
	{
		cout << 0;
		//system("pause");
		return 0;
	}
	for (int i = 0; i < n - k; i++)
	{
		sm +=(long long) h[i];
	}cout << sm;

	//system("pause");
	return 0;
}

Measured memory_bytes: 1024
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using ll = long long;
using itn = int;
const int MAX_N = 110000;
using namespace std;

int main(){
    ll N; cin >> N;
    ll A[5];
    ll minValue=1e16;
    for(int i=0; i<5; i++){
        cin >> A[i];
        if(A[i]<minValue) minValue = A[i];
    }
    //ボトルネックになる部分がある（送り出せる人数が最少）
    //のでそれにかかる時間＋4分(一番乗りが都市6につくのにかかる時間)
    ll res = 4 + (N/minValue);
    if(N%minValue) res++;
    cout << res << endl;

}

Measured memory_bytes: 256
/*********************
*@Author:   CKang *
*@Language: C++11    *
*********************/
#include<bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
//#define DEBUG
#define RI register int
#define endl "\n"

using namespace std;

typedef long long ll;
//typedef __int128 lll;
//const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-9;
const int INF = 0x3fffffff;
const ll LINF = 0x3fffffffffffffff;

inline ll read(){
    long long 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^48);
        ch=getchar();
    }
    return x*f;
}

ll gcd(ll a,ll b){
    return b ? gcd(b,a%b) : a ;
}

int main()
{
#ifdef DEBUG
    freopen("input.in", "r", stdin);
    //freopen("output.out", "w", stdout)
#endif
    //cout.tie(0);
    //ll t = read() ;
    //while(t--){}
    ll n=read(),sum=0;
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            for(ll k=1;k<=n;k++){
                sum+=gcd(i,gcd(j,k));
            }
        }
    }
    cout<<sum<<endl;
#ifdef DEBUG
    printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Fuck You !" << endl;
    return 0;
}


Measured memory_bytes: 3596
#include<bits/stdc++.h>
#include<utility>
using namespace std;
bool ok(pair<int, int> a, pair<int, int> b) {
  if (a.first < b.first && a.second < b.second) return true;
  else return false;
}
int main () {
  int N;
  cin >> N;
  vector<pair<int, int>>A(N + 1);
  vector<pair<int, int>>B(N);
  vector<bool>ex(N + 1, true);
  for (int i = 0; i < N; i ++) {
    int a, b;
    cin >> a >> b;
    A[i] = make_pair(a, b);
  }
  A[N] = make_pair(-1, -1);
  ex[N] = false;
  for (int i = 0; i < N; i ++) {
    int a, b;
    cin >> a >> b;
    B[i] = make_pair(a, b);
  }
  sort(B.begin(), B.end());
  int ans = 0;
  for (int i = 0; i < N; i ++) {
    int k = N;
    for (int j = 0; j < N; j ++) {
      if (ok(A[j], B[i]) && A[j].second > A[k].second && ex[j])k = j;
    }
    if (ex[k]) {
      ans ++;
      ex[k] = false;
    }
  }
  cout << ans << endl;
}

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

int main() {
  int N, X;
  cin >> N >> X;
  vector<int> g(N);
  int least = 0;
  
  for(int i = 0; i < N; i++) {
    cin >> g.at(i);
    least += g.at(i);
  }
  
  sort(g.begin(), g.end());
  int plus = (X - least) / g.at(0);
  
  cout << N + plus << endl;
}

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

using namespace std;

vector<string> split(const string &s, char delim) {
  vector<string> elems;
  string item;
  for (char ch: s) {
    if (ch == delim) {
      if (!item.empty()) {
        elems.push_back(item);
      }
      item.clear();
    }
    else {
      item += ch;
    }
  }
  if (!item.empty()) {
    elems.push_back(item);
  }
  return elems;
}

string to_str_with_zero(int i, int w) {
  ostringstream sout;
  sout << std::setfill('0') << std::setw(w) << i;
  string s = sout.str();
  return s;
}

int letter_to_int(char c) {
  return tolower(c) - 'a';
}

int compare_array(vector<int> a1, vector<int>a2) {
  if (a1.size() != a2.size()) {
    return a1.size() - a2.size();
  }
  for (int i=0; i<a1.size(); i++) {
    if (a1.at(i) != a2.at(i)) {
      return a1.at(i) - a2.at(i);
    }
  }
  return 0;
}

int gcd(int a, int b) {
  if (a == b) {
    return a;
  }
  else if (a > b) {
    return gcd(a - b, b);
  }
  else {
    return gcd(a, b - a);
  }
}

char int_to_char(int a) {
  if (a == -1) {
    return 'z';
  }
  else {
    return 'a' + a;
  }
}

long nCr(int n, int r) {
    long ans = 1;
    for (int i = n; i > n - r; --i) {
        ans = ans*i;
    }
    for (int i = 1 ; i < r + 1; ++i) {
        ans = ans / i;
    }
    return ans;
}

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}

int divide_count(int a, int divider) {
  int r = 0;
  while(a % divider == 0) {
    a /= divider;
    r++;
  }
  return r;
}

bool is_prime(int a) {
  int i = 2;
  while(i * i <= a) {
    if(a % i == 0) {
      return false;
    }
    i++;
  }
  return true;
}

int main() {
  std::cout << std::setprecision(9);
  int h, w, k;
  cin >> h >> w >> k;

  vector<vector<int>> c(h, vector<int>(w, 0));
  for(int i=0; i<h; i++) {
    for(int j=0; j<w; j++) {
      char s;
      cin >> s;
      if(s == '#') {
        c[i][j] = 1;
      }
    }
  }

  int ans = 0;
  for(int i=0; i<(1<<h); i++) {
    bitset<6> bs_h(i);
    for(int j=0; j<(1<<w); j++) {
      bitset<6> bs_w(j);
      int count = 0;
      for(int m=0; m<h; m++) {
        for(int n=0; n<w; n++) {
          if(c[m][n] == 1 && bs_h.test(m) == 0 && bs_w.test(n) ==0) {
            count++;
          }
        }
      }
      if(count == k) {
        ans++;
      }
    }
  }

  cout << ans << endl;
}

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

int n;
long long a[100005];
long long b[100005];
long long sum = 0;

int main()
{
	
	cin>>n;
	for(int i = 0; i < n; i++)
		cin>>a[i]>>b[i];
	
	for(int i = n - 1; i >= 0; i--)
	{
		if((a[i] + sum) % b[i] == 0)
			continue;
		long long aux = (a[i] + sum) / b[i] + 1;
		sum += b[i] * aux - (a[i] + sum);
	}
	
	cout<<sum<<endl;
	
	//system("PAUSE");
	return 0;
}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int gcd(int a,int b)
{
	if(b==0)
	return a;
	
	return gcd(b,a%b);
}
int power(int x,int y,int p)
{
	int res=1;
	x=x%p;
	while(y>0)
	{
		if(y&1)
		res=(res*x)%p;
		
		y=y>>1;
		x=(x*x)%p;
	}
	return res;
}
int modInverse(int n,int p)
{
	return power(n,p-2,p);
}
int main() {
	// your code goes here
	 ios_base::sync_with_stdio(0);
	 cin.tie(0);
	 string s,t;
	 cin>>s>>t;
	 if(s==t.substr(0,t.size()-1))
	 cout<<"Yes";
	 else
	 cout<<"No";
	 return 0;
}

Measured memory_bytes: 3656
#include <bits/stdc++.h>
using namespace std;
#define ll              long long int
#define int             ll
#define ff              first
#define ss              second
#define pb              emplace_back
#define mpair           make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define sbits(x)      __builtin_popcountll(x)
#define zbits(x)      __builtin_popcountlliltin_ctzll(x)
#define w(t)            int t;cin>>t;while(t--)
#define uset            unordered_set
#define forr(i, a, b)   for(int i =int(a);i<=int(b);i++)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
    template <typename Arg1>
     void __f(const char* name,Arg1&& arg1){
        cout<<name<<" : "<<arg1<<std::endl;
     }
     template <typename Arg1, typename... Args>
     void __f(const char* names,Arg1&& arg1,Args&&... args){
        const char* comma = strchr(names+1 ,',');
        cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
     }
#else
#define trace(...)
#endif
/*     
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
*/
double pi = 2.0*acos(0);
void fast(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout<<fixed<<setprecision(10);}
int max2(int a,int b){ if(a>b) return a;return b;}
int min2(int a,int b){ if(a<b)return a;return b;}
int max3(int a,int b,int c){ return max2(a,max2(b,c));}
int min3(int a,int b,int c){ return min2(a,min2(b,c));}

///////////////////////////////////////////////////////////////////////////////////////////////


void solve(){
    
    int n,k;
    cin>>n>>k;

    vector<int > a(n+1);
    for(int  i =1;i<=n;i++)
      cin>>a[i];
    
    std::vector<int> vis(n+1,-1);
    vis[1] = 0;
     std::vector<int> step(n+1,-1);
     step[0] =1;
     int nxt = a[1];
     int start;int sloop;int counter = 1;int size;
    while(1)
    {
         if(vis[nxt]!= -1)
         {
             sloop = nxt;
             start = vis[nxt];
             size = counter - vis[nxt];
             break;
         }
         else
         {
             vis[nxt] = counter;
             step[counter] = nxt;
             nxt = a[nxt];
             counter++;

         }
    }

    if(k<=start)
        cout<<step[k]<<endl;
    else
    {
        k = k-start;
        cout<<step[(start+(k%size))]<<endl;
    }

 }


signed main(void)
{
      fast();
      //w(t)
      solve();

}

Measured memory_bytes: 7952
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int main(){
    long long h, w;
    cin >> h >> w;

    char maze[h][w];

    for(long long i = 0; i < h; i++){
        for(long long j = 0; j < w; j++){
            cin >> maze[i][j];
        }
    }

    vector<vector<long long> > dp(h, vector<long long>(w, 1e9));

    if(maze[0][0] == '#'){
        dp[0][0] = 1;
    }
    else{
        dp[0][0] = 0;
    }

    for(long long i = 0; i < h; i++){
        for(long long j = 0; j < w; j++){
            long long nx, ny;
            nx = j;
            ny = i + 1;
            if(ny < h){
                if(maze[ny][nx] == '.'){
                    dp[ny][nx] = min(dp[ny][nx], dp[i][j]);
                }
                else{
                    if(maze[i][j] == '.'){
                        dp[ny][nx] = min(dp[ny][nx], dp[i][j]+1);
                    }
                    else{
                        dp[ny][nx] = min(dp[ny][nx], dp[i][j]);
                    }
                }
            }
            
            nx++;
            ny--;
            if(nx < w){
                if(maze[ny][nx] == '.'){
                    dp[ny][nx] = min(dp[ny][nx], dp[i][j]);
                }
                else{
                    if(maze[i][j] == '.'){
                        dp[ny][nx] = min(dp[ny][nx], dp[i][j]+1);
                    }
                    else{
                        dp[ny][nx] = min(dp[ny][nx], dp[i][j]);
                    }   
                }
            }
        }
    }

    cout << dp[h-1][w-1] << endl;
    return 0;
}

Measured memory_bytes: 3712
#include<stdio.h>
#include<string.h>
int main(void){
    char str[3];
    
    scanf("%s",str);
    int length=strlen(str);

    if(length==2)
        printf("%s",str);
    else{ 
        for(int i=2;i>=0;i--)
            printf("%c",str[i]);
        }

    return 0;
}

Measured memory_bytes: 128
n = gets.to_i
l = gets.split.map(&:to_i)

cmb = l.combination(3).map(&:uniq).select{|i|i.count == 3}
count = 0
cmb.each do |i|
    if (i[0] + i[1] > i[2]) && (i[0] + i[2] > i[1]) && (i[1] + i[2] > i[0])
        count += 1
    end
end
puts count


Measured memory_bytes: 30364
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define sf scanf
#define pf printf
#define MAX 200002
#define INF 1000000
#define EXP 1e-6
#define endl '\n'
#define all(v) v.begin(), v.end()
#define FOR(i, a, b, c) for( int i = a; i < b; i+=c )
#define rep(i, a) for( int i = 0; i < a; i++ )
#define fastio ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);

typedef long long int ll;
const int maxn = 1e6 + 5;
const ll MOD = 1e9 + 7;

using namespace std;

typedef pair <int, int> ii;
typedef pair <int, ii> trie;
typedef vector <int> vi;
typedef vector <ii> vii;
typedef vector <vii> vvii;
typedef vector <vi> vvi;
typedef vector <bool> vb;
typedef vector <trie> vtrie;
typedef bitset <maxn> bs;

int arr[maxn], n, k;
int dp[maxn];

int cost( int pos )
{
	int &ret = dp[pos];
	if( pos == (n-1) ) return 0;
	if( pos == (n-2) ) return abs(arr[pos + 1] - arr[pos]);
	if( ret != -1 ) return ret;
	int nth_place = INT_MAX;

	for( int i = 1; i <= 2; i++ )
		nth_place = min( nth_place, cost( pos + i ) + abs(arr[pos + i] - arr[pos]) );
	//snd_place = cost( pos + 2 ) + abs(arr[pos + 2] - arr[pos]);

	return ret = nth_place;
}

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

    memset( dp, -1, sizeof(dp) );

    int min_cost = cost( 0 );
    cout << min_cost << endl;

	return 0;

}

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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

int N, K;

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

  int t = 0;
  while(N > 0){
      t += 1;
      N /= K;
  }

  std::cout << t << std::endl;
}


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

const ll MX = 1e18;
const long double PI = acos(-1);

int main()
{
    char s; cin >> s;
    if(isupper(s))
        pri("A");
    else
        pri("a");
    return 0;
}


Measured memory_bytes: 3632
package main

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

func main() {
	N := nextInt()
	ok := true

	for i := 0; i < N; i++ {
		a := nextInt()

		if a%2 == 1 {
			continue
		}
		if a%3 == 0 || a%5 == 0 {
			continue
		}
		ok = false
		break
	}

	if ok {
		fmt.Println("APPROVED")
	} else {
		fmt.Println("DENIED")
	}
}

var stdin = initStdin()

func initStdin() *bufio.Scanner {
	bufsize := 1 * 1024 * 1024 // 1 MB
	var stdin = bufio.NewScanner(os.Stdin)
	stdin.Buffer(make([]byte, bufsize), bufsize)
	stdin.Split(bufio.ScanWords)
	return stdin
}

func nextString() string {
	stdin.Scan()
	return stdin.Text()
}

func nextBytes() []byte {
	stdin.Scan()
	return stdin.Bytes()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextString())
	return i
}

func nextInt64() int64 {
	i, _ := strconv.ParseInt(nextString(), 10, 64)
	return i
}


Measured memory_bytes: 640
// TODO: WA
// problem: longest distance from 0 to V-1, checking for positive cycle on this path
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1 << 30;

struct Tadj {
	int id, w;
};
vector<vector<Tadj>> adjList;
int V;

vector<ll> ld;

vector<bool> visited;
void dfs(int u) {
   if (visited[u]) return;
   visited[u] = 1;
   for (Tadj v : adjList[u])
      dfs(v.id);
}

/* O(V*E) - retorna true se há um ciclo positivo que altere ld[V-1] */
bool belmman_ford(int s) {
   ld.assign(V, -INF); ld[s] = 0;

   ll first0, first1;
   for (int i = 0; i < V; i++) {
      // para cada aresta u --v.w--> v.id
      for (int u = 0; u < V; u++)
         for (Tadj v : adjList[u]) {
            if (ld[u] > -INF && ld[u] + v.w > ld[v.id]) {
               ld[v.id] = ld[u] + v.w;

               if (i == V-1) {
                  visited.assign(V, 0);
                  dfs(v.id);
                  if (visited[0] || visited[V-1]) return true;
               }
            }
         }
   }

   return false;
}

int main() {
   int E, P; cin >> V >> E >> P;
   adjList.assign(V, vector<Tadj>());
   while (E--) {
      int u, v, w; cin >> u >> v >> w; u--; v--; w -= P;
      adjList[u].push_back({v, w});
   }

   cout << (belmman_ford(0)? -1 : max(ld[V-1], 0ll)) << endl;
}

Measured memory_bytes: 3868
"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)
 
def iin(): return int(input())
 
 
def lin(): return list(map(int, input().split()))
 
 
# range = xrange
# input = raw_input
 
def main():
    T = 1
    while T:
        T-=1
        n = iin()
        ans = set()
        ans.add(n)
        def check1(x):
            n1 = x-1
            i = 1
            while i*i<=n1:
                if n1%i==0:
                    ans.add(i)
                    ans.add(n1//i)
                i+=1
        def check2(x):
            n1 = n
            while n1%x==0:
                n1//=x
            n1 = n1%x
            if n1==1 :
                ans.add(x) 
        check1(n)
        i = 2
        while i*i<=n:
            if n%i==0:
                check2(i)
                if i!=n//i:
                    check2(n//i)
            i+=1
        ans = [i for i in ans if i>1]
        print(len(ans))
        # print(ans)






 
 
 
 
main()
 
# threading.Thread(target=main).start()

Measured memory_bytes: 39152
#include "bits/stdc++.h"
using namespace std;
int Freq[2005];

int main(){
    int N , X , Y , i , j;
    scanf( "%d%d%d" , &N , &X , &Y );

    for( i = 1 ; i <= N ; ++i ){
        for( j = i + 1 ; j <= N ; ++j ){
            if( i <= X ){
                if( j <= X ) ++Freq[j - i];
                else if( j <= Y ) ++Freq[min( j - i , Y - j + 1 + X - i )];
                else ++Freq[( X - i ) + 1 + ( j - Y )];
            }
            else if( i < Y ){
                if( j <= Y ) ++Freq[min( j - i , i - X + 1 + Y - j )];
                else ++Freq[min( Y - i , i - X + 1 ) + j - Y];
            }
            else ++Freq[j - i];
        }
    }

    for( i = 1 ; i < N ; ++i )
        printf( "%d\n" , Freq[i] );

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>

using namespace std;
using ll = long long;


int main() {
    ll n,cnt=0;
    cin >> n;
    vector<ll> a(n),b(n);
    for(ll i=0;i<n;i++) cin >> a[i] >> b[i];
    for(ll i=n-1;i>=0;i--){
        if((a[i]+cnt)%b[i]!=0){
            cnt+=(b[i]-((a[i]+cnt)%b[i]));
        }
    }
    cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 1792
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

int main()
{
    int a, b, c, x;
    int cnt = 0;
    int sum;

    scanf("%d%d%d%d", &a, &b, &c, &x);

    int i, j, k;
    for (i = 0; i <= a;i++){
        for (j = 0; j <= b;j++){
            for (k = 0; k <= c;k++){
                int temp = i * 500 + j * 100 + k * 50;
                if(temp==x)
                    cnt++;
            }
        }
    }
        printf("%d\n", cnt);
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <bitset>
#include <algorithm>
#include <map>

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 pb push_back

vector<vector<int> > G(100);
typedef bitset<100> bs;

int dfs(int v, bs vis){
    vis[v] = true;
    int res = 1;
    rep(i, G[v].size()){
        int to = G[v][i];
        if(!vis[to]){
            res = max(res, dfs(to, vis) + 1);
        }
    }
    return res;
}

int main(){
    int n;
    while(cin >> n, n){
        rep(i, 100) G[i].clear();
        rep(i, n){
            int a, b; cin >> a >> b;
            G[a - 1].push_back(b - 1);
            G[b - 1].push_back(a - 1);
        }
        bs init;
        int ans = 1;
        rep(i, 100){
            ans = max(dfs(i, init), ans);
        }
        cout << ans << endl;
    }
}

Measured memory_bytes: 1224
#include <bits/stdc++.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define rn(i,n) for(int i=1;i<=n;i++)

int main(void){
  int n,d;
  cin>>n>>d;
  vector<vector<int> > x(n, vector<int>(d));
  r(i,n){
    r(j,d){
      cin>>x.at(i).at(j);
    }
  }
  double dis=0;
  int dif,cnt=0;
  r(i,n){
    for(int j=0;j<n;j++){
      if(i==j)break;
      r(k,d){
        dis+=(x.at(i).at(k)-x.at(j).at(k))*(x.at(i).at(k)-x.at(j).at(k));
      }
      dis=sqrt(dis);
      dif=dis;
      if(dis-dif==0)cnt++;
      dis=0;
    }
  }

  cout<<cnt<<endl;


  return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long int ll;

#define INF (1e9)

int N, W;

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

    vector<int> w(N), v(N);
    for (int i = 0; i < N; i++) {
        cin >> w[i] >> v[i];
    }

    vector<vector<ll> > dp(N, vector<ll>(W + 1, 0));
    for (int j = 0; j < W + 1; j++) {
        for (int i = 0; i < N; i++) {
            ll x, y;

            if (j - w[i] < 0)
                x = 0;
            else if (i == 0)
                x = v[i];
            else
                x = dp[i - 1][j - w[i]] + v[i];

            if (i == 0)
                y = 0;
            else
                y = dp[i - 1][j];

            dp[i][j] = max(x, y);
        }
    }

    cout << dp[N - 1][W] << endl;
    return 0;
}


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

using namespace std;

#define pb push_back
#define mp make_pair
#define fi(a, b) for(int i=a; i<=b; i++)
#define fj(a, b) for(int j=a; j<=b; j++)
#define fo(a, b) for(int o=a; o<=b; o++)
#define fdi(a, b) for(int i=a; i>=b; i--)
#define fdj(a, b) for(int j=a; j>=b; j--)
#define fdo(a, b) for(int o=a; o>=b; o--)
#define clr(x) memset(x, 0, sizeof(x))
#define cpy(x,y) memcpy(x, y, sizeof(y))
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;

#ifdef _DEBUG
#define LOCAL
#endif

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while (0)
#endif

double START_TIME;

void exit() {
#ifdef LOCAL	
	cerr << "TIME: " << setprecision(5) << fixed << (clock() - START_TIME) / CLOCKS_PER_SEC << endl;
#endif	
	exit(0);
}

template<typename A, typename B>
ostream& operator<<(ostream& os, pair<A, B> p) {
	os << "(" << p.first << ", " << p.second << ")";
	return os;
}

template<typename T>
ostream& operator<<(ostream& os, vector<T> v) {
	fi(0, sz(v) - 1) {
		os << v[i] << " ";
	}
	return os;
}

template<typename T>
ostream& operator<<(ostream& os, set<T> t) {
	for (auto z : t) {
		os << z << " ";
	}
	return os;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& os, map<T1, T2> t) {
	cerr << endl;
	for (auto z : t) {
		os << "\t" << z.first << " -> " << z.second << endl;
	}
	return os;
}

#ifdef LOCAL
#define dbg(x) {cerr << __LINE__ << "\t" << #x << ": " << x << endl;}
#define dbg0(x, n) {cerr << __LINE__ << "\t" << #x << ": "; for (int ABC = 0; ABC < n; ABC++) cerr << x[ABC] << ' '; cerr << endl;}
#define dbg1(x, n) {cerr << __LINE__ << "\t" << #x << ": "; for (int ABC = 1; ABC <= n; ABC++) cerr << x[ABC] << ' '; cerr << endl;}
#else
#define dbg(x) while(0){}
#define dbg0(x, n) while(0){}
#define dbg1(x, n) while(0){}
#endif

#ifdef LOCAL
#define ass(x) if (!(x)) { cerr << __LINE__ << "\tassertion failed: " << #x << endl, abort(); }
#else
#define ass(x) assert(x)
#endif

///////////////////////////////////////////////////


const int MAX = 100 * 1000 + 41;
string F = "first";
string S = "second";

int n;
int a[MAX];

void printans(int x) {
	printf("%s\n", (x == 1 ? F : S).c_str());
	exit();
}

void solve() {
	fi(1, n) {
		if (a[i] % 2 == 1) printans(1);
	}
	printans(2);
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	START_TIME = (double)clock();
#endif
	
	scanf("%d", &n);
	fi(1, n) scanf("%d", &a[i]);
	solve();

	exit();
	return 0;
}





Measured memory_bytes: 640
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

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

    bool nn = false, ss = false, ee = false, ww = false;
    rep(i, s.size())
    {
        nn = nn || (s[i] == 'N');
        ss = ss || (s[i] == 'S');
        ee = ee || (s[i] == 'E');
        ww = ww || (s[i] == 'W');
    }

    if (nn == ss && ee == ww)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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

typedef long long ll;
typedef pair<ll, ll> p;


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n-1);
  rep(i, n-1) cin >> v[i];
  map<int, int> mp;
  rep(i, n-1) mp[v[i]]++;

  for (int i = 1; i <= n; ++i) {
    cout << mp[i] << endl;
  }
  // cout << 0 << endl;
}



Measured memory_bytes: 13540
#include <iostream>

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

int main() {
	int N, K, X, Y; cin >> N >> K >> X >> Y;

	int	sum = 0;
	if (N > K)
	{
		sum += (N - K) * Y;
		sum += K * X;
	}
	else {
		sum += N * X;
	}
	cout << sum << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
int main(){
  int w;
  while(1){
    scanf("%d",&w);
    if(w==-1)break;
    int p=0;
    if(30<w){
      p+=160*(w-30);
      w=30;
    }
    if(20<w){
      p+=140*(w-20);
      w=20;
    }
    if(10<w){
      p+=125*(w-10);
      w=10;
    }
    p+=1150;
    printf("%d\n",4280-p);
  }
  return 0;
}

Measured memory_bytes: 600
#include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (int i = 0; i < (int)c; i++)
using ll = long long;
int main() {
    int n;
    scanf("%d", &n);
    vector<ll> a(n);
    // Nの合計 - xの合計 = yは合計、最小の|y - x|=|(N - x) - x|=N - 2 * x
    ll sum = 0;
    ll x = 0;
    ll ans = 1000000000000000000LL;
    rep(i, n) {
        scanf("%lld", &a[i]);
        sum += a[i];
    };
    rep(i, n) {
        x += a[i];
        // 必ず1枚以上選ぶ必要があるので
        if (i + 1 < n) ans = min(ans, abs(sum - 2 * x));
    }
    printf("%lld\n", ans);
    return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; i++)
#define ll long long int

using namespace std;

int main() {
  int n,k;
  cin >> n >> k;
  cout << n -k +1 << endl;

}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	static int min;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int[] s = new int[M];
		int[] c = new int[M];
		for(int i = 0;i < M;i++){
			s[i] = sc.nextInt();
			c[i] = sc.nextInt();
		}
		sc.close();
		if(N==1){
			min = 0;
		}else{
			min = (int)Math.pow(10, N-1);
		}
		for(int i = min;i < (int)Math.pow(10, N);i++){
			String str = String.valueOf(i);
			boolean b = true;
			for(int j = 0;j < M;j++){
				if(!str.substring(s[j]-1,s[j]).equals(String.valueOf(c[j]))){
					b = false;
					break;
				}
			}
			if(b){
				System.out.println(i);
				return;
			}
		}
		System.out.println(-1);
	}
}

Measured memory_bytes: 21716
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define MOD 1000000007
#define INF 1000000000
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\n"
#endif
#ifndef __DEBUG__
#define CH_P(a) 
#define DEBUG(x) 
#define DEBUGS(v) 
#endif
#define TIMER_S(start) chrono::system_clock::time_point start = chrono::system_clock::now();
#define TIMER_E(end) chrono::system_clock::time_point end = chrono::system_clock::now();
#define TIME(start, end) cout << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count()) << "ms" << "\n";
/* clang-format on */

int main() {
  ll n, q;
  ll sum = 0;
  cin >> n;
  vector<ll> a(n);
  int cnt[100001];
  REP(i, 100001) { cnt[i] = 0; }
  REP(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  REP(i, q) {
    ll b, c;
    cin >> b >> c;
    if (cnt[b] > 0) {
      sum -= cnt[b] * b;
      sum += cnt[b] * c;
      cnt[c] += cnt[b];
      cnt[b] = 0;
    }
    cout << sum << "\n";
  }
  return 0;
}


Measured memory_bytes: 4572
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define REPS(i,n) for (ll i = 1; i<=(ll)(n); i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653

#define N 210000

vector<ll> a(N,-100000000000); //探索するものを定義しておく,初期値設定の値に注意!!
//大きい順に並んでいて、自分より小さいものの最小のindexを見つける
//自分より大きいもの見つけたければ昇順のvectorに対して不等号逆にする
bool isOK(ll index, ll key){ 
    if (a.at(index) > key) return true; 
    else return false;
}

ll binse(ll key, ll sizem){ //sizem番目まで二分探索
	ll left = -1;
	ll right = sizem;
	while (right-left>1){
		ll mid = left+(right-left)/2;
		if (isOK(mid, key)) right = mid;
		else left = mid;
	}
	return right; //自分より大きい、または小さいものの最小のindex、なければsizemを返す
}

bool isOK2(ll index, ll key){ 
    if (a.at(index) < key) return true; 
    else return false;
}

ll binse2(ll key, ll sizem){ //sizem番目まで二分探索
	ll left = -1;
	ll right = sizem;
	while (right-left>1){
		ll mid = left+(right-left)/2;
		if (isOK2(mid, key)) left = mid;
		else right = mid;
	}
	return left; //自分より大きい、または小さいものの最小のindex、なければsizemを返す
}

int main(){
	ll n, x; cin >> n;
	vector<ll> sa(n);
	FOR(i,1,n){
		cin >> x;
		a[i-1] = i+x;
		sa[i-1] = i-x;
	}
	sort(a.begin(),a.end());
	sort(sa.begin(),sa.end());
	ll ans = 0;
	REP(i,n){
		//cout << i << endl;
		//cout << binse(sa[i],N-1) << " " << binse2(sa[i],N) << endl;
		 ans += binse(sa[i],N-1)-binse2(sa[i],N)-1;
	}
	cout << ans << endl;
}

Measured memory_bytes: 6376
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <queue>
#include <deque>
#include <algorithm>
#include <stack>
#include <functional>
#include <sstream>
#define rep(i,j,n) for(i=j;i<(n);i++)
#define rrep(i,j,n) for(i=j;i>(n);i--)
typedef long long int lli;
#define N 1000000007
#define M 998244353

using namespace std;

int main(){
    lli n,m,p,y;
    vector<lli> data;
    map<lli,lli> mp,year;
    lli i,j;
    cin>>n>>m;
    rep(i,0,m){
        cin>>p>>y;
        year[y]=p;
        data.push_back(y);
    }
    for(auto itr=year.begin();itr!=year.end();itr++){
        mp[itr->second]++;
        itr->second=itr->second*1000000+mp[itr->second];
//            cout<<year[i]<<"認証番号とする"<<endl;
    }
    rep(i,0,data.size()){
//        cout<<year[data[i]]<<endl;
        cout<<setw(12)<<setfill('0')<<year[data[i]]<<endl;
    }
}


Measured memory_bytes: 14708
#include <bits/stdc++.h>
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++) 
#define FOR(i, a, b) for(int (i) = a; (i) < (int)b; i++)
#define RREP(i, n) for(int (i)=((int)(n)-1); (i)>=0; i--)
#define RFOR(i, a, b) for(int (i) =((int)(b)-1); (i)>=(int)a; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define SZ(x)  (int)x.size()
#define SP(x)  setprecision((int)x)


using namespace std ;
typedef long long ll;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<string> vstr;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} //最小公倍数


int main()
{
  vector<int> vec(4,0) ;
  REP(i,3){
    int a, b ;
    cin >> a >> b ;
    vec.at(a-1)++ ;
    vec.at(b-1)++ ;
  }
  REP(i,4){
    if(vec.at(i)<=0 || vec.at(i)>=3){
      cout << "NO"<<endl ;
      return 0 ;
    }
  }
  
  cout << "YES"<<endl ;

    


  return 0 ;
}




Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class T>
istream& operator >> (istream& is, vector<T>& v)
{
	for (auto &i : v) is >> i;
	return is;
}
template<class T>
ostream& operator<<(ostream& os, vector<T>& v)
{
	const string delimiter = "\n";
	REP(i, v.size())
	{
		os << v[i];
		if (i != v.size() - 1) os << delimiter;
	}
	return os;
}
/*--------------------template--------------------*/

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	int n; cin >> n;
	vi v(n); cin >> v;
	sort(v.rbegin(), v.rend());
	int a = 0, b = 0;
	REP(i, n)
	{
		if (i % 2) b += v[i];
		else a += v[i];
	}
	cout << a - b << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <set>

using namespace std;

int main() {
  int n, m, a, b;
  while (true) {
    set<int> friends[500];
    set<int> my_friends;
    cin >> n >> m;
    if (n == 0) {
      return 0;
    }
    for (int i = 0; i < m; ++i) {
      cin >> a >> b;
      friends[a - 1].insert(b - 1);
      friends[b - 1].insert(a - 1);
    }
    my_friends.insert(0);
    for (int i = 0; i < 2; ++i) {
      set<int> tmp;
      for (set<int>::iterator it = my_friends.begin(); it != my_friends.end(); ++it) {
        for (set<int>::iterator it2 = friends[*it].begin(); it2 != friends[*it].end(); ++it2) {
          tmp.insert(*it2);
        }
      }
      my_friends.insert(tmp.begin(), tmp.end());
    }
    cout << my_friends.size() - 1 << endl;
  }
}

Measured memory_bytes: 2196