akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define MAX_N 100000
#define MAX_P 200005
int fact[MAX_P];
int extgcd(int a,int b,int& x,int& y){
  int d=a;
  if(b!=0){
    d=extgcd(b,a%b,y,x);
    y-=(a/b)*x;
  }else{
    x=1;y=0;
  }
  return d;
}
int mod_inverse(int a,int m){
  int x,y;
  extgcd(a,m,x,y);
  return (m+x%m)%m;
}

int euler_phi(int n){
  int res=n;
  for(int i=2;i*i<=n;i++){
    if(n%i==0){
      res=res/i*(i-1);
      for(;n%i==0;n/=i);
    }
  }
  if(n!=1) res=res/n*(n-1);
  return res;
}

int euler[MAX_N];

void euler_phi2(){
  for(int i=0;i<MAX_N;i++) euler[i]=i;
  for(int i=2;i<MAX_N;i++){
    if(euler[i]==i){
      for(int j=i;j<MAX_N;j+=i) euler[j]=euler[j]/i*(i-1);
    }
  }
}

int mod_pow(int x,int n,int mod){
  int res=1;
  while(n>0){
    if(n&1) (res*=x)%=mod;
    (x*=x)%=mod;
    n>>=1;
  }
  return res;
}

void init(int p){
  fact[0]=1;
  for(int i=1;i<MAX_P;i++) fact[i]=(fact[i-1]*i)%p;
}

int mod_fact(int n,int p,int& e){
  e=0;
  if(n==0) return 1;
  int res=mod_fact(n/p,p,e);
  e+=n/p;
  if(n/p%2!=0)return res*(p-fact[n%p]) %p;
  return res*fact[n%p]%p;
} 
int mod_comb(int n,int k,int p){
  if(n==k||k==0) return 1;
  int e1,e2,e3;
  int a1=mod_fact(n,p,e1),a2=mod_fact(k,p,e2),a3=mod_fact(n-k,p,e3);
  if(e1>e2+e3) return 0;
  return a1*mod_inverse(a2*a3%p,p)%p;
}

signed main(){
  init(MOD);
  int n,m;
  cin>>n>>m;
  if(abs(n-m)>1){
    cout<<0<<endl;
    return 0;
  }
  int ans=(fact[n]*fact[m])%MOD;
  if(n==m) (ans*=2)%=MOD;
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 1792
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
 
sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**9
mod = 10**9 + 7
 
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
 
 
def main():
    n = I()
    e = collections.defaultdict(list)
    for _ in range(n-1):
        a,b,c = LI()
        e[a].append((b,c))
        e[b].append((a,c))
 
    # メソッドのみ版
    def search(s):
        d = collections.defaultdict(lambda: inf)
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True
 
            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))
 
        return d
 
    q,k = LI()
 
    d = search(k)
    r = []
    for _ in range(q):
        x,y = LI()
        r.append(d[x]+d[y])
 
    return '\n'.join(map(str,r))
 
 
 
print(main())

Measured memory_bytes: 121832
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <climits>
#include <cmath>
#include <unordered_map>

using namespace std;

typedef long long ll;


int main(void) {
  int N, M; cin >> N >> M;
  vector<int> L(M, 0), R(M, 0);
  for (int i = 0; i < M; i++) { cin >> L[i] >> R[i]; }

  int lmax = -1;
  int rmin = N + 1;
  for (int i = 0; i < M; i++) {
    lmax = max(lmax, L[i]);
    rmin = min(rmin, R[i]);
  }

  int count = 0;
  for (int i = 1; i <= N; i++) {
    if (lmax <= i && i <= rmin)
      count++;
  }
  cout << count << endl;
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <numeric>
#include <math.h>
using namespace std;
typedef long long ll;

bool check(int n){
    for(int i=2;i*i<=n;i++)if(n%i==0)return 0;
    return 1;
}

int main(){
   int n;cin>>n;
   vector<int> v;
   for(int i=11;i<=55555;i++){
       if(i%5==1&&check(i))v.push_back(i);
   }
   for(int i=0;i<n;i++)cout<<v[i]<<' ';
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type pair struct {
	a, b int
}

func main() {
	scanner := makeWordScanner()
	h := eGetInt(scanner)
	n := eGetInt(scanner)
	ps := make([]pair, n)
	for i := 0; i < n; i++ {
		a := eGetInt(scanner)
		b := eGetInt(scanner)
		ps[i] = pair{a, b}
	}
	// 動的計画法
	hs := make([]int, h+1)
	// HP1~最大まで
	for i := 1; i <= h; i++ {
		min := 10000 * 10000 // 魔力最大 * HP
		lost := min
		for j := 0; j < len(ps); j++ {
			// +aのダメージでHを超えられるならOK
			if i-ps[j].a < 0 {
				// aだけでhを超える
				lost = ps[j].b
			} else {
				lost = hs[i-ps[j].a] + ps[j].b
			}
			if lost < min {
				min = lost
			}
		}
		hs[i] = min
	}
	fmt.Println(hs[h])
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeCharScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanRunes)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func pow(a, n int) int {
	return int(math.Pow(float64(a), float64(n)))
}
func countBitOfOn(i, numbits int) int {
	count := 0
	var bit uint64
	for bit = 0; bit < uint64(numbits); bit++ {
		t := 1 << bit
		if i&t > 0 {
			count++
		}
	}
	return count
}


Measured memory_bytes: 768
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    string s;
    cin >> s;
    output(s.substr(0, s.size() - 8));
    
    return 0;
}


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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
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;
}

int main() {
  int N; cin >> N;
  string S, T; cin >> S >> T;
  vector<char> ans;

  REP(i, N) {
    ans.push_back(S[i]);
    ans.push_back(T[i]);
  }

  REP(i, 2 * N) {
    cout << ans.at(i);
  }
  cout << endl;
}

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

int N;
int main (){
    cin >> N;
    int n = N;
    int sum = 0;
    for(int i=0; i<10; i++) {
        sum += n % 10;
        n /= 10;
    }
    if( N % sum == 0 ) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}


Measured memory_bytes: 256
package main

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

var sc *bufio.Scanner
var out *bufio.Writer

func main() {
	sc = bufio.NewScanner(os.Stdin)
	//bytes, _ := ioutil.ReadFile("./stdin.txt")
	//sc = bufio.NewScanner(strings.NewReader(string(bytes)))
	out = bufio.NewWriter(os.Stdout)
	buf := make([]byte, 1<<20)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)
	defer out.Flush()

	n, s, q := nextInt(), next(), nextInt()

	t := NewIntSegmentTree(n, func(a, b int) int { return a | b }, 0)
	for i := 0; i < n; i++ {
		c := s[i] - 'a'
		t.node[i+t.size-1] = 1 << c
	}
	t.Build()
	for i := 0; i < q; i++ {
		com := nextInt()
		switch com {
		case 1:
			a, b := nextInt(), next()
			c := b[0] - 'a'
			t.Update(a-1, monoid1(1<<c))
		case 2:
			a, b := nextInt(), nextInt()
			query := t.Query(a-1, b)
			ans := 0
			for query > 0 {
				if query&1 == 1 {
					ans++
				}
				query >>= 1
			}
			fmt.Fprintln(out, ans)
		}
	}
}

func next() string {
	sc.Scan()
	return sc.Text()
}
func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

type monoid1 int
type monoid2 monoid1

type SegmentTree struct {
	node []monoid1
	size int
	opNN func(monoid1, monoid1) monoid1
	en   monoid1
}

func NewIntSegmentTree(n int, nn func(int, int) int, en int) *SegmentTree {
	cnn := func(a monoid1, b monoid1) monoid1 {
		return monoid1(nn(int(a), int(b)))
	}
	return NewSegmentTree(n, cnn, monoid1(en))
}

func NewSegmentTree(n int, nn func(monoid1, monoid1) monoid1, en monoid1) *SegmentTree {
	size := 1
	for size < n {
		size <<= 1
	}
	t := &SegmentTree{
		node: make([]monoid1, 2*size-1),
		size: size,
		opNN: nn,
		en:   en,
	}
	for i := 0; i < size; i++ {
		k := i + size - 1
		t.node[k] = en
	}
	return t
}

func (t *SegmentTree) Build() {
	for k := t.size - 2; k >= 0; k-- {
		t.node[k] = t.opNN(t.node[2*k+1], t.node[2*k+2])
	}
}
func (t *SegmentTree) Update(i int, value monoid1) {
	k := i + t.size - 1
	t.node[k] = value
	for k > 0 {
		k = (k - 1) >> 1
		t.node[k] = t.opNN(t.node[2*k+1], t.node[2*k+2])
	}
}
func (t *SegmentTree) Query(a, b int) monoid1 {
	return t.querySub(a, b, 0, 0, t.size)
}
func (t *SegmentTree) querySub(a, b, k, l, r int) monoid1 {
	if b <= l || r <= a {
		return t.en
	}
	if a <= l && r <= b {
		return t.node[k]
	}
	vl := t.querySub(a, b, 2*k+1, l, (l+r)/2)
	vr := t.querySub(a, b, 2*k+2, (l+r)/2, r)
	return t.opNN(vl, vr)
}


Measured memory_bytes: 12104
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

int main(){
  int index ,flag=0 ,temp= 'z' - '`' ,cnt=0;
  string s ,key[3]={ "the" ,"this" ,"that" };

  while( true ){
    flag = 0;
    getline( cin ,s );
    if( cin.eof() ){
      break;
    }

    while( true ){
      for( int i=0 ; i<s.length()+1 ; i++ ){ // シーザー暗号を戻す
	if( s[i] != '.' && s[i] != ' ' && s[i] != '\n' ){ //　記号はスルー
	  s[i]++;
	  if( s[i] > 'z' ){ // zからaに飛ばす
	    s[i] = 'a';
	  }
	}
      }

      for( int i=0 ; i<3 ; i++ ){
	index = s.find( key[i] ,0 ); // the ,this ,that を探す
	if( index != string::npos ){ // あった
	  cout << s << endl;
	  flag = 1;
	  break;
	}
      }

      if( flag == 1 ){
	break;
      }
    }
  }
}

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

int main() {
	int h, w;
	cin >> h >> w;
	char grid[h][w];
	char grid2[h][w];
	int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
	for(int i = 0; i < h; i++)
	{
		for(int j = 0; j < w; j++)
		{
			cin >> grid[i][j];
			grid2[i][j] = '.';
		}
	}
	for(int i = 0; i < h; i++)
	{
		for(int j = 0; j < w; j++)
		{
			if(grid[i][j] == '#')
			{
				for(int d = 0; d < 4; d++)
				{
					int ni = i + dx[d];
					int nj = j + dy[d];
					if(ni >= 0 && ni < h && nj >= 0 && nj < w)
					{
						if(grid[ni][nj]=='#')
						{
							grid2[ni][nj] = '#';
							grid2[i][j] = '#';
						}
					}
				}
			}
		}
	}
	
	for(int i = 0; i < h; i++)
	{
		for(int j = 0; j < w; j++)
		{
			if(grid2[i][j] != grid[i][j])
			{
				cout << "No";
				return 0;
			}
		}
	}
	cout << "Yes";
	return 0;
}

Measured memory_bytes: 256
from math import gcd
import sys
sys.setrecursionlimit(1000000)

def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = gcd(A[0],A[1])
    set_A = set(A)
    for i in range(2,N):
        tmp = gcd(tmp, A[i])

    if tmp != 1:
        print('not coprime')
        exit()
    max_A = max(A)
    ans = [False] * (max_A + 1)
    flag = True
    def divisor(n):
        ass = []
        for i in range(1,int(n**0.5)+1):
            if n%i == 0:
                ass.append(i)
                if ans[i]:
                    return False
                if i != 1:
                    ans[i] = True
                if i**2 == n:
                    continue
                if ans[n//i]:
                    return False
                ans[n//i] = True
                ass.append(n//i)
        if n != 1:
            ans[n] = True
        return True #sortされていない
    for a in A:
        if not divisor(a):
            flag = False
            break

    if flag:
        print('pairwise coprime')
    else:
        print('setwise coprime')
    






if __name__ == "__main__":
    main()

Measured memory_bytes: 229536
import java.util.*;

class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int X = sc.nextInt();
        int T = sc.nextInt();
        int time = 0;
      	if (N%X == 0) {
        	time =(N/X)*T;
        } else {
        time = (N/X + 1)*T;
        }
        System.out.println(time);
    }
}

Measured memory_bytes: 35668
#include<iostream>
using namespace std;
int main(){
  const int n=100;
  int t[n]={};
  const int m=100;
  int p[m]={};
  int x[m]={};
  int N,M;
  int i=0;
  int sum=0;
  cin>>N;
  while(i<N && cin>>t[i]){ //time
  sum+=t[i];
  i++;
  }
  cin>>M;
  i=0;
  while(i<M && cin>>p[i]>>x[i]) i++;

  for(int j=0;j<M;j++){
    cout<<sum-t[p[j]-1]+x[j]<<endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int a, b, c, d;

bool solve() {
  if (abs(a-c) <= d) return true;
  if (abs(a-b) <= d && abs(b-c) <= d) return true;
  return false;
}
int main() {
  while (cin >> a >> b >> c >> d) {
	if (solve()) cout << "Yes" << endl;
	else cout << "No" << endl;
  }
}


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

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;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a%b);
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}
bool is_prime(ll n) {
  for (ll i = 2; i*i <= n; i++) {
    if (n%i == 0) return false;
  }
  return n != 1;
}
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll res = mod_pow(x*x%mod, n/2, mod);
  if (n & 1) res = res * x % mod;
  return res;
}

int main() {
  ll n, m, l;
  cin >> n >> m >> l;
  ll tmp = 0;
  bool flag = 0;
  rep(i, m) {
    tmp += n%m;
    if (tmp%m == l) flag = 1;
  }
  if (flag) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

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

#define all(x) begin(x), end(x)
#define cina(s, n, a) for(int quer = s; quer < n; quer++) cin >> a[quer]
#define mod(a, b) (abs(b) + (a % abs(b))) % abs(b)
#define setp(x) fixed << setprecision(x)

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll MOD = 1e9 + 7;
const int N = 100500; // are you sure?

void solve(){
    int n, m, x;
    cin >> n >> m >> x;
    vi a(m);
    cina(0, m, a);
    int s1 = 0, s2 = 0;
    for(int i = x; i <= n; i++){
        for(int j = 0; j < m; j++){
            if(i == a[j]) s1++;
        }
    }
    for(int i = x; i >= 1; i--){
        for(int j = 0; j < m; j++){
            if(i == a[j]) s2++;
        }
    }
    cout << min(s1, s2);
}

int main(){
    ios::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int q = 1;
    //cin >> q;
    for (int i = 1; i <= q; i++) {
        solve();
    }
}


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

using namespace std;
long long a, b, x[100001];
int n;
int main(){
    cin >> n >> a >> b;
    for(int i = 1; i <= n; i++){
        cin >> x[i];
    }
    long long ans = 0;
    for(int i = 2; i <= n; i++){
        if((x[i] - x[i - 1]) * a > b){
            ans += b;
        }
        else{
            ans += (x[i] - x[i - 1]) * a;
        }
    }
    cout << ans;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define pf push_front
#define ulli unsigned long long int
#define what_is(x) cerr << #x << " is " << x << endl;
#define pi 2*acos(0.0)
#define F  first
#define S  second
//#define endl '\n'
#define all(x) x.begin(),x.end()
using namespace std;
const lli INF = 1e18L+5;
/*
lli power(lli x,lli y)
{
    lli res=1;
    while(y>0)
    {
        if(y%2==1)
            res=(res*x)%mod;
        x=(x*x)%mod;
        y=y/2;
    }
    return res;
}
*/
int main()
{
	//cout.precision(10);
	//cout.setf(ios::fixed,ios::floatfield);
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	lli T=1;
	//cin>>T;
	while(T--)
	{
		lli i,k,cunt=0,ans=0,n;
		string s,t;
		cin>>s>>k;
		t=s;
		n=s.size();
		bool flag=true;
		for(i=0;i<n-1;i++)
		{
			if(s[i]!=s[i+1])
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<(k*n)/2<<endl;
			return 0;
		}
		for(i=0;i<n-1;i++)
		{
			if(s[i]==s[i+1])
			{
				s[i+1]='1';
				cunt++;
			}
		}
		s=t+t;
		n=s.size();
		ans=0;
		for(i=0;i<n-1;i++)
		{
			if(s[i]==s[i+1])
			{
				s[i+1]='1';
				ans++;
			}
		}
		cout<<cunt+(ans-cunt)*(k-1)<<endl;
	}
}
/*

*/


Measured memory_bytes: 256
# 目標値を全探索
N = int(input())
lst_a = list(map(int, input().split()))


target_values = range(-100, 101)
ans = float("inf")
for target_val in target_values:
    cost = sum([(a - target_val) ** 2 for a in lst_a])
    if cost <= ans:
        ans = cost
print(ans)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
int n,q,x,tot=0;
int a[100001];
void b_search(int l,int r,int x){
	 int mid;
	 mid=(l+r)/2;
	 if (l>=r) return;
	 if (x==a[mid]){
	 	tot++; return;
	 }
	 else{
	 	if (x>a[mid])
	 	   b_search(mid+1,r,x);
	 	if (x<a[mid]){
	 	   b_search(l,mid,x);
		 }
	 }
}
int main(){
    
	cin>>n;
	for (int i=0; i<n; i++)
	    cin>>a[i];
	cin>>q;
	while (q--){
		  cin>>x;
		  b_search(0,n,x);
	}
	cout<<tot<<endl;
	return 0;
}  


Measured memory_bytes: 3468
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Numerics;

namespace AtCoderWorkspace
{
    public class Solver
    {
        public void Solve()
        {
            var cin = new Scanner();
            var n = cin.nextInt();
            var k = cin.nextInt();
            var l = cin.ArrayInt(n);

            Array.Sort(l);
            Array.Reverse(l);
            Console.WriteLine(l.Take(k).Sum());
        }
    }

    class Scanner
    {
        string[] s;
        int i;

        char[] cs = new char[] { ' ' };

        public Scanner()
        {
            s = new string[0];
            i = 0;
        }

        public string next()
        {
            if (i < s.Length) return s[i++];
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length == 0) return next();
            i = 0;
            return s[i++];
        }

        public int nextInt()
        {
            return int.Parse(next());
        }
        public int[] ArrayInt(int N, int add = 0)
        {
            int[] Array = new int[N];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextInt() + add;
            }
            return Array;
        }

        public int[] DPArrayInt(int N, int ex)
        {
            int[] Array = new int[N + ex];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextInt();
            }
            return Array;
        }

        public long nextLong()
        {
            return long.Parse(next());
        }

        public long[] ArrayLong(int N, long add = 0)
        {
            long[] Array = new long[N];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextLong() + add;
            }
            return Array;
        }

        public long[] DPArrayLong(int N, int ex)
        {
            long[] Array = new long[N + ex];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextLong();
            }
            return Array;
        }

        public double nextDouble()
        {
            return double.Parse(next());
        }


        public double[] ArrayDouble(int N, double add = 0)
        {
            double[] Array = new double[N];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextDouble() + add;
            }
            return Array;
        }
    }

    class Program
    {
        static void Main(string[] s) => new Solver().Solve();
    }
}


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

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

  vi x(5);
  int k;
  rep(i, 5)cin >> x[i];
  cin >> k;

  string ans="Yay!";
  rep(i, 4){
    rep(j, 4-i){
      if(x[j+i+1]-x[i]>k){
        ans=":(";
        break;
      }
    }
    if(ans==":(")break;
  }

  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 3652
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(a) int((a).size())

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    string s;
    cin>>s;
    for(ll i =0;i<s.length();i++) cout<<'x';

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 1e9
#define All(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;


int main() {
  int n, m, X;
  cin >> n >> m >> X;
  ll ans = INF;
  vector<vector<int>> a(n, vector<int>(m + 1));
  for (int i = 0; i < n;i++){
    for (int j = 0; j < m + 1;j++){
      cin >> a[i][j];
    }
  }

  for (int bit = 0; bit < (1 << n);bit++){
    ll sum = 0;
    vector<int> x(m);
    for (int i = 0; i < n; i++) {
      if(bit&(1<<i)){
        sum += a[i][0];
        for (int j = 0; j < m;j++){
          x[j] += a[i][j + 1];
        }
      }
    }
    bool f = true;
    for (int j = 0; j < m; j++) {
      if(x[j]<X)
        f = false;
    }
    if(f)
      ans = min(ans, sum);
  }
  if(ans==INF){
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  int C1,C2,C3,k;
  double prob;
  while(cin >> C1 >> C2 >> C3) {
    k=0;
    for(int i=1;i<11;i++) {
      if((C1 + C2 + i <= 20) && (i != C1)&&(i != C2)&&(i != C3)) {
        k++;
      }
    }
    prob = k / 7.0;
    if(prob >= 0.5)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
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; }
using namespace std;
using P = pair<int,int>;
using ll = long long;
const ll INF = 1LL<<60;

//cin.tie(0);ios::sync_with_stdio(false);
int main() {
  int n, y;
  cin >> n >> y;
  bool ok = false;
  ll ans;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j + i <= n; j++) {
      for (int k = 0; k + j + i <= n; k++) {
        ans = 1000 * i + 5000 * j + 10000 * k;
        if (ans == y && n == i + j + k) {
          printf("%d %d %d\n", k, j, i);
          return 0;
        }
      }
    }
  }

  printf("%d %d %d\n", -1, -1, -1);
  return 0;
}


Measured memory_bytes: 3744
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <math.h>
#include <assert.h>
#include <set>
#include <map>
#include <bitset>
#include <ctime>
#include <time.h>
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <stack>
#include <ctype.h>
#include <numeric>
#include <sstream>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <limits>
#include <random>
#include <chrono>
#include <iomanip>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define endl '\n'
#define debug(name) DEBUG(#name, (name))
template<typename T>
void DEBUG(string label, T value) {
    cerr << "[" << label << " = " << value << "]\n";
}

void solve(){
    int n;
    cin >> n;
    cout << (1000 - n % 1000) % 1000 << endl;
}

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

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


Measured memory_bytes: 3632
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

N = I()
A = LI()

def f(num):
    return sum([num%a for a in A])
# 最大値の２倍-1まで順に探索していく
res = []
#  for i in range(max(A)*2+1):
#  for i in range(46*20*11*7):
#  for i in range(994*941*851):
res = 0
#  for i in range(10**7):
#  for i in range(min(A)-1, min(A)):
#  for i in range(max(A)-1, 10**10, max(A)):
#      # 他のすべての余りがa - 1になれば終了
#      for a in A:
#          if i % a != a - 1:
#              break
#      else:
#          print()
#      res = max(res, f(i))
#  print(res)
print(sum([a-1 for a in A]))


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

int main(void) {
  long long N;
  cin >> N;
  set<long long> A;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A.insert(a);
  }
  if (A.size() == N) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

Measured memory_bytes: 9600
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1145141919810893364
//#define INF 810114514
//#define PI 3.141592653589
typedef pair<int,int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define GOODBYE do { cout << "0" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl

signed main(void){
    string S;
    cin >> S;
    if(S[2]==S[3] && S[4]==S[5]){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    
    
    return 0;
}



Measured memory_bytes: 256
def main():
    DW = 'SUN,MON,TUE,WED,THU,FRI,SAT'
    DW = DW.split(',')

    s = input()
    ind_sun = DW.index('SUN')
    ind_s = DW.index(s)
    diff = ind_sun - ind_s
    print(diff if diff > 0 else diff + 7)


if __name__ == '__main__':
    main()


Measured memory_bytes: 8956
f = lambda k:[list(map(int,input().split())) for _ in range(k)]
n,m = map(int,input().split())
lln = f(n)
llm = f(m)

for a,b in lln:
  mi,p = 10**9,m
  for i, [c,d] in enumerate(llm):
    di = abs(c-a)+abs(d-b)
    if di < mi:
      mi,p = di,i+1
  print(p)

Measured memory_bytes: 3064
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<fstream>
using namespace std;
int main()
{
    int i,j,x,y,l;
//    while(777)
//    {
        scanf("%d%d",&x,&y);
    int ss=0;
    if(x<0&&y<0&&x>y)
    {
        x=fabs(x);
        y=fabs(y);
        x=y-x;
        x+=2;
        printf("%d\n",x);
           exit(0);
    }
   else if(x<y)
    {
        if(x>=0||y==0)printf("%d\n",y-x);
        else if(y<0)printf("%d\n",y-x);
        else if(x<0&&-x<y)printf("%d\n",y+x+1);
        else if(x<0&&-x>y)
        {
           printf("%d\n",-x-y+1);
        }
        else if(x<0&&-x==y)printf("%d\n",1);
        else printf("%d\n",y-x);
        exit(0);
    }
    else if(x>y)
    {
        if(y>0)
        {
            ss+=2;
            printf("%d\n",ss+x-y);
        }
        else
        {
            if(-y>x)
            {
                printf("%d\n",-x-y+1);
            }
            else
            {
                printf("%d\n",x+y+1);
            }
        }
         exit(0);
    }
//    }
    return 0;
}


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

typedef long long int ll;
typedef long double ld;

#define Pb push_back
#define Mp make_pair

#define ff first
#define ss second

#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))

#define INF LONG_LONG_MAX
#define MINF LONG_LONG_MIN

#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define db1(x) cout<<#x<<" : "<<x<<endl;
#define db2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;
#define db3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;

const int N = 1e5+5;
ll dp[N][3];
ll n;
ll v[N][3];

ll sol(int d,int a)
{
  if(d == n)
  {
    return 0;
  }
  ll &ans = dp[d][a];
  if(ans != -1)
  {
    return ans;
  }
  if(a == 0)
  {
    ans = v[d][a] + max(sol(d+1,1),sol(d+1,2));
  }
  else if(a == 1)
  {
    ans = v[d][a] + max(sol(d+1,0),sol(d+1,2));
  }
  else
  {
    ans = v[d][a] + max(sol(d+1,0),sol(d+1,1));
  }
  return ans;
}

void solve()
{
  cin >> n;
  for(int i=0;i<n;i++)
  {
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  }
  memset(dp,-1,sizeof(dp));
  ll ans = Max(sol(0,0),sol(0,1),sol(0,2));
  cout << ans;
}

signed main()
{
  IOS;
  //freopen("input.txt", "rt", stdin);
  //freopen("output.txt", "wt", stdout);
  int t=1;
  //cin >> t;
  while(t--)
  {
    solve();
  }
  return 0;
}
/*==============================================================================================================================================================*/


Measured memory_bytes: 12952
#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
#define MOD 1000000007LL
#define endl "\n"
#define fst first
#define snd second
const ll UNDEF = -1;
const ll INF=1e18;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;
typedef vector<ll> vll;

bool go(ll a,ll b,ll c,ll d) {
	if (d<b) return false;
	if (a<b) return false;
	if (!(c+1<=b-1)) return true;
	if (a>c) {
		ll reduce=(a-c+b-1)/b; assert(reduce>0);
		a-=reduce*b; assert(a<=c&&a+b>c);
	}
	else {
		a-=b;
	}
	if (a<0) return false;
	if (b==d) return true;
	assert(0<=a&&a<=c&&a+b>c);
	ll g=__gcd(b,d-b);
	vector<pll> v;
	{
		ll x=a;
		ll l=c+1-x; if (l<0) l+=b;
		ll r=b-1-x; if (r<0) r+=b;
		if (l<=r) v.PB(MP(l,r));
		else {
			v.PB(MP(l,b-1));
			v.PB(MP(0,r));
		}
	}
	for (auto &w:v) {
		ll l=w.fst,r=w.snd;
		ll got=(r/g)*g;
		//printf("x:%lld g:%lld l:%lld r:%lld\n",a,g,l,r);
		if (l<=got&&got<=r) return false;
	}
	return true;
}

void solve(long long T, vector<long long> A, vector<long long> B, vector<long long> C, vector<long long> D){
	for (ll i=0;i<T;i++) {
		bool ans=go(A[i],B[i],C[i],D[i]);
		if(ans)printf("Yes\n");
		else printf("No\n");
	}
}

int main(){	
	ios_base::sync_with_stdio(false); cin.tie(0);
	long long T;
	scanf("%lld",&T);
	vector<long long> D(T-1+1);
	vector<long long> C(T-1+1);
	vector<long long> B(T-1+1);
	vector<long long> A(T-1+1);
	for(int i = 0 ; i <= T-1 ; i++){
		scanf("%lld",&A[i]);
		scanf("%lld",&B[i]);
		scanf("%lld",&C[i]);
		scanf("%lld",&D[i]);
	}
	solve(T, A, B, C, D);
	return 0;
}



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

using namespace std;
#define int1(a) scanf("%d",&a)
#define int2(a,b) scanf("%d %d",&a,&b)
#define int3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define int4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
#define lint1(a) scanf("%lld",&a)
#define lint2(a,b) scanf("%lld %lld",&a,&b)
#define lint3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define lint4(a,b,c,d) scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define lli long long int
#define pint(x) printf("%d",x)
#define ps printf(" "); 
#define plint(x) printf("%lld",x)
//#define sort(a,n) qsort(a,n,sizeof(int),cmpfunc)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define bg begin()
#define ed end()
#define itn int
#define Set(a, s) memset(a, s, sizeof (a))
#define mod 1000000007
#define tmod(k) if ((k) >= mod) k %= mod;
#define endl '\n'
#define nl printf("\n");
#define fors(i,n) for(int i=0; i<n; i++)
#define fore(i,n) for(int i=n-1; i>=0; i--)
#define forv(i,a,n) for(int i=a; i<n; i++)
#define fora(i,ele) for(auto i: ele)
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
typedef string st;
typedef vector<int> vi;
typedef vector<st> vs;
typedef pair<int,int> pii;
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
void ASCII_Chart(){int i,j,k;printf("ASCII Chart:(30-129)\n");forv(i,30,50){fors(j,5){k=i+j*20;printf("%3d---> '%c'   ",k,k);}printf("\n");}}
const int INF = 0x3f3f3f3f;
#define pi 3.14159265
float roundoff(float val, float p){ return floor(val*pow(10.0,p)+0.5)/pow(10.0,p);}

//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};
//int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1};
class node{
public:
	int id;
	int prio;
	int prd;
};

class compare{
public:
	bool operator()(node &n1, node &n2){
		if(n1.prio>n2.prio)//ascending
			return true;
		else if(n1.prio==n2.prio){
			return n1.id>n2.id;
		}
		return false;
	}
};
//priority_queue<node, vector<node>, compare> pq;

int main(void) {
    ios_base::sync_with_stdio(true);
    cin.tie(NULL);
    cout.tie(NULL);
    /*#ifdef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #else
    // online submission
    #endif*/
    
   int n,m,k;
   int3(n,m,k);
   int sum = 0;
   bool check = false;
   fors(i,n+1){
       fors(j,m+1){
           int r = i;
           int c = j;
           int cl = m-j;
           int rl = n - i;
            sum = r*c + rl*cl;
           if(sum==k){
               cout<<"Yes";
               check = true;
               break;
           }
       }
       if(check) break;
   }
   if(!check) cout<<"No";
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define pi 2*asin(1.0)


//template functions
/////////////////////////
long Comb(long N,long M){// nCm
long ans=1;
for(long i=N;i>N-M;i--){
    ans=ans*i;
}
for(long i=M;i>0;i--){
    ans=ans/i;
}
return ans;
}
long Perm(long N,long M){// nPm
long ans=1;
for(long i=N;i>N-M;i--){
    ans=ans*i;
}
return ans;
}
/////////////////////////



//Answer


int main() {

long N;
cin>>N;
string A,B;
cin>>A>>B;

for(long i=0;i<N;i++){

cout<<A.at(i)<<B.at(i);
 
}

cout<<endl;

}


Measured memory_bytes: 3636
import java.util.*;

public class Main {    
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);                
        
        int N = sc.nextInt();                
        
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        
        
        for(int i = 0;i < N;i++){
            int x = sc.nextInt();
            if(x>max){
                max = x;
            }
            if(x < min){
                min = x;
            }            
        }        
        
        System.out.println(max-min);
        
        
    }                                      
}
        
        
        

Measured memory_bytes: 21716
def comb(n,k,p):
  from math import factorial
  if n<0 or k<0 or n<k:return 0
  if n==0 and k==0:return 1
  a=factorial(n)%p
  b=factorial(k)%p
  c=factorial(n-k)%p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
  if b==0:
    return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    return (d*d)%p
  else:
    return (a*power_func(a,b-1,p))%p
n,k=map(int,input().split())
mod=10**9+7
for i in range(1,k+1):
  print((comb(k-1,i-1,mod)*comb(n-k+1,i,mod))%mod)

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

int main()
{
  int n;
  cin>>n;
  string ans;
  vector<int> s(n);
  for(int i=0;i<n;i++)
  {
    cin>>s.at(i);
  }
  for(int i=0;i<n;i++)
  {
    if(s.at(i)%2==0)
    {
      if((s.at(i)%3!=0)&&(s.at(i)%5!=0))
      {
        ans="DENIED";
        break;
      }
    }
  }
  if(ans!="DENIED")
    ans="APPROVED";
  cout<<ans<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n;i>=0;i--)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;
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; }
//const ll INF = 1LL << 60;
//const int INF = 1001001001;

int main(void){
    int n,k;
    cin >> n >> k;
    vector<int> h(n);
    REP(i,n) cin >> h[i];
    sort(ALL(h));
    ll sum=0;
    REP(i,n-k)sum+=h[i];
    cout << sum;

    return 0;
}

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> num(100000);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    num[a[i]-1]++;
    ans += a[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    b--; c--;
    int k = c - b;
    ans += num[b] * k;
    num[c] += num[b];
    num[b] = 0;
    cout << ans << '\n';
  }
  return 0;
}

Measured memory_bytes: 3992
## coding: UTF-8
N = int(input())

alphabet = 'abcdefghijklmnopqrstuvwxyz'

ans = []


#N = 123456789


while N > 0:
    if(N > 26):
        ans.append(N % 26)
        if(N % 26 == 0):
            N -= 26
        else:
            N -= N % 26
        N = N//26
        #print(ans, N)
    elif(N == 26):
        ans.append(0)
        N = 0
    else:
        ans.append(N)
        N -= N

#print(ans, N)
#print(ans[::-1])
ans = ans[::-1]


ret = []
for i in range(len(ans)):
    ret.append(alphabet[ans[i]-1])

print(''.join(ret))


'''
for i in range(12, 0, -1):
    #print(i)
    if(N // 26**i > 0)



if(N // 26**1 > 0):
    ans.append(alphabet[N//26**1 - 1])
    N -= 26**1 * (N//26**1)
ans.append(alphabet[N-1])
'''
#print(''.join(ans))



Measured memory_bytes: 61928
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0

for i in range(K):
  last = "D"
  wl = "l"
  for j in range(N//K+1):
    if  j*K + i >= N:
      break
    else:
      te = T[j*K+i]
      if te != last:
        last = te
        wl = "w"
        if te == "r":
          ans += P
        elif te == "s":
          ans += R
        else:
          ans += S
      else:
        if wl == "l":
          last = te
          wl = "w"
          if te == "r":
            ans += P
          elif te == "s":
            ans += R
          else:
            ans += S
        else:
          last = te
          wl = "l"
print(ans)



Measured memory_bytes: 9400
#include <iostream>

int main(){
   int A[3][3];
   for(int i=0; i<3; ++i){
      for(int j=0; j<3; ++j)
	 std::cin >> A[i][j];
   }
   int N;
   std::cin >> N;
   int b;
   for(int i=0; i<N; ++i){
      std::cin >> b;
      for(int j=0; j<3; ++j){
	 for(int k=0; k<3; ++k){
	    if(b==A[j][k]){
	       A[j][k] = 0;
	    }
	 }
      }
   }
   for(int i=0; i<3; ++i){
      if(A[i][0]==0 && A[i][1]==0 && A[i][2]==0) {
	 std::cout << "Yes" << std::endl;
	 return 0;
      }
      if(A[0][i]==0 && A[1][i]==0 && A[2][i]==0) {
	 std::cout << "Yes" << std::endl;
	 return 0;
      }
   }
   if(A[0][0]==0 && A[1][1]==0 && A[2][2]==0) {
      std::cout << "Yes" << std::endl;
      return 0;
   }
   if(A[0][2]==0 && A[1][1]==0 && A[2][0]==0) {
      std::cout << "Yes" << std::endl;
      return 0;
   }
   std::cout << "No" << std::endl;
   return 0;
}


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

vector<vector<pair<int, long long>>> tree;
vector<long long> dist;

void dfs(int u, long long d) {
  dist[u] = d;
  for (unsigned v = 0; v < tree[u].size(); ++v)
    if (dist[tree[u][v].first] == -1)
      dfs(tree[u][v].first, d + tree[u][v].second);
}

int main() {
  int N, Q, K;
  cin >> N;
  vector<int> a(N - 1), b(N - 1), c(N - 1);
  for (int i = 0; i < N - 1; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    --a[i], --b[i];
  }
  cin >> Q >> K;
  --K;
  vector<int> x(Q), y(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> x[i] >> y[i];
    --x[i], --y[i];
  }

  tree.resize(N), dist.resize(N, -1);
  for (int i = 0; i < N - 1; ++i) {
    tree[a[i]].push_back(make_pair(b[i], c[i]));
    tree[b[i]].push_back(make_pair(a[i], c[i]));
  }
  dfs(K, 0);

  for (int i = 0; i < Q; ++i)
    cout << dist[x[i]] + dist[y[i]] << endl;
}

Measured memory_bytes: 14720
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = 305, mod = (int) 1e9 + 7;
int n, m;

int read() {
  if (scanf("%d%d", &n, &m) < 2) {
    return false;
  }
  return true;
}

int dp[maxn][maxn][maxn];

void add(int &a, int b) {
  a += b;
  if (a >= mod) {
    a -= mod;
  }
}

int mul(int a, int b) {
  return (long long) a * b % mod;
}

void solve() {
  memset(dp, 0, sizeof(dp));
  dp[0][1][1] = 1;
  for (int i = 0; i < m; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = j; k <= n; k++) {
        if (!dp[i][j][k]) {
          continue;
        }
        add(dp[i + 1][j + 1][k + 1], mul(n - k, dp[i][j][k]));
        add(dp[i + 1][j][k + 1], mul(n - k, dp[i][j][k]));
        add(dp[i + 1][j][k], mul(j, dp[i][j][k]));
        add(dp[i + 1][j - 1][k], mul(j, dp[i][j][k]));
      }
    }
  }
  printf("%d\n", dp[m][0][n]);
}

int main() {
  precalc();
#ifdef LOCAL
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (true) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
	ll N, M; cin >> N >> M;
	vector<ll> A(N);
	vector<pair<ll, ll>> CB(M);
	priority_queue<ll, vector<ll>, greater<ll>> pq;
	for(int i = 0; i < N; i++) {
		cin >> A[i];
		pq.push(A[i]);
	}
	ll Bsum = 0;
	for(int i = 0; i < M; i++) {
		cin >> CB[i].second >> CB[i].first;
		Bsum += CB[i].second;
	}
	sort(CB.begin(), CB.end());
	int j = M - 1, cnt = 0;
	for(int i = min(N, Bsum); i >= 0; i--) {
		ll u = pq.top();
		if(CB[j].first > u) {
			pq.pop(); pq.push(CB[j].first);
			cnt++;
			if(cnt == CB[j].second) {
				cnt = 0; j--;
			}
		}
	}
	ll ans = 0;
	while(!pq.empty()) {
		ans += pq.top(); pq.pop();
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 6668