akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
    #include <ostream>
    using namespace std;
    template <typename T> void printRange(T &x) { for (auto y : x) cout << y << ' '; cout << endl; }
    typedef long long ll;
    typedef vector<int> vi;
    typedef pair<int, int> ii;
    typedef vector<ii> vii;
    #define getNum(i) scanf("%d", &(i))
    #define MODULO 321555123
    #define INF 10000000007ll
    #define FOR(i,j,k) for(int (i)=(j);(i)<=(k);(i)++)
    #define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
    #define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #define getNum(i) scanf("%d", &(i))
    #define mp make_pair
    #define reset(a) memset((a), 0, sizeof((a)))
     
    const int maxn = 2e5 + 10;
     
    int main() {
        IOS
        string s;
        cin >> s;
        if (s.length() > 3) {
            s = s.substr(0, 4);
            if (s == "YAKI") {
                cout << "Yes" << endl;
                return 0;
            }
        }
        cout << "No" << endl;
     
        return 0;
    }

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

using namespace std;

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)


lli dp[1100000][2];

signed main(){

	string n;
	cin>>n;

	lli cnt=0;
	lli ans=0;
	bool before=false;

	reverse(n.begin(),n.end());

	lli zero = n.at(0)-'0';
	dp[0][0]=zero;
	dp[0][1]=10-zero;
	for(lli i=1;i<n.size();i++){
		lli now=n.at(i)-'0';
		if(DEBUG)cout<<"now="<<now<<endl;
		//繰り上がらない時
		dp[i][0] = now + min(dp[i-1][0],dp[i-1][1]+1);

		//繰り上げる時
		dp[i][1] = (10-now)+min(dp[i-1][0],dp[i-1][1]-1);

	}
	lli size = n.size()-1;
	if(DEBUG)cout<<dp[size][0]<<" "<<dp[size][1]<<endl;
	cout<<min(dp[size][0],dp[size][1]+1)<<endl;

	return 0;
}

Measured memory_bytes: 17668
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  vector<long>p(n);
  int i;
  for(i=0;i<n;i++)cin>>p[i];
  vector<long>a(n);
  vector<long>b(n);
  a[0]=1;
  b[0]=1e9-50000;
  for(i=1;i<n;i++){
    a[i] = a[i-1]+30000;
    b[i] = b[i-1]-30000;
  }
  for(i=1;i<n;i++){
    a[p[i]-1]+=i;
  }
  for(i=0;i<n;i++){
    cout << a[i] << " ";
  }
  cout<<endl;
  for(i=0;i<n;i++){
    cout<<b[i]<<" ";
  }
  cout<<endl;
}


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

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

    ll i,j,k,l,m,n,w,x,y,z,p,q,tmp,sum=0;
    string str;
    cin>>n>>m;
    vector<ll> a(n);
    vector<ll> b(n);

    rep(i,m){
        cin>>k;
        rep(j,k){
            cin>>l;
            l--;
            a[i]=a[i]|(1<<l);
        }
    }
    rep(i,m) cin>>b[i];
    ll f;
    for(int bit=0,i=0;bit<(1<<n);++bit,++i){
        f=1;
        rep(i,m){
            ll cnt=0;
            tmp=a[i]&bit;
            while(tmp!=0){
                if(tmp%2==1) cnt++;
                tmp/=2;
            }
            if((cnt+b[i])%2==1) f=0;
        }
        if(f==1) sum++;
    }

    cout<<sum<<endl;
    

    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
using namespace std;
int T,a,b,c,d;
int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d",&a,&b,&c,&d);
		int k=(max(a,b)-1)/(min(a,b)+1)+1;
		int l=0,r=a+b,A,B;
		while(l<r){
			int mid=(l+r)>>1;
			A=a-mid/(k+1)*k-mid%(k+1),B=b-mid/(k+1);
			if(B<=1ll*A*k)l=mid+1;else r=mid;
		}A=a-l/(k+1)*k-l%(k+1),B=b-l/(k+1);r=l+B-A*k+1;
		for(int i=c;i<=min(l,d);i++)printf("%c",i%(k+1)?'A':'B');
		for(int i=max(c,l+1);i<=d;i++)printf("%c",(i-r)%(k+1)?'B':'A');
		puts("");
	}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi; 
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
const int mod = 1e9+7;
const int maxn = 5e5+10;
const int inf = 0x3f3f3f3f;
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	double a;
	cin>>a;
	a/=3;
	cout<<fixed<<setprecision(8)<<a*a*a<<'\n';
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<list>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;


using ll = long long int;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define repr(i, n, m) for (ll i = n - 1; i >= 0; --i)

#define co(n) cout << n << endl

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

    ll N;
    cin >> N;

    vector<ll> a(N);

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

    ll sum = 0;

    rep(i, N) {
        sum = sum ^ a[i];
    }

    if (sum == 0) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}

Measured memory_bytes: 1024
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50


def run():
    N,M, *PY = map(int, read().split())
    PY = [(PY[2 * i + 1], PY[2 * i], i) for i in range(M)]

    count_town = [0] * (N+1)
    ans = [''] * M
    PY = sorted(PY)
    for y, p, i in PY:
        count_town[p] += 1
        right = count_town[p]
        left = p
        right = (6- len(str(right))) * '0' + str(right)
        left = (6 - len(str(left))) * '0' + str(left)
        ans[i] = left + right

    for a in ans:
        print(a)






if __name__ == "__main__":
    run()

Measured memory_bytes: 34972
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <random>
#include <chrono>
using namespace std;

int n;
const int maxn = 2e5 + 5;

int x[maxn], y[maxn];

pair<int, pair<int, int> > p[maxn];
int tot;
typedef pair<int, int> pii;

pii z[maxn];
int fa[maxn];
int Find(int x){
    return x == fa[x] ? x : fa[x] = Find(fa[x]);
}

int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        scanf("%d%d", &x[i], &y[i]);
    }
    for(int i = 1;i <= n;i++){
        z[i] = make_pair(x[i], i);
    }
    tot = 0;
    sort(z + 1, z + 1 + n);
    for(int i = 1;i < n;i++){
        int u = z[i].second;
        int v = z[i + 1].second;
        p[++tot] = make_pair(z[i + 1].first - z[i].first, make_pair(u, v));
    }
    for(int i = 1;i <= n;i++){
        z[i] = make_pair(y[i], i);
    }
    sort(z + 1, z + 1 + n);
    for(int i = 1;i < n;i++){
        int u = z[i].second;
        int v = z[i + 1].second;
        p[++tot] = make_pair(z[i + 1].first - z[i].first, make_pair(u, v));
    }
    sort(p + 1, p + 1 + tot);
    for(int i = 1;i <= n;i++){
        fa[i] = i;
    }
    long long ans = 0;
    for(int i = 1;i <= tot;i++){
        int u = p[i].second.first;
        int v = p[i].second.second;
        int fx = Find(u);
        int fy = Find(v);
        if(fx != fy){
            fa[fx] = fy;
            ans += p[i].first;
        }
    }
    cout << ans << endl;
    return 0;
}


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

constexpr int inf = 1e9;

struct edge {
    int to, cost;
};
using edges = vector<edge>;
using graph = vector<edges>;

int main() {
    int n, m, p; cin >> n >> m >> p;
    graph g(n), rg(n);
    for(int i = 0; i < m; ++i) {
        int a, b, c; cin >> a >> b >> c;
        a--, b--;
        g[a].push_back(edge{b, p - c});
        rg[b].push_back(edge{a, -1});
    }

    vector<bool> from_s(n), from_t(n);
    {
        function<void(graph const&, vector<bool>&, int)> dfs = [&] (graph const& g, vector<bool>& r, int v) {
            r[v] = true;
            for(auto const& e : g[v]) {
                if(r[e.to]) continue;
                dfs(g, r, e.to);
            }
        };
        dfs(g, from_s, 0);
        dfs(rg, from_t, n - 1);
    }

    vector<int> ans(n, inf);
    ans[0] = 0;
    bool neg = false;
    for(int cnt = 0; cnt <= 2 * n; ++cnt) {
        for(int v = 0; v < n; ++v) {
            if(!from_s[v] || !from_t[v]) continue;
            for(auto const& e : g[v]) {
                if(ans[e.to] > ans[v] + e.cost) {
                    neg |= cnt >= n;
                    ans[e.to] = ans[v] + e.cost;
                }
            }
        }
    }

    if(neg) {
        cout << -1 << endl;
    } else {
        cout << max(0, -ans[n - 1]) << endl;
    }
}


Measured memory_bytes: 640
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
using namespace std;

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

  int   N,K;
  cin>>N>>K;
  vector<vector<int  >>dp(2,vector<int   >(K+1,-1));
  vector<int    >a(N+1);
  for(int   i=0;i<N;i++)
  {
    cin>>a[i+1];
  }
  for(int   i=0;i<K+1;i++)
  {
    if(a[1]>=i)dp[1][i]=i+1;
    else  dp[1][i]=a[1]+1;
  }
  for(int   i=2;i<N+1;i++)
  {
    dp[i%2][0]=1;
    for(int   j=1;j<K+1;j++)
    {
      if(j>a[i])
      {
        dp[i%2][j]=dp[i%2][j-1]+dp[(i+1)%2][j];
        dp[i%2][j]%=1000000007;
        dp[i%2][j]-=dp[(i+1)%2][j-a[i]-1];
      }
      else dp[i%2][j]=dp[i%2][j-1]+dp[(i+1)%2][j];
      dp[i%2][j]%=1000000007;
    }
  }
  if(dp[N%2][K]<0){dp[N%2][K]+=1000000007;}
  dp[N%2][K]%=1000000007;
  int   res=dp[N%2][K]-dp[N%2][K-1];
  if(res<0){res+=1000000007;}
  if(res<0){res+=1000000007;}
  res%=1000000007;
  if(K>0){cout<<res;return 0;}
  cout<<dp[N%2][K];
  cout<<endl;

}


Measured memory_bytes: 1408
/*Infor {{{
 ******************************************************
 * > File Name: tmp.cpp
 * > Author: samcompu
 * > Mail: admin@samcompu.com
 * > Created Time: 2018/7/14 18:57:02
 ******************************************************
}}}*/

#include <bits/stdc++.h>

using namespace std;

/*Abbreviation {{{ */
#define FOR(i,a,b) for(int i = (int)(a); i <= (int)(b); i++)
#define ROF(i,a,b) for(int i = (int)(a); i >= (int)(b); i--)
#define cini(a) scanf("%d", &(a))
#define cinc(a) scanf("%c", &(a))
#define cinl(a) scanf("%lld", &(a))
#define cins(a) scanf("%s", (a))
#define cind(a) scanf("%lf", &(a))
#define coui(a) printf("%d", (a))
#define couc(a) printf("%c", (a))
#define coul(a) printf("%lld", (a))
#define cous(a) printf("%s", (a))
#define coud(a) printf("%f", (a))
#define SPA printf(" ")
#define LIN printf("\n")
#define ull unsigned long long int
/*}}}*/

/*Constant {{{*/
const int MAXN = 100;
/*}}}*/

/*Class {{{*/
/*}}}*/

/*Variable {{{*/
int color[MAXN + 1];
/*}}}*/

/*Declare {{{*/
/*}}}*/

/*Main {{{*/
int main() {
  int n;
  cini(n);
  FOR(i, 1, n) cini(color[i]);
  int ans = 0;
  int sumNum = 1;
  FOR(i, 2, n) {
    if (color[i] == color[i - 1]) sumNum++;
    else {
      ans += sumNum / 2;
      sumNum = 1;
    }
  }
  coui(ans + sumNum / 2);
  return 0;
}
/*}}}*/

/*Define {{{*/
/*}}}*/


Measured memory_bytes: 256
process.stdin.resume();
process.stdin.setEncoding('utf8');

function _c2(n){
    if (n < 2){
        return 0;
    } else {
        return (n*(n-1))/2;
    }
}

function Main(input){
    line = input.split(" ");
    m = parseInt(line[0]);
    n = parseInt(line[1]);
    ans = _c2(m) + _c2(n);
    console.log(ans);
}

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

Measured memory_bytes: 7500
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define foo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(a,min(b,min(c,d)))
#define max4(a,b,c,d)       max(a,max(b,max(c,d)))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
typedef pair<int, int>  pii;
typedef pair<ll, ll>  pl;
typedef vector<int>   vi;
typedef vector<ll>    vl;
typedef vector<pii>   vpii;
typedef vector<pl>    vpl;
typedef vector<vi>    vvi;
typedef vector<vl>    vvl;
int mpow(int base, int exp); 
void ipgraph(int m);
void DFS(int n);
bool isprime(ll n);
ll gcd(ll a,ll b);
void rstgraph(int n);

const int mod = pow(10,9)+7;
const int N = 3e5, M = N;

//=======================================================================================
vi g[N];
int vis[N];
int nodes=0;

void iluzn() {
  int a,b,c;
  cin>>a>>b>>c;
  if(a+b+c>=22)  cout<<"bust";
  else cout<<"win";
}

 
int main() {
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t=1,i;
    // cin>>t;
     while(t--) iluzn();
    return 0;
}
int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}
bool isprime(ll n){
    if(n<=1)
        return false;
    if(n==2)
        return true;
    if(n%2==0)
        return false;
    ll rt = sqrt(n);
    for(ll i=3;i<rt;i+=2){
        if(n%i==0)
            return false;
    }
    return true;
}
ll gcd(ll a, ll b) 
{ 
    if (a == 0) 
        return b; 
    return gcd(b % a, a); 
} 
void ipgraph(int m){
  int u, v;
  while(m--){
    cin>>u>>v;
    g[u].pb(v);
    // g[v].pb(u);
  }
}

void DFS(int n)
{  ++nodes;
    vis[n] = 1;
    for(vector<int>::iterator i = g[n].begin(); i != g[n].end() ; i++)
    {
        if(!vis[*i])
            DFS(*i);
    }
}
void rstgraph(int n){
  int i;
  fo(i,n+1){
    g[i].clear();
    vis[i]=0;
  }
  nodes=0;
}

Measured memory_bytes: 10600
#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=n-1;i>=a;--i)
#define cl(x,a) memset(x,a,sizeof(x))
#define all(x) x.bein(),x.end()
#define INF 0x3f3f3f3f
#define EPS 1e-8
using namespace std;
const int maxn=1e6+5;
char ss[1000];
int n,l,r,mid,start; //断开头

int main()
{
    scanf("%d",&n);
    l=0;r=n-1;
    int flag=0;
    cout<<0<<endl;
    fflush(stdout);
    scanf("%s",ss);
    if(ss[0]=='V') flag=1;
    else if(ss[0]=='M') start=1;
    else start=0;
    
    while(!flag){
        mid=(l+r)>>1;
        if(mid==l) mid++;
        printf("%d\n",mid);
        fflush(stdout);
        scanf("%s",ss);
        if(ss[0]=='V') flag=1;
        int dis=mid-l;
        int ttemp;
            ttemp=(ss[0]=='M'?1:0);
        if(dis&1){
            if(ttemp!=start){
                l=mid;
                start=ttemp;
            }
            else {
                r=mid-1;
            }
        }
        else {
            if(ttemp==start){
                l=mid;
                start=ttemp;
            }
            else {
                r=mid-1;
            }

        }

    }
    return 0;
}


Measured memory_bytes: 880
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
typedef long long LL;
const LL N=200005;
LL n,X; 
LL a[N];
LL ans;
LL sum[N];
int main()
{
	scanf("%lld%lld",&n,&X);
	ans=0;
	for (LL u=1;u<=n;u++) 
	{
		scanf("%lld",&a[u]);
		sum[u]=sum[u-1]+a[u];
		ans=ans+a[u]+X+a[u]*4;
	}
	//printf("%lld\n",ans);
	for (LL u=n-1;u>=1;u--)
	{
		LL j;LL cnt=1;
		LL tot=X*u;
		for (LL i=n;i>=1;i=j-1)
		{
			j=max(1LL,i-u+1);//这一段都是cnt
			if (cnt==1) tot=tot+(sum[i]-sum[j-1])*(1+4);
			else tot=tot+(sum[i]-sum[j-1])*(2*cnt+1);
			cnt++;
			if (tot>=ans) break;
		}
		ans=min(ans,tot);
	}
	printf("%lld\n",ans+n*X);
	return 0;
}

Measured memory_bytes: 3328
#include"bits/stdc++.h"

using namespace std; 

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef priority_queue<int> PQ_DESC;
typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) int((a).size())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ROF(i,a,b) for(int i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int LARGE_INT = 1e9+100;
const int INF = 2e9+100;
const LL INF_LL = (LL)INF * 300 * 300;
const int MOD = 1e9+7;

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

LL modpow(LL a, LL n) {
    LL res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

void Main()
{
    int n;cin>>n;

    VL a(n);
    LL sum_a=0;
    REP(i,n){
        cin>>a[i];
        sum_a += a[i];
    }
    VL b(n);
    LL sum_b=0;
    REP(i,n){
        cin>>b[i];
        sum_b += b[i];
    }
    LL k = sum_b-sum_a;

    LL num=0;
    REP(i,n){
        if(a[i]<b[i]){
            num += (b[i]-a[i]+1)/2;
        }
        //if(a[i]>b[i]){
        //    num += a[i]-b[i];
        //}
    }

    if(num>k){
        cout<<"No"<<endl;
        return;
    }
    
    cout<<"Yes"<<endl;



	return;
}

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

Measured memory_bytes: 384
using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;

namespace AOJ
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }

        struct pair
        {
            public long k;
            public long v;
            public pair(long K, long V) { k = K; v = V; }
        }
        static void Main(string[] args)
        {
            var N = Sarray()[0];
            var Ai = Sarray();
            var As = new List<pair>();
            for (var i = 0; i < N; ++i)
            {
                var p = new pair(i, Ai[i]);
                As.Add(p);
            }
            As.Sort((l, r) => (int)(l.v - r.v));

            var v = new bool[N];
            var cst = 0L;
            for (var i = 0L; i < N; ++i)
            {
                if (v[i]) continue;
                var j = i;
                var sum = 0L;
                var cnt = 0L;
                do
                {
                    ++cnt;
                    sum += As[(int)j].v;
                    v[j] = true;
                    j = As[(int)j].k;
                }
                while (i != j);
                if (1 < cnt)
                    cst += sum + Math.Min((cnt - 2) * As[(int)i].v, As[(int)i].v + (cnt + 1) * As[0].v);
            }
            WriteLine(cst);
        }
    }
}


Measured memory_bytes: 18560
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc *bufio.Scanner = func() *bufio.Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return sc
}()

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func calcMax(n int) (sum int) {
	return n * (n - 1) / 2
}

func main() {
	n := nextInt()
	fmt.Println(n * (n - 1) / 2)
}


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

using namespace std;

int main() {
  string s;
  cin >> s;
  deque<char> dq(s.begin(), s.end());
  bool flipped(false);
  int q, t, f;
  char c;
  cin >> q;
  while (q--) {
    cin >> t;
    if (t == 1) flipped = !flipped;
    else {
      cin >> f >> c;
      flipped ^ (f - 1) ? dq.push_back(c) : dq.push_front(c);
    }
  }
  //while
  if (flipped) reverse(dq.begin(), dq.end());
  //cout << flipped << endl;
  for (char x: dq) {
    cout << x;
  }
  return 0;
}

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

import itertools

chars = '123456789'

size = int(input())
p = tuple(input().split(' '))
q = tuple(input().split(' '))

values = list(itertools.permutations(chars[:size], size))
index_p = values.index(p)
index_q = values.index(q)

print(abs(index_p - index_q))


Measured memory_bytes: 8052
#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<set>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
set<string>se;
vector<string>str(15);
int n,K;
void dfs2(int a,int b,int done,string s){
  if(done==(1<<2)-1){
    se.insert(s);
    return;
  }
  for(int i=0;i<2;i++){
    if(!(done&(1<<i))){
      switch(i){
      case 0:dfs2(a,b,done|(1<<i),s+str[a]);break;
      case 1:dfs2(a,b,done|(1<<i),s+str[b]);break;
      }
    }
  }
}
void dfs3(int a,int b,int c,int done,string s){
  if(done==(1<<3)-1){
    se.insert(s);
    return;
  }
  for(int i=0;i<3;i++){
    if(!(done&(1<<i))){
      switch(i){
      case 0:dfs3(a,b,c,done|(1<<i),s+str[a]);break;
      case 1:dfs3(a,b,c,done|(1<<i),s+str[b]);break;
      case 2:dfs3(a,b,c,done|(1<<i),s+str[c]);break;
      }
    }
  }
}
void dfs(int a,int b,int c,int d,int done,string s){
  if(done==(1<<4)-1){
    se.insert(s);
    return;
  }
  for(int i=0;i<4;i++){
    if(!(done&(1<<i))){
      switch(i){
      case 0:dfs(a,b,c,d,done|(1<<i),s+str[a]);break;
      case 1:dfs(a,b,c,d,done|(1<<i),s+str[b]);break;
      case 2:dfs(a,b,c,d,done|(1<<i),s+str[c]);break;
      case 3:dfs(a,b,c,d,done|(1<<i),s+str[d]);break;
      }
    }
  }
}
int main(){
  while(cin>>n>>K,n){
    se.clear();
    for(int i=0;i<n;i++)
      cin>>str[i];
    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
	if(K==2){dfs2(i,j,0,"");continue;}
	for(int k=j+1;k<n;k++){
	  if(K==3){dfs3(i,j,k,0,"");continue;}
	  for(int l=k+1;l<n;l++){
	    dfs(i,j,k,l,0,"");
	  }
	}
      }
    }
    cout<<se.size()<<endl;
  }
  return 0;
}



Measured memory_bytes: 3364
n,m = map(int,input().split())
k = [0]*m
s = [0]*m
for i in range(m):
    k[i],*s[i] = map(int,input().split())
p = list(map(int,input().split()))

ans = 0
for i in range(2**n):
    ls = []
    for j in range(n):
        if (i>>j)&1:
            ls.append(j+1)
    
    flg = 1
    for k in range(m):
        if len(set(s[k])&set(ls))%2==p[k]:
            pass
        else:
            flg = 0
    if flg ==1:
        ans +=1

print(ans)


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

typedef long long ll;
typedef vector <bool> v_bool;
typedef vector <ll> v_ll;
typedef vector <vector <ll> > vv_ll;
typedef vector < pair <ll,ll> > vp_ll;
typedef pair <ll,ll> p_ll;

bool TEST=false;
ll MOD = 1000000007;

void show(){if (TEST){cout << endl;}}
void show(string x){if (TEST){cout << x;}}
void show(string x,bool enter){
  if (TEST){if (enter) cout << "<#> " << x << endl;else cout << x;}
}
void show(ll x){if (TEST){cout << x;}}
void show(ll x,bool enter){
  if (TEST){if (enter) cout << "<#> " << x << endl;else cout << x;}
}
void show(p_ll p){
  if (TEST){cout << "{ ";show(p.first);cout << " ";show(p.second);cout << " }";}
}
void show(v_ll v){
  if (TEST){
    cout << "<begin> ";
    for (ll i=0;i<v.size();i++){show(v[i]); cout << " ";}cout <<"<end>"<< endl;
}}
void show(vv_ll v){
  if (TEST){
    cout << "//////////" << endl;
    for (ll i=0;i<v.size();i++){
      show(v[i]); cout << endl;
}}}
void show(vp_ll v){
  if (TEST){
    cout << "//////////" << endl;
    for (ll i=0;i<v.size();i++){
      show(v[i]); cout << endl;
}}}

ll mod(ll x){
  ll n=MOD;
  ll ret=x%n;
  if (ret<0)ret+=n;
  return ret;
}
void mod_self(ll &x){
  x=mod(x);
}
ll mod_inv(ll x){
  ll n=MOD;
  ll y = x,z = n-2, ret=1;
  y=mod(y);
  while (z>0){
    if(z%2==1) ret=mod(ret*y);
    y=mod(y*y);
    z/=2;
  }
  return ret;
}
ll mod_pow(ll x,ll a){
  ll y = x,z = a,ret=1;
  y=mod(y);
  if (z<0)z=-z;
  while (z>0){
    if(z%2==1) ret=mod(ret*y);
    y=mod(y*y);
    z/=2;
  }
  if (a<0) ret=mod_inv(ret);
  return ret;
}
v_ll mod_nck(ll n){
  v_ll nck(n+1,0);
  nck[0]=1;
  for (ll i=1;i<=n;i++){
    nck[i]=mod(nck[i-1]*(n+1-i));
    nck[i]=mod(nck[i]*mod_inv(i));
  }
  return nck;
}
v_ll mod_factorial(ll n){
  v_ll fct(n+1,0);
  fct[0]=1;
  for (ll i=1;i<=n;i++){
    fct[i]=mod(fct[i-1]*i);
  }
  return fct;
}

ll N,K,S;
v_ll A,B,C,D;
int main(){
  cin >> N >> K;
  S=0;
  A.assign(N+10,0);
  B.assign(N+10,0);
  C.assign(N+10,0);
  D.assign(N+10,0);
  for (ll i=1;i<=N;i++){
    cin >> A[i];
    S+=A[i];
  }
  ll ans_max=0;
  ll ans = 0;
  TEST=false;
  show(S,true);
  show(A);
  for (ll i=1;i*i<=S;i++){
    if (S%i!=0) continue;
    ll j=S/i;
    for (ll k=1;k<=N;k++){
      B[k]=(A[k]%i);
    }

    sort(B.begin()+1,B.begin()+N+1);

        show(B);
    for (ll k=1;k<=N;k++){
      C[k]=C[k-1]+B[k];
    }
    for (ll k=N;k>=1;k--){
      D[k]=D[k+1]+(i-B[k]);
    }

    show(C);
    show(D);
    for (ll k=1;k<=N-1;k++){

      ll t=(C[k]>D[k+1])?C[k]:D[k+1];
      if (t<=K) ans = i;
    }
    if (ans_max<ans) ans_max=ans;


    for (ll k=1;k<=N;k++){
      B[k]=A[k]%j;
    }
    sort(B.begin()+1,B.begin()+N+1);

    show(B);
    for (ll k=1;k<=N;k++){
      C[k]=C[k-1]+B[k];
    }
    for (ll k=N;k>=1;k--){
      D[k]=D[k+1]+(j-B[k]);
    }
    show(C);
    show(D);
    for (ll k=1;k<=N-1;k++){
      ll t=(C[k]>D[k+1])?C[k]:D[k+1];
      if (t<=K) ans = j;
    }
    if (ans_max<ans) ans_max=ans;

  }

  cout << ans_max << endl;
}


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

typedef long long ll;

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define AA(i) cout << i << endl;
#define AS(i) cout << i << " ";

const int INF = 1001001001;

const double PI = 3.14159;

using namespace std;

//vector<ll> memo(1000007, INF);

int main() {

    int n;
    cin >> n;

    

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

    sort(a, a+n);
    int max_a = a[n - 1];
    vector<int> dp(max_a+1, 0);

    rep(i, n) {
        for (int j = a[i]; j <= max_a; j += a[i]) {
            dp[j] += 1;
        }
    }

    int ans = 0;
    rep(i, n) {
        if(dp[a[i]] <= 1) {
            ans++;
        }
    }

    AA(ans);
}

Measured memory_bytes: 7936
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
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; }
using namespace std;

int main(){
    Init();
    ll a, b, c;
    cin >> a >> b >> c;
    (a == b && b == c && c == a) ? Yes() : No();
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i,n) cin >> a[i] >> b[i];
  ll ans = 0;
  for(int i = n-1; i>=0; i--) {
    ll x = (a[i]+ans)%b[i];
    if(x!=0) ans += b[i]-x; 
  }

  cout << ans << endl;
}

Measured memory_bytes: 1024
import java.util.Scanner;
public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		double num = sc.nextInt();

		if(num%2 == 0)
		{
			System.out.println(((num/2)/num));
		}
		else
		{
			System.out.println((((int)num/2+1))/num);
		}
	}
}

Measured memory_bytes: 22868
package main
 
import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
  "sort"
)
 
var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  var line, buffer []byte
  var isPrefix bool = true
  var err error
  for isPrefix {
    line, isPrefix, err = reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
  }
  return string(buffer)
}
func NextInt() int {
  var n int
  n, _ = strconv.Atoi(NextLine())
  return n
}
func NextIntVec() []int {
  L := strings.Split(NextLine(), " ")
  N := make([]int, len(L))
  for i := range N {
    N[i], _ = strconv.Atoi(L[i])
  }
  return N
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func Output() {
  _ = writer.Flush()
}

func Find (R *[]int, i int) int {
  if (*R)[i] == i { return i }
  (*R)[i] = Find(R, (*R)[i])
  return (*R)[i]
}

func Union (R *[]int, i, j int) {
  Ri := Find(R, i)
  Rj := Find(R, j)
  if Ri < Rj {
    (*R)[Rj] = Ri
  } else {
    (*R)[Ri] = Rj
  }
}

func Intersection(S1, S2 *[]int) int {
  cnt := 0
  i := 0
  j := 0
  for i < len((*S1)) && j < len((*S2)) {
    if (*S1)[i] < (*S2)[j] {
      i += 1
    } else if (*S1)[i] == (*S2)[j] {
      i += 1
      j += 1
      cnt += 1
    } else {
      j += 1
    }
  }
  return cnt
}

func main() {
  NM := NextIntVec()
  P := NextIntVec()
  R := make([]int, NM[0])
  for i := range R {
    R[i] = i
  }
  for i := 0; i < NM[1]; i++ {
    XY := NextIntVec()
    Union(&R, XY[0] - 1, XY[1] - 1)
  }
  S := make([][][]int, NM[0])
  for i := range S {
    S[i] = make([][]int, 2)
    S[i][0] = make([]int, 0)
    S[i][1] = make([]int, 0)
  }
  for i, _ := range R {
    Find(&R, i)
    S[R[i]][0] = append(S[R[i]][0], P[i] - 1)
    S[R[i]][1] = append(S[R[i]][1], i)
  }
  cnt := 0
  for i, s := range S {
    sort.Ints(S[i][0])
    cnt += Intersection(&s[0], &s[1])
  }
  fmt.Println(cnt)
}

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

int main(){
  int k,cnt=0;
  string s;
  string sub[25000];
  cin>>s>>k;

  for(int i=0;i<s.size();++i){

    for(int j=0;j<5;++j){
        if(i+j<s.size()){
            sub[cnt]=s.substr(i,j+1);
            cnt++;
        }
    }

  }

  sort(sub,sub+cnt);
  int pre=0;
  cnt=1;
  s=sub[0];
  while(cnt<k){
    for(int i=1;i<25000;++i){
        if(sub[pre+i]!=sub[pre]){
            s=sub[pre+i];
            cnt++;
            pre=pre+i;
            break;
        }
    }
  }
  cout << s << endl;
  return 0;
}


Measured memory_bytes: 1664
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int n, k, x, y, ans;
    cin >> n >> k >> x >> y;
    while (n > 0)
    {
        if (k > 0)
        {
            ans += x;
            k--;
        }
        else
        {
            ans += y;
        }
        n--;
    }
    cout << ans;
    /* code */
    return 0;
}


Measured memory_bytes: 256
n,k = map(int, input().split())
m = 1000000007
ans = []
for i in range(1,n+2):
    ans_min = (0+(i-1))*i*0.5
    ans_max = ((n+1-i)+n)*i*0.5
    ans.append(ans_max-ans_min+1)

ans_wa = [ans[0]]
for i in range(1,n+1):
    ans_wa.append(ans_wa[i-1]+ans[i])

if (k>=2):
    print(int((ans_wa[n]-ans_wa[k-2])%m))
else:
    print(int(ans_wa[n]%m))

Measured memory_bytes: 24624
#include <iostream>
using namespace std;
char colors[1001][1001];
#define rep(i,n) for(int i=0;i<n;++i)

int main()
{
    int h; cin >> h;
    int w; cin >> w;
    int d; cin >> d;

    rep (i, 1001) {
        char color1 = (((i / d) & 1LL) ? 'R' : 'Y');
        char color2 = (((i / d) & 1LL) ? 'G' : 'B');
        rep (j, 1001) {
            colors[i][j] = (((j / d) & 1LL) ? color1 : color2);
        }
    }

    int sx = 500;
    int sy = 0;

    rep (i, h) {
        int x = sx;
        int y = sy;
        rep (j, w) {
            cout << colors[x++][y++];
        }
        cout << endl;
        sx--;
        sy++;
    }

    return 0;
}


Measured memory_bytes: 1408
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0;i < (n);i++)
using namespace std;
  
int main () {
  vector<int> v;
  int p, ans;
  rep(i,5) {
    cin >>p;
    if(p % 10 == 0)
      ans += p;
    else {
      ans += p/10 * 10;
      v.push_back(p%10);
    }
  }
  sort(v.begin(),v.end());
  if(v.size() >= 1)
    ans += v.at(0) + (v.size()-1)*10;
  cout << ans;
}


Measured memory_bytes: 256
n = gets.chomp.to_i
t = Array.new()
n.times {|i|
  t << gets.split(' ').map {|m| m.to_i }
}
d = t.map {|t1|
  t.map {|t2|
    Math.sqrt(((t1[0] - t2[0]) ** 2) + (t1[1] - t2[1]) ** 2)
  }
}

p = 1
t.count.times {|i|
  p *= (i + 1)
}

puts d.sum(&:sum) * (p/n) / p


Measured memory_bytes: 14332
#include<cstdio>
#include<iostream>
using namespace std;
int dislike[10];

bool like(int n){
	while(n>0){
		if(dislike[n%10])
			return false;
		n/=10;
	}
	return true;
	}
int main()
{
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=k;++i){
		int x;
		cin>>x;
		dislike[x]=1;
		}
	while(!like(n)){
		++n;
		}
	cout<<n;
	return 0;
	}

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

int main(){
  int a,b;
  
  char s[100];

  scanf("%d%d",&a,&b);
  scanf("%s",s);

  for(int i=0;i<a+b+1;i++){
    if(i==a){
      if(s[i]!='-'){
	printf("No\n");
	return 0;
      }
    }
    else{
      if(s[i]=='-'){
	printf("No\n");
	return 0;
      }
    }
  }

  printf("Yes\n");
  return 0;
}


Measured memory_bytes: 128
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;

void main()
{
    int[] hw = readln().chomp().split(" ").map!(to!int).array();
    int h = hw[0],
        w = hw[1];
    int n = readln().chomp().to!int;
    auto as = readln().chomp().splitter(' ').map!(to!int).array();

    int cnt;
    int iy, ix;
    int di = 0;
    int[2][] dyx = [[0, 1], [1, 0], [0, -1], [-1, 0]];
    int[][] field = new int[][](h, w);
    foreach(int i, a; as){
        foreach(_; 0 .. a){
            field[iy][ix] = i+1;
            iy += dyx[di % $][0];
            ix += dyx[di % $][1];
            if(iy < 0 || ix < 0 || iy >= h || ix >= w || field[iy][ix] != 0){
                iy -= dyx[di % $][0];
                ix -= dyx[di % $][1];
                di += 1;
                iy += dyx[di % $][0];
                ix += dyx[di % $][1];
            }
        }

        ++cnt;
    }

    writefln("%(%(%s %)\n%)", field);
}

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

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

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;
}
vector<bool> seen;
void dfs(const Graph &G, int v){
    seen[v] = true; // vを訪問済みにする
    
    for(auto next_v: G[v]){
        if(seen[next_v]) continue; // next_vが探索済みならスルー
        dfs(G, next_v); // 再帰的に探索
    }
}

int main(){

    int s, w;
    cin >> s >> w;
    if(s <= w) cout << "unsafe" << endl;
    else cout << "safe" << endl;
    return 0;
}

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

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

    cout << 48 - M << endl;

    return 0;
}

Measured memory_bytes: 256
#define  REP(i, s, e) for(register int i = s; i <= e ;i++)
#define DREP(i, s, e) for(register int i = s; i >= e ;i--)

#define DEBUG fprintf(stderr, "Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__)
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

#include <bits/stdc++.h>

using namespace std;

template <typename T> T read() {
	T ans(0), p(1);
	char c = getchar();
	while (!isdigit(c)) {
		if (c == '-') p = -1;
		c = getchar();
	}
	while (isdigit(c)) {
		ans = ans * 10 + c - 48;
		c = getchar();
	}
	return ans * p;
}

int m, n, k;

int main() {
#ifdef CraZYali
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	n = read<int>();
	DREP(i, sqrt(n) + 5, 1)
		if (i * i <= n) {
			cout << i * i;
			return 0;
		}
	return 0;
}

Measured memory_bytes: 384
N,M=gets.split.map(&:to_i)
LR=M.times.map{gets.split.map(&:to_i)}

l,r=0, 10**5+1
LR.each do |ll,rr|
    l = ll if l < ll
    r = rr if r > rr
end

ans = r - l + 1
if ans < 0
    puts 0
else
    puts ans
end


Measured memory_bytes: 9596
s = input()
n = len(s)
count_R = 0
count_L = 0
ans = [0] * n
for i in range(n):
    if s[i] == "R":
        if count_L:
            su = count_R + count_L
            if su % 2:
                if count_L % 2:
                    ans[sakai] = su // 2 + 1
                    ans[sakai - 1] = su // 2
                else:
                    ans[sakai] = su // 2
                    ans[sakai - 1] = su // 2 + 1
            else:
                ans[sakai] = su // 2
                ans[sakai - 1] = su // 2
            count_R = 0
            count_L = 0
        count_R += 1
    else:
        if s[i - 1] == "R":
            sakai = i
        count_L += 1
if count_L:
    su = count_R + count_L
    if su % 2:
        if count_L % 2:
            ans[sakai] = su // 2 + 1
            ans[sakai - 1] = su // 2
        else:
            ans[sakai] = su // 2
            ans[sakai - 1] = su // 2 + 1
    else:
        ans[sakai] = su // 2
        ans[sakai - 1] = su // 2
    count_R = 0
    count_L = 0
print(" ".join(map(str, ans)))

Measured memory_bytes: 10484
#include "bits/stdc++.h"

using namespace std;
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; }

//using Matrix = vector< vector<int> >;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 30;
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
bool cmp(P a, P b) { return a.second < b.second; } //End sort (Interval scheduling problem)

struct UnionFind {
	vector<int> par;
	vector<int> rank;

	UnionFind(int n) {
		init(n);
	}

	void init(int n) {
		par.resize(n);
		rank.resize(n);

		for (int i = 0; i < n; i++) {
			par[i] = i;
			rank[i] = 0;
		}
	}

	int find(int x) {
		if (par[x] == x) {
			return x;
		} else {
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) return;

		if (rank[x] < rank[y]) {
			par[x] = y;
		} else {
			par[y] = x;
			if (rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

vector<pair<ll, int>> x, y;

//struct edge { int u, v; ll cost; };
using edge = pair<ll, P>;

vector<edge> es;
ll e_size;
int n;

int kruskal() {
	sort(es.begin(), es.end());
	UnionFind uf(n);
	ll res = 0;
	for (int i = 0; i < e_size; i++) {
		edge e;	e = es[i];
		int u = e.second.first, v = e.second.second;
		ll cost = e.first;
		if (!uf.same(u, v)) {
			uf.unite(u, v);
			res += cost;
		}
	}
	return res;
}

int main(void){
	cin>>n;
	for (int i = 0; i < n; i++) {
		ll a, b; cin>>a>>b;
		x.push_back({a, i});
		y.push_back({b, i});
	}

	sort(x.begin(), x.end());
	sort(y.begin(), y.end());

	for (int i = 0; i < n - 1; i++) {
		int u = x[i].second, v = x[i + 1].second;
		ll cost = abs(x[i].first - x[i + 1].first);
		es.push_back(edge(cost, P(u, v)));
		e_size++;
	}

	for (int i = 0; i < n - 1; i++) {
		int u = y[i].second, v = y[i + 1].second;
		ll cost = abs(y[i].first - y[i + 1].first);
		es.push_back(edge(cost, P(u, v)));
		e_size++;
	}

	cout<< kruskal() <<endl;
	
	return 0;
}

Measured memory_bytes: 9572
/* 全探索 */
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n) for (ll (i) = m ; (i) < (ll)(n) ; ++(i))
#define REP_REV(i, n) for (ll (i) = (ll)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (ll (i) = (ll)(n) - 1 ; (i) >= m ; --(i))

#define INF 2e9
#define INF_LL 1LL<<60
#define ll long long

typedef pair<ll, ll> P;

int n, A, B, C;
vector<int> l(8);

int dfs(int num, int a, int b, int c) {
    if(num == n) {
        if(a == 0 || b == 0 || c == 0) return INF;
        return abs(a - A) + abs(b - B) + abs(c - C);
    }

    /* 採用しない */
    int res = dfs(num+1, a, b, c);

    /* aに使う */
    if(a == 0) {
        res = min(res, dfs(num+1, a+l[num], b, c));
    } else {
        res = min(res, dfs(num+1, a+l[num], b, c) + 10);
    } 

    /* bに使う */
    if(b == 0) {
        res = min(res, dfs(num+1, a, b+l[num], c));
    } else {
        res = min(res, dfs(num+1, a, b+l[num], c) + 10);
    }

    /* cに使う */
    if(c == 0) {
        res = min(res, dfs(num+1, a, b, c+l[num]));
    } else {
        res = min(res, dfs(num+1, a, b, c+l[num]) + 10);
    }

    return res;
}

int main(){
    cin >> n >> A >> B >> C;
    REP(i, n) cin >> l[i];

    cout << dfs(0, 0, 0, 0) << endl;

}


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

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n,k;
  cin>>n>>k;
  vector<int>x(n,0),dp(n+1,INT_MAX);
  int i=0;
  for(;i<n;i++)
    cin>>x[i];
  dp[0]=0;  
  for(i=0;i<n;i++)
  {
    for(int j=1;j<=k;j++)
    {
      if(i+j<n)
        dp[i+j]=min(dp[i+j],dp[i]+abs(x[i]-x[i+j]));
    }
  }
  cout<<dp[n-1]<<"\n"; 
  return 0;
}


Measured memory_bytes: 1024
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


typedef ::std::int_fast64_t i64;
typedef ::std::uint_fast64_t u64;
typedef ::std::int_fast32_t i32;
typedef ::std::uint_fast32_t u32;

namespace tools {
  template <typename T>
  void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    vector.reserve(size);
    ::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
  }
  template <typename T>
  void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    ::tools::read(::std::cin, vector, size);
  }

  template <typename T, ::std::size_t N>
  void read(::std::istream& is, ::std::array<T, N>& array) {
    ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
  }
  template <typename T, ::std::size_t N>
  void read(::std::array<T, N>& array) {
    ::tools::read(::std::cin, array);
  }
}

#endif

// main.cpp

int main() {
  i64 N, K;
  std::cin >> N >> K;

  if (K > (N - 1) * (N - 2) / 2) {
    std::cout << -1 << std::endl;
    return 0;
  }

  std::cout << N * (N - 1) / 2 - K << std::endl;
  for (i64 i = 1; i < N; ++i) {
    std::cout << i << ' ' << N << std::endl;
  }
  for (i64 i = 1; K < (N - 1) * (N - 2) / 2 && i < N - 1; ++i) {
    for (i64 j = i + 1; K < (N - 1) * (N - 2) / 2 && j < N; ++j, ++K) {
      std::cout << i << ' ' << j << std::endl;
    }
  }
  return 0;
}

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

int main(void) {
  int a, b;
  scanf("%d %d", &a, &b);
  if (a + b == 15) puts("+");
  else if (a * b == 15) puts("*");
  else puts("x");

  return 0;
}

Measured memory_bytes: 128
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

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

  ll mn = LONG_LONG_MAX;
  
  for (int i = 0; i < 5; i++) {
    ll x;
    cin >> x;
    mn = min(mn, x);
  }

  ll div = (n - 1) / mn;
  cout << div + 5 << endl;
}


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

ll odd(ll a){
  ll p=a%4;
  if(p==1) return 1;
  else return 0;
}
 
ll f(ll a){
  if(a%2){
    return odd(a);
  }else{
    return odd(a-1)^a;
  }
}

int main(){
    ll a,b;
    //ll result;
    cin>>a>>b;
    ll x=f(a-1);
    ll y=f(b);
    ll result=x^y;
    /*
    result=a;
    for (size_t i = a+1; i <= b; i++)
    {
        result^=i;
    }
    */
    cout<<result<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(void){
    int A,B,C;
    cin>>A>>B>>C;
    int X=C;
    int Y=B/A;
    if (X<Y){
        cout<<X<<endl;
    }
    else{
        cout<<Y<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []

for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

S = sum(A)
memory = [0] * 100001
for num in A:
    memory[num] += 1

for i in range(Q):
    S += (C[i] - B[i]) * memory[B[i]]
    print(S)
    memory[C[i]] += memory[B[i]]
    memory[B[i]] = 0


Measured memory_bytes: 92536
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;
const int maxn=2e5+5;
typedef long long ll;
string str[maxn];
map<string,int>mp;
int main(){
    int n;
    cin>>n;
    int maxx=0;
    for(int i=1;i<=n;i++){
        cin>>str[i];
        mp[str[i]]++;
        maxx=max(maxx,mp[str[i]]);
    }
    map<string,int>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
        if(it->second==maxx){
            cout<<it->first<<endl;
        }
    }
    return 0;
}


Measured memory_bytes: 22272
#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
string a[514],b[514],r[514];
bool aru[514];
int main()
{
	memset(aru,false,sizeof(aru));
	int h,w,lx=514,hx=0,ly=514,hy=0;
	cin>>h>>w;
	rep(i,h) cin>>a[i];
	rep(i,h) b[i]=r[i]=string(w,'.');
	rep(i,h) rep(j,w){
		if(a[i][j]=='.') continue;
		lx=min(i,lx);
		hx=max(i,hx);
		ly=min(j,ly);
		hy=max(j,hy);
	}
	rep(i,h) rep(j,w){
		if(i<lx || i>hx || j<ly || j>hy){
			b[i][j]='#';
		}
		else{
			if(a[i][j]=='#') b[i][j]=r[i][j]='#';
			else{
				if(i==lx) r[i][j]='#';
				else{
					if(j%2==ly%2) r[i][j]='#';else b[i][j]='#';
				}
			}
		}
	}
	rep(i,h) cout<<b[i]<<endl;
	cout<<endl;
	rep(i,h) cout<<r[i]<<endl;
}


Measured memory_bytes: 1536