akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;
using ll = long long;

ll total[3005][3005];
ll dpmax[3005][3005];
int arr[3005];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
        dpmax[i][i] = arr[i];
    }
    if (n == 1) {
        printf("%lld\n", dpmax[0][n-1]);
        return 0;
    }
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (j-1 >= i) {
                total[i][j] = total[i][j-1] + arr[j];
            } else {
                total[i][j] = arr[j];
            }  
        }
    }
    for (int i = 0; i < n; i++) {
        if (i+1 < n) {
            dpmax[i][i+1] = max(arr[i], arr[i+1]);
        }
    }
    for (int k = 2; k < n; k++) {
        for (int i = 0; i < n; i++) {
            if (i+k < n) {
                dpmax[i][i+k] = max(arr[i]+total[i+1][i+k]-dpmax[i+1][i+k], arr[i+k]+total[i][i+k-1]-dpmax[i][i+k-1]);
            }
        }
    }
    
    // printf("%lld %lld", dpmax[0][n-1], dpmax[0][n-1]-(total[0][n-1]-dpmax[0][n-1]));
    printf("%lld", dpmax[0][n-1]-(total[0][n-1]-dpmax[0][n-1]));
    return 0;
}

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

int main(){
    double N,K; cin>>N>>K;
    vector<double> p(N);
    rep(i,N) cin>>p[i];

    double avg=0;
    for(int i=0; i<K; i++){
        avg += (p[i]*(1+p[i]))/(2*p[i]);
    }
    // cout << avg << endl;

    double max_avg=avg,ret_avg=avg;
    for(int i=0; i+K<N; i++){
        ret_avg -= (p[i]*(1+p[i]))/(2*p[i]);
        ret_avg += (p[i+K]*(1+p[i+K]))/(2*p[i+K]);
        // cout << ret_avg << endl;
        max_avg = max(max_avg, ret_avg);
    }

    cout << fixed << setprecision(15) << max_avg << endl;
}


Measured memory_bytes: 4880
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;

int main(){
	ios_base::sync_with_stdio(false);
	ll n, k;
	cin >> n >> k;
	ll answer = 0;
	if(k % 2 == 0){
		const ll t = n / k;
		const ll s = t + (n % k >= k / 2 ? 1 : 0);
		answer = t * t * t + s * s * s;
	}else{
		const ll t = n / k;
		answer = t * t * t;
	}
	cout << answer << endl;
	return 0;
}



Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main()
{
  int n;
  cin >> n;
  int count = 0;
  bool bOK = true;
  for(int i=0; i<n; i++){
    char p;
    int x;
    cin >> p >> x;
    if(p == '(') {
      count += x;
      continue;
    }
    count -= x;
    if(count < 0) bOK = false;
  }
  if(bOK && count == 0) cout << "YES" << endl;
  else cout << "NO" << endl;
  return 0;
}

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

int main() {
	int n, x;
	cin >> n >> x;
	vector<int> cake(n);
	for (int i = 0; i < n; i++) {
		cin >> cake.at(i);
	}
	sort(cake.begin(), cake.end());
	int sum = 0;
	int count = 0;
	for (int i = 0; i < n; i++) {
		sum += cake.at(i);
		if (x >= sum) count++;
		else break;
	}
	if (x > sum)count--;
	cout << count << endl;
}

Measured memory_bytes: 256
import math

n = int(input())
x = [float(x) for x in input().split()]
y = [float(y) for y in input().split()]
d = 0
for i in range(n):
    d += abs(x[i] - y[i])

print(d)

d = 0
for i in range(n):
    d += abs((x[i] - y[i])**2)

d = math.sqrt(d)
print(d)

d = 0
for i in range(n):
    d += abs((x[i] - y[i])**3)

d = math.pow(d, 1.0/3.0)
print(d)

d = 0
for i in range(n):
    if d <= abs(x[i] - y[i]):
        d = abs(x[i] - y[i])

print(d)

Measured memory_bytes: 7888
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
long double const pi = std::acos(-1.0L);
typedef pair<int,int> P;

int main() {
  int n, x, t;
  cin >> n >> x >> t;
  int ans = 0;
  rep(i,1001){
    if(x*i>=n){
      ans = i;
      break;
    }
  }
  cout << ans*t << endl;
  return 0;
}

Measured memory_bytes: 3632
#include<cstdio>
int n,k,x,maxv,g;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%d",&x); if(x>maxv) maxv=x;
		g=gcd(g,x);
	}
	if(k%g||k>maxv) printf("IMPOSSIBLE"); else printf("POSSIBLE");
	return 0;
}

Measured memory_bytes: 128
n = int(input())
p = list(map(float, input().split()))

n2 = n//2
dp = [[0]*(n+1) for _ in range(n+1)]
dp[0][0] = 1

for i in range(n):
    for j in range(i+1):
        dp[i+1][j] = dp[i+1][j] + dp[i][j]*(1-p[i])
        dp[i+1][j+1] = dp[i+1][j+1] + dp[i][j]*p[i]

print(sum(dp[-1][n2+1:]))

Measured memory_bytes: 254472
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(D)]
t = []
last = [[-1] for i in range(26)]

# max index greedy search
for i in range(D):
    op = 0
    mx = 0
    for j in range(26):
        if s[i][j]*c[j] > mx:
            mx = s[i][j]*c[j]
            op = j
    t.append(op+1)
    last[op].append(i)
    # ans += s[i][op]
    # last[op]=i
    # for j in range(26):
    #     ans -= c[j]*(i-last[j])
    # for j in range(26):
    #     ans -= c[j]*(i-last[j][-1])

# update
from bisect import *
import random
m = 800000
def suminc(a,b):
    return b*(b-1)//2-a*(a-1)//2
while m:
    m-=1
    update = 0
    d = random.randint(1,D)
    q = random.randint(1,26)
    d-=1
    q-=1
    oq = t[d]-1
    update += s[d][q]-s[d][oq]
    idx = bisect_left(last[oq],d)
    if idx == len(last[oq])-1:
        cnt = D - last[oq][idx]
    else:
        cnt = last[oq][idx+1] - last[oq][idx]
    update += c[oq]*suminc(0,cnt)
    cntprv = last[oq][idx] - last[oq][idx-1]
    update -= c[oq]*suminc(cntprv,cntprv+cnt)

    idx2 = bisect_left(last[q],d)-1
    if idx2 == len(last[q])-1:
        cnt = D - d
    else:
        cnt = last[q][idx2+1] - d
    if idx2 == len(last[q])-1:
        cntprv = D - last[q][idx2] - cnt
    else:
        cntprv = last[q][idx2+1] - last[q][idx2] - cnt
    update += c[q]*suminc(cntprv,cntprv+cnt)
    update -= c[q]*suminc(0,cnt)

    if update > 0:
    # ans += update
        del last[oq][idx]
        last[q].insert(idx2+1,d)
        # print(ans)
        t[d] = q+1

# with open('ramdom.out', 'w') as f:
#     for i in range(D):
#         print(t[i],file=f)

for i in range(D):
    print(t[i])


Measured memory_bytes: 88524
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

int add (string a, string b)
{
	reverse (a.begin(), a.end() );
	reverse (b.begin(), b.end() );

	while (a.length() != b.length()){
		if (a.length() > b.length()){
			b += '0';
		}else if (a.length() < b.length() ){
			a += '0';
		} // end if
	} // end while

	int i;
	string res = "";
	int carry = 0;

	for (i = 0; i < a.length(); ++i){

		int an = a[i] - '0';
		int bn = b[i] - '0';
		int sum = an + bn + carry;

		res += sum % 10 + '0';
		carry = sum/10;

	} // end for

	if (carry == 1){
		res += carry + '0';
	} // end if

	return res.length();
}

int main()
{
	string str = "";
	string a,b;

	while (getline (cin, str )){

		stringstream ssn(str );
		ssn >> a >> b;

		int res = add (a, b );

		cout << res << endl;	

	} // end while
		
	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;

int main() {
	ll n,m;
	cin >> n >> m;
	vector<ll> a(n,0);
	vector<int> b(n,0);
	ll lcm = 1;
	rep(i,n) {
		cin >> a[i];
		int x = a[i];
		while (x % 2 == 0)
		{
			x /= 2; 
			b[i]++;
		}
		lcm = a[i] / __gcd(a[i],lcm) * lcm;
	} 

	rep(i,n-1) {
		if (b[i] != b[i+1]) {
			cout << 0 << endl;
			return 0;
		}
	}
	ll ans = (2*m/lcm + 1) / 2;
	cout << ans << endl;


	return 0;
}

Measured memory_bytes: 4304
n,m=map(int,input().split())
stu=[]
for i in range(n):
    stu.append(list(map(int,input().split())))
check=[]
for i in range(m):
    check.append(list(map(int,input().split())))
for i in stu:
    res=float('inf')
    point=-1
    for q,j in enumerate(check):
        tmp = abs(i[0]-j[0])+abs(i[1]-j[1])
        if res>tmp:
            res=tmp
            point=q+1
    print(point)


Measured memory_bytes: 3064
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = 0;
        String t = "+";
        String s = sc.next();

        for (int i = 0; i < 4; i++) {
            if (t.equals(s.substring(i,i + 1))) {
                n = n + 1;
            } else {
                n = n - 1;
            }

        }
        System.out.println(n);

    }
}



Measured memory_bytes: 22868
#include<bits/stdc++.h>
#define For(i,l,r) for(int i = (l),i##end = (r);i <= i##end;i++)
#define Fordown(i,r,l) for(int i = (r),i##end = (l);i >= i##end;i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x,T y) { return y < x ? x = y,1 : 0; }
template <typename T> inline bool chkmax(T &x,T y) { return x < y ? x = y,1 : 0; }

const int INF = 0x3f3f3f3f;
const int N = (1 << 17) + 10;

int n,a,b;
int vis[N];

inline int read() {
	int x = 0,flag = 1;
	char ch = getchar();
	while(!isdigit(ch) && ch != '-')ch = getchar();
	if(ch == '-')flag = -1,ch = getchar();
	while(isdigit(ch))x = (x << 3) + (x << 1) + (ch - '0'),ch = getchar();
	return x * flag;
}

inline void dfs(int diff,int st,int S) {
	if(__builtin_popcount(S) == 1) {
		printf("%d %d ",st,st ^ S);
		return;
	}
	For(i,0,n - 1) if((diff & (1 << i)) && (S & (1 << i)))
		For(j,0,n - 1) if((S & (1 << j)) && i != j) {
			dfs(1 << j,st,S ^ (1 << i));
			dfs(diff ^ (1 << i) ^ (1 << j),st ^ (1 << i) ^ (1 << j),S ^ (1 << i));
			return;
		}
}

int main() {

	n = read(),a = read(),b = read();
	if(!((__builtin_popcount(a ^ b)) & 1)) return puts("NO"),0;
	puts("YES");
	dfs(a ^ b,a,(1 << n) - 1);

	return 0;
}

Measured memory_bytes: 1024
def insertion(A, n, g):
    cnt = 0
    for i in range(g, n):
        while i >= g and A[i - g] > A[i]:
            A[i], A[i - g] = A[i - g], A[i]
            i -= g
            cnt += 1
    return cnt

def main():
    n = int(raw_input())
    A = [int(raw_input()) for i in range(n)]

    G = list()
    h = k = 1
    while h <= n:
        G.append(h)
        h = (4 ** k) + (3 * (2 ** (k - 1)) + 1)
        k += 1
    G = G[::-1]

    cnt = 0
    for i in G:
        cnt += insertion(A, n, i)

    print(len(G))
    print(" ".join(map(str, G)))
    print(cnt)
    for j in A: print(j)

main()

Measured memory_bytes: 75576
// Example program
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;


int main()
{
    string input;
    getline( cin, input);
    getline( cin, input);
    vector<int> numbers;
    istringstream iss(input);
    int x;
    while ( iss >> x) {    
        numbers.push_back(x);
    }
    int cnt = 0;
    int idx = 1;
    for(int i : numbers) {
        if(idx == numbers[(i - 1)])   
            cnt++;
        idx++;
    }
    cout << cnt/2 << endl;
}


Measured memory_bytes: 2308
INF = 10 ** 10
n, _ = gets.split.map(&:to_i)
nums = gets.split.map(&:to_i)
lowest = INF
possible = 0
nums.each do |num|
  possible = num - lowest if possible < num - lowest
  lowest = num if lowest > num
end

count = 0
lowest = INF
nums.each do |num|
  count += 1 if num - lowest == possible
  lowest = num if lowest > num
end

puts count


Measured memory_bytes: 20880
n=int(input())
A=list(map(int,input().split()))
x=[]
for i in range(n):
    x.append([A[i],i+1])
    
y=sorted(x,key=lambda x:x[0])
s=''
for i in range(n):
    s+=str(y[i][1])+' '
print(s)

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

int main(){
    int H,W;
    for(;;){
        scanf("%d %d",&H,&W);
        if (H==0&&W==0)break;
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                printf("#");
            }printf("\n");
        }printf("\n");
    }
}


Measured memory_bytes: 2704
package main

import (
        "fmt"
)

func main() {
        var stk [101]int
        var c int
        pvt := 0
        for {
                _, err := fmt.Scanf("%d", &c)
                if err != nil {
                        break
                }
                if c > 0 {
                        stk[pvt] = c
                        pvt++
                } else {
                        pvt--
                        fmt.Printf("%d\n", stk[pvt])
                }
        }
}



Measured memory_bytes: 1136
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
#include <queue>
#include <iomanip>
#include <algorithm>
#pragma comment(linker, "/STACK:16000000")
using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

const int mod = 1000000007;
const int Maxn = 200005;
const int Maxm = 20;

int pw2[Maxn];
int n;
vector <int> neigh[Maxn];
int cur, lef[Maxn], rig[Maxn];
int L[Maxn], P[Maxn][Maxm];
vector <int> inlvl[Maxn];
int res;
vector <int> tneigh[Maxn];
int res0[Maxn], res1[Maxn];

void Traverse(int v)
{
	lef[v] = ++cur;
	inlvl[L[v]].push_back(v);
	for (int i = 0; i < neigh[v].size(); i++) {
		int u = neigh[v][i];
		L[u] = L[v] + 1; P[u][0] = v;
		Traverse(u);
	}
	rig[v] = cur;
}

int getLCA(int a, int b)
{
	if (L[a] < L[b]) swap(a, b);
	for (int i = Maxm - 1; i >= 0; i--)
		if (L[a] - (1 << i) >= L[b])
			a = P[a][i];
	if (a == b) return a;
	for (int i = Maxm - 1; i >= 0; i--)
		if (P[a][i] != P[b][i])
			a = P[a][i], b = P[b][i];
	return P[a][0];
}

int Inv(int a)
{
	int p = mod - 2;
	int res = 1;
	while (p) {
		if (p & 1) res = ll(res) * a % mod;
		p >>= 1; a = ll(a) * a % mod;
	}
	return res;
}

void Compute(int v)
{
	if (tneigh[v].empty()) {
		res0[v] = 1; res1[v] = 1;
		return;
	}
	int all = 1, zers = 1;
	int nils = 0;
	for (int i = 0; i < tneigh[v].size(); i++) {
		int u = tneigh[v][i];
		Compute(u);
		all = ll(all) * (res0[u] + res1[u]) % mod;
		if (res0[u] == 0) nils++;
		else zers = ll(zers) * res0[u] % mod;
	}
	res1[v] = 0;
	for (int i = 0; i < tneigh[v].size(); i++) {
		int u = tneigh[v][i];
		int my;
		if (res0[u] == 0)
			if (nils - 1 > 0) my = 0;
			else my = ll(zers) * res1[u] % mod;
		else if (nils > 0) my = 0;
			 else my = ll(zers) * Inv(res0[u]) % mod * res1[u] % mod;
		res1[v] = (res1[v] + my) % mod;
	}
	res0[v] = (all - res1[v] + mod) % mod;
}

int Solve(int lvl)
{
	vector <ii> seq;
	for (int i = 0; i < inlvl[lvl].size(); i++) {
		int v = inlvl[lvl][i];
		seq.push_back(ii(lef[v], v));
		if (i > 0) {
			int u = inlvl[lvl][i - 1];
			int lca = getLCA(u, v);
			seq.push_back(ii(lef[lca], lca));
		}
	}
	sort(seq.begin(), seq.end()); seq.erase(unique(seq.begin(), seq.end()), seq.end());
	for (int i = 0; i < seq.size(); i++)
		tneigh[seq[i].second].clear();
	vector <int> S;
	for (int i = 0; i < seq.size(); i++) {
		int v = seq[i].second;
		while (!S.empty() && rig[S.back()] < seq[i].first) S.pop_back();
		if (!S.empty()) tneigh[S.back()].push_back(seq[i].second);
		S.push_back(seq[i].second);
	}
	Compute(seq[0].second);
	return res1[seq[0].second];
}

int main()
{
	pw2[0] = 1;
	for (int i = 1; i < Maxn; i++)
		pw2[i] = 2ll * pw2[i - 1] % mod;
	scanf("%d", &n); n++;
	for (int i = 2; i <= n; i++) {
		int p; scanf("%d", &p); p++;
		neigh[p].push_back(i);
	}
	Traverse(1);
	for (int j = 1; j < Maxm; j++)
		for (int i = 1; i <= n; i++)
			P[i][j] = P[P[i][j - 1]][j - 1];
	for (int i = 0; i <= n; i++) if (inlvl[i].size() > 0)
		res = (res + ll(Solve(i)) * pw2[n - int(inlvl[i].size())]) % mod;
	printf("%d\n", res);
	return 0;
}

Measured memory_bytes: 53376
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <deque>
#include <list>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;


#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repr(i,n) for(int (i)=(n); (i)>=(0); (i)--)
#define SIZE(x) ((int)(x).size())
#define GCD(a, b) int gcd(int a,int b){return b?gcd(b,a%b):a;}
#define QP(a, b) int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(v) (v).begin(),(v).end()


template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
template<typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

typedef vector<int, int> vint;
typedef pair<int,int> pint;
typedef vector<pint>vpint;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, c; cin >> n >> m >> c;
    int ans = 0;
    vector<int> B(m);
    rep(i, m) cin >> B[i];
    rep(i, n){
        int sum = 0;
        rep(j, m){
            int a; cin >> a;
            sum += a*B[j];
        }
        if(sum+c > 0) ans++;
    }
    cout << ans;
    cout << endl;
    return 0;
}




Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
ll n,k;
int main()
{
	scanf("%lld%lld",&n,&k);
	if(k==0) 
	{
		return printf("%lld\n",n*n),0;
	}
	ll ans=0;
	for(int i=k+1;i<=n;i++)
	{
		for(int j=k;j<=n;j+=i)
		{
			int nex=min(n,(j+(i-k-1)));
			ans+=(nex-j+1);
		}
	}
	printf("%lld\n",ans);
}


Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
void initialize(void);
int isEmpty(void);
int isFull(void);
void  push(int);
int pop(void);
int top;
int stack[MAX];
int main(){
  int a,b;
  char s[100];
  while( scanf("%s", s) != EOF ){
    if ( s[0] == '+' ){
      push(pop()+pop());
    } 
    else if ( s[0] == '-' ){
      a=pop();
      b=pop();
      push(b-a);
    } 
    else if ( s[0] == '*' ){
      push(pop()*pop());
    } 
    else {
      push(atoi(s));
    }
  }
  printf("%d\n",pop());
  return 0;
}
void initialize()
{
  top=0;
}
int isEmpty()
{
  if(top==0)
    return 1;
  else
    return 0;
}
int isFull()
{
  if(top > MAX-1)
    return 1;
  else 
    return 0;
}
void push(int x)
{
  if(isFull()){
    fprintf(stderr,"エラー（オーバーフロー）\n");
    exit(1);
  }
  stack[++top]=x;
}

int pop()
{
  if(isEmpty()){
    fprintf(stderr,"エラー（アンダーフロー）\n");
    exit(2);
  }
  top--;
  return stack[top+1];
}

Measured memory_bytes: 548
N = gets.to_i
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)

ans = 0

B.each_with_index do |b, idx|
  n = [A[idx], b].min
  b -= n

  m = [A[idx + 1], b].min
  A[idx + 1] -= m

  ans += n + m
end

puts ans


Measured memory_bytes: 19356
import math

x,y = map(int,input().split())
input()

low = math.floor(x / (y - 1))
high = math.ceil(x / (y - 1))

if low == high:
    low = low - 1

if ((y - 1) * low) + 1 >= x:
    print(low)
else:
    print(low + 1)

Measured memory_bytes: 4724
#include<bits/stdc++.h>
#define ll long long int
#define Y printf("YES\n")
#define N printf("NO\n")
#define mp make_pair
#define pb push_back
#define tp make_tuple
#define sc(a) scanf("%lld",&a)
#define sc2(a,b) scanf("%lld%lld",&a,&b)
#define pr(a) printf("%lld\n",a)
using namespace std;

#define mod 1000003


int main()
{
    ll n,t,x,y,k,i,j,p,q;

    cin>>n;

    ll a[n+5];

    map<ll,ll>m,kk;

    set<ll>s;

    k=0;

    for(i=0;i<n;i++)
    {
        cin>>a[i];

        k+=a[i];

        m[a[i]]++;

       // s.insert(a[i]);
    }

    cin>>t;

    while(t--)
    {
        cin>>x>>y;

        p=m[x];

        q=y-x;

        k+=(p*q);

        m[y]=m[y]+p;

        m[x]=0;

        cout<<k<<endl;
    }
}


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

int main( void )
{
    char tmp;
    unsigned char i = 0;
    int j = 0;
    unsigned char k = 0;
    int v = 0;
    
    int Num = 0;
   
    do{
        tmp = getchar();
        if(tmp != '\n')
        {
            Num = (Num * 10) + (tmp - '0');
        }
    }while(tmp != '\n');
   

   int Array[Num];
  
  
   for(k=0; k<Num; k++ )
   {
    Array[k]=0;
    
       do{
            tmp = getchar();
            printf("%c",tmp);
            if(tmp == '\n')
            {
                break;
            }
            
            if( tmp != ' ' )
            {
                Array[k] = Array[k] * 10 + (tmp - '0');
            }
       }while(tmp != ' ' && tmp != '\n');
   }


   for( i = 1; i < Num; i++ )
   {
         v = Array[i];
         j = i - 1;
     
         while( j >= 0 && Array[j] > v )
         {
             Array[j+1] = Array[j];
             j--;
             Array[j+1] = v;
             
         }
    
        for(k=0; k<Num; k++ )
        {
            printf("%d",Array[k]);
            if( k == Num-1 )
            {
               printf("\n");
            }
            else
            {
               printf(" ");
            }
        }
    }
    
    return 0;
}

Measured memory_bytes: 1808
n, k = map(int, input().split())
'''
while True:
  tar = abs(n - k)
  if (tar)
'''

diff = n - k
div = n // k

if (diff == 0):
    ans = 0
elif (diff < 0):
    ans = min(n, abs(n - k))
else:
    temp = n - div * k
    ans = min(temp, abs(temp - k))


print(ans)


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

using namespace std;

#define all(c) (c).begin(), (c).end()

int main() {
	string s;
	cin>>s;
	int n = s.length();
	long long sum = 0;
	for (int i = 0; i < n; i++) {
		sum += (s[i] - '0');
	}
	if (sum%9 == 0) cout<<"Yes";
	else cout<<"No";
	cout<<endl;
	return 0;
}

Measured memory_bytes: 3720
def solve():
    A, B = map(int, input().split())

    primeFactorA = prime_factorize(A)
    primeFactorB = prime_factorize(B)

    commonFactor = [1]
    for f in primeFactorB:
        if f in primeFactorA:
            commonFactor.append(f)
    
    print(len(commonFactor))


def prime_factorize(num):
    a = set() 
    while num % 2 == 0:
        a.add(2)
        num //= 2
    
    f = 3
    while f * f <= num:
        if num % f == 0:
            a.add(f)
            num //= f
        else:
            f += 2
    if num != 1:
        a.add(num)
    
    return a

if __name__ == '__main__':
    solve()


Measured memory_bytes: 38384
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int in(void){
	int i;scanf("%d",&i);
	return i;
}
long long llin(void){
	long long i;scanf("%lld",&i);
	return i;
}
void print(int a){
	printf("%d\n",a);
}
void llprint(long long a){
	printf("%lld\n",a);
}
void print2(int a,int b){
	printf("%d %d\n",a,b);
}
long long max(long long a,long long b){
	return a>b?a:b;
}
long long min(long long a,long long b){
	return a<b?a:b;
}

void solve(int h[],int w,int l){
	int i;
	for(i=0;i<5;i++){
		if(h[i]==w){
			puts("1");
		}
		else{
			puts("2");
		}
	}
}

int main(void){
	int h[5],f[4],i;
	while(1){
		memset(f,0,sizeof(f));
		for(i=0;i<5;i++){
			h[i]=in();
			if(h[i]==0){
				return 0;
			}
			f[h[i]]++;
		}
		if(f[1]&&f[2]&&f[3]||!f[1]&&!f[2]||!f[2]&&!f[3]||!f[3]&&!f[1]){
			printf("3\n3\n3\n3\n3\n");
		}
		else if(!f[1]){
			solve(h,2,3);
		}
		else if(!f[2]){
			solve(h,3,1);
		}
		else if(!f[3]){
			solve(h,1,2);
		}
	}
	return 0;
}

Measured memory_bytes: 584
#include <iostream>
#include <string>
using namespace std;
int main() {
    int H, W, A, B;
    cin >> H >> W >> A >> B;
    int t[] = {B, H - B};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < t[i]; j++) {
            cout << string(A, i + '0') + string(W - A, 1 - i + '0') << endl;
        }
    }
}


Measured memory_bytes: 1280
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <limits>
#include <numeric>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <stack>

using namespace std;

#define INFint (1<<30)
#define BOUND 27182818284
#define MAT 2

typedef long long ll;
typedef long long int lli;
typedef pair<ll, ll> P;

ll MOD = 1000000007;
const ll INF = (1ll<<60);
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for(int i=int(a);i<int(b);++i)

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

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

// gcd
template<typename T>
T gcd(T a, T b) {
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int findGCD(vector<int> arr) {
    int result = arr[0];
    for (auto a: arr) {
        result = gcd(a, result);
    }
    return result;
}

template<typename T>
T lcm(T m, T n) {
    // 引数に0がある場合は0を返す
    if ((0 == m) || (0 == n))
        return 0;
    return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    fill((T *) array, (T *) (array + N), val);
}


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

// v.front() = -BOUND;
// v.back() = BOUND;

//struct edge{
//    int cost, to;
//
//    edge(int in_cost, int in_to){
//        cost=in_cost;
//        to=in_to;
//    }
//    bool operator<(const edge &a) const
//    {
//        return cost > a.cost;
//    }
//};

int main() {
    int N, M; cin >> N >>M;
    vector<P> CB;
    vector<ll> A(N);

    rep(i,N){
        cin >> A[i];
    }

    rep(i,M){
        ll b, c; cin >> b >> c;
        CB.emplace_back(c,b);
    }

    sort(CB.begin(), CB.end(), greater<>());

    sort(A.begin(), A.end());
    int cb_num=0;
    for(int i=0; i<N; i++){
        if(cb_num>=CB.size())
            break;
        if(CB[cb_num].first>A[i] && CB[cb_num].second>0){
            A[i] = CB[cb_num].first;
            CB[cb_num].second--;
        }

        if(CB[cb_num].second==0){
            cb_num++;
        }
    }

    ll sum = 0LL;
    for(auto a :A){
        sum += a;
    }
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 3188
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>

int main(){
  int N, K;
  scanf("%d",&N);
  scanf("%d",&K);
  std::vector<long long > H(N);
  for (int i=0; i<N; i++){
    scanf("%lld",&H[i]);
  }
  std::sort(H.begin(), H.end(), std::greater<long long>());
  if (K>=N){
    printf("%d\n",0);
  } else {
    long long sum = 0;
    for (int i=K; i<N; i++){
      sum += H[i];
    }
    printf("%lld\n",sum);
  }
  return 0;
}


Measured memory_bytes: 2424
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <iomanip>
typedef long long ll;

using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    
    if(a + b + c >= 22) {
        cout << "bust" << endl;
    }
    else {
        cout << "win" << endl;
    }
    
    return 0;
}


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

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define rep(i,n) for(int i=0; i<(int)(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

//constexpr ll mod = 998244353;
constexpr ll mod = 1e9+7;
const double PI = acos(-1.0);
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

struct {
    int N;
    vector<long long> dat;

    void init(int n) {
      N = 1;
      while(N < n) N *= 2;
      dat.resize(2*N-1, LLONG_MAX);
    }

    // update k-th element
    void update(int k, long long a) {
      k += N-1;
      dat[k] = a;
      while(k > 0) {
        k = (k-1)/2;
        dat[k] = min(dat[k*2+1], dat[k*2+2]);
      }
    }

    // add k-th element by a
    void add(int k, long long a) {
      k += N-1;
      dat[k] += a;
      while(k > 0) {
        k = (k-1)/2;
        dat[k] = min(dat[k*2+1], dat[k*2+2]);
      }
    }

    // return min[a, b)
    long long query(int a, int b) {
      return query(a, b, 0, 0, N);
    }
    long long query(int a, int b, int k, int l, int r) {
      if (r <= a || b <= l) return LLONG_MAX;
      if (a <= l && r <= b) return dat[k];
      int m = (l+r)/2;
      return min(query(a, b, k*2+1, l, m), query(a, b, k*2+2, m, r));
    }
} O, E;

map<P, vector<P>> mp;

P calc(P p) {
  debug(p.first, p.second);
  vector<P> vec;
  ll l = p.first;
  ll r = p.second;
  ll first;
  if (l%2 == 0) {
    first = E.query(l, r);
  } else {
    first = O.query(l, r);
  }
  ll posfirst = first%mod;
  if (l != posfirst) {
    debug(l, posfirst);
    vec.emplace_back(l, posfirst);
  }
  l = posfirst+1;
  ll second;
  if (l%2 == 0) {
    second = E.query(l, r);
  } else {
    second = O.query(l, r);
  }
  ll possecond = second%mod;
  if (l != possecond) {
    debug(l, possecond);
    vec.emplace_back(l, possecond);
  }
  if (possecond+1 < r) {
    debug(possecond+1, r);
    vec.emplace_back(possecond+1, r);
  }
  mp[p] = vec;
  return {first/mod*mod+p.first, second/mod*mod+p.second};
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vector<int> p(n);
  O.init(n+1);
  E.init(n+1);
  rep(i, n) {
    int p; cin >> p;
    if (i%2==0) {
      E.update(i, p*mod+i);
    } else {
      O.update(i, p*mod+i);
    }
  }

  priority_queue<P, vector<P>, greater<>> que;
  que.push(calc(P(0, n)));
  while(!que.empty()) {
    P pa = que.top(); que.pop();
    cout << pa.first/mod << ' ' << pa.second/mod << ' ';
    ll l = pa.first%mod;
    ll r = pa.second%mod;
    debug(l, r);
    for(auto e: mp[P(l, r)]) {
      que.push(calc(e));
    }
  }

  cout << endl;
}

Measured memory_bytes: 21504
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

H, W = map(int, raw_input().split())

A = []

for i in range(H):
    A.append(raw_input())

H_flag = [0] * H
W_flag = [0] * W

for i in range(H):
    for j in range(W):
        if A[i][j] == '.':
            W_flag[j] += 1
            H_flag[i] += 1

for i in range(H):
    if H_flag[i] != W:
        S = ''
        for j in range(W):
            if W_flag[j] != H:
                S += A[i][j]
        print S

Measured memory_bytes: 2936
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl
#define COYE cout<<"YES"<<endl
#define COYe cout<<"Yes"<<endl
#define COye cout<<"yes"<<endl
#define CONO cout<<"NO"<<endl
#define CONo cout<<"No"<<endl
#define COno cout<<"no"<<endl
#define FORE(i,a) for(auto &i:a)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Graph=vector<vector<int>>;
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T>inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
  ll N,ans=0;
  cin>>N;
  ll A[N],B[N];
  // A[i] S[i]の最小値
  // B[i] S[i]の値
  
  REP(i,N){
    A[i]=0;
    B[i]=0;
  }
  
  string S[N];
  REP(i,N) cin>>S[i];
  
  REP(i,N){
    ll cnt=0;
    REP(j,S[i].size()){
      if(S[i].at(j)=='(') cnt++;
      else cnt--;
      chmin(A[i],cnt);
    }
    B[i]=cnt;
  }
  
  vector<pair<int,int>> L,LL,LLL;
  bool CAN=true;
  
  REP(i,N){
    if(B[i]>0){
      if(A[i]>=0ll) L.PB(MP(A[i],B[i]));
      else LL.PB(MP(A[i],B[i]));
    }
    else LLL.PB(MP(B[i]-A[i],A[i]));
  }
  
  SORT(L);
  REVERSE(L);
  SORT(LL);
  REVERSE(LL);
  SORT(LLL);
  REVERSE(LLL);
  
  REP(i,L.size()){
    if(ans+L[i].first<0) CAN=false;
    ans+=L[i].second;
  }
  
  REP(i,LL.size()){
    if(ans+LL[i].first<0) CAN=false;
    ans+=LL[i].second;
  }
  
  REP(i,LLL.size()){
    if(ans+LLL[i].second<0) CAN=false;
    ans+=LLL[i].first+LLL[i].second;
  }
  
  if(ans!=0) CAN=false;
  if(CAN) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  return 0;
}

Measured memory_bytes: 58148
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;   
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ld long double
#define dd double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (ll)(a.size())
typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef vector<pll> vplll;
ll mod = 1e9+7;
const ld PI = 2 * acos(0.0);
const vector<ll> dx = {1, -1, 0, 0};
const vector<ll> dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,c,t,n,m,k,q; 
    t = 1;
    // cin>>t;
    while(t--)
    {
    	cin>>n;
    	a = n/1000;
    	a++;
    	if(n%1000)
    	cout<<a*1000 - n<<endl;
    	else
    		cout<<0<<endl;
    }
    return 0;
}

Measured memory_bytes: 3648
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int MOD = 1000000007;
typedef long long ll;

int main(){
    int N, H, W;
    cin >> N >> H >> W;
    cout << (N - H + 1) * (N - W + 1) << endl;
    return 0;
}


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

using namespace std;

int main(){
  int n,k,ans=1;
  cin>>n>>k;

  for (int i=0;i<n;i++){
    if (ans+k > 2*ans) ans*=2;
    else ans+=k;
  }
  cout << ans << endl;

}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		try {
			while(scan.hasNext()){
				System.out.println(scan.nextLine().toUpperCase());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}

Measured memory_bytes: 17140
#include<stdio.h>

int main(){
    long int n,k,a,b,x,y,i;

    scanf("%ld",&n);
    scanf("%ld%ld",&x,&y);
    for(i=1;i<n;i++){
        scanf("%ld%ld",&a,&b);
        if((x-1)/a<(y-1)/b){
            k=(y-1)/b+1;
        }else{
            k=(x-1)/a+1;
        }
        x=a*k;
        y=k*b;
    }
    printf("%ld",x+y);
    return 0;
}


Measured memory_bytes: 1736
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
 
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define point pair<double,double>
#define segment pair<point,point>
#define circle pair<point,double> //titik pusat, radius
#define line pair<double,double> //m,c
#define ins insert
#define er erase
 
#define FORU(a,b,c) for(int a=b; a<=c; a++)
#define FORD(a,b,c) for(int a=b; a>=c; a--)
#define FOR(a,b) for(int a=1; a<=b; a++)
#define FORA(a,b) for(auto (a) : (b))
 
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 1e9+7;
 
#define yu using
#define don namespace
#define sei std
 
yu don sei;

int setBit(int bit, int idx)
{
    return bit | (1<<idx);
}

int getBit(int bit, int idx)
{
    return bit & (1<<idx);
}

int n;
int A[25][25];
ll memo[23][5000000];

ll dp(int idx, int bit)
{
    if(idx==n)return 1;
    if(memo[idx][bit]!=-1)return memo[idx][bit];
    memo[idx][bit]=0;
    for(int i=0;i<n;i++)
    {
        if((getBit(bit, i) == 0) && A[idx][i])
        {
            memo[idx][bit]+=dp(idx+1, setBit(bit, i));
            memo[idx][bit]%=MOD;
        }
    }
    return memo[idx][bit];
} 

int main()
{
    memset(memo, -1, sizeof(memo));
    cin>>n;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>A[i][j];
    cout << dp(0, 0) << endl;
    return 0;
}

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

int solve(int n) {
	long long int count = 0;
	for (int i = 0; i < n; i++) {
		char p;
		int x;
		cin >> p >> x;

		if (p == '(') {
			count += x;
		}
		else {
			count -= x;
		}

		if (count < 0) {
			return 0;
		}
	}
	return count == 0;
}

int main() {
	int n;
	cin >> n;
	if (solve(n))
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
	return 0;
}

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

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


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

int a, b;
int r;

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

    if (a > 0 && a < 10 && b > 0 && b < 10) {
        r = a * b;
    } else {
        r = -1;
    }
    cout << r << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

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

  int H, N;
  cin >> H >> N;

  int A[N];
  int B[N];

  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }

  long long costs[10000] = { 0 };

  long long best;
  for (int i = 1, j; i < H + 1; i++) {
    best = 1 << 30;
    for (int j = 0; j < N; j++) {
      best = min(best, B[j] + costs[max(0, i - A[j])]);
    }

    costs[i] = best;
  }

  cout << costs[H] << endl;
}

Measured memory_bytes: 384
n, k = [int(i) for i in input().split()]
a = [[0,0] for i in range(n)]
for i in range(n):
    a[i][0], a[i][1] = [int(i) for i in input().split()]
a = sorted(a)
sum = 0
for i in range(n):
    sum += a[i][1]
    if sum >= k:
        print(a[i][0])
        exit()

Measured memory_bytes: 20804
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
const int inf = 1 << 30;
const LL INF = 1LL << 60;
const int MaxN = 1e5;

char s[2 * MaxN + 5];
int cnt[30];
int sum;
LL ans;

int main()
{
	while(scanf("%s", s + 1) != EOF)
	{
		sum = 0;
		ans = 0;
		int len = strlen(s + 1);
		for(int i = 1; i <= len; i++) {
			cnt[s[i] - 'a' + 1]++;
		}
		for(int i = 1; i <= 26; i++)
			sum += cnt[i];
		for(int i = 1; i <= 26; i++) {
			if(cnt[i] > 0)
				ans += (LL)cnt[i] * (sum - cnt[i]);
		}
		printf("%lld\n", ans / 2 + 1);
		memset(s, 0, sizeof(s));
		memset(cnt, 0, sizeof(cnt));
	}
	return 0;
}

Measured memory_bytes: 384
#include<stdio.h>

int main(void)
{
	int i,j,A[100][100],n,m,l;
	int B[100][100];
	long sum,C[100][100];
	int k;
	sum =0;
	scanf("%d %d %d", &n, &m, &l);

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

  for (i=0;i<n;i++)
  {
		for (j=0;j<l;j++)
		{
			for(k=0;k<m;k++)
			{
				sum=sum+A[i][k]*B[k][j];
			}
			C[i][j]=sum;
			sum=0;
		}
  }
 
  for(i=0;i<n;i++)
  {
		for(j=0;j<l;j++)
		{
			printf("%ld", C[i][j]);
			if (j==l-1)
			{
				printf("\n");
				break;
			}
			printf(" ");
		}
  }
  return 0;
}

Measured memory_bytes: 760
from operator import itemgetter
import math
a=[i for i in input()]
b=[i for i in input()]
c=[i for i in input()]
d=[i for i in input()]
e=[i for i in input()]
nums = []
nums.append(a)
nums.append(b)
nums.append(c)
nums.append(d)
nums.append(e)
nums.sort(key=itemgetter(-1),reverse = True)
last = 0
for num in nums:
    if num[-1]!="0":
        last= num[-1]
ans = 0
for num in nums:
    ans+=math.ceil(int("".join(num))/10)*10
if last !=0:
    print(ans-(10-int(last)))
else:
    print(ans)

Measured memory_bytes: 3188
#include<bits/stdc++.h>
#define int long long
#define rep(i,m,n) for(int i=m;i<n;i++)
#define repr(i,n,m) for(int i=n;i>=m ;i--)
#define MOD 1000000007
#define float long double
#define pb push_back
#define mp make_pair
#define pp pop_back
#define pi 3.141592653589793238
#define print(v) for(int i=0;i<v.size();i++) cout<<v[i]<<" ";cout<<endl;
using namespace std;
bool sortbysec(const pair<int,int> &a,
             const pair<int,int> &b)
{
   return (a.second < b.second);
}
int lcm(int a, int b)  
 {  
    return (a*b)/__gcd(a, b);  
 }  
bool sortinrev(const pair<int,int> &a,  
              const pair<int,int> &b)
{
      return (a.first > b.first);
}
void solve()
{
	int n,d,total=0;
	cin>>n>>d;
	rep(i,0,n){
		int a,b;
		cin>>a>>b;
		if(sqrt(a*a+b*b)<=d)
			total++;
	}
	cout<<total;
}    
signed main()
{
                     // #ifndef ONLINE_JUDGE
                     // freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
                     // #endif 
  int t=1;
 //cin>>t;
  while(t--){
  solve();
  cout<<"\n";
}
  cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
 return 0;
}  


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

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

//マクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 //10^12
#define MOD 10000007 //10^9+7
#define PB push_back
#define MP make_pair
#define F first
#define S second

std::vector<std::vector<P>> tree; // treeのインデックスが節の番号 itemのP.F 子供のインデックス,　P.S　辺の番号
std::vector<int> ans;

void dfs(int parent, int now, int color) {
  int children_size = SIZE(tree[now]);
  int c = 1;
  REP(i,children_size) {
    if (tree[now][i].F == parent) continue;
    if (c == color) c++;
    ans[tree[now][i].S] = c;
    // std::cout << "parent " << parent+1 <<"\tnow " << now+1 << "\tchildren " << tree[now][i].F+1 << "\tcolor " << c <<  '\n';
    dfs(now,tree[now][i].F,c);
    c++;
  }
}

int main(int argc, char const *argv[]) {
  int n; std::cin >> n;
  tree.resize(n);
  ans.resize(n-1);
  REP(i,n-1) {
    int a,b; std::cin >> a >> b;
    a--; b--;
    tree[a].PB((P){b,i});
    tree[b].PB((P){a,i});
  }
  dfs(-1,0,-1);
  std::cout << MAX(ans) << '\n';
  REP(i,n-1) std::cout << ans[i] << '\n';

  return 0;
}


Measured memory_bytes: 8320
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
#include <bitset>

using namespace std;

int main()
{
    unsigned int x;
    cin >> x;

    bitset<32> bi(x);

    cout << bi << endl;
    cout << (~bi) << endl;
    cout << (bi << 1) << endl;
    cout << (bi >> 1) << endl;

}


Measured memory_bytes: 3160
#include<stdio.h>
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
int main(int argc, char const *argv[]){
	long i,n,a,num[2][2],s=0,p;
	scanf("%ld",&n);
	for(i=0;i<n;i++){
		scanf("%ld",&a);
		if(s==0){
			num[0][0]=a;
			num[0][1]=1;
			s=1;
		}else if(s==1){
			if(a==num[0][0]) num[0][1]++;
			else{
				num[1][0]=a;
				num[1][1]=1;
				s=2;
				if((num[0][0]-num[1][0])!=1 && (num[1][0]-num[0][0])!=1){
					printf("No\n");
					return 0;
				}
			}
		}else{
			if(a==num[0][0]) num[0][1]++;
			else if(a==num[1][0]) num[1][1]++;
			else{
				printf("No\n");
				return 0;
			}
		}
	}
	if(s==1){
		if(n/2<num[0][0] && num[0][0]!=(n-1)){
			printf("No\n");
			return 0;
		}
	}else{
		if(num[0][0]>num[1][0]) p=0;
		else p=1;
		if(!((num[1-p][1]+1)<=num[p][0] && num[p][0]<=(num[1-p][1]+num[p][1]/2))){
			printf("No\n");
			return 0;
		}
	}
	printf("Yes\n");
	return 0;
}

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

int main() {
    int N, M, C; cin >> N >> M >> C;
    vector<vector<int>> A(N, vector<int>(M));
    vector<int> B(M);
    for (int i = 0; i < M; i++) {
        cin >> B.at(i);
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> A.at(i).at(j);
        }
    }
    int cnt = 0, S;
    
    for (int i = 0; i < N; i++) {
        S = 0;
        for (int j = 0; j < M; j++) {
            S += A.at(i).at(j) * B.at(j);
        }
        //cout << S << endl;
        if (S > -C) {
            cnt++;
        }
    }
    
    cout << cnt << endl;
}

Measured memory_bytes: 256
#include <stdio.h>

int main() {
	int sm, m, ss=0, am=0, c=0;
	int av;
	while(scanf("%d,%d", &sm, &m)!=EOF) {
		ss += sm*m;
		am += m;
		c++;
	}
	if (c>0) av = ( (double)am / (double)c + 0.5);
	
	printf("%d\n%d\n",ss, av);
	return 0;
}

Measured memory_bytes: 0
/*
ID: wangjun30
TASK:
LANG: C++
*/
#include<iostream>
using namespace std;
int main(){
	int n,sm=-1,m=-1,pos=0;
	cin>>n;
	int num[n];
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		if(x>m){
			pos=i;
			sm=m;
			m=x;
		}
		else
			sm=max(sm,x);
	}
	for(int i=1;i<=n;i++){
		if(i==pos)
			cout<<sm<<endl;
		else
			cout<<m<<endl;
	}
	return 0;
}

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


int main() {
  int n , h , w ;
  cin >> n >> h >> w ;
  
  vector<int> a(n);
  vector<int> b(n);
  int c =0;
  for(int i=0; i<n ; i++ ){
    cin >> a.at(i);
    cin >> b.at(i);
  if(a.at(i)>=h&&b.at(i)>=w){
    c++;
  }
  }
  
  cout << c << endl;
}

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

int main(){
    string alp = "abcdefghijklmnopqrstuvwxyz";
    char C;cin >> C;
    int j;
    for (int i = 0; i < 26; i++)
    {
        if (alp.at(i) == C){
             j = i;
            break;
        }
    }
    cout << alp.at(j + 1) << endl;
}

Measured memory_bytes: 256
import java.util.*;

public class Main{
  public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    while(sc.hasNext()){
      int n=Integer.parseInt(sc.next());
      TreeMap<Integer,Integer>map=new TreeMap<>();
      for(int i=0;i<n;i++){
        map.put(Integer.parseInt(sc.next()),i+1);
      }
      for(int i=0;i<n;i++){
        System.out.print(map.get(i+1)+" ");
      }
      System.out.println("");
    }
    sc.close();
  }
}

Measured memory_bytes: 73248
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int N, M, a, b, ans = 0, c = 0;
	cin >> N >> M;
	vector<vector<int>> d(N);
	for (int i = 0; i < M; i++) {
		cin >> a >> b;
		d[b - 1].push_back(a - 1);
	}
	for (vector<int>& i : d) sort(i.begin(), i.end());
	for (int i = 1; i < N; i++) {
		if (d[i].empty()) continue;
		if (c <= d[i].back()) {
			ans++;
			c = i;
		}
	}
	cout << ans;
}

Measured memory_bytes: 4864