akhauriyash/Code-Regression huggingface dataset:
/*
 * author: Tran Minh Khoa
 * Ho Chi Minh University of Science
 */

#include <bits/stdc++.h>

using namespace std;

const int inf = (int) 1e9 + 7;
const long long INF = (long long) 1e18 + 7;

const int N = 123456;
const int M = 1010;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) (int) a.size()
#define all(a) a.begin(), a.end()
#define sqrt(x) (x) * (x)

#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define DEBUG(x) cerr << #x << " = " << x << '\n';
#define PR(x, a, b) { cerr << #x << " = "; REP(i, a, b) cerr << x[i] << ' '; cerr << '\n'; }
                 
int main() {
  #ifdef KM777
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
  #endif
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr); cout.tie(nullptr);
  int n, Q;
  char s[N];
  cin >> n >> Q;
  cin >> s + 1;
  vector<int> fx(n + 1);
  for (int i = 1; i < n; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      fx[i] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    fx[i] += fx[i - 1];
  } 
  while (Q--) {
    int l, r;
    cin >> l >> r;
    int result = fx[r - 1] - fx[l - 1];
    cout << result << '\n';
  }
        
  return 0;
}

Measured memory_bytes: 1408
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main()
{
    int N;
    cin >> N;
    cout << N / 3 << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define co(x) cout << x;
#define ce cout << endl;
#define cs cout << " ";
#define coe(x) cout << x << endl;
#define cos(x) cout << x << " ";

int main() {
  
  int X, N; cin >> X >> N;
  int ps[N];
  rep(i, N){
    int p; cin >> p; ps[i] = p;
  }
  
  int ansp, ansm;
  
  rep(i, 101){
    bool bp = false;
    for (int j : ps) {
      if (X+i == j) bp = true;
    }
    if (!bp) {
      ansp = X+i;
      break;
    }
  }
  
  rep(i, 101){
    bool bm = false;
    for (int j : ps) {
      if (X-i == j) bm = true;
    }
    if (!bm) {
      ansm = X-i;
      break;
    }
  }
 
  int ans;
  if (ansp - X >= X - ansm) ans = ansm;
  else ans = ansp;
  coe(ans)
}

Measured memory_bytes: 3620
S = input()
ns = len(S)
T = input()
nt = len(T)
if T in S:
    print(S.replace('?', 'a'))
    exit()

for i in reversed(range(ns - nt + 1)):
    for x, y in zip(S[i:], T):
        if not (x == '?' or x == y):
            break
    else:
        r = S.replace('?', 'a')
        print(r[:i] + T + r[i + nt:])
        break
else:
    print('UNRESTORABLE')

Measured memory_bytes: 3064
#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

struct UnionFind{
    vector<int> par,rank;
    
    //初期化(n:頂点数)
    UnionFind(int n){
        for(int i=0;i<n;++i){
            par.push_back(i);
            rank.push_back(0);
        }
    }

    void init(){
        par.resize(0);
        rank.resize(0);
    }

    //根をFind
    int find(int i){
        if(par[i]==i)return i;

        return par[i]=find(par[i]);
    }

    //Union
    void unite(int x,int y){
        int rx=find(x);
        int ry=find(y);

        if(rx==ry)return;

        if(rank[rx]>rank[ry])par[ry]=rx;
        else{
            par[rx]=ry;
            if(rank[rx]==rank[ry])
                rank[ry]++;
        }
    }

    //同じ木に属するか判定
    bool same(int x,int y){
        return find(x)==find(y);
    }
};

int main(){
    int N,M;cin>>N>>M;
    int a,b;
    pair<int,int> p[60];
    for(int i=0;i<M;i++){
        cin>>a>>b;
        a--;b--;
        p[i]={a,b};
    }

    int bridge=0;
    for(int i=0;i<M;i++){
        UnionFind UF(N);
        for(int k=0;k<i;k++)
            UF.unite(p[k].first,p[k].second);
        for(int k=i+1;k<M;k++)
            UF.unite(p[k].first,p[k].second);
        if(!UF.same(p[i].first,p[i].second))bridge++;
        UF.init();
    }
    cout<<bridge<<endl;
    return 0;
}

Measured memory_bytes: 256
import sys
input = lambda: sys.stdin.readline().rstrip()


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


n, k = map(int, input().split())
a = list(map(int, input().split()))
g = a[0]
for i in range(1, n):
    g = gcd(g, a[i])
if k % g == 0 and max(a) >= k:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')


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

int main(){
    int N;
    cin >> N;
    int A[N];
    for (int i = 0; i < N; i++){
        cin >> A[i];
    }
    bool flag = true;
    for (int i = 0; i < N; i++){
        if (A[i] % 2 == 0){
            if (A[i] % 3 != 0 && A[i] % 5 != 0) flag = false;
        }
    }
    if (flag){
        cout << "APPROVED" << endl;
    } else{
        cout << "DENIED" << endl;
    }
    return 0;
}

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

int main() {
  int n,m,k;
  cin >> n >> m >> k ;
  vector<int>a(n),b(m);
  rep(i,n) cin >> a.at(i);
  rep(i,m) cin >> b.at(i);
  vector<ll>suma(n+1),sumb(m+1);//引数冊読むのにかかる時間
  int ans =0;
  rep(i,n+1) {
    if (i==0) suma.at(i)=0;
    else suma.at(i)=suma.at(i-1)+a.at(i-1);
  }
  rep(i,m+1) {
    if (i==0) sumb.at(i)=0;
    else sumb.at(i)=sumb.at(i-1)+b.at(i-1);
  }
  rep(i,n+1) {
    int left=0,right=m+1,mid;
    ll t=k-suma.at(i);
    if (t<0) {
      break;
    }
    else {
      while (left<right-1) {
        mid = (left+right)/2;
        if (sumb.at(mid)>t) right=mid;
        if (sumb.at(mid)<t) left=mid;
        if (sumb.at(mid)==t) left=mid, right=mid+1;
      }
      ans=max(ans,i+left);
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 7992
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
    using namespace std;
 
    int main(){
      ll s;
      cin >> s;
      ll a[10];
      for(int i=0;i<10;i++){
       a[i]=s%10;
        s/=10;
      }
      ll ans=inf;
      for(int i=0;i+2<=10;i++){
        ans=min(ans,abs(753-100*a[i+2]-10*a[i+1]-a[i]));
      }
      cout << ans << endl;
      return 0;}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=100010;
const ll mod=1e9+7;

set<int> st;
bool ch(int x){
  bool flag = true;
  while(x>0){
    int tmp = x%10;
    if(st.find(tmp)!=st.end()){flag=false; break;}
    x/=10;
  }
  return flag;
}

int main(){
  int n,k;
  cin >> n >> k;
  rep(i,n){
    int a;
    cin>>a;
    st.insert(a);
  }
  for(; ; n++){
    if(ch(n)){break;}
  }  
  cout << n << endl;
  return 0;
}


Measured memory_bytes: 256
/*
It's been a long day without you, my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again
When I see you again
*/
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define null nullptr
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define precise(x) cout << fixed << setprecision(x)
#define pb push_back
#define mp make_pair
#define test int t; cin>>t; while(t--)
#define FAST                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
 
using namespace std;
 

 
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef pair<int,int> pii;
typedef unordered_map<int,int> mp;
 
const int mod = 1e9+7;
 
int sod(int n){int sum=0;while(n>0){sum+=n%10;n/=10;}return sum;}
 
int dig(int n){if(n==0)return 1;int cnt=0;while(n>0){n/=10;cnt++;}return cnt;}
 
int divs(int n){int cnt=0;for(int x=1;x*x<=n;x++){if(n%x==0){cnt++;if(n/x!=x)cnt++;}}return cnt;}
 
int isprime(int n){if(n<2)return false;for(int x=2;x*x<=n;x++)if(n%x==0)return false;return true;}
 
int power(int x,unsigned int y,int p=mod){int res=1;x=x%p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}

bool pal(string s){int l=0,r=s.size()-1;while(l<r){if(s[l++]!=s[r--])return false;}return true;} 

int lcm(int a,int b){return (a*b)/__gcd(a,b);}

bool isSquare(int n){int root=sqrt(n);return (root*root==n);}

const int inf = 1e18;



int32_t main(){
    FAST;

    string s;   cin >> s;

    int fi = 0, se = 0;
    bool done = 0;

    for(int x=0;x<s.size();x++){
        if(s[x] == 'A' and !done){
            fi = x;
            done = 1;
        }

        if(s[x] == 'Z'){
            se = x;
        }

    }

    cout << (se - fi + 1) << endl;


    return 0;
}
 
 
 

// const int N = 1e6;
// bool prime[N+3];
// vi primes;
// void sieve(){
 
//   memset(prime , true , sizeof prime );
 
//   prime[0] = false , prime[1] = false;
//   prime[2] = true;
 
//   for(int x=2;x*x<=N;x++){
//     if(prime[x] == true){
//       for(int i=x*x;i<=N;i+=x)
//         prime[i] = false;
//     }
//   }

//   //for(int x=0;x<N;x++)
//   //  if(prime[x])
//   //      primes.pb(x);
 
// } 


// const int N = 1e6+50;
// int spf[N+11];
 
// void SPF(){
 
// 	spf[1] = 1; 
  
//     for (int i = 2; i <= N; i++) 
//     { 
//         if (spf[i] == 0) 
//         { 
//             spf[i] = i; 
  
//             for (int j = 2*i; j <= N; j += i) 
//                 if (spf[j] == 0) 
//                    spf[j] = i; 
//         } 
//     } 
// }
 

Measured memory_bytes: 720
#include <cstdio>
using namespace std;

inline bool find(int* arr, int len, int a, int b, int c) // Returns: a in arr && b in arr && c in arr.
{
	bool hasa = false, hasb = false, hasc = false;
	for(int i=0; i<len; i++)
	{
		if(arr[i] == a) hasa = true;
		if(arr[i] == b) hasb = true;
		if(arr[i] == c) hasc = true;
	}
	return hasa && hasb && hasc;
}

int main(int argc, char** argv)
{
	int a, b, c,
		d, e, f,
		g, h, i, n;
	scanf("%d%d%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &n);
	int* ar = new int[n];
	for(int i=0; i<n; i++) scanf("%d", ar + i);
	puts((find(ar, n, a, b, c) || find(ar, n, d, e, f) || find(ar, n, g, h, i) || find(ar, n, a, d, g) || find(ar, n, a, d, g) || find(ar, n, b, e, h) || find(ar, n, c, f, i) || find(ar, n, a, e, i) || find(ar, n, c, e, g))? "Yes": "No");
	delete[] ar;
	return 0;
}

Measured memory_bytes: 256
import java.util.*

class Bridge(a: Int, b: Int) {
    val a = a
    val b = b
}

class Island(parent: Int, depth: Int, size: Int) {
    var parent = parent
    var depth = depth
    var size = size
}

fun root(i: Int, a: Array<Island>): Int {
    var now: Int = i
    while (a[now].parent != -1) {
        now = a[now].parent
    }
    return now
}

fun main(args: Array<String>){
	val cin = Scanner(System.`in`)
    val n = cin.nextInt()
    val m = cin.nextInt()
    
    val arr = Array<Bridge>(m, { Bridge(cin.nextInt() - 1, cin.nextInt() - 1) })

    val unionfind = Array<Island>(n, { Island(-1, 1, 1) })

    val inconvenient = Array<Long>(m, { 0 })

    inconvenient[m - 1] = n.toLong() * (n - 1).toLong() / 2

    for (i in 0..m-1) {
        val j = m - 1 - i
        val ia = arr[j].a
        val ib = arr[j].b

        val ix = root(ia, unionfind)
        val iy = root(ib, unionfind)

        if (ix == iy) {
            if (j != 0) {
                inconvenient[j - 1] = inconvenient[j]
            }
            continue
        }

        val along: Long = unionfind[ix].size.toLong() * unionfind[iy].size.toLong()
        if (j != 0) {
            inconvenient[j - 1] = inconvenient[j] - along
        }

        if (unionfind[iy].depth > unionfind[ix].depth) {
            unionfind[ix].parent = iy
            unionfind[iy].size += unionfind[ix].size
        } else {
            unionfind[iy].parent = ix
            unionfind[ix].size += unionfind[iy].size
            if (unionfind[ix].depth == unionfind[iy].depth) {
                unionfind[ix].depth += 1
            }
        }
    }

    for (i in 0..m-1) {
        println(inconvenient[i])
    }
}

Measured memory_bytes: 77424
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <numeric>
#include <deque>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <assert.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define eb emplace_back
#define el '\n'
typedef  long long  ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;


const int mod=1000000007;
//const int mod=998244353;


long long binpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}


vll  mult(vll & a, vll &b){
    int n=(int)a.size();
    vll c(n);
    for(int i=0; i<n; i++){
        for(int j=0; j+i<n; j++){
            c[i+j]+=a[i]*b[j];
            c[i+j]%=mod;
        }
    }
    return c;
}



int main()
{   ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll t,n,m,k,q,x,a,b,c,d,y,l,r,z;
    cin >> n >> c;
    vll A(n), B(n);
    for(int i=0; i<n; i++){
        cin >> A[i];
    }
    for(int i=0; i<n; i++){
        cin >> B[i];
    }
    vvll v(n,vll(c+1,0));
    for(int i=0; i<n; i++){
        v[i][0]=B[i]-A[i]+1;
        for(int j=1; j<=c; j++){
            for(int k=A[i]; k<=B[i]; k++){
                v[i][j]+=binpow(k,j,mod);
            }
            v[i][j]%=mod;
        }
    }
//    print(v);
    for(int i=1; i<n; i++){
        v[i]=mult(v[i-1],v[i]);
    }
    cout << v[n-1][c] << el;
    return 0;
}


Measured memory_bytes: 1536
#include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
#include<complex>
typedef long long int llint;
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n,k;
	cin>>n>>k;
	vector<int>x(n);
	rep(i,n){
		cin>>x[i];
	}
	int ans=0;
	rep(i,n){
		if(x[i]<=k/2){
			ans+=2*x[i];
		}
		else{
			ans+=2*abs(x[i]-k);
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXSIZE = 1e2+7;
int num[MAXSIZE];
int n;
int selectSort(){
	int minj;
	int cnt = 0;
	for(int i = 0; i < n-1; i++){
		minj = i;
		for(int j = i; j < n; j++){
			if(num[minj] > num[j]){
				minj = j;
			}
		}
		swap(num[i],num[minj]);
		if(i != minj) cnt++;
	}
	return cnt;
}
int main(){
	while(~scanf("%d",&n)){
		memset(num,0,sizeof(num));
		for(int i = 0; i < n; i++){
			scanf("%d",&num[i]);
		}
		int cnt = selectSort();
		for(int i = 0; i < n; i++){
			if(i) printf(" ");
			printf("%d",num[i]);
		}
		printf("\n");
		printf("%d\n", cnt);
	}
	return 0;
}


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

int main(){
    int A, B, C, D;

    cin >> A >> B >> C >> D;

    A *= B;
    C *= D;
    if(A > C){
        cout << A << endl;
    }else{
        cout << C << endl;
    }

    return 0;
}


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

int main( void ) {
  int A , B, ans;
  cin >> A;
  cin >> B;

  if( ( A < 10 ) &&  ( B < 10 ) ) {
    ans = A * B;
    cout << ans << endl;
  }
  else {
    cout << -1 << endl;
  }
  
  return ( 0 );
}


Measured memory_bytes: 256
#include<iostream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<cstring>
#include<algorithm>
#include<cmath>
#define Max zhi5968
typedef long long ll;
using namespace std;

string s,t;

int find(int tt)
{
	int i;
	for(i=0;i<t.length();i++)
	{
		if(s[i+tt]!=t[i]&&s[i+tt]!='?')
			return 0;
	}
	return 1;
}

int v(int tt)
{
	for(int i=0;i<t.length();i++)
		s[i+tt]=t[i];
	return 0;
}

int main ()
{
	cin>>s;
	cin>>t;
	int i,bj=0;
	for(i=s.length()-t.length();i>=0;i--)
	{
	//	cout<<i<<endl;
		if(find(i))
			bj++;
		if(bj!=0)
		{
			v(i);
			break;
		}
	}
//	cout<<bj<<endl;
	if(bj==0)
	{
		cout<<"UNRESTORABLE"<<endl;
		return 0;
	}
	for(i=0;i<s.length();i++)
	{
		if(s[i]=='?')
			cout<<"a";
		else
			cout<<s[i];
	}
	return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
    int num1[1001],num2[1001]={0},a=0,b=0;
    while(scanf("%d",&num1[a])!=EOF)
    {
        if(num1[a]==0)
        {
            num2[b]=num1[a-1];
            a-=2;
            b++;
        }
        a++;
    }
    b=0;
    while(num2[b])
    {
        printf("%d\n",num2[b]);
        b++;
    }
    return 0;
}

Measured memory_bytes: 608
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
const ll MOD=1000000007;
const long double PI=3.14159265358979;
const ll MAX=0;

int main() {
  ll N;
  cin>>N;
  ll a[N],sum=0;
  rep(i,N){
    cin>>a[i];
    sum+=a[i];
    sum%=MOD;
  }
  sum*=sum;
  rep(i,N){
    ll A=a[i];
    A*=A;
    A%=MOD;
    sum-=A;
    if(sum<0){
      sum+=MOD;
    }
    sum%=MOD;
  }
  if(sum%2==1){
    sum+=MOD;
  }
  sum/=2;
  cout<<sum;
}


Measured memory_bytes: 5232
#a,bの最大公約数
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

#リスト l の最大公約数
def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = gcd(a,l[i])
    return a

#互いに素なa,bについて、a*x+b*y=1の一つの解
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

#a*x+b*y=gcd(a,b)の一つの解
def extgcd_2(a,b):    
    g = gcd(a,b)
    a = a//g
    b = b//g
    return extgcd(a,b)

#a,bの最小公倍数
def lcm(a, b):
	return a * b // gcd (a, b)

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

Measured memory_bytes: 38256
#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;
const int N = 100000 + 5;

int a[N], b[N], n;
int ans = (1<<30);
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    int cnt = 0;
    for(int i = 1; i <= n; i++){
            b[i] = a[i];
        if(a[i] == i){
            cnt++;
            swap(a[i], a[i+1]);
        }

    }
    ans = min(ans, cnt);
    cnt = 0;
    for(int i = n; i >= 1; i--){
        if(b[i] == i){
            cnt++;
            swap(b[i], b[i+1]);
        }
    }
    ans = min(ans, cnt);
    printf("%d\n", ans);

}


Measured memory_bytes: 1024
# 未完成

import sys
N = int(input())
x = [0]*N
y = [0]*N
kouholist = []
ans = N
if N == 1:
    print(1)
    sys.exit()

for i in range(N):
    x[i], y[i] = map(int, input().split())
for m in range(N):
    for p in range(N):
        if m == p:
            continue
        diff_x = x[m] - x[p]
        diff_y = y[m] - y[p]
        ans_kari = N
        for r in range(N):
            for q in range(N):
                if x[q] - x[r] == diff_x and y[q] - y[r] == diff_y:
                    ans_kari -= 1
        if ans_kari < ans:
            ans = ans_kari

print("{}".format(ans))


Measured memory_bytes: 3188
#include<bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main() {
    string S;
    cin >> S;
    int N = S.size();
    vector<ll> ans(N);
    //Rについてのみ考える
    int count = 0;
    REP(i,N){
        if(S[i] == 'R'){
            count += 1;
        }else{
            ans[i-1] += (count+1)/2;
            ans[i] += count/2;
            count = 0;
        }
    }
    count = 0;
    for(int i = N; i >= 0; i--){
        if(S[i] == 'L'){
            count += 1;
        }else{
            ans[i+1] += (count+1)/2;
            ans[i] += count/2;
            count = 0;
        }
    }
    REP(i,N) cout << ans[i] << " ";

    return 0;
}

Measured memory_bytes: 1408
N = int(input())
H = list(map(int, input().split()))

result = True
for i in range(len(H) - 1, 0, -1):
    current = H[i]
    left = H[i - 1]

    diff = current - left
    if diff == -1:
        H[i - 1] -= 1
    elif diff < -1:
        result = False
        break


answer = 'Yes' if result else 'No'

print(answer)


Measured memory_bytes: 14252
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

INF = 10**9 + 1

def precompute(N, M, data):
    data = data.reshape(-1, 3)
    A, B, C = data[:N].T
    D, E, F = data[N:].T
    X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
    Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
    A = np.searchsorted(X, A)
    B = np.searchsorted(X, B)
    D = np.searchsorted(X, D)
    C = np.searchsorted(Y, C)
    E = np.searchsorted(Y, E)
    F = np.searchsorted(Y, F)
    return A, B, C, D, E, F, X, Y

def solve(A, B, C, D, E, F, X, Y):
    H, W = len(X), len(Y)
    N = H * W
    DX = X[1:] - X[:-1]
    DY = Y[1:] - Y[:-1]

    def set_ng(A, B, C, D, E, F, H, W):
        ng = np.zeros((H * W, 4), np.bool_)

        for i in range(len(A)):
            for x in range(A[i], B[i]):
                v = x * W + C[i]
                ng[v][1] = 1
                ng[v - 1][0] = 1

        for i in range(len(D)):
            for y in range(E[i], F[i]):
                v = D[i] * W + y
                ng[v][3] = 1
                ng[v - W][2] = 1
        return ng

    ng = set_ng(A, B, C, D, E, F, H, W)
    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
    v0 = x0 * W + y0
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = np.empty(N, np.int32)
    p = 0
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return np.int64(DX[x]) * np.int64(DY[y])

    def push(x):
        nonlocal p, ret
        stack[p] = x
        visited[x] = 1
        ret += area(x)
        p += 1

    def pop():
        nonlocal p
        p -= 1
        return stack[p]

    push(v0)
    move = (1, -1, W, -W)
    while p:
        v = pop()
        for i in range(4):
            if ng[v][i]:
                continue
            w = v + move[i]
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    signature = '(i8[::1],i8[::1],i8[::1],i8[::1],i8[::1],i8[::1],i8[::1],i8[::1])'
    cc.export('solve', signature)(solve)
    cc.compile()

if sys.argv[-1] == 'ONLINE_JUDGE':
    cc_export()
    exit()

from my_module import solve

N, M = map(int, readline().split())
data = np.int64(read().split())

x = solve(*precompute(N, M, data))
if x == 0:
    print('INF')
else:
    print(x)


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

using namespace std;

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

int const INF = 1<<29;

typedef vector<int> edges;
typedef vector<edges> graph;

namespace articulation
{
  
  int const MaxNum = 100000;
  
  int prenum[MaxNum];
  int lowest[MaxNum];
  int parent[MaxNum];
  int timer;
  int V;
  
  bool visited[MaxNum];
  graph g;
  
  void dfs(int curr) {
    prenum[curr] = timer;
    lowest[curr] = timer;
    timer ++;
    
    visited[curr] = 1;
    
    rep(i, g[curr].size()) {
      int next = g[curr][i];
      if(!visited[next]) {
        parent[next] = curr;
        dfs(next);
        lowest[curr] = min(lowest[curr], lowest[next]);
      }
      else if(next != curr) {
        lowest[curr] = min(lowest[curr], prenum[next]);
      }
    }
  }
  
  vector<int> doit(int V_) {
    V = V_;
    rep(i, V) {
      visited[i] = 0;
    }
    timer = 1;
    dfs(0);
    
    vector<int> ret;
    int cnt = 0;
    rep(i, V) {
      if(i && parent[i] == 0) { cnt ++; }
      if(parent[i] > 0 &&
         lowest[i] >= prenum[parent[i]]) { ret.push_back(parent[i]); }
    }
    if(cnt >= 2) { ret.push_back(0); }
    sort(ret.begin(), ret.end());
    ret.erase(unique(ret.begin(), ret.end()), ret.end());
    return ret;
  }
  
}

int main() {
  
  using namespace articulation;
  
  int N, M; cin >> N >> M;
  g.resize(N);
  rep(i, M) {
    int a, b; cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  
  vector<int> arts = doit(N);

  rep(i, arts.size()) {
    cout << arts[i] << endl;
  }
  
  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1<<30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

//#define int long long
int md = 100000;
#define addm(X, Y) ((X) = ((X) + (Y) % md) % md)

int n;
int a[100000 + 5];
ll ans = 0;

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    rep(i, n) cin >> a[i];
    rep(i, n) {
        if (a[i] % 2 == 0) {
            ans += a[i] / 2;
        }
        else {
            ans += a[i] / 2;
            if (a[i + 1] == 0) continue;
            a[i + 1]++;
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 640
import bisect
primes = [0, 0] + [1] * 49999
for i in range(2, 224):
    if primes[i]:
        for j in range(i*i, 50001, i):
            primes[j] = 0
values = [i for i, k in enumerate(primes) if k]
while True:
    n = int(input())
    if n == 0:
        break
    I = bisect.bisect(values, n//2)
    print(len([1 for v in values[:I] if primes[n-v]]))

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

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if(a < b) {
        for(int i = a; i < b; ++i) {
            if(i == c) {
                cout << "Yes" << '\n';
                return 0;
            }
        }
    } else {
        for(int i = a; i > b; --i) {
            if(i == c) {
                cout << "Yes" << '\n';
                return 0;
            }
        }
    }
    cout << "No" << '\n';
}

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

namespace AizuOnlineJudge {
    class Program {
        static void Main() {
            Console.ReadLine();
            int min = int.MaxValue, sum = 0;
            foreach (var p in Console.ReadLine().Split().Select(int.Parse)) {
                sum += p;
                if ((p & 1) == 1) {
                    min = Math.Min(min, p);
                }
            }

            if ((sum & 1) == 1) {
                sum -= min;
            }

            Console.WriteLine(sum >> 1);
        }
    }
}



Measured memory_bytes: 18392
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define tll tuple<ll, ll, ll>
#define all(c) c.begin(),c.end()

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
 
const ll inf = 1000000001;
const ll INF = 1e18;
const ll MOD = 1000000007;

char albt[26] = {'a','b','c','d','e','f','g','h','i','j','k','l',
                 'm','n','o','p','q','r','s','t','u','v','w','x',
                 'y','z'};

//vector<ll> g[202020];
//fill(score, score+202020, 0);
//vector<ll> v;

int main(){
  ll n,m;
  cin >> n >> m;
  vll G(n);
  if (m>0){
    for(int i=0;i<m;i++){
      ll u, v;
      cin >> u >> v;
      u--; v--;
      G[u].push_back(v);
    }
  }
  
  ll s,t;
  cin >> s >> t;
  s--; t--;
  
  vll dis(n, vl(3, INF));
  queue<pll> que;
  que.push(pll(s,0));
  
  while (!que.empty()){
    ll node = que.front().first;
    ll val = que.front().second;
    que.pop();
    if (G[node].size() == 0) continue;
    for (int i=0;i<G[node].size();i++){
      ll node2 = G[node][i];
      if (dis[node2][(val+1)%3] > val+1){
        dis[node2][(val+1)%3] = val+1;
        que.push(pll(node2,val+1));
      }
    }
  }
  
  if (dis[t][0] == INF) cout << -1 << endl;
  else cout << dis[t][0]/3 << endl;

  //cout << sum << endl;
}

Measured memory_bytes: 11136
#pragma GCC optimize ("O3")
#pragma GCC target ("tune=native")
#pragma GCC target ("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i,n) for (unsigned long long i=0, i##_len=(n); i<i##_len; i++)
#define RANGE(i,is,ie) for (unsigned long long i=(is), i##_end=(ie); i<=i##_end; i++)
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end())
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0; }
template<class T> bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0; }
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q) cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) cerr << "[DEBUG] " #q ": ["; REP(dumpall_i, (q).size()) { cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", "); } cerr << "] at " __FILE__ ":" << __LINE__ << endl

// gcc拡張マクロ
#define gcd __gcd
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using  ll = long long;
using ull = unsigned long long;
using  ld = long double;
using namespace std;

// モジュール


// 処理内容
int main() {

    ll n, a, b;
    cin >> n >> a >> b;

    // p_i および番兵0, N+1
    vector<ll> p(n+2);
    p.front() = 0;
    RANGE(i, 1, n) cin >> p[i];
    p.back() = ++n;
    
    // 遷移 O(N^2)
    vector<ll> dp(n+1, LINF / 2); // dp[i] := {p_k|k<=i}が(狭義)単調増加（含番兵0）
    dp[0] = 0; // 初期状態：何もしなくても番兵は{0}で単調増加
    RANGE(i, 1, n) {
        ll term = 0;
        for (int j = i; j--; ) {
            if (p[j] < p[i]) {
                chmin(dp[i], dp[j] + term);
                term += b;
            } else {
                term += a;
            }
        }
    }

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

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

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

    cout << t << s << endl;

    return 0;
}
 
 


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define maxn 5009
#define mod 1000000007
using namespace std;
char s[maxn];
int f[maxn][maxn*2];
inline void upd(int &x,int y){x+=y;if(x>=mod)x-=mod;}
int main(){
  int n,m;
  scanf("%d%s",&n,s);
  m=strlen(s);
  f[0][0]=1;
  for(int i=0;i<=n;i++)
    for(int j=0;j<=n+m;j++)
      if(f[i][j]) upd(f[i+1][max(0,j-1)],f[i][j]),upd(f[i+1][j+1],f[i][j]*2%mod);
  long long ans=1;
  for(int i=1;i<=m;i++) ans=ans*500000004%mod;
  printf("%lld",ans*f[n][m]%mod);
  return 0;
}


Measured memory_bytes: 193664
use std::cmp;
use std::io::Read;

fn main() {
    let mut buff = String::new();
    std::io::stdin().read_to_string(&mut buff).unwrap();
    let mut iter = buff.split_whitespace();

    let a: i32 = iter.next().unwrap().parse().unwrap();
    let b: i32 = iter.next().unwrap().parse().unwrap();

    let x = a + b;
    let y = a * b;
    let z = a - b;

    println!("{}", cmp::max(x, cmp::max(y, z)));

    ()
}


Measured memory_bytes: 4352
#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 FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
//const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<typename T>
struct RMQ {
  int n, ndat = 1;
  vector<T> dat;
  RMQ(int n = 0): n(n) {
    while (ndat < n) ndat *= 2;
    dat.resize(2 * ndat - 1, inf);
  }

  void update(int k, T a) {
    k += ndat - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return inf;
    if (a <= l && r <= b) return dat[k];
    else {
      T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};

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

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) cin >> p[i];

  int ans = 1;
  RMQ<int> rminq(n), rmaxq(n);
  rep(i, n) rminq.update(i, p[i]);
  rep(i, n) rmaxq.update(i, -p[i]);
  rep(i, n - k) {
    int minp = rminq.query(i + 1, i + k, 0, 0, rminq.ndat);
    int maxp = -rmaxq.query(i + 1, i + k, 0, 0, rmaxq.ndat);
    if (p[i] < minp && maxp < p[i + k]) continue;
    ans++;
  }
  int res = 1, sum = -1;
  rep(i, n - 1) {
    if (p[i] < p[i + 1]) res++;
    else res = 1;
    if (res == k) sum++;
  }
  if (sum > 0) ans -= sum;
  cout << ans << '\n';




  
  return 0;
}

Measured memory_bytes: 5248
#include<iostream>

using namespace std;

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

	int A[101][101] = {};
	int B[101][101] = {};
	long long C[101][101] = {};
	
	for(int i=0;i<a;i++)
	for(int j=0;j<b;j++)
	cin >> A[i][j];
	
	for(int i=0;i<b;i++)
	for(int j=0;j<c;j++)
	cin >> B[i][j];
	
	for(int i=0;i<a;i++) 
		
	for(int j=0;j<c;j++) 
		
	for(int k=0;k<b;k++)
	C[i][j] += ( A[i][k] * B[k][j] );
	
	for(int i=0;i<a;i++) {
	for(int j=0;j<c;j++) {
		
	if( j != c-1 )
	cout << C[i][j] << " ";
	
	else
	cout << C[i][c-1];
	}
	cout << "\n";
}
}
	return 0;
}


Measured memory_bytes: 3248
#include <stdio.h>

#define NMAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n,M[NMAX][NMAX];

int prim(void){
  int i,j,u,minv;
  static int sum = 0;
  int d[NMAX],p[NMAX],color[NMAX];

  for(i = 0;i < n;i++){
    d[i] = (1<<21);
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while(1){
    minv = (1<<21);
    u = -1;
    for(i = 0;i < n;i++){
      if(minv > d[i] && color[i] != BLACK){
	u = i;
	minv = d[i];
      }
    }
    if(u == -1){
      break;
    }
    color[u] = BLACK;
    for(j = 0;j < n;j++){
      if((color[j] != BLACK) && (M[u][j] != (1<<21))){
	if(d[j] > M[u][j]){
	  d[j] = M[u][j];
	  p[j] = u;
	  color[j] = GRAY;
	}
      }
    }
  }

  for(i = 0;i < n;i++){
    if(p[i] != -1){
      sum += M[i][p[i]];
    }
  }

  return sum;
}


int main(void){
  int i,j,e;

  scanf("%d",&n);

  for(i = 0;i < n;i++){
    for(j = 0;j < n;j++){
      scanf("%d",&e);
      if(e == -1){
	M[i][j] = (1<<21);
      }else{
	M[i][j] = e;
      }
    }
  }

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

  return 0;
}

	    



Measured memory_bytes: 2080
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;


int main(){

    int n;
    cin >> n;

    cout << n/3 << endl;

    return 0;
}

Measured memory_bytes: 3596
N, x = map(int, input().split())
a = list(map(int, input().split()))

a_sort = sorted(a)

for i in range(N):
    x -= a_sort[i]
    if x < 0 or (x > 0 and i == N-1):
        print(i)
        break
    elif x == 0:
        print(i + 1)
        break


Measured memory_bytes: 2940
#include<bits/stdc++.h>
#include<string.h>
 
typedef long long int ll;
#define all(x) (x).begin(), (x).end()
 
using namespace std;

int nxt() {
    int x;
    cin >> x;
    return x;
}

ll nxtl(){
    ll x;
    cin>>x;
    return x;
}
 
void SieveOfEratosthenes(int n,vector <int> &primes) 
{ 
    // Create a boolean array "prime[0..n]" and initialize 
    // all entries it as true. A value in prime[i] will 
    // finally be false if i is Not a prime, else true. 
    bool prime[n+1]; 
    memset(prime, true, sizeof(prime)); 
  
    for (int p=2; p*p<=n; p++) 
    { 
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) 
        { 
            // Update all multiples of p greater than or  
            // equal to the square of it 
            // numbers which are multiple of p and are 
            // less than p^2 are already been marked.  
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
  
    // Print all prime numbers 
    for (int p=2; p<=n; p++) 
       if (prime[p]) 
          primes.push_back(p);
} 
 
ll max(ll a,ll b)
{
    if(a>b)
        return a;
    return b;
}
 
ll power(ll x, ll y,ll mod) 
{ 
    ll temp; 
    if( y == 0) 
        return 1; 
    temp = power(x, y/2,mod); 
    if (y%2 == 0) 
        return (temp*temp)%mod; 
    else
        return (((x*temp)%mod)*temp)%mod; 
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll x=nxtl(),y=nxtl(),a=nxtl(),b=nxtl(),c=nxtl();
    vector <ll> red(a),green(b),both(c);
    generate(all(red),nxtl);
    generate(all(green),nxtl);
    generate(all(both),nxtl);
    sort(all(red),greater<int>());
    sort(all(green),greater<int>());
    sort(all(both),greater<int>());
    int i=0,j=0,k=0;
    ll test=0;
    int x1=0,y1=0;
    while(x1<x&&y1<y&&k<c)
    {
        if(both[k]>=red[i]&&both[k]>=green[j])
        {
            if(red[i+x-x1-1]<green[j+y-y1-1])
            {
                test+=both[k++];
                x1++;
            }
            else
            {
                test+=both[k++];
                y1++;
            }
        }
        else
        {
            if(red[i]>green[j])
            {
                test+=red[i++];
                x1++;
            }
            else
            {
                test+=green[j++];
                y1++;
            }
        }
    }

    while(x1<x&&k<c)
    {
        if(red[i]<both[k])
        {
            test+=both[k++];
            x1++;
        }
        else
        {
            test+=red[i++];
            x1++;
        }
    }

    while(y1<y&&k<c)
    {
        if(green[j]<both[k])
        {
            test+=both[k++];
            y1++;
        }
        else
        {
            test+=green[j++];
            y1++;
        }
    }

    while(x1<x)
    {
        test+=red[i++];
        x1++;
    }

    while(y1<y)
    {
        test+=green[j++];
        y1++;
    }

    cout<<test<<endl;


    
    
    return 0;   
}


 
 


Measured memory_bytes: 2560
#https://atcoder.jp/contests/m-solutions2019/submissions/5730635



#x = [1,2,3]
#while x:
#    if x : 
#        print(x)
#        x.pop()


n = int(input())

abn = [list(map(int,input().split())) for _ in range(n-1)]

cn = list( map(int, input().split()) )
cn.sort(reverse = True)


links = [set() for _ in range(n)]

for abi in abn:
    a, b = abi

    a -= 1; b-=1
    links[a].add(b)
    links[b].add(a)



# points of vertices
pv = [0]*n
ans = 0


#リンクを嘗めてsizeが1のもののindexだけ選ぶ
leaves = { i for i, l in enumerate(links) if len(l) == 1 }

# print(*abn)
# print(*cn)
# print(*leaves)
while leaves:
    
    index = leaves.pop()
    cmax = cn.pop()#popなので最小値から利用していく=>大きな値が残っていく
    
    pv[index] = cmax

    if not cn: # 最後のcmax (= 与えられた数列cnの最大値 )を加算する前に抜ける
        break

    ans += cmax
    for ci in links[index]:
        lu = links[ci]
        lu.remove(index)
        if len(lu) == 1:
            leaves.add(ci)

print(ans)
print(*pv)


Measured memory_bytes: 9976
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using static System.Console;
using static System.Math;

namespace Atcoder
{
    class Program
    {
        static StreamWriter sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false };
        static void Main(string[] args)
        {
            try
            {
                SetOut(sw);
                Solve.Answer();
                Out.Flush();
            }
            catch (Exception ex)
            {
                WriteLine(ex.ToString());
                Out.Flush();
            }
        }
    }

    public class Solve
    {
        public static void Answer()
        {
            var n = IO.RInt;
            var ab = IO.RSInt;
            var p = IO.RSInt;

            var c1 = p.Where(x => x <= ab[0]).Count();
            var c2 = p.Where(x => x > ab[0] && x <= ab[1]).Count();
            var c3 = p.Where(x => x > ab[1]).Count();

            WriteLine(Min(c1, Min(c2, c3)));
        }
    }

    public class IO
    {
        #region Input
        public static int RInt => int.Parse(ReadLine());
        public static long RLong => long.Parse(ReadLine());
        public static double RDouble => double.Parse(ReadLine());

        public static string[] RSString => ReadLine().Split();
        public static int[] RSInt => ReadLine().Split().Select(s => int.Parse(s)).ToArray();
        public static long[] RSLong => ReadLine().Split().Select(s => long.Parse(s)).ToArray();
        public static double[] RSDouble => ReadLine().Split().Select(s => double.Parse(s)).ToArray();
        #endregion

        #region Output
        public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list));
        public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No");
        public static void WriteLineYES_or_NO(bool flag) => WriteLine(flag ? "YES" : "NO");
        #endregion
    }

    public class Mod
    {
        #region
        private readonly static long M = 1000000007;

        public static long Add(long a, long b) => (a * b) % M;
        public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M;
        public static long Multi(long a, long b) => (a * b) % M;
        #endregion
    }
}

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

using namespace std;

#define int long long
#define ar array
#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
const int M = 1e9 + 7;

template<class T>
bool umin(T &a, const T &b) {
    return b < a ? a = b, 1 : 0;
}

template<class T>
bool umax(T &a, const T &b) {
    return a < b ? a = b, 1 : 0;
}

template<class A>
void read(vt<A> &v);

template<class A, size_t S>
void read(ar<A, S> &a);

template<class T>
void read(T &x) {
    cin >> x;
}

void read(double &d) {
    string t;
    read(t);
    d = stod(t);
}

void read(long double &d) {
    string t;
    read(t);
    d = stold(t);
}

template<class H, class... T>
void read(H &h, T &... t) {
    read(h);
    read(t...);
}

template<class A>
void read(vt<A> &x) {
    EACH(a, x)read(a);
}

template<class A, size_t S>
void read(array<A, S> &x) {
    EACH(a, x)read(a);
}

string to_string(char c) {
    return string(1, c);
}

string to_string(bool b) {
    return b ? "true" : "false";
}

string to_string(const char *s) {
    return string(s);
}

string to_string(string s) {
    return s;
}

string to_string(vt<bool> v) {
    string res;
    FOR(sz(v))res += char('0' + v[i]);
    return res;
}

template<size_t S>
string to_string(bitset<S> b) {
    string res;
    FOR(S)res += char('0' + b[i]);
    return res;
}

template<class T>
string to_string(T v) {
    bool f = 1;
    string res;
    EACH(x, v) {
        if (!f)
            res += ' ';
        f = 0;
        res += to_string(x);
    }
    return res;
}

template<class A>
void write(A x) {
    cout << to_string(x);
}

template<class H, class... T>
void write(const H &h, const T &... t) {
    write(h);
    write(t...);
}

void print() {
    write("\n");
}

template<class H, class... T>
void print(const H &h, const T &... t) {
    write(h);
    if (sizeof...(t))
        write(' ');
    print(t...);
}

template<class T, class U>
void vti(vt<T> &v, U x, size_t n) {
    v = vt<T>(n, x);
}

template<class T, class U>
void vti(vt<T> &v, U x, size_t n, size_t m...) {
    v = vt<T>(n);
    EACH(a, v)vti(a, x, m);
}


int modpow(int x, int p) {
    if (!p) return 1;
    int val = modpow(x, p / 2);
    val = (val * val) % M;
    if (p & 1) val = (val * x) % M;
    return val;
}

int inverse(int x) {
    return modpow(x, M - 2);
}

void solve() {
    int n, k;
    read(n, k);
    
    int ans = 0;
    FOR(n) {
        int x;
        read(x);
        ans += 2 * min(k - x, x);
    }
    
    print(ans);
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int tt = 1;
    //read(tt);
    FOR(tt) {
        //write("Case #", i + 1, ": ");
        solve();
        
    }
    
}


Measured memory_bytes: 3692
#include <bits/stdc++.h>
#define rep(i, m, n) for(int i = m; i < (n); i++)
#define rrep(i,m,n) for(int i = m; i >= n; i--)
#define print(x) cout << (x) << endl;
#define print2(x,y) cout << (x) << " " << (y) << endl;
#define printa(x,n) for(int i = 0; i < n; i++){ cout << (x[i]) << " ";} cout << endl;
#define printa2(x,m,n) for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ cout << x[i][j] << " ";} cout << endl;}
#define printp(x,n) for(int i = 0; i < n; i++){ cout << "(" << x[i].first << ", " << x[i].second << ") "; } cout << endl;
#define INF (1e18)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
typedef struct{
    ll to;
    ll cost;
} edge;
typedef pair<ll, ll> lpair;
ll dp[110][100010] = {};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N,K;
    cin >> N >> K;
    ll a[110];
    rep(i,0,N){
        cin >> a[i];
    }
    dp[0][0] = 1;
    rep(i,0,N){
        ll sum[100010] = {};
        rep(j,0,100001){
            sum[j+1] = (sum[j]+dp[i][j]) % MOD;
            sum[j+1] %= MOD;
        }
        rep(j,0,K+1){
            ll min_val = max(0LL,j-a[i]);
            dp[i+1][j] += (sum[j+1] - sum[min_val] + MOD) % MOD;
            dp[i+1][j] %= MOD;
        }
    }
    print(dp[N][K]);


}


Measured memory_bytes: 80128