akhauriyash/Code-Regression huggingface dataset:
using System;

namespace Test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var nums = Array.ConvertAll(Console.ReadLine().Split(),int.Parse);
            var commands = Console.ReadLine().ToCharArray();

            var dice = new Dice(nums);
            for (int i = 0; i < commands.Length; i++)
            {
                switch (commands[i])
                {
                    case 'E':
                        dice.RollX(true);
                        break;
                    case 'N':
                        dice.RollY(true);
                        break;
                    case 'S':
                        dice.RollY(false);
                        break;
                    case 'W':
                        dice.RollX(false);
                        break;
                    default:
                        break;
                }
            }

            Console.WriteLine(dice.Front);
        }
    }

    class Dice
    {
        public int Front { get; private set; }
        public int Bottom { get; private set; }
        public int Right { get; private set; }
        public int Left { get; private set; }
        public int Top { get; private set; }
        public int Rear { get; private set; }

        public Dice(int[] label)
        {
            Front = label[0];
            Bottom = label[1];
            Right = label[2];
            Left = label[3];
            Top = label[4];
            Rear = label[5];
        }

        public void RollX(bool direction)
        {
            var temp = 0;
            if (direction)
            {
                temp = Front;
                Front = Left;
                Left = Rear;
                Rear = Right;
                Right = temp;
            }
            else
            {
                temp = Front;
                Front = Right;
                Right = Rear;
                Rear = Left;
                Left = temp;
            }
        }
        public void RollY(bool direction)
        {
            var temp = 0;
            if (direction)
            {
                temp = Front;
                Front = Bottom;
                Bottom = Rear;
                Rear = Top;
                Top = temp;
            }
            else
            {
                temp = Front;
                Front = Top;
                Top = Rear;
                Rear = Bottom;
                Bottom = temp;
            }
        }
        public void RollZ(bool direction)
        {
            var temp = 0;
            if (direction)
            {
                temp = Top;
                Top = Left;
                Left = Rear;
                Rear = Right;
                Right = temp;
            }
            else
            {
                temp = Top;
                Top = Right;
                Right = Bottom;
                Bottom = Left;
                Left = temp;
            }
        }
    }
}

Measured memory_bytes: 13816
#include <stdio.h>
#include <ctype.h>

#define SIZE 128

int main(void) {
  char integer[SIZE];
  int count = 1;
  int i = 0;

  fgets(integer, SIZE, stdin);
  
  while (integer[0] != '0') {
    printf("Case %d: %s", count, integer);
    
    count++;

    fgets(integer, SIZE, stdin);    
  }
  
  return 0;
}

Measured memory_bytes: 524
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
vector <int> a;

vector <int> calc(int x, int y){
    vector <int> ret = {x, y};
    for(int i = 0 ; i < n - 2 ; i++){
        ret.push_back(ret[(int)ret.size() - 2] ^ ret.back());
    }
    return ret;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    a.resize(n);
    for(auto &i : a) cin >> i;
    set <int> s(a.begin(), a.end());
    if(s.size() > 3) finish("No");
    for(auto &i : s){
        for(auto &j : s){
            vector <int> c = calc(i, j);
            map <int, int> cnt;
            for(auto &i : a) cnt[i]++;
            bool ok = 1;
            for(auto &i : c){
                if(cnt[i] == 0) ok = 0;
                cnt[i]--;
            }
            if(ok) finish("Yes");
        }
    }
    cout << "No" << endl;
}


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

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


Measured memory_bytes: 3596
#include<bits/stdc++.h>
using namespace std;
int a,b; 
int ans;
int main() {
	cin>>a>>b;
	if(a<=3) ans+=(4-a)*100000;
	if(b<=3) ans+=(4-b)*100000;
	if(ans==600000) ans+=400000;
	cout<<ans;
	return 0;
}

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

int main(){
  int A,B,c,d;
  c=0;
  cin >> A >> B;
  for(int i=0;i<B-A+1;i++){
    d=A+i;
    if(d/10000==d%10 && (d/1000)%10==(d/10)%10){
      c++;
    }
  }
  cout << c << endl;
}

Measured memory_bytes: 256
#include<iostream>

using namespace std;

int main() {
	int r;

	cin >> r;

	if (r < 1200) {
		cout << "ABC" << endl;
	}
	else if (r < 2800) {
		cout << "ARC" << endl;
	}
	else {
		cout << "AGC" << endl;
	}

	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define INF (int)1e9
#define endl "\n"
#define ll long long
#define ull unsigned long long

using namespace std;

vector<vector<int>> adj;
vector<int> vis;

void dfs(int i)
{
    if (vis[i])
        return;
    vis[i] = 1;
    for (int x : adj[i])
        dfs(x);
}

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

    adj.assign(n, {});
    vis.assign(n, 0);

    for (int i = 0; i < m; i++)
    {
        int p, q, r;
        cin >> p >> q >> r;
        --p;
        --q;
        adj[p].push_back(q);
        adj[q].push_back(p);
    }

    int ans = 0;

    for (int i = 0; i < n; i++)
    {
        if (!vis[i])
        {
            ans++;
            dfs(i);
        }
    }
    cout << ans << endl;
}

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

int main() {
  int N, A;
  
  // input
  cin >> N >> A;

  // 500 で割った余りを求める
  int amari = N  % 500;
  
  // あまりが 1 円玉の枚数以下なら成功
  if(amari <= A)
  	cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


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

N, A = MAP()
x = LIST()

X = max(max(x), A)

y = [0]*len(x)
for i in range(len(x)):
	y[i] = x[i]-A

dp = [[0]*(2*N*X+1) for _ in range(N+1)]  #dp[j][t] = y1...yj から0枚以上選んで合計がt-N*Xにするような選び方

for j in range(N+1):
	for t in range(-N*X, N*X+1):
		if j == 0 and t == 0:
			dp[j][t] = 1
		elif j >= 1 and (t-y[j-1] < -N*X or t-y[j-1] > N*X):
			dp[j][t] = dp[j-1][t]
		elif j >= 1 and -N*X <= t-y[j-1] <= N*X:
			dp[j][t] = dp[j-1][t] + dp[j-1][t-y[j-1]]
		else:
			dp[j][t] = 0

print(dp[N][0]-1)



Measured memory_bytes: 74240
#include <iostream>
#include <queue>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
using namespace std;

int main(){
    int n,u,k,v,src=0;
    cin >> n;
    int D[n],g[n][n];
    REP(i,n){
        D[i] = -1;
        REP(j,n){
            g[i][j] = 0;
        }    
    }
    REP(i,n){ 
        cin >> u >> k;
            REP(j,k){
                cin >> v;
                g[u-1][v-1] = 1;
            }
    }

    queue<int> Q;
    Q.push(src);
    D[src] = 0;
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
            REP(i,n){
                if(g[cur][i] == 1 && D[i] < 0){
                    D[i] = D[cur] + 1;
                    Q.push(i);
                }
            }
    }

    REP(i,n){
        cout << i+1 << " " << D[i] << endl;    
    }
}

Measured memory_bytes: 3096
#include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;
typedef long long ll;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define D()
#define INF 1000000000000
#define MOD 10000007
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define INITA(a,i,j,v) for(ll k=i;k<=j;k++){a[k]=v;}

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

    cout << string(w+2, '#') << endl;
    REP(i, h) {
        string s = "";
        REP(j, w) {
            char c;
            cin >> c;
            s += c;
        }
        cout << "#" << s << "#" << endl;
    }
    cout << string(w+2, '#') << endl;
    return 0;
}

Measured memory_bytes: 256
from collections import deque

k = int(input())
queue = [1, 2, 3, 4, 5, 6, 7, 8, 9]
queue = deque(queue)
cnt = 0
while cnt < k:
    cnt += 1
    a = queue.popleft()
    hitoketa = a % 10
    kouho = [hitoketa - 1, hitoketa, hitoketa + 1]
    for b in kouho:
        if b >= 0 and b <= 9:
            queue.append(10 * a + b)
print(a)


Measured memory_bytes: 12396
#include <iostream>
#include <cstdio>
#include <cmath>
#include <functional>

using namespace std;
typedef long long ll;
#define REP(i,n) for (ll i = 0; i < (n); ++i)

#define deg_to_rad(deg) (((deg)/360)*2*M_PI)

double continuous_binary_search(
    double lo, // 絶対 OK な値
    double hi, // [lo, hi] の間に最適値があるとする
    function<double(double)> is_ok, // OK かどうかを判定する
    double eps // 閾値
){
    while(hi - lo > eps){
        const double mid = (hi + lo) / 2.0;
        if(is_ok(mid)){
            lo = mid;
        }else{
            hi = mid;
        }
    }
    return lo;
}

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

    double ans = continuous_binary_search(
        0, 90,
        [&](double mid){
            const double rad = deg_to_rad(mid);
            const double rad2 = M_PI / 2 - rad;
            const double aheight = sin(rad) * a;
            const double bheight = sin(rad2) * b;
            double vol = 0;
            if(aheight >= bheight){
                const double aheight2 = bheight / cos(rad2);
                vol = aheight2 * a * b / 2.0;
            }else{
                const double y = aheight / tan(rad2);
                const double z = aheight / sin(rad2);
                vol = (
                    a * a * z / 2.0 + a * (a * cos(rad) + y) * (bheight - aheight)
                );
            }
            return (vol >= x);
        }, 1e-8
    );
    printf("%.8f\n", ans);
    return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
#define l long long
long long a[200010];
long long ans[200010];
int main()
{
    l n;
    cin>>n;
    /*vector<l>v(n+1);
    for(l i=2; i<=n; i++)
    {
        l t;
        cin>>t;
        v[t]++;
    }
    for(l i=1; i<=n; i++)
    {
        cout<<v[i]<<endl;
    }*/

   /* for(l i=2; i<=n; i++){cin>>a[i];}


    for(l i=2; i<=n; i++)
    {
        ans[a[i]]++;
    }
    for(l i=1; i<=n; i++)
    {
        cout<<ans[i]<<endl;
    }*/
    map<int,int>m;
    map<int,int>::iterator it;
    for(l i=2; i<=n; i++)
    {
        int t;
        cin>>t;
        m[t]++;
    }
   for(l i=1; i<=n; i++)
   {
       cout<<m[i]<<endl;
   }
}


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

#define long long long

using namespace std;

const int N = 1e5+5;

int n;
int l[N], r[N];
long ans, lz_l, lz_r;
multiset<long> L, R;

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d %d", l+i, r+i), r[i] -= l[i];
    for(int i = 1; i <= n; i++) {
        ans += l[i];
        L.emplace(l[i] + lz_l), R.emplace(l[i] - lz_r);
        while(*L.rbegin() - lz_l > *R.begin() + lz_r) {
            long l = *L.rbegin(), r = *R.begin();
            L.erase(L.find(l)), R.erase(R.find(r));
            L.emplace(r + lz_r + lz_l), R.emplace(l - lz_l - lz_r);
        }
        long pv = 0;
        while(!L.empty() && *L.begin() - lz_l - r[i+1] <= 0) {
            ans -= abs(*L.begin() - lz_l - pv) * L.size();
            pv = *L.begin() - lz_l;
            L.erase(L.begin());
        }
        ans -= abs(pv - r[i+1]) * L.size();
        lz_l += r[i+1], lz_r += r[i];
    }
    long m = L.size(), pv = 0;
    for(long x : L) {
        ans -= abs(x - lz_l - pv) * m--;
        pv = x - lz_l;
    }
    printf("%lld\n", ans);

    return 0;
}

Measured memory_bytes: 10368
#!/usr/bin/env python3

def main():
    [H, W] = list(map(int, input().split()))
    a = []
    for i in range(H):
        a.append(list(map(int, input().split())))

    moves = []

    for y in range(H):
        for x in range(W - 1):
            if a[y][x] % 2 == 1:
                a[y][x] -= 1
                a[y][x + 1] += 1
                moves.append((y + 1, x + 1, y + 1, x + 2))
        if y != H - 1:
            if a[y][W - 1] % 2 == 1:
                a[y][W - 1] -= 1
                a[y + 1][W - 1] += 1
                moves.append((y + 1, W, y + 2, W))


    print(len(moves))
    for v in moves:
        print('{} {} {} {}'.format(v[0], v[1], v[2], v[3]))


if __name__ == '__main__':
    main()



Measured memory_bytes: 27244
# coding: utf-8
n,k=map(int,input().split())
a=list(map(int,input().split()))
v=[0 for i in range(42)]
for i in range(n):
    t=a[i]
    for j in range(42):
        v[j]+=t%2
        t=t>>1

x=0
tmp=2**42
for i in range(42):
    tmp//=2
    if v[41-i]<(n-v[41-i]):
        if x+tmp>k:
            continue
        else:
            x+=tmp
ans=0
for i in range(n):
    ans+=x^a[i]
print(ans)

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


int main(){
    int n,k;
    cin>>n>>k;
    double ans=0;
    for(int i=1;i<=n;i++){
        double x=1.0/n;
        int tmp=i;
        while(tmp<k){
            x*=1.0/2;
            tmp*=2;
        }
        ans+=x;
    }
    printf("%.12lf\n",ans);
    return 0;
}

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

char str[100][100], a[102][102];
int main(){
	int h, w;
	scanf("%d%d", &h, &w);
	fill((char*)a, (char*)(a+102), '#');
	rep(i,h)scanf("%s", str[i]);
	rrep(i,h)rrep(j,w)a[i][j] = str[i-1][j-1];
	rep(i,h+2){
		rep(j,w+2)printf("%c", a[i][j]);
		printf("\n");
	}
	return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long LL;
const int INF = 1 << 25;
const LL MOD = 1000000007LL;
using namespace std;
typedef pair<int, int> P;
int T[100000];
int A[100000];
bool h1[100000];
bool h2[100000];
int main() {
	int N;
	cin >> N;
	rep(i, N) cin >> T[i];
	rep(i, N) cin >> A[i];
	h1[0] = 1;
	for (int i = 1; i < N; i++) {
		if (T[i - 1] < T[i]) h1[i] = 1;
	}
	h2[N - 1] = 1;
	for (int i = N - 2; i >= 0; i--) {
		if (A[i + 1] < A[i]) h2[i] = 1;
	}
	LL ans = 1;
	bool NG = 0;
	rep(i, N) {
		if (h1[i] && h2[i]) {
			if (T[i] != A[i]) {
				NG = 1;
				break;
			}
		}
		else if (h1[i]) {
			if (T[i] > A[i]) {
				NG = 1;
				break;
			}
		}
		else if (h2[i]) {
			if (A[i] > T[i]) {
				NG = 1;
				break;
			}
		}
		else {
			(ans *= min(A[i], T[i])) %= MOD;
		}
	}
	if (NG) {
		cout << 0 << endl;
	}
	else {
		cout << ans << endl;
	}
}

Measured memory_bytes: 1152
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    prev = 0
    sides = []
    for a in A:
        if a == prev:
            sides.append(a)
            prev = 0
        else:
            prev = a
        if len(sides) >= 2:
            break
    if len(sides) >= 2:
        print(sides[0] * sides[1])
    else:
        print(0)


if __name__ == "__main__":
    main()


Measured memory_bytes: 63344
N,D=map(int,input().split())
A=[]
cnt=0

for i in range(N):
    A+=list(map(int,input().split()))

for i in range(1,N+1):
    if (A[2*i-2]**2+A[2*i-1]**2)**(1/2)<=D:
        cnt+=1

print(cnt)


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

int main() {
	int N, A, B;
	string S;
	cin >> N >> A >> B >> S;
	int a = 0, b = 0;
	for (int i = 0; i < N; i++) {
		if (S[i] == 'a' && a + b < A + B) {
			cout << "Yes" << endl;
			a++;
		} else if (S[i] == 'b' && a + b < A + B && b < B) {
			cout << "Yes" << endl;
			b++;
		} else {
			cout << "No" << endl;
		}
	}
	return 0;
}


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


int n,k;
cin >>n>>k;
vector <int> p(n);

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

}


sort(p.begin(), p.end());
int a = 0;

for (int i =0;i<(k);++i){
  a += p[i];
}
cout<<a<<endl;
}

Measured memory_bytes: 3620
#include <cstdio>

int main()
{
  int n,a,b;
  scanf("%d%d%d",&n,&a,&b);
  int c=0;
  for(int i=0;i<n;i++){
    int t;
    scanf("%d",&t);
    if(t<a||b<=t){
      c++;
    }
  }
  printf("%d\n",c);
  return 0;
}


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

long long X, Y, i;

int main() {
  cin >> X >> Y;
  
  int ans = 0;
  do {
    ans++;
    X = X * 2;
  } while ( X <= Y );
  
  cout << ans << endl;
  
  return 0;
}

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

using ll  = long long;
using vl  = vector<ll>;
using vvl = vector<vl>;
#define rng(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define DEBUG(x) cerr<<__LINE__<<": "<<#x<<" = "<<x<<"\n"

const int inf = 1e9+10;
const int mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0) : x((x%mod+mod)%mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res *= a;
    }
    mint pow(ll t) const {
        if (t == 0) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint &operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res /= a;
    }
};
istream& operator>>(istream& i, mint& var) {
    ll tmp; i >> tmp;
    var.x = tmp;
    return i;
}
ostream& operator<<(ostream& o, const mint& var) {
    o << var.x;
    return o;
}

mint ans;
vector<mint> pow2;
int N;
vl size;
vvl edges;

void dfs(int v, int pre) {
    size[v] = 1;
    mint cnt = pow2[N - 1] - 1;
    for (auto to : edges[v]) {
        if (to == pre) continue;
        dfs(to, v);
        size[v] += size[to];
        cnt -= pow2[size[to]] - 1;
    }
    cnt -= pow2[N - size[v]] - 1;
    ans += cnt;
}

int main() {
    cin >> N;
    edges.resize(N);
    size.resize(N);
    pow2.assign(N+1, 1);
    rep(i,N-1) {
        int a, b; cin >> a >> b;
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    rep(i,N) pow2[i+1] = pow2[i] * 2;

    int root = 0;
    dfs(root, -1);
    ans /= pow2[N];
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 27392
#include<iostream>
#define N 10020
using namespace std;
int main(){
  while(true){
    int n;
    int ans=0;
    scanf("%d",&n);

    if(n==0)break;
    int row[N]={0,};
    int h[5]={0,};
    short M[5][N]={{0,},};
    
    for(int i = 0; i < n; ++i){
      int mh = 0;
      int d,p,q;
      scanf("%d%d%d", &d, &p, &q);
      --q;
      
      if(d==1){
	int maxh = 0;
	for(int j = q; j < q + p; ++j){
	  maxh = max( maxh, h[j] );
	}
	for(int j = q; j < q + p; ++j){
	  h[j]=maxh+1;
	  M[j][maxh]=1;
	}
	row[maxh]+=p;
      }else{
	for(int j = h[q]; j < p + h[q]; ++j){
	  ++row[ j ];
	  M[q][j]=1;
	}
	h[q]+=p;
      }

      for(int j = 0; j < 5; ++j){
	mh = max( mh , h[j] );
      }
      /*
      cout << " STACKED : " << endl;
      for(int j = mh; j >= 0; --j){
	for(int k = 0; k < 5; ++k){
	  cout << M[k][j];
	}
	cout << endl;
      }
      cout << endl;
      */
      for(int j = 0; j < mh+1; ++j){
	if( row[j] == 5 ){
	  for(int k = j; k < mh+2; ++k){
	    row[k] = row[k+1];
	    for(int l = 0; l < 5; ++l){
	      M[l][k] = M[l][k+1];
	    }
	  }
	  j = -1;
	}
      }
      /*
      cout << " ERASED : " << endl;
      for(int j = mh; j >= 0; --j){
	for(int k = 0; k < 5; ++k){
	  cout << M[k][j];
	}
	cout << endl;
      }
      cout << endl;
      */
      for(int j = 0; j < 5; ++j){
	h[j] = 0;
      }
      for(int j = 0; j < 5; ++j){
	for(int k = 0; k < mh+3; ++k){
	  if( M[j][k] == 1 ){
	    h[j] = k+1;
	  }
	}
      }
      /*
      cout << " HEIGHTS : " << endl;
      for(int j = 0; j < 5; ++j){
	cout << ' ' << h[j] << endl;
      }
      */
    }
    for(int i = 0; i < 5; ++i){
      for(int j = 0; j < N; ++j){
	if( M[i][j] )
	  ans++;
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}

Measured memory_bytes: 1016
// at coder beginner
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define  boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ull unsigned long long
#define deb(x) cout<<#x<<" "<<x<<endl;
#define deb2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define deb2l(x,y,i) cout<<#x<<i<<" "<<x<<" "<<#y<<i<<" "<<y<<endl;

#define fr(i,l,r) for(ll i=l;i<r;i++)
#define mems(a,x) memset(a,x,sizeof(a))
#define mod 1000000007
#define ff first
#define ss second
#define pb(x) push_back(x)
#define vll vector<ll>
#define pbp(x,y) push_back(make_pair(x,y))
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const ll  maxx=1e5+5;
ll distT[maxx],distA[maxx];
vll adj[maxx];
bool used[maxx];
void dfs(ll u,ll par,ll *dist){
    dist[u]=dist[par]+1;
    for(ll v:adj[u]){
        if(v==par)continue;
        dfs(v,u,dist);
    }
}
// find usable vertex by comparing distT(i)>distA(i)
void dfs2(ll u,ll par){
    used[u]=true;
    for(ll v:adj[u]){
        if(v==par)continue;
        if(distT[v]>=distA[v])continue;
        dfs2(v,u);
    }
}
void solve(){
ll n,u,v;cin>>n>>u>>v;
ll x,y;
fr(i,1,n)cin>>x>>y,adj[x].pb(y),adj[y].pb(x);
distT[0]=-1,distA[0]=-1;
dfs(u,0,distT);
dfs(v,0,distA);
mems(used,false);
dfs2(u,0);
ll ans=0;
for(ll i=1;i<=n;i++)
if(used[i]){
    ans=max(ans,distA[i]);
}
cout<<ans-1<<endl;
    
}
int main(){
    ll t=1;
//cin>>t;
    while(t--){

     solve();
    }
    return 0;
}


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

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll L[10];
//! [bitで表される竹を使って][長さiの竹を作るための]=MP
ll dp[300][5];
//! bitで表される竹を全て足した和
ll T[300];
int N, K[3];

int main()
{
    cin >> N >> K[0] >> K[1] >> K[2];
    int m = pow(2, N);
    REP(i, N)
    {
        cin >> L[i];
    }
    sort(L, L + N);
    REP(i, m)
    {
        ll sum = 0;
        REP(j, N)
        {
            if (((i >> j) & 1) == 1)
                sum += L[j];
        }
        T[i] = sum;
        REP(j, 3)
        {
            dp[i][j] = INF;
        }
    }

    FOR(i, m, 1)
    {
        int n = 0;
        REP(j, N)
        {
            if (((i >> j) & 1) == 1)
                n++;
        }
        REP(j, 3)
        {
            dp[i][j] = std::min(dp[i][j], 10 * (n - 1) + abs(T[i] - K[j]));
        }
    }

    ll min = INF;
    FOR(i, m, 1)
    {
        FOR(j, m, 1)
        {
            FOR(k, m, 1)
            {
                bool f = false;
                REP(l, N)
                {
                    int s = 0;
                    s = ((i >> l) & 1) + ((j >> l) & 1) + ((k >> l) & 1);
                    if (s > 1)
                    {
                        f = true;
                        break;
                    }
                }
                if (f)
                    continue;
                min = std::min(min, dp[i][0] + dp[j][1] + dp[k][2]);
            }
        }
    }
    cout << min << endl;
    return 0;
}

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

bool IsPrime(int num){
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false;
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2){
        if (num % i == 0)
        {
            return false;
        }
    }
    return true;
}
LL gcd(LL m, LL n){
    LL temp;
    while(n!=0){
    temp=n;
    n=m%n;
    m=temp;
  }
  return m;
}
LL lcm(LL m,LL n){
  return m*n/gcd(m,n);
}
int main(){
  LL A,B;
  LL C,D;
  cin>>A>>B>>C>>D;
  A--;
  A-=(A/C)+(A/D)-A/lcm(C,D);
  B-=(B/C)+(B/D)-B/lcm(C,D);
  cout<<B-A<<endl;
  return 0;
}

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

#define TARGET "slv"

using namespace std; 

using ll = long long;

int main() {
#ifdef INSPIRATION
    freopen(TARGET".in", "r", stdin);
    freopen(TARGET".out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);               
    int a, b, c;
    cin >> a >> b >> c;
    int L1 = min(c, b);
    int ans = 0;
    ans += 2*L1;
    b -= L1;
    c -= L1;
    if (c == 0) {
        cout << ans+b;
        exit(0);
    } else {
        assert(b == 0);
        int L2 = min(c, a);
        c -= L2;
        a -= L2;
        ans += L2;
        if (c > 0) {
            assert(a == 0);
            ans++;
            c--;
        }
        if (a > 0)
            assert(c == 0);
        cout << ans;
    }
    return false;
} 

Measured memory_bytes: 256
object Main extends App {
    var get = readLine
    var a = get.split("/")(0).toInt
    var b = get.split("/")(1).toInt
    var c = get.split("/")(2).toInt
  
	if(a<2020 && b < 5 && c < 31) {
      println("Heisei")
    } else {
      println("TBD")
    }
}

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

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

long long MOD = 1000000000 + 7;

int main(){
  cout << setprecision(10);
  int a; cin >> a;
  cout << a + a*a + a*a*a << endl;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define all(x) (x).begin(),(x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {

	string s;
	cin >> s;
	vi ans(s.size());

	int k = 0;
	rep(i, s.size()) {
		if (s[i] == 'R')k++;
		else {
			ans[i] += k / 2;
			ans[i - 1] += k - k / 2;
			k = 0;
		}
	}
	rep(i, s.size()) {
		if (s[s.size() - 1 - i] == 'L')k++;
		else {
			ans[s.size() - 1 - i] += k / 2;
			ans[s.size() - i] += k - k / 2;
			k = 0;
		}
	}

	rep(i, s.size()) {
		cout << ans[i];
		if (i < s.size())cout << " ";
		else cout << endl;
	}

}

Measured memory_bytes: 1024
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <cstdlib>
#include <climits>
#include <cstdio>
#include <vector>
#include <bitset>
#include <set>
#include <map>
#include <queue>
#include <stack>
#define PI 3.14159265
#define base 27
#define  fi first
#define  se second

#define TASK "Traveling"

using namespace std;
typedef long long lli;
typedef long double lb;
typedef unsigned long long ull;
typedef pair<int,int> pa;
const long long mod =1e9+7;
const long long oo=1e15+7;
const int maxn=1e6+1;
const int maxN=1e3+1;

/******************************************************************************
 *                                                                            *
 *                       Be a warrior not a worrier!! =)))                    *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/
 int n;
 int t[maxn],x[maxn],y[maxn];



void in()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen(TASK".inp","r",stdin);
    freopen(TASK".out","w",stdout);
}

void solve()
{
      cin>>n;
      for(int i=1;i<=n;i++)
      {
            cin>>t[i]>>x[i]>>y[i];
            if(t[i]<x[i]+y[i])
            {
                  cout<<"No";
                  exit(0);
            }
            else
            {
                  if(abs(x[i]-y[i])%2!=(t[i]%2))
                  {
                        cout<<"No";
                        exit(0);
                  }
            }
      }
      cout<<"Yes";


}

int main()
{
    //in();
    solve();

}


Measured memory_bytes: 8832
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;
#else
#define debug 0;
#endif

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template < template < typename ... > class Container, typename Value >
auto runlength(Container< Value > const& v) {
  auto n = (int)v.size();
  if (n == 0)
    { return Container< std::pair< Value, int > >{}; }
  auto pos = Container< int >{};
  for (auto i = 1; i < n; i++) {
    if (v.at(i - 1) != v.at(i))
      { pos.emplace_back(i); }
  }
  pos.emplace_back(n);
  auto m = (int)pos.size();
  auto len = Container< int >(m);
  std::adjacent_difference(pos.begin(), pos.end(), len.begin());
  auto ret = Container< std::pair< Value, int > >(m);
  for (auto i = 0; i != m; i++)
    { ret.at(i) = {v.at(pos.at(i) - 1), len.at(i)}; }
  return ret;
}

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n; std::cin >> n;
  std::vector< char > s(n);
  std::cin >> s;
  auto rle = runlength(s);
  std::cout << rle.size() << std::endl;
  return 0;
}


Measured memory_bytes: 2040
N, M, C = map(int, input().split())
B = list(input().split())
code = [input().split() for i in range(N)]
AC_num = 0

for i in range(N):
    tmp = 0
    for j in range(M):
        tmp += int(B[j])*int(code[i][j])
    tmp += C
    if tmp > 0:
        AC_num += 1
    else:
        continue
        
print(AC_num)

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

using namespace std;

long long a, b, c, d;

int main() {
  cin >> a >> b >> c >> d;
  long long p0, p1, p2, p3;
  p0 = a * c;
  p1 = a * d;
  p2 = b * c;
  p3 = b * d;
  cout << max(p0, max(p1, max(p2, p3)));
  return 0;
}

Measured memory_bytes: 3592
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a.sort(reverse = True)
b.sort(reverse = True)
a.append(-1)
b.append(-1)
ans = 1
mod = 10**9+7
count = 0
x, y = 0, 0
for i in range(n*m, 0, -1):
    x_f, y_f = 0, 0
    while i == a[x]:
        x += 1
        x_f += 1
    while i == b[y]:
        y += 1
        y_f += 1
    if x_f * y_f > 1:
        ans = 0
    elif x_f * y_f == 1:
        pass
    elif x_f == 1:
        ans *= y
    elif y_f == 1:
        ans *= x
    else:
        ans *= max(0, x*y-count)
    count += 1
    ans %= mod

print(ans)


Measured memory_bytes: 3188
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <numeric>
#include <climits>
#define ll long long
#define LIM 1000
using namespace std;
int main() {
  vector<bool> v = {false, false, false, false};
  int n;
  char c;
  cin >> n;
  for (int i = 0;i < n; i++) {
    cin >> c;
    if (c == 'P') {
      v[0] = true;
    } else if (c == 'W') {
      v[1] = true;
    } else if (c == 'G') {
      v[2] = true;
    } else if (c == 'Y') {
      v[3] = true;
    }
    if (v[0] == true && v[1] == true  && v[2] == true && v[3] == true) {
      cout << "Four" << endl;
      return 0;
    }
  }
  cout << "Three" << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int main(){
    int n,r;
    cin>>n>>r;
    if(n>=10) cout<<r<<endl;
    else cout<<r+100*(10-n)<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>
#define F first
#define S second
using namespace std;
int n, m, ans;
double k;
pair<int,int> a[100005];
int main() {
	int i;
	scanf("%d%d", &n, &m);
	for(i=0; i<m; i++) scanf("%d%d", &a[i].S, &a[i].F);
	sort(a, a+m);
	for(i=0; i<m; i++) {
		if(a[i].S>k) ans++, k = a[i].F - 0.5;
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 1024
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    ans = 0

    for i in range(n):
        beat_now = min(a[i], b[i])
        ans += beat_now
        beat_next = min(a[i + 1], b[i] - min(a[i], b[i]))
        ans += beat_next
        a[i + 1] -= beat_next

    print(ans)


resolve()

Measured memory_bytes: 18624
from math import sqrt, factorial

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

all_sum = 0

for i in range(N):
    xi, yi = xy[i]
    for j in range(i + 1, N):
        xj, yj = xy[j]
        all_sum += sqrt((xi - xj) ** 2 + (yi - yj) ** 2)

fN = factorial(N - 1)

all_sum /= fN * N
all_sum *= fN * 2

print(all_sum)


Measured memory_bytes: 3060
// Template by [thunder_blade]
// IIIT ALLAHABAD
// includes :)

#include <bits/stdc++.h>
using namespace std;
#define TEST  int test_case; cin>>test_case; while(test_case--)
#define all(v) (v).begin(),(v).end()
#define fi  first
#define se  second
#define pb push_back
#define mp make_pair
#define ll long long int
#define SPEED ios_base::sync_with_stdio(false);  cin.tie(0);  cout.tie(0);
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define pf(x) printf("%f\n",x)
#define ps(x) printf("%s\n",x)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define sf(x) scanf("%f",&x)
#define ss(x) scanf("%s",x)
#define pis(x) cout<<x<<" "//printf("%d ",x)
#define pls(x) printf("%lld ",x)
#define pfs(x) printf("%f ",x)
#define pss(x) printf("%s ",x)
#define FOR(i,a,b) for(i=a;i<b;i++)
#define mod 1000000009
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
ll power(ll a, ll n) {
	ll p = 1;
	while (n > 0) {
		if(n&1) {
			p = ((p%mod) * (a%mod))%mod;
		}
		n >>= 1;
		a = ((a%mod)*(a%mod))%mod;
	}
	return p%mod;
}
ll inf  = 1000;
const long long B = 100000007;
const long long C = 10010107;
const int N = 500005;
const int LN = 25;


int main()
{
    int n;
    si(n);
    int ans = INT_MAX;
    for(int i = 1 ;i < n; i++) {
        int x = n-i;
        int y = i;
        int an1 = 0;
        while(y > 0){
            an1 += (y%10);
            y /= 10;
        }
        while(x > 0){
            an1 += (x%10);
            x /= 10;
        }
        ans = min(ans, an1);
    }
    cout<<ans<<endl;
}








Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
	int i, W = 0, K = 0, max[3] = {0}, s;
	
	for (i = 0; i < 10; i++){
		scanf("%d", &s);
		
		if (max[0] < s){
			max[2] = max[1];
			max[1] = max[0];
			max[0] = s;
		}
		else if (max[1] < s){
			max[2] = max[1];
			max[1] = s;
		}
		else if (max[2] < s){
			max[2] = s;
		}
	}
	
	for (i = 0; i < 3; i++){
		W += max[i];
		max[i] = 0;
	}
	
	for (i = 0; i < 10; i++){
		scanf("%d", &s);
		
		if (max[0] < s){
			max[2] = max[1];
			max[1] = max[0];
			max[0] = s;
		}
		else if (max[1] < s){
			max[2] = max[1];
			max[1] = s;
		}
		else if (max[2] < s){
			max[2] = s;
		}
	}
	
	for (i = 0; i < 3; i++){
		K += max[i];
	}
	
	printf("%d %d\n", W, K);
	
	return (0);
}

Measured memory_bytes: 596
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int ret;
int main(){
  int n;cin>>n;
  int a[n];
  for(int i = 0; n > i; i++){
    cin>>a[i];
  }
  vector<int> tes;
  for(int i = 1; n-1 > i; i++){
    tes.clear();
    for(int j = -1; 2 > j; j++){
      tes.push_back(a[i+j]);
    }
    sort(tes.begin(),tes.end());
    if(tes[1]==a[i])ret++;
  }
  cout << ret << endl;
}

Measured memory_bytes: 256
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n, x, y = MI()

    def dist(a, b):
        return b - a

    ans = [0 for _ in range(n-1)]
    for i in range(1, n):
        for j in range(i+1, n+1):
            if i <= x and y <= j:
                d = dist(i, x) + 1 + dist(y, j)
            elif x < i < y:
                if x < j < y:
                    d = min(dist(i, j), dist(x, i) + 1 + dist(j, y))
                else:
                    d = min(dist(x, i)+1, dist(i, y)) + dist(y, j)
            elif x < j < y:
                d = min(dist(i, j), dist(i, x) + 1 + dist(j, y))
            else:
                d = dist(i, j)
            # print(i, j, d)
            ans[d-1] += 1
    print("\n".join(map(str, ans)))



if __name__ == '__main__':
    solve()


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

int main(void) {
    char* buffer = malloc(sizeof(char)*10);
    if (fgets(buffer, 10, stdin) == NULL) {
        return 1;
    }
    int new_len = atoi(buffer)*2;
    char* buffer2 = malloc(sizeof(char)*new_len);
    if(fgets(buffer2, new_len, stdin) != NULL) {
        if (strstr(buffer2, "Y") != NULL) {
            printf("Four");
        } else {
            printf("Three");
        }
    }
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

const string ANS = "6174";
const int N = 4;

int main(int argc, char const* argv[])
{
	string buf;
	int c;
	int l[N], s[N], res[N];
	bool na;
	while(cin >> buf){
		c = 0;
		na = true;
		if(buf == "0000") break;
		for(int i(0);i < N - 1;++i){
			na = na && (buf[i] == buf[i + 1]);
		}
		if(na) {
			cout << "NA" << endl;
			continue;
		}
		while(buf != ANS){
			for(int i = 0;i < N;++i){
				l[i] = s[i] = (int)(buf[i] - '0');
			}
			sort(l, l + N, greater<int>());
			sort(s, s + N);
			for(int i = N - 1;0 <= i;--i){
				res[i] = l[i] - s[i];
				if(res[i] < 0){
					--l[i - 1];
					res[i] += 10;
				}
			}
			for(int i = 0;i < N;++i){
				buf[i] = (char)(res[i] + '0');
			}
			++c;
		}
		cout << c << endl;
		
	}
	return 0;
}

Measured memory_bytes: 1212
import java.io.*;
import java.util.*;

class Main{
    public static void main(String[] args){
        solve();
    }

    public static void solve(){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        int b = 0;
        for(int i=0;i<n;i++){
        	a[i] = sc.nextInt();
        }
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<n;i++){
        	list.add(a[i]-(i+1));
        }
        Collections.sort(list);
        b = list.get(list.size()/2);
        long ans = 0;
        for(int i=0;i<n;i++){
        	ans += (long)Math.abs(a[i]-(b+i+1));
        }
        System.out.println(ans);
    }
}

Measured memory_bytes: 65996
#include <iostream>
#include <numeric>
#include <algorithm>
#include <vector>
#include <map>

int main()
{
    int64_t N;
    std::cin >> N;

    std::map<int64_t, int64_t> AM;
    int64_t s = 0;
    for (size_t n = 0; n < N; ++n)
    {
        int64_t A;
        std::cin >> A;

        ++AM[A];
        s += A;
    }

    int64_t Q;
    std::cin >> Q;

    for (size_t q = 0; q < Q; ++q)
    {
        int64_t B, C;
        std::cin >> B >> C;

        s = s  - (AM[B] * B) + (AM[B]) * C;

        AM[C] += AM[B];
        AM[B] = 0;
        std::cout << s << std::endl;
    }

    return 0;
}


Measured memory_bytes: 9500
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args){
		
		Scanner sc = new Scanner(System.in);
		
		while(true){
			int H = sc.nextInt();
			int W = sc.nextInt();
			
			if(H == 0 && W == 0){
				break;
			}
			
			char[][] input = new char[H][W];
			for(int i = 0; i < H; i++){
				char[] tmp = sc.next().toCharArray();
				for(int j = 0; j < W; j++){
					input[i][j] = tmp[j];
				}
			}
			
			int count = 0;
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					if (input[i][j] != '.') {
						count++;

						Queue<Integer> queue = new LinkedList<Integer>();
						queue.add(j + i * W);

						while (!queue.isEmpty()) {
							int k = queue.poll();
							final int x = k % W;
							final int y = k / W;
							final char in = input[y][x];

							input[y][x] = '.';

							if (x != 0 && in == input[y][x - 1]
									&& input[y][x - 1] != '.') {
								queue.add(y * W + (x - 1));
							}

							if (y != 0 && in == input[y - 1][x]
									&& input[y - 1][x] != '.') {
								queue.add((y - 1) * W + x);
							}

							if (x != W - 1 && in == input[y][x + 1]
									&& input[y][x + 1] != '.') {
								queue.add(y * W + (x + 1));
							}

							if (y != H - 1 && in == input[y + 1][x]
									&& input[y + 1][x] != '.') {
								queue.add((y + 1) * W + x);
							}

						}
					}
				}
			}
			
			System.out.println(count);
		}
		
		return;
	}
	
}

Measured memory_bytes: 12000
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
#include<string.h>
#include<math.h>

typedef int32_t i32;
typedef int64_t i64;

const i32 mod = 1000000007;

#define ADD(x, a) do{(x) += (a); if ((x) >= mod) (x) -= mod;} while (0)
#define SUB(x, a) do{(x) += mod - (a); if ((x) >= mod) (x) -= mod;} while (0)

i32 inv (i32 a) {
  i32 t = 1;
  while (a > 1) {
    t = (i64) t * (mod - mod / a) % mod;
    a = mod % a;
  }
  return t;
}

i32 mod_pow (i32 r, i32 n) {
  i32 t = 1;
  i32 s = r;
  while (n > 0) {
    if (n & 1) t = (i64) t * s % mod;
    s = (i64) s * s % mod;
    n >>= 1;
  }
  return t;
}

i32 *fact = NULL;
i32 *iFact = NULL;
void init_fact (const i32 n) {
  fact = (i32 *) calloc (n + 1, sizeof (i32));
  fact[0] = 1;
  for (i32 i = 1; i <= n; ++i) {
    fact[i] = (i64) i * fact[i - 1] % mod;
  }
  iFact = (i32 *) calloc (n + 1, sizeof (i32));
  iFact[n] = inv (fact[n]);
  for (i32 i = n - 1; i >= 0; --i) {
    iFact[i] = (i64) (i + 1) * iFact[i + 1] % mod;
  }
}

i32 perm (i32 n, i32 k) {
  if (!(0 <=k && k <= n)) return 0;
  return (i64) fact[n] * iFact[n - k] % mod;
}

i32 comb (i32 n, i32 k) {
  if (!(0 <= k && k <= n)) return 0;
  return (i64) fact[n] * iFact[k] % mod * iFact[n - k] % mod;
}

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))

const i32 f = 10000000;

i64 func (i64 n) {
  i64 sum = 0;
  while (n > 0) {
    sum += n / mod;
    n /= mod;
  }
  return sum;
}

i32 calc_fact (i32 n) {
  i32 res = 1;
  while (n > 0) {
    res = (i64) res * n % mod;
    n--;
  }
  return res;
}

i32 calc_comb (i64 n, i64 k) {
  if (func (n) - func (k) - func (n - k) > 0) {
    return 0;
  }
  i32 nu = (i64) mod_pow (mod - 1, n / mod) * calc_fact (n % mod) % mod;
  i32 de = (i64) mod_pow (mod - 1, k / mod) * calc_fact (k % mod) % mod * mod_pow (mod - 1, (n - k) / mod) % mod * calc_fact ((n - k) % mod) % mod;
  return (i64) nu * inv (de) % mod;
}

void run (void) {
  i64 n, m, k;
  scanf ("%" SCNi64 "%" SCNi64 "%" SCNi64, &n, &m, &k);
  //init_fact (n * m);
  i64 ans = 0;
  i32 c = calc_comb (n * m - 2, k - 2);
  for (i32 d = 1; d < m; ++d) {
    ans = (ans + (i64) c * d % mod * (m - d) % mod * n % mod * n) % mod;
  }
  for (i32 d = 1; d < n; ++d) {
    ans = (ans + (i64) c * d % mod * (n - d) % mod * m % mod * m) % mod;
  }
  printf ("%" PRIi64 "\n", ans);
}

void gen (void) {
  
}

int main (void) {
  run();
  return 0;
}


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

int main(){
  int x,cnt=0,ans=0;
  string s;
  cin>>x>>s;
  for(int i=0;i<x;i++){
    if(s[i]=='I') cnt++;
    else cnt--;
    ans=max(ans,cnt);
  }
  cout<<ans;
}


Measured memory_bytes: 256
fun main(args: Array<String>) {
    val (N, K) = listOfLong()
    var n0 = N % K
    val n1 = K - n0
    val ans = Math.min(n0, n1)
    println(ans)
}


fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map(String::toInt)
fun listOfLong() = listOfString().map(String::toLong)


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

#define MAX 100

int dp[MAX+1][MAX+1]; //乗算回数
int p[MAX+1]; 

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

    for(int i = 1; i <= n; i++){
        cin >> p[i-1] >> p[i];
    }

    for(int i = 1; i <= n; i++) dp[i][i] = 0;

    for(int l = 2; l <= n; l++){  //lは対象とする行列の数
        for(int i = 1; i + l - 1 <= n; i++){
            int j = i + l - 1; //iからjまでの最小乗算回数を見ていく
            dp[i][j] = (1 << 21); //INFTYで埋める
            for(int k = i; k <= j-1; k++){
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j]);
            }
        }
    }

    cout << dp[1][n] << endl;

    return 0;
}


Measured memory_bytes: 3116