akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr = ds.begin(); itr != ds.end(); ++itr)
#define mp make_pair
#define INF 2000000000
#define INFL 2000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge> > pqlE;


int main() {
	int N, W;
	cin >> N >> W;
	vll w(N), v(N);
	vvi wv(4);
	ll w0;
	rep(i, N) {
		cin >> w[i] >> v[i];
		if (i == 0) {
			w0 = w[0];
			wv[0].push_back(v[0]);
		}
		else {
			wv[w[i] - w0].push_back(v[i]);
		}
	}

	rep(i, 4) {
		sort(wv[i].begin(), wv[i].end());
		reverse(wv[i].begin(), wv[i].end());
		/*
		rep(j, wv[i].size()) {
			cout << wv[i][j] << ' ';
		}
		cout << endl;
		*/
	}

	vvll sum(4);
	rep(i, 4) {
		sum[i] = vll(wv[i].size() + 1);
		sum[i][0] = 0;
		rep(j, wv[i].size()) {
			sum[i][j + 1] = sum[i][j] + wv[i][j];
		}
	}

	ll ans = 0;
	rep(i0, wv[0].size() + 1) {
		rep(i1, wv[1].size() + 1) {
			rep(i2, wv[2].size() + 1) {
				rep(i3, wv[3].size() + 1) {
					if (w0*i0 + (w0 + 1)*i1 + (w0 + 2)*i2 + (w0 + 3)*i3 <= W) {
						ans = max(ans, sum[0][i0] + sum[1][i1] + sum[2][i2] + sum[3][i3]);
					}
				}
			}
		}
	}
	cout << ans << endl;

	return 0;
}

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

vector<int> sieveOfEratosthenes(int n) {
  vector<int> primes(n);
  for (int i = 2; i < n; ++i) primes[i] = i;
  for (int i = 2; i * i < n; ++i) {
    if (primes[i]) {
      for (int j = i * i; j < n; j += i) primes[j] = 0;
    }
  }
  primes.erase(remove(begin(primes), end(primes), 0), end(primes));
  return primes;
}

int main() {
  int m, a, b;
  auto primes = sieveOfEratosthenes(10001);
  while (cin >> m >> a >> b, m) {
    pair<int, int> ans;
    int mx = 0;
    for (auto p : primes) {
      for (auto q : primes) {
        if (p > q) continue;
        if (p * q > m) continue;
        if (a * q > b * p) continue;
        if (mx < p * q) {
          mx = p * q;
          ans = {p, q};
        }
      }
    }
    cout << ans.first << ' ' << ans.second << endl;
  }
}



Measured memory_bytes: 3124
#include <cstdio>

int main()
{
  char s[100001];
  scanf("%s", s);
  int ans = 0;
  char last = *s;
  for (char *p = s + 1; *p; ++p)
  {
    if (last != *p)
    {
      ++ans;
      last = *p;
    }
  }
  printf("%d\n", ans);
  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<ll> Vector;
typedef vector<Vector> DVector;

#define fi          first
#define se          second
#define pb          push_back
#define INF         INT_MAX
#define bcnt        __builtin_popcount
#define all(x)      (x).begin(),(x).end()
#define dbg(x)      cout<<#x"="<<x<<endl
#define uni(x)      x.erase(unique(all(x)),x.end())
#define ub(x,y)     upper_bound(all(x),y)-x.begin()
#define lb(x,y)     lower_bound(all(x),y)-x.begin()
#define rep(i,n)    repl(i,0,n-1)
#define usort(x)    sort(all(x))
#define dsort(x)    sort(all(x),greater<ll>())
#define mkp(x,y)    make_pair(x,y)
#define mmax(x,y)   (x>y?x:y)
#define mmin(x,y)   (x<y?x:y)
#define maxch(x,y)  x=mmax(x,y)
#define minch(x,y)  x=mmin(x,y)
#define exist(x,y)  (find(all(x),y)!=x.end())
#define each(itr,v) for(auto itr:v)
#define repl(i,a,b) for(ll i=(ll)(a);i<=(ll)(b);i++)

int main(){
  cin.sync_with_stdio(false);
  ll n,k;
  cin >> n >> k;
  Vector x(n),y(n);
  Vector xary,yary;

  rep(i,n){
      cin >> x[i] >> y[i];
      xary.pb(x[i]);
      yary.pb(y[i]);
  }
  usort(xary);
  usort(yary);

  ll ans = 1LL * (xary[n-1] - xary[0]) * (yary[n-1] - yary[0]);
  rep(xi,n)repl(xj,xi+1,n-1)rep(yi,n)repl(yj,yi+1,n-1){
    ll num = 0;
    ll lx = xary[xi],rx = xary[xj];
    ll by = yary[yi],uy = yary[yj];

    rep(i,n)if(lx <= x[i] && x[i] <= rx && by <= y[i] && y[i] <= uy)num++;
    if(num >= k)ans = mmin(ans, 1LL * (rx - lx) * (uy - by));
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int  long long int
#define mod 1000000000000007
#define pb push_back
#define mp make_pair
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define gcd __gcd
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define all(i) i.begin(),i.end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define set_bit __builtin_popcount //gives number of set_bits
#define ff first
#define ss second
#define cases(t) int t;cin>>t; rep(casn,1,t+1)
#define el cout<<"\n";
#define deci(n) int n; scanf("%lld",&n);
#define decs(n) string n; cin>>n;
#define sz(v) v.size()
#define repit(m) for(auto it=m.begin();it!=m.end();it++)
// #define find(m,i) (m.find(i)!=m.end())
// #define find(m,i) (m.find(i)==m.end())
#define cYES cout<<"YES\n";
#define cNO cout<<"NO\n";
#define ins insert
#define imax 1000000000000000000
#define imin -1000000000000000000
// #define NP 50000
//  cerr << "\nTime : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
 
//  IIITA
//SUNEET KUMAR -->  ਸੁਨੀਤ
 
// scanning numbers extremely fast/////////
void fs(int &number)
{
    bool negative = false;
    register int c;
    number = 0;
    c = getchar();
    if (c=='-')
    {
        negative = true;
        c = getchar();
    }
    for (; (c>47 && c<58); c=getchar())
        number = number *10 + c - 48;
    if (negative)
        number *= -1;
}
// ========================================
int power(int a,int x)
{
        a%=mod;
    int res=1;
    while(x>0)
    {
        if(x&1)
        res=res*a%mod;
        a=a*a%mod;
        x>>=1;
    }
    return res;
}
int max(int a,int b)
{
    if(a>b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
int min(int a,int b)
{
    if(a>b)
    {
        return b;
    }
    else
    {
        return a;
    }
}
int madd(int a,int b)
{
    int ans;
    ans=(a%mod+b%mod)%mod;
    return ans;
}
int mmult(int a,int b)
{
    int ans=1;
    ans=((a%mod)*(b%mod))%mod;
    return ans%mod;
}
int modinverse(int a)
{
    return power(a,mod-2);
}
 
void nhi_degi()
{
    int k,a,b;cin>>k>>a>>b;
    int flag=0;
    for(int i=a;i<=b;i++)
    {
        if(i%k==0)
        {
            flag=1;
            break;
        }
    }
    if(flag)
    {
        cout<<"OK\n";
    }
    else
    {
        cout<<"NG\n";
    }
}
signed main()
{
    // cases(t)
    // {
        nhi_degi();
    // }
}

Measured memory_bytes: 3600
import bisect

N = int(input())
L = list(map(int,input().split()))
count = 0
L.sort() # 昇順
for i in range(N):
    for j in range(i+1,N):
        ind = bisect.bisect_left(L,L[i]+L[j])
        count += ind-j-1

print(count)

Measured memory_bytes: 3188
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using namespace std;

int main()
{
	int r, c, k;
	cin >> r >> c >> k;
	vector<vector<int>> data(r, vector<int>(c, 0));
	for (int i = 0; i < k; i++)
	{
		int temp_r, temp_c, temp_v;
		cin >> temp_r >> temp_c >> temp_v;
		data[temp_r - 1][temp_c - 1] = temp_v;
	}

	vector<vector<array<long long, 4>>> values(r,
		vector<array<long long , 4>>(c, { 0, 0, 0, 0 }));
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
		{
			if (i == 0 && j == 0)
				values[0][0] = { 0, data[0][0], 0, 0 };
			else if (j == 0)
				values[i][j] = { values[i - 1][j][1], values[i - 1][j][1] + data[i][j], 0, 0 };
			else
			{
				array<long long, 4> neighbourLeft = values[i][j - 1];
				long long neighbourUp = 0;
				if (i != 0)
					neighbourUp = *max_element(values[i - 1][j].begin(),
						values[i - 1][j].end());
				long long cur = data[i][j];

				values[i][j] = {
					max(neighbourUp, neighbourLeft[0]),
					max({neighbourUp + cur, neighbourLeft[0] + cur, neighbourLeft[1]}),
					0, 0
				};
				if (neighbourLeft[1] != 0)
					values[i][j][2] = max(neighbourLeft[1] + cur, neighbourLeft[2]);
				if (neighbourLeft[2] != 0)
					values[i][j][3] = max(neighbourLeft[2] + cur, neighbourLeft[3]);
			}
		}

	cout << *max_element(values.back().back().begin(),
		values.back().back().end()) << endl;

	return 0;
}

Measured memory_bytes: 319864
if __name__ == '__main__':

    w, h, n = map(int, input().split())
    x = y = 0
    xx = w
    yy = h
    for i in range(n):
        xi, yi, a = map(int, input().split())
        if a == 1:
            x = max(x, xi)
        elif a == 2:
            xx = min(xx, xi)
        elif a == 3:
            y = max(y, yi)
        elif a == 4:
            yy = min(yy, yi)
    print(0 if x >= xx or y >= yy else (xx - x) * (yy - y))


Measured memory_bytes: 3064
#include<stdio.h>

int n=0;
int count=0;

int main(){
	while(scanf("%d",&n)!=EOF){
		count=0;
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				for(int k=0;k<10;k++){
					for(int l=0;l<10;l++){
						if(i+j+k+l==n) count++;
					}
				}
			}
		}
		printf("%d\n",count);
	}
	

	return 0;
}

Measured memory_bytes: 2612
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef double D;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
 
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d %d",&x,&y)
#define rl(x) scanf("%lld",&x)
#define rl2(x,y) scanf("%lld %lld",&x,&y)
#define wd(x) printf("%d" ,x)
#define wd2(x,y) printf("%d %d",x,y)
#define wl(x) printf("%lld",x)
#define wl2(x,y) printf("%lld %lld",x,y)
#define PC(x) putchar(x)
#define GC() getchar()
#define NL printf("\n")
#define SP printf(" ")
 
#define F first
#define S second
#define MP make_pair
#define PB push_back
 
#define fr(i,s,e) for(int i=s;i<e;i++)
#define frr(i,s,e) for(int i=s-1;i>=e;i--)
#define frv(i,a) for(int i = 0;i<(int)a.size();i++)
#define frvr(i,a) for(int i = a.size()-1;i>=0;i--)
#define tr(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end();i++)
int main(){
	int ch,n;
	rd2(ch,n);
	map<int,int>mp;
	fr(i,0,n){
		int x;
		rd(x);
		mp[x]=1;
	}	
	int flag=1;
	while(flag){
		int x=ch;
		int fg=1;
		while(x){
			if(mp.count(x%10))
				fg=0;
			x=x/10;
		}
		if(fg){
			wd(ch);
			flag=0;
		}
		ch++;
	}
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int h,n;
  cin>>h>>n;
  int p=0;
  for(int i=0;i<n;i++){
    int a;
    cin>>a;
    p+=a;
  }
  cout<<(p>=h?"Yes":"No")<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<math.h>
#define LL long long
#define FIO ios_base::sync_with_stdio(false), cin.tie(0)
using namespace std;

int main()
{
	int s, w; cin>>s>>w;
	if(s>w) cout<<"safe\n";
	else cout<<"unsafe\n";
}

Measured memory_bytes: 3600
#include <bits/stdc++.h>
using namespace std;
#define rep(i,ini,n) for(int i=ini;i<n;i++)
#define _rep(i,ini,n) for(int i=ini;i>=n;i--)
#define ToEnd(a) a.begin(),a.end()
uint64_t MOD=1000000007;

int main(){
    string s;
    cin>>s;

    int maxLen=0,len=0;
    for(auto c:s){
        if(c=='A' || c=='C' || c=='G' || c=='T') len++;
        else{
                maxLen=max(maxLen,len);
                len=0;
        }
    }
    maxLen=max(maxLen,len);

    cout<<maxLen<<endl;
}

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

    int mi=100;
    for(int long long i=1; i<=sqrt(n); i++){
        int count = 0;
        if(n%i==0){
            int long long x = max(i,n/i);
            while(x){
                x/=10;
                count++;
            }
            mi = min(mi,count);
            if(mi<count){
                break;
            }
        }
    }
    cout << mi << endl;
    return 0;
}

Measured memory_bytes: 256
def resolve():
    '''
    code here
    '''
    import collections
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]
    order_dict = dict(zip(range(1,N+1),A_list))

    foot_print = [-1 for _ in range(N+1)]
    loop_que = collections.deque([1])

    goto = 1

    while foot_print[goto] == -1:
        foot_print[goto] = 1
        goto = order_dict[goto]
        loop_que.append(goto) 

    loop_start = loop_que.index(goto) + 1
    loop_len = len(loop_que) - loop_start

    # print(loop_que)
    # print(loop_start, loop_len)


    data_list = list(loop_que)
    if K >= len(loop_que):
        temp_K = K - loop_start + 1
        temp_K %= loop_len
        temp_loop = data_list[loop_start-1:-1]
        print(temp_loop[temp_K])
    else:

        print(data_list[K])

if __name__ == "__main__":
    resolve()


Measured memory_bytes: 40916
/*
How to lambda
  ex) sort(ALL(cont),[](dat a,dat b){return a.l<b.l;});
lower_bound/upper_bound
  1 2 2 2 3 3 
    ^lb   ^ub */
#include <bits/stdc++.h>
#define BR "\n"
#define REP(i,n) for(int (i)=0;(i)<(n);++(i))
#define ALL(cont) begin(cont),end(cont)
#define AS_MOD(a,b) ((((a) % (b) ) + (b)) % (b))
#define FEACH(it,cont) for(auto (it) = begin(cont); it!=end(cont);++it)
#define FEACHR(it,cont) for(auto (it) = rbegin(cont); it!=rend(cont);++it)
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define getll() ([](){ll s;scanf("%lld", &s);return s;})()
#define getld() ([](){ld s;scanf("%Lf", &s);return s;})()
#define prl(P) printf("%lld", P)
#define prd(P) printf("%.10Lf", P)
#define pr(P) printf(P)
#define assign_if_greater(V, T) V=max(V,T)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
int N;
vector<ll> X,Y;
bool even=false;
int main() {
  cout << fixed << setprecision(15);
  cin >> N;
  X.resize(N);Y.resize(N);
  REP(i,N)
    cin >> X[i] >> Y[i];
  if ((X[0]+Y[0])%2==0)
    even=true;
  if (even)
    REP(i,N)
      X[i]+=1;
  REP(i,N)
    if ((X[i]+Y[i])%2==0) {
      cout << -1 << endl;
      exit(0);
    }
  if (even)
    cout << 40 << endl << 1 << " ";
  else
    cout << 39 << endl;
  REP(i,39)
    cout << (1ll<<i) << " ";
  cout << endl;
  REP(i,N) {
    if (even)
      cout << 'L';
    ll U = X[i]+Y[i],V=X[i]-Y[i];

    ll S=((1ll<<39)-1 + U)/2;
    ll T=((1ll<<39)-1 + V)/2;
    REP(j,39) {
      int s=(S>>j)&1,t=(T>>j)&1;
      char r[]={'L','D','U','R'};
      cout << r[s*2+t];
    }
    cout << endl;
  }
  return 0;
}


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

int main(void)
{
  long long N, A, B;
  cin >> N >> A >> B;

  long long result = N / (A + B) * A;
  long long rem = N % (A + B);
  result += min(rem, A);
  cout << result << endl;
  return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
int main(){
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<double> x(N), y(N);
  for (int i = 0; i < N; i++){
    cin >> x[i] >> y[i];
  }
  vector<int> p(N);
  for (int i = 0; i < N; i++){
    p[i] = i;
  }
  double sum = 0;
  int count = 0;
  while (1){
    for (int i = 0; i < N - 1; i++){
      sum += sqrt(pow(x[p[i + 1]] - x[p[i]], 2) + pow(y[p[i + 1]] - y[p[i]], 2));
    }
    count++;
    if (!next_permutation(p.begin(), p.end())){
      break;
    }
  }
  cout << sum / count << endl;
}

Measured memory_bytes: 256
//070 C - Multiple Clocks 2017/08/12
#include <bits/stdc++.h>
#define rep(i ,n) for(int i=0;i<(int)(n);++i)
using namespace std;
typedef long long int int64;
typedef unsigned long long uint64;

int64 gcd(int64 a, int64 b) {
    
    if(a < b) swap(a , b);
    return b ? gcd(b, a % b) : a;
}

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


int main(){
    int n; cin >> n;
    int64 t;
    int64 ans=1;
    rep(i ,n) {
        cin >> t;
        ans = lcm(t , ans);
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <stdio.h>
int main()
{
    int A, B, C, D;
    scanf("%d", &A);
    scanf("%d", &B);
    scanf("%d", &C);
    scanf("%d", &D);

    int sum = 0;
    if (A > B) sum += B;
    else       sum += A;

    if (C > D) sum += D;
    else       sum += C;

    printf("%d\n", sum);
}

Measured memory_bytes: 128
#include <iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<iomanip>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
const int INF = 1e9;  vector<int> t; vector<int>w;
vector<vector<int>> v(10, vector<int>(10));long long int dp[100011]; int a[100010];
long long int h[100010]; long double n, m, o;
int main() {
	cin >> n;
	rep(i, n) {
		rep(j, 2) cin >> v[i][j];
	}
	long double  q = 0;
	rep(i, n) {
		rep(j, n) {
			if (i == j)
				continue;
			m = v[i][0] - v[j][0]; o = v[i][1] - v[j][1];
			long double g = pow(m, 2);long double h = pow(o, 2);
			m = sqrt(g + h);
			q += m;
		}
	}
	q  /= n;
	cout << fixed << setprecision(10) << q << endl;
}

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

int main() {
  int n;
  cin >>n;
  
  int yakusuu_8_kosuu = 0;
  
  for (int i=1; i<n+1; i+=2) {
    int yakusuu_kosuu = 1;
    for (int j=1; j<(n+2)/2; j+=2) {
      if (i%j==0) ++yakusuu_kosuu;
    }
    if (yakusuu_kosuu == 8) ++yakusuu_8_kosuu;
  }
  
  cout << yakusuu_8_kosuu << endl;
}

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

int main(){
	int r, d, sd=90;
	double x=0.0, y=0.0;
	while( scanf("%d,%d",&r,&d)==2 && !(r==0 && d==0) ){
		x += r * cos(sd*3.141592/180.0);
		y += r * sin(sd*3.141592/180.0);
		sd -= d;
		if( sd>180 )			sd -= 360;
		else if( sd<-180 )	sd += 360;
	}
	printf("%d\n%d\n", (int)x, (int)y);
	return 0;
}

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

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


Measured memory_bytes: 3472
import java.util.*;

public class Main{
    public static void main( String[] args) {
        Scanner sc = new Scanner(System.in);
         int n = sc.nextInt();
         String s = sc.next();
         int max = 0;
        for (int i = 0; i < n; i++) {
             String s1 = s.substring(0,i);
             String s2 = s.substring(i);
             int count = 0;
            for(char c ='a'; c <= 'z'; c++){
                boolean flag1 = false;
                for(int j = 0; j < s1.length(); j++){
                    if(c==s1.charAt(j)){
                        flag1 = true;
                        break;
                    }
                }
                boolean flag2 = false;
                for(int j = 0; j < s2.length(); j++){
                    if(c==s2.charAt(j)){
                        flag2 = true;
                        break;
                    }
                }
                if(flag1 && flag2){
                    count++;
                }
            }
            max = Math.max(max, count);
        }   
        System.out.println(max);
    }
}

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

const int MAX_N = 1000;
const int MAX_W = 10000;

int n, s, A[MAX_N], B[MAX_N], T[MAX_W+1];

int solve(){
    int sum = 0;
    bool V[MAX_N];
    
    for (int i = 0; i < n; i++)
    {
        B[i] = A[i];
        V[i] = false;
    }
    sort(B, B+n);
    for (int i = 0; i < n; i++) T[B[i]] = i;
    for (int i = 0; i < n; i++)
    {
        if(V[i]) continue;
        int cur = i, S = 0, m = MAX_W, an =  0;
        while(!V[cur]){
            V[cur] = true;
            an++;
            m = min(m, A[cur]);
            S += A[cur];
            cur = T[A[cur]];
        }
        sum += min(S + (an - 2) * m, m + S + (an + 1) * s);
    }
    return sum;
}

int main(){
    s = MAX_W;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
        s = min(s, A[i]);
    }
    cout << solve() << endl;
}


Measured memory_bytes: 3176
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author RiaD
 */

#include <iostream>
#include <fstream>

#include <iostream>

using namespace std;

class A {
public:
	void solve(std::istream& in, std::ostream& out) {
		int a, b;
		in >> a >> b;
		if (a <= 0 && 0 <= b) {
			out << "Zero\n";
			return;
		}

		int non = 0;
		if (b < 0) {
			non = b - a + 1;
		} else if (a < 0) {
			non = abs(a);
		}
		if (non % 2 == 0) {
			out << "Positive";
		} else {
			out << "Negative";
		}

	}
};


int main() {
	std::ios_base::sync_with_stdio(false);
	A solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	in.tie(0);
	out << std::fixed;
	out.precision(20);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 764
#include<stdio.h>
#define MAX_N 999999
int prime[MAX_N];
int is_prime[MAX_N+1];

int sieve(int n)
{
	int p=0,i,j;
	for(i=0;i<=n;i++)
		is_prime[i]=1;
	is_prime[0]=is_prime[1]=0;
	for(i=2;i<=n;i++)
	{
		if(is_prime[i])
		{
			prime[p++]=i;
			for(j=2*i;j<=n;j+=i)
				is_prime[j]=0;
		}
	}
	return p;
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		printf("%d\n",sieve(n));
	}
	return 0;
}


Measured memory_bytes: 5916
#include <iostream>
#include <numeric>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
const ll MOD=1000000007;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define range(i) (i).begin(),(i).end()
#define rrange(i) (i).rbegin(),(i).rend()
inline ll gcd(ll a, ll b) {return b?gcd(b, a%b):a;}
inline constexpr int intpow(ll a,ll b){
    if(b==0) return 1;
    ll ans=intpow(a,b/2);
    return ans*ans*(b&1?a:1);
}
inline constexpr int modpow(ll a,ll b){
    if(b==0) return 1;
    ll ans=intpow(a,b/2);
    return ans*ans%MOD*(b&1?a:1)%MOD;
}
template<class T> inline bool chmax(T& a,T b) {if(a<b){a=b;return 1;} return 0;}
template<class T> inline bool chmin(T& a,T b) {if(a>b){a=b;return 1;} return 0;}
const ll LINF=0x1fffffffffffffff;

struct unionfind{
    vector<ll> d;
    unionfind(ll x):d(x,-1){}
    ll root(ll x) {return d[x]<0?x:d[x]=root(d[x]);}
    ll size(ll x) {return -d[root(x)];};
    bool unite(ll x,ll y) {x=root(x),y=root(y); if(x!=y){if(d[x]>d[y])swap(x,y);d[x]+=d[y];d[y]=x;}return x!=y;}
    bool find(ll x,ll y) {return root(x)==root(y);}
};
int main(){
    ll n,k,cnt=0;
    cin>>n>>k;
    vector<ll> a(2*n);
    rep(i,0,n) {
        cin>>a[i];
        a[n+i]=a[i];
    }
    rep(i,0,n){
        vector<bool> f(n);
        rep(j,i+1,2*n){
        if(a[i]>a[j]){
            if(j<n){
                f[j]=1;
                cnt+=k*(k+1)/2%MOD;
                cnt%=MOD;
            }
            else if(f[j-n]==0){
                cnt+=(k-1)*k/2%MOD;
                cnt%=MOD;
            }
        }
        }
    }
    cout<<cnt%MOD;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n)  for(ll i = 0; i < n; i++)
#define vi vector<int>
#define all(x) (x).begin(),(x).end()
#define INF 1e9
using ll = long long;
using namespace std;
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; }

int main(){
  int w,h,n; cin >> w >> h >> n;
  vi x(n),y(n),a(n); rep(i,n) cin >> x[i] >> y[i] >> a[i];
  vector<vi> white(h,vi(w,1));

  rep(i,n){
    if(a[i] == 1) rep(j,h) rep(k,x[i]) white[j][k] = 0;
    else if(a[i] == 2) rep(j,h) for(int k = x[i]; k < w; k++) white[j][k] = 0;
    else if(a[i] == 3) rep(j,y[i]) rep(k,w) white[j][k] = 0;
    else if(a[i] == 4) for(int j = y[i]; j < h; j++) rep(k,w) white[j][k] = 0;
  }
  int ans = 0;
  rep(i,h) ans += accumulate(all(white[i]),0);
  cout << ans;
  cout << "\n";
  return 0;
}


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

int main(){
    using namespace std;
    unsigned long Q;
    cin >> Q;
    constexpr unsigned long M{205891132094649UL};
    const auto& calc = [](unsigned long a, unsigned long b, unsigned long c, unsigned long d) -> unsigned long {
        auto u{b}, l{b}, p{M};
        while(p /= 3)if((a / p + (4 - a / p % 3) % 3) * p <= c){
            if(u / p % 3 == 1)u = u / (3 * p) * 3 * p + 2 * p;
            if(l / p % 3 == 1)l = l / (3 * p) * 3 * p + p - 1;
        }
        return c - a + min(u - b + max(u, d) - min(u, d), b + d - 2 * l);
    };
    for(unsigned long _{0}, a, b, c, d; _ < Q; ++_){
        cin >> a >> b >> c >> d;
        if(--a > --c)swap(a, c), swap(b, d);
        if(--b > --d){
            b = M - b - 1;
            d = M - d - 1;
        }
        cout << max(calc(a, b, c, d), calc(b, a, d, c)) << endl;
    }
    return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#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>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {return (!n)?1:(mod_pow((x*x)%MOD,n/2)*((n&1)?x:1))%MOD;}
int madd(int a, int b) {return (a + b) % MOD;}
int msub(int a, int b) {return (a - b + MOD) % MOD;}
int mmul(int a, int b) {return (a * b) % MOD;}
int minv(int a) {return mod_pow(a, MOD-2);}
int mdiv(int a, int b) {return mmul(a, minv(b));}

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int board[15][15];
signed main() {
    int n;
    while(cin >> n, n) {
        memset(board, 0, sizeof(board));
        rep(i,0,n) rep(j,0,n) cin >> board[i][j];
        rep(i,0,n) rep(j,0,n) {
            board[i][n] += board[i][j];
            board[n][n] += board[i][j];
        }
        rep(j,0,n) rep(i,0,n) {
            board[n][j] += board[i][j];
        }
        repq(i,0,n) {
            repq(j,0,n) printf("%5lld", board[i][j]);
            cout << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 3140
#include<stdio.h>

int main()

{
    int n,i,count1=0,count2=0,b=1;

    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        while(i/b>=1)
        {
            count1++;
            b=b*10;
        }

        if(count1%2==1)
        {
            count2++;
        }
    }

    printf("%d\n",count2);


 return 0;
}

Measured memory_bytes: 128
# coding: utf-8

input_ = input().split()

count_5 = 0
count_7 = 0

for i in input_:
    if i == '5':
        count_5 += 1
    elif i == '7':
        count_7 += 1

if count_5 == 2 and count_7 == 1:
    print('YES')
else:
    print('NO')


Measured memory_bytes: 9080
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;

int main() {
	long long a, b, c, d, r;
	double f;
	cin >> a >> b;
	d = a / b;
	r = a % b;
	f = 1.0 * a / b;
	printf("%d %d %.5lf\n", d, r, f);
	return 0;
}


Measured memory_bytes: 3180
s = gets.chomp.split("")
arr = Hash.new(0)
("a".."z").each do |i|
    arr[i] += 1
end
s.each do |i|
    arr[i] -= 1
end
arr.each do |i,j|
    if j >= 1
        puts i
        exit
    end
end
puts "None"

Measured memory_bytes: 19172
import math
n = int(input())
prime = []
insu = {}
max_itr = math.ceil(math.sqrt(n))+1
try_range = [2] + list(range(3, max_itr, 2))
for i in try_range:
    if i > max_itr:
        break
    if n%i == 0:
        flag = 0
        for p in prime:
            if i%p == 0:
                flag = 1
                break
        if flag == 0:
            prime.append(i)
            while n%i == 0:
                if i in insu:
                    insu[i] += 1
                else:
                    insu[i] = 1
                n //= i
            if n < max_itr:
                max_itr = n

if n != 1:
    insu[n] = 1
# print(insu)

count = 0
for v in insu.values():
    i = 1
    while v >= i:
        v -= i
        i += 1
        count += 1

print(count)

Measured memory_bytes: 32624
#include <iostream>
#include <fstream>
#include <string> 
#include <cmath>  
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cctype>

#define BIT(a) (1 << (a))

using namespace std;

long long MOD = 1000000007;


long long mod_pow(long long x, long long n){
    long long res = 1;
    for(int i = 0;i < 60; i++){
        if(n >> i & 1) res = res * x % MOD;
        x = x * x % MOD;
    }
    return res;
}

int ascii[300];
int ascii2[300];

int main(void){
	char s[30];
	cin >> s;
	int len = strlen(s);
	int start = 'a';
	int end = 'z';
	for (int i = 0; i < len; i++){
		ascii[s[i]] = 1;
	}
	for (int i = start; i <= end; i++){
		if (!ascii[i]){
			s[len] = (char)i;
			s[len+1] = '\0';
			printf("%s\n", s);
			return 0;
		}
	}
	
	for (int i = len-1; i >= 0; i--){
		if (i == len-1){
			ascii2[s[i]] = 1;
			continue;
		}
		for (int j = s[i]; j <= end; j++){
			if (ascii2[j]){
				s[i] = (char)j;
				s[i+1] = '\0';
				printf("%s\n", s);
				return 0;
			}else{
				ascii2[s[i]] = 1;
			}
		}
	}

	printf("-1\n");


	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
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; }
template<class T>bool even(T n) { return ! (n & 1); }
template<class T>bool odd(T n) { return n & 1; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;}
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;

class UnionFind
{   
public:
    static const int N_MAX = 1000000;
    int par[N_MAX];
    int sz[N_MAX];
    int n;
    
    UnionFind(int n)
        : n{n}
    {
        for (int i = 0; i < n; ++i) {
            par[i] = i;
            sz[i] = 1;
        }
    }
    
    void init()
    {
        for (int i = 0; i < n; ++i) {
            par[i] = i;
            sz[i] = 1;
        }
    }

    int root(int x)
    {
        if (par[x] == x) {
            return x;
        }
        return par[x] = root(par[x]);
    }
    
    bool same(int x, int y)
    {
        return root(x) == root(y);
    }
    
    void unite(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) {
            return;
        }
        if (sz[rx] > sz[ry]) {
            par[ry] = rx;
            sz[rx] += sz[ry];
        } else {
            par[rx] = ry;
            sz[ry] += sz[rx];
        }
        return;
    }

    int size(int x)
    {
        return sz[root(x)];
    }
    
    int groups()
    {
        int cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (root(i) == i)
            ++cnt;
        }
        return cnt;
    }
};


int main()
{
        ll N, M;
        cin >> N >> M;
        vll p(N);
        rep(i, 0, N) cin >> p[i];

        UnionFind uf(N);
        rep(i, 0, M) {
                ll x, y;
                cin >> x >> y;
                uf.unite(x, y);
        }

        ll ans = 0;
        rep(i, 1, N+1) {
                if (uf.same(i, p[i-1])) ++ans;
        }
        cout << ans << endl;
}

Measured memory_bytes: 4840
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;

using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

const double EPS = 1e-8;

// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) {
    for(T &a : v) is >> a; return is;
}
using Coordinate = double;
bool eq(Coordinate a, Coordinate b) {
    return abs(a - b) < EPS;
}
/*
 * Point
 */
struct Point {
    Coordinate x,y;
    Point():x(0),y(0){}
    Point(Coordinate x,Coordinate y):x(x),y(y){}
    pair<Coordinate,Coordinate> to_pair() const { return make_pair(x,y); }
    bool operator == (const Point &a) const { return eq(x, a.x) && eq(y, a.y); }
    bool operator != (const Point &a) const { return !(*this == a); }
    
    Point operator + (const Point& p) const { return Point(x + p.x, y + p.y); }
    Point operator - (const Point& p) const { return Point(x - p.x, y - p.y); }
    Point operator - () const { return Point(-x, -y); }
    Point& operator += (const Point& p) { x += p.x; y += p.y; return *this; }
    Point& operator -= (const Point& p) { x -= p.x; y -= p.y; return *this; }
};
template<typename T> Point operator * (T k, const Point &p) { return Point(k * p.x, k * p.y); }
template<typename T> Point operator / (const Point &p, T k) { return Point(p.x / k, p.y / k); }
double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }
double norm(const Point& a) { return sqrt( dot(a, a) ); }

istream &operator >> (istream& is, Point& p){ return is >> p.x >> p.y;}

double distance(const Point& a, const Point& b) { return norm(a - b); }
double distance2(const Point& a, const Point& b) { return dot(a - b, a - b); }

// ??£?????´???????????????
Point orthonormal(Point &p) {
    Point p2 = Point(-1 * p.y, p.x); //rotate(p, 90);
    return p2 / norm(p2);
}

Point midPoint(const Point &a, const Point &b) {
    return Point((a.x + b.x) / 2, (a.y + b.y) / 2);
}


bool solve() {
    int n; cin >> n;
    if(n == 0) return 0;
    vector<Point> P(n); cin >> P;
    int ans = 1;
    rep(i, n) rep(j, i) if(distance(P[i], P[j]) <= 2.0 + EPS){
        Point a = P[i] - P[j];
        Point mid = midPoint(P[i], P[j]);
        double len = sqrt(1 - distance2(mid, P[j]));
        Point c = len * orthonormal(a) + mid;
        int cnt = 0;
        rep(l, n) {
            cnt += distance2(P[l], c) <= 1.0 + EPS;
        }
        ans = max(ans, cnt);
    }
    cout << ans << endl;
    return 1;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(solve());
    return 0;
}

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

#define C getchar()-48
ll read()
{
    ll s=0,t=1,k=C;
  for (;k>9||k<0;k=C) if (k==-3) t=-1;
  for (;k>=0&&k<=9;k=C) s=(s<<1)+(s<<3)+k;
  return s*t;
}

  ll A,n,r;
int main()
{
  for (ll n=read();~--n;)
    {
      ll x=read();
      A+=min(x,r);
      x-=min(x,r);
      A+=x>>1;
      x=x&1;
      r=x;
    }
  cout<<A;
  exit(0);
}

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

int h, w, k;

int main(int argc, char *argv[]) {
  std::cin >> h >> w >> k;
  std::vector<std::string> c(h);
  for (int i = 0; i < h; ++i) {
    std::cin >> c.at(i);
  }

  int ans = 0;
  for (int i = 0; i < (1 << h); ++i)
    for (int j = 0; j < (1 << w); ++j) {
      int cnt = 0;
      for (int l = 0; l < h; ++l) {
        for (int m = 0; m < w; ++m) {
          if (i & 1 << l)
            continue;
          if (j & 1 << m)
            continue;
          if (c.at(l).at(m) == '#')
            ++cnt;
        }
      }
      if (cnt == k)
        ans++;
    }

  std::cout << ans << std::endl;
}


Measured memory_bytes: 3136
#include<iostream>
#include<string>
#include<vector>

using namespace std;

void Main()
{
    int N;
    cin >> N;
    string S;
    cin >> S;

    int result = N;
    char prev = S[0];
    for (int i = 1; i < N; i++) {
        if (S[i] == prev) {
            result--;
        } else {
            prev = S[i];
        }
    }

    cout << result;

}

int main()
{
    Main();
    return 0;
}


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

const ll INF = 1e10;

int main(){
    ll n, k;
    scanf("%lld%lld", &n, &k);
    
    ll a[n];
    for(int i = 0; i < n; i++)
        scanf("%lld", a + i);
    
    vector<ll> dp(n, INF);
    
    dp[0] = 0;
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j <= i + k; j++){
            if(j < n){
                dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));
            }
        }
    }
    
    cout<<dp[n-1];
    
    return 0;
}

Measured memory_bytes: 4776
#include <iostream>
#include <algorithm>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main()
{
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  cout <<  min(a, b) + min(c, d) << endl;
}


Measured memory_bytes: 256
A,B,K=map(int,input().split())
while True:
    if A%2==1:
        A-=1
    K-=1
    B+=A//2
    A//=2
    if K==0: break
    if B%2==1:
        B-=1
    A+=B//2
    B//=2
    K-=1
    if K==0: break
    
print(A,B)

Measured memory_bytes: 3060
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: i32,
        v: [i32; n]
    }
    
    use std::collections::BTreeMap;
    
    let mut even_count : BTreeMap<i32, i32>= BTreeMap::new();
    let mut odd_count : BTreeMap<i32, i32> = BTreeMap::new();
    
    for (i, x) in v.iter().enumerate() {
        if i % 2 == 0 {
            *even_count.entry(*x).or_insert(0) += 1;
        } else {
            *odd_count.entry(*x).or_insert(0) += 1;
        }
    }
    
    let mut es = even_count.iter().map(|(k, v)| (*v, *k)).collect::<Vec<_>>();
    let mut os = odd_count.iter().map(|(k, v)| (*v, *k)).collect::<Vec<_>>();
    
    es.sort();
    es.reverse();
    os.sort();
    os.reverse();
    
    es.push((0, -1));
    os.push((0, -1));
    
    let ans = if es[0].1 != os[0].1 {
        n - es[0].0 - os[0].0    
    } else {
        n - std::cmp::max(es[0].0 + os[1].0, os[0].0 + es[1].0)
    };
    
    println!("{}", ans);
}

Measured memory_bytes: 10492
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
#define p(s) cout << s << endl;
typedef long long LL;
template <class T>
bool contain(const std::string &s, const T &v)
{
    return s.find(v) != std::string::npos;
}

int main()
{
    string a,b,c; cin>>a>>b>>c;
    if(a[a.size()-1]==b[0]&&b[b.size()-1]==c[0]) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define REP(i, n) for(int i = 0, i##_MACRO = (n); i < (i##_MACRO); i++)
#define RANGE(i, a, b) for(int i = (a); i < (b); i++)
#define EACH(e, a) for(auto&& e : a)
#define ALL(a) (a).begin(), (a).end()
#define AALL(a, n) (a), ((a) + (n))
#define FILL(a, n) memset((a), n, sizeof(a))
#define FILLZ(a) FILL(a, 0)
#define MODNUM (static_cast<int>(1e9 + 7))
#define MOD(x) ((x) % MODNUM)

using namespace std;

using ll = long long;
using VI = vector<int>;
using VI2D = vector<vector<int>>;

const int INF = 2020202021;
const double EPS = 1e-10;
const double PI = acos(-1.0);

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

inline int toInt(string s) {
	int v;
	istringstream sin(s);
	sin >> v;
	return v;
}

int main() {
	int n;
	scanf("%d", &n);
	VI a(n);
	vector<ll> s(n);

	REP(i, n) {
		scanf("%d", &a[i]);
		if(i == 0) {
			s[i] = a[i];
		} else {
			s[i] = s[i - 1] + a[i];
		}
	}

	ll sum = s[n - 1];
	ll min = INF;
	REP(i, n) {
		ll tmp = abs(s[i] - (sum - s[i]));
		if(tmp < min) {
			min = tmp;
		}
	}
	printf("%lld\n", min);
	return 0;
}

Measured memory_bytes: 2560
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
long long dsc(const void *a, const void *b) {
  return *(long long *)b - *(long long *)a;
}
int main(){
    long long n,i,j,k,ans=0,a[200020]={},b[200020]={};
    scanf("%lld",&n);
    for(i=0; i<n; i++){
        scanf("%d",&a[i]);
    }
    qsort(a,n,sizeof(long long),dsc);
    if(n%2==0){
        ans+=a[0];
        k=n/2;
        for(i=1; i<k; i++){
            ans+=2*a[i];
        }
    }
    if(n%2==1){
        ans+=a[0];
        k=n/2;
        for(i=1; i<k; i++){
            ans+=2*a[i];
        }
        ans+=a[k];
    }
    printf("%lld",ans);
    return 0;
}

Measured memory_bytes: 4684
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b)  for(int i=(a),i##formax=(b);i< i##formax;i++)
#define FORR(i,a,b) for(int i=(a),i##formin=(b);i>=i##formin;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pcnt __builtin_popcount
#define sz(x) (int)(x).size()
#define maxs(x,y) x=max((x),(y))
#define mins(x,y) x=min((x),(y))
#define show(x) cout<<#x<<" = "<<(x)<<endl;
#define all(a) ((a).begin()),((a).end())
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define bit(n) (1LL<<(n))
typedef long long ll;
typedef __int128_t lll;
template<class T>using V=vector<T>;
template<class T>using VV=V<V<T>>;
template<class T,class Y>using P=pair<T,Y>;
template<class T,class Y>ostream& operator<<(ostream&o,P<T,Y>&p){return o<<"("<<p.fi<<","<<p.se<<")";}
template<class T>ostream& operator<<(ostream&o,V<T>&v){for(T&t:v)o<<t<<",";return o<<endl;}
template<class T>void uniq(V<T>&v){sort(all(v));v.erase(unique(all(v)), v.end());}

lll gcd(lll a,lll b,lll&x,lll&y){if(!b){x=1;y=0;return a;}lll d=gcd(b,a%b,y,x);y-=a/b*x;return d;}
ll modInv(ll a,ll m){lll x,y;gcd(a,m,x,y);return(x%m+m)%m;}
ll modPow(lll a,lll n,ll m){lll p=1;for(;n;n>>=1,a=a*a%m)if(n&1)p=p*a%m;return p;}
//V<ll>F,R;void setF(int n,int mod){F=R=V<ll>(++n);F[0]=1;FOR(i,1,n)F[i]=F[i-1]*i%mod;R[n-1]=modInv(F[n-1],mod);FORR(i,n-1,1)R[i-1]=R[i]*i%mod;}
//ll comb(int a,int b,int m){return a<b||b<0?0:F[a]*R[b]%m*R[a-b]%m;}

const int IINF = 1e9+6;
const ll LINF = 1e18;
const int MOD = 1e9+7; //998244353;
const int N = 2e5;
ll n, d, a;
P<ll, ll> v[N];
map<ll, ll> m;
main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> d >> a;
  d *= 2;
  FOR(i, 0, n){
    cin >> v[i].fi >> v[i].se;
    m[v[i].fi] = 0;
    m[v[i].fi+d] = 0;
  }
  sort(v, v+n);
  int j = 0;
  ll s = 0, ans = 0;
  if(!d){
    FOR(i, 0, n) ans += (v[i].se+a-1)/a;
    cout << ans << endl;
    return 0;
  }
  for(auto p:m){
    if(j<n && v[j].fi==p.fi){
      ll f = max(0ll, (v[j].se+a-1)/a-s);
      ans += f;
      s += f;
      m[v[j].fi+d] += f;
      j++;
    }
    s -= p.se;
  }
  cout << ans << endl;
}

Measured memory_bytes: 28416
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include <cmath>
//#include <bits/stdc++.h>
using namespace std;

long int gcd(int a, int b, int c){
    for (int x=1;x<=a;x++){
        if (a%(a+1-x)==0&&b%(a+1-x)==0&&c%(a+1-x)==0){
            return  a+1-x;
            break;
       }
    }
    return 1;
}


int main(){
    long int K,sum=0;
    int s=0;
    cin>>K;
    for (int i=1;i<=K;i++){
        for (int j=i;j<=K;j++){
            for (int k=j;k<=K;k++){
                if (i==j&&j==k){
                    sum+=gcd(i,j,k);
                }
                else if (i==j) sum+=3*gcd(i,j,k);
                else if (j==k) sum+=3*gcd(i,j,k);
                else sum+=6*gcd(i,j,k);
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}

Measured memory_bytes: 3632
from queue import Queue


n, m = list(map(int, input().split()))
g = {i: [] for i in range(1, n + 1)}
for i in range(m):
    ai, bi = list(map(int, input().split()))
    if min(ai, bi) == 1:
        g[1].append(max(ai, bi))
    else:
        g[ai].append(bi)
        g[bi].append(ai)

q = Queue()
q.put(1)
m = {}
while not q.empty():
    x = q.get()
    for t in g[x]:
        if t not in m:
            m[t] = x
            q.put(t)

print('Yes')
for i in range(2, n + 1):
    print(m[i])


Measured memory_bytes: 50944
//見　た　な
//見たな
//見　た　な

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

int memo[101]; //N!の素因数分解した時のiの指数
//各素因数の数が基準以上であればよい


//memoの内、引数xに対しx以上のものの個数
int num(int x){
    int cnt = 0;
    for(int i = 1; i < 101; i++){
        if(memo[i] >= x) cnt++;
    }
    return cnt;
}



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

    //N！の素因数分解をmemoに保存
    int cur;
    for(int i = 2; i < n+1; i++){
        cur = i;
        for(int j = 2; j < i + 1; j++){
            while(cur % j == 0){
                memo[j]++;
                cur /= j;    
            }
        }
    }
    int ans = 0;
    cout << num(74) + num(24)*(num(2)-1) + num(14)*(num(4)-1) + num(4)*(num(4)-1)*(num(2)-2)/2 << endl;
}

Measured memory_bytes: 256
import bisect
n=int(input())
A=list(map(int,input().split()))
s=set()
for i in range(2**n):
    sum_=0
    for j in range(n):
        if(i>>j&1):
            sum_+=A[j]
    s.add(sum_)
li=sorted(s)
q=int(input())
M=list(map(int,input().split()))
for m in M:
    b=bisect.bisect_left(li,m)
    if(b<len(li) and li[b]==m):
        print('yes')
    else:
        print('no')


Measured memory_bytes: 5676
// B - Chocolate
#include <bits/stdc++.h>
using namespace std;

int main(){
	int N,D,X; cin>>N>>D>>X;
	int ans = X;
	while(N--){
		int a; cin>>a;
		for(int j=0; j<D && j*a+1<=D; ++j) ans++;
	}
	cout<< ans <<endl;
}

Measured memory_bytes: 256