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

public class Main {

    public Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
	    new Main().solveA();
    }
    public int[] dx8 = { 1, 1,  1, 0,  0, -1, -1, -1 };
    public int[] dy8 = { 1, 0, -1, 1, -1,  1,  0, -1 };

    public void solveA() {
        int N = in.nextInt();
        int[] L = new int[2*N];
        for (int i = 0; i < 2 * N; i++)
            L[i] = in.nextInt();
        Arrays.sort(L);

        int res = 0;
        for (int i = 0; i < 2 * N; i += 2) res += L[i];

        System.out.println(res);
    }

    class Pair<T1 extends Comparable<T1>, T2 extends Comparable<T2>> implements Comparable<Pair<T1, T2>>{
        public T1 first;
        public T2 second;
        public Pair(T1 f, T2 s) {
            first = f;
            second = s;
        }

        @Override
        public int compareTo(Pair<T1, T2> o) {
            int res = first.compareTo(o.first);
            if (res != 0) return res;
            return second.compareTo(o.second);
        }
    }
}


Measured memory_bytes: 10068
import java.util.Scanner;

public class Main {
	char[] c;
	int[] dp;
	char[][] w = { "dream".toCharArray(),
			"dreamer".toCharArray(),
			"erase".toCharArray(),
			"eraser".toCharArray() };

	int dp(int k) {
		if (c.length <= k) {
			return 1;
		}
		if (dp[k] != 0) {
			return dp[k];
		}

		int ret = -1;
		for (int i = 0; i < 4; i++) {
			if (check(k, i)) {
				ret = Math.max(ret, dp(k + w[i].length));
			}
		}

		return dp[k] = ret;
	}

	boolean check(int i, int k) {
		for (int j = 0; j < w[k].length; j++) {
			if (c.length <= i + j) {
				return false;
			}
			if (c[i + j] != w[k][j]) {
				return false;
			}
		}
		return true;
	}

	void run() {
		Scanner sc = new Scanner(System.in);

		c = sc.next().toCharArray();
		dp = new int[c.length];
		System.out.println(dp(0) == 1 ? "YES" : "NO");
	}

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


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

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    vector<int> c(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    rep(i, n - 1) cin >> c[i];
    c[n - 1] = 0;
    int sum = 0;
    rep(i, n)
    {
        int flag = a[i];
        if (a[i] == a[i - 1] + 1)
            sum += b[flag - 1] + c[flag - 2];
        else
            sum += b[flag - 1];
    }
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

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

    for (int i = 0; ; i++) {
        int D = M / N - i;
        if (M % D == 0) {
            cout << D << endl;
            return 0;
        }
    }

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define two(a) (1<<(a))
#define LINF (1ll<<61)
#define EPS (1e-14)
#define Lshift(a,b) ((a)<<(b))
#define Rshift(a,b) ((a)>>(b))
#define rep(a,b) for(a=0 ; a<b ; a++)
#define xrep(a,b,c) for(a=b ; a<c ; a++)
#define INF (1<<29)
#define sz(a) int(a.size())
#define mset(x) memset(x, 0, sizeof(x))
#define swap(a,b) ( (a^=b) , (b^=a) , (a^=b) )
#define GET(x) (mark[x>>5]>>(x&31)&1)
#define SET(x) (mark[x>>5]|=1<<(x&31))
#define maxL (10000000>>5)+1
#define mod 1000000007
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define si set<int>
#define pb push_back
typedef long long ll;
using namespace std;


int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    ll n, m, k;
    cin >> n >> m >> k;
    vector<ll> ba(n), bb(m);
    ll i;
    ll tt(0), ia(0);
    rep(i, n) cin >> ba[i];
    rep(i, m) cin >> bb[i];
    rep(ia, n) {
        if (tt + ba[ia] <= k) {
            tt += ba[ia];
        } else break;
    }
    ll ans(ia), ib(0);
    rep(ib, m) {
        while (ia >= 0 && tt + bb[ib] > k) {
            ia--;
            if (ia < 0) break;
            tt -= ba[ia];
        }
        if (tt + bb[ib] > k) break;
        tt += bb[ib];
        ans = max(ans, ia + ib + 1);
    }
    cout << ans << endl;
}


Measured memory_bytes: 6376
#include <stdio.h>

int main()
{
    int A, B, C, menor;
    scanf("%d %d %d", &A, &B, &C);
    if ((A < B && A < C) || (A <= B && A < C) || (A < B && A <= C))
    {
        menor = A;
        if ((menor + B) < (menor + C))
            printf("%d", (menor + B));
        else if ((menor + C) <= (menor + B))
            printf("%d", (menor + C));
    }
    else if ((B < A && B < C) || (B <= A && B < C) || (B < A && B <= C))
    {
        menor = B;
        if ((menor + A) < (menor + C))
            printf("%d", (menor + A));
        else if ((menor + C) <= (menor + A))
            printf("%d", (menor + C));
    }
    else if ((C < A && C < B) || (C <= A && C < B) || (C < A && C <= B))
    {
        menor = C;
        if ((menor + B) < (menor + A))
            printf("%d", (menor + B));
        else if ((menor + A) <= (menor + B))
            printf("%d", (menor + A));
    }
    else if (A == B && A == C)
    {
        printf("%d", (A + B));
    }

    return 0;
}

Measured memory_bytes: 128
#include "bits/stdc++.h"
using namespace std;

#define fi first
#define se second
#define pb push_back
#define int long long
#define all(x) (x).begin(), (x).end()

using LL = long long;
using LD = long double;
using pii = pair<int, int>;
using vii = vector<pii>;

const int INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;

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

    int n;
    cin >> n;

    vector<int> a(n);
    map<int, int> cnt;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        cnt[a[i]]++;
    }

    int ans = 0;
    for (auto i: cnt) {
        ans += i.se*(i.se-1)/2;
    }

    for (int i = 0; i < n; i++) {
        int c = cnt[a[i]];
        cout << ans - c*(c-1)/2 + (c-1)*(c-2)/2 << "\n";
    }
}

Measured memory_bytes: 11136
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] xab = scanner.nextLine().split(" ", 3);
        int x = Integer.parseInt(xab[0]);
        int a = Integer.parseInt(xab[1]);
        int b = Integer.parseInt(xab[2]);

        int aDist = Math.abs(x - a);
        int bDist = Math.abs(x - b);
        if (aDist < bDist) {
            System.out.println("A");
        } else {
            System.out.println("B");
        }
    }
}


Measured memory_bytes: 22740
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
   int n,k;
   cin>>n>>k;
   vector<int>arr(n);
   for(int i=0;i<n;i++)
   {
     cin>>arr[i];
   }
   vector<int>dist(n,INT32_MAX);
   dist[0]=0;
   dist[1]=abs(arr[0]-arr[1]);
   for(int i=2;i<n;i++)
{
    for(int j=i-1;j>=i-k;j--)
    {
        if(j>=0)
        {
            dist[i]=min(dist[i],dist[j]+abs(arr[i]-arr[j]));
        }
    }
}
cout<<dist[n-1];
return 0;
}

Measured memory_bytes: 4040
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>
#include<limits>
#include<bitset>
#include<functional>
#include<type_traits>
#include<queue>
#include<stack>
#include<array>
#include<random>
#include<utility>
#include<cstdlib>
#include<ctime>

using namespace std;

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

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

typedef long long ll;
long long INF = 1LL<<60;
long long dp[100010];
int h[100010];
string s;
ll ans=0;
void toku(ll,ll);
void solve(){
toku(0,0);
}
void toku(ll index,ll vton){
    if(index==s.size()){ans+= vton;return;}
    for(int i=1;i+index<=s.size();i++){
        string temp=s.substr(index,i);
        ll v=0;
        for(ll k=0;k<temp.size();k++){
            v*=10;
            v+=temp[k]-'0';
        }
        toku(index+i,vton+v);
    }
}
int main(){
    cin>>s;
    solve();
    cout<<ans<<endl;
    return 0;
}




Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define ld long double
#define MOD 1000000007
#define nl cout << bl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test() \
    ull t;     \
    cin >> t;  \
    while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define oa(a, n)               \
    for (ll i = 0; i < n; i++) \
        cout << a[i] << " ";
#define cn(a, n)               \
    for (ll i = 0; i < n; i++) \
        cin >> a[i];
#define ov(a, n)                    \
    for (ll i = n - 1; i >= 0; i--) \
        cout << a[i] << " ";        \
    nl
#define ovp(a, n)              \
    for (ll i = 0; i < n; i++) \
        cout << a[i].ff << " " << a[i].ss << bl;
#define maxa(ar, N) *max_element(ar, ar + N)
#define mina(ar, N) *min_element(ar, ar + N)
#define present(c, x) ((c).find(x) != (c).end())
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
typedef pair<int, int> pii;
ll fast_exp(ll base, ll exp)
{
    ll res = 1;
    while (exp > 0)
    {
        if (exp % 2 == 1)
            res = (res * base) % MOD;
        base = (base * base) % MOD;
        exp /= 2;
    }
    return res % MOD;
}
ll fib(ll n)
{
    if (n < 2)
        return n;
    ll a = 0, b = 1, pre;
    int i = 1;
    while (i < n)
    {
        pre = (a + b) % MOD;
        a = b;
        b = pre;
        i++;
    }
    return pre;
}
ll fac(ll n)
{
    if (n % 2 == 0)
        return 2;

    for (ll i = 3; i * i <= n; i += 2)
    {
        if (n % i == 0)
            return i;
    }

    return n;
}
ll fact(ll n)
{
    if (n > 1)
        return n * fact(n - 1);
    else
        return 1;
}
bool sortbysec(const tuple<int, int, int> &a,
               const tuple<int, int, int> &b)
{
    return (get<1>(a) < get<1>(b));
}

int main()
{
    int n;
    cin >> n;
    int a[n];
    cn(a, n);
    int count = 0;
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0 and a[i] % 2 != 0)
        {
            count++;
        }
    }
    cout << count;
}

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

using namespace std;

#define int long long
#define PB push_back
#define BP while(1){}
typedef pair<int, int> pii;
static const int INF = 1LL<<61;

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

  int n, k;
  while (cin >> n >> k, n) {
    bool c[100005] = {false};
    for (int i = 0; i < k; ++i) {
      int t;
      cin >> t;
      c[t] = true;
    }

    int ans = 0, number[100005] = {0}, sum = 0;
    bool flg = false;
    for (int i = 1; i <= n; ++i) {
      if (!c[i]) continue;
      number[i] += 1 + number[i - 1];
    }

    for (int i = 1; i <= n; ++i) {
      if (c[i]) {
        sum++;
      } else if (!flg && c[0]) {
        flg = true;
        sum++;
      } else {
        flg = true;
        ans = max(ans, sum);
        if (c[0]) {
          sum = number[i - 1] + 1;
        } else
          sum = 0;
      }
    }

    cout << max(ans, sum) << endl;
  }

  return 0;
}

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

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

vector<int> g[555];
int m, n;
int x, y;

int main(int argc, char *argv[])
{
  while(cin >> n >> m, n){
    rep(i, n) g[i].clear();
    rep(i, m){
      cin >> x >> y;
      g[x - 1].push_back(y - 1);
      g[y - 1].push_back(x - 1);
    }

    vector<int> can, can1;
    rep(i, g[0].size()) can.push_back(g[0][i]);
    rep(i, can.size()) rep(j, g[can[i]].size()) can1.push_back(g[can[i]][j]);
    can.insert(can.end(), can1.begin(), can1.end());
    sort(can.begin(), can.end());
    can.erase(unique(can.begin(), can.end()), can.end());

    cout << max<int>(0, can.size() - 1) << endl;
  }
  
  return 0;
}

Measured memory_bytes: 1380
# -*- coding: utf-8 -*-

N = int(input())
A = [int(i) for i in input().split()]

# min(A)より小さな値が作れるか検証していく感じか？
A.sort()
l = len(A)

tmp1 = list(A)
tmp2 = []

while len(tmp1) > 1:
    m = tmp1[0]
    tmp2.append(m)
    for d in tmp1:
        a = d % m
        if 0 < a < d:
            tmp2.append(a)
    tmp2.sort()
    tmp1 = tmp2
    tmp2 = []

print(tmp1[0])


Measured memory_bytes: 14252
#include<stdio.h>
int main()
{
 int a[100]={0};
  int N,i,j,tmp,Alice=0,Bob=0,point;
  scanf("%d",&N);
   for(i=0;i<N;i++)
  {
   scanf("%d",&a[i]);
  }
   for(j=0;j<N;j++)
  {
  for(i=0;i<N-1;i++)
  {
    if(a[i]<=a[i+1])
    {
      tmp=a[i];
      a[i]=a[i+1];
      a[i+1]=tmp;
    }
  }
  } 
     for(i=0;i<=N;i++)
     {if(i%2==0){Alice=Alice+a[i]; }
      else{Bob=Bob+a[i];}
     }  
     point=Alice-Bob;
  printf("%d",point);
  return 0;
}

Measured memory_bytes: 2072
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7;
const ll LINF=1e18;
using namespace std;
#define int long long
//template
//main
signed main(){
  int N,M;cin>>N>>M;
  std::vector<int> blwh(N,0);
  std::vector<bool> used(N,0);
  std::vector<int> edge[N];
  for(int i=0;i<M;i++){
    int a,b;cin>>a>>b;a--;b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  stack<int> st;
  st.push(0);blwh[0]=1;
  while(st.size()){
    int p=st.top();st.pop();
    if(used[p])continue;
    used[p]=true;
    for(int q:edge[p]){
      if(blwh[q]==blwh[p]*(-1))continue;
      if(blwh[q]==0){
        blwh[q]=blwh[p]*(-1);
        st.push(q);
      }
      else{
        cout<<N*(N-1)/2-M<<endl;
        return 0;
      }
    }
  }
  int B=0,H=0;
  for(int q:blwh)if(q==1)B++;
  H=N-B;
  cout<<B*H-M<<endl;
}


Measured memory_bytes: 7168
package main

import "fmt"

func main() {
	var a, b, c, x int
	fmt.Scan(&a, &b, &c, &x)

	var cnt int
	for i := 0; i <= a; i++ {
		for j := 0; j <= b; j++ {
			for k := 0; k <= c; k++ {
				if 500*i+100*j+50*k == x {
					cnt++
				}
			}
		}
	}

	fmt.Println(cnt)
}


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

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

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

  cout << min(b, a * n) << '\n';

  return (0);
}


Measured memory_bytes: 3572
import java.io.*;
import java.util.Scanner;


public class Main {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int ab = sc.nextInt();
		int bc = sc.nextInt();
		sc.nextInt();

		pr.println(ab * bc / 2);
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);
			
		solve();
			
		pr.close();
		sc.close();
	}

	static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}


Measured memory_bytes: 23764
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;


int main() {
	long long h, a;
	cin >> h >> a;
	cout << h / a + !(!(h % a));
}

Measured memory_bytes: 256
<?php

$stdin = preg_split('/ /', trim(fgets(STDIN)));
$a = intval($stdin[0]);
$b = intval($stdin[1]);
$c = intval($stdin[2]);
$count = 0;

for ($i = $a; $i <= $b; $i++) {
	if ($c % $i == 0) {
		$count++;
	}
}

print("{$count}\n");

Measured memory_bytes: 6644
H,W = map(int,input().split())
ls = [["#" for _ in range(W+2)]]

for _ in range(H):
  A = ["#"] + list(input()) + ["#"]
  ls.append(A)
  
ls.append(["#" for _ in range(W+2)])

for i in range(H+2):
  print("".join(ls[i]))

Measured memory_bytes: 9036
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void solve(int n)
{
    int arr[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];     
    }
    int dp[n+1];
    dp[1]=0;
    for(int i=2;i<=n;i++)
    {
        int opt1=dp[i-1]+abs(arr[i]-arr[i-1]);
        int opt2=(i==2)?INT_MAX:dp[i-2]+abs(arr[i]-arr[i-2]);
        dp[i]=min(opt1,opt2);        
    }
    cout<<dp[n]<<endl;
    return ;

}

int main() 
{
    int n;
    cin>>n;
    solve(n);
    return 0;
}


Measured memory_bytes: 1024
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define SORTD(a) sort(a.rbegin(), a.rend());
#define UNIQUE(a) unique(a.begin(), a.end())
#define ll long long
#define INF 1000000000000000000
#define INT_MAX 2147483647

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <numeric>
#include <algorithm>
#include <queue>
#include <set>
#include <utility>
#include <ios>
#include <iomanip>

using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

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

	ll a, b;
	ll ans = 1;
	int updown = -1;
	cin >> a;

	REP(i, n-1) {
		cin >> b;
		if (updown == 0 && b < a) {
			updown = -1;
			ans++;
		}
		else if (updown == 1 && b > a) {
			updown = -1;
			ans++;
		}
		else if (updown == -1) {
			if (a < b) {
				updown = 0;
			}
			else if (a > b) {
				updown = 1;
			}
		}
		a = b;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
import math
while True:
    try:
        n=int(input())
        if n==0:
            break
        else:
            x=list(map(int,input().split()))
            s=sum(x)
            ave=s/n
            y=[]
            for i in range(n):
                m=(x[i]-ave)*(x[i]-ave)
                y.append(m)
            a=sum(y)/n
            print(math.sqrt(a))
                
            
    except EOFError:
        pass



Measured memory_bytes: 5704
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin >> a >> b;
    for(int i=1;i<=1500;i++){
        if(((i*8)/100==a)&&((i*10)/100==b)){
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}

Measured memory_bytes: 256
from itertools import combinations_with_replacement as c
N, M, Q = map(int, input().split())
l1 = list(list(map(int, input().split())) for _ in range(Q))
ans = 0

for l2 in c(range(1, M + 1), N):
    total = 0
    for i in range(Q):
        if l2[l1[i][1] - 1] - l2[l1[i][0] - 1] == l1[i][2]:
            total += l1[i][3]
    ans = max(ans, total)

print(ans)

Measured memory_bytes: 9104
N = int(input())
C = input().split()
_C = C.copy()

for i in range(N):
    for j in range(N-1, i, -1):
        if (C[j][1] < C[j-1][1]):
            C[j], C[j-1] = C[j-1], C[j]
print(*C)
print("Stable")

for i in range(N):
    m = i
    for j in range(i, N):
        if (_C[j][1] < _C[m][1]):
            m = j
    _C[i], _C[m] = _C[m], _C[i]
print(*_C)
if(C ==_C):
    print("Stable")
else:
    print("Not stable")

Measured memory_bytes: 7732
#include <iostream>
using namespace std;
int solve(void){
    int X;
    cin >> X;
    int dp[100001];
    dp[0] = 1;
    for(int i=1;i<=99;i++){
        dp[i] = 0;
    }
    for(int i=100;i<=105;i++){
        dp[i] = 1;
    }
    for(int i=106;i<=X;i++){
        if(dp[i-100] || dp[i-101] || dp[i-102] || dp[i-103] || dp[i-104] || dp[i-105]){
            dp[i] = 1;
        }
    }
    return dp[X];
    
}
int main(void){
    
    int ans = solve();
    cout << ans << endl;
}


Measured memory_bytes: 3440
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=1100000,M=15000;
const int INF=0x7f7f7f7f;
struct node {
        int i,value;
        friend bool operator < (node a,node b){
                return a.value > b.value;
        }
};
struct edge{
	int to,value;
	int next;
}mem[N];
priority_queue<node> minq;
int head[N],size=0,n,m,start,end;
long long dis[N];
bool vis[N];
void  dijkstra(int start){
        dis[start]=0;
        node tmp={start,0};
        minq.push(tmp);
        while(!minq.empty()){
                tmp=minq.top();
                minq.pop();
                if(vis[tmp.i])continue;
                vis[tmp.i]=true;
                for(int j=head[tmp.i];j!=0;j=mem[j].next){
                		int to=mem[j].to;
                	    if(!vis[to]&&mem[j].value+dis[tmp.i]<dis[to]){
                            	dis[to]=mem[j].value+dis[tmp.i];
                                node tmp2={to,dis[to]};
                                minq.push(tmp2);
                        }
                }
        }
}
int main(){
        memset(head,0,sizeof(head));
        memset(dis,0x7f,sizeof(dis));
        memset(vis,false,sizeof(vis));
        scanf("%d",&n);m=n-1;
        int u,v,w;
        for(int i=1;i<=m;i++){
                scanf("%d %d %d",&u,&v,&w);
                size++;mem[size].to=v;mem[size].value=w;mem[size].next=head[u];head[u]=size;
                size++;mem[size].to=u;mem[size].value=w;mem[size].next=head[v];head[v]=size;
        }
        int q,k;
        cin>>q>>k;
        dijkstra(k);
        for(int i=1;i<=q;i++)
        {
        	int woyoubing,niyoubing;
        	cin>>woyoubing>>niyoubing;
        	cout<<dis[woyoubing]+dis[niyoubing]<<endl;
		}
        return 0;
}

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

int main() {
    int N, K; cin >> N >> K;
    long long h[N];
    for (int i = 0; i < N; i++)
    {
        cin >> h[i];
    }
    sort(h, h + N);

    long long ans = 1e9 + 10;
    for (int i = 0; i + K - 1 < N; i++)
    {
        ans = min(ans, h[K + i - 1] - h[i]);
    }
    cout << ans << endl;
}

Measured memory_bytes: 1152
x, n = map(int, input().split())
p = list(map(int,input().split()))
l = [i for i in range(x+101)]
for pi in p:
    l[pi] = 500
m = 10000
ans = 0
for li in l:
    if m > abs(x-li):
        m = abs(x-li)
        ans = li
print(ans)

Measured memory_bytes: 9188
#include<bits/stdc++.h>
#define LL long long
using namespace std;

map<int,int> mp;
vector<int> v;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        mp[x]++;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    LL fir=0;
    for(int i=v.size()-1;i>=0;i--){
        if(mp[v[i]]>=2&&fir){
            printf("%lld\n",(LL)v[i]*fir);
            return 0;
        }
        else if(mp[v[i]]>=4){
            printf("%lld\n",(LL)v[i]*(LL)v[i]);
            return 0;
        }
        else if (mp[v[i]]>=2){
            fir=(LL)(v[i]);
        }
    }
    puts("0");
    return 0;
}


Measured memory_bytes: 5368
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let p = arr(3, |_| (scan.read::<f64>(), scan.read::<f64>()));
    let a = dist(p[0], p[1]);
    let b = dist(p[1], p[2]);
    let c = dist(p[0], p[2]);
    let k = vec![Total(a), Total(b), Total(c)].into_iter().max().unwrap().0;
    let t = (a+b+c)/2.0;
    let s = (t*(t-a)*(t-b)*(t-c)).sqrt();
    let r = 2.0*s/(a+b+c);
    println!("{}", r*k/(2.0*r+k));
}

fn dist(p: (f64, f64), q: (f64, f64)) -> f64 {
    ((p.0 - q.0).powi(2) + (p.1 - q.1).powi(2)).sqrt()
}

#[derive(PartialEq, PartialOrd)]
pub struct Total<T>(pub T);

impl<T: PartialEq> Eq for Total<T> {}

impl<T: PartialOrd> Ord for Total<T> {
    fn cmp(&self, other: &Total<T>) -> std::cmp::Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}

// region template
#[allow(dead_code)]
fn iu(i: isize) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> isize { i as isize }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion

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

typedef long long ll;

const int mn=1e6+5;

int n,q,k;
char s[mn];
ll d,m,mc;

int main() {
#ifdef trote
    freopen("../1.txt","r",stdin);
#endif

    scanf("%d",&n);
    scanf("%s",s+1);
    scanf("%d",&k);
    while(k--){
        ll ans=0;
        scanf("%d",&q);
        int l=1;
        d=mc=m=0;
        for(int i=1;i<=n;i++){
            while(l<i&&(s[l]=='D'&&i-l>=q||s[l]!='D')){
                if (s[l]=='D'){
                    mc-=m;
                    d--;
                }
                else if (s[l]=='M') {
                    m--;
                }
                l++;
            }
            if (s[i]=='D') d++;
            else if (s[i]=='M'){
                mc+=d;
                m++;
            }
            else if (s[i]=='C'){
                ans+=mc;
            }

            //printf("%d %d %lld %lld %lld %lld\n",i,l,d,m,mc,ans);
        }
        printf("%lld\n",ans);
    }

    return 0;
}

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

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

    int a[n + 1];
    a[0] = 0;
    for (int i = 1; i <= n; i++) a[i] = a[i - 1] + w[i - 1];
    int ans = 1000000;
    for (int i = 1; i <= n; i++){
        ans = min(ans, abs(a[n] - 2 * a[i]));
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = LLONG_MAX;

int main()
{
    int H, W, h, w;
    cin >> H >> W >> h >> w;
    int ans = H * W - h * W - w * H + h * w;
    cout << ans << endl;
}


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

const int MOD = 1000000007;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define copy(from, to) copy(from.begin(), from.end(), back_inserter(to))
#define sort(a) sort(a.begin(), a.end())
#define reverse(s) reverse(s.begin(), s.end())
#define p(s) cout << (s) << endl

typedef long long ll;
using namespace std;

template<class T>
void load(vector<T> &v, int n) {
  for (int i = 0; i < n; ++i) {
    T t;
    cin >> t;
    v.push_back(t);
  }
}

template<class T>
T max(vector<T> &v) {
  return *max_element(v.begin(), v.end());
}

template<class T>
T min(vector<T> &v) {
  return *min_element(v.begin(), v.end());
}

void py(bool isUpper = false) {
  if (isUpper) {
    p("YES");
  } else {
    p("Yes");
  }
}

void pn(bool isUpper = false) {
  if (isUpper) {
    p("NO");
  } else {
    p("No");
  }
}

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

  int count = 0;
  for (int i = 1; i <= n; i += 2) {
    int c = 0;
    for (int j = 1; j <= i; ++j) {
      if (i % j == 0) {
        c++;
      }
    }
    if (c == 8) {
      count++;
    }
  }
  p(count);
}

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

public class Main {
    public static void main(String[] args) throws IOException {
        boolean[] isNotPrimeNumber = new boolean[1000000];
        isNotPrimeNumber[0] = true;
        for(int i = 1; i<isNotPrimeNumber.length; i++){
            int j = i + i + 1;
            while(j < isNotPrimeNumber.length) {
                isNotPrimeNumber[j] = true;
                j += i + 1;
            }
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String input;
        while(!(input = reader.readLine()).equals("0 0 0")){
            String[] tmp = input.split(" ");
            int a = Integer.parseInt(tmp[0]);
            int d = Integer.parseInt(tmp[1]);
            int n = Integer.parseInt(tmp[2]);
            int count = 0;
            while(true){
                if(!isNotPrimeNumber[a-1]){
                    count++;
                    if(count == n){
                        System.out.println(a);
                        break;
                    }
                }
                a += d;
            }
        }
    }
}

Measured memory_bytes: 22160
#include "bits/stdc++.h"
using namespace std;
const int mod = 998244353;
typedef long long ll;

ll pw(ll a, ll b) {
    ll r = 1;
    for( ; b ; b >>= 1, a = a * a % mod) {
        if(b & 1) {
            r = r * a % mod;
        }
    }
    return r;
}

ll inv(ll n) {
    return pw(n, mod - 2);
}

const int N = 1e6 + 10;

ll fac[N], invfac[N], ans;
int n, m;
ll C(int n, int m) {
    return n < m || m < 0 ? 0 : fac[n] * invfac[m] % mod * invfac[n - m] % mod;
}
ll calc(int n, int m) {
    return C(n + m, n);
}

int main() {
    scanf("%d%d", &n, &m);
    fac[0] = invfac[0] = 1;
    int lim = N - 1;
    for(int i = 1 ; i <= lim ; ++ i) fac[i] = fac[i - 1] * i % mod;
    invfac[lim] = pw(fac[lim], mod - 2);
    for(int i = lim - 1 ; i ; -- i) invfac[i] = invfac[i + 1] * (i + 1) % mod;
    ll inv2 = inv(2);
    for(int i = 1 ; i <= min(n, m) ; ++ i) {
        ans = (ans + calc(i, i) * calc(n - i, m - i) % mod * inv2 % mod) % mod;
    }
    ans = ans * inv(calc(n, m)) % mod;
    ans = (ans + max(n, m)) % mod;
    printf("%lld\n", (ans + mod) % mod);
}


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

using namespace std;
using ll = long long;

//container util
#define SORT(c) sort((c).begin(),(c).end())
#define all(a) (a).begin(), (a).end()

//repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//print
#define pv(val) cerr << #val << '=' << (val) << endl

const int INF = 1e9;

int main(void){

	int A, B; cin >> A >> B;
	cout << A * B << endl;


	return 0;

}


Measured memory_bytes: 3636
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部変数
static FILE *szpFpI;											// 入力
static long long slA, slB;										// A,B
static int siC, siD;											// C,D
static long long slLcm;											// C,Dの最小公倍数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// 出力
int
fOut(
	char *pcpLine				// <I> １行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// 最大公約数 - 取得
int
fGetGcd(
	int piVal1					// <I> 値１
	, int piVal2				// <I> 値２
)
{
	int liWork;

	// ユークリッドの互除法
	if (piVal1 < piVal2) {
		liWork = piVal1;
		piVal1 = piVal2;
		piVal2 = liWork;
	}
	while (piVal2 > 0) {
		liWork = piVal1 % piVal2;
		piVal1 = piVal2;
		piVal2 = liWork;
	}

	return piVal1;
}

// 最小公倍数 - 取得
long long
fGetLcm(
	int piVal1					// <I> 値１
	, int piVal2				// <I> 値２
)
{
	// 最大公約数 - 取得
	int liGcd = fGetGcd(piVal1, piVal2);

	// 約数
	int liVal1 = piVal1 / liGcd;
	int liVal2 = piVal2 / liGcd;

	return (long long)liVal1 * (long long)liVal2 * (long long)liGcd;
}

// 個数 - 取得
long long
fGetCnt(
	long long plVal				// <I> 対象値
)
{
	long long llCnt = plVal;
	llCnt -= plVal / (long long)siC;
	llCnt -= plVal / (long long)siD;
	llCnt += plVal / slLcm;

	return llCnt;
}

// 実行メイン
long long
fMain(
)
{
	char lc1Buf[1024];

	// A,B,C,D - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%lld%lld%d%d", &slA, &slB, &siC, &siD);

	// C,Dの最小公倍数 - 取得
	slLcm = fGetLcm(siC, siD);

	// 個数 - 取得
	long long llCnt = fGetCnt(slB);
	llCnt -= fGetCnt(slA - 1);

	return llCnt;
}

// １回実行
int
fOne(
)
{
	long long llRet;
	char lc1Buf[1024];

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	llRet = fMain();

	// 出力
	sprintf(lc1Buf, "%lld\n", llRet);
	fOut(lc1Buf);

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}



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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll INF = 1ll<<60;

#define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;
//#define DEBUG(x) ;

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

vector<string> S;
ll N;

void f(string s, char mc)
{
	if (s.size() == N)
	{
		S.push_back(s);
		return;
	}

	for (char c = 'a'; c <= mc+1; ++c)
	{
		f(s + c, max(c, mc));
	}
}


int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> N;
	f("a", 'a');
	for (auto &e : S) std::cout << e << std::endl;
	
	return 0;
}


Measured memory_bytes: 7284
#include <iostream>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int main() {
  int n, a;
  cin >> n >> a;
  n %= 500;
  if (n <= a) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
#define TEMPLATE_USED
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) for(ll i=0;i<(n);++i)
#define _rep3(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2)(__VA_ARGS__)
#define _repe2(i,n) for(ll i=0;i<=(n);++i)
#define _repe3(i,a,b) for(ll i=(a);i<=(b);++i)
#define repe(...) _overload3(__VA_ARGS__,_repe3,_repe2)(__VA_ARGS__)
#define _rrep2(i,n) for(ll i=(n)-1;i>=0;i--)
#define _rrep3(i,a,b) for(ll i=(b)-1;i>=(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
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;}
template<class T>
using pq=priority_queue<T,vector<T>,greater<T>>;


void solve();

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

void solve(){
    ll n,k;cin>>n>>k;
    vl a(n);rep(i,n)cin>>a[i];
    rep(i,n)--a[i];

    vl d(n,-1);
    d[0]=0;
    ll pos=0;
    repe(i,1,n){
        --k;
        pos=a[pos];
        if(!k)break;
        if(d[pos]!=-1){
            k%=i-d[pos];
            break;
        }
        d[pos]=i;
    }
    while(k--){
        pos=a[pos];
    }
    cout<<pos+1<<endl;
}


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

int main()
{
	int A, B, C;

	cin >> A >> B >> C;

	if (A + B < B + C && A+B < A+C)cout << A + B;
	else if (A + C < B + C && A+C < A+B)cout << A + C;
	else cout << B + C;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <functional>
#include <queue>
//#include <stack>
#include <climits>
#include <map>
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define put(a) cout<<a<<endl;
#define all(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
using namespace std;
typedef long long ll;
//typedef unsigned long long ull;
typedef pair<int, int> P;

int n;

int main() {
    cin >> n;
    if(n==1){
        put("Hello World");
    }else{
        int a,b;
        cin >> a >> b;
        put(a+b);
    }
    return 0;
}


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

L = [0] * N
R = [0] * N
L[0] = A[0]
R[-1] = A[-1]
for i in range(1, N):
    L[i] = max(L[i-1], A[i])
for i in range(N-1)[::-1]:
    R[i] = max(R[i+1], A[i])

for i in range(N):
    print(max(0 if i == 0 else L[i-1], 0 if i == N-1 else R[i+1]))

Measured memory_bytes: 72664
def main():
    A = [list(map(int, input().split())) for i in range(3)]
    M = [[False] * 3 for i in range(3)]

    N = int(input())
    for i in range(N):
        b = int(input())
        for j, r in enumerate(A):
            for k, c in enumerate(r):
                if c == b:
                    M[j][k] = True

    for i in M:
        if all(i):
            print('Yes')
            return

    for i in list(zip(*M)):
        if all(i):
            print('Yes')
            return

    if (M[0][0] and M[1][1] and M[2][2]):
        print('Yes')
        return

    if (M[0][2] and M[1][1] and M[2][0]):
        print('Yes')
        return

    print('No')


main()


Measured memory_bytes: 3064
// 2020-08-26 00:00:32
// clang-format off
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }

// clang-format on
const int MX = 505;
int d[MX][MX], c[MX][MX];
ll e[3][MX];
void answer() {
  int n, m;
  cin >> n >> m;
  rep(i, m) rep(j, m) cin >> d[i][j];
  rep(i, n) rep(j, n) {
    cin >> c[i][j];
    c[i][j]--;
  }
  ll ans = 1e18;
  rep(i, n) rep(j, n) {
    int k = (i + j) % 3;
    rep(x, m) e[k][x] += d[c[i][j]][x];
  }
  rep(x, m) rep(y, m) rep(z, m) {
    if (x == y || y == z || z == x) continue;
    chmin(ans, e[0][x] + e[1][y] + e[2][z]);
  }
  cout << ans << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Measured memory_bytes: 4632
#include <iostream>

int main(void){
	int A, B;
	int K;
	bool isOk = false;

	std::cin >> K;
	std::cin >> A >> B;

	for (int num = A; num <= B; num++){
		if(num % K == 0){
			isOk = true;
			break;
		}
	}

	if(isOk){
		std::cout << "OK" << std::endl;
	}else{
		std::cout << "NG" << std::endl;
	}

	return 0;
}


Measured memory_bytes: 3588
N = int(input())
a = list(map(int,input().split()))
count=0
hantei = [i%2 for i in a]
while sum(a)%2 ==0 and sum(hantei)==0:
        a = [i/2 for i in a]
        hantei = [i%2 for i in a]
        count+=1
print(count)
    

Measured memory_bytes: 2940
#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
void read(int& val) { int x = 0; int bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int mod=998244353;
const int maxn = 1e5+7;

int n,m,a[maxn];
LL num[maxn];
LL sum=0;
int main(){

    read(n);
    fo(i,1,n) read(a[i]),num[a[i]]++,sum+=a[i];

    int q;read(q);
    fo(i,1,q){
        int b,c;
        read(b);read(c);
        if(num[b]){
            sum-=b*num[b];
            sum+=num[b]*c;
            num[c]+=num[b];
            num[b]=0;
        }
        cout<<sum<<endl;
    }

    return 0;
}


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

using namespace std;

const int N = 100005;

long long arr[N];
int main() {
    int n; long long k;
    cin >> n >> k;
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
        sum += arr[i];
    }
    long long x = 0, res = 0;
    for (int i = 40; i >= 0; i--) {
        int a = 0;
        for (int j = 1; j <= n; j++) {
            if (arr[j] & (1LL << i)) {
                a++;
            }
        }
        if ((x + (1LL << i)) <= k && a < (n + 1) / 2) {
            x += (1LL << i);
            res += 1LL * (n - a) * (1LL << i);
        } else {
            res += 1LL * a * (1LL << i);
        }
    }
    cout << max(res, sum) << endl;
    return 0;
}


Measured memory_bytes: 1024
#include <stdio.h>

int main(void)
{
	int M, N;
	int i, j;
	int num, max = 0;
	int ac;
	
	scanf("%d %d", &M, &N);
	for (i = 0; i < M; i++){
		num = 0;
		for (j = 0; j < N; j++){
			scanf("%d", &ac);
			num += ac;
		}
		
		if (max < num){
			max = num;
		}
	}
	
	printf("%d\n", max);
	
	return (0);
}

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

int main() {
  //位置A,B;
  //速さV,W;
  //時間T；
  double A,V,B,W,T;
  cin >> A >> V >> B >> W >> T;
  double X = (abs(A-B) / (V-W));
  if(X <= T && X > 0){
    cout << "YES" << endl;
    return 0;
  }
  cout << "NO" << endl;
}


Measured memory_bytes: 3764
#include <bits/stdc++.h>
#define ALL(a) (a).begin(),(a).end()
#define N_MAX 1000
using namespace std;

int main() {
	int N; cin >> N;
	string S; cin >> S;

	for (int i = 0; i < S.length(); i++) {
		if (S[i] + N <= 'Z') S[i] += N;
		else S[i] = (S[i] + N) - 26;
	}

	cout << S << endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  ll i, j, k, n, now;
  scanf("%lld%lld", &n, &k);
  if(k > (n - 1) * (n - 2) / 2) {
    printf("-1");
    return 0;
  }
  now = (n - 1) * (n - 2) / 2;
  vector<ll> a, b;
  for(i = 1; i < n; ++i) a.push_back(1), b.push_back(i + 1);
  for(i = 2; now > k; ++i) {
    for(j = i; j < n && now > k; ++j) --now, a.push_back(i), b.push_back(j + 1);
  }
  printf("%lld\n", a.size());
  for(i = 0; i < a.size(); ++i) printf("%lld %lld\n", a[i], b[i]);
  return 0;
}

Measured memory_bytes: 384
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  vector<vector<int>> card(3, vector<int>(3));
  vector<vector<bool>> hole(3, vector<bool>(3, false));

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> card.at(i).at(j);
    }
  }

  int n;
  cin >> n;

  for (int k = 0; k < n; k++) {
    int num;
    cin >> num;

    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (card.at(i).at(j) == num) {
          hole.at(i).at(j) = true;
          break;
        }
      }
    }
  }

  for (int i = 0; i < 3; i++) {
    bool flag = true;

    for (int j = 0; j < 3; j++) {
      if (!hole.at(i).at(j)) {
        flag = false;
        break;
      }
    }

    if (flag) {
      cout << "Yes" << endl;
      return 0;
    }
  }

  for (int i = 0; i < 3; i++) {
    bool flag = true;

    for (int j = 0; j < 3; j++) {
      if (!hole.at(j).at(i)) {
        flag = false;
        break;
      }
    }

    if (flag) {
      cout << "Yes" << endl;
      return 0;
    }
  }

  if (hole.at(0).at(0) && hole.at(1).at(1) && hole.at(2).at(2)) {
    cout << "Yes" << endl;
    return 0;
  }

  if (hole.at(2).at(0) && hole.at(1).at(1) && hole.at(0).at(2)) {
    cout << "Yes" << endl;
    return 0;
  }

  cout << "No" << endl;

  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> P;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)
/*
vector<char>al(26);
al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char>AL(26);
AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
vector<char>NUM(10);
	NUM = { '0','1','2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };
*/



int main() {
	int A, B;
	cin >> A >> B;
	cout << (A + B) % 24 << endl;
}


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

// → ↑ ← ↓
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};

int main() {
    int H, W;
    cin >> H >> W;
    vector<vector<int>> M(H, vector<int>(W));
    vector<vector<int>> T(H, vector<int>(W));
    map<int, Pair> mp;
    rep(i, H) rep(j, W) {
        cin >> M[i][j];
        mp[M[i][j]] = make_pair(i, j);
        T[i][j] = M[i][j];
    }
    rep(h, H * W) {
        int y = mp[h + 1].first;
        int x = mp[h + 1].second;
        set<int> st;
        rep(i, 4) {
            int ny = y + dy[i], nx = x + dx[i];
            if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
            if(M[y][x] > M[ny][nx]) {
                st.insert(T[ny][nx]);
            }
        }
        if(st.size() == 1) T[y][x] = *st.begin();
        else if(st.size() > 1) T[y][x] = 0;
    }
    int ans = 0;
    rep(i, H) rep(j, W) {
        if(T[i][j] == 0) ans++;
    }
    cout << ans << '\n';
    return 0;
}



Measured memory_bytes: 73196
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int main()
{
	int a, b, x;
	scanf("%d %d %d", &x, &a, &b);
	int ans;
	ans = (x - a) % b;
	printf("%d\n", ans);
}

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

const ll MOD = 998244353;

vector<int> E[200005];
bool used[200005];

ll dfs(int v) {
  used[v] = true;
  ll res = 1;
  for (int u: E[v]) {
    res *= dfs(u);
    res %= MOD;
  }
  return (res+1) % MOD;
}

int main() {
  int n;
  cin >> n;
  vector<P> X(n);
  rep(i, n) cin >> X[i].first >> X[i].second;
  sort(X.begin(), X.end());
  priority_queue<P, vector<P>, greater<P>> q;
  for (int i = n-1; i >= 0; --i) {
    int x = X[i].first, d = X[i].second;
    while (!q.empty() && q.top().first < x + d) {
      E[i].push_back(q.top().second);
      q.pop();
    }
    q.emplace(x, i);
  }
  ll ans = 1;
  rep(i, n) {
    if (used[i]) continue;
    ans *= dfs(i);
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 25348