akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

vector<P> ans[4], ban[2];
Int d[2];
Int n ;
bool come[2][648][648];
Int color[648][648];
void dfs(int x, int y, int k, int c = 0){
  if(x < 0 || x >= 2*n || y < 0 || y >= 2*n)return;
  if(come[k][x][y])return;
  come[k][x][y] = true;
  color[x][y] +=  c << k;
  for(auto b:ban[k]){
    int nx = x + b.first, ny = y + b.second;
    dfs(nx, ny, k, !c);
  }
}

int main(){
  cin >> n >> d[0] >> d[1];
  for(int i = 0;i < 2*n;i++){
    for(int j = 0;j < 2*n;j++){
      for(int k = 0;k < 2;k++){
	if(i * i + j * j == d[k]){
	  ban[k].push_back(P(i,j));
	  ban[k].push_back(P(i,-j));
	  ban[k].push_back(P(-i,j));
	  ban[k].push_back(P(-i,-j));
	}
      }
    }
  }
  
  for(int i = 0;i < 2*n;i++){
    for(int j = 0;j < 2*n;j++){
      dfs(i, j, 0);
      dfs(i, j, 1);
    }
  }

  for(int i = 0;i < 2*n;i++){
    for(int j = 0;j < 2*n;j++){
      ans[color[i][j]].push_back(P(i,j));
    }
  }
  for(int i = 0;i < 4;i++){
    if(ans[i].size() < n*n)continue;
    for(int j = 0;j < n*n;j++){
      P point = ans[i][j];
      cout << point.first << " " << point.second << endl;
    }
    return 0;
  }
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define contains(v, t) (find(v.begin(), v.end(), t) != v.end())
#define MOD 1000000007
using ll = long long;
using ull = unsigned long long;

int main() {
  int n, m; cin >> n >> m;
  int c = abs(n - m);
  if (c > 1) {
    cout << 0;
  } else if (c == 1) {
    // A! * B!
    ll ans = 1;
    for(ll i = 1; i <= n; i++) {
      ans *= i;
      ans %= MOD;
    }
    for(ll i = 1; i <= m; i++) {
      ans *= i;
      ans %= MOD;
    }
    cout << ans;
  } else {
    // A! * B! * 2
    ll ans = 1;
    for(ll i = 1; i <= n; i++) {
      ans *= i;
      ans %= MOD;
    }
    for(ll i = 1; i <= m; i++) {
      ans *= i;
      ans %= MOD;
    }
    ans *= 2;
    ans %= MOD;
    cout << ans;
  }
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=(s);i<(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(n);i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define Eunique(v) v.erase(unique(all(v)),v.end())
#define pvec(v) rep(i,v.size()){cout << v[i] << " ";};cout<<endl;
#define pb push_back
#define pf push_front
#define mod 1e9+7
#define m_p make_pair
 
typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<vector<int>> vvi;
const double pi = acos(-1.0);
 
ll gcd(ll x, ll y) { return (y == 0) ? x : gcd(y, x % y); }
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false;

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if  (num % i == 0)
        {
            return false;
        }
    }
    return true;
}

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

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

int main(){

  int n, m , t, a;
  bool used[11000];
  cin >> n >> m >> t;

  for(int i = 0; i < t; i++) used[i] = false;
  for(int i = 0; i < n; i++){
    cin >> a;
    int l = a - m;
    int r = a + m-1;
    r = min(r, t);
    for(int j = l; j <= r; j++){
      used[j] = true;
    }
  }
  int ans = 0;
  for(int i = 0; i < t; i++){
    if(used[i] == false) ans++;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 3100
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

long long MOD = 1000000007;

long long gcd(long long N, long long M){
    while (true){
        if (min(N,M)==0){
            return max(N,M);
        }else if (N>M){
            N%=M;
        }else{
            M%=N;
        }
    }
}

long long bin_search(long long g, long long lower, long long l, long long r){
    long long mid = (l+r)/2+(l+r)%2;

    if(mid==r){
        return mid;
    }

    if(mid*g>=lower){
        return bin_search(g, lower, l, mid);
    }else{
        return bin_search(g, lower, mid, r);
    }
}

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

    long long ansT=0;
    long long ansA=0;
    for(int i=0; i<N; i++){
        long long T, A;
        cin >> T >> A;
        long long G=gcd(T, A);
        long long tmp=bin_search(T/G, ansT, 0, 1000000000000000000/(T/G)+1);
        tmp=max(tmp,bin_search(A/G, ansA, 0, 1000000000000000000/(A/G)+1));
        ansT=T/G*tmp;
        ansA=A/G*tmp;
        //cout << ansT << ' ' << ansA << endl;
    }
    cout << ansT+ansA << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
const int MOD=1e9+7;


int main() {
    ios_base::sync_with_stdio(false);

    while (1) {
        int n,x;
        cin >> n >> x;
        if (n==0 && x==0) break;
        int ans=0;
        repr(i,1,n-1) {
            repr(j,i+1,n) {
                repr(k,j+1,n+1) {
                    if (i+j+k==x) ++ans;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;   
}



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

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required

using namespace __gnu_pbds; 
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long  ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
ll a[55],p[55];
ll find(ll n,ll x)
{
    if(n==0)
    {
        if(x<=0)
            return 0LL;
        else
            return 1LL;
    }
    else if(x<=1+a[n-1])
        return find(n-1,x-1);
    else
        return p[n-1]+1+find(n-1,x-2-a[n-1]);
}
void solve()
{
    a[0]=1,p[0]=1;
    ll n,x;
    cin>>n>>x;
    fr(i,1,n+1)
    {
        a[i]=a[i-1]*2+3;
        p[i]=p[i-1]*2+1;
    }    
    cout<<find(n,x);
    return;
}
int main()
{
    IOS;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        //cout<<"Case #"<<pp-t<<":\n";
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
*/
/*
recursion - matrix exponential

*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm

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

public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int n = stdIn.nextInt();
		int a[] = new int[n];
		for(int i = 0; i < n; i++){
		    a[i] = stdIn.nextInt();
		}
		
		int m = stdIn.nextInt();
		int num[] = new int[n + m];
		for(int i = 0; i < n + m; i++){
		    if(i < n){
		        num[i] = a[i];
		    }else{
		        num[i] = stdIn.nextInt();
		    }
		}
		
		Arrays.sort(num);
        
        for(int i = 1; i < n + m; i++){
            if(num[i - 1] == num[i]){
                System.out.println(num[i]);
            }
        }
        
		stdIn.close();
	}

}




Measured memory_bytes: 132816
s = int(input())

a = s
ar = [a]
for i in range(1, 100000000):
    if(a % 2 == 0):
        a = a/2
    else:
        a = 3*a + 1
    if(ar.count(a) == 1):
        # print("a", a)
        print(i+1)
        break
    # print("a", a)
    ar.append(a)


Measured memory_bytes: 62140
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce
import statistics


# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = getInputInt()

ans = n*n

print(ans)

Measured memory_bytes: 23444
#include<bits/stdc++.h>
using namespace std;
//define
#define int unsigned long long
#define vint vector<int>
#define rep(x,y) for(int i=x;i<y;i++)
#define REP(x,y) for(int j=x;j<y;j++)
#define Rep(x,y) for(int k=x;k<y;k++)
#define all(x) x.begin(),x.end()
#define pb() push_back()
#define mod 100000007
#define elif else if
#define str string

//dp
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 long long INF=1LL<<60;
long long dp[100010];

//main
signed main(){
  int N,M,O,Q,count=0,count2=0,count3=0;
  for(int i=0;i<100010;i++)dp[i]=INF;
  dp[0]=0;
  double D=0;
  int c='A'-'a';
  char C;
  str S,T;
  cin>>N>>M>>O;
  while(1){
    count+=N;
    count2++;
    if(count2%7==0)count+=M;
    if(count>=O)break;
  }
  cout<<count2<<endl;
}



Measured memory_bytes: 3900
# pragma GCC optimize("O3")
# include <bits/stdc++.h>
std::pair<int,int> DR[] = {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define rcg(s) cout << s;exit(0)
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define db(x) cerr << #x << " = " << x << '\n'
# define pb push_back
# define mp make_pair
# define all(s) s.begin(),s.end()
# define sz(x) (int)((x).size())
# define int ll
using namespace std;

int n,a[3005],dp[3005][3005][2];

int32_t main(){_
    //freopen("in","r",stdin);
	cin >> n;
	for(int i = 1;i <= n;i++) cin >> a[i];
	for(int i = n;i >= 1;i--)
	{
		dp[i][i][0] = a[i];
		dp[i][i][1] = -a[i];
		for(int j = i + 1;j <= n;j++)
		{
			dp[i][j][0] = max(a[i] + dp[i + 1][j][1],a[j] + dp[i][j - 1][1]);
			dp[i][j][1] = min(-a[i] + dp[i + 1][j][0],-a[j] + dp[i][j - 1][0]);
		}
	}
	rc(dp[1][n][0]);
}

Measured memory_bytes: 139648
from itertools import accumulate
from collections import Counter

N = int(input())
num_list = list(map(int, input().split()))
cumulative_sum_list = list(accumulate(num_list))
cumulative_sum_list.append(0)

c = Counter(cumulative_sum_list)
s = 0
for i in c.values():
    s += i * (i - 1) // 2
print(s)


Measured memory_bytes: 41728
#include <iostream>

using namespace std;



int main(){

    int a,b,c;
    scanf("%d %d",&a,&b);
    b--;
    a--;
    if (b%a==0)
        b/=a;
    else {
        b/=a;
        b+=1;
    }

       printf("%d",b);


    return 0;

}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10**9)
mod=10**9+7
m,n=map(int,input().split())
grid=[input() for _ in range(m)]
cache={}
def dp(i,j):
  if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j]=='#':
    return 0
  elif (i,j) in cache:
    return cache[(i,j)]
  elif i==m-1 and j==n-1:
    return 1
  cache[(i,j)]=dp(i+1,j)+dp(i,j+1)
  return cache[(i,j)]

print(dp(0,0)%mod) 
  

Measured memory_bytes: 380944
k = int(input())
gx,gy = map(int,input().split())
if k%2 == 0 and (gx+gy)%2:
  print(-1)
  exit()
flgx = 1
flgy = 1
if gx < 0:
  flgx -= 2
  gx *= -1
if gy < 0:
  flgy -= 2
  gy *= -1
px = 0
py = 0
ansls = []
while True:
  if (gx-px+gy-py) <= 2*k:
    break
  if gx-px >= k:
    px += k
  elif gy-py >= k:
    py += k
  ansls.append((px,py))
if gx-px+gy-py <= 2*k:
  if gx-px+gy-py == 2*k:
    if px+k <= gx:
      px += k
    else:
      py += k
    ansls.append((px,py))
  if gx-px+gy-py == k:
    ansls.append((gx,gy))
  else:
    if (gx-px+gy-py)%2:
      if px+k <= gx:
        px += k
      elif py+k <= gy:
        py += k
      elif px+py+k <= gx+gy:
        py += k-(gx-px)
        px = gx
      else:
        py -= gx-px
        px -= k-(gx-px)
      ansls.append((px,py))
    if gx-px <= gy-py:
      ansls.append((px+(gx-px+gy-py)//2-k,py+(gx-px+gy-py)//2))
    else:
      ansls.append((px+(gx-px+gy-py)//2,py+(gx-px+gy-py)//2-k))
    ansls.append((gx,gy))
for i in range(len(ansls)-1):
  if abs(ansls[i+1][0]-ansls[i][0])+abs(ansls[i+1][1]-ansls[i][1]) != k:
    print("error")
print(len(ansls))
for x,y in ansls:
  print(x*flgx,y*flgy)
  
  

Measured memory_bytes: 26092
from collections import Counter


N = int(input())
lst_number = [int(input()) for _ in range(N)]
set_number = set(lst_number)
cnt_number = Counter(lst_number)
ans = 0


for key in set_number:
    if cnt_number[key] % 2 == 1:
        ans += 1


print(ans)

Measured memory_bytes: 23672
#include<stdio.h>
int main(void)
{	
	int a,b,c,d,e,t;
	scanf("%d",&a);
	scanf("%d",&b);
	scanf("%d",&c);
	scanf("%d",&d);
	scanf("%d",&e);
	if(a >0){
		t = e * (b-a);
	}
	else{
		t =(-1 * a * c + d)+e * b;
	}
	printf("%d\n",t);
	return 0;
}
	
	
	 


Measured memory_bytes: 2140
def resolve():
  n = int(input())
  a = []
  ma = 0
  for i in range(n):
    a.append(int(input()))
    ma = max(ma, a[i])
  b = a.copy()
  b.sort()
  for i in range(n):
    if a[i] == ma:
      print(b[n-2])
    else:
      print(ma)
  return

if __name__ == "__main__":
  resolve()


Measured memory_bytes: 14132
#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(),a.end()
#define forn(i,n) for(int i=0;i<int(n);i++)
#define forr(i,n) for(int i=int(n-1);i>=0;i--)
#define fora(i,a,b) for(int i=int(a);i<=int(b);i++)
#define forb(i,a,b) for(int i=int(a);i>=int(b);i--)
#define mod 1000000007
using namespace std;
using ll=long long int;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<ll> vii;
typedef pair<ll ,ll> pii;
template<typename T>
string ts(T v)
{
	string res="{";
	bool r=true;
	for(const auto &x:v)
	{
		if(!r)res+=",";
		r=false;
		res+=to_string(x);
	}
	res+="}";
	return res;
}
void solve()
{
	int a;
	cin>>a;
	cout<<a+a*a+a*a*a<<endl;
	
	
}
int main()
{
    ios::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0);
	solve();
    return 0;
}


Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }

int main() {
  cin.tie(NULL); ios::sync_with_stdio(false);
  lint n, a, b; cin >> n >> a >> b;
  if (n < a + b - 1 or n > a * b) return cout << -1 << '\n', 0;
  V<> res(n); iota(res.begin(), res.end(), 1);
  V<> c(a, 1);
  for (int i = 0; i < n - a; i++) c[i / (b - 1)]++;
  int l = 0, r = 0;
  for (auto&& i : c) {
    l = r, r = l + i;
    reverse(res.begin() + l, res.begin() + r);
  }
  for (int i = 0; i < n; i++) cout << res[i] << (i != n - 1 ? ' ' : '\n');
}

Measured memory_bytes: 4096
#include "bits/stdc++.h"
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); }
const lint MOD = 1e9 + 7;

lint N, A, B;
plint arr[100000];
int main() {
	cin.tie(0); ios_base::sync_with_stdio(false);
	cin >> N;
	REP(i, N) {
		cin >> A >> B;
		arr[i] = plint(A + B, A - B);
	}
	sort(arr, arr + N);
	lint cnt = 1;
	lint right = arr[0].first;
	REP(i, N) {
		if (arr[i + 1].second >= right) {
			cnt++;
			right = arr[i + 1].first;
		}
	}
	cout << cnt;
}


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

#define SZ(a) int((a).size())
#define _REP(_1,_2,_3,_4,name,...) name
#define _REP4(i,b,e,s) for(decltype(e) _b=(b),_e=(e),i=_b+(0<(s)?0:(s));(0<(s)?i<_e:_e<=i);i+=(s))
#define _REP3(i,b,e) for(decltype(e) _b=(b),_e=(e),i=(_b<_e?_b:_b-1);(_b<_e?i<_e:_e<=i);(_b<_e?i++:i--))
#define _REP2(i,n) for(decltype(n) i=0,_n=(n);i<_n;i++)
#define _REP1(n) for(decltype(n) _i=0,_n=(n);_i<_n;_i++)
#define REP(...) _REP(__VA_ARGS__,_REP4,_REP3,_REP2,_REP1)(__VA_ARGS__)
#define FOR(it,c) for(auto&& it=begin(c);it!=end(c);it++)
#define ROF(it,c) for(auto&& it=rbegin(c);it!=rend(c);it++)
#define INT(n) int n;scanf("%d",&n)
#define STR(s) string s;cin>>s

int main(){
  INT(n); STR(s);
  vector<string> init = {"SS", "SW", "WS", "WW"};
  string ans;
  auto check = [&](string s, string ans, int i){
    if ((ans[i] == 'S' && s[i] == 'o') || (ans[i] == 'W' && s[i] == 'x')){
      return (ans[(i + 1) % n] == ans[(i + n - 1) % n]);
    }
    else {
      return (ans[(i + 1) % n] != ans[(i + n - 1) % n]);
    }
  };

  REP(i, 0, 4){
    string tmp = init[i];
    REP(j, 1, n - 1){
      if ((tmp[j] == 'S' && s[j] == 'o') || (tmp[j] == 'W' && s[j] == 'x')){
        tmp += tmp[j - 1];
      }
      else if (tmp[j - 1] == 'S'){
        tmp += 'W';
      }
      else {
        tmp += 'S';
      }
    }
    if (check(s, tmp, 0) && check(s, tmp, n - 1)){
      ans = tmp;
      break;
    }
  }
  if(SZ(ans) == n){
    cout << ans << endl;
  }
  else {
    cout << -1 << endl;
  }
  return 0;
}


Measured memory_bytes: 932
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N, ans;
  string S;
  cin >> N >> S;
  ans=0;
  for (int i=0; i<N-2; i++) {
    if (S.at(i)=='A' && S.at(i+1)=='B' && S.at(i+2)=='C') {
      ans=ans+1;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 3592
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;

using namespace std;
using ll = long long int;
using vv = vector<vector<ll>>;
using edge = struct {
  int to;
  int cost;
};
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define AUTO(i, m) for (auto &i : m)
#define ALL(a) (a).begin(), (a).end()
#define MAX(vec) *std::max_element(vec.begin(), vec.end());
#define MIN(vec) *std::min_element(vec.begin(), vec.end());
#define BIT(n, num) std::bitset<(n)>((num)).to_string()
#define REV(T) greater<T>()

typedef pair<ll, ll> Pair;
bool pairCompare(const Pair &firstElof, const Pair &secondElof) {
  return firstElof.second > secondElof.second;
}

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

map<ll, int> prime_factor(ll n) {
  map<ll, int> ret;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

ll factorial(int n) {
  ll ans = 1;
  for (int i = 2; i <= n; i++) {
    ans *= i;
    ans %= MOD;
  }
  return ans;
}
ll P(int n, int m) {
  ll ans = 1;
  for (int i = m + 1; i <= n; i++) {
    ans *= i;
    ans %= MOD;
  }
  return ans;
}
ll power(int n, int m) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    ll tmp = power(n, m / 2);
    return (tmp * tmp) % MOD;
  } else {
    return (n * power(n, m - 1)) % MOD;
  }
}
ll C(int n, int m) {
  return (P(n, n - m) * power(factorial(m), MOD - 2)) % MOD;
}
ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll B[101][101] = {};

int main() {
  ll H, W, N;
  vector<ll> A;
  cin >> H >> W >> N;
  REP(i, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  int dir = 1;
  int now_w = 0;
  int now_h = 0;
  REP(i, N) {
    REP(j, A[i]) {
      B[now_h][now_w] = i + 1;
      if (dir == 1) {
        if (now_w == W - 1) {
          dir = -1;
          now_h++;
        } else {
          now_w++;
        }
      } else if (dir == -1) {
        if (now_w == 0) {
          dir = 1;
          now_h++;
        } else {
          now_w--;
        }
      }
    }
  }

  REP(i, H) {
    REP(j, W - 1) { cout << B[i][j] << " "; }
    cout << B[i][W - 1] << "\n";
  }

  return 0;
}


Measured memory_bytes: 512
$N=<>;
@A=map[glob],<>;
$_[0][0]=$A[0][0];
$_[0][$_]=$_[0][$_-1]+$A[0][$_]for 1..$N-1;
$_[1][0]=$A[0][0]+$A[1][0];
$_[1][$_]=($_[1][$_-1]>$_[0][$_]?$_[1][$_-1]:$_[0][$_])+$A[1][$_]for 1..$N-1;
print$_[1][$N-1]


Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<string> grid;
int n;
ll f[40][40][40];
ll solve(int l, int r, int p){
	if(l == p && r == p) return 1;
	if(l == p || r == p) return 0;
	if(l > r) return 0;
	if(p < l || r < p) return 0;
	if(~f[l][r][p]) return f[l][r][p];
	ll ans = 0;
	for(int u = l; u < p; u++){
		for(int v = u; v < p; v++){
			for(int rv = p + 1; rv <= r; rv++){
				for(int ru = rv; ru <= r; ru++){
					if(grid[u][ru] == '1'){
						ans += solve(l, v, u) * solve(v + 1, rv - 1, p) * solve(rv, r, ru);
					}
				}
			}
		}
	}
	return f[l][r][p] = ans;
}
int main(){
	memset(f, -1, sizeof(f));
	cin >> n; n *= 2;
	grid.resize(n);
	for(int i = 0; i < n; i++) cin >> grid[i];
	ll ans = 0;
	for(int i = 1; i < n; i++) if(grid[0][i] == '1') ans += solve(1, n - 1, i);
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 768
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
typedef long long ll;
int main(){
    int n,m; cin >> n >> m;
    int lim = m/n;
    int ans = 0;
    for(int i = lim; i>=1; i--){
        if(m % i == 0){
            ans = i;
            break;
        }
    }
    
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;

template <typename T>
struct Edge
{
    int to;
    T cost;
};

template <typename T>
struct WeightedGraph
{
    int n;
    vector<vector<Edge<T>>> g;
    
    WeightedGraph(int n) : n(n){
        g.resize(n);
    }
    
    void init(int n_){
        n = n_;
        g.resize(n_);
    }
    
    void add_edge(int from, int to, T cost){
        g[from].push_back((Edge<T>){to, cost});
    }
};

struct Graph
{
    int n;
    vector<vector<int>> g;
    
    Graph(int n) : n(n){
        g.resize(n);
    }
    
    void init(int n_){
        n = n_;
        g.resize(n_);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
    }
};

int main()
{
    ll n, m;
    cin >> n >> m;
    Graph g(n);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        g.add_edge(a, b);
        g.add_edge(b, a);
    }
    int d[100002] {0};
    queue<int> que;
    d[0] = 1;
    que.push(0);
    while(que.size()){
        int u = que.front();
        que.pop();
        for(int v : g.g[u]){
            if(d[u] != d[v]){
                if(d[v] == 0){
                    d[v] = 3 - d[u];
                    que.push(v);
                }
            }
            else{
                cout << n * (n - 1) / 2 - m << endl;
                return 0;
            }
        }
    }
    ll s[2] {0};
    for(int i = 0; i < n; i++) s[d[i] - 1]++;
    cout << s[0] * s[1] - m << endl;
}

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

int main(){
  
  int N, Sum = 0;
  cin >> N;
  vector<int> d(N);
  
  for (int i = 0; i < N; i++){
    cin >> d.at(i);
  }
  
  for (int i = 0; i < N; i++){
    for (int j = i + 1; j < N; j++){
      
      Sum += d.at(i) * d.at(j);
    }
  }
  cout << Sum << endl;
  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <list>
#include <numeric>
#include <map>	
#include <cmath>
#include <string>
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define Cout(s) cout<<s<<endl
using namespace std;
typedef long long ll;
string Yes(bool b) { if (b) { return "Yes"; } else { return "No"; } }
string YES(bool b) { if (b) { return "YES"; } else { return "NO"; } }
//================================================
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int a = 1, b = 0, c, d, n, k = 0, ans = 0;
string s, t;

int main()
{
	int n, m, c; cin >> n >> m >> c;
	vector<int> B(m); rep(i, m)cin >> B[i];
	int sum = 0, cnt = 0;
	rep(i, n) {
		sum = 0;
		rep(j, m) {
			cin >> a;
			sum += a * B[j];
		}
		if (sum + c > 0)cnt++;
	}
	cout << cnt << endl;

	return 0;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using static Atcoder;
using System.Threading.Tasks;
using System.ComponentModel;

public class Hello
{

    public static void Main()
    {
        int[] box = SpIntRead(' ');
        Console.WriteLine((box[0] % box[1] == 0 ? 0 : 1));
        return;
    }
}


public class Atcoder
{
    static void Swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
    public static long LCM(long a, long b)
    {
        long c = a * b;
        if (a < b) Swap(ref a, ref b);
        long d = a % b;
        while (d != 0)
        {
            a = b;
            b = d;
            d = a % b;
        }
        return c / b;
    }
    public static string strReverce(string s)
    {
        return new string(s.Reverse().ToArray());
    }
    public static int intRead()
    {
        return int.Parse(Console.ReadLine());
    }
    public static bool IsPrime(int n)
    {
        if (n < 2) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;

        //double sqrtNum = Math.Sqrt(n);
        for (int i = 3; i < n; i += 2)
        {
            if (n % i == 0) return false;
        }
        return true;
    }
    public static string[] SpRead(char c)
    {
        return Console.ReadLine().Split(c);
    }
    public static int[] SpIntRead(char c)
    {
        return Console.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
    }

}


Measured memory_bytes: 13268
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring> 
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <memory.h>
#include <iomanip>
#include <bitset>
#include <list>
#include <stack>
 
using namespace std;

#define mod 1000000007

int main()
{
    int n, t;
    cin >> n >> t;
    int a[100001];
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    int mincost = a[0];
    int benefit = 0;
    int ans = 0;
    for(int i = 1; i < n; i++){
        mincost = min(mincost, a[i]);
        int nowbenefit = a[i] - mincost;
        if(benefit == nowbenefit){
            ans++;
        } else if(benefit < nowbenefit){
            ans= 1;
            benefit = nowbenefit;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 640
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1e3 + 5;
int n,m,x_1,y_1,x_2,y_2;
char s[MAXN][MAXN];
int vis[MAXN][MAXN];
int move_1_x[10] = {0,1,0,-1,0};
int move_1_y[10] = {0,0,1,0,-1};
struct node{
	int x,y,time;
	node(){}
	node(int X,int Y,int TIME){x = X,y = Y,time = TIME;}
	friend bool operator < (node X,node Y){return X.time > Y.time;}
};
priority_queue<node> Q;
bool check(int x,int y,int time) {
	if(x < 1 || y < 1 || x > n || y > m || vis[x][y] <= time || s[x][y] == '#')
	return false;
	return true;
}
void bfs() {
	int Time = 0x3f3f3f3f;
	memset(vis,0x3f,sizeof(vis));
	Q.push(node(x_1,y_1,0));
	vis[x_1][y_1] = 0;
	while(!Q.empty()) {
		node f = Q.top();
		Q.pop();
		for(int i = 1;i <= 4;i ++) {
			int nx = f.x + move_1_x[i];
			int ny = f.y + move_1_y[i];
			int time = f.time;
			if(check(nx,ny,time)) {
				vis[nx][ny] = time;
				//printf("%d %d\n",nx,ny);
				if(nx == x_2 && ny == y_2) {
					Time = min(Time,time);
				}
				Q.push(node(nx,ny,time));
			}
		}
		for(int i = -2;i <= 2;i ++) {
			for(int j = -2;j <= 2;j ++) {
				if(i == 0 && j == 0)
				continue;
				int nx = f.x + i;
				int ny = f.y + j;
				int time = f.time + 1;
				if(check(nx,ny,time)) {
					vis[nx][ny] = time;
					if(nx == x_2 && ny == y_2) {
						Time = min(Time,time);
					}
					Q.push(node(nx,ny,time));
				}
			}
		}
	}
	if(Time == 0x3f3f3f3f)
	printf("-1");
	else
	printf("%d",Time);
}
int main() {
	scanf("%d %d %d %d %d %d",&n,&m,&x_1,&y_1,&x_2,&y_2);
	for(int i = 1;i <= n;i ++) {
		scanf("\n");
		for(int j = 1;j <= m;j ++) {
			scanf("%c",&s[i][j]);
		}
	}
	bfs();
	return 0;
}

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

int main() {
  int n, ma, mb;
  cin>>n>>ma>>mb;
  int a[n], b[n], c[n];
  for (int i=0; i<n; i++) cin>>a[i]>>b[i]>>c[i];

  int size = 401;
  int dp[size][size];

  for (int i=0; i<size; i++) {
    for (int j=0; j<size; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  for (int i=0; i<n; i++) {
    int ai = a[i];
    int bi = b[i];
    int ci = c[i];
    for (int j=size-1; j>=ai; j--) {
      for (int k=size-1; k>=bi; k--) {
        int tmp = dp[j-ai][k-bi];
        if (tmp < 0) {
          continue;
        } else if (dp[j][k] == -1) {
          dp[j][k] = tmp + ci;
        } else {
          dp[j][k] = min(dp[j][k], tmp + ci);
        }
      }
    }
  }
  int ans = 5000;
  for (int i=1; i*ma < size && i*mb < size; i++) {
    if (dp[i*ma][i*mb] > 0) {
      ans = min(ans, dp[i*ma][i*mb]);
    }
  }
  if (ans == 5000) {
    cout<<-1<<endl;
  } else {
    cout<<ans<<endl;
  }
}

Measured memory_bytes: 896
import sys
import collections
sys.setrecursionlimit(10 ** 8)

def input(): return sys.stdin.readline().strip()
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

class UnionFind:
    def __init__(self, size):
        self.par = [i for i in range(size)]
        self.rank = [0] * size

    def find(self, x):
        if self.par[x] == x: return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y: return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return

    def same(self, x, y): return self.find(x) == self.find(y)

def main():
    N = Z()
    Cs = [ZZ()+[i] for i in range(N)]
    X = sorted(Cs)
    Y = sorted(Cs, key=lambda x:x[1])
    es = []
    for i in range(N-1):
        c = X[i+1][0] - X[i][0]
        u, v = X[i][2], X[i+1][2]
        es.append([c, u, v])
    for i in range(N-1):
        c = Y[i+1][1] - Y[i][1]
        u, v = Y[i][2], Y[i+1][2]
        es.append([c, u, v])
    es.sort()
    uf = UnionFind(N)
    output = 0
    for i in range(len(es)):
        c, u, v = es[i]
        if not uf.same(u, v):
            uf.unite(u, v)
            output += c
    print(output)

    return

if __name__ == '__main__':
    main()


Measured memory_bytes: 56292
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N;
vector<ll> A,B;

int main(){
  cin>>N;
  A.resize(N);
  B.resize(N);
  rep(i,N) cin>>A[i]>>B[i];

  bool same=true;
  for(int i=0;i<N;i++){
      if(A[i]!=B[i]) same=false;
  }
  if(same){
      cout<<0<<endl;
      return 0;
  }

  ll ans=0;
  ll mi=MOD;
  ll sumb=0;
  for(int i=0;i<N;i++){
      if(A[i]<=B[i]) ans+=A[i];
      else{
          ans+=(A[i]-B[i]);
          sumb+=B[i];
          mi=min(mi,B[i]);
      }
  }

  if(mi<MOD) sumb-=mi;
  ans+=sumb;
  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 512
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char s[101];
int f=0;
int main()
{
    cin>>s;
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]=='C'&&!f)
        f=1;
        if(s[i]=='F'&&f)
        {cout<<"Yes"<<endl;return 0;}
    }
    cout<<"No"<<endl;
    return 0;
}

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

void push(double x);
double pop();

int top;
double stack[100];

int main() {
	int len, i, x, flag;
	double a, b;
	char str[100];
	while (gets(str) != NULL) {
		top = 0;
		len = strlen(str);
		for (i = 0; i < len; i++) {
			flag = 0;
			if ((str[i] == '-' && str[i+1] >= '0' && str[i+1] <= '9') || (str[i] >= '0' && str[i] <= '9')) {
				if (str[i] == '-' && str[i+1] >= '0' && str[i+1] <= '9') {
					flag = 1;
					i++;
				}
				for (x = 0; str[i] >= '0' && str[i] <= '9'; i++) {
					x = x * 10 + (str[i] - '0');
				}
				if (flag)
					x *= -1;
				push(x);
			} else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
				b = pop();
				a = pop();
				switch (str[i]) {
				case '+':
					push(a+b);
					break;
				case '-':
					push(a-b);
					break;
				case '*':
					push(a*b);
					break;
				case '/':
					push(a/b);
					break;
				}
			}
		}
		printf("%lf\n", pop());
	}
	return 0;
}

void push(double x) {
	stack[top++] = x;
}

double pop() {
	return stack[--top];
}

Measured memory_bytes: 988
N=int(input())
a,b,c=list(map(int,input().split())),list(map(int,input().split())),list(map(int,input().split()))
d=set(a[1:]+b[1:]+c[1:])
ans=0
for e in d:
    if(e not in a[1:]and e in c[1:])or( e in b[1:]and e in c[1:]):
        ans+=1
print(ans)

Measured memory_bytes: 7700
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll n,i,mx=0;
    vector<string> vi;
    cin>>n;
    string s;
    map<string ,ll> mp1;
    set<string> st;
    for(i=0;i<n;i++){
        cin>>s;
        mp1[s]++;
        st.insert(s);
    }
    for(auto x:st){
        mx=max(mx,mp1[x]);
    }
    for(auto x:st){
        if(mp1[x]==mx){
            vi.push_back(x);
        }
    }
    sort(vi.begin(),vi.end());
    for(auto x:vi){
        cout<<x<<endl;
    }
}

Measured memory_bytes: 36212
#include<stdio.h>
#include<string.h>

#define M (244140625/8+1)
#define NIL (-1)
#define L 14

unsigned char H[M]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return 0/* your task */; }
int h2(int key){ return 0/* your task */; }

int find(char str[]){
	long long t = getKey(str);
	return H[t/8] & (1<<t%8);
}

int insert(char str[]){
	long long t = getKey(str);
	H[t/8] |= (1<<t%8);
}

int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i] = 0;
    
    scanf("%d", &n);
    
    for ( i = 0; i < n; i++ ){
	scanf("%s %s", com, str);
	
	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}

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

int main() {
    int n, size = 0;
    cin >> n;
    int nn = n;
    while (n != 0) {
        n /= 10;
        size++;
    }
    int sum = 0;
    int n2 =1;
    for (int i = 0; i < size; i++) {
        int count = 0;
        sum += ( nn / n2 % 10 );
        n2 *= 10;
    }
    if ( nn % sum == 0 )
        cout << "Yes" << endl;
    else
        cout << "No" << endl; 
}

Measured memory_bytes: 256
// Problem : D - Replacing
// Contest : AtCoder - AtCoder Beginner Contest 171
// URL : https://atcoder.jp/contests/abc171/tasks/abc171_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;	

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

ll ile[maxN];

// sprawdz MODULO!
int main() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	ll sum = 0;
	REP(i, n) {
		int a;
		cin >> a;
		sum += a;
		ile[a]++;
	}
	// cout << sum << endl;
	int q;
	cin >> q;
	REP(_, q) {
		int a, b;
		cin >> a >> b;
		sum -= ile[a] * a;
		sum += ile[a] * b;
		ile[b] += ile[a];
		ile[a] = 0;
		cout << sum << "\n";
	}
	return 0;
}
 


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

using namespace std;

typedef long long ll;
#define INF 100000000
#define MOD 1000000007

int main()
{
	int a, b, c;
	bool ok = false;
	
	cin >> a >> b >> c;

	// aの倍数の総和は，aのk倍の数 k * a と考えられる
	// (k * a) % b = c (k = 1,2,...)となるかどうかを調べる
	// ここで，((k + b) * a) % b = (ka + ba) % b = (k * a) % bより，kが1～bまでについて調べればよい
	for (int i = 1; i <= b; i++) {
		// aのi倍をbで割った余りがcならYES確定
		if ((a * i) % b == c) {
			ok = true;
			break;
		}
	}
	if (ok) cout << "YES" << endl;
	else cout << "NO" << endl;

	return (0);
}

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

const int INF = 1e9+5;
int main(){
    int n; cin>>n;
    vector<int> h(n);
    for(int &x: h){
        cin>>x;
    }
    vector<int> dp(n, INF);
    dp[0] = 0;
    for(int i=0; i<n; ++i){
        for(int j : {i+1, i+2}){
            if(j<n){
                dp[j] = min(dp[j], dp[i] + abs(h[i]-h[j]));
                // cout<<j;
            }
                
        }
    }
    cout<<dp[n-1];
    return 0;
}

Measured memory_bytes: 1024
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

int n,m,k;
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};

signed main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m>>k;
	vector<vector<int> > d;
	d.resize(n+1,vector<int>(m+1,-1));
	vector<vector<char> > c;
	c.resize(n+1,vector<char>(m+1));
	int xa,ya,xb,yb;
	cin>>xa>>ya>>xb>>yb;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>c[i][j];
		}
	}
	queue<pair<int,int> > q;
	d[xa][ya]=0;
	q.push(make_pair(xa,ya));
	while(!q.empty())
	{
		pair<int,int> t=q.front();
		q.pop();
		for(int dir=0;dir<4;dir++){
			int nx=t.first,ny=t.second;
			for(int i=1;i<=k;i++){
				nx+=dx[dir],ny+=dy[dir];
				if(nx<=0||ny<=0||nx>n||ny>m) break;
				if(d[nx][ny]>=0&&d[nx][ny]<=d[t.first][t.second]) break;
				if(c[nx][ny]=='@') break;
				if(d[nx][ny]==-1){
					d[nx][ny]=d[t.first][t.second]+1;
					q.push(make_pair(nx,ny));
				}
			}
		}
	}
	cout<<d[xb][yb]<<endl;
	return 0;
}

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

int n,a[110],sum=0,dp[110],minn=110;

int bug(int a,int b)
{
    if((a+b)%10==0) return 0;
    else return a+b;
}
int main()
{

	scanf("%d",&n);
	for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    if(sum%10!=0)
    {
        printf("%d",sum);
        return 0;
    }
    else{
        for(int i=0;i<n;i++)
        {
            if(a[i]%10!=0&&minn>a[i]) minn=a[i];
        }
        sum-=minn;
        if(sum%10==0) printf("0");
        else printf("%d",sum);
    }

	return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#pragma GCC optimize("Os")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i,N) for(int i=0;i<(int)N;i++)
const int MOD=1e9+7,MAX=1e6,Primes[168]=
{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,
 73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,
 179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,
 283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,
 419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,
 547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,
 661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,
 811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,
 947,953,967,971,977,983,991,997};
int A[10000];
static inline char CH()
{
  static char buf[100000],*ptr1=buf,*ptr2=buf;
  return ptr1==ptr2&&(ptr2=(ptr1=buf)+fread(buf,1,100000,stdin),ptr1==ptr2)?EOF:*ptr1++;
}
static inline int IN(void)
{
  int x=0,f=0,c=CH();while(c<48||c>57){f^=c==45,c=CH();}
  while(c>47&&c<58){x=x*10+c-48,c=CH();}return f?-x:x;
}
static inline int MPow(int a,int b){return b?1l*MPow(1l*a*a%MOD,b>>1)*(b&1?a:1)%MOD:1;}
static inline int count(const int prime,const int sz)
{
  int max=0;rep(i,sz){int now=0;while(A[i]%prime==0){now++;A[i]/=prime;}if(max<now)max=now;}return max;
}
int main(void)
{
  int N=IN(),x=0;
  rep(i,N){A[i]=IN();x=(x+1l*MPow(A[i],MOD-2))%MOD;}
  rep(i,168)x=1l*x*MPow(Primes[i],count(Primes[i],N))%MOD;
  rep(i,N){if(A[i]>1){rep(j,N-1-i){if(A[i]==A[i+j+1]){A[i+j+1]=1;}}x=1l*x*A[i]%MOD;}}
  return printf("%d\n",x),0;
}

Measured memory_bytes: 1692
#include <bits/stdc++.h>
#define INF 1000000000000000001
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main()
{
  ll K;
  cin >> K;
  vector<ll> A(K);
  for (ll i = 0; i < K; ++i)
  {
    cin >> A.at(i);
  }
  if (A.at(K - 1) != 2)
  {
    cout << -1 << endl;
    return 0;
  }
  ll l = 2, r = 2;
  for (ll i = K - 1; i >= 0; --i)
  {
    l = (l / A.at(i) + (l % A.at(i) == 0 ? 0:1)) * A.at(i);
    r = r / A.at(i) * A.at(i) + A.at(i) - 1;
    if (r < l)
    {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << ' ' << r << endl;
}


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

namespace atcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var acDic = new Dictionary<int, bool>();
            var waDic = new Dictionary<int, int>();
            var nm = Console.ReadLine().Split(' ').Select(num => int.Parse(num)).ToArray();
            for (var i = 0; i < nm[1]; i++)
            {
                var r = Console.ReadLine().Split(' ');
                var num = int.Parse(r[0]);
                var isAc = r[1] == "AC";
                if (acDic.TryGetValue(num, out var pastReslt))
                {
                    if (pastReslt) { continue; }
                    else
                    {
                        if (isAc) acDic[num] = true;
                        else
                        {
                            waDic[num]++;
                        }
                    }
                }
                else
                {

                    acDic.Add(num, isAc);
                    waDic.Add(num, isAc ? 0 : 1);
                }
            }

            var acPros = acDic.Where(kvp => kvp.Value).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            Console.WriteLine($"{acPros.Count()} {waDic.Where(kvp => acPros.ContainsKey(kvp.Key)).Sum(kvp => kvp.Value)}");
        }
    }
}

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

using namespace std;

typedef multiset<int> :: iterator iter;
int n, k, q;
int f[20001];
int ans = 0x3f3f3f3f;
void add(int x) {
	for (; x <= n; x += (x & -x)) f[x] ++;
}

int query(int x) {
	int sum = 0;
	for (; x; x -= (x & -x)) sum += f[x];
	return sum;
}

pair<int, int> b[20001];
int a[20001];
multiset<int> S;

int main( ) {
	scanf("%d %d %d", &n, &k, &q);
	for (int i = 1; i <= n; i ++) {
		scanf("%d", &a[i]);
		b[i] = make_pair(a[i], i);
	}
	sort(b + 1, b + 1 + n);
	for (int i = 1; i <= n; i ++) {
		int cnt = 0;
		for (int j = i; j <= n; j ++) {
			int x = b[j].second, ta, tb;
			iter xx = S.upper_bound(x);
			if (xx == S.begin()) ta = 0;
			else ta = *(--xx);
			xx = S.upper_bound(x);
			if (xx == S.end()) tb = n + 1;
			else tb = (*xx);
			int gap = tb - ta - 1 - (query(tb - 1) - query(ta));
			if (gap >= k) {
				++ cnt;
				if (cnt == q) {
					ans = min(ans, b[j].first - b[i].first);
					break;
				}
				add(b[j].second);
			}
		}
		for (int j = 1; j <= n; j ++) f[j] = 0;
		S.insert(b[i].second);
	}
	printf("%d\n", ans);
	return 0;
}


Measured memory_bytes: 384