akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct P {
    bool operator<(const P &p) const {
        return a < p.a;
    }
    int a, b;
};

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

    int n;
    cin >> n;

    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    reverse(b.begin(), b.end());

    vector<int> c(n + 1, 0);
    for (int i = 0; i < n; i++) {
        c[a[i]]++;
        c[b[i]]++;
    }
    for (int cc : c) {
        if (cc > n) {
            cout << "No" << endl;
            exit(0);
        }
    }

    for (int i = 0; i < n; i++) {
        if (a[i] == b[i]) {
            int t = a[i];
            int i0 = i, i1 = i;
            for (int j = i; j < n; j++) {
                if (a[j] != b[j]) break;
                i1 = j;
            }
            i1++;
            for (int j = 0; j < n; j++) {
                if (a[j] == t || b[j] == t || i0 >= i1) break;
                swap(b[j], b[--i1]);
            }
            for (int j = n - 1; j >= 0; j--) {
                if (i0 >= i1) break;
                swap(a[j], a[i0++]);
            }
            break;
        }
    }

    int r = 1;
    vector<P> p(n);
    for (int i = 0; i < n; i++) {
        p[i] = { a[i], b[i] };
        if (a[i] == b[i]) r = 0;
    }
    sort(p.begin(), p.end());

    if (r == 0) throw;
    cout << "Yes" << '\n';
    for (int i = 0; i < n; i++) {
        cout << p[i].b << " \n"[i == n - 1];
    }

    //個数のみが重要 片方にしかない数はどうでもいい
    //足してN個を超える数があったらNo そうでない場合は？
    //

    return 0;
}

Measured memory_bytes: 7148
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int N,S;
    cin >> N;
    
    if (N==1){
        cout << 1 <<endl;
    }else{
    
    for(int i=0;i < N ; i++){
        
        S=i*i;
        
        if (S>N){
            S=(i-1)*(i-1);
            break;
        }
   
    } cout << S << endl;
    }
    
}





Measured memory_bytes: 256
#include<stdio.h>
int main()
{
    int n,k,d,x;
    scanf("%d",&n);
    scanf("%d",&k);
    int a[512]={0};
    for (int i=0;i<k;i++) {
        scanf("%d",&d);
        for (int j=0;j<d;j++) {
            scanf("%d",&x);
            if (x>0&&x<=n) {
                a[x-1]++;
            }
        }
    }
    int c=0;
    for (int i=0;i<n;i++) {
        if (a[i]==0) c++;
    }
    printf("%d",c);
    return 0;
}

Measured memory_bytes: 1692
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int number = 0;
		int resultCount = 0;
		for (int i = 0; i < N; i++) {
			number = sc.nextInt();
			if (number >= K) {
				resultCount++;
			}
		}
		System.out.println(resultCount);
	}
}

Measured memory_bytes: 47648
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <memory.h>
#include <cassert>

using namespace std;


#define all(c) ((c).begin()), ((c).end())
#define debug(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back


const double EPS = 1e-10;


typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef complex<double> P;


int main() {
	for (string s; cin >> s, s != "#"; ) {
		string k = "yuiophjklnm";
		int a = -1;
		int ans = -1;
		rep(i, s.size()) {
			int b = 0;
			rep(j, k.size()) if (k[j] == s[i]) b = 1;
			if (a != b) ans++, a = b;
		}
		cout << ans << endl;
	}
	
	return 0;
}

Measured memory_bytes: 0
(function(input) {
  var p = input.replace(/\n$/, '').split('\n');
  var m = [];
  for (var i = 0; i < p.length; i++) {
    m.push(p[i].split('').map(Number))
  }
  console.log('R' + move(m, 0, 0, 'R'));
})(require('fs').readFileSync('/dev/stdin', 'utf8'));

function move(m, x, y, d) {
  //console.log(m, x, y, d);
  if (x === 0 && y === 0 && d === 'L') {
    return '';
  }
  if (d === 'R') {
    if (mm(m, x + 1, y - 1)) {
      return 'U' + move(m, x + 1, y - 1, 'U');
    }
    if (mm(m, x + 1, y)) {
      return 'R' + move(m, x + 1, y, 'R');
    }
    if (mm(m, x + 1, y + 1)) {
      return 'D' + move(m, x + 1, y + 1, 'D');
    }
    return 'L' + move(m, x, y, 'L');
  }
  if (d === 'L') {
    if (mm(m, x, y + 1)) {
      return 'D' + move(m, x, y + 1, 'D');
    }
    if (mm(m, x - 1, y)) {
      return 'L' + move(m, x - 1, y, 'L');
    }
    if (mm(m, x, y - 1)) {
      return 'U' + move(m, x, y - 1, 'U');
    }
    return 'R'+ move(m, x, y, 'R');
  }
  if (d === 'D') {
    if (mm(m, x, y + 1)) {
      return 'R' + move(m, x, y + 1, 'R');
    }
    if (mm(m, x, y + 2)) {
      return 'D' + move(m, x, y + 2, 'D');
    }
    if (mm(m, x - 1, y + 1)) {
      return 'L' + move(m, x - 1, y + 1, 'L');
    }
    return 'U' + move(m, x, y, 'U');
  }
  if (d === 'U') {
    if (mm(m, x - 1, y - 1)) {
      return 'L' + move(m, x - 1, y - 1, 'L');
    }
    if (mm(m, x, y - 2)) {
      return 'U' + move(m, x, y - 2, 'U');
    }
    if (mm(m, x, y - 1)) {
      return 'R' + move(m, x, y - 1, 'R');
    }
    return 'D' + move(m, x, y, 'D');
  }
}

function mm(m, x, y) {
  if (y < 0 || y >= m.length) {
    return 0;
  }
  if (x < 0 || x >= m[y].length) {
    return 0;
  }
  return m[y][x] === 1;
}

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

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

  if(a<=0&&0<=b)cout<<"Zero"<<endl;
  else if(a>0&&b>0)cout<<"Positive"<<endl;
  else if((a-b)%2==0)cout<<"Negative"<<endl;
  else cout<<"Positive"<<endl;
  return 0;
}


Measured memory_bytes: 256
def main():
  ans = 1000
  d,g  = map(int,input().split())
  pc = [list(map(int,input().split())) for i in range(d)]
  pc.reverse()
  #print(pc)
  for i in range( 2 ** d):
    score = 0
    count = 0
    for j in range(d):
      if i & (1<<j):
        score += pc[j][0] * (d-j) * 100 + pc[j][1]
        count += pc[j][0]
    if score >= g:
      if ans > count:
        ans = count
      #print(bin(i),count,score)
      continue
    for j in range(d):
      if not(i &(1<<j)):
        #print("nokori"+str(j))
        c = 0
        for k in range(1,pc[j][0]):
          score += (d-j) * 100
          count += 1
          c += 1
          if score >= g:
            if ans > count:
              ans = count
            #print(bin(i),j,c,count,score)
            break
        break
  print(ans)

if __name__ == '__main__':
  ans = 0
  main()

Measured memory_bytes: 9192
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
	string s;
	int ans=0;
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]=='1'){
			ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 256
// AtCoder Regular Contest 099
// Problem F - Eating Symbols Hard
#include <cstdio>
#include <cstring>
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <algorithm>
using std::sort;
#include <vector>
using std::vector;
#include <queue>
using std::queue; using std::priority_queue;
#include <map>
using std::map;
#include <utility>
using std::pair;
typedef pair<int, int> pii;
typedef long long LL;
#define pass printf("%s : Line.%d\n", __FUNCTION__, __LINE__);
const int maxN = 250000 + 233; // 2.5e5
int n;
char opt[maxN];
int d[maxN], df; LL h1[maxN], hf1, h2[maxN], hf2;
//const LL P = 1e9 + 7;
bool isp(LL x) {
	for(int i = 2; i*i <= x; ++i)
		if(x % i == 0) return false;
	return true;
}
const LL p1 = 1000000007;
const LL p2 = 1000000123;
const LL b1 = 109;
const LL b2 = 101;
LL buc1[maxN*2], buc2[maxN*2];
LL *pow1 = &buc1[maxN], *pow2 = &buc2[maxN];
LL inv(LL x, LL p1) {
	return x == 1 ? x : (p1 - (p1 / x) * inv(p1 % x, p1)) % p1;
}
LL inc(LL &a, LL b1, LL p1) { return (a += b1) >= p1 ? a -= p1 : a; }
LL dec(LL &a, LL b1, LL p1) { return (a -= b1) < 0 ? a += p1 : a; }
map<pii, int> M;

void init() {
	pow1[0] = 1; pow2[0] = 1;
	pow1[1] = b1; pow1[-1] = inv(b1, p1); pow2[1] = b2; pow2[-1] = inv(b2, p2);
	for(int i = 1; i <= n; ++i) pow1[i] = pow1[i-1] * b1 % p1, pow2[i] = pow2[i-1] * b2 % p2;
	for(int i = -1; i >= -n; --i) pow1[i] = pow1[i+1]* pow1[-1] % p1, pow2[i] = pow2[i+1]* pow2[-1] % p2;
	hf1 = hf2 = df = 0;
	for(int i = 1; i <= n; ++i) {
		switch(opt[i]) {
			case '+':
				inc(hf1, pow1[df], p1);
				inc(hf2, pow2[df], p2);
				break;
			case '-':
				dec(hf1, pow1[df], p1);
				dec(hf2, pow2[df], p2);
				break;
			case '>':
				++df;
				break;
			case '<':
				--df;
				break;
		}
	}
}

int main() {
//	for(int i = (int)1e9; ; ++i) if(isp(i)) printf("%d\n", i), getchar();
//	printf("%.3lfMB\n", sizeof(buc1)*6/1000.0/1000.0);
	scanf("%d%s", &n, opt + 1);
	init();
	LL cnt = 0;
	M.clear();
	h1[0] = h2[0] = d[0] = 0;
	M[pii((h1[0] + hf1 * pow1[d[0]]) % p1, (h2[0] + hf2 * pow2[d[0]]) % p2)] = 1;
	for(int i = 1; i <= n; ++i) {
		d[i] = d[i-1]; h1[i] = h1[i-1]; h2[i] = h2[i-1];
		switch(opt[i]) {
			case '+':
				inc(h1[i], pow1[d[i]], p1);
				inc(h2[i], pow2[d[i]], p2);
				break;
			case '-':
				dec(h1[i], pow1[d[i]], p1);
				dec(h2[i], pow2[d[i]], p2);
				break;
			case '>':
				++d[i];
				break;
			case '<':
				--d[i];
				break;
		}
		pii c(h1[i], h2[i]), ins((h1[i] + hf1 * pow1[d[i]]) % p1, (h2[i] + hf2 * pow2[d[i]]) % p2);
		if(M.find(c) != M.end()) cnt += M[c]/*, printf("i = %d, cnt += %d\n", i, M[h1[i]])*/;
		if(M.find(ins) == M.end()) M[ins] = 1; else ++M[ins];
	}
	printf("%lld\n", cnt);
	return 0;
}


Measured memory_bytes: 28800
def abc061_b():
    n, m = map(int, input().split())
    a = [0] * m
    b = [0] * m
    for i in range(m):
        a[i], b[i] = map(int, input().split())

    for i in range(n):
        print(a.count(i + 1) + b.count(i + 1))


abc061_b()

Measured memory_bytes: 3060
#define _CRT_SECURE_NO_WARNINGS
#define M_PI 3.14159265358979
#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
#include<iomanip>
#include<vector>
#include<cmath>
#include <algorithm>

using namespace std;

int main(){
	int x,y,z,n,a,b;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> x >> y >> z;
		if (x < y){
			a = y;
			y = x;
			x = a;
		}
		if (x < z){
			b = z;
			z = x;
			x = b;
		}
		if (z*z + y*y == x*x){
			cout << "YES" << endl;
		}
		else cout << "NO" << endl;
	}
	
	
	return 0;
}

Measured memory_bytes: 1152
// #include<iostream>
// #include<algorithm>
// #include<math.h>
// using namespace std;
//
// int main(){
//   int a,b;cin>>a>>b;
//   int f=min(log10(a)+1,log10(b)+1);
//   cout<<f;
// }

#include<iostream>
#include<algorithm>
typedef long long lint;
using namespace std;

lint f(lint a,lint b)
{
	lint ret=0;
	a=max(a,b);
	while(a>0)
	{
		a/=10;
		ret++;
	}
	return ret;
}

int main()
{
	lint N;
	lint ans;

	cin >> N;

	ans=f(1,N);
	for(lint i=1;i*i<=N;i++)
	{
		if(N%i!=0)continue;
		ans=min(ans,f(i,N/i));
	}
	cout << ans << endl;
	return 0;
}


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

struct P{
	double x, y;
};

typedef P Vector;

struct Triangle{
	P ps[3];
};

int crossProduct(Vector v1, Vector v2){
	return v1.x*v2.y - v1.y*v2.x;
}

Vector pToVector(P p1, P p2){
	return {p1.x-p2.x, p1.y-p2.y};
}

bool triangle_contains(Triangle t, P p){
	int count = 0;
	for(int i=0;i<3;i++){
		Vector a = pToVector(t.ps[i], t.ps[(i+1)%3]),
			b = pToVector(p, t.ps[i]);
		if(crossProduct(a, b) > 0)count++;
		else count--;
	}
	return (count%3 != 0);
}

int main(){
	int n;
	std::cin >> n;
	for(;n--;){
		P ps[3], k, s;
		for(int i=0;i<3;i++){
			std::cin >> ps[i].x >> ps[i].y;
		}
		std::cin >> k.x >> k.y >> s.x >> s.y;
		Triangle t;
		for(int i=0;i<3;i++){
			t.ps[i] = ps[i];
		}
		bool k_contains = triangle_contains(t, k),
			s_contains = triangle_contains(t, s);
		if(k_contains != s_contains){
			puts("OK");
		}else{
			puts("NG");
		}
	}
}

Measured memory_bytes: 1184
import numpy as np


def main():
    h, w = map(int, input().split())
    s = np.array([[i == "." for i in input()] for _ in range(h)], dtype=int)
    left = s.copy()
    right = s.copy()
    up = s.copy()
    down = s.copy()
    for i in range(w - 1):
        left[:, i + 1] = (left[:, i] + 1) * s[:, i + 1]
        right[:, -i - 2] = (right[:, -i - 1] + 1) * s[:, -i - 2]
    for i in range(h - 1):
        up[i + 1] = (up[i] + 1) * s[i + 1]
        down[-i - 2] = (down[-i - 1] + 1) * s[-i - 2]
    print((left + right + up + down).max() - 3)


if __name__ == '__main__':
    main()


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

using ll = long long;
using vi = vector<int>;
using vi2 = vector<vector<int>>;
#define MP(a,b) make_pair((a),(b))
#define MT(...) make_tuple(__VA_ARGS__)

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl;

#define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl;

#define sz(x) (int)(x).size()
#define HOGE cout << "hoge" << endl;
using P = pair<ll,ll>;

template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,sz(v))i>>v[j];return i;}

int main(){
  	int x, y, z;
  	cin >> x >> y >> z;
  	cout << z << " " << x << " " << y << endl;
}

Measured memory_bytes: 3620
#include <stdio.h>

int main(void){
    int n,i,k,count;
    char c;
    scanf("%d",&n);
    getchar();
    k=count=0;
    for(i=0;i<n;i++){
        c=getchar();
        if(c=='A'&&k==0)
            k++;
        else if(c=='B'&&k==1)
            k++;
        else if(c=='C'&&k==2)
            k++;
        else if(c=='A'&&k==1)
            k=1;
        else
            k=0;
        if(k==3){
            count++;
            k=0;
        }
        
    }
    printf("%d",count);
    return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<string>
using namespace std;
int main(){
	int h,w;
	while(cin>>h>>w){
		if(h==0&&w==0)
			break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(i==0||i==h-1||j==0||j==w-1)
					cout<<"#";
				else
					cout<<".";
			}
			cout<<endl;
		}
		cout<<endl;
	}
	return 0;
}

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

using namespace std;

int getCombinationNum(const int target, const int upperLimit);
int canMake2Combination(int target, const int upperLimit, const int lowerLimit);

int main(){
	int n, x, count;
	vector<int> output;
	while(1){
		cin >> n;
		cin >> x;
		if(0 == n && 0 == x) {
			output.push_back(-1);
			break;
		}
		output.push_back(getCombinationNum(x, n));
	}

	for(const int& e : output){
		if(-1 == e) { break; }
		cout << e << endl;
	}

	return 0;
}

int getCombinationNum(const int target, const int upperLimit){
	int _target, count, iMax;
	count = 0;
	iMax = target / 3 - 1;

	for(int i = 1; i <= iMax; ++i) {
		int _target = target - i;
		if(upperLimit * 2 <= _target) { continue; }
		count += canMake2Combination(_target, upperLimit, i + 1);
	}
	return count;
}

int canMake2Combination(int target, const int upperLimit, const int lowerLimit){
	int count, iMax;
	count = 0;
	iMax = (0 == target % 2) ? target / 2 - 1 : target / 2;

	for(int i = lowerLimit; i <= iMax; ++i){
		int _target = target - i;
		if(upperLimit >= _target) { count++; }
	}
	return count;
}


Measured memory_bytes: 3140
MOD=10**9+7
cnt=0; sum=0; prev=nil; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
n = gi
ab = n.times.map{gsmi}.sort{|x,y|x[1]<=>y[1]}

can = true
t = 0
# 締め切り近い順にやる
n.times do |i|
  time = ab[i][0]
  dl = ab[i][1]
  t += time
  if t>dl
    can = false
    break
  end
end

puts_yesno(can)

Measured memory_bytes: 19320
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <set>

using namespace std;

int main()
{
	int l, r, d;
	cin >> l >> r >> d;

	int ans = 0;

	for (int i = l; i <= r; i++) {
		if (i % d == 0) {
			ans++;
		}
	}
	cout << ans << "\n";
}

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

int main(void) {

	int n;
	cin >> n;
	int R[n];
	cin >> R[0];
	int minv = R[0];
	int maxv = -1000000000;

	for (int j = 1; j < n; j++) {
		cin >> R[j];
		maxv = max(maxv, R[j]-minv);
		minv = min(minv, R[j]);
	}

	cout << maxv << endl;


	return 0;
}

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

int INF = 1e9;

int main(){
	int n; cin >> n;
	vector<int> p(n);
	rep(i,n) cin >> p[i];
	int ans=0;
	int cmin = INF;
	rep(i,n){
		cmin = min(cmin, p[i]);
		cmin==p[i] ? ans++ : 0;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1024
cards = [[False for i in range(13)] for j in range(4)]
pattern = ['S', 'H', 'C', 'D']
n = int(input())
# i,j = 0, 0
# while i < n:
for i in range(n):
    a,b = input().split()
    rank = int(b)
    if a=='S':a=0
    elif a=='H':a=1
    elif a=='C':a=2
    else: a=3
    cards[a][rank-1]=True
    # i =+1
for i in range(4):
    for j in range(13):
        if cards[i][j]==False:
            print(pattern[i],j+1)


Measured memory_bytes: 5604
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
using namespace std;
using ll = long long;
const int INF = +100100100;
typedef pair<int,int> P;

int main(){
    int n,w;
    cin >> n >> w;
    int MAX = 10000+500;
    vector<vector<int>> dp(n+10,vector<int>(MAX));
    rep(i,w+1)dp[0][i]=0;
    rep(i,n+1)dp[i][0]=0;
    vector<P> items;
    items.push_back(make_pair(0,0));
    rep(i,n){
        int val,wei;
        cin >> val >> wei;
        items.push_back(make_pair(val,wei));
    }
    rrep(j,w){
        rrep(i,n){
            if(j-items[i].second<0)dp[i][j] = dp[i-1][j];
            else dp[i][j] = max(dp[i-1][j-items[i].second]+items[i].first, dp[i-1][j]);
            // cout << dp[i-1][j-items[i].second]+items[i].first << endl;
        }
    }
    // rep(i,n+1){
    //     rep(j,w+1){
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    cout << dp[n][w]<<endl;
    return 0;
}


Measured memory_bytes: 7356
#include<iostream>
using namespace std;
typedef unsigned long long int ll;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n,s=0;
  cin>>n;
  ll a[n];
  for(ll i=0;i<n;i++)
    cin>>a[i];
  for(ll i=1;i<n;i++)
  {
    if(a[i-1]<=a[i]);
    else
    {
      s+=a[i-1]-a[i];
      a[i]+=a[i-1]-a[i];
    } 
  }
  cout<<s<<"\n";
  return 0;
} 

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

using namespace std;

int main()
{
    int n, m, d, r;
    
    scanf("%d", &n);
    scanf("%d", &m);
    
    vector<int> s(n); s[0] = 0;
    vector<int> a(m + 1); a[0] = 0;
    
    for(int i = 1; i < n; i++)
    {
        scanf("%d", &d);
        
        s[i] = d + s[i - 1];
    }
    
    int ret = 0;
    
    for(int i = 1; i <= m; i++)
    {
        scanf("%d", &r);
        
        a[i] = r + a[i - 1];
        
        ret = (ret + abs(s[a[i]] - s[a[i - 1]])) % 100000;
    }
    
    printf("%d\n", ret);
    
    return 0;
}

Measured memory_bytes: 3616
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        A solver = new A();
        solver.solve(1, in, out);
        out.close();
    }

    static class A {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            Set<Character> S = new HashSet<>();
            char[] a = in.next().toCharArray();
            for (int i = 0; i < a.length; i++) {
                S.add(a[i]);
            }
            int ans = S.size();

            // 出力
            boolean ok = false;
            if (ans == 2) ok = true;
            out.println(ok ? "Yes" : "No");

        }

    }
}



Measured memory_bytes: 35668
#include<iostream>
#include <vector>
#define rep(i,n) for(int i = 0;i<n;i++)
typedef long long ll;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

const int INF = 1000000000;
int main() {
    int n,k;
    std::cin >> n >> k;
    std::vector<int>h(n);
    rep(i,n){
        std::cin >> h[i];
    }
    std::vector<int>dp(n,INF);
    dp[0]=0;
    rep(i,n){
        for(int j=i+1;j<=k+i;j++){
            if(j>n){
                break;
            }
            chmin(dp[j],dp[i]+abs(h[i]-h[j]));
        }
    }
    std::cout << dp[n-1] << std::endl;
}


Measured memory_bytes: 1024
#include<iostream>
using namespace std;
int main(){
  int a,i,sum;
  sum = 0;
  cin >> a;
  for(i=0;i<6;i++){
    sum += a%10;
    a = a/10;
  }
  if(sum == 1)
    sum = 10;
  cout << sum << "\n";
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <utility>
#include <functional>
#define debug(x) cout << #x << " = " << x << endl
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repr(i,n) for(int i = (n); i > -1; i--)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const double EPS = 1e-9;

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

  ll n,a,b;
  cin>>n>>a>>b;
  ll ans = 0;
  if(a > b || n == 1 && a != b) {
    ans = 0;
  } else if(n==1) {
    ans = 1;
  } else {
    ans = b * (n - 1) + a - a * (n-1) - b + 1;
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define befv(V) ((V)[(sz(V)-2)])
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
void fuk() { puts("NO"); exit(0); }
void fg(vector<int> G[], int a, int b) { G[a].eb(b); G[b].eb(a); }

const bool debug = 0;
const int MAXN = 200055;
const int MAXK = 200055;

vector<int> G[MAXN];
bitset<MAXN> chk;

int D[MAXN], E[MAXN];

int B[MAXK], C[MAXK];
char A[MAXN];

int N, K;

int f(int i) {
	int ret = E[i]; chk[i] = true;
	for(int v : G[i]) if(!chk[v]) {
		ret = (ret + f(v)) % 26;
	}
	return ((ret % 26) + 26) % 26;
}

int main() {
	scanf(" %s", A+1);
	N = int(strlen(A+1));
	scanf("%d", &K);
	for(int i = 1; i <= K; i++) scanf("%d%d", &B[i], &C[i]);

	if(N&1) {
		N++;
		for(int i = N-1; N/2 <= i; i--) A[i+1] = A[i];
		for(int i = 1; i <= K; i++) {
			if(N/2 < B[i]) B[i]++;
			if(N/2 <= C[i]) C[i]++;
		}
	}

	for(int i = 1; i <= K; i++) {
		if(C[i] <= N/2) continue;
		if(N/2 < B[i]) {
			B[i] = N+1-B[i];
			C[i] = N+1-C[i];
			swap(B[i], C[i]);
			continue;
		}
		int l = N/2-B[i]+1, r = C[i]-N/2;
		if(l > r) swap(l, r);
		B[i] = N/2-r+1;
		C[i] = N/2-l;
	}

	if(debug) {
		printf("N=%d, K=%d\n", N, K);
		for(int i = 1; i <= N; i++) putchar(A[i]); puts("");
		for(int i = 1; i <= K; i++)
			printf("%d ; %d %d\n", i, B[i], C[i]);
	}

	N /= 2;
	for(int i = 1; i <= N; i++)
		D[i] = (A[N*2+1-i] - A[i] + 26) % 26;
	for(int i = 1; i <= N+1; i++)
		E[i] = ((D[i] - D[i-1] + 26) % 26 + 26) % 26;
	for(int i = 1; i <= K; i++) C[i]++;
	N++;

	for(int i = 1; i <= K; i++) if(B[i] < C[i]) fg(G, B[i], C[i]);
	for(int i = 1; i <= N; i++) if(!chk[i]) {
		int t = f(i);
		if(t) fuk();
	}

	puts("YES");
	return 0;
}

Measured memory_bytes: 10880
n = int(input())
a=list(map(int,input().split()))
cnt,cnt2,cnt4=0,0,0
for val in a:
    if val%4==0:
        cnt4+=1
    elif val%2==0:
        cnt2+=1
    else:
        cnt+=1
if cnt2==n: print("Yes")
elif cnt2==0:
    if cnt4>=cnt-1: print("Yes")
    else: print("No")
else:
    if cnt4>=cnt: print("Yes")
    else: print("No")

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

int main(){
	int n;
	cin>>n;
	
	char a[101];
	int b=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		
		if(a[i]=='Y') b=1;
	}
	
	if(b) cout<<"Four"<<endl;
	else cout<<"Three"<<endl;
	
	return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int N=2005;
int a[N],fst[N],nxt[N],d[N],n;
bool u[N];
priority_queue<int> q;
void addedge(int x,int y){
	nxt[y]=fst[x];fst[x]=y;d[y]++;
}
int gcd(int x,int y){
	return y?gcd(y,x%y):x;
}
void dfs(int x){
	u[x]=1;
	for (int i=1;i<=n;i++)
	if (!u[i] && gcd(a[x],a[i])>1){
		addedge(x,i);
		dfs(i);
	}
}
int main(){
	scanf("%d\n",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	for (int i=1;i<=n;i++)
		if (!u[i]) dfs(i);
	for (int i=1;i<=n;i++)
		if (!d[i]) q.push(i);
	for (;!q.empty();){
		int x=q.top();q.pop();
		printf("%d ",a[x]);
		for (int i=fst[x];i;i=nxt[i])
			if (!--d[i]) q.push(i);
	}
}

Measured memory_bytes: 384
#include <stdio.h>

int main(void){

  int n, l;
  int sum;//りんごn個すべての和
  int ans;

  scanf("%d %d", &n, &l);

  sum=(n*(2*l+n-1))/2;

  if(0<l){
    ans=sum-l;
  }else if(l+n-1<0){
    ans=sum-(l+n-1);
  }else{
    ans=sum;
  }
  printf("%d\n", ans);

  return 0;
}


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

using namespace std;


int main() {
    int n;
    cin>>n;
    vector<int> a;
    vector<int> b;
    vector<int> c;
    int temp;
    
    for(int i=0;i<n;i++)
    {
        cin>>temp;
        a.push_back(temp);
    }
    for(int i=0;i<n;i++)
    {
        cin>>temp;
        b.push_back(temp);
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>temp;
        c.push_back(temp);
    }
    int sum=0;
    
    for(int i=0;i<n;i++)
    {
        sum=sum+b[a[i]-1];
        
        if(a[i+1] == a[i]+1)
        {
            sum+=c[a[i]-1];
        }
    }
    cout<<sum;
    
}

Measured memory_bytes: 256
#pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <cassert>
#include <complex>
#include <time.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define FILL(A,val) memset(A,val,sizeof(A))
#define ITER(it,a) for(__typeof(a.begin()) it=a.begin();it!=a.end();++it)
#define DBG1(a) cerr<<#a<<"="<<(a)<<"\n"
#define DBG2(a,b) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"

#define ALL(V) V.begin(),V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = 1LL * INF * INF;



int main() 
{
	//freopen("in.txt", "r", stdin);
	//ios::sync_with_stdio(0);cin.tie(0);

	string s;
	cin >> s;
	int res = 0;
	FOR(i, 0, SZ(s))
	{
		if (i % 2 == 0)
		{
			if (s[i] == 'p') res--;
		}
		if (i % 2 == 1)
		{
			if (s[i] == 'g') res++;
		}
	}

	cout << res << endl;

	return 0;
}

Measured memory_bytes: 512
#include "bits/stdc++.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int,int> pii;
#define int ll
#define fi first
#define se second
#define Sort(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; i++) 
#define REP(i,n) for(int i = 0;i < (n) ; i++) 
#define MP(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define INF LLONG_MAX/4
#define INTINF INT_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
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;}
/*----------------------------------------------------------------*/
// int MOD = 998244353;
int MOD = 1000000007;

int rgb[3][40001] = {};
string t = "RGB";

/*----------------------------------------------------------------*/
signed main(){
	
	int n;
	cin >> n;
	string s;
	cin >> s;

	rep(i,n){
		rep(j,3){
			if(s[i] == t[j])rgb[j][i+1] = rgb[j][i]+1;
			else rgb[j][i+1] = rgb[j][i];
		}
	}

	int ans = 0;
	rep(i,n){
		int c;
		rep(j,3)if(s[i] == t[j])c = j;
		int c1 = (c+1)%3;
		int c2 = (c+2)%3;
		ans += rgb[c1][i] * (rgb[c2][n] - rgb[c2][i+1]);
		ans += rgb[c2][i] * (rgb[c1][n] - rgb[c1][i+1]);
		for(int j = 1;i-j >= 0 && i+j < n;j++){
			if(s[i-j] == t[c1] && s[i+j] == t[c2])ans--;
			if(s[i-j] == t[c2] && s[i+j] == t[c1])ans--;
		}
	}
	cout << ans << endl;
	

	
	return 0;
}
/*----------------------------------------------------------------*/

// g++ -std=gnu++17 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl https://code-festival-2018-quala.contest.atcoder.jp/tasks/code_festival_2018_quala_c
// rm -r -f test;oj dl https://pakencamp-2019-day3.contest.atcoder.jp/tasks/pakencamp_2019_day3_c
// rm -r -f test;oj dl https://atcoder.jp/contests/abc162/tasks/abc162_b


Measured memory_bytes: 3700
from decimal import Decimal
import numpy as np

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

deg = h*30 + m/2 - m*6#度
length = Decimal(str(a*a + b*b - 2*a*b*np.cos(np.deg2rad(deg))))**Decimal('0.5')

print(length)

Measured memory_bytes: 27316
#include <cstdio>

int main(void)
{
	int n;
	int i;
	long sum;
	
	sum = 1;
	
	scanf("%d", &n);
	
	for (i = 0; i < n; i++){
		sum *= (n - i);
	}
	
	printf("%ld\n", sum);
	
	return (0);
}

Measured memory_bytes: 1020
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FASTIO
#define endl "\n"  // Since endl forces a buffer flush which slows down large I/O operations.
#define ff first
#define ss second
#define pb push_back
#define input(c, n) for (int i = 0; i < n; ++i) cin >> c[i];
#define exit_message(s) return 0 * printf(s)
#define show_all(c) for (auto i: c) cout << i << " "; cout << "\n";
#define display(c, a, n) cout << "Displaying array \"" << #c << "\" : \n"; for (int (i) = (a); (i) < (n); ++(i)) cout << (c)[(i)] << " "; cout << "\n";
#define forl(i, a, n) for (int i = (a); (i) < (n); ++(i))
#define dugbe(k) cout << "-\t> " << #k << " = " << k << "\n";

// What follows is a magical ordered set data structure.
// Supports the following functions:
// find_by_order(int k) - returns k'th smallest element (0-indexed)
// order_of_key(int k) - returns number of elements strictly smaller than k.
using namespace __gnu_pbds;
#define ordered_set tree <ll, null_type, greater_equal <ll>, rb_tree_tag, tree_order_statistics_node_update>

/* TIPS FOR DEBUGGING
 * Check if values are not overflowing (use long long where required)
 * Check if ranges are inclusive or not.
 * Check properly if greedy will work or not before using it.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Keep adding to this list.
*/

using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
typedef pair<ll, ll> prll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pr> vpr;
ifstream in("input.txt");
const ll mod = 1e9 + 7;
const long double eps = 1e-7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

long long true_rand(long long n)
{
	// Returns a random number between 0 and n - 1 inclusive using mt19937.
    uniform_int_distribution<long long> uid(0, n - 1);
    return uid(rng);
}

ll mod_pow(ll a, ll b)
{
	if (b == 0)
		return 1LL;

	ll res = mod_pow(a, b >> 1);
	res = (res * res) % mod;
	if (b & 1LL)
		res = (res * a) % mod;
	
	return res;
}

const int N = 2e5 + 5;

int32_t main()
{
	#ifdef FASTIO
		ios_base::sync_with_stdio(false);
		cin.tie(NULL);
	#endif
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

	string s, t;
	cin >> s >> t;

	vi mp[26];
	forl(i, 0, (int)s.length())
	{
		int cx = s[i] - 'a';
		mp[cx].pb(i);
	}

	int pos = 0; // Position in s.
	int fail = 0;
	ll cnt = 0;
	for (int k = 0; k < (int)t.length(); )
	{
		// Character to match: t[k]
		int key = t[k] - 'a';

		if ((int)mp[key].size() == 0)
		{
			fail = 1;
			break;
		}

		auto temp = lower_bound(mp[key].begin(), mp[key].end(), pos);
		if (temp == mp[key].end())
		{
			// No matches.
			++cnt;
			pos = 0;
		}
		else
		{
			pos = (*temp) + 1;
			++k;
		}
	}
	ll len = (int)s.length();
	cnt = (cnt * len) + pos;

	if (fail)
	{
		cnt = -1;
	}
	cout << cnt << "\n";

	return 0;
}

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

int main(){
    string s,t;
    cin >> s >> t;
    int ans = 0;
    for(int i = 0;i < s.size();i++){
        if(s.at(i) != t.at(i)) ans++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3912
while True:
    N, M = map(int, raw_input().split())
    if (N, M) == (0, 0):
        break
    rules = []
    for _ in xrange(N):
        f, p1, p2 = raw_input().split()
        rules += [(f, p1+p2)]
    rules = rules[::-1]
    def check(s):
        def match(pattern, address):
            for p, a in zip(pattern, address):
                if p == "?":
                    continue
                if p != a:
                    return False
            return True
        a1, a2, _ = s.split()
        for flag, pattern in rules:
            if flag == "permit" and match(pattern, a1+a2):
                return True
            if flag == "deny" and match(pattern, a1+a2):
                return False
    ans = filter(check, [raw_input() for _ in xrange(M)])
    print len(ans)
    if len(ans) != 0:
        print "\n".join(ans)

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

int main() {
  int N;
  cin >> N;
  vector<string> S;
  string s;
  while (cin >> s) {
    S.push_back(s);
  }

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

  auto last = unique(S.begin(), S.end());

  S.erase(last, S.end());

  cout << S.size() << endl;
}

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

#define S_MAX     1000
#define STR_BUF_MAX S_MAX+1
#define BUFF_MAX    S_MAX+20

#define DEBUG_MODE  0

void print(char *str, int a, int b)
{
    int i;

    for(i=a;i<b+1;i++) {
        printf("%c", str[i]);
    }
    putchar('\n');

    return;
}

void reverse(char *str, int a, int b)
{
    int i;
    int d = b-a+1;
    char c;

    for(i=0;i<d/2;i++) {
        c = str[i+a];
        str[i+a] = str[d-i+a-1];
        str[d-i+a-1] = c;
    }

#if DEBUG_MODE == 1
    printf("%s\n", str);
#endif

    return;
}

void replace(char *str, int a, int b, char *p)
{
    int i;
    int d = b-a+1;

    for(i=0;i<d;i++) {
        str[i+a] = p[i];
    }

#if DEBUG_MODE == 1
    printf("%s\n", str);
#endif

    return;
}

int judge_order(const char *str)
{
    if(str[0] == 'p')       return 1;
    else if(str[2] == 'v')  return 2;
    else                    return 3;
}

void excute_order(int order,char *str, int a, int b, char *tmp)
{
    switch(order) {
     case 1: print(str, a, b);  break;
     case 2: reverse(str, a, b);    break;
     case 3: replace(str, a, b, tmp);   break;
     default: break;
    }

    return;
}

int main(void)
{
    int i;
    int n;
    char str[STR_BUF_MAX];
    char buf[BUFF_MAX];
    char *tok;
    char tmp[] = " \n";

    scanf("%s", str);

#if DEBUG_MODE == 1
    printf("%s\n", str);
#endif

    scanf("%d%*c", &n);

    for(i=0;i<n;i++) {
        int order;
        int a, b;

        fgets(buf, BUFF_MAX, stdin);

        tok = strtok(buf, tmp);     order = judge_order(tok);
        tok = strtok(NULL, tmp);    a = atoi(tok);
        tok = strtok(NULL, tmp);    b = atoi(tok);
        if(order == 3) tok = strtok(NULL, tmp);

        excute_order(order, str, a, b, tok);
    }

    return 0;
}

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

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int main() {
    int N, K;
    cin >> N >> K;
    int maxA = 0, g = 0;
    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;
        maxA = max(maxA, a);
        g = gcd(g, a);
    }

    if (K % g == 0 && K <= maxA) {
        cout << "POSSIBLE" << endl;
    } else {
        cout << "IMPOSSIBLE" << endl;
    }

    return 0;
}

Measured memory_bytes: 256
a,b,c,d=map(int, input().split())
if c-a>=0:
    if b-d>=0:
        print(d-c)
    elif b-c>=0:
        print(b-c)
    else:
        print(0)
else:
    if d-b>=0:
        print(b-a)
    elif d-a>=0:
        print(d-a)
    else:
        print(0)

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);

  int64_t n, k;
  cin >> n >> k;

  int64_t ans;
  if (n > k) {
    n %= k;
  }
  if (n == 0 || n == k) {
    ans = 0;
  } else if (n < k - n) {
    ans = n;
  } else {
    ans = k - n;
  }

  cout << ans << '\n';

  return 0;
}


Measured memory_bytes: 3672
N=int(input())
alist=list(map(int,input().split()))

def bs_left(mid,mp):
  sum2=abs(2*slist[mid]-slist[mp])
  if mid==0:
    sum1=sum2
  else:
    sum1=abs(2*slist[mid-1]-slist[mp])
  if mid==mp:
    sum3=sum2
  else:
    sum3=abs(2*slist[mid+1]-slist[mp])

  if sum2<=sum1 and sum2<=sum3:
    return 0
  elif sum1<sum2:
    return -1
  elif sum2>sum3:
    return 1    

def bs_right(mid,mp):
  sum2=abs(2*(slist[mid]-slist[mp])-(slist[-1]-slist[mp]))
  if mid==0:
    sum1=sum2
  else:
    sum1=abs(2*(slist[mid-1]-slist[mp])-(slist[-1]-slist[mp]))
  if mid==mp:
    sum3=sum2
  else:
    sum3=abs(2*(slist[mid+1]-slist[mp])-(slist[-1]-slist[mp]))

  if sum2<=sum1 and sum2<=sum3:
    return 0
  elif sum1<sum2:
    return -1
  elif sum2>sum3:
    return 1    
  
slist=[]
asum=0
for a in alist:
  asum+=a
  slist.append(asum)
#print(alist)
#print(slist)

dic_lp,dic_rp={},{}
for mp in range(1,N-2):  
  #lp
  l,r=0,mp
  while(l<=r):
    mid=(l+r)//2
    ret=bs_left(mid,mp)
    if ret==1:
      l=mid+1
    elif ret==-1:
      r=mid-1
    else:
      dic_lp[mp]=mid
      #print(mp,mid)
      break
  
  #rp
  l,r=mp+1,N-2
  while(l<=r):
    mid=(l+r)//2
    ret=bs_right(mid,mp)
    if ret==1:
      l=mid+1
    elif ret==-1:
      r=mid-1
    else:
      dic_rp[mp]=mid
      #print(mp,mid)
      break
    
min_diff=10**9
for mp in range(1,N-2):
  lp=dic_lp[mp]
  rp=dic_rp[mp]
  
  sum1=slist[lp]
  sum2=slist[mp]-slist[lp]
  sum3=slist[rp]-slist[mp]
  sum4=slist[-1]-slist[rp]
  #print(sum1,sum2,sum3,sum4)
  
  sum_list=[sum1,sum2,sum3,sum4]
  sum_list.sort()
  diff=sum_list[-1]-sum_list[0]
  #print(lp,mp,rp,diff)
  if diff<min_diff:
    min_diff=diff    
  
print(min_diff)


Measured memory_bytes: 122548
#include<iostream>

using namespace std;

struct Card{
    char suit, value;
};

void bubbleSort(struct Card A[], int N){
    for(int i = 0; i < N; i++){
        for(int j = N - 1; j >= i+1; j--){
            if(A[j].value < A[j-1].value){
                Card t = A[j]; A[j] = A[j-1]; A[j-1] = t;
            }
        }
    }
}

void selectionSort(struct Card A[], int N){
    for(int i = 0; i < N; i++){
        int minj = i;
        for(int j = i; j < N; j++){
            if(A[minj].value > A[j].value) minj = j;
        }
        Card t = A[i]; A[i] = A[minj]; A[minj] = t;
    }
}

bool judgeStable(struct Card A[], struct Card B[], int N){
    for(int i = 0; i < N; i++) if(A[i].suit != B[i].suit) return false;
    return true;
}

void printData(struct Card A[], int N){
    for(int i = 0; i < N; i++){
        if(i != 0) cout << " ";
        cout << A[i].suit << A[i].value;
    }
    cout << endl;
}

int main(){
    struct Card C1[100], C2[100];
    int N;
    
    cin >> N;

    for(int i = 0; i < N; i++) cin >> C1[i].suit >> C1[i].value;

    for(int i = 0; i < N; i++) C2[i] = C1[i];

    bubbleSort(C1, N);
    selectionSort(C2, N);

    printData(C1, N);
    cout << "Stable" << endl;

    printData(C2, N);
    if(judgeStable(C1, C2, N)){
        cout << "Stable" << endl;
    }else{
        cout << "Not stable" << endl;
    }


    return 0;
}




Measured memory_bytes: 3096
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))

res=0

for i in range(n):
	if y[i]<x[i]:
		res+=y[i]
	else:
		if x[i+1]-(y[i]-x[i])>0:
			res+=y[i]
			x[i+1]=x[i+1]-(y[i]-x[i])
		else:
			res+=x[i]+x[i+1]
			x[i+1]=0

print(res)

Measured memory_bytes: 19156
N=int(input())
A=[0]*N
for i in range(N):
    A[i]=int(input())

j=0
ans=0
count=[0]*N
while j!=1:
    ans+=1
    j=A[j]-1
    count[j]+=1
    if count[j]>=2:
        ans=-1
        break

print(ans)

Measured memory_bytes: 7796
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)

int main(){
    int n;
    cin>>n;
    
    vector<int> v(n);
    rep(i,n)cin>>v[i];
    
    sort(all(v));
    
    cout<<v[v.size()-1]-v[0]<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
#define rep(i,n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
using ll=long long;
using P=pair<ll,ll>;
using PP=tuple<ll,ll,ll>;
using S=multiset<ll>;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
const ll inf=1001001001001001;
const int INF=1001001001;
const int mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
void decimal(int x){cout<<fixed<<setprecision(x);}
ll GCD(ll a,ll b){if(b==0) return a; return GCD(b,a%b);}
ll LCM(ll a,ll b){return(a/GCD(a,b))*(b/GCD(a,b))*GCD(a,b);}
ll POW(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*POW(a,b-1)%mod;ll k=POW(a,b/2);return k*k%mod;}
vi calc(ll x){vi res;while(x>0){res.eb(x%10);x/=10;}reverse(all(res));return res;}

void solve(){
  string s; cin>>s;
  int n=s.size();
  vvi dp(n,vi(13,0));
  rep(i,n){
    if(i==0){
      if(s[0]=='?'){
        rep(j,10) dp[0][j]=1;
      }
      else dp[0][s[i]-'0']=1;
    }else{
      if(s[i]=='?'){
        rep(k,10){
          rep(j,13){
            dp[i][(j*10+k)%13]+=dp[i-1][j];
            dp[i][(j*10+k)%13]%=mod;
          }
        }
      }else{
        int p=s[i]-'0';
        rep(j,13){
          dp[i][(j*10+p)%13]=dp[i-1][j];
        }
      }
    }
  }
  out(dp[n-1][5]);
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}


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

int main() {
  string s;
  ll k;
  cin >> s >> k;
  char c = '1';
  for (ll i = 0; i < k; i++) {
    if (s[i] != '1') {
      c = s[i];
      break;
    }
  }
  cout << c << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <deque>
using namespace std;

#define REP(i,n) for(int i = 0; i < n; i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};
ll MOD = 1000000007;

// int h[100009];
int main(){
	int n;
	long long int a,b;
	cin>>n>>a>>b;
  ll h[n];
	for(int i=0;i<n;i++){
		cin>>h[i];
	}
	long long int sa=a-b;
	long long int mi=0,ma=1000000000,mid;
	while(ma-mi>1){
		mid=(mi+ma)/2;
		long long int cnt=0;
		for(int i=0;i<n;i++){
			if(h[i]>b*mid){
				cnt+=(h[i] - b * mid + a - b - 1) / (a-b);
			}
		}
		if(cnt>mid){
			mi=mid;
		}
		else{
			ma=mid;
		}
	}
	cout<<ma<<endl;
	return 0;
}


Measured memory_bytes: 1024
//#include "pch.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <utility>
#include <queue>
#include <algorithm>
#include <functional>
#include <string>
#include <bitset>

#define ll long long
#define fri(n) for(i=0;i<(n);i++)
#define frj(n) for(j=0;j<(n);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define INF 1000000000000000000//10^18
#define INFINT 2000000001//2*10^9+1
#define MOD 1000000007
#define MODANOTHER 998244353
#define PI acos(-1)

using namespace std;

struct bangumi_t{
	int s;
	int t;
	int c;
};

int compare_channel(const void *a, const void *b) {//降順と昇順あってる？？
	struct bangumi_t *A = (struct bangumi_t *)a;
	struct bangumi_t *B = (struct bangumi_t *)b;
	if(A->c - B->c >0){
		return 1;
	}else if(A->c - B->c <0){
		return -1;
	}else{
		return 0;
	}
}


int main(void)
{
	//変数の宣言
	int n;
	int b[110];

	//よく使う変数
	int i,j,k,l;
	int flag=0;
	int ans=0;
	int count=0;
	int temp=0;
	int temp1=0;
	int temp2=0;
	int temp3=0;
	int max=0;
	int min=INFINT;
	int len=0;
	int sum=0;
	int ok=0;
	int ng=0;
	char dummy;
	char stemp[11];
	
	//データの読み込み


	scanf("%d",&n);
//	scanf_s("%d",&n);

//	scanf("%s",&s);
//	scanf_s("%s",&s,5);


	for(i=0;i<n-1;i++){
		scanf("%d",&b[i]);
//		scanf_s("%d",&b[i]);
	}


/*	for(i=0;i<n;i++){
//		scanf("%d",&c[i]);
		scanf_s("%d",&c[i]);
		d[i]=0;
	}*/


	//	printf("nは%dです\n", n);
	//	printf("データの読み込み終了\n");
	//実際の処理

//	clock_t start=clock();

	sum=b[0]+b[n-2];
	for(i=0;i<n-2;i++){
		sum=sum+min(b[i],b[i+1]);
	}

//	clock_t end=clock();

	//	printf("計算部分終了\n");

	//出力
	printf("%d",sum);

//	printf("time=%lf",(double)(end-start)/CLOCKS_PER_SEC);
	//	printf("結果の出力終了\n");

	return 0;
}



Measured memory_bytes: 256