akhauriyash/Code-Regression huggingface dataset:
n,m,l = gets.split(" ").map{|x| x.to_i}
l_mat =Array.new()
r_mat=Array.new()
n.times do |i|
    l_mat[i]=gets.split(" ").map{|x| x.to_i}
end
m.times do |i|
    r_mat[i]=gets.split(" ").map{|x| x.to_i}
end
ans_mat=Array.new()
n.times do |i|
    ans_mat[i]=Array.new(l)
    l.times do |j|
        ans_mat[i][j]=0
        m.times do |k|
            ans_mat[i][j]+=l_mat[i][k]*r_mat[k][j]
        end
    end
end
n.times do|i|
    l.times do |j|
        print(ans_mat[i][j])
        if(j!=l-1)
            print(" ")
        end
    end
    puts()
end

Measured memory_bytes: 9140
#include <stdio.h> 
int main(void){
	int x,i,n;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		x=i;
		if(x%3==0){
			printf(" %d",i);
		}else if(x%10==3){
			printf(" %d",i);
		}else{
			while(x!=0){
				x /=10;
				if(x % 10 == 3) {
					printf(" %d",i);
					break;
				}
			}
		}
	}
	printf("\n");
	return 0;
}


Measured memory_bytes: 2052
class Dice:
    def __init__(self, default = [1, 2, 3, 4, 5, 6]):
        self.d = default

    def e(self):
        self.d[0], self.d[2], self.d[3], self.d[5] = self.d[3], self.d[0], self.d[5], self.d[2]

    def w(self):
        self.d[0], self.d[2], self.d[3], self.d[5] = self.d[2], self.d[5], self.d[0], self.d[3]

    def n(self):
        self.d[0], self.d[1], self.d[4], self.d[5] = self.d[1], self.d[5], self.d[0], self.d[4]

    def s(self):
        self.d[0], self.d[1], self.d[4], self.d[5] = self.d[4], self.d[0], self.d[5], self.d[1]

    def r(self):
        self.d[1], self.d[2], self.d[3], self.d[4] = self.d[3], self.d[1], self.d[4], self.d[2]

    def top(self):
        return self.d[0]

    def forward(self):
        return self.d[1]

    def right(self):
        return self.d[2]

    def bottom(self):
        return self.d[5]

    def forTop(self, num):  #ある目がトップd[0]になるように回転させる。トップ以外の状態は知らん。
        for i in range(4):
            if self.top() == num:
                break
            self.e()
        else:
            for j in range(3):
                if self.top() == num:
                    break
                self.s()
    def same(self, dice):
        if self.d == dice.d:
            return True


def main():
    a_ = [int(i) for i in input().split()]
    b_ = [int(i) for i in input().split()]
    if not sorted(a_) == sorted(b_):
        print('No')
        return
    a = Dice(a_)
    b = Dice(b_)
    for i in range(4):
        for j in range(4):
            if b.same(a):
                print('Yes')
                return
            else:
                b.e()
        b.s()
    for i in range(4):
        for j in range(4):
            if b.same(a):
                print('Yes')
                return
            else:
                b.e()
        b.r()
    for i in range(4):
        for j in range(4):
            if b.same(a):
                print('Yes')
                return
            else:
                b.s()
        b.r()


    print('No')
    return




if __name__ == '__main__':
    main()



Measured memory_bytes: 5648
def main():
	X = int(input())
	low = 0
	high = 6000
	ans = 0
	while low <= high:
		mid = low + (high - low) // 2
		money = 100
		for i in range(1, mid + 1):
			money = int(money + (money * 1) / 100)
		if money >= X:
			ans = mid
			high = mid - 1
		else:
			low = mid + 1;
	return ans

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

Measured memory_bytes: 9128
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

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

    int n;
    cin>>n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    sort(all(a));
    cout << a[n - 1] - a[0] << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
// #define int long long
#define debug(x) cerr << #x << " = " << x << endl
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const ll LINF = 1e18;
const double EPS = 1e-9;

void Main(int argc, char** argv) {
    ll n;
    cin>>n;
    ll a[n];
    rep(i,n) {
        cin>>a[i];
    }
    ll sum = 0;
    rep(i,n) sum += a[i];
    ll mi = LINF;
    ll left = 0;
    ll right = sum;
    rep(i,n) {
        left += a[i];
        right -= a[i];
        ll diff = abs(left - right);
        if (diff <= mi) {
            mi = diff;
        }
    }
    cout << mi << endl;

}

signed main(int argc, char** argv) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main(argc, argv);
    return 0;
}


Measured memory_bytes: 1792
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<limits>
#include<iomanip>
#include<cmath>
#include<climits>
using namespace std;

#define rp(i,n) for(int i=0;i<n;i++)
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define llrp(i,n) for(long long i=0;i<n;i++)
#define llrep(i,m,n) for(long long i=m;i<=n;i++)
#define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(),a.end())
#define pb push_back
#define elif else if
#define vcin(a); rp(i,a.size()){cin>>a[i];}
#define vcout(a); rp(i,a.size()){cout<<a[i]<<endl;}
#define allchar(c) for(char c='a';c<='z';c++)

typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef long long ll;
typedef string S;
typedef pair<int,int> P;
typedef queue<int> qi;

const int mod=1e9+7;

ll modpow(ll a,ll b,ll p){
    if(b==0){
        return 1;
    }elif(b%2==0){
        return modpow((a*a)%p,b/2,p);
    }else{
        return (a*modpow(a,b-1,p))%p;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vll a(n);
    vcin(a);
    sort(a);
    ll ans=0;
    if(a.size()%2==0){
        rp(i,n){
            if(i<n/2){
                ans-=a[i]*2;
            }else{
                ans+=a[i]*2;
            }
        }
        ans-=a[n/2];
        ans+=a[n/2-1];
    }else{
        rp(i,n){
            if(i<n/2){
                ans-=a[i]*2;
            }elif(i>n/2){
                ans+=a[i]*2;
            }
        }
        ll c=ans,d=ans;
        c-=a[n/2];
        c+=a[n/2-1];
        d+=a[n/2];
        d-=a[n/2+1];
        ans=max(c,d);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1024
import java.util.*;

class Main{
    public static void main(String[] args){
    	Scanner scn = new Scanner(System.in);
    	int[] list = new int[10];
    	
    	for(int i=0; i<10; i++)
    		list[i] = scn.nextInt();
    	
    	Arrays.sort(list);
    	
    	for(int i=0; i<3; i++)
    		System.out.println(list[9-i]);
    }
}

Measured memory_bytes: 12000
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
 
#define ll 				long long
#define int             long long int
#define setbits(x)      __builtin_popcountll(x)
#define mod             1000000007
#define modp            998244353
#define inf             (ll)1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define w(x)            int x; cin>>x; while(x--)
#define FIO           	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl 			'\n'
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
unsigned long long power(unsigned long long x, 
                         int y, int p) 
{ 
    unsigned long long res = 1; // Initialize result 
  
    x = x % p; // Update x if it is more than or 
    // equal to p 
  
    while (y > 0) { 
        // If y is odd, multiply x with result 
        if (y & 1) 
            res = (res * x) % p; 
  
        // y must be even now 
        y = y >> 1; // y = y/2 
        x = (x * x) % p; 
    } 
    return res; 
} 
  
// Returns n^(-1) mod p 
unsigned long long modInverse(unsigned long long n, int p) 
{ 
    return power(n, p - 2, p); 
} 
  
// Returns nCr % p using Fermat's little 
// theorem. 
unsigned long long nCrModPFermat(unsigned long long n, 
                                 int r, int p) 
{ 
    // Base case 
    if (r == 0) 
        return 1; 
  
    // Fill factorial array so that we 
    // can find all factorial of r, n 
    // and n-r 
    unsigned long long fac[n + 1]; 
    fac[0] = 1; 
    for (int i = 1; i <= n; i++) 
        fac[i] = (fac[i - 1] * i) % p; 
  
    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; 
} 

void solve()
{
	int s; cin >> s;
	if(s <= 2)
		cout << 0;
	else
	{
		if(s < 6)
			cout << 1;
		else
		{
			int ans = 0;
			for(int i = 1; i <= s/3+5; i++)
			{
				int rem = s - i*3;
				if(rem < 0) break;
				ans += nCrModPFermat(rem+i-1, i-1, mod);
				ans = (ans+mod)%mod;
			}
			cout << ans << endl;
		}
	}
}
 
int32_t main()
{
	FIO
    // w(t)
    {
    	solve();
    }
    return 0;
}
	

	

Measured memory_bytes: 3636
N,M,X,Y=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A.append(X)
B.append(Y)
a=sorted(A)
b=sorted(B)

if a[-1]<b[0]:
  print('No War')
else:
  print('War')

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;++i)
typedef long long int ll;
ll INF = 1LL<<60;

ll N;
int White = -1, Gray = 0, Black = 1;

vector<vector<ll> > M(110,vector<ll>(110));
vector<ll> color(110,White);
ll d[110],f[110];
vector<ll> nt(110,0);

ll tt = 0;

int next(int u){
    for(int v = nt[u]; v < N; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void dfs(ll u){
    
    stack<int> S;
    S.push(u);
    color[u] = Gray;
    d[u] = ++tt;

    while(!S.empty()){
        u = S.top();
        ll v = next(u);
        if(v != -1){
            if(color[v] == White){
                color[v] = Gray;
                d[v] = ++tt;
                S.push(v);
            }
        }else {
            S.pop();
            color[u] = Black;
            f[u] = ++tt;        }
    }
}

int main(){
    cin >> N;
    rep(i,N){
        ll u,k; cin >> u >> k;
        u--;
        rep(j,k){
            ll v; cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    rep(i,N){
        if(color[i] == White) dfs(i);
    }

    rep(i,N){
        cout << i+1 << " " << d[i] << " " << f[i] << endl;
    }
}


Measured memory_bytes: 3272
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <numeric>
#include <queue>
//#include <bits/stdc++.h>

typedef long long ll;
const ll INF = (ll)1e18 + 1;

// print(x, y, z);
void print(){ std::cout << std::endl; }
template <typename Head> void print(Head&& head) { std::cout << head << std::endl; }
template <typename Head, typename... Tail> void print(Head&& head, Tail&&... tail)
{
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...); // or print(tail...);
}

template<typename T> T lminimum(const std::vector< T >& v) {return *std::min_element(v.begin(), v.end());}
template<typename T> T lmaximum(const std::vector< T >& v) {return *std::max_element(v.begin(), v.end());}


std::pair<ll, ll> divide(std::vector< ll >& vsum, ll s, ll e)
{
  ll ok = s, ng = e;
  ll section_sum = vsum[e] - vsum[s-1];


  while (ng - ok > 1) {
    ll mid = (ok + ng) / 2;
    if ((vsum[mid] - vsum[s-1]) * 2 < section_sum) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  std::pair<ll, ll> former = std::make_pair(vsum[ok+0] - vsum[s-1], vsum[e] - vsum[ok+0]);
  std::pair<ll, ll> later  = std::make_pair(vsum[ok+1] - vsum[s-1], vsum[e] - vsum[ok+1]);

  if (std::fabs(former.first - former.second) < std::fabs(later.first - later.second)) {
    return former;
  } else {
    return later;
  }
}

int main(int argc, char* argv[])
{
  ll N;
  std::cin >> N;
  std::vector< ll > va(N, 0LL);
  std::vector< ll > vsum(N+1, 0LL);
  vsum[0] = 0;
  for (ll i = 0; i < N; i++) {
    std::cin >> va[i];
    vsum[i+1] = va[i] + vsum[i];
  }

  ll ans = INF;

  // vsum は [0, N] の配列
  // 最も左端の例だと, 区間の和は vsum[1] - vsum[0], vsum[2] - vsum[1]
  // なので, 2 が最小
  // 最も右端の例だと, 区間の和は vsum[N] - vsum[N-1], vsum[N-1] - vsum[N-2]
  // の分を考慮する必要があるので, N-2 が最大
  for (ll i = 2; i <= N-2; i++) {
    std::vector< ll > v4(4);
    std::pair<ll, ll> p1 = divide(vsum, 1, i);
    std::pair<ll, ll> p2 = divide(vsum, i+1, N);
    v4[0] = p1.first;
    v4[1] = p1.second;
    v4[2] = p2.first;
    v4[3] = p2.second;
    ans = std::min(ans, lmaximum(v4) - lminimum(v4));
  }

  print(ans);
  return 0;
}


Measured memory_bytes: 3328
#include<stdio.h>
int main( )
{
   int a,b,i,j,temp;
   scanf ("%d %d",&a,&b);

   int arr[a], brr[b];

   for (i=0; i<a; i++)
   {
       scanf ("%d",&arr[i]);
   }
     for (i=0; i<a; i++)
   {
       for (j=i+1; j<a; j++)

       if (arr[i] > arr[j])
       {
           temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;

       }
   }
   int x=0;
   for (i=0; i<b; i++)
   {

      x = x+arr[i];
   }
   printf ("%d\n",x);

   return 0;
}


Measured memory_bytes: 1736
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 500+7;

int x[M*M],y[M*M];
int dx[10]={-1,1,0,0},dy[10]={0,0,-1,1};
int vs[M][M];//i,j位置当前时刻 是否还有人 
int d[M][M];//i,j位置的人，最快离开剧场所需要的时间 
struct node{
	int x,y;
};
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int n;
  	cin>>n;
  	for(int i=1;i<=n*n;i++){
  		int z;
  		cin>>z;
  		x[i]=(z-1)/n+1;
  		y[i]=(z-1)%n+1;
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			d[i][j]=min(min(i-1,n-i),min(j-1,n-j)),vs[i][j]=1;
	int ans=0;
	for(int i=1;i<=n*n;i++)
	{
		ans+=d[x[i]][y[i]];
		vs[x[i]][y[i]]=0;
		queue<node>q;
		q.push(node{x[i],y[i]});
		while(q.size())
		{
			node tp=q.front();q.pop();
			int tx=tp.x,ty=tp.y;
			for(int j=0;j<4;j++)
			{
				int px=tx+dx[j],py=ty+dy[j];
				if(px<1||px>n||py<1||py>n||d[tx][ty]+vs[tx][ty]>=d[px][py])	continue;//px,py 先到 tx,ty 是否更优，如果不优没必要拓展下去
			 	d[px][py]=d[tx][ty]+vs[tx][ty]; 
				q.push(node{px,py});
			}
		}
//		cout<<i<<"   "<<x[i]<<" "<<y[i]<<"-----"<<endl;
//		for(int j=1;j<=n;j++)
//		for(int k=1;k<=n;k++)
//		{
//			cout<<d[j][k]<<" ";
//			if(k==n)cout<<endl;
//		}
	}
	cout<<ans<<endl;
	return 0;
}

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

int main(){
  int N,M,B=0;
  cin >> N >> M;
  vector<int> A(M);
  for(int i=0;i<M;i++){
    cin >> A.at(i);
    B+=A.at(i);
  }
  if(B>N){
    cout << -1 << endl;
  }
  else{
    cout << N-B << endl;
  }
}

Measured memory_bytes: 3624
#include<bits/stdc++.h>
#define ll long long
#define all(v) v.begin(),v.end()
#define rall(v) v.begin(),v.end()
#define sz(x) (int)sz.size()
#define PB push_back
#define PI 3.1415926535897932384626433832795
#define what(x) cout<<#x<<" is "<<x<<endl;
using namespace std;

#ifdef LOCAL//ONLINE_JUDGE
#include "D:\c_c++\template.h"
#else
#define debug(...) 42
#endif

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

void solve(){
	ll n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		if(((n-1)/i)==0)break;
		ans+=(n-1)/i;
	}
	cout<<ans<<'\n';
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
//    #ifdef LOCAL
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
//    #endif
    int tc=1;
    // cin>>tc;
    while(tc--)solve();
}
			

Measured memory_bytes: 3648
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll x,y,z,n,m,i,j,sum=0,c=0;
    vector<ll>v;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
        sum+=x;
    }
    double k=sum*1.0/(4*m*1.0);
    double f;
    //cout<<k<<endl;
    for(i=0;i<n;i++)
    {
        f=v[i]*1.0;
        if(f>=k)
        {
            c++;
        }
    }
//    cout<<k<<endl;
//    cout<<fixed<<setprecision(2)<<f<<endl;
    if(c>=m)
    {
        cout<<"Yes"<<endl;
          }

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


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>

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

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

using namespace std;

#ifdef INPUT
#include <sstream>
stringstream input(R"(
9
0
1
1
0
1
2
2
1
2
)");
#define cin input
#endif

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

	ll c = 0;
	int l = -1;
	REP (i, n)
	{
		int a;
		cin >> a;

		if (a > l + 1)
		{
			cout << -1 << endl;
			return 0;
		}
		if (a && a == l + 1)
			++c;
		else
			c += a;
		l = a;
	}

	cout << c << endl;

	return 0;
}


Measured memory_bytes: 256
import itertools

N,D=map(int, input().split())
X=[list(map(int, input().split())) for _ in range(N)]

combi=list(itertools.combinations([i for i in range(N)],2 ))

ans=0

for c in combi:
	temp=0
	for d in range(D):
		temp+=(X[c[0]][d]-X[c[1]][d])**2
	temp=temp**.5
	if float.is_integer(temp):ans+=1

print(ans)

Measured memory_bytes: 9364
from sys import stdin

A, B, M = (int(x) for x in stdin.readline().rstrip().split())
ai = [int(x) for x in stdin.readline().rstrip().split()]
bj = [int(x) for x in stdin.readline().rstrip().split()]

minimum = min(ai) + min(bj)
for i in range(M):
    x, y, c = (int(x) for x in stdin.readline().rstrip().split())
    price = ai[x-1] + bj[y-1] - c
    if price < minimum:
        minimum = price

print(minimum)

Measured memory_bytes: 18148
diagram = gets.chomp.chars

upper_skip = ->{
  loop do
    a = diagram[0]
    return false if a == "\\"
    return true unless a
    diagram.shift
  end
}

downer_skip = ->(target_height) {
  height = index = 0
  until height == target_height
    height -= 1 if diagram.shift == "\\"
    index += 1
  end
  index
}

highest = ->{
  max_index = height = 0
  max_height = -Float::INFINITY 
  0.upto(diagram.size - 1) do |j|
    case diagram[j]
    when "\\"
      height -= 1
    when "/"
      height += 1
      max_height, max_index = height, j if max_height < height
      return [0, j] if height.zero?
    end
  end
  [max_height, max_index]
}

calc = ->(from, to) {
  depth = area = 0
  from.upto(to) do
    case diagram.shift
    when "\\"
      area += depth + 1/2r
      depth += 1
    when "_"
      area += depth
    when "/"
      depth -= 1
      area += depth + 1/2r
    end
  end
  area.to_i
}

pools = []
loop do
  break if upper_skip.()
  height, index = highest.()
  if height.zero?
    pools << calc.(0, index)
  elsif height == -Float::INFINITY
    break
  else
    first_index = downer_skip.(height)
    pools << calc.(first_index, index)
  end
  break if diagram.size.zero?
end

if pools.empty?
  puts 0
  puts 0
else
  puts pools.inject(&:+)
  puts pools.size.to_s + " " + pools.join(" ")
end


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

int partition(int A[],int p,int r){
    int x = A[r];
    int i = p-1;
    int ex, j;
    for(j=p; j<r; j++){
        if(A[j]<=x){
      i = i+1;
      ex = A[j];
      A[j] = A[i];
      A[i] = ex;
        }
    }
    ex = A[r];
    A[r] = A[i+1];
    A[i+1] = ex;
    return i+1;
}

int main(int argc, char** argv){
    int n;
    scanf("%d",&n);
    int A[n];

    for(int i=0; i<n; i++){
        scanf("%d",&A[i]);
    }

    int tar = partition(A, 0, n-1);

    for(int i=0; i<tar; i++){
        printf("%d ", A[i]);
    }
        printf("[%d] ", A[tar]);

    for(int i=tar+1; i<n; i++){
        if(i==n-1){
            printf("%d\n", A[i]);
        }
        else printf("%d ",A[i]);
    }

    return 0;
}



Measured memory_bytes: 2108
#include <stdio.h>

int main(void){
   int H, W, iH, iW;
   while(scanf("%d %d",&H,&W)==2){
      if(H==0&&W==0) break; 
      for(iH=0;iH<H;iH++){
         for(iW=0;iW<W;iW++){
            printf("#");
         }
         printf("\n");
      }
      printf("\n");
   } 
   return 0;
}

Measured memory_bytes: 572
#include "bits/stdc++.h"
using namespace std;
#define int long long
int mod=1e9+7;

signed main(){
  int n;
  cin>>n;
  vector<int> a(n);
  int sum=0;
  for(int i=0;i<n;i++){
    cin>>a[i];
    sum+=a[i];
  }
  int ka=n*(n+1)/2;
  if(sum%ka!=0){
    cout<<"NO"<<endl;
    return 0;
  }
  int x=sum/ka;
  vector<int> b(n);
  for(int i=0;i<n-1;i++){
    b[i]=a[i]-a[i+1]+x;
  }
  b[n-1]=a[n-1]-a[0]+x;
  bool f=true;
  for(int i=0;i<n;i++){
    //cout<<b[i]<<endl;
    if(b[i]<0||b[i]%n!=0)f=false;
  }
  if(f)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;

}


Measured memory_bytes: 1792
#include<stdio.h>

int main(){
	while(1){
		int n,s;
		scanf("%d %d",&n,&s);
		if(n==0)return 0;
		int ans=0;
		int i,j;
		int sum;
		int time;
		for(i=0;i<1<<10;i++){
			sum=0;
			time=0;
			for(j=0;j<10;j++)if(i>>j&1){sum+=j;time++;}
			if(sum==s&&time==n)ans++;
		}
		printf("%d\n",ans);
	}
}

Measured memory_bytes: 1028
#include <stdio.h>

int main(void)
{
  int a[3], b, i;
  scanf("%d %d %d", &a[0], &a[1], &a[2]);

  for (i = 0; i < 2; i++) {
    if (a[0] > a[1]) {
      b = a[0];
      a[0] = a[1];
      a[1] = b;
    }
    if (a[1] > a[2]) {
      b = a[1];
      a[1] = a[2];
      a[2] = b;
    }
  }

  printf("%d %d %d\n", a[0], a[1], a[2]);

  return 0;
}

Measured memory_bytes: 596
#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
constexpr ll mod = 1e9+7;
constexpr ll inf = INT32_MAX/2;
constexpr ll INF = LLONG_MAX/2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------テンプレート------------------ */
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;
}
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b); 
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
    int n,m;cin >> n >> m;
    int x;cin >> x;
    vvi a(n, vi(m+1));
    rep(i, n) {
        int c;cin >> c;
        a[i][0] = c;
        rep(j, m) {
            cin >> a[i][j+1];
        }
    }
    /*rep(i, n) {
        rep(j, m+1) {
            cout << a[i][j];
            if(j==m)cout << endl;
            else cout << ' ';
        }
    }*/
    int mi = inf;
    for(int bit = 0; bit < (1<<n);bit++) {
        vi ans(m,0);
        int money=0;
        for(int i = 0;i<n;i++) {
            if(1&(bit>>i)) {
                money+=a[i][0];
                rep(j, m) ans[j]+=a[i][j+1];
            }
        }
        /*cout << bitset<3>(bit) << endl;
        for(auto p:ans) cout << p << ' ';
        cout << "\n";
        cout << "money:" << money << endl;*/
        bool ok = true;
        rep(i, m) if(ans[i]<x) ok = false;
        if(ok) chmin(mi,money);
    }
    if(mi == inf) cout  << -1 << endl;
    else cout << mi << endl;
    return 0;
}

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

void main_()
{
    int N;
    vector<pair<int, int>> xys;
    cin >> N;
    for (int i = 0; i < N; ++i) {
        int x, y;
        cin >> x >> y;
        xys.emplace_back(x, y);
    }

    sort(begin(xys), end(xys));

    vector<double> dists_all;
    do {
        vector<double> dists;
        transform(begin(xys), end(xys) - 1, begin(xys) + 1,
            back_inserter(dists),
            [](auto&& a, auto&& b) {
            auto x = a.first - b.first;
            auto y = a.second - b.second;
            return sqrt(x * x + y * y); });

        dists_all.push_back(
            accumulate(begin(dists), end(dists), 0.0));
    } while (next_permutation(begin(xys), end(xys)));

    auto ans = accumulate(begin(dists_all), end(dists_all), 0.0) / dists_all.size();

    cout << ans << endl;
}
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    main_();
    return 0;
}


Measured memory_bytes: 892
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <numeric>
#include <deque>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <limits>
#include <cassert>
#include <bitset>
#include <list>
#include <iomanip>
#include <cctype>
#include<chrono>
#include<random>

#define for0(i, N)    for (long long i = 0  ; i <  (N); ++i)
#define for1n(i, N)   for (long long i = 1  ; i <= (N); ++i)
#define for1(i, N)    for (long long i = 1  ; i <  (N); ++i)
#define forn(i, N)    for (long long i = 0  ; i <= (N); ++i)
#define forx(i, N, x) for (long long i = (x); i <  (N); ++i)
#define forl(i, N, x, a) for (long long i = (x), a = 0; a < (N);    \
                              i = (i + 1) % (N), ++a)
#define int long long

long double constexpr pi = 3.141592653589793;

template<class T>
constexpr T sigma1(T i, T n){
    return (n - i + 1) * (i + n) / 2;
}

template<class T>
constexpr T sigma2(T i, T n){
    return (i == 1 ? n * (n + 1) * (2 * n + 1) / 6 :
            sigma2(1, n) - sigma2(1, i - 1));
}

template<class T>
constexpr T sigma3(T i, T n){
    if (i == 1) {
        auto temp = sigma1(i, n);
        return temp * temp;
    }
    return sigma2(1, n) - sigma2(1, i - 1);
}

template<size_t i>
std::bitset<i> operator++(std::bitset<i> b, signed){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    return b = (unsigned long long)b + 1;
}

template<size_t i>
std::bitset<i> operator++(std::bitset<i> b){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    auto temp = b;
    b++;
    return temp;
}

template<size_t i>
std::bitset<i> operator--(std::bitset<i> b, signed){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    return b = (unsigned long long)b - 1;
}

template<size_t i>
std::bitset<i> operator--(std::bitset<i> b){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    auto temp = b;
    b--;
    return temp;
}

long long constexpr inf = std::numeric_limits<long long>::max();

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef priority_queue<ll> pq;
typedef priority_queue<ll,vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef set<ll> sll;
typedef unordered_set<ll> usll;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<vector<ll>> vvll;


signed main(void){
    cin.tie(0); ios::sync_with_stdio(false);cout<<fixed<<setprecision(10);
    char a;
    cin >> a;

    cout << (0 <= a - 'A' && a - 'A' <= 'Z' - 'A' ? 'A' : 'a') << "\n";


    return 0;
}


Measured memory_bytes: 3596
x,y = map(int, input().split())

if x <= y:
    if abs(x) <= abs(y):
        ans = min(y-x, y-(-x)+1)
    else:
        ans = min(y-x, 1 + abs(-y-x))
else:
    ans = abs(abs(x) - abs(y)) + 1
    if x*y > 0: ans += 1
print(ans)

Measured memory_bytes: 3060
#include<bits/stdc++.h> 
using namespace std; 
  
const int MAX = 1000001; 
  
// structure to store queries 
struct Query 
{ 
    int l, r, idx; 
}; 
  
// cmp function to sort queries according to r 
bool cmp(Query x, Query y) 
{ 
    return x.r < y.r; 
} 
  
// updating the bit array 
void update(int idx, int val, int bit[], int n) 
{ 
    for (; idx <= n; idx += idx&-idx) 
        bit[idx] += val; 
} 
  
// querying the bit array 
int query(int idx, int bit[], int n) 
{ 
    int sum = 0; 
    for (; idx>0; idx-=idx&-idx) 
        sum += bit[idx]; 
    return sum; 
} 
  
void answeringQueries(int arr[], int n, Query queries[], int q) 
{ 
    // initialising bit array 
    int bit[n+1]; 
    memset(bit, 0, sizeof(bit)); 
  
    // holds the rightmost index of any number 
    // as numbers of a[i] are less than or equal to 10^6 
    int last_visit[MAX]; 
    memset(last_visit, -1, sizeof(last_visit)); 
  
    // answer for each query 
    int ans[q]; 
    int query_counter = 0; 
    for (int i=0; i<n; i++) 
    { 
        // If last visit is not -1 update -1 at the 
        // idx equal to last_visit[arr[i]] 
        if (last_visit[arr[i]] !=-1) 
            update (last_visit[arr[i]] + 1, -1, bit, n); 
  
        // Setting last_visit[arr[i]] as i and updating 
        // the bit array accordingly 
        last_visit[arr[i]] = i; 
        update(i + 1, 1, bit, n); 
  
        // If i is equal to r of any query  store answer 
        // for that query in ans[] 
        while (query_counter < q && queries[query_counter].r == i) 
        { 
            ans[queries[query_counter].idx] = 
                     query(queries[query_counter].r + 1, bit, n)- 
                     query(queries[query_counter].l, bit, n); 
            query_counter++; 
        } 
    } 
  
    // print answer for each query 
    for (int i=0; i<q; i++) 
        cout << ans[i] << endl; 
} 
  
// driver code 
int main() 
{ 

    int n,q;
    cin>>n>>q;
    int a[n];
    for(int i =0;i<n;i++)
        cin>>a[i]; 
    Query queries[q];
    for(int i=0;i<q;i++){
        int x,y,in;
        cin>>x>>y;
        x--;
        y--;
        // cout<<x<<" "<<y<<" "<<in<<endl;;
        queries[i].l = x;
        queries[i].r = y;
        queries[i].idx = i;
    } 
    // int q = sizeof(queries)/sizeof(queries[0]); 
    sort(queries, queries+q, cmp); 
    answeringQueries(a, n, queries, q); 
    // return 0; 
} 

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

#define F first
#define S second
#define PB push_back
#define MK make_pair
#define REP(i, a, b) for (int i=a; i < b; i++)


bool sort_by(pi c, pi d){
	return c.S < d.S;
}

int main() {
	ll N;
	cin >> N;
	cout << ll((N*(N-1))/2);

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> c(n + 1), x(n + 1), y(n + 1);
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        c[a[i]]++;
        x[a[i]]++;
    }
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
        cin >> b[i];
        c[b[i]]++;
        y[b[i]]++;
    }
    for (int i = 1; i <= n; i++) {
        if (c[i] > n) {
            cout << "No" << '\n';
            return 0;
        }
    }
    cout << "Yes" << '\n';
    vector<int> o(n);
    iota(o.begin(), o.end(), 1);
    sort(o.begin(), o.end(), [&](int i, int j) {
        return c[i] > c[j];
    });
    int k = 0;
    a = b = vector<int>(n);
    int ia = 0, ib = 0;
    int ra = n, rb = n;
    for (int i : o) {
        if (k == n) {
            debug(i, ia, ib);
            debug(x[i], y[i]);
            for (int j = 0; j < x[i]; j++) {
                while (a[ia]) {
                    ia++;
                }
                a[ia] = i;
            }
            for (int j = 0; j < y[i]; j++) {
                while (b[ib]) {
                    ib++;
                }
                b[ib] = i;
            }
            debug("");
        } else if (k + c[i] <= n) {
            for (int j = k; j < k + x[i]; j++) {
                a[j] = i;
            }
            for (int j = k + x[i]; j < k + x[i] + y[i]; j++) {
                b[j] = i;
            }
            ra -= x[i];
            rb -= y[i];
            k += c[i];
        } else {
            int z = -1;
            debug(k);
            for (int j = 0; j <= n - k; j++) {
                if (n - k - j > y[i] || j > x[i]) {
                    continue;
                }
                if (ra - n + k >= x[i] - j && rb - n + k >= y[i] - n + k + j) {
                    z = j;
                    break;
                }
            }
            debug(z);
            assert(z != -1);
            for (int j = 0; j < z; j++) {
                a[n - 1 - j] = i;
            }
            for (int j = z; j < n - k; j++) {
                b[n - 1 - j] = i;
            }
            x[i] -= z;
            y[i] -= n - k - z;
            for (int j = 0; j < x[i]; j++) {
                while (a[ia]) {
                    ia++;
                }
                a[ia] = i;
            }
            for (int j = 0; j < y[i]; j++) {
                while (b[ib]) {
                    ib++;
                }
                b[ib] = i;
            }
            k = n;
        }
        debug(a, b);
    }
    o = vector<int>(n);
    iota(o.begin(), o.end(), 0);
    sort(o.begin(), o.end(), [&](int i, int j) {
        return a[i] < a[j];
    });
    for (int i : o) {
        cout << b[i] << " ";
    }
    cout << '\n';
    return 0;
}

Measured memory_bytes: 8784
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
	N, Q = MAP()
	ab = [LIST() for _ in range(N-1)]
	px = [LIST() for _ in range(Q)]

	graph = defaultdict(list)

	for a, b in ab:
		graph[a-1].append(b-1)
		graph[b-1].append(a-1)

	dic = defaultdict(int)

	for p, x in px:
		dic[p-1] += x

	ans = [0]*N

	checked = [0]*N
	checked[0] = 1

	q = deque([])
	q.append(0)
	
	while q:
		n = q.popleft()
		ans[n] += dic[n]
		for node in graph[n]:
			if checked[node]:
				continue
			ans[node] += ans[n]
			checked[node] = 1
			q.append(node)

	print(*ans)

if __name__ == '__main__':
	main()


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

int64_t gcd(int a, int b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}

int64_t lcm(int a, int b)
{
   return (int64_t) a * b / gcd(a, b);
}

int main()
{
   int a, b;
   cin >> a >> b;

//   cout << "最大公約数:" << gcd(a, b) << endl;
   cout  << lcm(a, b) << endl;
   return 0;
}

Measured memory_bytes: 256
fun main(args: Array<String>) {
    abc070d()
}

private data class Edge(val from: Int, val to: Int, val cost: Long)

fun abc070d() {
    val n = readLine()!!.toInt()
    val abcList = (1 until n)
        .map { readLine()!!.split(' ').map { it.toInt() }.let { Edge(it[0] - 1, it[1] - 1, it[2].toLong()) } }
    val (q, k) = readLine()!!.split(' ').map { it.toInt() }
    val queries = (1..q)
        .map { readLine()!!.split(' ').map { it.toInt() }.let { it[0] - 1 to it[1] - 1 } }

    // [from] = to, cost
    val graph = Array(n) { mutableListOf<Pair<Int, Long>>() }
    abcList.forEach {
        val (from, to, cost) = it
        graph[from].add(to to cost)
        graph[to].add(from to cost)
    }

    // cost from K
    val costs = LongArray(n) { -1 }

    fun dfs(beforeCost: Long, currIndex: Int, currCost: Long) {
        costs[currIndex] = beforeCost + currCost

        graph[currIndex].filter { costs[it.first] == -1L }.forEach {
            val (nextIndex, nextCost) = it
            dfs(costs[currIndex], nextIndex, nextCost)
        }
    }

    dfs(0, k - 1, 0)

    val answer = queries.map { costs[it.first] + costs[it.second] }.joinToString(System.lineSeparator())

    println(answer)
}


Measured memory_bytes: 131268
#include<iostream>
using namespace std;
int K;
string f(string s){
    //cout<<s<<endl;
    for(int i=s.size()-1;i>0;i--){
        //cout<<s[i]-'0'<<"search"<<endl;
        int a=s[i-1]-'0',b=s[i]-'0';
        if((a!=9&&a==b)||a==b+1){//一個増やせる
            s[i]=s[i]+1;
            for(int j=i+1;j<s.size();j++){
                if(s[j-1]=='0')s[j]='0';
                else s[j]=s[j-1]-1;
            }
            return s;
        }
        else if(a+1==b||(a==9&&b==9)){
            if(i-1==0){
                //cout<<"last keta"<<endl;
                if(s[0]!='9'){
                    s[0]=s[0]+1;
                    for(int j=1;j<s.size();j++){
                        if(s[j-1]=='0')s[j]='0';
                        else s[j]=s[j-1]-1;
                    }
                    return s;
                }
                else{
                    for(int j=0;j<s.size();j++)s[j]='0';
                    s="1"+s;
                    return s;

                }
            }
            //cout<<"next num"<<endl;
        }
    }
}
int main(){
    cin>>K;
    if(K<=10){cout<<K<<endl;return 0;}
    string num="10";
    K-=10;
    for(int i=0;i<K;i++){
        num=f(num);
        //cout<<num<<endl;
    }
    cout<<num<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std; typedef long long ll; const int INF=1e9+7;
typedef pair<int,int> P;

int main() {
  string s; cin>>s;
  int w=0;
  for(int i=0;i<s.size();i++) w+=s[i]=='x';
  if (w>7) cout<<"NO"<<endl; else cout<<"YES"<<endl;
}

Measured memory_bytes: 256
#include <cstdio>
using namespace std;
int main(void) {
	int n; scanf("%d", &n);
	char t[21];
	int l = 0;
	int r = n-1;
	int c = 0;
	for(int i=0;i<20;i++){
		printf("%d\n", c);
		fflush(stdout);
		char s[10];
		scanf("%s", &s);
		if(s[0]=='V'){
			return 0;
		}else{
			t[i] = s[0];
			if(i==0){
				c = (l+r)/2;
			}
			if(i>0){
				if(c%2==0 && t[0]!=t[i]){
					r = c;
					c = (l+r)/2;
					continue;
				}
				if(c%2==0 && t[0]==t[i]){
					l = c+1;
					c = (l+r)/2;
					continue;
				}
				if(c%2==1 && t[0]==t[i]){
					r = c;
					c = (l+r)/2;
					continue;
				}
				if(c%2==1 && t[0]!=t[i]){
					l = c+1;
					c = (l+r)/2;
					continue;
				}
			}
		}
		//c = (l+r)/2;
	}
	return 0;
}

Measured memory_bytes: 760
L, R = map(int, input().split())
# print("debug: " + str(L*R%2019))
if R - L >= 2019:
    print(0)
# elif L <= 2019 and R <= 2019:
#     print(L*R%2019)
else:
    ans = 2020
    L_mod = L % 2019
    R_mod = R % 2019
    for i in range(min(L_mod,R_mod),max(L_mod,R_mod)):
        for j in range(i+1,max(L_mod,R_mod)+1):
            # print((i*j)%2019, i, j)
            ans = min(ans, (i*j)%2019)
    print(ans)

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

int main(void){
  string s, ans="";cin >> s;
  for(int i=0;i<s.size();i++){
    char c=s.at(i);
    if(c!='B'){
      ans+=c;
    }else if(ans != ""){
      ans.pop_back();
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for( int i=a; i<b; i++ )
#define REP(i,n) rep(i,0,n)
using lli = long long int;
using ulli = unsigned long long int;
typedef pair<lli,lli> P;

struct edge{ int u, v, cost; };
struct DirectedGraph
{
  int vertex_num;
  int edge_num;
  vector<edge> edge_list;
  vector< vector<int> > adj_list;
};

void displayVector(vector<lli> v)
{
	lli n = v.size();
	REP(i,n){
		cout << v[i] << " " ;
	}
	cout << endl;
}

int max_v( vector<int> x)
{
	std::vector<int>::iterator iter = std::max_element(x.begin(), x.end());
  size_t index = std::distance(x.begin(), iter);
	return x[index];
}

int main(){
	lli res;
	int n, q; cin >> n;
	vector<int> a(n); REP(i, n){cin >> a[i]; }
	cin >> q;
	vector<int> b(q), c(q); REP(i, q){cin >> b[i] >> c[i]; }

	sort( a.begin(), a.end() );
	res = accumulate(a.begin(), a.end(), 0LL);


	vector<int> hist( max(a[n-1], max_v(c)) + 10);
	REP(i, n){ hist.at(a[i])++; }

	REP(i, q){
		int tmp = hist.at(b[i]);
		res += tmp * (c[i]-b[i]);
		hist.at(b[i]) = 0;
		hist.at(c[i]) += tmp;

		cout << res << endl;
	}
	return 0;
}

Measured memory_bytes: 5100
x, k, d = input().split(' ')
x = int(x)
k = int(k)
d = int(d)
x = x - k * d
ans = abs(x)
low = 0
high = k
d = d * 2
while low <= high:
	mid = (low + high) // 2
	pos = x + mid * d
	ans = min(ans, abs(pos))
	if pos == 0:
		break
	elif pos > 0:
		high = mid - 1
	else:
		low = mid + 1
print(ans)



Measured memory_bytes: 61952
#include <math.h>
#include <ctype.h>
#include <limits.h>
#include <assert.h>

#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>

#define nl                std::cout << "\n"
#define in(x)             std::cin  >> (x)
#define out(x)            std::cout << (x) << " "
#define err(x)            std::cerr << #x << "=" << (x) << " | "

#define inn(x)            int x; in(x)
#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define per(i, n)         for (int i = (n) - 1; i > -1; --i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)

#define no                puts("NO")
#define yes               puts("YES")

using ll  = long long int;
using vi  = std::vector<int>;
using ii  = std::pair<int, int>;
using iii = std::pair< ii, int>;
using vii = std::vector<ii>;

const int N = 1010101;
const int MD = 1e9 + 7;

void solve() {
    inn(n);
    inn(W);

    int v[n], w[n];
    rep(i, n) {
        in(w[i]);
        in(v[i]);
    }

    int V = *std::max_element(v, v + n)*n;

    ll dp[V + 1]; rep(j, V + 1) dp[j] = INT_MAX;
    dp[0] = 0; dp[v[0]] = w[0];
    repr(i, 1, n) {
        for (int j = V; j >= v[i]; --j) {
            ll x = dp[j - v[i]] + w[i];
            if (x < dp[j]) dp[j] = x;
        }
    }

    per(i, V + 1) {
        if (dp[i] <= W) {
            out(i); break;
        }
    }
}

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

    //inn(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <math.h>
#include <iomanip>
#include <vector>
#include <queue>
#include <functional>
#include <random>
#include <time.h>
using namespace std;
typedef long long int ll;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s;
	cin >> s;
	ll ans = 0;
	int n = s.size();
	for (int i = 0; i < n; i++)for (int j = 0; i + j < n; j++) {
		if (i + j < n - 1)ans += (s[i] - '0')*pow(10, j)*(1 << (n - j - 2));
		else ans += (s[i] - '0')*pow(10, j)*(1 << (n - j - 1));
	}
	cout << ans << endl;
	return 0;
}


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

int main() {
  int N,yon = 0,ni = 0,iti = 0;
  cin >> N;
  for(int i=0; i<N; i++){
    int X;
    cin >> X;
    if(X%4 == 0){
      yon++;
    }
    else if(X%2 == 0){
      ni++;
    }
    else{
      iti++;
    }
  }
  if(ni == 0){
    if(iti-1 <= yon){
      cout << "Yes" << endl;
      return 0;
    }
    else{
      cout << "No" << endl;
      return 0;
    }
  }
  else{
    if(iti <= yon){
      cout << "Yes" << endl;
      return 0;
    }
    else{
      cout << "No" << endl;
      return 0;
    }
  }
}


Measured memory_bytes: 3616
#include <iostream>
#include <map>

using namespace std;

long long wy[10];

map<pair<int, int>, int> m;

int main()
{
    ios_base::sync_with_stdio( 0 );
    cin.tie( NULL );
    long long w, h, n;
    cin >> w >> h >> n;
    wy[0] = (w-2)*(h-2);
    pair<int, int> p;
    for( int x=0; x<n; x++ ) {
        cin >> p.first >> p.second;
        for( int i=-2; i<=0; i++ ) {
            for( int j=-2; j<=0; j++ ) {
                if( p.first+i>0 && p.first+i+2<=w && p.second+j>0 && p.second+j+2<=h ) {
                    wy[ m[{p.first+i, p.second+j}] ]--;
                    m[{p.first+i, p.second+j}]++;
                    wy[ m[{p.first+i, p.second+j}] ]++;
                }
            }
        }
    }
    for( long long i : wy ) {
        cout << i << "\n";
    }
    return 0;
}


Measured memory_bytes: 56576
def main(str)
  lines = str.split("\n")
  h, w, k = lines[0].split(" ").map(&:to_i)
  cs = lines[1..-1].map { |x| x.split(//) }
  subx(cs, 0, h, w, k)
end

def subx(cs, x, h, w, k)
  return suby(cs, 0, h, w, k) if h == x
  x0 = subx(copy(cs, h, w), x + 1, h, w, k)
  copied = copy(cs, h, w)
  (0...w).each { |j| copied[x][j] = "-" }
  x1 = subx(copied, x + 1, h, w, k)
  x0 + x1
end

def suby(cs, y, h, w, k)
  return count(cs, h, w) == k ? 1 : 0 if w == y
  x0 = suby(copy(cs, h, w), y + 1, h, w, k)
  copied = copy(cs, h, w)
  (0...h).each { |i| copied[i][y] = "-" }
  x1 = suby(copied, y + 1, h, w, k)
  x0 + x1
end

def copy(cs, h, w)
  copied = []
  (0...h).each do |i|
    row = []
    copied << row
    (0...w).each do |j|
      row << cs[i][j]
    end
  end
  copied
end

def count(cs, h, w)
  count = 0
  (0...h).each do |i|
    (0...w).each do |j|
      count += 1 if cs[i][j] == "#"
    end
  end
  count
end

unless ENV["TEST"]
  puts(main($stdin.read.chomp))
  exit(0)
end

require 'minitest/autorun'

class TestA < Minitest::Test
  def test_main
    assert_equal(5, main("2 3 2
..#
###"))
    assert_equal(1, main("2 3 4
..#
###"))
    assert_equal(0, main("2 2 3
##
##"))
    assert_equal(208, main("6 6 8
..##..
.#..#.
#....#
######
#....#
#....#"))
  end
end




Measured memory_bytes: 14252