akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
using namespace std;

string str;
int J=0,O=0,I=0,ans=0;
int main(){
    //freopen("/Users/shitian/Desktop/gcj/gcj/in.txt","r",stdin);
    cin>>str;
    for(int ii=0;ii<str.length();){
        J=0,O=0,I=0;
        while(str[ii]!='J'&&ii<str.length()){
            ii++;
        }
        while(str[ii]=='J'&&ii<str.length()){
            J++;
            ii++;
        }
        if(ii==str.length())break;
        if(str[ii]=='O'){
            O=0;
            for(int t=0;t<J;t++){
                if(str[ii]!='O'){
                    break;
                }
                O++;
                ii++;
                if(ii==str.length())break;
            }
            if(ii==str.length())break;
            if(str[ii]=='J'){
                continue;
            }
            if(O==0){
                continue;
            } else {I=0;
                for(int t=0;t<O;t++){
                    //cout<<"t: "<<t<<"  "<<str[ii]<<endl;
                    if(str[ii]!='I'){
                        break;
                    }
                    I++;
                    ii++;
                    if(ii==str.length())break;
                }
                //cout<<I<<endl;
                if(I<O)continue;
                ans=max(ans,I);
                if(ii==str.length())break;
                if(str[ii]=='O'){
                    continue;
                } else {
                    ans=max(ans,I);
                }
            }
            //cout<<"c"<<endl;
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 2048
#include <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
#include <numeric>
#include <queue>
#include <map>

const int mod = 1000000007;
//const int mod = 998244353;
int maximum(int a, int b) { return a < b ? b : a; }
bool same(char*s, char*t) {
	for (int i = 0;; i++) {
		if (s[i] != t[i])return false;
		if (s[i] == 0) {
			return true;
		}
	}
}

int main() {
	char S[101];
	scanf("%s", S);
	for (int i = 0; S[i] != 0; i++) {
		S[i] = 'x';
	}
	printf("%s", S);
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main(){
	long long int S, C; 
	cin >> S >> C; 
	long long int N = min(S, C/2); 
	long long int M = (C - N * 2) / 4; 
	cout << N + M << endl; 
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
#include<stack>
#include<vector>
#include<cstdio>
#define MAXN 2555
#define INF 0x3f3f3f3f
#define v first
#define b second
using namespace std;
stack<int>q;
stack<int>p;
int s[15];
bool flag;
void dfs(int x)
{
   if(x==10)
   {
      flag=true;
      return ;
   }
   if(s[x]>q.top())
   {
      q.push(s[x]);
      dfs(x+1);
      q.pop();
   }
   if(s[x]>p.top())
   {
      p.push(s[x]);
      dfs(x+1);
      p.pop();
   }
   return ;
}
int main()
{
   int t;
   scanf("%d",&t);
   q.push(0);
   p.push(0);
   while(t--)
   {
      flag=false;
      for(int i=0;i<10;i++)
         scanf("%d",&s[i]);
      dfs(0);
      if(flag)
         cout<<"YES"<<endl;
      else cout<<"NO"<<endl;
   }
   return 0;
}

Measured memory_bytes: 3136
#include <cstdio>

int main(){
  int N;
  scanf("%d",&N);
  int sum=0;
  for(int i=0;i<N;i++){
    int A;
    scanf("%d",&A);
    sum^=A;
  }
  printf("%s\n",(sum&1)?"NO":"YES");
  return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    int n, c;
    cin >> n >> c;
    vector<vector<ll>> dp(2*n, vector<ll>(c+5, 0));
    vector<vector<int>> d(c, vector<int>(c, 0));
    for (int i = 0; i < c; i++) {
        for (int j = 0; j < c; j++) {
            cin >> d[i][j];
        }
    }
    vector<vector<int>> cc(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> cc[i][j];
            cc[i][j]--;
        }
    }
    for (int i = 0; i < 2 * n - 1; i++) {
        for (int j = 0; j < c; j++) {
            for (int k = 0; k <= i; k++) {
                int x = k;
                int y = i - k;
                if (x >= n || y >= n || y < 0 || x < 0) continue;
                //cout << cc[y][x] << " " << j << "\n";
                dp[i][j] += d[cc[y][x]][j];
            }
        }
    }
    long long ans = 3e12;
    for (int i = 0; i < c; i++) {
        for (int j = 0; j < c; j++) {
            if (j == i) continue;
            for (int k = 0; k < c; k++) {
                if (k == i || k == j) continue;
                map<int, int> p;
                p[0] = i;
                p[1] = j;
                p[2] = k;
                ll temp = 0;
                for (int l = 0; l < 2 * n - 1; l++) {
                    temp += dp[l][p[l%3]];
                    if (temp >= ans) {
                        break;
                    }
                }
                //cout << temp << "\n";
                ans = min(ans, temp);
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
  int a, b;
  cin >> a >> b;
  int ans = a * b - a - b + 1;

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


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

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

int main() {
    int n, m;
    cin >> n >> m;
    vector<tuple<int, int, int>> ver_lines(n), hor_lines(m);
    vector<int> X, Y;
    X.push_back(0);
    Y.push_back(0);
    rep(i, n) {
        int a, b, c;
        cin >> a >> b >> c;
        ver_lines[i] = make_tuple(a, b, c);
        X.push_back(a);
        X.push_back(b);
        Y.push_back(c);
    }
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        hor_lines[i] = make_tuple(a, b, c);
        X.push_back(a);
        Y.push_back(b);
        Y.push_back(c);
    }
    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    X.erase(unique(X.begin(), X.end()), X.end());
    Y.erase(unique(Y.begin(), Y.end()), Y.end());
    map<int, int> mp_x, mp_y;
    rep(i, X.size()) {
        mp_x[X[i]] = i;
    }
    rep(i, Y.size()) {
        mp_y[Y[i]] = i;
    }
    int h = X.size() * 2;
    int w = Y.size() * 2;
    vector<vector<int>> F(h, vector<int>(w, 0));
    rep(i, n) {
        int a, b, c;
        tie(a, b, c) = ver_lines[i];
        a = mp_x[a] * 2;
        b = mp_x[b] * 2;
        c = mp_y[c] * 2;
        for (int j = a; j <= b; ++j) F[j][c] = -1;
    }
    rep(i, m) {
        int a, b, c;
        tie(a, b, c) = hor_lines[i];
        a = mp_x[a] * 2;
        b = mp_y[b] * 2;
        c = mp_y[c] * 2;
        for (int j = b; j <= c; ++j) F[a][j] = -1;
    }
    queue<P> q;
    int sx = mp_x[0]*2, sy = mp_y[0]*2;
    q.emplace(sx, sy);
    F[sx][sy] = 1;
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        rep(i, 4) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
            if (F[nx][ny] != 0) continue;
            F[nx][ny] = 1;
            q.emplace(nx, ny);
        }
    }
    ll ans = 0;
    rep(i, h) rep(j, w) {
        if (F[i][j] != 1) continue;
        if (i == 0 || i == h-1 || j == 0 || j == w-1) {
            cout << "INF" << endl;
            return 0;
        }
        if (i % 2 == 0 || j % 2 == 0) continue;
        ll x = X[i/2+1] - X[i/2];
        ll y = Y[j/2+1] - Y[j/2];
        ans += x * y;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 144508
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#include <fstream>
#include <random>
#include <map>
#include <unordered_map>
#include <cstdlib>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define p pair<int,int>
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
using namespace std;
int POW(int x,int y){return int(pow(double(x),double(y)));}



int main(){
	
	int n,k;
	cin>>n>>k;
	vector<int> a(n);
	rep(i,n){
		cin>>a[i];
	}
	sort(a.begin(),a.end());
	vector<int> x;
	int b=a[0];
	int c=0;
	rep(i,n){
		if(b==a[i]){
			c++;
		}else{
			x.push_back(c);
			c=1;
			b=a[i];
		}
		if(i==n-1){
			x.push_back(c);
		}
	}
	sort(x.begin(),x.end());
	int ans=0;
	if(x.size()<=k){
		cout<<0<<endl;
		return 0;
	}
	rep(i,x.size()-k){
		ans+=x[i];
	}
	cout<<ans<<endl;
}


Measured memory_bytes: 2168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contest
{
	class Program
	{
		static void Main()
		{
			var AB = Console.ReadLine().Split().Select(int.Parse);
			Console.WriteLine(AB.Sum() > 9 ? "error" : AB.Sum().ToString());
#if DEBUG
			Console.ReadKey();
#endif
		}
	}
}

Measured memory_bytes: 13268
#include<stdio.h>
#include<string.h>
#include<math.h>
#define FOR(n) for(int i = 0;i < n;i++)
#define PRN(n) printf("%d\n",n)
int sosu(int a){
  if(a < 2)return 0;
  for(int j = 2;j <= (int)pow(a,0.5);j++){
    if(a % j < 1)return 0;
  }
  return 1;
}
int main(void){
  int n,l,r,f[100001],cnt = 0,x[100001];
  scanf("%d",&n);
  FOR(100001){
    f[i] = sosu(i);
  }
  FOR(100001){
    if(f[i] && i % 2 > 0){
      if(f[(i + 1) / 2]){
        cnt++;
      }
    }
    x[i] = cnt;
  }
  FOR(n){
    scanf("%d %d",&l,&r);
    PRN(x[r] - x[l - 1]);
  }
  return 0;
}

Measured memory_bytes: 1280
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[40]={0};
int main()
{
    int n,m,k,x;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int o=1;o<=m;o++)a[o]--;
        scanf("%d",&k);
        for(int j=1;j<=k;j++)
        {
           scanf("%d",&x);a[x]++;
        }
    }
    int ans=0;
    for(int i=1;i<=m;i++)
        if(a[i]==0)ans++;
    printf("%d",ans);
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

int main(){
	int n;
	long s=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		char c;
		int d;
		scanf(" %c %d",&c,&d);
		switch(c){
			case 'S':d+=13*0;break;
			case 'H':d+=13*1;break;
			case 'C':d+=13*2;break;
			case 'D':d+=13*3;break;
		}
		s|=1L<<--d;
	}
	for(int i=0;i<52;i++)if(~s>>i&1)printf("%c %d\n","SHCD"[i/13],i%13+1);
}



Measured memory_bytes: 2104
S = list(map(int,input()))
S_len = len(S);
result = 753;
i = 0;
while(i+2<S_len):
  a = S[i]*100 + S[i+1]*10 + S[i+2];
  tmp = abs(753 - a);
  if result > tmp:
    result = tmp;
  i = i + 1;
print(result)

Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <map>

using ll = long long;

using namespace std;

int main() {
    int x, a, b, n;
    cin >> x >> a >> b >> n;
    map<string, int> m{{"nobiro", a}, {"tidime", b}, {"karero", -1000000000}};
    for (int i=0; i<n; i++) {
        string s; cin >> s;
        x = max(0, x + m[s]);
    }
    cout << x << '\n';
    return 0;
}


Measured memory_bytes: 3212
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int MAX = 1010000;

long long fac[MAX], finv[MAX], inv[MAX];
 
int main (void){
	long long int i,j,h,w,n,sr,sc,keep;
	char s[200005],t[200005];
	cin >> h >> w >> n >> sr >> sc >> s >> t;
	
	keep=0;
	for(i=0;i<n;i++){
		if(s[i]=='L') keep++;
		if(keep>=sc){
			cout << "NO" << endl;
			return 0;
		}
		if(t[i]=='R'&&(1-keep)!=(w+1-sc)) keep--;
	}
	
	keep=0;
	for(i=0;i<n;i++){
		if(s[i]=='R') keep++;
		if(keep>=(w+1-sc)){
			cout << "NO" << endl;
			return 0;
		}
		if(t[i]=='L'&&(1-keep)!=sc) keep--;
	}
	
	keep=0;
	for(i=0;i<n;i++){
		if(s[i]=='U') keep++;
		if(keep>=sr){
			cout << "NO" << endl;
			return 0;
		}
		if(t[i]=='D'&&(1-keep)!=(h+1-sr)) keep--;
	}
	
	keep=0;
	for(i=0;i<n;i++){
		if(s[i]=='D') keep++;
		if(keep>=(h+1-sr)){
			cout << "NO" << endl;
			return 0;
		}
		if(t[i]=='U'&&(1-keep)!=sr) keep--;
	}

	cout << "YES" << endl;
	return 0;
}

Measured memory_bytes: 640
#![allow(unused_imports)]

//input
use proconio::input;
use proconio::marker::{Bytes, Chars};
//use std::io::Read;

//collections
//use std::collections::BTreeMap;
//use std::collections::BTreeSet;
//use std::collections::HashMap;
//use std::collections::HashSet;
//use std::collections::VecDeque;
//use std::collections::BinaryHeap;

//use std::cmp::*;

fn main() {
    input! {
        n: i64,
    }
    if n == 2 {
        println!("1");
        return;
    }
    let mut ans = 0;
    for i in 1..n - 1 {
        if i * i >= n {
            break;
        }
        let mut ok = i;
        let mut ng = n;
        while (ok - ng).abs() > 1 {
            let mid = (ok + ng) / 2;
            if i * mid < n {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        ans += (ok - i) * 2 + 1;
        //println!("{}", ok - i);
    }
    println!("{}", ans);
}


Measured memory_bytes: 2112
#include <iostream>
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9+7;
const ll LINF = 1LL<<60;

ll euclidean_gcd(int a, int b) {
    if (a < b) return euclidean_gcd(b, a);
    ll r;
    while(r = a % b) {
        a = b;
        b = r;
    }
    return b;
}


int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int k;
    cin >> k;

    ll res = 0;
    REP(a, 1, k + 1) {
        REP(b, 1, k + 1) {
            REP(c, 1, k + 1) {
                res += euclidean_gcd(euclidean_gcd(a, b), c);
            }
        }
    }

    cout << res << endl;

    return 0;
}

Measured memory_bytes: 3648
#include <bits/stdc++.h>
#define f(i,n) for(long long i=0;i<n;i++)
using namespace std;main(){int x,y,z,n;cin>>x>>y>>z>>n;long long a[x],b[y],c[z];vector<long long>r;f(i,x)cin>>a[i];f(i,y)cin>>b[i];f(i,z)cin>>c[i];sort(a,a+x,greater<long long>()),sort(b,b+y,greater<long>()),sort(c,c+z,greater<long>());f(i,x)f(j,y)f(k,z)if((i+1)*(j+1)*(k+1)<=n)r.push_back(a[i]+b[j]+c[k]);sort(r.begin(),r.end(),greater<long>());f(i,n)cout<<r[i]<<'\n';}

Measured memory_bytes: 1400
'use strict'
function main(inp) {
  inp = Number(inp.trim())
  if (inp === 1) {
    console.log(1)
  } else if (inp === 0) {
    console.log(0)
  } else {
    console.log(inp%2 ? Math.ceil(inp/2)/inp : 0.5)
  }
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));


Measured memory_bytes: 22080
#include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i,a,b) for(int64_t i=(a); i<(b); ++i) // a ≦ i < b 
#define Rrep(i,a,b) for(int64_t i=(a);i>=(b);--i) // reverse repeat. a から b まで減少．
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend() //逆イテレータ
#define INF 1000000000000000;
#define MOD 1000000007LL;
#define RANGE(a,b,c) (a).begin()+(b),(a).begin()+(c) // [b,c) イテレータ
typedef pair<int64_t, int64_t> PII;
typedef vector<int64_t> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

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

int64_t N; cin >> N;
if (N == 0){
    cout << "0\n";
    return 0;
}

string S = "";
while (N != 1){
    if (N < 0) {
        if (N % 2) {
            N -= 2;
            S += '1';
        }
        else S += '0';
    }
    else 
        S += (char)(N % -2 + '0');
    N /= -2;
}
S += '1';
reverse(ALL(S));
cout << S << endl;

}
// 書いて考える
// 境界，出力文字列 チェック
// 可読性優先．高速化次点．
// まずは全探索，分割統治，次にDP
// 制限を見る．境界に注意．求めたい量の変域．動かせる量．
// 偶奇，逆から，ソート，出現回数，出現位置，DP， 余事象，包除
// データ構造． 問題の特徴量．単調性，二分探索
// 存在判定：構成方法，入力の特徴
// gcd, lcm ,素因数分解．
// 例外を十分に含む一般化．想像力の限界
// 小さい系から例示
// 代数的処理
// 始めは過剰に例示・場合分けしてもいい．各場合を確実に対処．
// 自明な例から処理，除外．
// 小数のときは，精度の設定する．doubel 変数に数値を入力するときは 123. とする．
// テストケース作成は数表あり
// 実行エラー：vector添え字超え．0割り．



Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <stack>
#include <iostream>
#include <string>
#include <tuple>
#include <random>
#include <map>
#include <queue>
#include <set>
#include <complex>
#include <algorithm>
#include <cassert>
#include <iterator>
#include <numeric>
#include <cmath>
#include <stdio.h>
#include <functional>

using namespace std;
using ll = long long;
using ld = double;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll, ll>;
const ll INF = 1e17;
const ld eps = 1e-10;
const ll MOD = 1e9 + 7;

const double pi = acos(-1.0);


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll N;
	cin >> N;
	vector<vector<ll>> es(N);
	vector<ll> dim(N);
	for (int i = 0;i < N - 1;i++) {
		ll a, b;
		cin >> a >> b;
		a--; b--;
		dim[a]++;
		dim[b]++;
		es[a].push_back(b);
		es[b].push_back(a);
	}
	vector<ll> c(N);
	for (int i = 0;i < N;i++)
		cin >> c[i];
	sort(c.begin(), c.end());
	queue<ll> leaf;
	for (int i = 0;i < N;i++)
		if (dim[i] == 1)
			leaf.push(i);
	vector<ll> ans(N, -1);
	ll weight = 0;
	ll cnt = 0;
	while (!leaf.empty()) {
		ll now = leaf.front();
		leaf.pop();
		ans[now] = c[cnt];
		weight += c[cnt];
		cnt++;
		for (auto &&e : es[now]) {
			dim[e]--;
			if (dim[e] == 1)
				leaf.push(e);
		}
	}
	cout << weight - c.back() << endl;
	for (int i = 0;i < N;i++) {
		cout << ans[i];
		if (i == N - 1)
			cout << endl;
		else
			cout << " ";
	}
	return 0;
}






Measured memory_bytes: 1280
import java.io.*;
import java.util.*;
import java.math.*;

class Main{
		public static void physical(ArrayList<Double> list){
			int res;
			double t,y,ya,v;
			
			for(int i=0;i<list.size();i++){
				v = list.get(i);
				t = v/9.8;
				y = 4.9 * t * t;
				for(int j=1;j<1000;j++){
					ya = 5 * j - 5;
					if(y < ya){System.out.println(j); break;}
				}
			}
			
		}
		
		public static void main(String args[]) throws IOException{
			String str;
			ArrayList <Double> inp = new ArrayList<Double>();
			int out;

			Scanner scan = new Scanner(System.in);
			
			while(scan.hasNext()){
				str = scan.next();
				double x = Double.parseDouble(str);
				inp.add(x);
			}
			physical(inp);
		}
}

Measured memory_bytes: 11000
x = [c  for c in input()]

n = len(x)
ret=[]
for i in range(n-1):
    ret.append([0,0])

if x[n-1] == "1":
    print (-1)
elif x[0] == "0":
    print (-1)
else:
    flg = 1
    for i in range(n-1):
        if x[i] != x[n-1-i-1]:
            flg = 0
    if flg == 0:
        print (-1)
    else:
        a=1
        for i in range(n-1):
            if x[i] == "1":
                ret[i] = [a,i+2]
                a=i+2
            else:
                ret[i] = [a,i+2]
        
        for i in range(n-1):
            print ("{0} {1}".format(ret[i][0], ret[i][1]))

Measured memory_bytes: 21624
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

vector<ll>cnt[202020];

int main(){

    ll N, K;
    cin >> N >> K;
    vector<ll>A(N), nxt(N, -1), cost(N, 0);    
    for(ll i = 0; i < N; i++){
        cin >> A[i];
        cnt[A[i]].push_back(i);
    }
    if(N == 1){
        if(K % 2)cout << A[0] << endl;
        else cout << endl;
        return 0;
    }

    for(ll i = 1; i <= 200000; i++){
        for(ll j = 0; j < cnt[i].size(); j++){
            ll idx = cnt[i][j];
            ll nidx = (cnt[i][(j + 1) % cnt[i].size()] + 1) % N;
            nxt[idx] = nidx;
            if(j + 1 == cnt[i].size() || nidx <= idx)cost[idx] = 1;
            if(nxt[idx] == 0 && j == 0 && idx + 1 == N)cost[idx] = 2;
        }
    }
    ll M = 0;
    for(ll idx = 0; ;){
        M += cost[idx];
        idx = nxt[idx];
        if(idx == 0)break;
    }
    K %= M;
    vector<ll>ans;
    if(K){
        ll idx = 0, k = 0;
        for(;k < K;){
            ll tmpIdx = nxt[idx];
            ll tmpK = k + cost[idx];
            if(tmpK < K || (tmpK == K && tmpIdx == 0)){
                idx = tmpIdx;
                k = tmpK;
                continue;
            }
            // cout << idx << " : " << A[idx] << endl;
            ans.push_back(A[idx]);
            idx++;
            if(idx == N){
                idx = 0;
                k++;
            }
            // cout << idx << ", " << k << endl;
        }
    }
    if(ans.size()){
        for(ll i = 0; i < ans.size(); i++){
            if(i)cout << " ";
            cout << ans[i];
        }
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 18804
#include <iostream>
#include <vector>
#include <complex>
#include <algorithm>

using namespace std;

#define all(c) (c).begin(), (c).end()
const double eps = 1e-10;
typedef complex<double> Point;

ostream& operator<<(ostream& os, const Point& p){
    return os << real(p) << " " << imag(p);
}

inline double cross(const Point&a, const Point& b){
    return imag(conj(a) * b);
}

inline double dot(const Point& a, const Point& b){
    return real(conj(a) * b);
}

int ccw(Point a, Point b, Point c){
    b -= a; c -= a;
    if(cross(b, c) > eps) return  1;
    if(cross(b, c) < -eps)return -1;
    if(dot(b, c) < 0)     return  2;
    if(norm(b) < norm(c)) return  2;
    return 0;
}

typedef vector<Point> Polygon;

Polygon convexhull(Polygon g){
    if((int)g.size() < 3)return g;
    Polygon res(g.size() << 1);
    auto comp = [](const Point& a, const Point& b){
        return make_pair(real(a), imag(a)) < make_pair(real(b), imag(b));
    };
    sort(g.begin(), g.end(), comp);
    int k = 0;
    for(int i=0; i<(int)g.size(); res[k++]=g[i++]){
        while(2 <= k && ccw(res[k-2], res[k-1], g[i]) <= 0)k--;
    }
    for(int i=(int)g.size()-2, t=k+1; i>=0; res[k++]=g[i--]){
        while(t <= k && ccw(res[k-2], res[k-1], g[i]) <= 0)k--;
    }
    res.resize(k - 1);
    return res;
}

int main(){
    int n;
    cin >> n;
    Polygon g;
    while(n--){
        int x, y;
        cin >> x >> y;
        g.emplace_back(x, y);
    }
    g = convexhull(g);
    auto comp = [](const Point& a, const Point& b){
        return make_pair(imag(a), real(a)) < make_pair(imag(b), real(b));
    };
    rotate(g.begin(), min_element(all(g), comp), g.end());
    cout << g.size() << '\n';
    for(const auto& p: g)cout << p << '\n';
    return 0;
}

Measured memory_bytes: 7304
k, t = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
sa = a.sort
l = 0
r = t - 1
loop do
  break if l == r
  eat = [sa[l], sa[r]].min
  sa[l] -= eat
  sa[r] -= eat
  if sa[l] == 0
    l += 1
  else
    r -= 1
  end
end
puts [sa[l] - 1, 0].max


Measured memory_bytes: 1788
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i=0; i<(n); i++) 
#define rep2(i, k, n) for (ll i=k; i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


int main() {
    ll n, a;
    cin >> n;
    vector<ll> x(n+2), s(n+2);
    rep1(i, n){
        cin >> a;
        if (i==0){
            x[i+1] = a;
            s[i+1] = a;
        }
        else {
            x[i+1] = x[i] ^ a;
            s[i+1] = s[i] + a;
        }
    }
    ll ans = 0;
    for (ll i=0; i<(n+1); i++){
        ll left = i;
        ll right = n+1;
        while (right-left>1){
            ll mid = (right + left) / 2;
            ll res = x[mid] ^ x[i];
            if (res==s[mid]-s[i]) left = mid;
            else right = mid;
        }
        ans += left - i;
    }
    cout << ans << endl;
    //printf("%.12f", ans);
}

Measured memory_bytes: 6404
#include<algorithm>
#include<cstring>
#include<cctype>
#include<cstdio>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define repd(i,x,y) for(int i=x; i>=y; --i)

using namespace std;
const int N=1<<18,mod=998244353;
typedef long long LL;
const LL inv2=(mod+1)/2;
int n;
LL tot,p[N],f[N],_f[N];
LL L[N],R[N],_L[N],_R[N],ans[N];

int getint()
{
	char ch;
	while(!isdigit(ch=getchar()));
	int x=ch-48;
	while(isdigit(ch=getchar())) x=x*10+ch-48;
	return x;
}

LL getmi(LL a,LL x)
{
    LL rt=1;
    while(x)
    {
        if(x&1) rt=rt*a%mod;
        a=a*a%mod,x>>=1;
    }
    return rt;
}

void FWT(LL a[],int n)
{
    for(int i=1; i<n; i<<=1)
        for(int j=0; j<n; j+=i<<1)
            rep(k,0,i-1)
            {
                LL x=a[j+k],y=a[i+j+k];
                a[j+k]=(x+y)%mod,a[i+j+k]=(x-y)%mod;
            }
}

void IFWT(LL a[],LL n)
{
    for(LL i=1; i<n; i<<=1)
        for(LL j=0; j<n; j+=i<<1)
            rep(k,0,i-1)
            {
                LL x=a[j+k],y=a[i+j+k];
                a[j+k]=inv2*(x+y)%mod;
                a[i+j+k]=inv2*(x-y)%mod;
            }
}

int main()
{
	n=getint();
	rep(i,0,(1<<n)-1) tot+=(p[i]=getint());
	tot=getmi(tot,mod-2);
	rep(i,0,(1<<n)-1) p[i]=p[i]*tot%mod;
	FWT(p,1<<n);

	rep(i,0,(1<<n)-1) L[i]=R[i]=1-p[i];
	rep(i,1,(1<<n)-1) L[i]=L[i]*L[i-1]%mod;
	repd(i,(1<<n)-2,0) R[i]=R[i]*R[i+1]%mod;

	rep(i,0,(1<<n)-1)
	{
		LL x=1;
		if(i>0) x=L[i-1],_L[i]=_L[i-1]*(1-p[i])%mod;
		_L[i]=(_L[i]+x*(-p[i]))%mod;
	}

	repd(i,(1<<n)-1,0)
	{
		LL x=1;
		if(i<(1<<n)-1) x=R[i+1],_R[i]=_R[i+1]*(1-p[i])%mod;
		_R[i]=(_R[i]+x*(-p[i]))%mod;
	}

	rep(i,0,(1<<n)-1)
	{
		LL l=i>0?L[i-1]:1;
		LL _l=i>0?_L[i-1]:0;
		LL r=i<(1<<n)-1?R[i+1]:1;
		LL _r=i<(1<<n)-1?_R[i+1]:0;
		f[i]=l*r%mod,_f[i]=(l*_r+_l*r)%mod;
	}
	
	IFWT(f,1<<n),IFWT(_f,1<<n);
	rep(i,0,(1<<n)-1)
	{
		LL x=(_f[i]*f[0]-f[i]*_f[0])%mod;
		LL y=getmi(f[0]*f[0]%mod,mod-2);
		ans[i]=(x*y%mod+mod)%mod;
		printf("%lld\n",ans[i]);
	}
	return 0;
}

Measured memory_bytes: 19072
#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MAX 2000005
#define MOD 1000000007
#define MOD2 998244353
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
#define vi vector<ll>
#define vp vector<pii>
#define all(v) v.begin(),v.end()
#define sz(x) (ll)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define ROF(i,a,b) for(ll i=a;i>=b;--i)
#define trace(x) cerr<<#x<<": "<<x<<'\n';
typedef long long ll;
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

ll mexp(ll a, ll b){
    ll ans=1;
    a%=MOD;
    while(b){
        if(b&1) ans=ans*a%MOD;
        b>>=1;
        a=a*a%MOD;
    }
    return ans;
}

int main(){
    IOS
    string s;
    cin>>s;
    int n=sz(s);
    s='x'+s;
    if(s[1]=='0' || s[n]=='1'){
        cout<<-1;
        return 0;
    }
    
    FOR(i,2,n){
        if(s[i]!=s[n-i]){
            cout<<-1;
            return 0;
        }
    }
    int u=1;
    FOR(i,2,n+1){
        cout<<u<<" "<<i<<endl;
        if(s[i-1]=='1'){
            u=i;
        }
    }


    cerr<<"Time: "<<1.0/CLOCKS_PER_SEC<<" ms"<<endl;
    return 0;
}

Measured memory_bytes: 1664
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int main(){
  long long N;
  rd(N);
  long long i;
  long long j;
  long long k;
  long long res = 0;
  for(i=(1);i<(N+1);i++){
    for(j=1;;j++){
      if(i * j >= N){
        break;
      }
      res++;
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200916-1

// --- original code ---
// {
//   ll @N, i, j, k, res = 0;
//   rep(i,1,N+1) for(j=1;;j++){
//     if(i * j >= N) break;
//     res++;
//   }
//   wt(res);
// }


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

using namespace std;

void solve()
{
    int n;
    cin >> n;
    priority_queue<double, vector<double>, greater<double>> pq;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        pq.push(x);
    } 
    for (int i = 0; i < n-1; i++)
    {
        double r = pq.top();
        pq.pop();
        r += pq.top();
        pq.pop();
        r /= 2;
        pq.push(r);
    }
    cout << pq.top() << endl;
}

int main()
{
    solve();
    cerr << "努力不足ですね！" << endl;
    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()
#define debug(x) cerr<<#x<<": "<<(x)<<endl
using namespace std;
using llong = long long;
using vi    = vector<int>;
using vvi   = vector<vi >;
using vvvi  = vector<vvi >;
using pii   = pair<int,int>;
constexpr int    INF=1e9;
constexpr double EPS=1e-9;
constexpr int    MOD=1e9+7;
template<class Type>
void line(const Type &a){int cnt=0;for(const auto &elem:a){cerr<<(cnt++?' ':'>');cerr<<elem;}cerr<<endl;}

/*最長共通接頭辞*/
vi z_algorithm(const string &s){//O(|S|).
	vi z(s.size(),0);
	z[0]=s.size();
	int i=1,j=0;
	while(i<s.size()){
		while(i+j<s.size() and s[j]==s[i+j]) j++;
		z[i]=j;
		if(j==0){
			i++;
			continue;
		}
		int k=1;
		while(k<j and k+z[k]<j){
			z[i+k]=z[k];
			k++;
		}
		i+=k, j-=k;
	}
	return z;
}

int main(){
	int n;
	string s;
	cin>>n>>s;
	
	int ans=0;
	for(int i=n-1;i>=0;--i){
		string t=s.substr(i,n-i);
		vi dp=z_algorithm(t);
		REP(j,dp.size()) ans=max(ans,min(j,dp[j]));
	}
	
	cout<<ans<<endl;
	return 0;
}

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

namespace {
int32_t gcd(const int32_t &a, const int32_t &b) {
  return b == 0 ? a : gcd(b, a % b);
}
void solve() {
  int32_t n;
  std::cin >> n;

  std::vector<int32_t> l(n + 1, 0), r(n + 1, 0);

  std::vector<int32_t> v(n, 0);
  for (int32_t i = 0; i < n; i++) {
    std::cin >> v[i];
    l[i + 1] = gcd(l[i], v[i]);
  }
  for (int32_t i = n - 1; i >= 0; i--) {
    r[i] = gcd(r[i + 1], v[i]);
  }

  int32_t a = 0;
  for (int32_t i = 0; i < n; i++) {
    a = std::max(a, gcd(l[i], r[i + 1]));
  }

  std::cout << a << std::endl;
}
} // namespace

int32_t main(void) { solve(); }


Measured memory_bytes: 1408
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
C=list(map(int, input().split()))
F=0
for  i  in range(N-1):
      if A[i+1]==A[i]+1 and i<N-1:
         a=A[i]
         F+=C[a-1]
print(F+sum(B))

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
typedef long long l;
int main()
{
    l n ,k;
    cin>>n>>k;
    l arr[n];
    for(l i = 0 ; i<n;i++)
    cin>>arr[i];
    l dp[n];
    dp[0]=0;
    dp[1]=abs(arr[1]-arr[0]);
    for(l i = 2 ; i<n ; i++)
    {
        l ans  = 1000000000;
        for( l t = 1 ; t<=k ;t++)
        {
            l p = i-t;
            l temp = 100000000;
            if(p>=0)
            temp = dp[p]+abs(arr[i]-arr[p]);
            ans = min(ans , temp);
        }
        dp[i]=ans;
    }
    cout<<dp[n-1];
    
    return 0;
}

Measured memory_bytes: 1792
import java.util.Scanner;

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

		int n = sc.nextInt();
		int[] d = new int[n];
		for (int i = 0; i < n; i++) {
			d[i] = sc.nextInt();
		}
		int ans = 0;
		for (int i = 0; i < n - 1; i++) {
			for (int j = i + 1; j < n; j++) {
				ans += d[i] * d[j];
			}
		}
		System.out.println(ans);
	}

}


Measured memory_bytes: 21844
def bubble(l):
	count = 0
	N = len(l)
	flag = 1

	while flag:
		flag = 0
		for j in range(N - 1, 1 - 1, -1):
			if l[j] < l[j - 1]:
				l[j], l[j - 1] = l[j - 1], l[j]
				flag = 1
				count += 1

	return l, count


if __name__ == '__main__':
	n = int(input())
	l = input().split()

	for i in range(n):
		l[i] = int(l[i])

	l, c = bubble(l)
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i)
			print(c)



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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;


int main() {
    V<string> s(3);
    for(int i = 0; i < 3; i++) cin >> s[i];
    string ans = "";
    ans += s[0][0];
    ans += s[1][1];
    ans += s[2][2];
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3600
use std::io::{Read, stdin};

use proconio::input;

fn main() {
    const modulo: u64 = 1000000007;
    input! {
        n: u64,
        k: usize
    }

    let mut gcd_count = vec![0; k + 1];
    let mut res = 0u64;

    for gcd in (1..=k).rev() {
        gcd_count[gcd] = pow((k / gcd) as u64, n);
        for i in 2..=k / gcd {
            gcd_count[gcd] = (gcd_count[gcd] + modulo - gcd_count[gcd * i]) % modulo;
        }
        res += gcd_count[gcd] * gcd as u64 % modulo;
        res %= modulo;
    }

    println!("{:?}", res);
}

fn pow(a: u64, b: u64) -> u64 {
    const modulo: u64 = 1000000007;
    if b == 0 {
        1
    } else if b % 2 == 1 {
        a * pow(a, b - 1) % modulo
    } else {
        let r = pow(a, b / 2);
        r * r % modulo
    }
}


Measured memory_bytes: 2756
#define _CRT_SECURE_NO_WARNINGS
#include <array>
#include <cstdio>

using namespace std;

const int limit = 50;

array<array<bool, limit>, limit> graph;

array<bool, limit> visited;

array<int, limit> a;

array<int, limit> b;

int N, M;

void dfs(int x)
{
    visited[x] = true;

    for (int i = 0; i < N; i++)
    {
        if (visited[i]) continue;
        if (!graph[x][i]) continue;
        dfs(i);
    }
}

bool judge()
{
    for (int i = 0; i < N; i++)
    {
        if (!visited[i]) return true;
    }
    return false;
}


int main()
{
    if (scanf("%d%d", &N, &M) < 2) return 0;

    for (int i = 0; i < M; i++) graph[i].fill(false);

    for (int i = 0; i < M; i++)
    {
        int tmpa, tmpb;
        if (scanf("%d%d", &tmpa, &tmpb) < 2) return 0;

        a[i] = tmpa - 1;
        b[i] = tmpb - 1;

        graph[a[i]][b[i]] = true;
        graph[b[i]][a[i]] = true;
    }

    int count = 0;

    for (int i = 0; i < M; i++)
    {
        graph[a[i]][b[i]] = false;
        graph[b[i]][a[i]] = false;

        visited.fill(false);

        dfs(0);

        if (judge()) count++;

        graph[a[i]][b[i]] = true;
        graph[b[i]][a[i]] = true;
    }

    printf("%d\n", count);
    return 0;
}


Measured memory_bytes: 128
#include<string>
#include<iostream>
using namespace std;
int main(){
    string a,b,h;
    int c,d=0,e=0,f,g;
    char U[1000][1000];
    cin>>b;
    for(int i=0;i<b.size();i++){
        U[0][i]=b[i];
    }
    cin>>c;
    for(int i=1;i<=c;i++){
        cin>>a;
        if(a=="print"){
            cin>>d>>e;
            for(int j=0;j<b.size();j++){
                U[i][j]=U[i-1][j];
            }
            for(int j=d;j<=e;j++){
                cout<<U[i][j];
            }
            cout<<endl;
        }
        else if(a=="replace"){
            cin>>d>>e>>h;
            for(int j=0;j<b.size();j++){
                U[i][j]=U[i-1][j];
            }
            for(int j=d;j<=e;j++){
                U[i][j]=h[j-d];
            }
        }
        else if(a=="reverse"){
            cin>>d>>e;
            for(int j=0;j<b.size();j++){
                U[i][j]=U[i-1][j];
            }
            for(int j=d;j<=e;j++){
                U[i][j]=U[i-1][d+e-j];
            }
        }
    }
    return 0;
}

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

vector<string>v;

int main() {
	string s, t;
	cin >> s >> t;
	for (int i = 0;i + t.size() <= s.size();i++) {
		string a = s.substr(i, t.size());
		bool check = true;
		for (int j = 0;j < t.size();j++) {
			if (a[j] != '?'&&a[j] != t[j]) {
				check = false;
				break;
			}
		}
		if (check == true) {
			a = s.substr(0, i) + t + s.substr(i + t.size(), s.size() - (i + 1));
			for (int j = 0;j < s.size();j++) {
				if (a[j] == '?')a[j] = 'a';
			}
			v.push_back(a);
		}
	}
	if (v.size() == 0) {
		cout << "UNRESTORABLE" << endl;
	}
	else {
		sort(v.begin(), v.end());
		cout << v[0] << endl;
	}
}

Measured memory_bytes: 256
#include <algorithm>
#include <iostream> //入出力
#include <stdio.h>
#include <map>
#include <set>
#include <string>
#include <math.h> //算術演算子
#include <vector>
#include <cstdlib>
#include <queue>
#include <stack>
#include <list>
#include <iomanip> //小数点以下を表示させる(setprecision())
//#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //64bit型

#define debug(x) cerr << x << " " \
                      << "(L:" << __LINE__ << ")" << '\n'
#define PRINT(V)                                   \
        {                                          \
                for (int i = 0; i < V.size(); i++) \
                {                                  \
                        cout << V[i] << " ";       \
                }                                  \
                cout << endl;                      \
        }

int gcd(int a, int b)
{
        return b ? gcd(b, a % b) : a;
}

int main()
{
        //input->compute->output//
        int N, A, B, C;
        cin >> N >> A >> B >> C;

        cout << min(N,A)*B+max(N-A,0)*C << endl;

        return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

vec<int, 2> lcp(string& a, string& b) {
  vec<int, 2> dp = make_v(a.size() + 1, b.size() + 1, 0);
  for (int i = a.size() - 1; i >= 0; i--) {
    for (int j = 0; j < b.size(); j++) {
      if (a[i] == b[j] || a[i] == '?' || b[j] == '?') {
        dp[i][j] = dp[i + 1][j + 1] + 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  return dp;
}

int solve(string& a, string& b, string& c) {
  auto ab = lcp(a, b);
  auto ac = lcp(a, c);
  auto bc = lcp(b, c);
  int res = a.size() + b.size() + c.size();
  auto check = [&](auto& dp, int ai, int sizeA, int sizeB) {
    if (ai >= sizeA) return true;
    int match = dp[ai][0];
    int lim = min(sizeA - ai, sizeB);
    return match >= lim;
  };
  for (int i = 0; i <= a.size(); i++) {
    int len = max(a.size(), b.size() + i);
    for (int j = i; j <= len; j++) {
      if (!check(ab, i, a.size(), b.size())) continue;
      if (!check(ac, j, a.size(), c.size())) continue;
      if (!check(bc, j - i, b.size(), c.size())) continue;
      chmin(res, max(len, j + (int)c.size()));
    }
  }
  return res;
}

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  string a, b, c;
  while (cin >> a >> b >> c) {
    int res = a.size() + b.size() + c.size();
    vector<string> vs = {a, b, c};
    vector<int> perm = {0, 1, 2};
    do {
      chmin(res, solve(vs[perm[0]], vs[perm[1]], vs[perm[2]]));
    } while (next_permutation(perm.begin(), perm.end()));
    cout << res << endl;
  }
  return 0;
}


Measured memory_bytes: 47452
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7


def main():
    n,k = list(map(int, input().split()))
    r = k
    for _ in range(1,n):
        r *= k-1
    return r


print(main())


Measured memory_bytes: 7836
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

int main() {
    int H;
    int W;

    if (scanf("%d %d", &H, &W) < 0) return 0;

    int ASTCOUNT = 0;

    char tmp[8];

    vector< vector<char> > A;
    vector< vector<int> > I;

    A.resize(H);
    I.resize(H);

    for (int i = 0; i < H; i++)
    {
        A[i].resize(W);
        I[i].resize(W);

        if (scanf("%s", tmp) < 1) return 0;

        for (int j = 0; j < strlen(tmp); j++)
        {
            A[i][j] = tmp[j];
            if (tmp[j] == '#') ASTCOUNT++;
        }
    }

    int y = 0;
    int x = 0;
    I[0][0] = 1;

    int STPCOUNT = 1;

    while (true)
    {
        if (x == W - 1 && y == H - 1)
        {
            if (A[y - 1][x] == '#' && I[y - 1][x] == 0)
            {
                printf("%s\n", "Impossible");
                return 0;
            }
            if (A[y][x - 1] == '#' && I[y][x - 1] == 0)
            {
                printf("%s\n", "Impossible");
                return 0;
            }
            break;
        }
        if (-1 < y - 1)
        {
            if (A[y - 1][x] == '#' && I[y - 1][x] == 0)
            {
                printf("%s\n", "Impossible");
                return 0;
            }
            I[y - 1][x] == 1;
        }
        if (-1 < x - 1)
        {
            if (A[y][x - 1] == '#' && I[y][x - 1] == 0)
            {
                printf("%s\n", "Impossible");
                return 0;
            }
            I[y][x - 1] = 1;
        }
        if (x + 1 < W)
        {
            I[y][x + 1] = 1;
            if (A[y][x + 1] == '#')
            {
                x = x + 1;
                STPCOUNT++;
                continue;
            }
        }
        if (y + 1 < H)
        {
            I[y + 1][x] = 1;
            if (A[y + 1][x] == '#')
            {
                y = y + 1;
                STPCOUNT++;
                continue;
            }
        }
        printf("%s\n", "Impossible");
        return 0;
    }

    if (ASTCOUNT == STPCOUNT)
    {
        printf("%s\n", "Possible");
        return 0;
    }
    else
    {
        printf("%s\n", "Impossible");
        return 0;
    }
}


Measured memory_bytes: 256
#include<stdio.h>
#include<algorithm>
using namespace std;
int b[200];
int dp1[1000001];
int dp2[1000001];
int main(){
	int a;
	for(int i=0;i<200;i++){
		b[i]=i*(i+1)*(i+2)/6;
	}
	for(int i=0;i<1000001;i++)dp1[i]=dp2[i]=99999999;
	dp1[0]=dp2[0]=0;
	for(int i=0;i<1000001;i++){
		for(int j=1;j<200;j++){
			if(i+b[j]<=1000000)dp1[i+b[j]]=min(dp1[i+b[j]],dp1[i]+1);
			if(b[j]%2&&i+b[j]<=1000000)dp2[i+b[j]]=min(dp2[i+b[j]],dp2[i]+1);
		}
	}
	while(scanf("%d",&a),a){
		printf("%d %d\n",dp1[a],dp2[a]);
	}
}

Measured memory_bytes: 8840
#include <array>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <tuple>
using namespace std;


typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define PB push


#define MOD 1000000007LL
#define EPS 1e-10

int main()
{
    int N, M;
    cin >> N >> M;
    vector<pair<LL, LL> > stores(N);
    REP(i, N)
    {
        LL A, B;
        cin >> A >> B;
        stores[i] = make_pair(A, B);
    }
    sort(stores.begin(), stores.end());
    LL yen = 0;
    int m = M;
    for (const auto& store : stores)
    {
        if (store.second >= m)
        {
            yen += store.first * m;
            m = 0;
            break;
        }
        else
        {
            yen += store.first * store.second;
            m -= store.second;
        }
    }
    cout << yen << endl;
    return 0;
}


Measured memory_bytes: 1792
n = int(input())
l = list(map(int,input().split()))
i=0
count,j = 0,1
while i < n:
  if j == l[i]:
    count+=1
    j+=1
  i+=1
if count!=0 or n==count:
  print(n-count)
else:
  print(-1)
  

Measured memory_bytes: 24744
#include <cstdio>

typedef long long ll;
const ll mod = 1e9+7;

ll a[100005],b[100005],dy,ans;
int main(){
	int N, M; scanf("%d %d",&N,&M);
	for(int i=0;i<N;i++) scanf("%lld",a+i);
	for(int i=0;i<M;i++) scanf("%lld",b+i);
	for(int i=1;i<N;i++){
		dy+=(a[i]-a[i-1])*(i)%mod*(N-i)%mod;
		dy%=mod;
	}
	for(int i=1;i<M;i++){
		ans+=(b[i]-b[i-1])*dy%mod*(i)%mod*(M-i)%mod;
		ans%=mod;
	}
	printf("%lld\n",ans);
}

Measured memory_bytes: 1664
#include<iostream>
using namespace std;
int main(){
int n,r,p,c,huda[51],temp[51],i,j,k,q,s;
while(1){
scanf("%d%d",&n,&r);
if(n==0 && r==0)break;
for(i=1;i<=n;i++)huda[i]=n-i+1;
for(s=0;s<r;s++){
scanf("%d%d",&p,&c);
for(i=1;i<p;i++)temp[i]=huda[i];
for(i=1;i<=c;i++)huda[i]=huda[i+p-1];
for(i=1;i<p;i++)huda[i+c]=temp[i];
}
printf("%d\n",huda[1]);
}
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define debug(a) cout << #a << " = " << a << " ";
template<typename T> istream &operator>>(istream &is, vector<T> &vec)
                                        { for (auto &v : vec) is >> v; return is; }

const int  MOD=1000000007;
const int N=100005;

void solve(){
	long s;
	cin>>s;
	long dp[2005]={0};
	dp[3]=1;
	for(long i=4;i<=s;i++){
		dp[i]=1;
		for(long j=3;i-j>=3;j++){
			dp[i]=(dp[i]+dp[i-j])%MOD;
		}
	}
	cout<<dp[s]<<endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long int t = 1;
    // cin>>t;
    for (long int tt = 1; tt <= t; tt++) {
        solve();
    }
    return 0;
}

Measured memory_bytes: 3624
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
		int ans = 0;

		for(int i = 1; i <= n; i++) {
			int sum = 0;
			String s = String.valueOf(i);
			for(int j = 0; j < s.length(); j++) {
				sum += Integer.parseInt(s.substring(j,j+1));
			}
			if(sum <= b && sum >= a) ans += i;
		}

		System.out.println(ans);

		sc.close();
	}
}

Measured memory_bytes: 24404