akhauriyash/Code-Regression huggingface dataset:
import sys
sys.setrecursionlimit(10**7)

N, M = list(map(int, input().split()))
edge = {n: [] for n in range(1, N + 1)}

for _ in range(M):
    a, b = list(map(int, input().split()))
    edge[a].append(b)
    edge[b].append(a)

colors = [0 for i in range(N + 1)]
def dfs(v, color):
    colors[v] = color
    for t in edge[v]:
        if colors[t] == color:
            return False
        if colors[t] == 0 and not dfs(t, -color):
            return False
    return True

def is_bipartite():
    return dfs(1, 1)

if is_bipartite():
    # print(colors)
    cB, cW = 0, 0
    for c in colors[1:]:
        if c == 1:
            cB += 1
        else:
            cW += 1
    print(cB * cW - M)
else:
    print(N * (N - 1) // 2 - M)

Measured memory_bytes: 160996
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 1000000000000000000LL
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144*4
#define REP(a,b) for(long long a = 0;a < b;++a)
int grid[2000][2000];
int tmp[2000][2000];
vector<long long> calc(vector<long long> a, vector<long long> b) {
	vector<long long> c;
	REP(i, 2) {
		REP(q, 2) {
			long long sum = 0;
			REP(j, 2) {
				sum += a[q + j * 2] * b[i * 2 + j];
			}
			c.push_back(sum% MAX_MOD);
		}
	}
	return c;
}
int main() {
	long long h, w, k;
	cin >> h >> w >> k;
	long long cnt = 0;
	REP(i, h) {
		string s;
		cin >> s;
		REP(q, w) {
			if (s[q] == '#') {
				grid[i][q] = 1;
				cnt++;
			}
		}
	}
	int bad = 0;
	REP(q, w) {
		if (grid[0][q] == 1 && grid[h - 1][q] == 1) {
			bad = 1;
			break;
		}
	}
	if (bad == 1) {
		REP(i, h) {
			REP(q, w) {
				tmp[q][i] = grid[i][q];
			}
		}
		swap(h, w);
		REP(i, h) {
			REP(q, w) {
				grid[i][q] = tmp[i][q];
			}
		}
	}
	REP(q, w) {
		if (grid[0][q] == 1 && grid[h - 1][q] == 1) {
			cout << 1 << endl;
			return 0;
		}
	}
	vector<long long> tmp;
	tmp.push_back(cnt);
	tmp.push_back(MAX_MOD);
	tmp.push_back(0);
	tmp.push_back(0);
	REP(i, h) {
		REP(q, w) {
			if (q != 0 && grid[i][q] == 1 && grid[i][q - 1] == 1) {
				tmp[1]--;
			}
		}
		if (grid[i][0] == 1 && grid[i][w - 1]) {
			tmp[3]++;
		}
	}
	if (tmp[3] == 0) tmp[1] = 0;
	vector<long long> ans;
	ans.push_back(1);
	ans.push_back(0);
	ans.push_back(0);
	ans.push_back(1);
	k--;
	while (k != 0) {
		if (k % 2) {
			ans = calc(ans, tmp);
		}
		tmp = calc(tmp, tmp);
		k /= 2;
	}
	cout << (ans[0] + ans[1]) % MAX_MOD << endl;
}

Measured memory_bytes: 18688
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; }

int main() {
	int N; int A; int B;
	while (~scanf("%d%d%d", &N, &A, &B)) {
		ll ans;
		if (A > B || (N == 1 && A != B)) {
			ans = 0;
		} else {
			ll L = (ll)A * (N - 1) + B;
			ll U = (ll)B * (N - 1) + A;
			ans = U + 1 - L;
		}
		printf("%lld\n", ans);
	}
	return 0;
}


Measured memory_bytes: 256
#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 eb emplace_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;
}

void run(){
    int n,m;
    cin>>n>>m;
    vi h(n);
    rep(i,n)cin>>h[i];
    bool f[n];
    rep(i,n)f[i]=true;
    rep(i,m){
        int a,b;
        cin>>a>>b;
        a--,b--;
        f[a]&=h[a]>h[b];
        f[b]&=h[b]>h[a];
    }
    int ans=0;
    rep(i,n)ans+=f[i];
    cout<<ans<<endl;
}

Measured memory_bytes: 3996
//#include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
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 (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vector<int> a(n);
    rep(i,n)cin>>a[i];
    sort(ALL(a));
    cout<<a.back()-a[0]<<"\n";
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>
#include<cstring>
#include<bitset>
#include<cassert>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++)
#define all(x) (x).begin(),(x).end()

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;



void print(vector<vector<int>> a){
	for (int i = 0; i < a.size(); i++)
	{
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(vector<long long int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(set<int> a){
	for (auto x:a)
		cout << x << " ";
	cout << endl;
}

//繰り返し2乗法を用いたべき乗.x^nを返す.
long long int pow_(long long int x,long long int n,ll mod){
	long long int res = 1;
	while (n)
	{
		if (n&1){
			res *= x;
			res %= mod;
		}
		n >>= 1;
		x = x * x;
		x %= mod;
	}
	res %= mod;
	return res;
}

int rec(ll n,int c){

	if (n == 0)
	{
		return c;
	}
	ll t = n;
	int pc = 0;
	while (t)
	{
		if(t&1)
			pc++;
		t = t >> 1;
	}

	return rec(n % pc, c + 1);
}

int main()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	string rs = s;
	reverse(all(rs));

	int opc = count(all(s),'1');

	vector<ll> bekia(n,0);
	vector<ll> bekib(n,0);
	rep(i, n)
	{
		bekia[i] = pow_(2, i, opc + 1);
		if (opc > 1)
			bekib[i] = pow_(2, i, opc - 1);
	}

	ll oa = 0;
	ll ob = 0;

	rep(i,n){
		if (opc == 0)
			break;
		if (rs[i] == '1')
		{
			ob += bekib[i];
			if (opc > 1)
				ob %= opc - 1;
			oa += bekia[i];
			oa %= opc + 1;
		}
	}

	vector<ll> res(n, 0);
	rep(i, n){
		ll temp = -1;
		if(rs[i] == '1'){
			if (opc > 1)
				temp = (ob + (opc - 1) - bekib[i]) % (opc - 1);
		} else {
			temp = (oa + bekia[i]) % (opc + 1);
		}
		res[n - i - 1] = temp;
	}

	rep(i, n) cout << (res[i]!=-1?rec(res[i], 1):0) << endl;
	

	return 0;
}

Measured memory_bytes: 8388
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MOD 1000000007
#define MAX 510000
#define Rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for(ll i = 1; i <= (ll)(n); i++)

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

//cout << fixed << setprecision(10);//
//最小公倍数//
ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd(y%x, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

//べき//
ll squ(ll n, ll p, ll m){
	if(p==0) return 1;
	if(p%2==0){
		ll t=squ(n, p/2, m);
		return t*t%m;
	}
	return n*squ(n,p-1,m);
	
}


//逆元mod(mを法とするaの逆元)//
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//Cmonp//
ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
ll COM(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//nが大

ll com(ll n,ll m){
    if(n<m || n<=0 ||m<0){
		return 0;
	}
	if( m==0 || n==m){
		return 1;
	}
	
	ll k=1;
	for(ll i=1;i<=m;i++){
       k*=(n-i+1); 
	   k%=MOD;
	   k*=modinv(i,MOD);
	   k%=MOD;
	}

	return k;
}


////////////////////////////////////////////////////////////////////


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

int human[n+1];

int say[n+1][16];
for(int i=1;i<=n;i++){
for(int j=1;j<=15;j++){

		say[i][j]=-1;
}

}

for(int i=1;i<=n;i++){
int k;
cin>>k;
for(int l=1;l<=k;l++){
	int u,v;
	cin>>u>>v;
	say[i][u]=v;
}
}





int maxv=0;

for(int bit=0;bit<(1<<n); ++bit){
	int cnt=0;
for(int i=0;i<n;i++){
	if(bit&(1<<i)){
		human[i+1]=1;
		cnt++;
	}
	else{human[i+1]=0;}
}





int cl=1;
int k;
cin>>k;
for(int i=1;i<=n;i++){
	for(int j=1;j<=15;j++){
		int v=say[i][j];
if(v!=-1 && human[i]==1 && human[j]!=v){
cl=0;
}

	}

}



if(cl==1){
maxv=max(maxv,cnt);}

}


cout<<maxv<<endl;
return 0;
}




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

int main() {
    int N;
    int x[100];
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> x[i];
    }

    sort(x, x + N, greater<>());
    long long alice = 0, bob = 0;
    for (int i = 0; i < N; i++) {
        if (i % 2 == 0) {
            alice += x[i];
        } else {
            bob += x[i];
        }
    }
    cout << alice - bob << endl;

    return 0;
}


Measured memory_bytes: 256
// Example program
#include <iostream>
#include <string>
#include <bits/stdc++.h>

using namespace std;
int main()
{
    
vector<char> prime (100005, true);
prime[0] = prime[1] = false;
for (int i=2; i<=100005; ++i)
    {if (prime[i])
        {if (i * 1ll * i <= 100005)
            {for (int j=i*i; j<=100005; j+=i)
                {prime[j] = false;
                }
            }
        }
    }
    
   
	
	long long int like[100005];
	like[0] = 0;
	
	for (long long int i = 1;i<100005;i++)
	{
	    if (i%2 == 0)
	    {
	        like[i] = like[i-1];
	    }
	    else
	    {
	    if (prime[i] == true && prime[(i+1)/2] == true)
	    {
	        like[i] = like[i-1] + 1;
	    }
	    else
	    like[i] = like[i-1];
	    }
	}
	
	long long int N;
	cin >> N;
	while(N--)
	{
	long long int X,Y;
	cin >> X >> Y;
	
	cout << like[Y] - like[X-1] << "\n";
	}
}


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

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    string b;
    cin >> b;
  	//四種類の塩基 A, C, G, T が存在し、A と T、C と G がそれぞれ対
  	if(b == "A")
    {
      cout << "T" << endl;
    }
    else if(b == "T")
    {
      cout << "A" << endl;
    }
  	else if(b == "C")
    {
      cout << "G" << endl;
    }
    else if(b == "G")
    {
      cout << "C" << endl;
    }
    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
  
using namespace std;
  
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)((x).size()))
  
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long int64;
typedef unsigned int uint;
typedef unsigned long long uint64;
  
#define gi(x) ((x) = F())
#define gii(x, y) (gi(x), gi(y))
#define giii(x, y, z) (gii(x, y), gi(z))
  
int F()
{
    char ch;
    int x, a;
    while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-');
    if (ch == '-') ch = getchar(), a = -1;
    else a = 1;
    x = ch - '0';
    while (ch = getchar(), ch >= '0' && ch <= '9')
        x = (x << 1) + (x << 3) + ch - '0';
    return a * x;
}
  
int n, a[2010], x, mn[2010][2010];
  
long long ans;
  
int main()
{
    gii(n, x);
    for (int i = 1; i <= n; ++i) gi(a[i]), ans += a[i];
    for (int i = 1; i <= n; ++i)
    {
        mn[i][i] = a[i];
        for (int j = i + 1; j <= n; ++j)
            mn[i][j] = min(mn[i][j - 1], a[j]);
    }
    for (int i = 0; i <= n; ++i)
    {
        long long ret = 1LL * x * i;
        for (int j = 1; j <= n; ++j)
        {
            int k = j - i;
            if (k < 1) k += n, ret += min(mn[k][n], mn[1][j]);
            else ret += mn[k][j];
        }
        ans = min(ans, ret);
    }
    printf("%lld\n", ans);
    return 0;
}

Measured memory_bytes: 15360
#include<stdio.h>

int main(void){

    int a,b;

    scanf("%d %d",&a,&b);

    if(a < b){
        printf("a < b\n");
    }else if(a > b){
        printf("a > b\n");
    }else{
        printf("a == b\n");
    }


    return 0;

}

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

int main(){
  int N;
  vector<pair<pair<string,int>,int>> SP(110);
  
  cin >> N;
  for(int i=0; i<N; i++){
    cin >> SP[i].first.first >> SP[i].first.second;
    SP[i].first.second *= -1;
    SP[i].second = i;
  }

  sort(SP.begin(), SP.begin()+N);

  for(int i=0; i<N; i++){
    cout << SP[i].second + 1 << endl;
  }
}

Measured memory_bytes: 764
object Main {
  def main(args:Array[String]) {
    val N = readLine().toInt
    val A = readLine().split(' ').map(_.toInt)
    val ans = new Array[Int](N)
    for(i <- 0 until N) {
      ans(A(i) - 1) = i + 1
    }
    ans.map(a => print(a + " "))
  }
}


Measured memory_bytes: 51604
#  --*-coding:utf-8-*--


# 'A' ならば aの個数を +1
# 'BC'ならば、変換の個数 に +a
# 'BB'なら aを0
# 'C' なら　aを0

def f(S):
    a = 0
    n = 0
    b = False

    for c in S:
        if c == 'A':
            if b:
                a = 1
                b = False
            else:
                a += 1

        elif c == 'B':
            if b:
                a = 0
            else:
                b = True
                
        else:
            if b:
                n += a
                b = False
            else:
                a = 0
                
    return n


def main():
    S = input()

    print(f(S))
 


if __name__ == '__main__':
    main()


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

#define Mset(s, n)      memset(s, n, sizeof(s))
#define Rp0(i, m)       for(int i = 0; i < m; ++i)
#define Rpd(i, m, l)    for(int i = l; i >= m; --i)
#define Rp1(i ,m)       for(int i = 1; i <= m; i++)
#define All(a)          (a).begin(), (a).end()
#define Lwb             lower_bound
#define Upb             upper_bound
#define Sz(a)           int((a).size())
#define u_map           unordered_map
#define fi              first
#define se              second
#define pb              push_back
#define pf              push_front
#define is              insert
#define ooi             INT_MAX
#define ool             LLONG_MAX
#define ll              long long
#define ull             unsigned ll
#define ld              long double
#define pii             pair<int, int>
#define vi              vector<int>
#define vpii            vector<pii>

const ll MOD = (ll) 1e9 + 7;
const ld PI = acos((ld) -1);
inline int Get_bit(int x, int k){return (x >> (k - 1)) & 1;}
inline ll Gcd(ll a, ll b) {ll r; while(b){ r = a % b; a = b; b = r;} return a;}
inline ll Lcm(ll a, ll b) {return a / Gcd(a, b) * b;}
inline ll Pow(ll n, ll k) {ll r = 1; for(; k; k >>= 1, n = n * n % MOD){if(k & 1) r = r * n % MOD;} return r;}
const int base = 311;


const int N = 2e5 + 10;


ll n, m, k, ans, a[N];

void Solve(){
    cin >> n >> m;
    int cnt = 1, res = 0;
    while(cnt < m){
        cnt -= 1;
        cnt += n;
        res ++;
    }
    cout << res;
}


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(10);
    Solve();
    return 0;
}


Measured memory_bytes: 256
from collections import deque


def main():
    H, W = list(map(int, input().split()))
    S = [input() for _ in range(H)]
    visited = [[0] * W for _ in range(H)]
    ans = 0
    for h in range(H):
        for w in range(W):
            if S[h][w] == '#' or visited[h][w] == 1:
                continue
            n_white, n_black = 0, 0
            que = deque()
            que.append((h, w))
            while len(que) > 0:
                ch, cw = que.pop()
                if visited[ch][cw] == 1:
                    continue
                visited[ch][cw] = 1
                if S[ch][cw] == '.':
                    n_white += 1
                else:
                    n_black += 1
                for dh, dw in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                    next_h, next_w = ch + dh, cw + dw
                    if 0 <= next_h < H and 0 <= next_w < W and \
                        S[ch][cw] != S[next_h][next_w] and \
                        visited[next_h][next_w] == 0:
                        que.append((next_h, next_w))
            ans += n_white * n_black
    print(ans)


if __name__ == '__main__':
    main()

Measured memory_bytes: 25692
#include <bits/stdc++.h>
const int MAX = 2e5 + 10;
typedef long long i64;
using namespace std;
const double EPS = 1e-6;

int ar[MAX];
vector<int> ars[MAX];
const int MOD = 1e9 + 7;
#define MUL(a, b) (((i64)(a) * (b)) % MOD)
int powmod(int a, int n){
	int b = 1;
	while(n > 0){
		if(n & 1)b = MUL(b, a);
		a = MUL(a, a);
		n >>= 1;

	}
	return b;
}
int fac[MAX];
int ifac[MAX];
int bin(int n,int a){
	if(n < a)return 0;
	return MUL(fac[n], MUL(ifac[a], ifac[n - a]));

}

int main() {

    #ifdef LOCAL_DEBUG
    freopen ("data.in", "r", stdin );
    //freopen ("data.out", "w", stdout );
    #endif
    ios_base::sync_with_stdio(0); cin.tie(0);
    #define endl '\n'
    int n, m; cin >> n >> m;

    i64 tot = 0;
    for(int i = 0; i < n; i++){
    	cin >> ar[i];
    	ars[ar[i]].push_back(i);
    	if(i > 0){
    		if(ar[i] > ar[i - 1])tot += ar[i] - ar[i - 1];
    		else tot += ar[i] + m - ar[i - 1];
    	}
    }
    i64 ts = 0, si = 0;
    for(int i = 0; i < n - 1; i++){
    	if(ar[i] < ar[i + 1]){
    		// never occurs
    		if(ar[i] < 1 and ar[i + 1] <= 1){
    			si++;
    			ts += 1 - ar[i];
    		}
    	}
    	else{
    		if((ar[i] < 1 and 1 <= ar[i + 1] + m)){
    			// never
    			si++;
    			ts += 1 - ar[i];
    		}
    		else if(ar[i] < 1+m and 1+m <= ar[i + 1] + m){
    			si++;
				ts += m + 1 - ar[i];
    		}
    	}
    }

    //cout << tot << " " << ts << " " << si << endl;
    i64 ans = tot;
    if(ans > tot - ts+si)ans = tot - ts+si;

    for(int i = 2; i <= m; i++){
    	for(auto v: ars[i - 1]){
    		if(v > 0){
    			if(ar[v] > ar[v - 1])ts -= ar[v] - ar[v - 1];
    			else ts -= ar[v] + m - ar[v - 1];
    		}
    		else{
    			//ts += 1;
    			si++;
    		}
    	}
    	if(ar[n - 1] == i - 1)si--;

    	ts += si;
    	if(ans > tot - ts+si)ans = tot - ts+si;
    	//cout << tot << " " << ts << " " << si << endl;
    }
    cout << ans << endl;



}



Measured memory_bytes: 7680
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	    // for getting input from input.txt
	    freopen("input.txt", "r", stdin);
	    // for writing output to output.txt
	    freopen("output.txt", "w", stdout);
	#endif
   	ll n;
   	cin>>n;
   	ll a[n+1];
   	vector<ll>adj[n+1];
   	for(ll i=2;i<=n;i++)
   	{
   		cin>>a[i];
   		adj[a[i]].push_back(i);
   	}
   	for(ll i=1;i<=n;i++)
   		cout<<adj[i].size()<<endl;
}

Measured memory_bytes: 16128
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Design;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Schema;
using static System.Console;

namespace ConsoleApp2
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<int> Slist() { return new List<int>(ReadLine().Split().Select(int.Parse)); }
        static void Main(string[] args)
        {
            var N = Sarray()[0];
            var dic = new Dictionary<string, int>()
            {
                {"AC",0 },
                {"WA",0 },
                {"TLE",0 },
                {"RE",0 },
            };
            for(var i=0;i<N;i++)
            {
                var tmp = ReadLine();
                    dic[tmp]++;
            }
            WriteLine($@"AC x {dic["AC"]}
WA x {dic["WA"]}
TLE x {dic["TLE"]}
RE x {dic["RE"]}");
            
        }
    }
}

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

int main() {
  int N, M, A, B, C, D, E, F, G, H, S;
  int count = 0;
  cin >> N >> M;
  for(int i = N; i < M+1; i++){
     E = i/10000;//1
     A = i%10000;//1009
     G = A/1000;//1
     B = A%1000;//009
     H = B/100;//0
     C = B%100;//09
     S = C/10;//0
     D = C%10;//9
   
     F = D*10000 + S*1000 + H*100 + G*10 + E;
    if(i == F){
       count++;
   }

  }
  cout << count << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <limits>
#include <random>
#include <utility>
#include <vector>

namespace procon {
  class UnionFind {
  private:
    struct nodeinfo {
      int par;
      int rank;
      nodeinfo(int par) : par(par), rank(0) {}
    };
    std::vector<nodeinfo> node;

  public:
    UnionFind(int n) : node() {
      node.reserve(n);
      for (int i = 0; i < n; ++i) {
	node.push_back(nodeinfo(i));
      }
    }
    int root(int x) {
      if (node[x].par == x) {
	return x;
      }
      return node[x].par = root(node[x].par);
    }
    void unite(int x, int y) {
      x = root(x);
      y = root(y);
      if (x == y) {
	return;
      }
      if (node[x].rank < node[y].rank) {
	node[x].par = y;
      } else {
	node[y].par = x;
	if (node[x].rank == node[y].rank) {
	  ++node[x].rank;
	}
      }
    }
    bool is_same_set(int x, int y) { return root(x) == root(y); }
  };
}
  
int main(void) {
  int size, n_query;
  std::cin.read((char *)&size, sizeof(int));
  std::cin.read((char *)&n_query, sizeof(int));

  std::vector<uint64_t> query(n_query);
  std::cin.read((char *)query.data(), sizeof(uint64_t) * n_query);

  const int mask = (1 << 30) - 1;

  uint64_t res = 123456789;
  procon::UnionFind uf(size);
  for (uint64_t q : query) {
    const int com = q >> 60;
    const int x = (q >> 30) & mask;
    const int y = (q >> 0) & mask;
    assert(0 <= x && x < size);
    assert(0 <= y && y < size);
    if (com) {
      res = res * 17 + (uf.is_same_set(x, y) ? 1 : 0);
    } else {
      uf.unite(x, y);
    }
  }
  std::cout << res << std::endl;
  return 0;
}



Measured memory_bytes: 88808
import java.util.Scanner;
public class Main
{
	static int field[][];
	static int which[][];
	static int white,black,cnt;
	static int W,H;
	static int dx[]={0,1,-1,0};
	static int dy[]={1,0,0,-1};
	public static void main(String[] args) 
	{
		Scanner in=new Scanner(System.in);
		for(;;)
		{
			W=in.nextInt();
			H=in.nextInt();
			if((W|H)==0)
				return;
			field=new int[H][W];
			which=new int[H][W];
			for(int i=0;i<H;i++)
			{
				String st=in.next();
				for(int j=0;j<W;j++)
				{
					if(st.charAt(j)=='.')
						field[i][j]=0;
					else if(st.charAt(j)=='W')
						field[i][j]=1;
					else if(st.charAt(j)=='B')
						field[i][j]=2;
				}
			}
			black=0;
			white=0;
			for(int i=0;i<H;i++)
			{
				for(int j=0;j<W;j++)
				{
					if(field[i][j]==1)//白で未訪問かつ白色なら
						dfs(i,j,"w",0);
					if(field[i][j]==2)//黒で未訪問かつ黒色なら
						dfs(i,j,"b",0);
				}
			}
			for(int i=0;i<H;i++)
			{
				for(int j=0;j<W;j++)
				{
					if(which[i][j]==1)
						white++;
					else if(which[i][j]==2)
						black++;
				}
			}
			System.out.println(black+" "+white);
		}	
	}
	public static void dfs(int h,int w,String s,int n)
	{
		if(n!=0)
		{
			if(s.equals("w"))
			{
				field[h][w]=3;//白で訪問済み
				which[h][w]+=1;//白なら1足す
			}
			else if(s.equals("b"))
			{
				field[h][w]=4;//黒で訪問済み
				which[h][w]+=2;//黒なら2足す
			}
		}
		for(int i=0;i<4;i++)
		{
			int nh=h+dy[i];
			int nw=w+dx[i];
			if(s.equals("w"))
				if(nh>=0&&nh<H&&nw>=0&&nw<W&&(field[nh][nw]==0||field[nh][nw]==4))//白で未訪問なら
					dfs(nh,nw,s,n+1);
			if(s.equals("b"))
				if(nh>=0&&nh<H&&nw>=0&&nw<W&&(field[nh][nw]==0||field[nh][nw]==3))//黒で未訪問なら
					dfs(nh,nw,s,n+1);
		}
	}
}

Measured memory_bytes: 46700
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>
#include <random>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod  1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

int main(){
  int A, B, C;
  cin >> A >> B >> C;
  if(A + B >= C) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 764
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _20170410_AOJ
{
    class ALDS1_10C
    {
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int q = int.Parse(line);
                for(int i=0;i<q;i++)
                {
                    string x = Console.ReadLine();
                    string y = Console.ReadLine();
                    int len = GetLCSLen(x.ToCharArray(), y.ToCharArray());
                    Console.WriteLine(len);
                }
            }
        }

        static int GetLCSLen(char[] s1, char[] s2)
        {
            int[,] matrix = new int[s2.Length + 1, s1.Length + 1];
            for(int y = 0; y<s2.Length;y++)
            {
                for(int x=0;x<s1.Length;x++)
                {
                    if (s1[x] == s2[y])
                    {
                        matrix[y + 1, x + 1] = matrix[y, x] + 1;
                    }
                    else
                    {
                        matrix[y + 1, x + 1] = Math.Max(matrix[y + 1, x], matrix[y, x + 1]);
                    }
                }
            }

            return matrix[s2.Length, s1.Length];
        }
    }
}



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


int main() {
int N;
  cin >>N;
  vec B(N-1);
  for(int i=0;i<N-1;i++) cin >> B[i];
  int ans =0;
  for(int i=0;i<N;i++){
  if(i ==0) ans += B[0];
    else if (i == N-1) ans += B[N-2];
    else ans += min(B[i-1],B[i]);
  }
  cout <<ans <<endl;
}
  


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

int ReturnDigitsSum(int n){
  int sum = 0;
  while (n > 0){
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

int main () {
  int N, A, B;
  cin >> N >> A >> B;
  
  int res = 0;
  for (int i = 0; i < N; i++) {
    int sum = ReturnDigitsSum(i+1);
    if (A <= sum && sum <= B) {
      res += (i+1);
    }
  }  
  cout << res << endl;
}


Measured memory_bytes: 3564
import os
import sys
import numpy as np

# >>> binary indexed tree >>>
# 必要な要素数+1 の長さの ndarray の 1 要素目以降を使う

def bit_sum(bit, i):  # [bit_sum, "i8(i8[:],i8)"],
    # (0, i]
    res = 0
    while i:
        res += bit[i]
        i -= i & -i
    return res

def bit_add(bit, i, val):  # [bit_add, "void(i8[:],i8,i8)"],
    n = len(bit)
    while i < n:
        bit[i] += val
        i += i & -i

# <<< binary indexed tree <<<


def inversion_number(arr):  # [inversion_number, "i8(f8[:])"],
    # 転倒数
    n = len(arr)
    arr = np.argsort(arr) + 1
    bit = np.zeros(n+1, dtype=np.int64)
    res = n * (n-1) >> 1
    for val in arr:
        res -= bit_sum(bit, val)
        bit_add(bit, val, 1)
    return res


# >>> numba compile >>

numba_config = [
    [bit_sum, "i8(i8[:],i8)"],
    [bit_add, "void(i8[:],i8,i8)"],
    [inversion_number, "i8(f8[:])"],
]
if sys.argv[-1] == "ONLINE_JUDGE":
    from numba import njit
    from numba.pycc import CC
    cc = CC("my_module")
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature)(func)
        cc.export(func.__name__, signature)(func)
    cc.compile()
    exit()
elif os.name == "posix":
    exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
else:
    from numba import njit
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature, cache=True)(func)
    print("compiled", file=sys.stderr)

# <<< numba compile <<<


N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N)]
A, B, C = zip(*ABC)
th = N*(N-1)//2 // 2 + 1
def solve(A, B, C):
    # y = (-Ax+C) / B
    if N < 100:
        ok = -1e10
        ng = 1e10
        n_iteration = 70
    else:
        ok = -1e4
        ng = 1e4
        n_iteration = 46
    A, B, C = zip(*sorted(zip(A, B, C), key=lambda x: -x[0]/x[1]))
    A = np.array(A)
    B = np.array(B)
    C = np.array(C)
    for _ in range(n_iteration):
        x = (ok+ng) * 0.5
        Y = (-A*x+C) / B
        inv_num = inversion_number(Y)
        if inv_num >= th:
            ok = x
        else:
            ng = x
    return ok

print(solve(A, B, C), solve(B, A, C))


Measured memory_bytes: 43556
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e5+1;

int main(){
    vector<bool> prime(MAX, true);
    prime[0] = prime[1] = false;
    for(int i=0; i*i<MAX; i++){
        if(prime[i]){
            for(int j=2; i*j<MAX; j++){
                prime[i*j] = false;
            }
        }
    }

    vector<int> nleq(MAX, 0);
    for(int i=1; i<MAX; i++){
        if(i%2==1 && prime[i] && prime[(i+1)/2]){
            nleq[i] = nleq[i-1]+1;
        }else{
            nleq[i] = nleq[i-1];
        }
    }
    
    int q;
    cin >> q;
    for(int i=0; i<q; i++){
        int l,r;
        cin >> l >> r;
        cout << nleq[r] -nleq[l-1] << endl;
    }
    return 0;
}


Measured memory_bytes: 1024
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <string> 
#include <map>
#include <iomanip>

using namespace std;


typedef int INT;
#define int long long


INT main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	
	int n;

	string s;
	cin >> n >> s;
	int e = 0, w = 0, ce = 0, cw = 0, mn = n;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'E')
			e++;
		else
			w++;
	}

	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'E')
			e--;
		else
			w--;

		mn = min(mn, cw + e - ce);


		if (s[i] == 'E')
		{
			e++;
			ce++;
		}
		else
		{
			w++;
			cw++;
		}
	}
	cout << mn;
	return 0;
}

Measured memory_bytes: 720
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

b = [list(map(int, input().split())) for _ in range(m)]
b.sort(key=lambda x: x[1], reverse=True)

x = 0
y = 0
for i in range(m):
    for j in range(b[i][0]):
        if (a[x] < b[i][1]):
            a[x] = b[i][1]
            x += 1
            if (x == n):
                y = 1
                break
        else:
            y = 1
            break
    if (y==1):
        break
print(sum(a))

Measured memory_bytes: 32968
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

void solve() {
    int n; cin >> n;
    vector<int> v(n);
    vector<int> c(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
    }
    for(int i = 0; i < n; i++){
        cin >> c[i];
    }
    int res = 0;
    for(int i = 0; i < n; i++){
        if(v[i] > c[i]){
            res += v[i] - c[i];
        }
    }
    cout << res << endl;

}

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

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

void solve();

signed main () {
    cout.precision(10);
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif
    int t = 1; 
    // cin >> t;	
    int tt = t;
    while (t--) { 
        solve();
    }
    return 0;
}

void solve() { 
	string s;
	cin >> s;
	int n = (int) s.size();
	int ans = 0;
	for (int i = 0; i < n; i += 1) {
		if (s[i] != s[n - 1 - i]) {
			s[i] = s[n - 1 - i];
			++ans;
		}
	}
	cout << ans;
}



Measured memory_bytes: 256
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/STACK:1024000000,1024000000")

#include <bits/stdc++.h>

#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;

#define popCnt(x) (__builtin_popcountll(x))
#define all(v) begin(v), end(v)

typedef long long Long;
typedef vector<int> vi;

const int N = 1e5 + 5;

struct DSU {
  int parent[N];
  void init() {
    for (int i = 0; i < N; ++i) {
      parent[i] = i;
    }
  }
  int getRoot(int x) {
    if (parent[x] == x) return x;
    return parent[x] = getRoot(parent[x]);
  }
  void join(int x, int y) {
    x = getRoot(x);
    y = getRoot(y);
    parent[x] = y;
  }
} dsu;

struct Edge {
  int from, to;
  bool out_to = false;
};

vector<Edge> edges;
vector<int> mst_adj[N];
bool deg[N];

void kruskal() {
  dsu.init();
  for (int i = 0; i < edges.size(); ++i) {
    auto& edge = edges[i];
    if (dsu.getRoot(edge.from) == dsu.getRoot(edge.to)) {
      edge.out_to = false;
      deg[edge.from] ^= 1;
    } else {
      dsu.join(edge.from, edge.to);
      mst_adj[edge.from].emplace_back(i);
      mst_adj[edge.to].emplace_back(i);
    }
  }
}

void dfs(int node, int parent, int p_edge) {
  for (int v : mst_adj[node]) {
    auto& edge = edges[v];
    int to = edge.from ^ edge.to ^ node;
    if (to == parent) continue;
    dfs(to, node, v);
  }
  if (deg[node]) {
    if (p_edge == -1) {
      cout << -1 << endl;
      exit(0);
    }
    auto& edge = edges[p_edge];
    if (node == edge.to) {
      edge.out_to = true;
    }
    deg[node] ^= 1;
  } else {
    if (p_edge == -1) return;
    auto& edge = edges[p_edge];
    if (node != edge.to) {
      edge.out_to = true;
    }
    deg[parent] ^= 1;
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  int n;
  cin >> n;
  if (n <= 2 || popCnt(n) == 1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= 2; ++i) {
    cout << i << " " << i + 1 << endl;
    cout << i + n << " " << i + 1 + n << endl;
  }
  cout << 3 << " " << n + 1 << endl;
  for (int i = 4; i < n; i += 2) {
    int j = i + 1;
    if (n % 2 == 0 && i == (n & -n)) {
      swap(i, j);
    }
    cout << i << " " << j << endl;
    cout << j << " " << 1 << endl;
    cout << 1 << " " << i + n << endl;
    cout << i + n << " " << j + n << endl;
    if (n % 2 == 0 && j == (n & -n)) {
      swap(i, j);
    }
  }
  if (n % 2 == 0) {
    cout << n << " " << n - (n & -n) + 1 << endl;
    cout << 2 * n << " " << (n & -n) << endl;
  }
  return 0;
}


Measured memory_bytes: 4736
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int SIZE = 200010;
int n;
int num[SIZE],pos[SIZE];

int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i ++)
	{
		scanf("%d",&num[i]);
		pos[num[i]] = i;
	}
	int ans = 0,len = 1;
	for(int i = 2;i <= n;i ++)
	{
		if(pos[i] < pos[i-1])
		{
			ans = max(ans,len);
			len = 1;
		}
		else	len++;
	}
	ans = max(ans,len);
	printf("%d\n",n-ans);
 	return 0;
}


Measured memory_bytes: 1792
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)
#define Rep(i, r, n) for (ll i = r; i < n; i++)
#define RRep(i, r, n) for (ll i = n - 1; i <= r; i--)
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define int long long
#define pb push_back
#define mp make_pair
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
const int MOD = (1000000007);
//const int MOD = (998244353);
//const int INF = (1 << 30)-1;
const int INF = (1LL << 60)-1;
const double EPS = (1 >> 30);

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}

using Graph = vector<vector<pair<int, int>>>;
using Ki = vector<vector<int>>;
using vec = vector<int>;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    //---------------------------------------------
    
    int n;
    cin>>n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    vector<int> b(n);
    rep(i, n) cin >> b[i];
    int suma=0,sumb=0;
    rep(i,n){
        suma+=a[i];
        sumb+=b[i];
    }
    if(suma<sumb){
        cout<<-1<<endl;
    }
    else{
        int amari=0,tarinai=0;
        vec c(0);
        vec d(n);
        int cc=0;
        rep(i,n){
            if(a[i]<b[i]){
                tarinai+=b[i]-a[i];
                c.pb(tarinai);
            }
            else if(a[i]>b[i]){
                d[cc]=(a[i]-b[i]);
                cc++;
            }
        }
        sort(rall(d));
        vec dd(n);
        dd[0]=d[0];
        rep(i,n-1){
            dd[i+1]=dd[i]+d[i+1];
        }
        int ans=c.size();
        //cout<<ans<<endl;
        if(ans==0){
            cout<<0<<endl;
        }
        else{
        rep(i,n){
            ans++;
            if(tarinai<=dd[i]){
                break;
            }
        }
        cout<<ans<<endl;
        }
    }
}

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())

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

ll lcm(ll a, ll b){
    return a * b / gcd(a,b);
}

void solve(){
    int X,Y;
    cin >> X >> Y;

    cout << X + Y/2 << endl;

    return;
}

int main(int argc, char const* argv[])
{
    solve();
    return 0;
}


Measured memory_bytes: 764
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int main(){
	cout << fixed << setprecision(10);
	int a,b;
	cin >> a >> b;
	cout << max({a+b,a-b,a*b}) << endl;
	return 0;
}

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

const int inf=0x3f3f3f3f;
const int maxn=2005;
typedef long long LL;
int n, z, w;
int data[maxn];

int main(){
    while(~scanf("%d%d%d", &n, &z, &w)){
        for(int i=1;i<=n;i++)scanf("%d", &data[i]);
        printf("%d\n", n==1?abs(w-data[1]):max(abs(w-data[n]), abs(data[n]-data[n-1])));
    }
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define NIL -1
const int MAX=1e5+10;
struct Node{
    int p,l,r;//parent,left,right
    Node(int p=NIL,int l=NIL,int r=NIL){
        this->p=p;this->l=l;this->r=r;
    }
}T[MAX];
int n,D[MAX];//D是深度，O(n)递归求
void rec(int cur,int dep){
    D[cur]=dep;
    if(T[cur].l!=NIL) rec(T[cur].l,dep+1);
    if(T[cur].r!=NIL) rec(T[cur].r,dep);
}
void print(int cur){
    cout<<"node "<<cur<<": parent = "<<T[cur].p<<", "<<"depth = "<<D[cur]<<", ";
    if(T[cur].p==NIL) cout<<"root, [";
    else if(T[cur].l==NIL) cout<<"leaf, [";
    else cout<<"internal node, [";
    for(int i=0,c=T[cur].l;c!=NIL;c=T[c].r,i++){
        if(i) cout<<", ";
        cout<<c;
    }
    cout<<"]\n";
}
int main()
{
    int id,k,v;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>id>>k;
        int rr;
        for(int j=0;j<k;j++){
            cin>>v;
            if(j==0) T[id].l=v;
            else T[rr].r=v;
            rr=v;
            T[v].p=id;
        }
    }
    int root;
    for(int i=0;i<n;i++) if(T[i].p==NIL) {root=i;break;}//找根节点
    rec(root,0);
    for(int i=0;i<n;i++) print(i);
}



Measured memory_bytes: 4596
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep_1(i,n) for(int (i)=0;(i)<=(n);(i)++)
#define rep_2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;


// 再帰　メモ　DP

vector<ll> memo;

ll rec(ll n){
    if (n == 0) return 0; //終端条件
    if (memo[n] != 0) return memo[n]; //既に探索済みならリターン

    ll res = LINF;

    for(int pow6 = 1;pow6 <= n;pow6 *= 6){
        res = min(res, rec( n - pow6 ) + 1);
    }

    for(int pow9 = 1;pow9 <= n;pow9 *= 9){
        res = min(res,rec (n - pow9 ) + 1);
    }

    return memo[n] = res;

}

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

    memo.resize(n+1,0);

    cout<<rec(n)<<endl;


    return 0;
}



Measured memory_bytes: 5760
#include <cstdio>
#include <algorithm>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

using ll = long long;
constexpr ll mod = 1000000007;

ll n, k;

ll powMod( ll x, ll y )
{
  ll ret = 1ll;

  while( y > 0 )
  {
    if( y & 1 )
      ret = (ret * x) % mod;

    x = (x*x) % mod;

    y >>= 1;
  }

  return ret;
}

ll fact[200010], inv[200010];

ll comb( ll n, ll r )
{
  if( n < r || n <= 0 || r < 0 )
    return 0;

  return fact[n]*inv[r]%mod*inv[n-r]%mod;
}

int main()
{
  scanf( "%lld%lld", &n, &k );

  k = std::min(n-1,k);

  fact[0] = 1;
  rep( i, 200010 )
    fact[i+1] = (i+1)*fact[i]%mod;

  rep( i, 200010 )
    inv[i] = powMod( fact[i], mod-2 );

  ll ans = 0;

  rep( r, k+1 )
    ans = (ans+comb(n,r)*comb(n-1,r)%mod)%mod;

  printf( "%lld\n", ans );

  return 0;
}

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

using namespace std;

int arr[200] ;

int solve(int x)
{
    for(int i = 1 ; i <= 10000 ; ++i)
    {
        if(i != arr[x-1] && i != arr[x+1])
            return i ;
    }
}

int main()
{
    int n ;
    cin>>n ;
    for(int i = 0 ; i < n ; ++i)
        cin>>arr[i] ;
    int cnt = 0 ;
    for(int i = 1 ; i < n ; ++i)
    {
        if(arr[i] == arr[i-1])
          arr[i] = solve(i) , cnt++;
    }
    cout<<cnt;
    return 0 ;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
string grid[55];
int h, w;

int dx[8] = {0, 0,  1, -1, 1, -1, 1, -1};
int dy[8] = {1, -1, 0, 0, -1, 1, 1, -1};
int valid(int i, int j){
    return i >= 0 && j >= 0 && i < h && j < w && grid[i][j] == '#';
}
int main()
{
    cin >> h >> w;
    for(int i=0; i<h; i++)
        cin >> grid[i];

    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            if(grid[i][j]=='.'){
                int res = 0;
                for(int d=0; d<8; d++){
                    res += valid(i+dx[d], j+dy[d]);
                }
                grid[i][j] = (res + '0');
            }
        }
        cout << grid[i] << endl;
    }

    return 0;
}


Measured memory_bytes: 256
import sys
import numpy as np
import copy
sys.setrecursionlimit(1000000)
 
N = int(input())
SP =  [ input().split() for N in range(N)]

SP_raw = copy.deepcopy(SP)

for i in range(N):
    SP[i][1] = -int(SP[i][1])

SP.sort()

"""
print(SP)
print(SP_raw)

sys.exit()
"""
for i in range(N):
    SP[i][1] = str(-SP[i][1])
    
#print(SP)
#print(SP_raw)
#sys.exit()
#print(SP.index(SP_raw[0]))
for i in range(N):
    print(SP_raw.index(SP[i])+1)

Measured memory_bytes: 21656
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

#define all(x) begin(x), end(x)
#define rep(i,n) for(int i=0; i<n; i++)

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

    int limit = sqrt(n);
    ll ans = 0;
    for (int a=1; a<=limit; a++) {
        for (int b=n/a; b>=a; b--) {
            if (a*b < n) {
                ans++;
                if (a != b) ans++;
            }
        }
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3676
{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.Maybe
import Debug.Trace
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.IntMap.Strict as IntMap
import qualified Data.Set as Set
import qualified Data.IntSet as IntSet
import Data.Functor
import Data.Array
-- import Data.Array.Unboxed
import Control.Monad.ST
import Data.Array.ST

main :: IO ()
main = do
  n <- readLn :: IO Int
  as <- map read . words <$> getLine :: IO [Int]
  bs <- map read . words <$> getLine :: IO [Int]
  print $ solve n as bs

solve :: Int -> [Int] -> [Int] -> Int
solve n as bs = x + min y (last as)
  where
    (x, y) = foldl' f (0, 0) $ zip as bs
    f :: (Int, Int) -> (Int, Int) -> (Int, Int)
    f (count, left) (a, b) =
      (
        count + min a (b + left),
        max 0 $ b - max 0 (a - left)
      )



Measured memory_bytes: 170364
def actual(K, S):
    count = 0

    for x in range(K + 1):
        for y in range(K + 1):
            z = S - (x + y)

            if 0 <= z <= K:
                count += 1

    return count

K, S = map(int, input().split())
print(actual(K, S))

Measured memory_bytes: 9180