akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
int main(){
  char f[55];
  int c[55][3]={},i,j,k,q,w,sum,min,e,t,y;
  scanf("%d %d",&t,&y);
  for(i=0;i<t;i++){
    scanf("%s",f);
    for(j=0;j<y;j++){
      if(f[j]=='W')c[i][0]++;
      else if(f[j]=='B')c[i][1]++;
      else if(f[j]=='R')c[i][2]++;
    }
  }
  min=2600;
  for(i=0;i<=t-3;i++){
    for(j=i+1;j<=t-2;j++){
      //printf("%d %d  ",i,j);//// 
      sum=0;
      for(k=0;k<=i;k++){
	sum+=y-c[k][0];
      }
      for(w=i+1;w<=j;w++){
	sum+=y-c[w][1];
      }
      for(e=j+1;e<=t-1;e++){
	sum+=y-c[e][2];
      }
      if(min>sum){
	min=sum;
	//printf("%d %d %d  ",i,j,min);///
      }
    }
  }
  printf("%d\n",min);
  return 0;
}

Measured memory_bytes: 1024
/* template.cpp {{{ */
#include <bits/stdc++.h>
using namespace std;
#define get_macro(a, b, c, d, name, ...) name
#define rep(...) get_macro(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) get_macro(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define rep1(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, s) for (ll i = (a); i < (ll)(b); i += (ll)(s))
#define rrep1(n) rrep2(i_, n)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep3(i, a, b) rrep4(i, a, b, 1)
#define rrep4(i, a, b, s) for (ll i = (ll)(b) - 1; i >= (ll)(a); i -= (ll)(s))
#define each(x, c) for (auto &&x : c)
#define fs first
#define sc second
#define all(c) begin(c), end(c)
using ui = unsigned;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int inf = 1e9 + 10;
const ll inf_ll = 1e18 + 10;
const ll mod = 1e9 + 7;
const ll mod9 = 1e9 + 9;
const int dx[]{-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[]{0, -1, 0, 1, -1, -1, 1, 1};
template<class T, class U> void chmin(T &x, const U &y){ x = min<T>(x, y); }
template<class T, class U> void chmax(T &x, const U &y){ x = max<T>(x, y); }
struct prepare_ { prepare_(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(12); } } prepare__;
/* }}} */

int main(){
  int a, b;
  char c;
  cin >> a >> c >> b;
  if (c == '+') cout << a + b << endl;
  else cout << a - b << endl;
}


Measured memory_bytes: 256
import collections

i = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
li = list(set(li))
li.sort(reverse=True)
ans = 1
x = 0
i = 0

for i in li:
    if c[i] > 3 and x == 0:
        ans = i**2
        x += 2
    elif c[i] > 1:
        ans *= i
        x += 1
    if x == 2:
        break

if x == 2:
    print(ans)
else:
    print(0)

Measured memory_bytes: 77720
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define range(i,a,n) for(ll i=(a);i<(n);i++)
#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI (3.1415926535897932384)

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S sum(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
int sgn(const double&r){return (r>EPS)-(r<-EPS);} // a>0  : sgn(a)>0
int sgn(const double&a,const double&b){return sgn(a-b);} // b<=c : sgn(b,c)<=0

template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>void tf(bool b,S t,T f){if(b)puta(t);else puta(f);}
void YN(bool b){tf(b,"YES","NO");}
void Yn(bool b){tf(b,"Yes","No");}
void yn(bool b){tf(b,"yes","no");}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1;for(auto s:t){os<<(a?"":" ")<<s;a=0;}return os;}
template<class S>auto&operator>>(istream&is,vector<S>&t){for(S&a:t)cin>>a;return is;}

/*他のライブラリを入れる場所*/


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n;
	cin>>n;
	ll ans=0;
	range(i,1,n-1){
		if(i < n-i)ans++;
	}
	puta(ans);
	return 0;
}

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

#define INF 1000000

void make_multiplier(int x, int N, vector<int>& coins) {
    int num = x;
    while(num <= N) {
        coins.push_back(num);
        num *= x;
    }
}

int main() {
    int N;
    cin >> N;
    int dp[100010];
    for(int i = 0;i < 100010;i++) dp[i] = INF;
    dp[0] = 0;
    vector<int> coins = {1};
    make_multiplier(6, N, coins);
    make_multiplier(9, N, coins);
    sort(coins.begin(), coins.end());
    unique(coins.begin(), coins.end());

    int m = coins.size();
    for(int i = 0;i < m;i++) {
        int j = 0;
        while(j + coins.at(i) <= N) {
            if(dp[j + coins.at(i)] > dp[j] + 1) dp[j + coins.at(i)] =  dp[j] + 1;
            j++;
        }
    }

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

Measured memory_bytes: 640
#include <stdio.h>

int ssort(int a[] , int n);

int main(void)
{
	int n;
	scanf("%d",&n);
	int a[n];
	int i , count = 0;
	for( i = 0; i < n; i++)
	{
		scanf("%d",&a[i]);
	}
	count = ssort(a,n);
	
	for( i = 0; i < n; i++)
	{
		if( i != n-1)printf("%d ",a[i]);
		else printf("%d\n",a[i]);
	}
	printf("%d\n",count);
	return 0;
}

int ssort(int a[] , int n)
{
	int i , j , min , tmp , count = 0;
	for( i = 0; i < n; i++)
	{
		min = i;
		for( j = i; j < n; j++)
		{
			if(a[j] < a[min]) min = j;
		}
		tmp = a[i];
		a[i] = a[min];
		a[min] = tmp;
		if( i != min) count++;
	}
	return count;
}


Measured memory_bytes: 2080
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
  int n,x;
  cin>>n>>x;
  ll arr[n];
  for(int i=0;i<n;i++)
  {
      cin>>arr[i];
  }
  ll g=0;
  if(n==2)
  {
      if(arr[0]+arr[1]>x)
      cout<<arr[0]+arr[1]-x<<endl;
      else
        cout<<0<<endl;
  }
  else
  {
  for(int i=1;i<n-1;i++)
  {

      if(arr[i]+arr[i-1]>x)
      {
          ll k=arr[i]+arr[i-1]-x;
          if(arr[i]>=k)
          {
              arr[i]=arr[i]-k;
              g=g+k;
          }
          else
          {
              arr[i]=0;
              arr[i-1]=arr[i-1]-(k-arr[i]);
              g=g+k;
          }

      }
      if(arr[i]+arr[i+1]>x)
      {
          ll k=arr[i]+arr[i+1]-x;
          if(arr[i+1]>=k)
          {
              arr[i+1]=arr[i+1]-k;
              g=g+k;
          }
          else
          {
              arr[i+1]=0;
              arr[i]=arr[i]-(k-arr[i+1]);
              g=g+k;
          }
      }
  }
  cout<<g<<endl;
}
}


Measured memory_bytes: 4408
//
//  main.cpp
//  0406
//
//  Created by kao ching shan on 2019/4/6.
//  Copyright © 2019年 kao ching shan. All rights reserved.
//

#include <iostream>
#include <algorithm>

using namespace std;
# include<vector>
int arr[6];

int main(int argc, const char * argv[]) {
    
    int count=0,m=0;
    
    for(int i=0;i<6;i++)
        cin >> arr[i];
    for(int j=1;j<5;j++){
        for(int i=0;i<j;i++){
            if(arr[j]-arr[i]>arr[5]){
                cout << ":(" << endl;
                m=1;
                break;
            }
        }
        if(m==1)
            break;
    }
    if(m==0)
        cout << "Yay!" << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void d()
{
    int n, h;
    cin >> n >> h;
    vector<pair<long long int, long long int>> knifes(n);
    vector<int> bs(n);
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        bs[i] = b;
        knifes[i] = {a, b};
    }
    sort(knifes.begin(), knifes.end());
    sort(bs.begin(), bs.end());
    vector<unsigned long long int> dp(n);
    dp[0] = bs.back();
    for(int i = 1; i < n; i++)
    {
        if(bs[n - i - 1] < knifes.back().first)
        {
            dp[i] = dp[i - 1] + knifes.back().first;
        }else{
            dp[i] = dp[i - 1] + bs[n - i - 1];
        }
    }
    int l = 0;
    int r = 1000000001;
    int minimum = r;
    while (r - l > 1)
    {
        int m = (r + l) / 2;
        bool can = false;
        if(m > n)
        {
            unsigned long long int result = dp[n - 1] + knifes.back().first * (m - n);
            if(result >= h)
            {
                can = true;
            }
        }else {
            for (int i = 0; i < m; i++) {
                unsigned long long int result = dp[i] + knifes.back().first * (m - i - 1);
                if(result >= h)
                {
                    can = true;
                    break;
                }
            }
        }
        if(can)
        {
            r = m;
            minimum = m;
        }else{
            l = m;
        }
    }
    cout << r << endl;
}

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


Measured memory_bytes: 2944
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>

using namespace std;

#define sz(x) (int) x.size()
#define unique(x) x.erase(unique(x.begin(), x.end()), x.end())
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x)) 
#define y1 aksjdaskdjksjfksdjf
#define left kdnvldvoiwejifejg
#define right lkdsjflksdjfdjfk
#define prev asdasfsadjkjsdfjs
#define tm aklsjdasjdasjdkdjjf
#define lcm hddalkwjeidjjhasda
#define random_shuffle asdasdd

const int MAXN = 105;
const int mod = (int) 1e9 + 7;
const char go[] = {'A', 'G', 'C', 'T'};
vector <string> bad = {"AGAC", "AGGC", "AGTC", "ATGC", "AGGC", "ACGC", "AGC", "GAC", "ACG"};

int n;
map <string, int> dp[MAXN];

bool isBadState(string s) {
  for (string b : bad) {
    if (s.find(b) != string::npos) {
      return true;
    }
  }
  return false;
}

int main() { 

  cin >> n;
  dp[0][""] = 1;

  for (int i = 0; i < n; i++) {
    for (auto x : dp[i]) {
      // cerr << i << " " << x.first << " " << x.second << endl;
      string state = x.first;
      for (int j = 0; j < 4; j++) {
        string newState = state + go[j];
        if (sz(newState) > 4) {
          newState = newState.substr(1);
        }
        if (isBadState(newState)) {
          continue;
        }
        dp[i + 1][newState] = (dp[i + 1][newState] + dp[i][state]) % mod;
      }
    }
  }

  long long ans = 0;
  for (auto x : dp[n]) {
    // cerr << n << " " << x.first << " " << x.second << endl;
    ans = (ans + x.second) % mod;
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 2688
#include <bits/stdc++.h>
#include <math.h>
#define ALL(a)    a.begin(), a.end() 
#define rep(i, K, N)  for(int i = K; i < N; i++)
#define ll long long int
using namespace std;

int main(){
ll N, Ans = 0;cin >> N;

ll a = 1;

while(floorl(N/(10*powl(5, a - 1))) >= 1){

Ans += floorl(N/(10*powl(5, a - 1)));

a++;
}

if(N%2 == 0)cout << Ans;

else cout << 0;


}


Measured memory_bytes: 3676
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main() {
	string S, T;
	cin >> S >> T;
	sort(S.begin(), S.end());
	sort(T.begin(), T.end());
	reverse(T.begin(), T.end());
	if (S < T) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
}

Measured memory_bytes: 256
n = int(input())
lp = []
for i in range(3,int(n//2)+1):
  for k in lp:
    if i%2 == 0 or i%k == 0:
      break
  else:
    lp.append(i)

ans = 0
for i in (range(n)):
  li = []
  for k in lp:
    tmp = i+1
    cnt = 0
    while tmp%k == 0:
      tmp //= k
      cnt += 1
    if cnt > 0:
      li.append(cnt)
  if li in [[1,1,1], [1,3], [3,1], [7]]:
    ans += 1

print(ans)

Measured memory_bytes: 3064
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import Counter

n,k = li()
a = list(li())

cnt = Counter(a)

al = cnt.most_common()

if len(al) <= k:
    print(0)
else:
    ans = 0
    for _, v in al[k:]:
        ans += v
        
    print(ans)

Measured memory_bytes: 39536
import java.io.PrintWriter;
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		PrintWriter pr = new PrintWriter(System.out);
		while(true){
			int e = sc.nextInt();
			if(e==0) break;
			int z = 0;
			int y = 0;
			int x = e - y*y - z*z*z;
			int min = x + y + z;
			while(z*z*z<=e){
				while(y*y<=e-z*z*z){
					x = e - y*y - z*z*z;
					int tmp = x + y + z;
					if(tmp<min){
						min = tmp;
					}
					y++;
				}
				y = 0;
				z++;
			}
			pr.println(min);
		}
		pr.flush();
		sc.close();
	}
}

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

using namespace std;
static const int MAX = 10;

int factorial(int n) {
    int sum = 1;
    for (int i = 1; i <= n; ++i) {
        sum *= i;
    }
    return sum;
}

int main() {
    int N, X[MAX], Y[MAX], R[MAX];
    cin >> N;
    for (int i = 0; i < N; ++i) {
        cin >> X[i];
        cin >> Y[i];
        R[i] = i;
    }

    double D[MAX][MAX];
    for (int i = 0; i < N - 1; ++i) {
        for (int j = i + 1; j < N; ++j) {
            // i - j 間距離 (i > j)
            double d = sqrt(pow((X[j] - X[i]), 2) + pow((Y[j] - Y[i]), 2));
            D[i][j] = d;
        }
    }

    double sum = 0;
    int f = factorial(N);
    for (int i = 0; i < f; ++i) {
        int prev = R[0];
        for (int j = 1; j < N; ++j) {
            sum += D[min(prev, R[j])][max(prev, R[j])];
        }
        next_permutation(R, R + N);
    }

    cout.precision(10);
    cout << sum / f << endl;

    return 0;
}


Measured memory_bytes: 256
n = gets.to_i
m = gets.to_i
target = gets.split.map(&:to_i)
score = Array.new(n, 0)
m.times do |j|
  h = gets.split.map(&:to_i)
  n.times do |i|
    if h[i] == target[j] then score[i] += 1
    else score[target[j]-1] += 1
    end
  end
end
score.each{|v| p v}

Measured memory_bytes: 8800
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <cmath>
#include <cstring>

#define fi first
#define se second
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = 1; i < (n); ++i)
#define drep(i,n) for (int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(), (x).end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define pb push_back
#define sz(x) (int)(x).size()
#define PQ(T) priority_queue<T, v(T), greater<T> >

using namespace std;
typedef long long ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;

const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

const int mod = 1000000007;

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





























Measured memory_bytes: 256
#include <iostream>
#include <math.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <typeinfo>
#include <climits>
#include <ctype.h>
#include <string>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
struct omt{omt(){ios::sync_with_stdio(false);cin.tie(0);}}star;

int main(){
    string s;
    cin >> s;

    if(s[0] == '9' or s[1] == '9') cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
	int l[10], v[2], sl;
	char c;
	while(cin >> l[0]){
		sl = l[0];
		for(int i = 1; i < 10; i++){
			cin >> c >> l[i];
			sl += l[i];
		}
		cin >> c >> v[0] >> c >> v[1];
		for(int i = 0; i < 10; i++){
			l[i] *= v[0] + v[1];
		}
		sl *= v[0];

		for(int i = 0; i < 10; i++){
			sl -= l[i];
			if(sl <= 0){
				cout << i + 1 << endl;
				break;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 1160
import sys
input = sys.stdin.readline


def main():
    s = input().strip()
    l, r = 0, len(s)-1
    ans = 0
    while l < r:
        if s[l] == s[r]:
            l += 1
            r -= 1
        elif s[l] == "x" and s[r] != "x":
            ans += 1
            l += 1
        elif s[l] != "x" and s[r] == "x":
            ans += 1
            r -= 1
        else:
            print(-1)
            return
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3316
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=a;i<b;i+=1)
#define reps(i,a,b) for(int i=a;i>=b;i-=1)
#define vi vector<int>
#define pb push_back
#define in(x) int x; cin>>x;
#define mi unordered_map<int,int>
#define mem(x) memset(x, 0, sizeof(x))
using namespace std;

double helper(double arr[], int n){
	
	double dp[n+1][n+1];
	mem(dp);
	dp[0][0] = 1.0;
	rep(i, 1, n+1)
	{
		rep(j, 0, i+1)
		{
			if(j == 0) dp[i][j] = dp[i-1][j]*(1.00 - arr[i]);
			else dp[i][j] = dp[i-1][j]*(1.00 - arr[i]) + dp[i-1][j-1]*arr[i];
		}
	}
	
	double ans = 0.0;
	rep(i, (n+1)/2, n+1) ans+=dp[n][i];
	return ans;
}

int32_t main(void){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	
	in(n);
	double arr[n+1];
	rep(i, 1, n+1) cin>>arr[i];
	double ans = helper(arr, n);
	cout<<fixed<<setprecision(12)<<ans;
}

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

void chmax(int& a, int b) {
  if (a < b) a = b;
}

int main() {
  int K,T;
  cin >> K >> T;
  
  vector<int> cake(110, 0);
  
  for (int i = 0; i < T; i++) {
    cin >> cake[i];
  }
  
  int cakemax = 0;
  
  for (int i = 0; i < T; i++) {
    chmax(cakemax, cake[i]);
  }
  
  
  if (cakemax == K) {
    cout << K-1 << endl;
  } else if (cakemax * 2 > K) { 
    cout << cakemax * 2 - K - 1 << endl;
  } else {
    cout << 0 << endl;
  }
    
  
  
}

Measured memory_bytes: 3576
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')




    
    
def solve():
    
    # for _ in range(ii()):
    n,m,k=mi()
    a=li()
    b=li()
    a1=a[:]
    b1=b[:]
    for i in range(1,n):
        a[i]+=a[i-1]
    for i in range(1,m):
        b[i]+=b[i-1]
    # print(a,b)
    i=0
    j=0
    # print(a,b)
    x1,y1=0,0
    while(k>0):
        x=bisect_right(a,k+x1,i,n)-1
        y=bisect_right(b,k+y1,j,m)-1
        if((x-i)>=(y-j)):
            if(k<a1[i]):
                break
            k-=a1[i]
            x1+=a1[i]
            i+=1
        else:
            if(k<b1[j]):
                break
            k-=b1[j]
            y1+=b1[j]
            j+=1
    print(i+j)

    
    
    
    
    
    
    
    
    
    
        
        
        
        
if __name__ =="__main__":

    if path.exists('input.txt'):
        sys.stdin=open('input.txt', 'r')
        sys.stdout=open('output.txt','w')
    else:
    	input=sys.stdin.readline
    solve()


Measured memory_bytes: 119828
//بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n, m;
    cin >> n >> m;
    cout << abs((n * m) - (n * 2) - (m * 2) + 4);
}


Measured memory_bytes: 256
N = int(input())
L = list(map(int, input().split()))

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(j + 1, N):
            if (abs(L[i] - L[j]) < L[k]) and (L[k] < L[i] + L[j]) and (L[i] != L[j]) and (L[j] != L[k]) and (L[i] != L[k]):
                ans += 1

print(ans)

Measured memory_bytes: 9204
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>   
#include <stdio.h>    
 
using namespace std;
     
#define F first
#define S second
#define lb lower_bound               
#define ub upper_bound
#define pb push_back
#define pf push_front    
#define ppb pop_back
#define mp make_pair
#define bpp __builtin_popcountll                                                                                        
#define sqr(x) ((x) * (x)) 
#define al 0x3F3F3F3F
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define in insert
#define ppf pop_front
#define endl '\n'
//#define int long long
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
 
const int mod = (int)1e9 + 7;
const int N = (int)2e5 + 123;
const ll inf = (ll)3e18 + 1;

const double pi = acos (-1.0);
const double eps = 1e-7;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};

int n, k, d[10];
bool used[10];

inline bool check (int x) {
   bool ok = 0;
   while (x) ok |= used[x % 10], x /= 10;
   return ok;
}

inline void boost () {                    
   ios_base :: sync_with_stdio (NULL);
   cin.tie (NULL), cout.tie (NULL);          
}                                                     

inline void Solve () {
   cin >> n >> k;
   for (int i = 1; i <= k; i ++) cin >> d[i], used[d[i]] = 1; 
   while (check (n)) n ++;
   cout << n;
}                                     

main () {                                       
// freopen (".in", "r", stdin);    
// freopen (".out", "w", stdout);              
   boost ();
   int tt = 1;
// cin >> tt;   
   while (tt --) {
      Solve ();
   }                                               
   return 0;      
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
    
#define ll long long int
#define pb push_back
#define rep(i , j , n) for(ll i = j ; i < n ; i++)
#define pre(i , j , n) for(ll i = j ; i >= n ; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vll;
typedef vector<char>	vc;
typedef vector<bool>	vb;
typedef pair<ll,ll> 	pll;
#define br "\n"
#define ff first
#define ss second
ll MAX =  (ll)1000005;
ll mod =  1e9 + 7;

void solve(){
    string s,t;
    cin >> s >> t;
    ll ans = 1e9;
    rep(i,0,s.size()){
        if(i + t.size() > s.size()) break;
        ll sum = 0;
        rep(j,0,t.size()){
            if(s[i + j] != t[j]) sum++;
        }
        ans = min(ans,sum);

    }
    cout << ans;

}
	

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif  

	ll t = 1;
	// cin >> t;
	rep(i,0,t){
		solve();
	}
 
}	

Measured memory_bytes: 3600
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

#define all(v) v.begin(), v.end()

const ll mod=1000000007;

ll countBits(ll in){
  int res=0;
  for(;in>0;in>>=1){
    if((in&0x01)!=0){
      res++;
    }
  }
  return res;
}

template<typename T>
void show2dVector(vector<vector<T>>& v){
  for(int i=0;i<v.size();i++){
    int m=v[i].size();
    cout<<i<<" : ";
    for(int j=0;i<m;i++){
      cout<<v[i][j]<<" ";
    }
    cout<<endl;
  }
}

void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){
  seen[v]=true;
  cout<<v<<" ";
  for(auto next: g[v]){
    if(seen[next]){
      continue;
    }
    bfs(g,next,seen);
  }
}

bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){
  bool res=false;
  seen[start]=true;
  if(start==goal){
    return true;
  }
  for(auto next: g[start]){
    if(seen[next]){
      continue;
    }
    res=dfs(g,next,goal,seen);
    if(res){
      break;
    }
  }
  return res;
}

ll gcd(ll a,ll b) {
  return b? gcd(b,a%b): a;  
}
ll lcm(ll a,ll b) {
  return a*b/gcd(a,b);
}
bool isLowerCase(char c){
  return (c>='a'&&c<='z');
}

ll powm(ll a,ll n, ll m){
  ll ret=1;
  while(n>0){
    if(n%2==1){
      ret=(ret*a)%m;
    }
    n>>=1;
    a=(a*a)%m;
  }
  return ret;
}

void primeFactor(ll val, map<ll,ll> &primeList){
  if(val<1){
    return;
  }
  for(int i=2;i*i<val;i++){
    while(val%i==0){
      primeList[i]++;
      val/=i;
    }
  }
  if(val!=1){
    primeList[val]=1;
  }
  return;
}

const string yesno(bool ans){
  return (ans?"Yes":"No");
}
int main() {
  int n,k;cin>>n>>k;
  vector<int> a(n);
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  int l,r,x;
  l=0;r=INT_MAX;
  int ans=0;
  while(l+1<r){
    x=(l+r)/2;
    int cnt=0;
    for(int i=0;i<n;i++){
      int d=(a[i]+x-1)/x-1;
      if(d>0){
        cnt+=d;
      }
    }
    if(cnt<=k){
      r=x;
      ans=x;
    }else{
      l=x;
      ans=x+1;
    }
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 4052
def main():
    N = int(input())
    tuples = []
    for _ in range(N):
        x, y, h = [int(e) for e in input().split()]
        tuples.append((x, y, h))
        if h > 0:
            x_, y_, h_ = x, y, h

    # 適当な中心座標を用意
    for cs in range(101):
        for cy in range(101):
            # 一つの座標、その高さ、中心座標から、ピラミッドの高さを計算
            H = h_ + abs(cs - x_) + abs(cy - y_)
            flag = True
            # 別の検証値を使って正当性を確認（全てOKならそれが答えになる）
            for t in tuples:
                # 中心座標と検証値から座標の高さを計算
                h = H - abs(cs - t[0]) - abs(cy - t[1])
                h = max([h, 0])
                # その高さが検証値の高さと一致すればOK（違ったらダメ）
                if t[2] != h:
                    flag = False
            if flag:
                print(cs, cy, H)
                break


if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define P 1000000007
#define M 2000010
using namespace std;

typedef long long ll;

int n, d, h;

ll tmp = 0;

ll F[M], S[M];

int main() {
	scanf("%d%d%d", &n, &h, &d), h--;
	ll s = 1;
	for(int i = 1; i <= n; i++) s = s * i % P, tmp = (tmp + s) % P;
	F[0] = 1, F[1] = tmp, S[0] = 1, S[1] = tmp + 1;
	for(int i = 2; i <= d; i++) F[i] = F[i - 1] * (tmp + 1) % P, S[i] = (S[i - 1] + F[i]) % P;
	for(int i = d + 1; i <= h; i++) F[i] = tmp * (S[i - 1] - S[i - d - 1]) % P, S[i] = (S[i - 1] + F[i]) % P;
	ll ans = 0;
	for(int i = h + 1 - d; i <= h; i++) ans = (ans + F[i]) % P;
	printf("%lld\n", ((ans * s) % P + P) % P) % P;
	return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;

ll N;
vector<vector<ll>> G;
vector<ll> c;
ll idx;
ll d[10001];

ll bfs(ll n, ll p){
    ll ret = 0;
    d[n] = c[idx];
    idx++;
    if(p != -1) ret += d[n];
    for(auto &e : G[n]){
        if(e==p) continue;
        ret += bfs(e,n);
    }
    return ret;
}

int main(){
    cin >> N;
    G.resize(N);
    c.resize(N);
    for(int i=0;i<N-1;i++){
        ll a,b;
        cin >> a >> b;
        G[a-1].push_back(b-1);
        G[b-1].push_back(a-1);
    }
    for(int i=0;i<N;i++) cin >> c[i];
    sort(c.rbegin(),c.rend());
    idx = 0;
    cout << bfs(0,-1) << endl;
    for(int i=0;i<N;i++){
        cout << d[i];
        if(i < N-1) cout << " ";
    }
    cout << endl;
    return 0;
}

Measured memory_bytes: 4336
import java.util.Scanner;

//AtCoder Beginner Contest 140
//B	Buffet
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());
		int[] aList = new int[N];
		int[] bList = new int[N];
		int[] cList = new int[N - 1];
		for (int i = 0; i < N; i++) {
			aList[i] = Integer.parseInt(sc.next());
		}
		for (int i = 0; i < N; i++) {
			bList[i] = Integer.parseInt(sc.next());
		}
		for (int i = 0; i < N - 1; i++) {
			cList[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		int ans = 0;
		for (int i = 0; i < N; i++) {
			ans += bList[aList[i] - 1];
			if (i > 0) {
				if (aList[i] == aList[i - 1] + 1) {
					ans += cList[aList[i] - 1 - 1];
				}
			}
		}

		System.out.println(ans);
	}
}


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

int main() {
  double n, m, d; cin >> n >> m >> d;
  cout << fixed << setprecision(10);
  if (d == 0) cout << 1.0 * (m - 1) / n << endl;
  else cout << 2.0 * (n - d) * (m - 1) / (n * n) << endl;
}

Measured memory_bytes: 256
import sys
import random
import time
my_input = sys.stdin.readline

start = time.time()
D = int(input())
C = list(map(int, my_input().split()))
S = [list(map(int, my_input().split())) for _ in range(D)]


def main():
    cnt = 0
    best_ans, best_score = generate_first_ans()
    while time.time() - start < 1.81:
        cnt += 1
        ans = best_ans[:]
        si, sj = random.randint(0, 364), random.randint(0, 364)
        if si == sj:
            continue
        ans[si], ans[sj] = ans[sj], ans[si]
        score = calc_score(ans)
        if score > best_score:
            best_score = score
            best_ans = ans

    #print(best_score, cnt)
    for a in best_ans:
        print(a + 1)


def generate_first_ans():
    best_ans = []
    best_score = 0
    last_day = [-1] * 26
    for d in range(365):
        d_ans = -1
        d_score = -float('inf')
        d_last_day = [-1] * 26
        for t in range(26):
            d_tmp_score = 0
            d_tmp_score += S[d][t]
            d_tmp_last_day = last_day[:]
            d_tmp_last_day[t] = d
            for c, ld in zip(C, d_tmp_last_day):
                d_tmp_score -= c * (d - ld)

            if d_tmp_score > d_score:
                d_ans = t
                d_score = d_tmp_score
                d_last_day = d_tmp_last_day[:]

        best_ans.append(d_ans)
        best_score += d_score
        last_day = d_last_day[:]

    return best_ans, best_score


def calc_score(T):
    score = 0
    last_day = [-1] * 26
    for d, t in enumerate(T):
        score += S[d][t]
        last_day[t] = d
        for c, ld in zip(C, last_day):
            score -= c * (d - ld)
    return score


if __name__ == "__main__":
    main()


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

const int N = (int) 1e5 + 5;

int n;
int a[N];

int main() {
	// freopen("1.inp", "r", stdin); freopen("1.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]); 
	sort(a + 1, a + 1 + n);
	int mi = a[1];
	for (int i = 2; i < n; i++)
		if (abs((double(a[n]) / 2.0) - mi) > abs((double(a[n]) / 2.0) - a[i])) mi = a[i];
	cout << a[n] << " " << mi;
	return 0;
}

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

int main()
{
	int n; cin >> n;
	
	vector<int> a(n);
	map<int,int> mp;
	
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		mp[a[i]]++;
	}
	
	int ans = 0;
	for(auto p: mp){

		int x = p.first;
		int y = p.second;

		if (x <= y) {
			ans += y - x;
		}
		else { 
			ans += y;
		}
	}

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

Measured memory_bytes: 5120
#include <stdio.h>

int main(void)
{
    int a[3], i, count = 0;
    scanf("%d%d%d", &a[0], &a[1], &a[2]);
    for(i = 0; i < 3; i++)
    {
        switch(a[i])
        {
            case 5:count++;
            case 7:break;
            default:printf("NO\n");
                return 0;
        }
    }
    if(count != 2){
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define whlie while
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repr(i,N) for(auto i = (N) - 1; i >= 0; i -= 1)
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define vrep(v,it) for(auto it = v.begin(); it != v.end(); it++)
#define vrepr(v,it) for(auto it = v.rbegin(); it != v.rend(); it++)
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define rand mtrand
#ifdef NyaanDebug
  #include "NyaanDebug.h"
  #define trc(...) do { cout << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
  #define trca(v,...) do { cout << #v << " = "; array_out(v , ##__VA_ARGS__);cout << endl;} while(0)
#else
  #define trc(...)
  #define trca(...)
#endif
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector< vector<int> >;
constexpr int inf = 1001001001;
constexpr ll infLL = 0x1FFFFFFFFFFFFFFFLL;
struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15);} } iosetupnya;
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; }
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}

random_device seed_gen; mt19937_64 mtrand(seed_gen());
constexpr int MOD = 1000000007;
//////////////////////////////////////////

int main(){
  ini(N,H,W);
  int ans = 0;
  rep(i,N){
    ini(x,y);
    ans += x>=H&&y>=W;
  }
  out(ans);
}

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

int main() {
    string S;
    cin >> S;

 if(S=="SUN"){
  cout << "7" << endl;
 }
 if(S=="MON"){
  cout << "6" << endl;
 }
 if(S=="TUE"){
  cout << "5" << endl;
 }
 if(S=="WED"){
  cout << "4" << endl;
 }
 if(S=="THU"){
  cout << "3" << endl;
 }
 if(S=="FRI"){
  cout << "2" << endl;
 }
 if(S=="SAT"){
  cout << "1" << endl;
 }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <unordered_set>
#include <random>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N, K;
	cin >> N >> K;
	vector<int> v(N);
	for (int i = 0; i < N; i++) cin >> v[i];

	deque<int> dq;
	for (int i = 0; i < K; i++) dq.push_back(v[i]);

	for (int i = K; i < N; i++) {
		if (dq.front() < v[i]) cout << "Yes\n";
		else cout << "No\n";
		dq.pop_front();
		dq.push_back(v[i]);
	}

	return 0;
}

Measured memory_bytes: 4796
#include<bits/stdc++.h>
using namespace std;
int main(){
  int X;
  cin >> X;
  int a,r, b = 0;
  a = float(X / 500);
  r = X % 500;
  b = float(r / 5);
  cout << a * 1000 + 5 * b << endl;
  return 0;
}


Measured memory_bytes: 256
N, M = map(int,input().split())
ps = [list(input().split()) for i in range(M)]
wa = 0
ACdict = {}
for i in range(M):
    if ps[i][0] not in ACdict.keys():
        if ps[i][1] == "WA":
            ACdict[ps[i][0]] = 1
        else: ACdict[ps[i][0]] = "AC"
    else:
        if ACdict[ps[i][0]] == "AC":
            continue
        else:
            if ps[i][1] == "WA":
                ACdict[ps[i][0]] += 1
            else: 
                wa += ACdict[ps[i][0]]
                ACdict[ps[i][0]] = "AC"
acs = [k for k, v in ACdict.items() if v == "AC"]
print(len(acs), wa)

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

using namespace std;
typedef long long int ll;
const ll MAX = 2e5+3;
const ll INF = 1e9;

ll dp[MAX];


int main()
{
    ll n,k;
    cin>>n>>k;
    ll ht[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>ht[i];
        dp[i]=INF;
    }
    if(n<k)
    {
        cout<<abs(ht[n]-ht[1]);
        return 0;
    }
    dp[0]=dp[1]=0;
    
    
    for(int i=2;i<=n;i++)
    {
        for(int j=max(1LL,i-k);j<=i-1;j++)
            dp[i]=min(dp[i],dp[j]+abs(ht[i]-ht[j]));
        
    }
    cout<<dp[n]<<endl;
}

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

long long calc(string s){
	long long ans=0,len=(int)s.size();
	for(int i=0;i<len;++i){
		ans+=(s.at(i)-'0')*(pow(10,len-1-i));
	}
	return ans;
}

int main(){
	string s;cin >> s;
	long long ans=0,len=(int)s.size()-1;
	for(int i=0;i<1<<len;++i){
		long long sum=0,t=0;
		for(int j=0;j<len;++j){
			if(i>>j & 1){
				sum+=calc(s.substr(t,j+1-t));
				t=j+1;
			}
		}
		sum+=calc(s.substr(t));
		ans+=sum;
	}
	cout << ans << endl;
}


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

using namespace std;

//minv = R[0]
//for j ??? 1 ?????? n-1 ?????§
//  maxv = (maxv ??¨ R[j]-minv ???????????§?????????)
//  minv = (minv ??¨ R[j] ??????????°??????????)

int main() {

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

	int maxv = -1000000000;
	int minv;
	scanf("%d", &minv);

	for (int i = 1; i < n; ++i) {
		int Rt;
		scanf("%d", &Rt);

		maxv = max(maxv, Rt - minv);
		minv = min(minv, Rt);
	}
	cout << maxv << endl;

	return 0;
}

Measured memory_bytes: 3184
// C - City Savers
// https://atcoder.jp/contests/abc135/tasks/abc135_c
#include <bits/stdc++.h>
#define lli long long int
#define uli unsigned long long int
#define rep(i, m, n) for (lli i = m; i < (n); i++)
#define repe(i, m, n) for (lli i = m; i <= (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((lli)(x).size())
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 //10^12
#define MOD 10000007      //10^9+7
#define SORT(n) sort(n.begin(), n.end())
#define SORTR(n) sort(n.begin(), n.end(), greater<int>())
#define REV(n) reverse(n.begin(), n.end())
#define Vec(K, L, N, S) vector<L> K(N, S)
#define rt sqrt
#define pb push_back
#define eb emplace_back
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vlli = vector<lli>;
using P = pair<lli, lli>;
// vector<P> vp;
// vp.emplace_back(a[i], b[i]);
using vs = vector<string>;
using ll = long long;
// vector入力
template <typename T>
istream &operator>>(istream &is, vector<T> &vec)
{
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec)
{
  // os << '{';
  for (int i = 0; i < vec.size(); i++)
  {
    os << vec[i] << (i + 1 == vec.size() ? "" : "");
  }
  // os << '}';
  return os;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli c, h, k, n, w, ans = 0, count = 0;
  string s, t;
  cin >> n;
  vlli a(n + 1), b(n);
  rep(i, 0, n + 1)
  {
    cin >> a[i];
  }
  rep(i, 0, n)
  {
    cin >> b[i];
  }
  rep(i, 0, n)
  {
    if (a[i] - b[i] <= 0)
    {
      ans += a[i];
      // cout << i << " " << a[i] << " " << b[i] << endl;

      b[i] = abs(a[i] - b[i]);
      a[i] = 0;
      if (a[i + 1] - b[i] <= 0)
      {
        // cout << a[i + 1] << "   " << b[i] << endl;
        ans += a[i + 1];
        b[i] = abs(a[i + 1] - b[i]);
        a[i + 1] = 0;
      }
      else
      {
        // cout << i << " " << a[i + 1] << " " << b[i] << endl;
        ans += b[i];
        a[i + 1] -= b[i];
        b[i] = 0;
      }
    }
    else
    {
      ans += b[i];
      a[i] -= b[i];
      b[i] = 0;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 1792
import sys

def make_burger(n):
    patty = [None] * (n + 1)
    bun = [None] * (n + 1)
    patty[0] = 1
    bun[0] = 0
    burger = [None] * (n + 1)
    burger[0] = 1
    for i in range(n):
        patty[i+1] = patty[i] * 2 + 1
        bun[i+1] = bun[i] * 2 + 2
        burger[i+1] = patty[i+1] + bun[i+1]
    
    return patty, bun, burger

n, x = map(int, sys.stdin.readline().split())

def main():
    r = x
    patty, bun, burger = make_burger(n)

    res = 0
    for l in range(n, 0, -1):
        half_burger = burger[l] // 2 + 1
        half_patty = patty[l] // 2 + 1
        if r == 1:
            r = 0
            break
        elif r < half_burger:
            r -= 1
        elif r == half_burger:
            res += half_patty
            r = 0
            break
        elif r < burger[l]:
            res += half_patty
            r -= half_burger
        elif r == burger[l]:
            res += patty[l]
            r = 0
            break
    if r:
        res += 1
    return res
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Measured memory_bytes: 3064
#include<iostream>
#include<cstdio>
#include<string>
#include<set>
using namespace std;
int main(){
	int n;
	string query, word;
	set<string> dict;
	cin>>n;
	for(;n--;){
		cin>>query>>word;
		if(query=="insert")dict.insert(word);
		else{
			if(dict.find(word)==dict.end())puts("no");
			else puts("yes");
		}
	}
}

Measured memory_bytes: 30612
#include <iostream>
#include <vector>
#include <limits>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define in(v) cin>>v;
#define out(v) cout<<v<<"\n"
const int INF = numeric_limits<int>::max();
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template <typename T> T gcd(T a, T b){return b?gcd(b,a%b):a;}

struct SegTree {
  private:
    int n;
    vector<int> node;
  public:
    SegTree(int size) {
      n = 1;
      while (n < size) n *= 2;
      node.resize(2*n-1, INF);
    }
    void update(int index, int value) {
      index = n - 1 + index;
      node[index] = value;
      while (index != 0) {
        index = (index-1) / 2;
        if (node[2*index+1] == INF)
          node[index] = node[2*index+2];
        else if (node[2*index+2] == INF)
          node[index] = node[2*index+1];
        else
          node[index] = gcd(node[2*index+1], node[2*index+2]);
      }
    }
    int get_gcd(int left, int right, int k = 0, int l = 0, int r = -1) {
      if (r < 0)
        r = n;
      if (r <= left || right <= l)
        return INF;
      if (left <= l && r <= right)
        return node[k];
      int vl = get_gcd(left, right, 2*k+1, l, (l+r)/2);
      int vr = get_gcd(left, right, 2*k+2, (l+r)/2, r);
      if (vl == INF)
        return vr;
      if (vr == INF)
        return vl;
      return gcd(vl, vr);
    }
};

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

  SegTree T(N);
  rep(i, N) {
    int a; in(a);
    T.update(i, a);
  }
  int ans = 1;
  rep(i, N) {
    int l = T.get_gcd(0, i),
        r = T.get_gcd(i+1, N);
    if (l == INF)
      chmax(ans, r);
    else if (r == INF)
      chmax(ans, l);
    else
      chmax(ans, gcd(l, r));
  }
  out(ans);

  return 0;
}


Measured memory_bytes: 1280
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define f(i,x,n) for(int i=x;i<(int)n;++i)
#define vpii vector<pair<int,int>>
#define vi vector<int>
#define mpii map<pair<int,int>,int>
#define mpivpii map<int,vector<pair<int,int>>>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define mpi map<int,int>
#define vvi vector<vector<int>>
#define vvvi vector<vvi>
ld pie=3.141592653589;
int mod=998244353;

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

vi pr;



void solve(){
	int n,x,m; cin>>n>>x>>m;
	int pre=x; int s=x; int vis[m];
	f(i,0,m) vis[i]=-1; vis[x]=0;
	vi ret; ret.pb(x);
	f(i,1,n){
		int nx=pre*pre; nx%=m;
		if(vis[nx]!=-1){
			int before=0,after=0,len=0,len1=0;
			bool pass=0; int id=-1;
			f(j,0,sz(ret)){
				if(ret[j]==nx){
					after+=ret[j];
					++len; id=j;
					pass=1;
					continue;
				}
				if(!pass) {
					before+=ret[j];
					++len1;
				}
				else{
					after+=ret[j];
					++len;
				}
			}
			int ans=before;
			int rem=n-len1; ans+=(rem/len)*after; rem%=len;
			f(j,0,rem){
				ans+=ret[id];	++id;
			}
			cout<<ans<<"\n";
			return ;
		}
		vis[nx]=i; ret.pb(nx);
		s+=nx; pre=nx;
	}
	cout<<s<<"\n";
}


int32_t main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);
  	int t=1;   //cin>>t;
  	while(t--) solve();
 	return 0;
}

Measured memory_bytes: 4600
#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

int main()
{
  string s1,s2,s3;
  int i,j;
  int a=0,b=0,c=0;

  cin>>s1;
  cin>>s2;
  cin>>s3;

  int now=0;//who

  while(1){
    if(now==0){
      if(a==s1.size()){
	cout<<"A"<<endl;
	return 0;
      }
      now=s1[a]-'a';//next
      a++;
    }else if(now==1){
      if(b==s2.size()){
	cout<<"B"<<endl;
	return 0;
      }
      now=s2[b]-'a';
      b++;
    }else{
      if(c==s3.size()){
	cout<<"C"<<endl;
	return 0;
      }
      now=s3[c]-'a';
      c++;
    }
  }

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define INF 2e9
#define IMAX 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long llong;

int main()
{
	int N;
	cin >> N;
	int A[N+1][N];

	int mIdx[N+1];

	int val;
	FOR(pIdx, 1, N) {
		REP(idx, N-1) {
			cin >> val;
			A[pIdx][idx] = val;
		}
		mIdx[pIdx] = 0;
	}

	int day = 0;
	int matches = 0;
	int maxMatch = N * (N-1) / 2;
	bool exist = false;
	int vs;
	bool checked[N+1];

	while(true) {
		// 初期化
		++day;
		exist = false;
		FOR(pIdx, 1, N) {
			checked[pIdx] = false;
		}

		FOR(pIdx, 1, N) {
			// 重複を回避するため、取得した数字が自分より小さい場合のみチェックする
			if (mIdx[pIdx] < N - 1) {
				vs = A[pIdx][mIdx[pIdx]];
				if (!checked[vs] && vs < pIdx) {
					if (mIdx[vs] < N - 1) {
						if (A[vs][mIdx[vs]] == pIdx) {
							// 一致
							exist = true;
							checked[vs] = true;
							checked[pIdx] = true;
							++matches;
							mIdx[pIdx] += 1;
							mIdx[vs] += 1;
						}
					}
				}
			}
		}

		// 1試合も行えない日があれば終了
		if (!exist) {
			day =-1;
			break;
		}
		// 試合数が最大に到達したら終了
		if (matches >= maxMatch) {
			break;
		}
	}

	cout << day << endl;

	return 0;
}


Measured memory_bytes: 4096
#include <stdio.h>

int main(void)
{
    int H, W, i, k, b;
    char a, n[6];
    scanf("%d %d ", &H, &W);

    for (i = 0; i < H*W; i++) {
        for (k = 0; k < 6; k++) {
            n[k] = getchar();
        }
        if (n[0] == 's' && n[1] == 'n' && n[2] == 'u'
        && n[3] == 'k' && n[4] == 'e') {
            a = 'A'+(i%W);
            b = (i/W)+1;
        }
    }
    printf("%C%d", a, b);
    return 0;
}


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

int main(){
  ll a,b,k;
  cin>>a>>b>>k;
  if(a>=k){
    cout<<a-k<<' '<<b<<endl;
  }
  else{
    cout<<0<<' '<<max((long long)0,b-(k-a))<<endl;
  }
  return(0);
}


Measured memory_bytes: 256