akhauriyash/Code-Regression huggingface dataset:
import itertools

N = int(input())

S = []
for _ in range(N):
    S.append(input())
    
M, A, R, C, H = [], [], [], [], []

march = ["M","A","R","C","H"]

for s in S:
    top = s[0]
    if top in march:
        if top == "M":
            M.append(s)
        elif top == "A":
            A.append(s)
        elif top == "R":
            R.append(s)
        elif top == "C":
            C.append(s)
        elif top == "H":
            H.append(s)

MARCH = [len(M), len(A), len(R), len(C), len(H)]

if MARCH.count(0) >= 3:
    print(0)

else:
    x = 0
    pattern = list(itertools.combinations(MARCH, 3))
    for i in pattern:
        x += i[0]*i[1]*i[2]
    print(x)
    

Measured memory_bytes: 10924
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <string>
#include <string.h>
#include <sstream>
#include <ctime>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll,ll>













const int N=1e5+10,M=1e9+7;
ll mn,mx,fct[N],res;


void solve()
{
	cin>>mn>>mx;
	if(mn>mx)swap(mn,mx);
	if(mx>mn+1)
	{
		cout<<"0\n";
		return;
	}
	fct[0]=1;
	for(int i=1;i<=mx;i++)fct[i]=(fct[i-1]*i)%M;
	res=(fct[mn]*fct[mx])%M;
	if(mn==mx)res=(res*2)%M;
	cout<<res<<"\n";
}

int main()
{
	std::ios::sync_with_stdio(0);
	cin.tie(0);
#ifdef localProject
	freopen("in.txt","r",stdin);
#endif
	solve();
	return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
typedef long long int uli;
const int oo=1e9;
int g[222][222];
int main(){
  int n,m,r;
  scanf("%d %d %d",&n,&m,&r);
  vector<int>p(r);
  for(int i=0;i<r;i++)
    scanf("%d",&p[i]),--p[i];
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      g[i][j]=oo;
  for(int i=0;i<m;i++){
    int u,v,c;
    scanf("%d %d %d",&u,&v,&c);
    --u,--v;
    g[u][v]=g[v][u]=c;
  }
  for(int i=0;i<n;i++)
    g[i][i]=0;
  for(int k=0;k<n;k++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
      }
    }
  }
  int ans=oo;
  sort(p.begin(),p.end());
  do{
    int bet=0;
    for(int i=1;i<r;i++)
      bet+=g[p[i-1]][p[i]];
    ans=min(ans,bet);
  }while(next_permutation(p.begin(),p.end()));
  printf("%d\n",ans);
  return 0;
}



Measured memory_bytes: 384
from collections import Counter
from itertools import product
n = int(input())
*v, = map(int, input().split())
o = Counter(v[1::2])
e = Counter(v[::2])
o[0] = 0
e[0] = 0
o_ls = o.most_common(2)
e_ls = e.most_common(2)


ans = n
for (k1, v1), (k2, v2) in product(o_ls, e_ls):
    if k1 == k2:
        continue
    x = n - v1 - v2
    if ans > x:
        ans = x

print(ans)

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

int main() {
	int r,d,x; cin >> r >> d >> x;
	int ans = x;
	rep(i,10) {
		ans=r*ans-d;
		cout << ans << endl;
	}
}

Measured memory_bytes: 256
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
#include<iomanip>
#include<set>
#include<utility>

const long long PRIME = 1000000007;
const double mpi = 3.14159265358979323846;

using namespace std;

int main() {
  int H, W, h, w;
  cin >> H >> W >> h >> w;
  if(H % h == 0 && W % w == 0) {
    cout << "No" << endl;
    return 0;
  }
  bool b = H % h == 0;
  if(b) {
    vector<int> v(W + 1), c(W);
    for(int i = w; i < W + 1; i += w) v[i] = v[i - w] - 1;
    v[W] = 1;
    for(int i = W - w; i >= 0; i -= w) v[i] = v[i + w] + 1;
    for(int i = 1; i <= w; i++) {
      for(int j = i + w; j < W + 1; j++) v[j] = v[j - w] - 1;
    }
    for(int i = 0; i < W; i++) c[i] = v[i + 1] - v[i];
    cout << "Yes" << endl;
    for(int i = 0; i < H; i++) {
      cout << c[0];
      for(int j = 1; j < W; j++) cout << " " << c[j];
      cout << endl;
    }
  } else {
    vector<int> v(H + 1), c(H);
    for(int i = h; i < H + 1; i += h) v[i] = v[i - h] - 1;
    v[H] = 1;
    for(int i = H - h; i >= 0; i -= h) v[i] = v[i + h] + 1;
    for(int i = 1; i <= h; i++) {
      for(int j = i + h; j < H + 1; j++) v[j] = v[j - h] - 1;
    }
    for(int i = 0; i < H; i++) c[i] = v[i + 1] - v[i];
    cout << "Yes" << endl;
    for(int i = 0; i < H; i++) {
      cout << c[i];
      for(int j = 1; j < W; j++) cout << " " << c[i];
      cout << endl;
    }
  }
  return 0;
}


Measured memory_bytes: 1408
a = input()
maxima=0
c=0
for i in range(0,len(a)):
	if(a[i:i+1]=="A" or a[i:i+1]=="T" or a[i:i+1]=="G" or a[i:i+1]=="C"):
		c=c+1
	else:
		if(c>maxima):
			maxima=c
			c=0
		else:
			c=0
if(c>maxima):
	maxima=c
print(maxima)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 2e9;
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

int main(){
    int n; cin >> n;
    vector<int> a(n);

    rep(i,n) cin >> a[i];

    sort(all(a));

    int tmp= 1;
    int ans = 0;

    rep(i,n-1){
        if(a[i] == a[i+1]) tmp++;

        else{

            if(a[i] > tmp){
                ans += tmp;
            }
            else{
                ans += tmp - a[i];
            }

            tmp = 1;
        }

    }

    if(tmp != 0){
        if(a[n-1] > tmp){
                ans += tmp;
            }
            else{
                ans += tmp - a[n-1];
            }
        
    }

    else{
        if(a[n-1] != 1) ans++;
    }

    cout << ans << endl;
}

Measured memory_bytes: 640
#include <stdio.h>
int main(){
  int i,H,A[251];
  scanf("%d\n",&H);
  for(i=1;i<=H;i++)scanf("%d",&A[i]);
  for(i=1;i<=H;i++){
    printf("node %d: key = %d, ",i,A[i]);
    if(i/2>=1)printf("parent key = %d, ",A[i/2]);
    if(i*2<=H)printf("left key = %d, ",A[i*2]);
    if(i*2+1<=H)printf("right key = %d, ",A[i*2+1]);
    printf("\n");
  }
  return 0;
}




Measured memory_bytes: 2048
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int max_n=100005;
int n,a[max_n],b[max_n],c[max_n];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    for(int i=1;i<=n;i++)scanf("%d",b+i);
    for(int i=1;i<=n;i++)scanf("%d",c+i);
    sort(a+1,a+n+1),sort(b+1,b+n+1),sort(c+1,c+n+1);
    ll ans=0;
    for(int i=1;i<=n;i++){
        int x=lower_bound(a+1,a+n+1,b[i])-a-1,y=upper_bound(c+1,c+n+1,b[i])-c;
        y=n-y+1;
        ans+=1ll*x*y;
    }
    printf("%lld\n",ans);
    return 0;
}

Measured memory_bytes: 1408
while 1:
	n,x=(int(a) for a in input().split())
	if n==0 and x==0:
		break
	count=0
	for i in range(1,n+1):
		for j in range(i+1,n+1):
			for k in range(j+1,n+1):
				z=i+j+k
				if z>x:
					break
				if z==x:
					count+=1
					z=0
	print(count)


Measured memory_bytes: 5604
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>



int main() {
    long long N;
    cin >> N;
    long long count = N+1;
    for(long long i = 1 ; i<= sqrt(N);i++){
        if(N%i == 0){
            if(count >= i + N/i){
                count = i + N/i;
            }
            else{
                break;
            }
        }
    }
    cout << count - 2 << endl;
} 

Measured memory_bytes: 256
#include  <iostream>
#include  <stdio.h>
#include <algorithm>
#include <map>
#include <math.h>

using namespace std;
#include <vector>
#define rep(i,n) for (ll i = 0; i < (n) ; i++)
#define INF 1e9
#define llINF 1e18
#define base10_4 10000      //1e4
#define base10_5 100000     //1e5
#define base10_6 1000000    //1e6
#define base10_7 10000000   //1e7
#define base10_8 100000000  //1e8
#define base10_9 1000000000 //1e9

#define MOD 1000000007
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

//#include <stack>
//#include <queue>

// #include <iomanip>
//  cout << fixed << setprecision(15) << y << endl;

string ans_Yes = "Yes"; 
string ans_No = "No"; 
string ans_yes = "yes"; 
string ans_no = "no"; 

ll A;
ll B;
ll C;
ll M;
ll H;
ll W;
vll h;
vll w;

ll ltmp;
string stmp;
double dtmp;
ll Hcnt[base10_6]={};
ll Wcnt[base10_6]={};

/*
max 448,000,000
map<string,ll> count_map;
count_map['0']=0;
for(auto x : count_map) {
    string key = x.first;
    ll value = x.second;
}
*/
map<string,ll> count_map;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> H;
    cin >> W;
    cin >> M;

    rep(mi,M){
        ll hi,wi;
        cin >> hi;
        hi--;
        h.push_back(hi);
        Hcnt[hi]++;
        cin >> wi;
        wi--;
        w.push_back(wi);
        Wcnt[wi]++;
        count_map[to_string(hi)+":"+to_string(wi)]=1;
    }
    ll maxHi=0;
    ll maxWi=0;
    rep(hi,H){
        if(Hcnt[hi]>Hcnt[maxHi]) maxHi = hi;
    }
    rep(wi,W){
        if(Wcnt[wi]>Wcnt[maxWi]) maxWi = wi;
    }

    vll mhi,mwi;
    rep(hi,H){
        if(Hcnt[hi]==Hcnt[maxHi]) mhi.push_back(hi);
    }
    rep(wi,W){
        if(Wcnt[wi]==Wcnt[maxWi]) mwi.push_back(wi);
    }

    ll hosei = 1;
    rep(h1,mhi.size()){
        rep(w1,mwi.size()){
            ll hi = mhi[h1];
            ll wi = mwi[w1];
            string key = to_string(hi)+":"+to_string(wi);
            ltmp = count_map[key];
            if(ltmp == 0){
                hosei = 0;
                break;
            }
        }
    }

    cout << Hcnt[maxHi] + Wcnt[maxWi] - hosei << endl;

    

}

Measured memory_bytes: 64264
/*
This code By @1353055672(Ligen)
[Warning]You're not excepted to understand this code!
RP++
*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#define ll long long
#define ull unsigned long long
#define mn 1000020
#define FOR(a,b,c) for (int a=b;a<=c;a++)
#define FORD(a,b,c) for (int a=b;a>=c;a--)
using namespace std;
inline ll read(){
    ll x=0,f=1;
    char c=getchar();
    while (c<'0'||c>'9'){
        if (c=='-')f=-1;
        c=getchar();
    }
    while (c>='0'&&c<='9'){
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
int Max(int a,int b){return (a>b)?a:b;}
int Min(int a,int b){return (a<b)?a:b;}
map<ll,ll>b;
ll sum[100020];
int main(){
	ll n=read(),m=read(),x,ans=0;
	b[0]=1;
	FOR(i,1,n){
		x=read();
		sum[i]=sum[i-1]+x;
		if(b[sum[i]%m]>=1)ans+=b[sum[i]%m];
		b[sum[i]%m]++;
	}
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 4992
N, K = map(int, input().split())
A = [int(a) for a in input().split()]

visited = [False] * N
loop = [0]
cur_town = 0
step = 0
while True:
    if visited[cur_town]:
        loop = loop[loop.index(cur_town):-1]
        step -= len(loop)
        print(loop[(K - step) % len(loop)] + 1)
        exit()
    else:
        visited[cur_town] = True
        cur_town = A[cur_town] - 1
        loop.append(cur_town)
        step += 1
        if step == K:
            print(cur_town + 1)
            exit()


Measured memory_bytes: 95040
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

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

    ll ma1 = -1001001001001001001, mi1 = 1001001001001001001;
    ll ma2 = -1001001001001001001, mi2 = 1001001001001001001;

    rep(i,n){
        ll x, y;
        cin >> x >> y;
        ma1 = max(ma1,x+y);
        mi1 = min(mi1,x+y);
        ma2 = max(ma2,x-y);
        mi2 = min(mi2,x-y);
    }

    ll ans = max(ma1-mi1, ma2-mi2);

    cout << ans << endl;
    return 0;
}




Measured memory_bytes: 3604
#include <vector>
#include <iostream>

using namespace std;

bool reachable[200001];

int main(int argc, char *argv[]) {
	int N, M;
	cin >> N >> M;

	vector<vector<int>> e(N+1);
	for (int i = 0; i < M; i++) {
		int a, b;
		cin >> a >> b;
		e[a].push_back(b);
		e[b].push_back(a);
	}

	reachable[1] = true;
	for (auto &i : e[1]) reachable[i] = true;

	for (int i = 0; i < e.size(); i++) {
		if (!reachable[i]) continue;
		for (auto &n : e[i]) {
			if (n == N) {
				cout << "POSSIBLE" << endl;
				return 0;
			}
		}
	}

	cout << "IMPOSSIBLE" << endl;

	return 0;
}

Measured memory_bytes: 12144
#include<cstdio>
#include<algorithm>
using namespace std;
int n,a,b;
int main(void){
	register int i;
	scanf("%d%d%d",&n,&a,&b);
	if(a<b)swap(a,b);
	if((a-b)&1)puts("Borys");
	else puts("Alice");
	return 0;
}

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

#define rep(i, a, b) for(int i = (a); i < int(b); ++i)
#define rrep(i, a, b) for(int i = (a) - 1; i >= int(b); --i)
#define trav(it, v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define all(v) (v).begin(), (v).end()
#define what_is(x) cerr << #x << " is " << x << endl;

typedef double fl;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;

int A[1000005];
ll sum[1000005];

int main(){
	int N;
	scanf("%d", &N);
	rep(i,0,N)
		scanf("%d", A+i);
	sort(A, A+N);
	sum[0]=0;
	rep(i,0,N)
		sum[i+1]=sum[i]+A[i];
	int ans=1;
	rrep(i,N,1){
		if(2*sum[i] >= A[i])
			++ans;
		else
			break;
	}
	cout << ans << endl;
}

Measured memory_bytes: 5120
# have to run on PyPy3
ABOVE,LEFT,UPPERLEFT=1,2,3
s = str(input())
t = str(input())
ls = len(s)
lt = len(t)
dp = [[0 for j in range(lt+2)] for i in range(ls+2)]
memo = [[0 for j in range(lt+2)] for i in range(ls+2)] #from ... ABOVE=1; LEFT=2; UPPERLEFT=3;
for i in range(ls+1):
    for j in range(lt+1):
        if i<ls and j<lt and s[i] == t[j]: #short-circuit
            dp[i+1][j+1] = dp[i][j] + 1
            memo[i+1][j+1] = UPPERLEFT
        if dp[i+1][j] < dp[i][j]:
            dp[i+1][j] = dp[i][j]
            memo[i+1][j] = ABOVE
        if dp[i][j+1] < dp[i][j]:
            dp[i][j+1] = dp[i][j]
            memo[i][j+1] = LEFT

result=""
row,column=ls,lt
while(memo[row][column]!=0):
    if memo[row][column] == ABOVE:
        row -= 1
    elif memo[row][column] == LEFT:
        column -= 1
    elif memo[row][column] == UPPERLEFT:
        result = s[row-1]+result
        row -= 1
        column -= 1
print(result)

Measured memory_bytes: 192392
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (1E9+7)
#define PI 3.1415926535897932384626433832795

std::ostream &operator<<(std::ostream &out, const vector<int> &tgt)
{
	string s;
	for (int i = 0; i<tgt.size(); i++) {
		s += (to_string(tgt[i]) + ((i != tgt.size() - 1) ? " " : ""));
	}
	out << s;
	return out;
}

vi v;
int n;

void maxHeapify(int i){
	i++;
	int l = i * 2;
	int r = i * 2 + 1;
	int largest;
	if (l <= n && v[l - 1]>v[i - 1]){
		largest = l;
	}
	else largest = i;
	if (r <= n && v[r - 1]>v[largest - 1]){
		largest = r;
	}
	if (largest != i) {
		swap(v[i-1], v[largest-1]);
		maxHeapify(largest - 1);
	}
}


int main() {
	int in;
	cin >> n;
	rep(i, n) {
		cin >> in;
		v.push_back(in);
	}
	for (int i = n / 2-1; i >= 0; i--) {
		maxHeapify(i);
	}
	for (auto x : v) {
		cout << " " << x;
	}
	cout << endl;


}


Measured memory_bytes: 4888
#![allow(unused_imports)]
use {
   proconio::{fastout, input, marker::*},
   std::cmp::*,
   std::collections::*,
};

#[fastout]
fn main() {
   input! {
      n: usize,
      m: usize,
      mut a: [usize; n]
   }

   a.sort();
   let mut high = a[n - 1] * 2 + 1;
   let mut low = 0;
   while high - low > 1 {
      let mid = (high + low) / 2;
      let mut k = 0;
      let mut c = 0;
      for i in (0..n).rev() {
         while k < n && a[i] + a[k] < mid {
            k += 1;
         }
         if k == n {
            break;
         }
         c += n - k;
      }
      if c >= m {
         low = mid;
      } else {
         high = mid;
      }
   }

   let mut b = a.clone();
   b.insert(0, 0);
   for i in 0..n {
      b[i + 1] += b[i];
   }
   let mut ans = 0;
   let mut k = 0;
   let mut c = 0;
   for i in (0..n).rev() {
      while k < n && a[i] + a[k] < low {
         k += 1;
      }
      if k == n {
         break;
      }
      c += n - k;
      ans += (n - k) * a[i];
      ans += b[n] - b[k];
   }
   ans -= low * (c - m);
   println!("{}", ans);
}


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

int main() {
	int A, B, C, K;
	cin>>A>>B>>C>>K;
    int S=0;
	int mx=max({A, B, C});
    S=A+B+C-mx;
    for(int i=0; i<K; i++){
	  mx =mx*2;
    }
    cout<<S+mx<<endl;
    return 0;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
using Graph=vector<vector<int>>;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
typedef long long ll;
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
ll power(ll A,ll B){
    if(B==0){
        return 1;
    }
    else if(B%2==0){
        long long s=power(A,B/2);
        return (s*s);
    }
    else{
        return ((A)*power(A,B-1));
    }
}

int main(){
    ll N,P;
    cin>>N>>P;
    vector<int>A(N);
    rep(i,N){
        cin>>A[i];
        A[i]=A[i]%2;
    }
    int N0=0;
    int N1=0;
    rep(i,N){
        if(A[i]==0){
            N0++;
        }
        else{
            N1++;
        }
    }
    ll P0=power(2,N0);
    //N0を0個以上選ぶ方法
    ll P1=power(2,N1);
    //N1を0個以上選ぶ方法
    ll P12=1;
    if(N1>1){
        vector<ll>comb(N1+1);
        comb.at(0)=1;
        for(ll i=1;i<=N1;i++){
            comb.at(i)=(N1-i+1)*comb.at(i-1)/i;
        }
        for(int i=1;i<=N1;i++){
            if(i%2==0){
                P12+=comb[i];
            }
        }
    }
    //N1を偶数個選ぶ方法
    //N1を奇数個選ぶ方法
    ll P11=P1-P12;
    if(P==0){
        cout<<P0*P12<<endl;
    }
    else{
        cout<<P0*P11<<endl;
    }
}

Measured memory_bytes: 256
#include <cstdio>
#include <bitset>
#define MAXN 2005
#define MAXS 4000005
using namespace std;
int a[MAXN];
bitset<MAXS> bs(0);
int read(void);
int main(void){
	int n;
	int l;
	int tlg,tmv;
	int s=0,tar;
	n=read();
	for (int i=0;i<n;i++){
	    a[i]=read();
	    s+=a[i];
	}
	tar=s>>1;
	tar+=(s&1);
	bs[0]=1;
	for (int i=0;i<n;i++)
		bs|=(bs<<a[i]);
	bs>>=tar;
	for (l=0;l<=tar;l++)
	    if (bs[l])
	        break;
    printf("%d\n",l+tar);
	return 0;
}

inline int read(void){
	int ans=0;
	char t;
	t=getchar();
	while (t<'0' || t>'9')
	    t=getchar();
	while (t>='0' && t<='9'){
		ans=ans*10+(t-'0');
		t=getchar();
	}
	return ans;
}


Measured memory_bytes: 1664
#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)

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

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

    string S;
    cin>>S;
    vector<ll> ctr(26);
    for (auto c : S) {
        ctr[c - 'a']++;
    }
    priority_queue<ll, vector<ll>, greater<ll>> q;
    rep(i, 26) {
        if (ctr[i] > 0) {
            q.push(ctr[i]);
        }
    }
    ll ans = 0;
    if (q.size() == 1) ans = S.size();
    while(q.size() >= 2) {
        ll n1 = q.top(); q.pop();
        ll n2 = q.top(); q.pop();
        ll n = n1 + n2;
        ans += n;
        q.push(n);
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 3356
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace B
{
    public class Program
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var N = int.Parse(Console.ReadLine());
            var d = new int[N];
            for (var i = 0; i < N; i++)
            {
                d[i] = int.Parse(Console.ReadLine());
            }
            Console.WriteLine(d.Distinct().Count());
        }
    }
}


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

using namespace std;

#define INF (1<<29)

int main()
{
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> a(N), b(M);
        int asum = 0, bsum = 0;
        for (int i = 0; i < N; i++) {
            cin >> a[i];
            asum += a[i];
        }
        for (int i = 0; i < M; i++) {
            cin >> b[i];
            bsum += b[i];
        }
        
        int p1 = -1, p2 = -1, p = INF;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                int nasum = asum - a[i] + b[j];
                int nbsum = bsum - b[j] + a[i];
                if (nasum == nbsum) {
                    if (a[i] + b[j] < p) {
                        p1 = a[i];
                        p2 = b[j];
                        p = a[i] + b[j];                        
                    }                    
                }
            }
        }
        if (p == INF) {
            cout << -1 << endl;
        } else {
            cout << p1 << " " << p2 << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 3048
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define f first
#define s second

typedef pair<int, int> P;
typedef pair<int, P> P2;

int main(){
  int n, m, a;
  
  while(cin >> n >> m >> a){
    if(n == 0 && m == 0 && a == 0) break;
    P2 amida[1001];
    
    for(int i = 0 ; i < m ; i++){
      cin >> amida[i].f >> amida[i].s.f >> amida[i].s.s;
    }
    sort(amida, amida+m, greater<P2>());
    
    for(int i = 0 ; i < m ; i++){
      if(a == amida[i].s.f) a = amida[i].s.s;
      else if(a == amida[i].s.s) a = amida[i].s.f;
    }
    cout << a << endl;
  }
  return 0;
}

Measured memory_bytes: 880
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define LOL cout<<'\n';
#define MOD 1000000007
#define sz(x) ((int)x.size())
#define INF LONG_LONG_MAX
#define MINF LONG_LONG_MIN

void solve(){
  int n; cin>>n; 
  int s=26;
  int l=1;
  while(n>s){
    n-=s;
    s*=26;
    l++;
  }
  string ans(l,'0');
  n--;
  for(int i=l-1 ; i>=0 ; i--){
    ans[i]=n%26+'a';
    n/=26;
  }
  cout<<ans;LOL
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  solve();
  return 0;
}


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

int main(){
  int x,a,b;
  cin >> x >> a >> b;
  if (b < a+1){
    cout << "delicious" << endl;
  }
  else if (b-a < x+1){
    cout << "safe" << endl;
  }
  else {
    cout << "dangerous" << endl;
  }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(),v.end()
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>=b;i--)
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define MOD 998244353 

using namespace std;

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

ll power(ll a, ll n, ll m)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=(ans*a)%m;
        n>>=1;
        a=(a*a)%m;
    }
    return ans;
}

ll invmod(ll a, ll m)
{
    return power(a,m-2,m);
}

int main()
{
    int n;
    cin>>n;
    int a[n],i;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    } 
    int dp[n];
    dp[0]=0;
    dp[1]=abs(a[0]-a[1]);
    for(i=2;i<n;i++)
    {
        dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
    }
    cout<<dp[n-1];

}

Measured memory_bytes: 1024
function Main(input) {
	var A = parseInt(input.split(" ")[0]);
  var B = parseInt(input.split(" ")[1]);
  var C = parseInt(input.split(" ")[2]);
  if(B - A == C - B){
     console.log("YES");
     }else{
      console.log("NO"); 
     }
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 7372
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

struct UnionFind {
  std::vector<int> parent;
  int _size;
  UnionFind(int size_) : parent(size_, -1), _size(size_) {}
  void unite(int x, int y) {
    if ( (x = root(x)) != (y = root(y)) ) {
      if (parent[y] < parent[x]) { std::swap(x, y); }
      parent[x] += parent[y];
      parent[y] = x;
      _size--;
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
  int size(int x) { return -parent[root(x)]; }
  int size() { return _size; }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind tree(n);

  vector<vector<int> > friends(n, vector<int>());
  vector<vector<int> > blocks(n, vector<int>());

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    friends[a].push_back(b);
    friends[b].push_back(a);
    tree.unite(a, b);
  }
  for (int i = 0; i < k; i++) {
    int c, d;
    cin >> c >> d;
    c--; d--;
    blocks[c].push_back(d);
    blocks[d].push_back(c);
  }

  for (int i = 0; i < n; i++) {
    long long ret = tree.size(i);
    set<int> t;
    for (int k = 0; k < friends[i].size(); k++) {
      // すでに友達の場合は友達候補ではない
      t.insert(friends[i][k]);
    }
    for (int k = 0; k < blocks[i].size(); k++) {
      if ( tree.same(blocks[i][k], i) ) {
        // ブロックされている人が連結成分に含まれていたらそいつは友達候補ではない
        t.insert(blocks[i][k]);
      }
    }
    ret -= (t.size() + 1);
    cout << ret << endl;
  }

  return 0;
}

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

using ll = long long int;
using P = std::pair<int, int>;

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

int main()
{
    ll x, k, d;

    std::cin >> x >> k >> d;

    x = std::abs(x);

    if (x / k >= d)
    {
        std::cout << std::abs((x - d * k)) << std::endl;
    }

    else
    {
        int m = k - x / d;

        if (m % 2)
            std::cout << d - x % d << std::endl;
        else
        {
            std::cout << x % d << std::endl;
        }
    }

    return 0;
}

Measured memory_bytes: 3620
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <bitset>
#include <functional>

using namespace std;

#define FOR(i,n) 		for(int(i)=0;(i)<(int)(n);(i)++)
#define FORN(i,a,b,c) 	for(int(i)=a;i<(int)(b);i+=(c))
#define RES(x,nilai)	memset(x,nilai,sizeof(x));
#define pb              push_back
#define mp              make_pair
#define INF				1000000000

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

inline string IS(int a){
    char x[100];
    sprintf(x,"%d",a); string s = x;
    return s;
}

inline int SI(string a){
    char x[100]; int res;
    strcpy(x,a.c_str()); sscanf(x,"%d",&res);
    return res;
}

//==============================================

bool cmp(int x,int y) {return x>y;}

double a,b, c, d, e, f, x,y;

int main() {
	//freopen("in.txt","r",stdin);
	//freopen("13.txt","w",stdout);
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	
	
    while(cin>>a>>b>>c>>d>>e>>f) {
        y = (a*f-c*d)/(a*e-d*b);
        x = (c-b*y)/a;
		cout << fixed << setprecision(3) << x << ' ' << y << endl;
    }
	
    return 0;
}

Measured memory_bytes: 3264
N, x = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
SUM = 0
f = 0
for i in range(N):
  SUM += A[i]
  if SUM > x:
    print(i)
    f = 1
    break
if f == 0:
  if SUM == x:
    print(N)
  else:
    print(N-1)

Measured memory_bytes: 61908
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vi in(m),s(n);
		rep(i,m)cin>>in[i];
		rep(i,n)cin>>s[i];
		vvi dp(n+1,vi(256,inf));
		dp[0][128]=0;
		rep(i,n)rep(j,256)if(dp[i][j]!=inf)rep(k,m){
			int I=i+1,J=j+in[k];
			if(J<0)J=0;
			if(J>255)J=255;
			dp[I][J]=min(dp[I][J],dp[i][j]+(s[i]-J)*(s[i]-J));
		}
		int out=inf;
		rep(i,256)out=min(out,dp[n][i]);
		cout<<out<<endl;
	}
}

Measured memory_bytes: 22000
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define rrep(i,n) for(int i=1;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int> c(n - 1), s(n - 1), f(n - 1);
	rep(i, n - 1) cin >> c[i] >> s[i] >> f[i];
	for (int num = 0; num < n - 1; ++num) {
		vector<int> now_train(n - 1);
		rep(i, n - 1) {
			now_train[i] = s[i];
		}
		int now;
		for (int i = num; i < n - 1; ++i) {
			if (i == num) {
				now = c[num] + s[num];
			}
			else {
				now = now_train[i] + c[i];
			}
			for (int j = i + 1; j < n - 1; ++j) {
				while (now_train[j] < now) now_train[j] += f[j];
			}
		}
		cout << now << endl;
	}
	cout << 0 << endl;
	return 0;
}


Measured memory_bytes: 3680
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = ReadInt();
            var a = Floor(Sqrt(n));
            WriteLine(a*a);
        }
        private static string Read() { return ReadLine(); }
        private static string[] Reads() { return (Read().Split()); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 13140
function main()
  
  S_0 = split(chomp(readline()),"")
  reverse!(S_0)
  S = join(S_0)
  
  check = 0
  ans = true
  N = length(S)
  for i in 1:N
    
    if i <= check
      continue
    end
    
    if i+6 <= N && S[i:i+6] == "remaerd"
      check = i+6
    elseif i+5 <= N && S[i:i+5] == "resare"
      check = i+5
    elseif i+4 <= N && S[i:i+4] == "maerd"
      check = i+4
    elseif i+4 <= N && S[i:i+4] == "esare"
      check = i+4
    else
      ans = false
    end
    
  end
  
  if ans
    println("YES")
  else
    println("NO")
  end
  
end

main()

Measured memory_bytes: 124556
package main

import (
	"fmt"
)

func isSame(s string) bool {
	for i := 1; i < len(s); i++ {
		if s[0] != s[i] {
			return false
		}
	}
	return true
}

func main() {
	var n string
	fmt.Scanln(&n)

	if isSame(n[1:]) || isSame(n[:3]) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


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

int main(){
  int count=0;
  string s;
  cin >> s;
  r(i,s.length())count+=s.at(i)-'0';
  if(count==1)cout << 10;
  else cout << count << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
ll a,b,c,k;
int main()
{
	cin>>a>>b>>c>>k;
	if(abs(a-b)>1e18) 
	{
		printf("Unfair");
		return 0;
	} 
		if(k&1) cout<<b-a;
		else cout<<a-b;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main() {
    int a, b;
    int sum = 0;
    cin >> a;
    cin >> b;

    if (a > b) {
        sum = a;
        a--;
        if (a > b) {
            sum += a;
        } else {
            sum += b;
        }
    } else {
        sum = b;
        b--;
        if (a > b) {
            sum += a;
        } else {
            sum += b;
        }
    }
    cout << sum << endl;

    return 0;
}

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

int main() {
	long n,m,p=0,q;
	cin>>n>>m;
	long d[m],a[m];
	vector<long>v={n+1};
	for(int i=0;i<m;i++)cin>>d[i]>>a[i];
	for(int i=0;i<m;i++){
		if(d[i]==1){
			v.push_back(a[i]);
		}
		else if(a[i]==n){
			v.push_back(d[i]);
		}
	}
	sort(v.begin(),v.end());
	q=v.size();
	if(q==1){
		cout<<"IMPOSSIBLE"<<endl;
	}
	else{
	for(int i=0;i<q-1;i++){
		if(v.at(i)==v.at(i+1)){
			p++;
		}
	}
	if(p==0){
		cout<<"IMPOSSIBLE"<<endl;
	}
	else{
		cout<<"POSSIBLE"<<endl;
	}
	}
}

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

int main() {
	int W;
	int H;
	int x;
	int y;
	int r;
	cin >> W;
	cin >> H;
	cin >> x;
	cin >> y;
	cin >> r;
	if (x + r <W + 1 && x - r>-1 && y + r<H + 1 && y - r>-1) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	     }
}


Measured memory_bytes: 3080
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;

#define rep(i,a) for(int i=0; i<a; i++)
#define rrep(i,a) for(int i=a; i>=0; i--)
#define loop3(i,j,k,a) for(int i=0; i<a; i++)for(int j=0; j<a; j++)if(i!=j)for(int k=0; k<a; k++)if(i!=k&&j!=k)
#define loop4(i,j,k,l,a) for(int i=0; i<a; i++)for(int j=0; j<a; j++)if(i!=j)for(int k=0; k<a; k++)if(i!=k&&j!=k)for(int l=0; l<a; l++)if(i!=l&&j!=l&&k!=l)
#define rep1(i,a) for(int i=1; i<=a; i++)

#define scnd1(a) scanf("%d", &a)
#define scnd2(a,b) scanf("%d%d", &a,&b)
#define scnd3(a,b,c) scanf("%d%d%d", &a,&b,&c)
#define scnd4(a,b,c,d) scanf("%d%d%d%d", &a,&b,&c,&d)

#define cin1(a) cin >> a;
#define cin2(a,b) cin >> a >> b;
#define cin3(a,b,c) cin >> a >> b >> c;
#define cin4(a,b,c,d) cin >> a >> b >> c >> d;
#define cout1(a) cout << a << endl;
#define cout2(a,b) cout << a << " " << b << endl;
#define cout3(a,b,c) cout << a << " " << b << " " << c << endl;
#define cout4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl;
#define prtd1(a) printf("%d\n", a)
#define prtd2(a,b) printf("%d %d\n", a,b)
#define prtd3(a,b,c) printf("%d %d %d\n", a,b,c)
#define prtd4(a,b,c,d) printf("%d %d %d %d\n", a,b,c,d)

#define mem(a,n) memset( a, n, sizeof(a))
typedef long long ll;
const int INF = 1e9;
const ll LLINF = 1e18;

int u_root( int a, int u_array[]) {
    if (u_array[a] == a) return a;
    u_array[a] = u_root(u_array[a], u_array);
    return u_array[a];
}

void u_connect( int a, int b, int u_array[]){
    int pa = u_root(a, u_array);  // 第一引数aの親
    int pb = u_root(b, u_array);  // 第二引数bの親
    
    if (pa==pb) return;         // 親が同じであれば何もしない
    else u_array[pb] = pa;      // 親が異なっていればaの親に統一する
}

bool u_same( int a, int b, int u_array[]) {
    return u_root(a, u_array)==u_root(b, u_array);
}

int main() {
    int N, K, L;
    scnd3(N,K,L);
    
    int u[N+5], t[N+5];
    
    rep1(i,N){
        u[i] = i;
        t[i] = i;
    }
    
    rep(i,K){
        int a,b; scnd2(a,b);
        u_connect(a,b,u);
    }
    
    rep(i,L){
        int a,b; scnd2(a,b);
        u_connect(a,b,t);
    }
    
    map<pair<int,int>,int> cnt;
    rep1(i,N){
        cnt[{u_root(i,u),u_root(i,t)}]++;
    }
    
    rep1(i,N){
        if(i>1) cout<<" ";
        cout<<cnt[make_pair(u_root(i,u),u_root(i,t))];
    }
    cout<<endl;
}

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

//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
//template <typename T> using orderMSet =
//tree <T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef unsigned long long llu;
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define inf 1000000000000000018
#define mod 1000000007
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

int main()
{
	ll L;
	scanf("%lld", &L);

	printf("%.10f\n", (double)(L*L*L)/27);
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{

    long long a,b,c,d,e,f,g,x,y;
    cin>>a;
    vector<long long>vc;
    for(long long i=0; i<a; i++)
    {
        cin>>c;
        vc.push_back(c);

    }
    long long bt=0;
      for(long long i=0; i<a-1; i++)
    {
        long long ct=0;
        for(long long j=i+1;vc[i]>=vc[j]&&i<a-1; j++)
        {

                ct++;
                i++;


        }
        if(ct>bt)
            bt=ct;


    }
    cout<<bt;




}


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


char s[15];

int main(){
	scanf("%s", s);
	int n = strlen(s);
	
	bool ok = false;
	if(n % 2 == 0){
		ok = true;
		for(int i = 0; i < n; i++){
			if(i % 2 == 0){
				if(s[i] != 'h'){
					ok = false;
					break;
				}
			}else{
				if(s[i] != 'i'){
					ok = false;
					break;
				}
			}
		}
	}
	
	if(ok)printf("Yes\n");
	else printf("No\n");
	
	return 0;
}


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

int main()
{
	int n;
	cin >> n;
	long long ans = 0;
	for(long long i = 1; i <= n; i++){
		ans += (n / i) * (2 * i + (n / i - 1) * i) / 2;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3608
#!python3

iim = lambda: map(int, input().rstrip().split())

from heapq import heappush, heappop

def calc(N, A, B, C, D):
    abc = [(3, B), (5, C), (2, A)]
    ans = N * D
    ans2 = min(2*D, D+A)

    dq = []

    heappush(dq, (0, N))
    seen = set()
    while dq:
        cost, rest = heappop(dq)

        if rest <= 2:
            if rest == 1:
                cost += D
            elif rest == 2:
                cost += ans2
            else:
                cost += -rest * D
            ans = min(ans, cost)
            continue

        if rest in seen:
            continue
        else:
            seen.add(rest)

        if cost >= ans:
            continue

        for r, co1 in abc:
            a1, a2 = divmod(rest, r)

            if a2 == 0:
                heappush(dq, (cost+co1, a1))
                continue

            if a2 > r - a2:
                a1 += 1
                a2 = r - a2

            cd = a2 * D
            if (rest - a1) * D < co1 + cd:
                ans = min(ans, rest*D + cost)
            else:
                co2 = cost + co1
                heappush(dq, (co2 + cd, a1))

        if a2 == 1:
            heappush(dq, (cost+A+D, a1+1))


    return ans


def resolve():
    T = int(input())
    S = [list(iim()) for i in range(T)]


    ans = []
    for s in S:
        ans.append(calc(*s))

    print(*ans, sep="\n")

if __name__ == "__main__":
    resolve()


Measured memory_bytes: 10496
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
typedef vector<int> Bector;
//---------------------------------------
double p(Bector x, Bector y, int p); 
double p_infinity(Bector x, Bector y);  //?????§????????§????????¢
//---------------------------------------

int main(void)
{
	cout << fixed <<setprecision(5);
	
	int n;  //n?¬????
	Bector x;
	Bector y;
	
	cin >> n;
	
	//????????????x???????´????????´?
	for(int i = 0; i < n; i++){
		int youso;
		cin >> youso;
		
		x.push_back(youso);
	}
	
	//????????????y???????´????????´?
	for(int i = 0; i < n; i++){
		int youso;
		cin >> youso;
		
		y.push_back(youso);
		
	}
	
	cout << p(x, y, 1) << endl
	     << p(x, y, 2) << endl
	     << p(x, y, 3) << endl
	     << p_infinity(x, y) << endl;
	     
	return 0;
	    
}

//---------------------------------------
double p(Bector x, Bector y, int p)
{
	long long int D = 0;
	
	for(int i = 0; i < x.size(); i++){
		D += abs(pow(x[i] - y[i], p));
	}
	
	return pow(D, 1.0/p);
}
//---------------------------------------
double p_infinity(Bector x, Bector y)  //?????§????????§????????¢
{
	int max = abs(x[0] - y[0]);
	
	for(int i = 1; i < x.size(); i++){
		if( abs(x[i] - y[i]) > max){
			max = abs(x[i] - y[i]);
		}
	}
	
	return max;
}

Measured memory_bytes: 3440
#include <iostream>
#include <bitset>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>

using namespace std;

int main()
{
	long long n,count=0,max=1;
	cin >> n;

	vector<string> s(n),ans2;
	vector<pair<string, long long>> ans1;

	for (long long i = 0; i < s.size(); i++)
		cin >> s[i];

	sort(s.begin(), s.end());

	ans1.push_back({s[0], 1});

	for (long long i = 1; i < s.size(); i++)
	{
		if (s[i - 1] == s[i])
		{
			ans1[count].second++;
			if (max < ans1[count].second)
				max = ans1[count].second;
		}
		else
		{
			ans1.push_back({ s[i],1 });
			count++;
		}
	}

	for (long long i = 0; i < ans1.size(); i++)
		if (ans1[i].second == max)
			ans2.push_back(ans1[i].first);
	
	for(long long i = 0;i < ans2.size();i++)
		cout << ans2[i] << endl;
}


Measured memory_bytes: 21744
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    scanf("%d",&n);
    long long ans = 0;
    for (int i = 1; i <= n; i++){
        if (i % 3  ==0 || i % 5 == 0) continue;
        else ans += i;
    }
    printf("%lld",ans);
}


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

#define FOR(i,l,r) for(int i = (l);i < (r);i++)
#define ALL(x) (x).begin(),(x).end()
template<typename T> bool chmax(T& a,const T& b){return a < b ? (a = b,true) : false;}
template<typename T> bool chmin(T& a,const T& b){return b < a ? (a = b,true) : false;}
typedef long long ll;

int N;

int main()
{
	while(~scanf("%d",&N)){
		int ans = 0;
		FOR(i,0,10) FOR(j,0,10) FOR(k,0,10) FOR(l,0,10){
			if(i + j + k + l == N) ans++;
		}
		printf("%d\n",ans);
	}

	return 0;
}

Measured memory_bytes: 3152
import java.util.*;

class Main {
        public static void main(String args[]) {
                try (Scanner sc = new Scanner(System.in)) {
                        int X = sc.nextInt();
                        long ans = 2;
                        for (int i = X; i > 0; i--) {
                                ans = ans * 2 + 2;
                        }
                        System.out.println(ans);
                }
        }
}

Measured memory_bytes: 9808
#include<stdio.h>

#define MAX 100000

int parent( int i ) { return i / 2; }
int left( int i ){ return 2 * i; }
int right( int i ){ return 2 * i + 1; }

int main( void ){
	
	int H,i,A[MAX+1];
	
	scanf( "%d" , &H );
	
	for( i = 1; i <= H; i++ ){
		scanf( "%d" , &A[i] );
	}
	
	for( i = 1; i <= H; i++ ){
		printf( "node %d: key = %d, " , i , A[i] );
		if( parent(i) >= 1 ){ printf( "parent key = %d, " , A[parent(i)] ); }
		if( left(i) <= H ){ printf( "left key = %d, " , A[left(i)] ); }
		if( right(i) <= H ){ printf( "right key = %d, " , A[right(i)] ); }
		printf( "\n" );
	}
			
	return 0;

}



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

int main() {
  int N, M;
  cin >> N >> M;
  int T = 1900 * M + 100 * (N - M);
  int ans = T;
  for (int i = 0; i < M; i++) ans *= 2;
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;
    //cin>>t;
    while(t--)
    {
    	int a,b,c,k;
    	cin>>a>>b>>c>>k;

    	if(k<=a)
    		cout<<k<<"\n";
    	else if(k<=a+b)
    		cout<<a<<"\n";
    	else
    		cout<<2*a+b-k<<"\n";


    	
    }


	return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define LLI long long int
#define FOR(v, a, b) for(LLI v = (a); v < (b); ++v)
#define FORE(v, a, b) for(LLI v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(LLI v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define fst first
#define snd second
#define popcount __builtin_popcount
#define UNIQ(v) (v).erase(unique(ALL(v)), (v).end())
#define bit(i) (1LL<<(i))

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(...) ((void)0)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;}

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}

template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);}
template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}

LLI power(LLI n, LLI p, LLI m){
  LLI ret = 1;
  while(p>0){
    if(p&1) (ret *= n) %= m;
    (n *= n) %= m;
    p /= 2;
  }
  return ret;
}

LLI mod_inv(LLI n, LLI p){return power(n,p-2,p);}


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

  LLI p;
  while(cin >> p){
    vector<LLI> a(p); cin >> a;

    vector<LLI> fac(p), ifac(p);
    fac[0] = 1;
    FOR(i,1,p){
      fac[i] = fac[i-1] * i % p;
    }
    REP(i,p) ifac[i] = mod_inv(fac[i],p);

    vector<vector<LLI>> power_table(p+1, vector<LLI>(p+1, 1));
    FORE(i,0,p){
      FORE(j,1,p){
        power_table[i][j] = power_table[i][j-1] * i % p;
      }
    }
    

    vector<LLI> ans(p);

    REP(i,p){
      if(a[i]){
        (ans[0] += 1) %= p;

        REP(j,p){
          LLI c = fac[p-1] * ifac[j] % p * ifac[p-1-j] % p;
          LLI t = c * power_table[i][p-1-j] % p;

          if((p-1-j)%2==0) t = (-t+p) % p;

          (ans[j] += t) %= p;
        }
      }
    }

    join(cout, ALL(ans));
  }
  
  return 0;
}


Measured memory_bytes: 70784
a,b=map(int,input().split())
s=[]
dels=[]
for i in range(a):
    s.append(input())
for i in range(len(s)):
    f=1
    for j in range(b):
        if s[i][j]!='.':
            f=0
            break
    if f:
        dels.append(i)
k=0
for i in dels:
    del s[i-k]
    k=k+1
dels=[]
for i in range(b):
    f=1
    for j in range(len(s)):
        if s[j][i]!='.':
            f=0
            break
    if f:
        dels.append(i)



for i in dels:
    for j in range(len(s)):
        s[j]=s[j][:i]+' '+s[j][i+1:]

for i in s:
    for j in i:
        if j!=' ':
            print(j,end='')
    print()

Measured memory_bytes: 4468
#include<iostream>
#include<string>
using namespace std;
string s;
int main(){
	cin>>s;
	for(int i=0;i<s.length();i++){
		if(s[i]=='C'){
			for(int j=i;j<s.length();j++){
				if(s[j]=='F'){
					cout<<"Yes";
					return 0;
				}
			}
		}
	}
	cout<<"No";
	return 0;
}

Measured memory_bytes: 256