akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define F first
#define S second
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	string s;
	cin>>s;
	int c=0;
	for (int i=0;i<(int)s.size();i++){
		if (s[i]=='C') c=1;
		if (c==1&&s[i]=='F'){
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	cout<<"No"<<endl;
}

Measured memory_bytes: 256
from Queue import Queue
 
n, q = [ int( val ) for val in raw_input( ).split( " " ) ]
processes = Queue( )
for i in range( n ):
    name, time = raw_input( ).split( " " )
    processes.put( ( name, int( time ) ) )
  
qsum = 0
output = []
while not processes.empty( ):
    process = processes.get( )
    if process[1] <= q:
        qsum += process[1]
        output.append( "{:s} {:d}".format( process[0], qsum ) )
    else:
        processes.put( ( process[0], process[1]- q ) )
        qsum += q
 
print( "\n".join( output ) )

Measured memory_bytes: 16132
#include <stdio.h>

int main(void){
	int a,b,c,d;
	scanf("%d %d %d %d",&a,&b,&c,&d);
	int i,j,k,l;
	scanf("%d %d %d %d",&i,&j,&k,&l);
	int x,y;
	x=a+b+c+d;
	y=i+j+k+l;
	if(x>=y){
		printf("%d\n",x);
	}else{
		printf("%d\n",y);
	}
	return 0;
}

Measured memory_bytes: 592
#include <bits/stdc++.h>
//#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
#define all(v) v.begin(),v.end()
int n,k,a[5010];
bool dp1[5010][5010],dp2[5010][5010];
signed main(){
	cin>>n>>k;
	rep(i,n)cin>>a[i];
	dp1[0][0]=true;
	rep(i,n-1){
		rep(j,k){
			if(dp1[i][j])dp1[i+1][j]=true;
			if(dp1[i][j]&&j+a[i]<=k)dp1[i+1][j+a[i]]=true;
		}
	}
	dp2[n-1][0]=true;
	for(int i=n-1;i>0;i--){
		rep(j,k){
			if(dp2[i][j])dp2[i-1][j]=true;
			if(dp2[i][j]&&j+a[i]<=k)dp2[i-1][j+a[i]]=true;
		}
	}
	int ans=0;
	rep(i,n){
		vector<int>a1,a2;
		rep(j,k+1){
			if(dp1[i][j])a1.push_back(j);
			if(dp2[i][j])a2.push_back(j);
		}
		bool flag=true;
		for(int x:a1){
			if(upper_bound(all(a2),k-x-1)-lower_bound(all(a2),k-x-a[i]))flag=false;
		}
		if(flag)ans++;
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 49280
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);++(i))
#define all(x) (x).begin(),(x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;
const ll INF = 1001001001001001;


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

  vector<ll> c(n, 0);
  vector<vector<ll>> a(n, vector<ll>(m, 0));

  rep(i, n) {
    cin >> c[i];
    rep(j, m) {
      cin >> a[i][j];
    }
  }

  ll ans = INF;
  for (int bit = 0; bit < (1 << n); ++bit) {
    vector<int> buy;
    rep(j, n) {
      if (bit & (1 << j)) {
        buy.push_back(j);
      }
    }

    ll price = 0;
    vector<ll> score(m, 0);

    for (int k = 0; k < buy.size(); ++k) {
        price += c[buy[k]];

        rep(l, m) {
          score[l] += a[buy[k]][l];
        }
    }

    bool ok = true;
    rep(p, m) {
      if (score[p] < x) {
        ok = false;
        break;
      }
    }

    if (ok) {
      ans = min(ans, price);
    }
  }
  if (ans == INF) ans = -1;
  cout << ans << endl;
}


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

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (ll i = m; i < (n); i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  int a,b,x;
  cin >>a>>b>>x;
  if(a<=x&&x<=a+b){
    cout << "YES" << endl;
  }
  else{
    cout << "NO" << endl;
  }
  
}   

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

int32_t main()
{

    int x,y,a,b,c;
    cin>>x>>y>>a>>b>>c;
    vector<int> red(a);for(auto &i:red)cin>>i;
    vector<int> gre(b);for(auto &i:gre)cin>>i;
    vector<int> col(c);for(auto &i:col)cin>>i;
    sort(red.rbegin(),red.rend());
    sort(gre.rbegin(),gre.rend());
    for(int i=0;i<x;i++)col.push_back(red[i]);
    for(int i=0;i<y;i++)col.push_back(gre[i]);
    sort(col.rbegin(),col.rend());

    int ans=accumulate(col.begin(),col.begin()+x+y,0LL);
    cout<<ans;



}


Measured memory_bytes: 7768
import collections
n=int(input())
s=[str(input()) for i in range(n)]
c=collections.Counter(s)
lst=[]
maxi=max(c.values())
for i in c.keys():
  if c[i]==maxi:
    lst.append(i)
lst.sort()
for k in range(len(lst)):
  print(lst[k])

Measured memory_bytes: 35564
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}


signed main(){
  string str;
  cin>>str;
  int ans = 0;
  int a = 0,b = str.size()-1;
  for(int i=0;i<(int)str.size();i++){
    if(a >= b) continue;
    //cout<<str[a]<<" "<<str[b]<<endl;
    if(str[a] == 'x' && str[b] !='x')  a++,ans++;
    else if(str[b] == 'x' && str[a] != 'x') b--,ans++;
    else if(str[a] != str[b]) {
      a++,b--;
      ans = -1;break;
    }
    else a++,b--;
  }
  
  cout<<ans<<endl;

  return 0;
}


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

using namespace std;

int suretu[100000];
int cont = 0;
int max_sum = 0;

int ret_maxsum(int n, int k, int wa, bool isBegin)
{
	/*
	int temp = 0;
	int max = 0, cont = 0;
	int wa;							//ツ陳シツ前ツづ個禿アツづつづ個計ツ算ツづ系ツ古つづ個計ツ算ツづ個和
	
	for(int i = 0; i < n-k+1; i++){
		for(int j = 0; j < k; j++){
			temp += suretu[cont+j];
			if(
			printf("suretu%d = %d\n",j,suretu[cont+j]);
		}
		cout << "**" << endl;
		if(temp > max){
			printf("*maxツ更ツ新\n");
			max = temp;
		}
		temp = 0;
		cont++;
	}
	
	return max;
	*/
	
	int temp = 0;
	int i;
	
	if(isBegin == 1){
		for(i = 0; i < k; i++){
			temp += suretu[cont+i];
		}
	}
	else{
		temp += wa + suretu[cont+k-1];
	}
	wa = temp - suretu[cont];
//	printf("temp = %d\nwa = %d\n\n",temp,wa);
	cont++;
	
	if(max_sum < temp){
//		cout << "maxツ更ツ新:" << temp << endl;
		max_sum = temp;
	}
	
	if(cont == n-k+1){
		return max_sum;
	}
	else{
		return ret_maxsum(n, k, wa, 0);
	}
}
	

int main()
{
	for(;;){
		int n,k;
		
		cin >> n;
		cin >> k;
		if(n == 0 && k == 0){
			break;
		}
		for(int i = 0; i < n; i++){
			cin >> suretu[i];
		}
		
		printf("%d\n",ret_maxsum(n, k, 0, 1));
	}
}

Measured memory_bytes: 1244
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <istream>

using namespace std;

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

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

	int ans = 0;
	while(true){
		int t=0;
		while(h[t]==0){
			t++;
			if(t==n){
				cout << ans << endl;

				return 0;
			}
		}
		for(;t<n;t++){
			if(h[t]>0) h[t]--;
			else break;
		}
		ans++;
	}
}

Measured memory_bytes: 256
#include<cctype>
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    char ch;
    int ar[26] = {};
    while(cin >> ch){
        ch = tolower(ch);
        if(isalpha(ch)){
            ar[ch - 'a']++;
        }
    }

    for(int i = 0; i < 26; i++){
        printf("%c : %d\n", i + 'a',ar[i]);
    }
    return 0;
}


Measured memory_bytes: 3196
#include<iostream>
#include<algorithm>

using namespace std;

typedef long long ll;

struct seg
{
	ll l, r, s;
	inline int operator<(const seg &a) const {return s > a.s;}
} s[5010];

const ll inf = (1ll<<62) - 1;
int n, m;
ll f[2510][2];

int main()
{
	ios::sync_with_stdio(0);
	
	cin >> n; m = n >> 1;
	for (int i = 1; i <= n; ++i)
	{
		cin >> s[i].l >> s[i].r;
		s[i].s = s[i].l + s[i].r;
	}
	sort(s+1, s+n+1);
	for (int i = 1; i <= m; ++i)
		f[i][0] = inf;
	
	int x = 0;
	for (int i = 1; i <= n; ++i)
	{
		x ^= 1;
		for (int j = 0; j <= m; ++j)
			f[j][x] = inf;
		for (int j = 0; j<i && j<=m; ++j)
		{
			const int k = i-j-1;
			if (j < m)
				f[j+1][x] = min(f[j+1][x], f[j][x^1] + s[i].r + s[i].s * j);
			if (k < m)
				f[j][x] = min(f[j][x], f[j][x^1] + s[i].l + s[i].s * k);
			if (n & 1)
				f[j][x] = min(f[j][x], f[j][x^1] + (n-1)/2 * s[i].s);
		}
	}
	
	cout << f[m][x] << endl;
	
	return 0;
}

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

int main() {
  int N; // 袋に入っているあられの数
  int P_n = 0, W_n = 0, G_n = 0, Y_n = 0; 
  // 各色の数をカウントし格納するための変数
  // 全て数え終わった時に1以上の変数が3個あればThree,４個あればFourを出力する
  string S; // 色を読み込むための文字型変数
  cin >> N;
  
  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S == "P") {
      P_n++;// 読み込んだSがPだった場合、P_nにプラス１カウント
    }
    
    else if (S == "W") {
      W_n++;// 読み込んだSがWだった場合、W_nにプラス１カウント
    }
    
    else if (S == "G") {
      G_n++;// 読み込んだSがGだった場合、G_nにプラス１カウント
    }
    
    else if (S == "Y") {
      Y_n++;// 読み込んだSがYだった場合、Y_nにプラス１カウント
    }
  }
  
  if (P_n*W_n*G_n*Y_n != 0) {
    cout << "Four" << endl; // 四つの掛け合わせが0出ない場合（=全て１以上）Fourを出力
  }
  
  else if (W_n*G_n*Y_n != 0) {
    cout << "Three" << endl; // Pが０で他が１以上の場合
  }
  
  else if (P_n*G_n*Y_n != 0) {
    cout << "Three" << endl; // Wが０で他が１以上の場合
  }
  
  else if (P_n*W_n*Y_n != 0) {
    cout << "Three" << endl; // Gが０で他が１以上の場合
  }
  
  else if (P_n*W_n*G_n != 0) {
    cout << "Three" << endl; // Yが０で他が１以上の場合
  }
  
}

Measured memory_bytes: 256
//毎回余りを求めることでオーバーフローしなくなる
//nextLineよりnextだ
//charは''で囲うんだぞ
//読み込みは先に全部やっておけ
//for文を回す回数に気をつけろ
	//for文は10^3の三重はTLE,10^5の二重はTLE,10^9のforはTLE
//貪欲は使えるか？？試した？？

import java.util.Scanner;

public class Main {

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

		    int a = sc.nextInt();
		    int b = sc.nextInt();
		    int k = sc.nextInt();
		    
		    for(int i = 0; i < k; i++) {
		    	if(i % 2 == 0) {
		    		if(a % 2 == 0) {
		    			a /= 2;
		    			b += a;
		    		}else {
		    			a--;
		    			a/=2;
		    			b+= a;
		    		}
		    	}else {
		    		if(b % 2 == 0) {
		    			b/=2;
		    			a += b;
		    		}else {
		    			b--;
		    			b /=2;
		    			a += b;
		    		}
		    	}
		    }
		    System.out.println(a + " " + b);
		    
		    
		    sc.close(); }

	public static void saiki(int a, String b) {
		//再帰コーディングのお手本

		if(a == 0) {
			System.out.println(b);
		}else {
			for(char ch = 'a'; ch <= 'c'; ch++) {
				saiki(a-1,b + ch);
			}
		}
	}

	public static  int gcd(int a, int b) {
	    if (a < b) {
	        int tmp = a;
	        a = b;
	        b = tmp;
	    }

	    int r = -1;
	    while (r != 0) {
	        r = a % b;
	        a = b;
	        b = r;
	    }

	    return a;
	}
}


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

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

using namespace std;

int main() {
    int n;
    cin >> n;
    double x, s = 0;
    string u;
    rep(i, n){
        cin >> x;
        cin >> u;
        if(u == "JPY"){
            s += x;
        }else{
            s += (x * 380000);
        }
    }
    cout << s << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<list>
#include<vector>
#include<cmath>

using namespace std;



int main(){

    double h, w, x, y;
    cin >> w >> h >> x >> y;

    cout << fixed;
    cout << w*h/2 << " ";

    if(x==w/2 && y==h/2)cout << 1 <<endl;
    else cout << 0 << endl;

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
    int N; 
    cin >> N;
    vector<vector<int>>A(N,vector<int>(N));
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            cin >> A[i][j];
        }
    }
    int ans = 0;
    for(int i = 0; i < N; i++) {
        for(int j = i+1; j < N; j++) {
            bool ok = false;
            for(int k = 0; k < N; k++) {
                if(i == k || j == k) continue;
                if(A[i][k]+A[k][j] < A[i][j]) {
                    cout << -1 << endl;
                    return 0;
                }
                if(A[i][k]+A[k][j] == A[i][j]) {
                    ok = true;
                }
            }
            if(!ok) {
                ans += A[i][j];
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 4340
N = 30

steps = []
steps[0..2] = 1, 2, 4
for i in 3..N do
  steps[i] = steps[i - 1] + steps[i - 2] + steps[i - 3]
end

while((n = gets.to_i - 1) && n != -1) do
  puts (steps[n] % 3650 == 0 ? steps[n] / 3650 : steps[n] / 3650 + 1)
end

Measured memory_bytes: 6092
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

pol1 = 0
pol2 = 0

ft = [[0] * 2 for _ in range(100000)] 

for i in ab:
    ft[i[0] - 1][0] += 1
    ft[i[1] - 1][1] += 1

ftsum = [[0] * 2 for _ in range(100000)]
ftsum[0][0] = ft[0][0]
for i in range(1, len(ft)):
    ftsum[i][0] = ftsum[i-1][0] + ft[i][0]
    ftsum[i][1] = ftsum[i-1][1] + ft[i][1]

for i in range(len(ft)-1):
    pol2 = max(pol2, ftsum[i][0] - ftsum[i][1])

for f, t in ab:
    f = f-1
    t = t-1
    temp = ftsum[t-1][0]
    if f != 0:
        temp -= ftsum[f][1]
    pol1 = max(pol1, temp)

print(pol1, pol2)
    



Measured memory_bytes: 64176
#include<bits/stdc++.h>
using namespace std;
using Int = long long;

struct Dinic{
  static const Int INF=1LL<<55LL;
  
  struct edge {
    Int to,cap,rev;
    edge(){}
    edge(Int to,Int cap,Int rev):to(to),cap(cap),rev(rev){}
  };

  Int n;
  vector<vector<edge> > G;
  vector<map<Int,int> > M;
  vector<Int> level,iter;

  Dinic(){}
  Dinic(Int sz):n(sz),G(n),M(n),level(n),iter(n){}
  
  void add_edge(Int from,Int to,Int cap){
    M[from][to]=G[from].size();
    M[to][from]=G[to].size();
    G[from].push_back(edge(to,cap,G[to].size()));
    // undirected
    //G[to].push_back(edge(from,cap,G[from].size()-1));
    // directed
    G[to].push_back(edge(from,0,G[from].size()-1));
  }
  
  void bfs(Int s){
    fill(level.begin(),level.end(),-1);
    queue<Int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
      Int v=que.front();que.pop();
      for(Int i=0;i<(Int)G[v].size();i++){
	edge &e = G[v][i];
	if(e.cap>0&&level[e.to]<0){
	  level[e.to]=level[v]+1;
	  que.push(e.to);
	}
      }
    }
  }
  
  Int dfs(Int v,Int t,Int f){
    if(v==t) return f;
    for(Int &i=iter[v];i<(Int)G[v].size();i++){
      edge &e=G[v][i];
      if(e.cap>0&&level[v]<level[e.to]){
	Int d = dfs(e.to,t,min(f,e.cap));
	if(d>0){
	  e.cap-=d;
	  G[e.to][e.rev].cap+=d;
	  return d;
	}
      }
    }
    return 0;
  }
  
  Int flow(Int s,Int t,Int lim){
    Int fl=0;
    for(;;){
      bfs(s);
      if(level[t]<0||lim==0) return fl;
      fill(iter.begin(),iter.end(),0);
      Int f;
      while((f=dfs(s,t,lim))>0){
	fl+=f;
	lim-=f;
      }
    }
  }

  Int flow(Int s,Int t){
    return flow(s,t,INF);
  }

  //cap==1 only
  bool back_edge(Int s,Int t,Int from, Int to){
    for(Int i=0;i<(Int)G[from].size();i++) {
      edge& e=G[from][i];
      if(e.to==to) {
	if(e.cap==0&&flow(from,to,1)==0) {
	  flow(from,s,1);
	  flow(t,to,1);
	  return 1;
	}
      }
    }
    return 0;
  }
};

signed main(){
  Int n;
  cin>>n;
  vector<Int> a(n+1);
  for(Int i=1;i<=n;i++) cin>>a[i];
  Dinic G(n+2);
  Int s=0,t=n+1;
  Int sum=0;
  for(Int i=1;i<=n;i++){
    if(a[i]>0) sum+=a[i];
    if(a[i]<0) G.add_edge(s,i,-a[i]);
    if(a[i]>0) G.add_edge(i,t,a[i]);
    for(Int j=i+i;j<=n;j+=i)
      G.add_edge(i,j,Dinic::INF);
  }
  cout<<sum-G.flow(s,t)<<endl;
  return 0;
}


Measured memory_bytes: 384
// Why I am so dumb? :c
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair

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

#define fi first
#define se second

using namespace std;

typedef long long ll;

int n, a, b;

void solve() {                   
	scanf("%d %d %d", &n, &a, &b);
	printf((b - a) % 2 ? "Borys\n" : "Alice\n");                                                   
}

int main() {    
    int tt = 1;

    while (tt--) {
        solve();
    }

    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
int n, a[55];
int main() {
    cin >> n;
    double ans = 0;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    ans = a[1];
    for (int i = 2; i <= n; i++) {
        ans = (ans + a[i]) / 2;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int n,q,t;
string ss;
struct node{
    node *l,*r;
    int s,m,e;
    set<char> lols;
    node(int s_,int e_){
        s=s_;
        e=e_;
        m=(s+e)/2;
        if (s!=e){
            l=new node(s,m);
            r=new node(m+1,e);
            for (auto i:l->lols){
                lols.insert(i);
            }
            for (auto i:r->lols){
                lols.insert(i);
            }
        }
        else {
            lols.insert(ss[s-1]);
        }
    }
    void upd(int x,char k){
        if (s==e){
            lols.clear();
            lols.insert(k);
            return;
        }
        if (x>m) r->upd(x,k);
        else l->upd(x,k);
        lols.clear();
        for (auto i:l->lols){
            lols.insert(i);
        }
        for (auto i:r->lols){
            lols.insert(i);
        }
    }
    set<char> qry(int x,int y){
        if (s==x && e==y){
            return lols;
        }
        if (x>m) return r->qry(x,y);
        if (y<=m) return l->qry(x,y);
        set<char> cur,temp;
        temp=l->qry(x,m);
        for (auto i:temp) cur.insert(i);
        temp=r->qry(m+1,y);
        for (auto i:temp) cur.insert(i);
        return cur;
    }
}*root;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>ss>>q;
    root=new node(1,n);
    while (q--){
        cin>>t;
        if (t==1){
            int iq;
            char cq;
            cin>>iq>>cq;
            root->upd(iq,cq);
        }
        else {
            int l,r;
            cin>>l>>r;
            set<char> ans=root->qry(l,r);
            cout<<ans.size()<<'\n';
        }
    }
}

Measured memory_bytes: 233628
#include<bits/stdc++.h>
#define int long long
using namespace std;
int Size = 1e14;

int check(int a,int b,int c){

      if(a+b>c) return 1;
      else return 0;
}

int32_t main()
{

    int n,a;
    cin>>n;

    vector<int>vec;


    for(int i=0; i<n; i++){
        cin>>a;
        //if(fre[a]==0)
        vec.push_back(a);


    }

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

    int cnt = 0, mx=0;
    int range = vec.size();

    for(int i=0; i<range-2; i++){

      for(int j=i+1; j<range-1; j++){

            //if(vec[i]==vec[j]) continue;

        for(int k=j+1; k<range; k++){

            //if(vec[j]==vec[k]) continue;

            int a = vec[i];
            int b = vec[j];
            int c = vec[k];

            //cout<<a<<" "<<b<<" "<<c<<endl;

            if(check(a,b,c)==1 and a!=b and b!=c and a!=c){
                cnt++;
            }
        }
      }
    }

    cout<<cnt<<endl;


	return 0;
}


Measured memory_bytes: 3624
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

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

        int N = sc.nextInt();
        int[] u = new int[N];
        int[] v = new int[N];
        int[] w = new int[N];


        for (int i=0; i<N-1; i++) {
            u[i] = sc.nextInt();
            v[i] = sc.nextInt();
            w[i] = sc.nextInt();
        }

        int[] color = solve2(N, u, v, w);
        for (int i=0; i<N; i++) {
            System.out.println(color[i]);
        }
    }

    private static int[] solve2(int N, int[] u, int[] v, int[] w) {
        int[] ans = new int[N];
        List<int[]>[] tree = new ArrayList[N];
        for (int i=0; i<N; i++) {
            tree[i] = new ArrayList();
        }

        for (int i=0; i<N-1; i++) {
            tree[u[i]-1].add(new int[]{v[i]-1, w[i]});
            tree[v[i]-1].add(new int[]{u[i]-1, w[i]});
        }

        find(tree, ans, 0, -1, 0);

        return ans;
    }

    private static void find(List<int[]>[] tree, int[] ans, int v, int parent, int color) {
        ans[v] = color;

        for (int[] edge: tree[v]) {
            if (edge[0] == parent) continue;
            if (edge[1]%2 == 0) {
                find(tree, ans, edge[0], v, color);
            } else {
                find(tree, ans, edge[0], v, 1-color);
            }
        }
    }
}


Measured memory_bytes: 97860
#include <cstdio>

double dp[302][302][302];

int cnt[4];

int main(){
  int N;
  scanf("%d",&N);
  for(int k=0;k<=N;k++){
    for(int j=0;j<=N;j++){
      for(int i=0;i<=N;i++){
	if(i==0&&j==0&&k==0){
	  continue;
	}
	double sum=0;
	if(i>0) sum+=i*dp[i-1][j][k];
	if(j>0) sum+=j*dp[i+1][j-1][k];
	if(k>0) sum+=k*dp[i][j+1][k-1];
	dp[i][j][k]=(sum+N)/(i+j+k);
	//printf("1:%d 2:%d 3:%d %.10lf\n",i,j,k,dp[i][j][k]);
      }
    }
  }
  for(int i=0;i<N;i++){
    int A;
    scanf("%d",&A);
    cnt[A]++;
  }
  printf("%.10lf\n",dp[cnt[1]][cnt[2]][cnt[3]]);
  return 0;
}


Measured memory_bytes: 215168
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <stack>
#include <functional>
#include <random>
using namespace std;
#define Would
#define you

int main() {
	long long a, b, mon, ans = 0;
	cin >> a >> b;
	mon = a;
	while (mon <= b) {
		mon *= 2;
		++ans;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
from collections import defaultdict
from operator import itemgetter

n = int(input())
v_list = [int(x) for x in input().split()]

temp = [defaultdict(int, {"": 0}), defaultdict(int, {"": 0})]
for i in range(0, n, 2):
    temp[0][v_list[i]] += 1
for i in range(1, n, 2):
    temp[1][v_list[i]] += 1

even_list = sorted(temp[0].items(), reverse=True, key=itemgetter(1))
odd_list = sorted(temp[1].items(), reverse=True, key=itemgetter(1))

if even_list[0][0] != odd_list[0][0]:
    ans = n - even_list[0][1] - odd_list[0][1]
else:
    ans = min(n - even_list[0][1] - odd_list[1][1], n - even_list[1][1] - odd_list[0][1])
print(ans)

Measured memory_bytes: 23008
// And you curse yourself for things you never done

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

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

const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;

int lst[26], C;
int nxt[26][maxn], val[26][maxn], fin[maxn];

pii add(string &s){
    memset(lst, -1, sizeof lst);
    for(int i = 0; i < sz(s); i++){
	lst[s[i] - 'a'] = i;
    }
    int nw = 0, nwb = 0;
    for(int i = 0; i < sz(s); i++){
	for(int j = 0; j < 26; j++)
	    val[j][nw]+= lst[j] >= i;
	nwb = nw;
	if(nxt[s[i] - 'a'][nw] == 0)
	    nxt[s[i] - 'a'][nw] = ++C;
	nw = nxt[s[i] - 'a'][nw];
    }
    fin[nw]++;    
    return {nwb, nw};
}

pii p[maxn];
int lstc[maxn];

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

    int n;
    cin >> n;
    for(int i = 0; i < n; i++){	
	string s;
	cin >> s;
	reverse(s.begin(), s.end());
	lstc[i] = s.back() - 'a';
	p[i] = add(s);
    }
    ll ans = 0;
    for(int i = 0; i < n; i++){
	ans+= val[lstc[i]][p[i].F];
    }
    for(int i = 0; i < maxn; i++){
	ans-= fin[i] * fin[i];
	ans+= 1ll * fin[i] * (fin[i]-1) / 2;
    }
    return cout << ans << endl, 0;
}


Measured memory_bytes: 208984
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    ll a,b,c,k;
    cin>>a>>b>>c>>k;
    int flag=0;
    int m=0;
    while(b<=a)
    {
        m++;
        b*=2;
    }
    while(c<=b)
    {
        m++;
        c*=2;
    }

    if(m>k)
    cout<<"No";
    else
    cout<<"Yes";

    return 0;
}


Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll; 
#define int ll   
#define long int
#define pb push_back
#define all int i=0; i<n; i++
#define INF 10000000000000000

#define Nirvana ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define mod 1000000007

long expo(long base, long exp) 
{ 
    long t = 1L; 
    while (exp > 0)  
    { 
        if (exp % 2 != 0) 
            t = (t * base) ; 
  
        base = (base * base) ; 
        exp /= 2; 
    } 
    return t; 
} 
long powmod(long base, long exp) 
{ 
    long t = 1L; 
    base%=mod;
    while (exp > 0)  
    { 
        if (exp % 2 != 0) 
            t = (t * base)%mod ; 
  
        base = (base * base)%mod ; 
        exp /= 2; 
    } 
    return t%mod; 
} 

#define pow expo

int abso(int k){
	if(k < 0)return -1 * k;
	else return k;
}
int ceilo(int l, int r){
	return l/r + min(l%r, int(1));
}
int flooro(int l, int r){
	return l/r;
}
vector<int>f;
int Logn(int n,  int r) 
{ 
    return (n > r - 1) ? 1 + Logn(n / r, r) : 0; 
}
int n, q;
vector<int>g[200005];

int cal(int i, int j){
	return ((j* (j+1))/2) - (i * (i-1))/2;
}
int32_t main(){
	int n, k;
	cin>>n>>k;
	int arr[n+2];
	int ans = 0;
	for(int i=k; i<=n+1; i++){
		//cout<<cal(n+1-i, n-i+k)<<" "<<cal(0, k-1)<<endl;
		ans+= (cal(n-i+1, n)%mod - cal(0, i-1)%mod + 1)%mod;
		//cout<<i<<" "<<ans<<endl;
		ans%=mod;
	}
	
	cout<<(ans+mod)%mod<<endl;
}

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

void solve(std::istream &ist, std::ostream &ost)
{
    /* ------- 宣言と入力 ------- */

    int N;
    ist >> N;

    unordered_set<string> keihin;

    for (int i = 0; i < N; i++)
    {
        string input;
        ist >> input;
        keihin.emplace(input);
    }

    /* ------- 計算 ------- */
    
    cout << keihin.size() << endl;
}

#ifndef WOMAIN
int main()
{
    solve(std::cin, std::cout);
    return 0;
}
#endif


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

int main(){

    int N,M;
    cin >> N >> M;
    vector<int> A(M),B(M);
    for(int i=0;i<M;i++){
        cin >> A[i] >> B[i];
        A[i]--; B[i]--;
    }

    vector<vector<int>> G(N);
    for(int i=0;i<M;i++){
        G[A[i]].push_back(B[i]);
        G[B[i]].push_back(A[i]);
    }

    vector<bool> flags(N,false);
    int cnt;
    int res = 0;
    for(int i=0;i<N;i++){
        if(G[i].empty())
            continue;
        else{
            cnt = 0;
            stack<int> dfs;
            dfs.push(i);
            flags[i] = true;
            while(!dfs.empty()){
                int now = dfs.top();
                if(G[now].empty()){
                    dfs.pop();
                    cnt++;
                }
                else{
                    int next = G[now].back();
                    G[now].pop_back();
                    if(flags[next] == false){
                        dfs.push(next);
                        flags[next] = true;
                    }
                }
            }
        }
        if(cnt > res)
            res = cnt;
    }

    if(M == 0)
        cout << 1;
    else
        cout << res;

    return 0;
}

Measured memory_bytes: 16624
require 'set'

N = gets.to_i

edges = Hash.new {|h,v| h[v] = [] }

(N-1).times do
  u, v, w = gets.split.map(&:to_i)
  edges[u-1] << [u-1, v-1, w]
  edges[v-1] << [v-1, u-1, w]
end

distance = Array.new(N)
distance[0] = 0


q = edges[0]

while !q.empty?
  u, v, w = q.shift
  next if distance[v]
  distance[v] = distance[u] + w
  edges[v].each do |e|
  	q << e
  end
end

distance.each do |d|
  puts d.even? ? '0' : '1'
end


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

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

	string ans = "No";
	for (int i = 1; i <= 9; i++) {
		if (ans == "Yes") {
			break;
		}
		else {
			for (int j = 1; j <= 9; j++) {
				if (i * j == n) {
					ans = "Yes";
					break;
				}
			}
		}
	}

	cout << ans << endl;
	return 0;

}

Measured memory_bytes: 3572
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0,i##_max=(N);i<i##_max;++i)
#define repp(i,l,r) for(int i=(l),i##_max=(r);i<i##_max;++i)
#define per(i,N) for(int i=(N)-1;i>=0;--i)
#define perr(i,l,r) for(int i=r-1,i##_min(l);i>=i##_min;--i)
#define all(arr) (arr).begin(), (arr).end()
#define SP << " " <<
#define SPF << " "
#define SPEEDUP cin.tie(0);ios::sync_with_stdio(false);
#define MAX_I INT_MAX //1e9
#define MIN_I INT_MIN //-1e9
#define MAX_UI UINT_MAX //1e9
#define MAX_LL LLONG_MAX //1e18
#define MIN_LL LLONG_MIN //-1e18
#define MAX_ULL ULLONG_MAX //1e19
  typedef long long ll;
  typedef pair<int,int> PII;
  typedef pair<char,char> PCC;
  typedef pair<ll,ll> PLL;
  typedef pair<char,int> PCI;
  typedef pair<int,char> PIC;
  typedef pair<ll,int> PLI;
  typedef pair<int,ll> PIL; 
  typedef pair<ll,char> PLC; 
  typedef pair<char,ll> PCL; 

inline void YesNo(bool b){ cout << (b?"Yes" : "No") << endl;}
inline void YESNO(bool b){ cout << (b?"YES" : "NO") << endl;}
inline void Yay(bool b){ cout << (b?"Yay!" : ":(") << endl;}

int main(void){
  SPEEDUP
  cout << setprecision(15);
  int N;cin >> N;
  vector<PII> v(N);
  rep(i,N){
    cin >> v[i].first;
    v[i].second = i+1;
  }
  sort(all(v));
  rep(i,N) cout << v[i].second SPF;
  cout << endl;
  return 0;
}


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

using namespace std;

int main(){
	
	ios::sync_with_stdio(0);
	
	int n,k;
	
	cin>>n>>k;
	
	vector <int> dp(k+1,0),arr(n);
	
	for(int i=0; i<n; i+=1){
		
		cin>>arr[i];
	}
	
	for(int i=0; i<=k; i+=1){
		
		if(dp[i] == 0){
			
			for(int j=0; j<n; j+=1){
				
				if(i+arr[j] <= k){
					
					dp[i+arr[j]] = 1;
				}
			}
		}
	}
	
	if(dp[k]){
		
		cout<<"First";
	}
	
	else{
		
		cout<<"Second";
	}
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vl vector<ll>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mod 1000000007
#define inf 1000000000000000
#define md 998244353
#define endl "\n"

void solve()
{
    int n,k,ind;
    cin>>n>>k;
    
    int a[n];
    
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
      if(a[i]==1)
      ind=i;
    } 
    
    if((n-1)%(k-1)==0)
    cout<<(n-1)/(k-1)<<endl;
    else
    cout<<(n-1)/(k-1)+1<<endl;
}

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

Measured memory_bytes: 640
#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <cmath>  
#include <string>  
#include <map>  
#include <stack>  
#include <vector>  
#include <set>  
#include <queue>  
#include <string>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cctype>
#include <map>
#include <cctype>
#include <vector>
#include <cstdlib>
#include <set>
#include <cmath>  
using namespace std;
long long mod = (long long)(1e9 + 7);
long long  a[] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997 };
long long b[168];
int main()
{
	long long  p;long long n;
	cin >> n;
	
	for(int p=1;p<=n;p++)
	for (long long i = 0; i < 168; i++)
	{
		int num = 0;
		int temp = p;
		while (temp%a[i] == 0)
		{
			temp /= a[i];
			num++;
		}
		b[i] += num;
	}
	long long sum = 1;
	for (int i = 0; i < 168; i++)
	{
		if (a[i] != 0)
			sum *= (b[i] + 1) % mod;
		sum %= mod;
	}
	cout << sum << '\n';
	return 0;

}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>

using namespace std;

#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 FORR(i, m, n) for(int i = n;i >= m;i--)
#define REPO(i, n) for(int i = 1;i <= n;i++)
#define ll long long
#define INF 1999999999
#define MINF -1999999999
#define ALL(n) n.begin(),n.end()
#define MP make_pair

int main() {
	string s;
	ll k, c = 0,d = 1;
	cin >> s >> k;
	REP(i,s.size()){
		if (s[i] == '1')c++;
		else {
			d = s[i] - '0';
			break;
		}
	}
	if (k <= c) cout << 1 << endl;
	else cout << d << endl;
}

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

using namespace std;

int main()
{
	int n;
	cin >> n;
	int m = 0;
	vector<int> t(n);
	for (int i = 0; i < n; i++)
	{
		cin >> t[i];
		m = max(m, t[i]);
	}
	vector<int> g;
	for (int i = 1; i <= (m + 1) / 2; i++)
		if (m % i == 0)
		{
			g.push_back(i);
			g.push_back(m / i);
		}
	sort(g.begin(), g.end());
	int ans = 0;
	for (int i = 0; i < n; i++)
		ans += g[lower_bound(g.begin(), g.end(), t[i]) - g.begin()] - t[i];
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 3292
#include<stdio.h> 
#include<string.h>
int main(){
	char a[11],b[11],c[11];
	scanf("%s%s%s",a,b,c);
	if(a[strlen(a)-1]==b[0]&&b[strlen(b)-1]==c[0])
	printf("YES\n");
	else printf("NO\n");
	return 0;
}

Measured memory_bytes: 128
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
// #define USE_LLONG_AS_INT
#ifdef USE_LLONG_AS_INT
#define int long long
#define inf (1ll<<60)
#else
#define inf (1<<30)
#endif
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=(n)-1;i>=0;i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;
const string sp=" ";

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

int x[1000*1500],y[1000*1500];

void run(){
    int n,m;
    cin>>n>>m;
    if(!n)return;
    vi h(n+1),w(m+1);
    rep(i,n)cin>>h[i+1];
    rep(i,m)cin>>w[i+1];
    rep(i,n)h[i+1]+=h[i];
    rep(i,m)w[i+1]+=w[i];
    MEMSET(x),MEMSET(y);
    rep(i,n)Rep(j,i+1,n+1)x[h[j]-h[i]]++;
    rep(i,m)Rep(j,i+1,m+1)y[w[j]-w[i]]++;
    int ans=0;
    rep(i,1000*1500)ans+=x[i]*y[i];
    cout<<ans<<endl;
    run();
}


Measured memory_bytes: 15180
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <stdio.h>
using namespace std;

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

	int L = (s.size() - 2);

	cout << s[0] << L << s[s.size() - 1] << endl;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i = 0; i != n; ++i)

const double EPS = 1e-10;
const int INF = 1e7;

int main(){
  int n, x;
  scanf("%d%d", &n, &x);
  vector<int> a(n, 0);
  int min_w = INF;
  int sum_w = 0;
  for (int i=0; i<n; ++i) {
    cin >> a[i];
    sum_w += a[i];
    if (min_w > a[i]) {
      min_w = a[i];
    }
  }
  int rest = x - sum_w;
  std::cout << n + (int)(rest / min_w) << '\n';
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<fstream>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;
#define mp(a,b) make_pair((a),(b))
bool vis[100005];
int n,dis,t,ans,g[100005];
pair<int,int> x[100005],y[100005];
pair<int,pair<int,int> > tmp;
priority_queue<pair<int,pair<int,int> > > q;
int Abs(int ooo){return ooo>=0?ooo:-ooo;}
int FIND(int ooo)
{
	if(g[ooo]==ooo)return ooo;
	else return g[ooo]=FIND(g[ooo]);
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d %d",&x[i].first,&y[i].first);
		x[i].second=i;
		y[i].second=i;
		g[i]=i;
	}
	sort(x,x+n);
	sort(y,y+n);
	for(int i=1;i<n;i++)
	{
		dis=x[i].first-x[i-1].first;
		q.push( mp( -dis , mp( x[i-1].second , x[i].second ) ) );
		dis=y[i].first-y[i-1].first;
		q.push( mp( -dis , mp( y[i-1].second , y[i].second ) ) );
	}
	while(!q.empty())
	{
		tmp=q.top();
		q.pop();
		if(FIND(tmp.second.first)==FIND(tmp.second.second))continue;
		ans+=-tmp.first;
		g[FIND(tmp.second.first)]=FIND(tmp.second.second);
		t++;
		if(t==n-1)break;
	}
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 6900
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		if (1 <= a && a <= 3000) {
			if (a < 1200) {
				System.out.println("ABC");
			} else {
				System.out.println("ARC");
			}
		}
	}
}

Measured memory_bytes: 9172
#include <iostream>
//#include <iomanip>
//#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

int main(){

	int h[10];

	for (int i = 0; i < 10; i++){
		cin >> h[i];
	}

	sort(h,h+10);

	for (int i = 9; i > 6; i--){
		cout << h[i] << endl;
	}

	return 0;
}

Measured memory_bytes: 1176
import heapq

N, M = map(int, input().split())
AB = [[] for i in range(10**5+1)]

for i in range(N):
    A, B = map(int, input().split())
    AB[A].append(-B)

hq = []
heapq.heapify(hq)

ans = 0
for i in range(1, M+1):
    for b in AB[i]:
        heapq.heappush(hq, b)

    if len(hq) > 0:
        ans += -heapq.heappop(hq)

print(ans)


Measured memory_bytes: 22272
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
ans=100000000000000

if x[0]*x[-1]>0:
    if x[0]>0:
        ans=x[k-1]
    elif x[0]<0:
        ans=abs(x[-k])
else:
    for i in range(n-k+1):
        if x[i]*x[i+k-1]<=0:
            ans=min(abs(x[i])+abs(x[i+k-1])+min(abs(x[i]),abs(x[i+k-1])),ans)
print(ans)

Measured memory_bytes: 14660
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B = [0] + B + [0]
total_ene = sum(A)


survive_enemy = 0
for i in range(N+1):
  
  if A[i] - B[i]<=0:
    pass    
  else:  
    surv_ene_i = A[i] - (B[i] + B[i+1])
    if surv_ene_i >=0:
      survive_enemy += surv_ene_i
      B[i+1] = 0
    else:
      B[i+1] = - surv_ene_i

max_lose_ene = total_ene - survive_enemy

print(max_lose_ene)

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

long long mod = 998244353;

int main(){
  long long N,K;
  cin >> N >> K;
  vector<long long> V(N*2+1,0),L(K),R(K);
  for(int i=0;i<K;i++) cin >> L[i] >> R[i];
  sort(L.begin(),L.end());
  sort(R.begin(),R.end());
  V[0] = 1;
  V[1] = -1;
  
  for(int i=0;i<N;i++){
    for(int j=0;j<K;j++){
      V[i+L[j]] += V[i];
      V[i+L[j]] %= mod;
      
      V[i+R[j]+1] += mod;
      V[i+R[j]+1] -= V[i];
      V[i+R[j]+1] %= mod;
    }
    V[i+1] += V[i];
    V[i+1] %= mod;
  }
  
  cout << V[N-1] << endl;
  return 0;
}


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

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) for (int i = 0; i <= (int)(n); 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 all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> P;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill( (T*)array, (T*)(array+N), val ); }


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

    string S; cin >> S;
    int ans = 0, n = S.size();
    rep(i, n/2) {
        if (S[i] != S[S.size()-i-1]) {
            ans++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
using System;
namespace aoj {
	public class Program {
		public static void Main() {
			string str;
			int c = 0;
			while ((str = Console.ReadLine()) != null) {
				int i = 0;
				while(i < str.Length && str[i] == str[str.Length - i - 1]) i++;
				c += (i == str.Length) ? 1 : 0;
			}
			Console.WriteLine(c);
		}
	}
}

Measured memory_bytes: 8988
#define debug_interval ','
#define dump_interval ' '
#define debug_toggle 1
//{
#include<bits/stdc++.h>
using namespace std;
#define hi cerr<<"hi"<<endl;
#define int long long
#define INT_MAX LLONG_MAX
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define fi first
#define se second
#define mp make_pair
#define rev reverse
#define dans dump(ans)
#define MOD 1000000007
#define amp(v,n) (v).count(n)?v[n]++:v[n]=1
#define sysp system("pause")
#define PI acos(-1)
#define pf push_front
#define ins insert
#define fh(x,v) for(auto x:v)

//{
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class...A> inline void dump(){cout<<endl;}
template<class...A> inline void dump_rest() {cout<<endl;}
template<class T, class...A> inline void dump_rest(const T& first, const A&... rest){cout<<dump_interval<<first;dump_rest(rest...);}
template<class T,class...A> inline void dump(const T&first,const A&...rest){cout<<first;dump_rest(rest...);}
template<class...A> inline void debug(){cerr<<endl;}
template<class...A> inline void debug_rest() {cerr<<endl;}
template<class T, class...A> inline void debug_rest(const T& first, const A&... rest){cerr<<debug_interval<<first;debug_rest(rest...);}
template<class T,class...A> inline void debug(const T&first,const A&...rest){if(debug_toggle)cerr<<first,debug_rest(rest...);}
unsigned XorShift(void){static unsigned x = 123456789;static unsigned y = 362436069;static unsigned z = 521288629;static unsigned w = 88675123;unsigned t;t = x ^ (x << 11);x = y; y = z; z = w;return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));}
//}

typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
template<int m>class mint{private:int i;public:mint() : i(0){}mint(int i): i((i%m+m)%m){}mint operator+(const mint& o){return o.i+i;}mint operator*(const mint& o){return o.i*i;}mint operator-(){return -i;}operator int() {return i;}};

//}

int ans;

main(){
	int n,a=0,b=0,c=0;
	cin>>n;
	rep(i,n){
		string s;
		cin>>s;
		if(s.front()=='B')b++;
		if(s.back()=='A')a++;
		if(s.front()=='B'&&s.back()=='A')c++;
		for(int i=0;i<s.size()-1;){
			if(s[i]=='A'&&s[i+1]=='B'){
				ans++;
				i+=2;
			}
			else i++;
		}
	}
	if(c==a&&c==b)dump(ans+max(min(a,b)-1,0ll));
	else dump(ans+min(a,b));
}





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

sys.setrecursionlimit(1000000)

def main(): 

    N = int(input())
    v = list(map(int,input().split()))
    c = list(map(int,input().split()))
    x=0
    y=0
    for i in range(N):
        if v[i] > c[i]:
            x+=v[i]
            y+=c[i]
    print(x-y)
        



if __name__ == "__main__":
    main()
    

Measured memory_bytes: 3060
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <climits>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

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

int N;
vector<string> A;

int main() {
	cin >> N;
	A = vector<string>(N);
	int res = 0;
	int cntBA = 0;
	int cntXA = 0;
	int cntBX = 0;

	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < N; i++) {
		int len = A[i].length();
		for (int j = 0; j < len; j++)
			if (A[i][j] == 'A' && A[i][j + 1] == 'B') {
				res++;
			}
		if (A[i][len-1] == 'A') {
			cntXA++;
		}
		if (A[i][0] == 'B') {
			cntBX++;
		}
		if (A[i][0] == 'B' && A[i][len-1] == 'A') {
			cntBA++;
			cntBX--;
			cntXA--;
		}
	}
	int ans = min(cntBA + cntBX, cntBA + cntXA);
	if (cntXA + cntBX == 0 && cntBA > 0 )
	{
		ans = cntBA - 1;
	}
	cout << ans + res << endl;
	return 0;
}


Measured memory_bytes: 768
from collections import defaultdict
import heapq
N = int(input())
S = list(map(int,input().split()))
d = defaultdict(lambda:0)
for s in S:
    d[s] += 1
S = []
for s in d:
    S.append([s,d[s]])
S.sort(reverse=True)
S = [s[1] for s in S]
c = [-N]
possible = True
for s in S:
    if len(c)<s:
        possible = False
        break
    l = []
    for k in range(s):
        i = -heapq.heappop(c)
        l.append(i)
    for i in l:
        for j in range(i):
            heapq.heappush(c,-j)
if possible:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 126920
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define pb push_back
#define eb emplace_back
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
const ll INF = numeric_limits<ll>::max()/4;
const int n_max = 1e5+10;

void print() {
    cout << endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
void print(vector<T> &vec) {
    for (auto& a : vec) {
        cout << a;
        if (&a != &vec.back()) cout << ' ';
    }
    cout << endl;
}

template <class T>
void print(vector<vector<T>> &df) {
    for (auto& vec : df) {
        print(vec);
    }
}

void print(Pll &p){
    print(p.first, p.second);
}


int main(){
    ll n,k,s; cin >> n >> k >> s;
    vector<ll> ans(n,1);
    if(s == 1e9){
        rep(i,k)ans[i] = s;
    }
    else{
        rep(i,k)ans[i] = s;
        for(int i = k; i < n;i++)ans[i] = 1e9;
    }
    print(ans);
}

Measured memory_bytes: 2048
N, M = map(int,input().split())
ans = 1
if abs(N-M) >= 2:
    ans = 0
elif N == M:
    for i in range(1,M+1):
        ans = (ans*i) % (10**9+7)
    for i in range(1,M+1):
        ans = (ans*i) % (10**9+7)
    ans = (ans*2) % (10**9+7)
else:
    for i in range(1,M+1):
        ans = (ans*i) % (10**9+7)
    for i in range(1,N+1):
        ans = (ans*i) % (10**9+7)
print(ans)

Measured memory_bytes: 9220
n = int(input())
S = input()
new_S = ''
alp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

for d in range(len(S)):
    for p in alp:
        if S[d] == p:
            i = int(alp.index(p))
            i = i + n
            if i >= 26:
                i = i - 26
            new_S += alp[i]
print(new_S)

Measured memory_bytes: 3060
import collections


def run(N, A):
    cnt = 0
    beki = [2**i for i in range(1, 32)]
    A = sorted(A, reverse=True)
    used = collections.Counter(A)
    for a in A:
        if used[a] == 0:
            continue
        used[a] -= 1
        while beki[-1] > 2*a:
            beki.pop(-1)
        if used[beki[-1] - a] >= 1:
            cnt += 1
            used[beki[-1] - a] -= 1
    return cnt


def main():
    N = int(input())
    A = list(map(int, input().split()))
    print(run(N, A))


if __name__ == '__main__':
    main()


Measured memory_bytes: 34952