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

using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define Get(t, i) get<(i)>((t))
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

const int inf = 1 << 25;
const ll INF = 1LL << 55;

struct edge{
  int to, weight;
  edge(){}
  edge(int to, int weight):to(to), weight(weight){}
};

struct Prim
{
  vector< vector<edge> > graph;
  Prim(int sz):graph(sz){}
  void add_edge(int u, int v, int weight)
  {
    graph[u].push_back(edge(v, weight));
    graph[v].push_back(edge(u, weight));
  }
  int build()
  {
    int total = 0;
    vector<bool> used(graph.size(), false);
    priority_queue< Pi, vector<Pi>, greater<Pi> > que;
    que.push(Pi(0, 0));
    while(!que.empty()) {
      Pi p = que.top(); que.pop();
      int curr = p.second, weight = p.first;
      if(used[curr]) continue;
      used[curr] = true;
      total += weight;
      for(int i = 0; i < graph[curr].size(); i++){
	edge& e = graph[curr][i];
	que.push(Pi(e.weight, e.to));
      }
    }
    return total;
  }
};


int main()
{
  int V, E;
  cin >> V >> E;
  Prim msp(V);
  while(E--) {
    int s, t, w;
    cin >> s >> t >> w;
    msp.add_edge(s, t, w);
  }
  cout << msp.build() << endl;
  return 0;
}

Measured memory_bytes: 7828
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

n = int(input())
a = [list(map(int, input().split())) for i in range(n)]

mask = 1<<n
group = [0]*mask
for i in range(mask):
    for j in range(n):
        for k in range(n):
            if (i>>j)&(i>>k)&1:
                group[i] += a[j][k]
    group[i] //= 2

DP = group[:]
for state in range(1, mask):
    j = (state-1)&state
    while j:
        k = state^j
        DP[state] = max(DP[state], DP[j]+DP[k])
        j = (j-1)&state

print(DP[-1])

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

int main() {
  string S;
  cin >> S;
  int N;
  N = S.length();
  bool check = true;
  for (int i = 0; i < (N - 1) / 2; i++) {
    if (S[i] != S[N-1-i]) {
      check = false;
      break;
    }
  }
  if (check) {
    for (int i = 0; i < (N - 1) / 2 / 2; i++) {
      if (S[i] != S[(N-1)/2-1-i] || S[(N-1)/2+1+i] != S[N-1-i]) {
        check = false;
        break;
      }
    }
  }
  if (check) {
    cout << "Yes";
  }
  else {
    cout << "No";
  }
}

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

int main() {
int a;
int b;
 cin>>a;
 cin>>b;
  
  if  (a*b%2==0){
  cout<<"Even"<<endl;
  }
  if (a*b%2==1){
  cout<<"Odd"<<endl;
  }
}

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

public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        int n = sc.nextInt();
        int s = (int) Math.sqrt(n) + 1;
        int [] dp = new int[n + 1];
        for (int x = 1; x <= s; x++) {
            for (int y = 1; y <= s; y++) {
                for (int z = 1; z <= s; z++) {
                    int temp = x * x + y * y + z * z + z * y + x * y + x * z;
                    if (temp > n) continue;
                    dp[temp]++;
                }
            }
        }
        for (int i = 1; i <= n; i++) out.println(dp[i]);
        out.close();
    }


    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }


    }

}

Measured memory_bytes: 34076
# Task Sceduling Problem
A1,A2,A3 = gets.chomp.split(" ").map(&:to_i)
cost = 0
A = []
A.push(A1)
A.push(A2)
A.push(A3)
A.sort!
for i in 0..2 do
    if i == 1 || i == 2
        cost = cost + (A[i] - A[i-1])
    end
end
puts cost

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

int main(){
    int N,X,L;
    cin >>  N >> X;
    int D = 0;
    int ans = 1;
    for (int i=1;i<=N;i++){
        cin >> L;
        D += L;
        if (D>X) break;
        else ans++;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
def solve():
    import numpy as np
    n = int(input())
    oc = [[] for _ in range(n)]
    prof = [[] for _ in range(n)]

    for i in range(n):
        oc[i] = list(map(int, input().split()))
    for i in range(n):
        prof[i] = list(map(int, input().split()))

    oc = np.array(oc)
    prof = np.array(prof)

    ans = -9999999999

    for a in range(1, 1 << 10):
        res = np.zeros(n, dtype=np.int64)
        for b in range(10):
            if a >> b & 1:
                res += oc[:, b]
        #print(res)
        tmp = 0
        for i, x in enumerate(res):
            tmp += prof[i, x]
        #print(tmp)
        ans = max(ans, tmp)
    print(ans)


solve()


Measured memory_bytes: 27352
#include<cstdio>
int main() {
	int N, A[2][100], sum=0, i;
	scanf("%d", &N);
	for (i = 0; i < N; i++) scanf("%d", &A[0][i]);
	for (i = 0; i < N; i++) scanf("%d", &A[1][i]);
	for (i = 1; i < N; i++) A[0][i] += A[0][i - 1];
	for (i = N - 2; i >= 0; i--)A[1][i] += A[1][i + 1];
	for (i = 0; i < N; i++)sum = sum < A[0][i] + A[1][i] ? A[0][i] + A[1][i] : sum;
	printf("%d", sum);
	return 0;
}

Measured memory_bytes: 128
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>

using namespace std;


void solve() {
    int N; cin >> N;
    vector<int> h(N);
    for (int i = 0; i < N; i++) cin >> h[i];

    vector<int> dp(N);

    dp[0] = 0;
    dp[1] = abs(h[0] - h[1]);

    for (int i = 2; i < N; i++) {
        dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]));
    }

    cout << dp[N-1] << endl;
}


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

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

ll N, a, b;
ll sum = 100000000000000;

void solve() {
    cin >> N;
    double root = sqrt(N);
    for(int i = 1; i <= root; i++) {
        if(N % i == 0) {
           sum = min(i + (N / i), sum);
        }
    }

    cout << sum-2 << endl;
    return; 
}

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


Measured memory_bytes: 256
using System.Linq;
using static System.Console;

namespace SelectionSort
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var n = int.Parse(ReadLine());
            var nums = ReadLine().Split(' ').Select(str => int.Parse(str)).ToArray();

            SelectionSort(nums, n);

        }

        public static void SelectionSort(int[] a, int n)
        {
            var count = 0;
            for (var i = 0; i < n; i++)
            {
                var minj = i;
                for (var j = i; j < n; j++)
                {
                    if (a[j] < a[minj]) minj = j;
                }

                if (i != minj)
                   {
                       var tmp = a[i];
                       a[i] = a[minj];
                       a[minj] = tmp;
                       count++;
                   }
               }
               WriteLine(string.Join(" ", a));
               WriteLine(count);
           }
           
       }
   }


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

const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}


int main() {
  
  int S;
  scanf("%d", &S);

  COMinit();

  if(S<3){ printf("0\n"); return 0; }

  int N_GROUP_MAX = S/3;
  int S_left;
  long long sum_group=0;

  for(int i=1;i<N_GROUP_MAX+1;i++){
    S_left = S-3*i;
    sum_group += COM(S_left+i-1, S_left);
    sum_group %= MOD;
  }

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

  return 0;
}




Measured memory_bytes: 15668
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;

int n,m,k;
char s[maxn],t[maxn];
int gcd(int a,int b){
	return (b>0)?gcd(b,a%b):a;
}
int main(){
	scanf("%d%d",&n,&m);
	scanf("%s",s);
	scanf("%s",t);
	k=gcd(n,m);
	if(k==1){
		if(s[0]==t[0]) printf("%lld\n", 1ll*n*m);
		else printf("-1\n");
	}else{
		int tn=n/k,tm=m/k;
		bool flag=false;
		for(int i=0,j=0;i<n&&j<m;i+=tn,j+=tm){
			if(s[i]!=t[j]){
				flag=true;
				break;
			}
		}
		if(flag) printf("-1\n");
		else printf("%lld\n",1ll*n*m/k);
	}
	return 0;
} 

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll; 
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 2e5 + 30, Mod = 1e9 + 7;
const ll SQ = 330;

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	ll a;
	string s;
	cin >> a >> s;
	vector<ll> vec;
	ll a1 = ll(s[0] - '0');
	ll a2 = ll(s[2] - '0');
	ll a3 = ll(s[3] - '0');
	ll num = a1 * 100 + a2 * 10 + a3;
	ll x = num;
	cout << a * x / 100;
}

Measured memory_bytes: 3648
package main

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

const (
	maxBufsize = 1000000
)

func inputs(lines int) []string {
	var strSlice []string

	rdr := bufio.NewReaderSize(os.Stdin, maxBufsize)

	for i := 0; i < lines; i++ {

		line := ReadLine(rdr)
		strSlice = append(strSlice, line)

	}

	return strSlice
}

// ReadLine is to read long line
func ReadLine(rdr *bufio.Reader) string {
	buf := make([]byte, 0, maxBufsize)
	for {
		l, p, e := rdr.ReadLine()

		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}

	return string(buf)
}

func main() {
	strSlice := inputs(2)

	tmp := strings.Split(strSlice[0], " ")
	tmp2 := strings.Split(strSlice[1], " ")

	var pos []int
	num, _ := strconv.Atoi(tmp[1])
	for i := 0; i < num; i++ {
		tmpPos, _ := strconv.Atoi(tmp2[i])
		pos = append(pos, tmpPos)
	}

	maxSize := len(pos)

	maxLength := pos[1] - pos[0]

	circumference, _ := strconv.Atoi(tmp[0])

	for i := 0; i <= maxSize-2; i++ {
		diff := pos[i+1] - pos[i]

		if diff > maxLength {
			maxLength = diff
		}
	}

	diff := circumference - pos[maxSize-1] + pos[0]

	if diff > maxLength {
		maxLength = diff
	}

	fmt.Println(circumference - maxLength)

}


Measured memory_bytes: 13312
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define rrep(i,n) for(int i=(int)n-1;i>=0;--i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T& a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U& u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U& u,const V... v){for(auto& e:u)fill_v<T>(e,v...);}

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

    string s;
    cin >> s;
    int n = s.size();

    stack<int> st;
    vector<P> ans;
    rep(i, n) {
        if (s[i] == '\\') {
            st.push(i);
        } else if (s[i] == '/') {
            if (!st.empty()) {
                int idx = st.top();
                st.pop();
                P p = P(idx, i - idx);
                while (ans.size() > 0 && p.first < ans.back().first) {
                    p.second += ans.back().second;
                    ans.pop_back();
                }
                ans.push_back(p);
            }
        }
    }
    int sum = 0;
    rep(i, ans.size()) sum += ans[i].second;
    cout << sum << endl;
    cout << ans.size();
    if (ans.size() > 0) {
        cout << " ";
        rep(i, ans.size()) cout << ans[i].second << (i == ans.size() - 1 ? '\n' : ' ');
    }
    else cout << endl;
}



Measured memory_bytes: 3296
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
ll x[MAX_N] = {}, y[MAX_N] = {};
int main() {
    ll n, q;
    cin >> n >> q;
    ll ans = (n-2)*(n-2);
    
    srep(i,2,n){
        x[i] = n;
        y[i] = n;
    }
    ll xx = n;
    ll yy = n;
    rep(_,q){
        ll t, z;
        cin >> t >> z;
        if(t == 1){
            if(z < xx){
                ans -= yy - 2;
                srep(i,z,xx) x[i] = yy;
                xx = z;
            }else{
                ans -= x[z] - 2;
            }
        }else{
            if(z < yy){
                ans -= xx - 2;
                srep(i,z,yy) y[i] = xx;
                yy = z;
            }else{
                ans -= y[z] - 2;
            }
        }
    }
    cout << ans << endl;
    return 0;
}




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


int main()
{
	int i, S, n;
	while (cin >> n) {
		i = 0;
		for (S = 1;S <= 9999;S++) {
			if (S/1000+(S-S/1000*1000)/100+(S-S/100*100)/10+S-S/10*10==n)  
				i++;
		}
		cout << i << endl;
	}
    return 0;
}


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

int main() {
    int N;
    cin >> N;
    bool ans = true;
    vector<vector<int>> process(N+1, vector<int>(3, 0));
    rep(i, 1, N+1){
        cin >> process[i][0] >> process[i][1] >> process[i][2];
    }
    rep(i, 0, N){
        int time = process[i+1][0] - process[i][0];
        int dist = abs(process[i+1][1] - process[i][1]) + abs(process[i+1][2] - process[i][2]);
        if(time%2 == dist%2 && dist <= time)continue;
        ans = false;
    }
    if(ans) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 5760
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int a,b;
    cin >> a >> b;
    int ans = max(a+b, max(a-b, a*b));
    cout << ans << endl;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main{
	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);

		String s = scan.next();
		String t = scan.next();

		if(s.equals("H")){
			if(t.equals("H")){
				System.out.println("H");
			}
			else{
				System.out.println("D");
			}
		}
		else{
			if(t.equals("H")){
				System.out.println("D");
			}
			else{
				System.out.println("H");
			}
		}
	}
}


Measured memory_bytes: 23380
#include <bits/stdc++.h>
using namespace std;
int main() {
    int ar[10000];
    int a1=0,a9=0,a7=0,a4=0;
    for(int i=0;i<4;i++){
        cin >> ar[i];
        if(ar[i]==1)
        a1++;
        if(ar[i]==9)
        a9++;
        if(ar[i]==7)
        a7++;
        if(ar[i]==4)
        a4++;
    }
     if(a1==1 && a9==1 && a7==1 && a4==1)
     cout << "YES" << endl;
     else
     cout << "NO" << endl;

   return 0;
}


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

int gcd(int x,int y){
    if(y == 0)return x;
    return gcd(y,x%y);
}

int main(){
    int w,h,c;
    cin>>w>>h>>c;
    cout<<(w/gcd(w,h))*(h/gcd(w,h))*c<<endl;
}


Measured memory_bytes: 3100
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
const int INF = 1e10;
const int MOD = 1e9 + 7;
#define dump(x)                             \
    if (dbg) {                              \
        cout << #x << " = " << (x) << endl; \
    }
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define P pair<int, int>
#define V vector<int>
#define S set<int>
#define itn int
bool dbg = false;
void solve() {
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int N;
    N = SZ(s);
    itn ans = 0;
    FOR(i, 0, N / 2) {
        if (s[i] != s[N - i - 1])
            ans++;
    }
    cout << ans << endl;
    solve();
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  int a, b;
  cin >> a >> b;
  int c, d;
  for(int i = 1; 1; i++){
    if(b == 1){
      d = 0;
      break;
    }
    c = a * i - i + 1;
    if(c >= b){
      d = i;
      break;
    }
  }
  
  cout << d;
}
      
      

Measured memory_bytes: 3628
#include <iostream>
#include<cstdio>
#include<stdio.h>
using namespace std;

int main() {
	int a, b;
	char c;
	while (true) {

		cin >> a;
		cin >> c;
		cin >> b;


		if (c == '?') {
			break;
		}

		switch (c)
		{
			case '+':
				cout << a + b << endl;
				break;

			case '-':
				cout << a - b << endl;
				break;

			case '*':
				cout << a * b << endl;
				break;

			case '/':
				cout << a / b << endl;
				break;

		default:
			break;
		}

	}


	return 0;
}

Measured memory_bytes: 3088
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <locale>
#include <iomanip>

#define nl '\n'

using namespace std;

int n, t, a, b, mn = 1001;

int main(){
    vector< pair<int, int> > v;

    cin >> n >> t;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        v.push_back(make_pair(a, b));
    }

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

    for(int i = 0; i < n; i++){
        if(v[i].first < mn && v[i].second <= t){
            mn = v[i].first;
        }
    }

    if(mn == 1001){
        cout << "TLE";
    } else{
        cout << mn;
    }

}

//g++ -std=c++14 comp.cpp -o b; ./b.exe
//29 - 30

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

int h, w;
int main()
{
  cin >> h >> w;
  for(int i = 0; i < h; i++){
    int dif = -1;
    for(int j = 0; j < w; j++){
      char sky;
      cin >> sky;
      if(sky == 'c') dif = 0;
      else if(dif >= 0) dif++;
      cout << dif;
      if(j == w - 1) cout << endl;
      else cout << " ";
    }
  }
  return 0;
}

Measured memory_bytes: 1164
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {
    double r;
    cin >> r;
    cout << fixed << setprecision(6) << M_PI*(r*r) << ' ' << M_PI*(2*r) << endl;
    return 0;
}


Measured memory_bytes: 3288
from collections import deque
n = int(input())
a = list(map(int,input().split()))
deq = deque()
for i in range(n):
    if (i+1)%2!=0:deq.append(a[i])
    else:deq.appendleft(a[i])
if n%2!=0:deq.reverse()
for i in range(n):print(deq[i],end=' ')
print()

Measured memory_bytes: 25532
#include <bits/stdc++.h>
using namespace std;
#define STREP(i, s) for (int(i) = 0; (s)[(i)]; (i)++)
#define REP(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++)
#define ALL(c) begin(c), end(c)
#define SIZE(c) int((c).size())
#define INF 0x3f3f3f3f
#define PB push_back
#define F first
#define S second

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

int n;
string s;
vi st(26), ans(26, INF);

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  REP(k, 1, n) {
    vi(26).swap(st);
    cin >> s;
    STREP(i, s) st[s[i]-'a']++;
    REP(i, 0, 25) ans[i] = min(ans[i], st[i]);
  }
  REP(i, 0, 25) REP(j, 1, ans[i]) cout << char('a' + i);
  cout << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
using ll = long long;
int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  for (int i = 0; i < n; i++){
    cin >> a[i] >> b[i] >> c[i];
  }
  vector<ll> dp1(n + 5), dp2(n + 5), dp3(n + 5);
  for (int i = 0; i < n; i++){
    dp1[i + 1] = max(dp2[i] + a[i], dp3[i] + a[i]);
    dp2[i + 1] = max(dp3[i] + b[i], dp1[i] + b[i]);
    dp3[i + 1] = max(dp1[i] + c[i], dp2[i] + c[i]);
  }

  cout << max({dp1[n], dp2[n], dp3[n]}) << "\n";
  return 0;
}

Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define ppii pair<pair<int,int>,pair<int,int>>
using namespace std;
 
int h,w;
vector<string> s;
 
void removeRect(char c){
    int minx = 50;
    int miny = 50;
    int maxx = 0;
    int maxy = 0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(s[i][j]==c){
                minx = min(minx,j);
                maxx = max(maxx,j);
                miny = min(miny,i);
                maxy = max(maxy,i);
            }
        }
    }
    bool f = true;
    for(int i=miny;i<=maxy;i++){
    	for(int j=minx;j<=maxx;j++){
    		if(s[i][j] != c && s[i][j] != '?') f=false;
    	}
    }
    //cout << cnt << " " << maxx-minx << " " << maxy-miny << endl;
    if(!f)return;
    for(int i=miny;i<=maxy;i++){
        for(int j=minx;j<=maxx;j++){
            s[i][j] = '?';
        }
    }
    return;
}
 
bool isEmpty(){
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(s[i][j] != '.' && s[i][j] != '?') return false;
        }
    }
    return true;
}
 
void show(){
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cout << s[i][j];
        }
        cout << endl;
    }
}
 
int main(){
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        s.clear();
        cin >> h >> w;
        for(int i=0;i<h;i++){
            string str;
            cin >> str;
            s.emplace_back(str);
        }
        int cnt=0;
        while(!isEmpty() && cnt<=25){
            char c;
            for(c='A';c<='Z';c++){
                //cout << c << endl;
                removeRect(c);
                //show();
            }
            cnt++;
        }
        if(isEmpty()){
            cout << "SAFE" << endl;
        } else {
            cout << "SUSPICIOUS" << endl;
        }
    }
     
    return 0;
}

Measured memory_bytes: 3144
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f3f3f3f3f
#define MAXN 500010
const int MOD=1e9+7;
typedef long long ll;
using namespace std;
char s[1010];
int main()
{
	while(cin >> s){
		int len = strlen(s), sum = 0;
		int flag=1;
		for(int i = 1; i < len; i++){
			if(s[i] != '9'){
				flag=0;
			}
		}
		if(flag){
			sum = (len-1)*9;
			sum += s[0]-'0';
		}
		else{
			sum = s[0]-'0'-1;
			sum += (len-1)*9;
		}
		cout << sum << endl;
	}
	return 0;
}

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

int main()
{
  int d,t,s;
cin >> d >> t >> s;
  int time = d/s;
  if( d%s != 0 ){
    time += 1;
  }
  if(t >= time){
    cout << "Yes";
  }
  else{
    cout << "No";
  }
    return 0;
}

Measured memory_bytes: 3624
#include <cstdio>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

const int MOD = 1000000007, INF = 1111111111; 
using namespace std;
typedef long long lint;

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;

	if (N < 1200) cout << "ABC" << "\n";
	else if (N < 2800) cout << "ARC" << "\n";
    else cout << "AGC" << "\n";

	return 0;
}


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

using namespace std;

#define REP(i,m) for(ll i=0; i<m; i++)
#define FOR(i,n,m) for(ll i=n; i<m; i++)
#define INF 1000000007
#define INFL (1LL << 60)
#define MOD 998244353
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define ll long long int
#define P pair<ll, ll>

int main(){
	int a, n, m, ans = 0;
	cin >> a >> n >> m;
	if(a==0) ans--;
	REP(i, 100){
		ll x = 1;
		REP(j, n){
			x *= (i+a);
			if(x > m){
				cout << ans << endl;
				return 0;
			}
		}
		int d = 0;
		while(x > 0){
			d += x%10;
			x /= 10;
		}
		if(i == d) ans++;
	}
	cout << ans << endl;
	return 0;
} 



Measured memory_bytes: 3116
process.stdin.resume();
process.stdin.setEncoding('utf8');
var lines = [];
var reader = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
reader.on('line', (line) => {
    lines.push(line);
});
reader.on('close', () => {
    // const [a, b, c, k] = lines[0].split(" ").map(e => parseInt(e));
    // //lines.shift();
    // const t = k - (a + b);
    // console.log( t <= 0 ? a : (a - t));

    // 組合せ
    const combination = (nums, k) => {
        let ans = [];
        if (nums.length < k) {
            return [];
        }
        if (k === 1) {
            for (let i = 0; i < nums.length; i++) {
                ans[i] = [nums[i]];
            }
        } else {
            for (let i = 0; i < nums.length - k + 1; i++) {
                let row = combination(nums.slice(i + 1), k - 1);
                for (let j = 0; j < row.length; j++) {
                    ans.push([nums[i]].concat(row[j]));
                }
            }
        }
        return ans;
    }

    const [N, M, X] = lines[0].split(" ").map(e => parseInt(e));
    lines.shift();
    const datas = lines.map(e => e.split(" ").map(e1 => parseInt(e1)));

    let list = [];
    for (let i = 1; i <= N; i++) {
        list = list.concat(combination(datas, i));
    }
    //console.log(list);

    const transpose = mtx => mtx[0].map((_, c) => mtx.map(r => r[c]));
    const list1 = list.map(e => {
        const sumlist = transpose(e);
        return sumlist.map(e => e.reduce((a, c) => a + c, 0));
    });
    //console.log(list1);

    const list2 = list1.filter(e => {
        const [c, ...algs] = e;
        return algs.every(e1 => e1 >= X);
    });
    
    if (list2.length === 0) {
        console.log(-1);
    } else {
        console.log(list2.map(e => e[0]).reduce((a, c) => a < c ? a : c));
    }
});

Measured memory_bytes: 37804
using System;
using System.Collections.Generic;
using System.Linq;
namespace AOJ {
  public class AOJ {
    public static void Main(string[] args) {
      var n = Console.ReadLine().Split(' ').ToList().Select(int.Parse).OrderBy(x => x).ToArray();
      Console.WriteLine("{0} {1} {2}", n[0] , n[1], n[2]);
    }
  }
}

Measured memory_bytes: 10120
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
#define loop(i,a,b) for(int i=a; i<b; i++)
#define INF 10000000000099ll
#define mod 1000000007

int solve(vector<int> arr, int n, int k)
{
    vector<int> dp(n+1);
    dp[1]=0;
    for(int i=2; i<=n; i++){
        dp[i]=INT_MAX;
        for(int j=1; j<=k; j++){
            if(j==i) break;
            dp[i]= min(dp[i], abs(arr[i]-arr[i-j])+dp[i-j]);
        }
    }
    return dp[n];
}

int main()
{
    FASTIO;
    int n,k;
    cin>>n>>k;
    vector<int> arr(n+1);
    for(int i=1; i<=n; i++) cin>>arr[i];
    cout<<solve(arr, n, k);
    return 0;
}





Measured memory_bytes: 4460
N = int(input())
input_list = sorted([int(i) for i in input().split()])

mod = 10 ** 9 + 7

if N % 2 == 1:
    if input_list[0] != 0:
        print(0)
    else:
        if N == 1:
            print(1)
        flag = 2
        limit = (N - 1) // 2
        for i in range(1, limit, 2):
            if input_list[i] == flag and input_list[i+1] == flag:
                flag += 2
                if i == limit-1:
                    print((2**limit) % mod)
            else:
                print(0)
                break
elif N % 2 == 0:
    flag = 1
    limit = N
    for i in range(0, limit, 2):
        if input_list[i] == flag and input_list[i+1] == flag:
            flag += 2
            if i == limit-2:
                print((2**(N//2)) % mod)
                break
        else:
            print(0)
            break

Measured memory_bytes: 13880
N = int(input())
T = list(map(int, input().split()))
M = int(input())
P, X = [], []
for _ in range(M):
    p, x = map(int, input().split())
    P.append(p)
    X.append(x)
for i in range(M):
    S = T[:P[i]-1] + T[P[i]:]
    print(X[i] + sum(S))


Measured memory_bytes: 3060
using System;
using System.Collections.Generic;

namespace ITP1_7_B
{
	class Program
	{
		static void Main(string[] args)
		{
			List<Tuple<int, int>> ts = new List<Tuple<int, int>>();

			// 入力
			Input(ts);

			// 出力
			Output(ts);
		}

		static void Input(List<Tuple<int, int>> ts)
		{
			while(true) {
				string[] vs = Console.ReadLine().Split(' ');
				int n = int.Parse(vs[0]);
				int x = int.Parse(vs[1]);

				if(n == 0 && x == 0) {
					break;
				}

				ts.Add(Tuple.Create(n, x));
			}
		}

		static void Output(List<Tuple<int, int>> ts)
		{
			foreach(Tuple<int, int> t in ts) {
				int cnt = 0;

				for(int i = 1; i <= t.Item1; i++) {
					for(int j = i + 1; j <= t.Item1; j++) {
						for(int k = j + 1; k <= t.Item1; k++) {
							if(k == i || k == j) {
								continue;
							}

							if(i + j + k == t.Item2) {
								cnt++;
							}
						}
					}
				}

				Console.WriteLine(cnt);
			}
		}
	}
}



Measured memory_bytes: 17980
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
import collections
import itertools
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

a, b = map(int, input().split())

area = a*b
perimeter = 2*a+2*b

print(area, perimeter)





Measured memory_bytes: 6872
import math
a, b, C = map(int, input().split())
rad = math.radians(C)
S = (a * b * math.sin(rad)) / 2
L = a + b + (a * a + b * b - 2 * a * b * math.cos(rad)) ** 0.5
h = 2 * S / a
print("%.10f %.10f %.10f" %(S, L, h))



Measured memory_bytes: 5728
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5+10 , M = 5e5+10 ,  MOD = 1e9+7;
const ll OO = 0x3f3f3f3f;
const double EPS = 1e-3;
#define M_PI 3.14159265358979323846
#define clr(arr,val) memset(arr , val , sizeof (arr))
#define loop(i,n) for(int i = 0;i < int(n);i++)
#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)
#define range(i,a,b) for(int i = int(a);i <= int(b);i++)
#define pb push_back
#define F first
#define S second
#define all(v) ((v).begin()) , ((v).end())
#define sz(v)  ((int)((v).size()))
typedef pair <int,int> pii ;
typedef vector < pii > vii ;
typedef vector < int > vi;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
 




int r;

    
int main(){
    
 
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
 
    

    scanf("%d",&r);
    printf("%d\n",r*r);
    
 

    




    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
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;}
template<class T> inline void prn(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << ' ';}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvv(vector<vector<T>>& v) {for (auto u : v) printv(u);}
template<class T> inline void printlnv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << endl;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    int x, n;
    cin >> x >> n;
    vi p(n);
    bool ok = true;
    REP(i, n) {
        cin >> p[i];
        if (p[i] == x) ok = false;
    }
    if (ok) {
        cout << x << endl;
        return;
    }
    REP1(i, 1000) {
        auto i1 = find(ALL(p), x - i);
        if (i1 == p.end()) {
            cout << x - i << endl;
            return;
        }
        auto i2 = find(ALL(p), x + i);
        if (i2 == p.end()) {
            cout << x + i << endl;
            return;
        }
    }

}


Measured memory_bytes: 3648
#include <bits/stdc++.h>
#define ll long long
#define vi vector <int>
#define vl vector <ll>
#define dl pair <ll, ll>
#define ii pair <ll, ll>
#define vii vector <ii>
#define F first
#define S second
#define tipo pair <ll, pair <ll, set <int> > >
#define REP(a, b) for (int i = a; i < b; i++)
#define REP2(a, b) for (int j = a; j < b; j++)
#define REPS(a, b, c) for (int i = a; i < b; i += c)

using namespace std;

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	int a;
	cin >> a;
	cout << !a << "\n";
	return 0;
}

Measured memory_bytes: 3612
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#define ll long long
using namespace std;
int main(){
	double w,h,x,y;
	cin>>w>>h>>x>>y;
	int flag=0;
	double area=w*h/2;
	if(x==w/2 && y==h/2){
		flag=1;
	}
//	if(w%2==0 || h%2==0){
//		if(x==w/2 && y!=h/2){
//			flag=1;
//		}
//		if(x!=w/2 && y==h/2){
//			flag=1;
//		}
//	}
	printf("%.6lf",area);
	cout<<" "<<flag<<endl; 
    return 0;
}

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

int main(){
	int n;
	cin>>n;
	if((n%100-n%10)==90){
		cout<<"Yes"<<endl;
		return 0;
	}
	if(n%10==9)cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}

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

int solve(int N,string c){
  int cnt = 0;
  int j=N-1; //後ろのカーソル
  for(int i=0;i<j;i++){
    if(c[i] == 'W'){
      for(;;j--){
        if(i == j)
          return cnt;
        else if(c[j] == 'R'){
          cnt++; j--;//入れ替え
          break;
        }
      }
    }
  }
  return cnt;
}

int main(){
  
  int N;
  string c;
  cin >> N >> c;

  cout << solve(N,c);
  return 0;
}

Measured memory_bytes: 3924
N = input()

str_list = list(N)

l = ["U", "D"]

r = "Yes"
for i, s in enumerate(str_list):

    if i == 0 or (i+1) % 2 != 0:
        if s == "R":
            continue
    else:
        if s == "L":
            continue

    if s not in l:
        r = "No"
        break

print(r)

Measured memory_bytes: 2940
N = int(input())

ans = 0
A = []
B = []
for i in range(N):
    si = input()
    for j in range(len(si)-1):
        if si[j] == 'A' and si[j+1] == 'B':
            ans += 1
    if si[0] == 'B':
        B.append(i)
    if si[len(si)-1] == 'A':
        A.append(i)
    
if len(A) != 0 and len(A) == len(B) == len(set(A)&set(B)):
    print(ans+len(A)-1)
else:
    print(ans+min(len(A),len(B)))

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

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

int main() {
	ios::sync_with_stdio(false);
	
	int n, m, k;
	int c[2][200];
	cin >> n >> m;
	
	rep(i, 2*n) c[0][i] = i;
	
	rep(i, m) {
		cin >> k;
		if(!k) {
			rep(j, n) {
				c[(i+1)%2][2*j] = c[i%2][j];
				c[(i+1)%2][2*j+1] = c[i%2][n+j];
			}
		}else {
			repi(j, k, 2*n) {
				c[(i+1)%2][j-k] = c[i%2][j];
			}
			rep(j, k) {
				c[(i+1)%2][2*n-k+j] = c[i%2][j];
			}
		}
	}
	
	rep(i, 2*n) cout << (c[m%2][i]+1) << endl;
	
	return 0;
}

Measured memory_bytes: 1192
//In The Name Of God
#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define L(x) (2*(x))
#define R(x) ((2*(x))+1)
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int N = 1e5 + 5, Mod = 1e9 + 7, P = 727, Lg = 22, TOF = 1900;

int Dp[N][2];
vector< int > A[N];

void Dfs(int u, int par){
      int cnt = 0;
      Dp[u][0] = Dp[u][1] = -1;
      for(int i = 0; i < (int)A[u].size(); i++){
            if(A[u][i] == par)
                  continue;
            int v = A[u][i];
            Dfs(v, u);
            if(Dp[u][0] == -1)
                  Dp[u][0] = (Dp[v][0] + Dp[v][1]) % Mod;
            else
                  Dp[u][0] = 1ll * Dp[u][0] * ((Dp[v][0] + Dp[v][1]) % Mod) % Mod;
            if(Dp[u][1] == -1)
                  Dp[u][1] = Dp[v][0];
            else
                  Dp[u][1] = 1ll * Dp[u][1] * Dp[v][0] % Mod;
      }
      if(Dp[u][0] == -1)
            Dp[u][0] = Dp[u][1] = 1;
}
int main(){
      ios_base::sync_with_stdio(0); cin.tie(0);
      int n; cin >> n;
      for(int i = 1; i < n; i++){
            int x, y; cin >> x >> y;
            A[x].pb(y), A[y].pb(x);
      }
      Dfs(1, 1);
      cout << (Dp[1][0] + Dp[1][1]) % Mod;
      return 0;
}



Measured memory_bytes: 13440
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007
#define llMAX 9223372036854775807
#define llMIN -9223372036854775808
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,k,x,y;

//2つの要素をまとめてソート、前の変数が優先。flag=0で小さい順、1で大きい順
void mysort2(vi &a,vi &b,ll flag){
  vector<pair<ll, ll>> p(a.size());
  REP(i,n){
    p[i].first=a[i];
    p[i].second=b[i];
  }
  sort(p.begin(),p.end());
  if (flag==1){
    reverse(p.begin(),p.end());
  }
  REP(i,n){
    a[i]=p[i].first;
    b[i]=p[i].second;
  }
  return;
}




int main(){
  ll ans=0;
  cin >> n ;
  vi a(n),b(n),c(n),d(n),e(n);
  REP(i,n){
    cin>>a[i];
    b[i]=i;
  }
  mysort2(a,b,1);
  ll r=n-1,l=0;

  REP(i,n){
    c[i]=a[i];
    d[i]=b[i];
  }


  for(ll i=0;i<n;i++){
    if (abs(r-b[i])>abs(b[i]-l)){
      ans+=abs(r-b[i])*a[i];
      c[r]=a[i];
      d[r]=b[i];
      r--;
    }else{
      ans+=abs(b[i]-l)*a[i];
      c[l]=a[i];
      d[l]=b[i];
      l++;
    }
  }




/*
  REP(i,n){
    cout<<c[i]<<"("<<d[i]<<")";
  }
  cout<<endl;
*/

  for(ll xxx=0;xxx<200;xxx++){


          r=n-1;l=0;
          for(ll i=0;i<n;i++){
            //cout<<c[r]<<" "<<c[l]<<endl;
            if (c[r]>c[l]){
              ll gk=c[r]*( abs(d[r]-l)-abs(d[r]-r) );
              //1こ右にずらす
              for(ll j=l;j<r;j++){
                gk+=c[j]*( abs(d[j]-(j+1))-abs(d[j]-j) );
              }
              if (gk>0){
                gk=c[r];
                ll dgk=d[r];
                //1こ右にずらす
                for(ll j=r-1;j>=l;j--){
                  c[j+1]=c[j];
                  d[j+1]=d[j];
                }
                c[l]=gk;
                d[l]=dgk;
                l++;
              }else{
                r--;
              }
              
            }else{

              ll gk=c[l]*( abs(d[l]-r)-abs(d[l]-l) );
              //1こ左にずらす
              for(ll j=l+1;j<=r;j++){
                gk+=c[j]*( abs(d[j]-(j-1))-abs(d[j]-j) );
              }
              if (gk>0){
                gk=c[l];
                ll dgk=d[l];
                //1こ左にずらす
                for(ll j=l;j<r;j++){
                  c[j]=c[j+1];
                  d[j]=d[j+1];
                }
                c[r]=gk;
                d[r]=dgk;
                r--;
              }else{
                l++;
              }
            }

          }



  }









/*
  REP(i,n){
    cout<<c[i]<<"("<<d[i]<<")";
  }
*/

  ans=0;
  REP(i,n){
    ans+=c[i]*abs(d[i]-i);
  }
  cout<<ans<<endl;
  return 0;
}

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

template <typename InputIterator>
typename InputIterator::value_type
summation(InputIterator first, InputIterator last) {
  using T = typename InputIterator::value_type;
  T *p = new T();
  const auto sum = std::accumulate(first, last, *p);
  delete p;
  return sum;
}
template <typename T>
std::istream& operator>> (std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream& operator>> (std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream& operator>> (std::istream &stream, std::vector<T> &v) {
  for (auto &i : v) {
    stream >> i;
  }
  return stream;
}
template <typename T1, typename T2>
std::istream& operator>> (std::istream &stream, std::pair<T1, T2> &p) {
  stream >> p.first >> p.second;
  return stream;
}

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

  int64_t i = std::ceil(std::sqrt(n)) + 1;
  while (true) {
    if (n < i * i) {
    } else {
      if (n % i == 0) {
        const auto j = n / i;
        std::cout << std::to_string(j).size() << std::endl;
        return 0;
      }
    }
    i--;
  }
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int N, K;
int ans;
vector<vector<int>> rev;

// int dfs(int v, int prev){
//     int depth = 0;
//     for(int to : rev[v]){
//         if(to == prev) continue;
//         depth = max(depth, 1 + dfs(to, v));
//     }
//     if(depth == K-1 && prev != 0){
//         ans++;
//         return 0;
//     }
//     return depth;
// }

int dfs(int pos, int pre) {
	int e = 0;
	for (int i : rev[pos]) {
		if (i == pre) continue;
		e = max(e, dfs(i, pos));
	}
	if (e == K - 1) {
		if(pre != 0) ans++;
		return 0;
	}
	return e + 1;
}

int main(){
    cin >> N >> K;

    ans = 0;
    rev.assign(N, {});
    for(int i=0;i<N;i++){
        int a;
        cin >> a;
        if(i == 0){
            ans += a != 1 ? 1 : 0;
        }else{
            rev[a-1].push_back(i);
        }
    }

    for (int i : rev[0]) dfs(i, 0);
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 10368
#include <bits/stdc++.h>
#define eps 1e-3
#define pi acos(-1.0)
#define inf 0x3f
#define INF 1e18
#define pb push_back
#define debug1 cout<<"&&";
#define debug2 cout<<"**";
#define ms(a, x) memset(a, x, sizeof(a))
#define for0(i, n) for(int i = 0; i < n; ++i)
#define for1(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef pair<long long, long long> pll;
const int mod = 1e9 + 7;
const int N = 310;
const int M = 1e6+10;
/*=================================================================================*/

int cnt[10], a, n;
db dp[N][N][N];

db dfs(int a, int b, int c) {
   if(!a && !b && !c) return 0;
   if(dp[a][b][c]) return dp[a][b][c];
   db sum = a+b+c;
   db ans = 1.0*n / sum;
   if(a) ans += 1.0*a/sum * dfs(a-1, b, c);
   if(b) ans += 1.0*b/sum * dfs(a+1, b-1, c);
   if(c) ans += 1.0*c/sum * dfs(a, b+1, c-1);
   dp[a][b][c] = ans;
   return ans;
}

int main() {
   cin>>n;
   for1(i, n) {
      scanf("%d", &a);
      cnt[a]++;
   }
   db ans = dfs(cnt[1], cnt[2], cnt[3]);
   printf("%.8lf\n", ans);
   return 0;
}

Measured memory_bytes: 114388
//
#if 0

#endif // 0
#include<iostream>
#define MAX 100
using namespace std;
int matri[MAX][MAX];

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            matri[i][j]=0;
    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int m;
            cin>>m;
            matri[u-1][m-1]=1;
        }
    }
    for(int i=0;i<n;i++)
    {
        int j;
        for(j=0;j<n-1;j++)
            cout<<matri[i][j]<<" ";
        cout<<matri[i][j]<<endl;
    }
    return 0;
}



Measured memory_bytes: 3104
#include <bits/stdc++.h>
#define ll long long int
#define inf 3e18+1
#define all(v) (v).begin(),(v).end() 
#define N 200005
#define F first 
#define S second 
using namespace std;
ll mod=1e9+7;
// ll parent[N];  
// ll size[N]; 
// ll find(ll i){
//     if(parent[i]!=i) {
//         parent[i]=find(parent[i]);
//     }
//     return parent[i];
// }
// void Union(ll x,ll y){
//     ll a=find(x);
//     ll b=find(y);
//     if(a==b) return;
//     parent[a]=parent[b];
// }
int main()
{
    // added the two lines below 
    ios_base::sync_with_stdio (false); 
    cin.tie(NULL);
    cout.tie(0);    
    ll test=1;//cin>>test;
    while(test--){
        ll n;
        cin>>n;
        vector<ll>v(n);
        for(int i=0;i<n;i++){
            cin>>v[i];
        }
        ll dp[n];
        dp[0]=0;
        dp[1]=abs(v[0]-v[1]);
        for(int i=2;i<n;i++){
            dp[i]=min(dp[i-1]+abs(v[i]-v[i-1]),dp[i-2]+abs(v[i]-v[i-2]));
        }
        cout<<dp[n-1];

    }
    return 0;
    
}




Measured memory_bytes: 4792
#include <iostream>
#include <stdio.h>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <unordered_map>
using namespace std;

int main(){
    int a;
    int b;
    cin >> a; cin >> b;
    if ((a * b) % 2 == 0){
        cout << "Even" << endl;
    }
    else{
        cout << "Odd" << endl;
    }
    
}

Measured memory_bytes: 256
// AtCoder ABC136: B - Uneven Numbers
// 2019.8.26 bal4u

#include <stdio.h>
 
int main()
{
	int N, ans;
	
	scanf("%d", &N);
	if      (N >= 100000) ans = 90909;
	else if (N >= 10000) ans = (N-10000+1) + 909;
	else if (N >= 1000) ans = 909;
	else if (N >= 100) ans = (N-100+1) + 9;
	else if (N >= 10) ans = 9;
	else ans = N;
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 128