akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
ld PI=3.14159265358979323846;

int main(){
    ll N,M;
    cin >> N >> M;
    vector<ll> a(M), b(M);
    vector<Pll> P(M);
    rep(i,M){
        cin >> a[i] >> b[i];
        a[i]--;b[i]--;
        P[i] = Pll(b[i], a[i]);
    }
    sort(P.begin(),P.end());
    //vector<ll> memo(N,-1);
    ll z = -1;
    ll ans = 0;
    rep(i,M){
        if(P[i].se < z){
            continue;
        }else{
            z = P[i].fi;
            ans++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 6404
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<iomanip>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
int main(){
	int a,b;scan(a>>b);
	if(a*b%2==1) prin("Yes");
	else prin("No");
	return 0;
}


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


int main()
{
	char a, b;
	cin >> a >> b;
	if (a == 'H') {
		cout << b << endl;
	}
	else {
		if (b == 'H') {
			cout << "D" << endl;
		}
		else
			cout << "H" << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

#define max(x, y) ((x)>(y)?(x):(y))
#define min(x, y) ((x)<(y)?(x):(y))

vector<P> as, bs;

Int n, a, b;
int main(){
  cin >> n;
  Int asum = 0, bsum = 0;
  for(int i = 0;i < n;i++){
    cin >> a >> b;
    asum += a;
    bsum += b;
    as.push_back(P(a,i));
    bs.push_back(P(b,i));
  }
  sort(as.begin(), as.end());
  sort(bs.begin(), bs.end());
  Int tmp = 0;
  for(int i = 0;i < n;i++)tmp += as[i].first;
  Int res = tmp;
  tmp = 0;
  for(int i = 0;i < n;i++)tmp += bs[i].first;
  res = min(res, tmp);
  tmp = 0;
  set<int> h;
  int acnt = 0, bcnt = 0;
  for(int i = 0;i < n;i++){
    if(as[acnt].first < bs[bcnt].first){
      h.insert(as[acnt].second);
      tmp += as[acnt++].first;
    }
    else{
      h.insert(bs[bcnt].second);
      tmp += bs[bcnt++].first;
    }
  }
  if(h.size() != n)res = min(res, tmp);
  else{
    int aacnt = acnt;
    int bbcnt = bcnt;
    int ntype, nntype;
    int nid, nnid;
    int nval, nnval;
    if(bcnt == n || acnt < n && as[acnt].first < bs[bcnt].first){
      ntype = 0;
      nid = as[acnt].second;
      nval = as[acnt].first;
      acnt++;
    }
    else{
      ntype = 1;
      nid = bs[bcnt].second;
      nval = bs[bcnt].first;
      bcnt++;
    }
    if(bcnt == n || acnt < n && as[acnt].first < bs[bcnt].first){
      nntype = 0;
      nnid = as[acnt].second;
      nnval = as[acnt].first;
      acnt++;
    }
    else{
      nntype = 1;
      nnid = bs[bcnt].second;
      nnval = bs[bcnt].first;
      bcnt++;
    }
    
    for(int i = 0;i < aacnt;i++){
      int id = as[i].second;
      if(id != nid)res = min(res, tmp - as[i].first + nval);
      else res = min(res, tmp - as[i].first + nnval);
    }
    
    for(int i = 0;i < bbcnt;i++){
      int id = bs[i].second;
      if(id != nid)res = min(res, tmp - bs[i].first + nval);
      else res = min(res, tmp - bs[i].first + nnval);
    }
  }

  cout << res << endl;
  
  return 0;
}

Measured memory_bytes: 8036
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
int main() {
  int n,m,c;
  cin>>n>>m>>c;
  int ans=0;
  int sum=c;
  int b[m],a[n];
  rep(i,m){
    cin>>b[i];
  }
  rep(i,n){
    rep(j,m){
      cin>>a[j];
      sum+=a[j]*b[j];
    }
    if(sum>0){
      ans++;
    }
    sum=c;
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 256
import math
import itertools

def main():
  n = int(input())
  A = list(map(int, input().split()))
  len_A = len(A)
  times = 0

  for i in range(len_A):
    for j in reversed(range(i+1, len_A)):
      if A[j] < A[j-1]:
        tmp = A[j]
        A[j] = A[j-1]
        A[j-1] = tmp
        times += 1

  print(*A)
  print(times)

if __name__ == '__main__':
  main()


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

int main(){

  int n,a,b,max,ans,num;
  
  while(1){

    max=0;
    ans=0;
  
    cin>>n;

    if(n==0) break;
    
    for(int i=0;i<n;i++){
      cin>>num>>a>>b;
      a+=b;
      if(max < a){
	max=a;
	ans=num;
      }
    }
    cout<<ans<<" "<<max<<endl;
  }
  
  
    return 0;
}



Measured memory_bytes: 3012
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
#define all(obj) (obj).begin(), (obj).end()
static const int dx[4] = {0, 1, 0, -1};
static const int dy[4] = {1, 0, -1, 0};
static const char dir[4] = {'u', 'r', 'd', 'l'};
static const ll INF = 1 << 21;
static const ll MOD = 1e9 + 7;
ll N, x;

signed main() {
  cin >> N >> x;
  int arr[2 * N];
  for (int i = 1; i < 2 * N; i++) {
    arr[i] = -1;
  }

  if (x <= 1 || x >= 2 * N - 1) {
    puts("No");
    return 0;
  } else {
    puts("Yes");
  }
  arr[N - 1] = 1;
  arr[N] = x;
  arr[N + 1] = N * 2 - 1;
  int iter = 0;
  for (int i = 2; i < 2 * N - 1; i++) {
    if (i == x) continue;
    while (arr[iter] != -1) ++iter;
    arr[iter] = i;
  }
  for (int i = 1; i < 2 * N; i++) {
    cout << arr[i] << endl;
  }
  return 0;
}

Measured memory_bytes: 2304
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace std;
typedef long long ll;

int n, x;
int ps[40];

bool isp[1000010];
void sieve(){
    const int N = 1000000;
    memset(isp, true, sizeof(isp));
    
    isp[0] = false;
    isp[1] = true;
    
    for(int p=2;p*p<=N;p++)if(isp[p]){
        for(int j=p*p;j<=N;j+=p)isp[j] = false;
    }
}

bool can[1000010];
void gen(){
    memset(can, false, sizeof(can));
    can[0] = true;
    
    for(int sum=0;sum<x;sum++){
        for(int i=0;i<n;i++){
            if(sum + ps[i] <= x){
                can[sum + ps[i]] |= can[sum];
            }
        }
    }
}


int main(){
    sieve();
    
    while(cin >> n >> x){
        if(n == 0 && x == 0)break;
        
        for(int i=0;i<n;i++)cin >> ps[i];
        gen();
    
        int res = -1;
        for(int sum=x;sum>=2;sum--)if(can[sum] && isp[sum]){
            res = sum;
            break;
        }
        
        if(res == -1){
            cout << "NA" << endl;
        } else {
            cout << res << endl;
        }
    }
    
    return 0;
}

Measured memory_bytes: 3120
import sys

f_i = sys.stdin

n = int(f_i.readline())

adj = [[]] * n
for x in f_i:
    x = list(map(int, x.split()))
    adj[x[0]] = [c_v for c_v in zip(x[3::2], x[2::2])]

import heapq

def dijkstra():
    PQ = [(0, 0)]
    isVisited = [False] * n
    distance = [999900001] * n

    distance[0] = 0
    
    while PQ:
        uc, u = heapq.heappop(PQ)

        if uc > distance[u]:
            continue

        isVisited[u] = True
        
        for vc, v in adj[u]:
            if isVisited[v] == False:
                t_cost = distance[u] + vc
                if t_cost < distance[v]:
                    distance[v] = t_cost
                    heapq.heappush(PQ, (t_cost, v))

    for v, d in enumerate(distance):
        print(v, d)


dijkstra()

Measured memory_bytes: 70404
#include <iostream>

using namespace std;

int main(){
  int a,b,c;cin>>a>>b>>c;
  int coin=0,cnt=1;
  while(1){
    coin += a;
    if(cnt%7==0) coin += b;
    if(coin>=c) break;
    cnt++;
  }
  cout<<cnt<<endl;
}


Measured memory_bytes: 3100
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
s = a[0]
if a[0] > x:
    ans = (a[0] - x)
    s = x
for i in range(1, n):
    if a[i] + s > x:
        ans += ((a[i] + s) - x)
        s = x - s
    else:
        s = a[i]
print(ans)

Measured memory_bytes: 14468
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200002;

int n;

struct Point
{
    int x, y;
};

struct Plane
{
    Point pos;
    char d;
};

Plane planes[N_MAX];

bool cmpx (const int &a, const int &b)
{
    return planes[a].pos.x < planes[b].pos.x;
}
bool cmpy (const int &a, const int &b)
{
    return planes[a].pos.y < planes[b].pos.y;
}

vector <int> vx[N_MAX];
vector <int> vy[N_MAX];

vector <int> v0[N_MAX * 2];
vector <int> v1[N_MAX * 2];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> planes[i].pos.x >> planes[i].pos.y >> planes[i].d;
        vx[planes[i].pos.x].push_back(i);
        vy[planes[i].pos.y].push_back(i);
        v0[planes[i].pos.x + planes[i].pos.y].push_back(i);
        v1[planes[i].pos.x - planes[i].pos.y + 200000].push_back(i);
    }
    int ans = INT_MAX;
    for(int x = 0; x <= 200000; x++)
    {
        sort(vx[x].begin(), vx[x].end(), cmpy);
        int lastU = -1;
        for(int i : vx[x])
        {
            if(planes[i].d == 'U')
                lastU = i;
            if(planes[i].d == 'D' && lastU != -1)
                ans = min(ans, (planes[i].pos.y - planes[lastU].pos.y) * 5);
        }
    }
    for(int y = 0; y <= 200000; y++)
    {
        sort(vy[y].begin(), vy[y].end(), cmpx);
        int lastR = -1;
        for(int i : vy[y])
        {
            if(planes[i].d == 'R')
                lastR = i;
            if(planes[i].d == 'L' && lastR != -1)
                ans = min(ans, (planes[i].pos.x - planes[lastR].pos.x) * 5);
        }
    }
    for(int k = 0; k <= 400000; k++)
    {
        sort(v0[k].begin(), v0[k].end(), cmpx);
        int lastD = -1;
        int lastR = -1;
        for(int i : v0[k])
        {
            if(planes[i].d == 'D')
                lastD = i;
            if(planes[i].d == 'R')
                lastR = i;
            if(planes[i].d == 'L' && lastD != -1)
                ans = min(ans, (planes[i].pos.x - planes[lastD].pos.x) * 10);
            if(planes[i].d == 'U' && lastR != -1)
                ans = min(ans, (planes[i].pos.x - planes[lastR].pos.x) * 10);
        }
    }
    for(int k = 0; k <= 400000; k++)
    {
        sort(v1[k].begin(), v1[k].end(), cmpx);
        int lastU = -1;
        int lastR = -1;
        for(int i : v1[k])
        {
            if(planes[i].d == 'U')
                lastU = i;
            if(planes[i].d == 'R')
                lastR = i;
            if(planes[i].d == 'L' && lastU != -1)
                ans = min(ans, (planes[i].pos.x - planes[lastU].pos.x) * 10);
            if(planes[i].d == 'D' && lastR != -1)
                ans = min(ans, (planes[i].pos.x - planes[lastR].pos.x) * 10);
        }
    }
    if(ans == INT_MAX)
        cout << "SAFE\n";
    else
        cout << ans << "\n";
    return 0;
}


Measured memory_bytes: 52932
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#define NO return !printf("Impossible\n")
#define N 433
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int n,a[N];
char s[N];
vector<pair<int,int> > ans;
string Rev(string s){
	reverse(s.begin(),s.end());
	for(auto &c:s){
		if(c>='a'&&c<='z')c+='A'-'a';
		else c+='a'-'A';
	}
	return s;
}
int main(){
	n=read();
	scanf("%s",s);
	for(int i=0;i<(1<<n);++i){
		a[i]=s[i]-'0';
	}
	if(!a[0])NO;
	for(int i=0;i<(1<<n);++i){
		if(a[i]){
			for(int j=(i-1)&i;j;j=(j-1)&i){
				if(!a[j])NO;
			}
		}
	}
	ans.emplace_back(0,0);
	for(int i=0;i<(1<<n);++i){
		if(a[i])continue;
		bool ok=true;
		for(int j=(i-1)&i;j;j=(j-1)&i){
			if(!a[j]){
				ok=false;
				break;
			}
		}
		if(!ok)continue;
		string myh;
		for(int j=0;j<n;++j){
			if((i>>j)&1){
				if(myh.empty())myh=(char)('a'+j);
				else myh=(char)('a'+j)+myh+(char)('A'+j)+Rev(myh);
			}
		}
		int pos=0;
		for(auto c:myh){
			int d=0,x=0;
			if(c>='a'&&c<='z'){
				x=c-'a',d=1;
			}
			else{
				x=c-'A',d=0;
			}
			while(pos<x)ans.emplace_back(++pos,0);
			while(pos>x)ans.emplace_back(--pos,0);
			if(d){
				ans.emplace_back(x,1);
				ans.emplace_back(x+1,1);
				ans.emplace_back(x+1,0);
				ans.emplace_back(x,0);
			}
			else{
				ans.emplace_back(x+1,0);
				ans.emplace_back(x+1,1);
				ans.emplace_back(x,1);
				ans.emplace_back(x,0);
			}
		}
		while(pos>0)ans.emplace_back(--pos,0);
	}
	printf("Possible\n%d\n",(int)ans.size()-1);
	for(auto x:ans){
		printf("%d %d\n",x.first,x.second);
	}
	return 0;
}



Measured memory_bytes: 3900
/*
 * Author: .-O_O-.
 */
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pb push_back
#define vi vector<int>
#define rep0(a,b) for(int a=0;a<b;a++)
#define rep1(a,b) for(int a=1;a<=b;a++)
#define repp(a,b,c) for(int a=b;a<c;a++)
#define in insert
#define len length()
#define si size()
#define lb lower_bound
#define ub upper_bound
#define beg begin()
#define en end()
#define f first
#define s second
#define pr(a) cout<<a
#define pr2(a,b) cout<<a<<" "<<b<<"\n"
#define nx cout<<"\n"
#define int long long
// void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
                                                     
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]"<<endl;}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define deb(x...)
#endif
const ll mod = 1000000007;
const ll N = 105;
const ll mm = 1e+18;
bool chk(int a[],int n,int mid,int k){
    vector<bool>dp(k+1,false);
    dp[0]=true;
    rep1(i,n){
        if(i==mid) continue;
        vector<bool>new_dp=dp;
        for(int j=a[i];j<=k;j++){
            if(dp[j-a[i]]) new_dp[j]=true;
        }
        swap(dp,new_dp);
    }
    repp(i,max(0LL,k-a[mid]),k) if(dp[i]) return false;
    return true;
}
void solve()
{
    int n,k;
    cin>>n>>k;
    int a[n+1];
    rep1(i,n) cin>>a[i];
    sort(a+1,a+n+1);
    int ans=0;
    int l=1;
    int r=n+1;
    while(l<r){
        int mid=(l+r)>>1;
        if(chk(a,n,mid,k)){
            ans=mid;
            l=mid+1;
        }
        else r=mid;
    }
    pr(ans);
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
    rep1(_i,t){
        //cout<<"Case #"<<_i<<": ";
        solve();
        //nx;
    }
    return 0;
}

Measured memory_bytes: 3632
/* by Natsu Kinmoe */
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(),(x).end()
#define loop(i,n) for(int i=0;i<(n);i++)
#define cont(i,n) for(int i=1;i<=(n);i++)
#define circ(i,a,b) for(int i=(a);i<=(b);i++)
#define range(i,a,b,c) for(int i=(a);((c)>0?i<=(b):i>=(b));i+=(c))
#define foreach(it,v) for(__typeof((v).begin()) it=(v).begin(),_e_D_=(v).end();it!=_e_D_;it++)
#define y0 y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0
#define y1 y1II11II11III11I1III11II111IIII1II1I1
#define pub push_back
#define pob pop_back
#define mak make_pair

typedef long long ll;
typedef long double lf;
const int Inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
/* Source code starts here */

int n,x,d;
int m[55],p[55];
ll dp[233333],st[55];
vector<int> nei[55];
int siz[55];
int sx[55];

void dfs(int now){
	siz[now]=1;
	st[now]=m[now];
	foreach(i,nei[now]){
		dfs(*i);siz[now]+=siz[*i];
		st[now]+=st[*i];
	}
}

bool Compare(int a,int b){
	return st[a]*siz[b]<st[b]*siz[a];
}

int main(){
	scanf("%d%d%d",&n,&x,&d);
	scanf("%d",m+1);
	circ(i,2,n){
		scanf("%d%d",m+i,p+i);
		nei[p[i]].pub(i);
	}
	dfs(1);memset(dp,0x3f,sizeof(dp));dp[0]=0;
	cont(i,n)cont(j,min(n,d))range(k,n*n*n,siz[i],-1)dp[k]=min(dp[k],dp[k-siz[i]]+st[i]);
	cont(i,n)sx[i]=i;sort(sx+1,sx+n+1,Compare);
	ll ans=0;
	loop(i,n*n*n+1)if(dp[i]<=x){
		ll res=i,lt=x-dp[i];
		cont(j,n){
			if(sx[j]==1)break;
			ll jan=min((ll)max(d-n,0),lt/st[sx[j]]);
			lt-=st[sx[j]]*jan;res+=jan*siz[sx[j]];
		}
		res+=lt/st[1]*siz[1];
		ans=max(ans,res);
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 5624
#include <iostream>
#include <set>

#define NUM { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 }

int main() {
    int n ;
    std::cin >> n ;
    
    std::set< int > s( NUM ) , h( NUM ) , c( NUM ) , d( NUM ) ;
    
    for( int i = 0 ; i < n ; i++ ) {
        char mark ;
        int num ;
        std::cin >> mark >> num ;
        
        switch( mark ) {
            case 'S' :
                s.erase( num ) ;
                break ;
            case 'H' :
                h.erase( num ) ;
                break ;
            case 'C' :
                c.erase( num ) ;
                break ;
            case 'D' :
                d.erase( num ) ;
                break ;
        }
    }
    for( auto &a : s ) {
        std::cout << "S " << a << std::endl ;
    }
    for( auto &a : h ) {
        std::cout << "H " << a << std::endl ;
    }
    for( auto &a : c ) {
        std::cout << "C " << a << std::endl ;
    }
    for( auto &a : d ) {
        std::cout << "D " << a << std::endl ;
    }
}


Measured memory_bytes: 3092
/*
 * 2016-ho-t2.cpp
 *
 *  Created on: 2017/02/09
 *      Author: joi
 */

#include<cstdio>
#include<cstring>
int main(void){
	char a[100001];
	int n;
	long long cj=0;
	long long co=0;
	long long ci=0;
	long long mj=0;
	long long mi=0;
	long long s;
	long long p;
	long long maxj;
	long long maxi;
	long long max;
	scanf("%d",&n);
	scanf("%s",a);
	for(int i=0;i<n;i++){
		if(a[i]=='J')cj++;
		if(a[i]=='O')co++;
		if(a[i]=='I')ci++;
	}
	mj=0;
    mi=ci;
    s=0;
	for(int i=0;i<n;i++){
		if(a[i]=='J'){
			mj++;
		}
		if(a[i]=='O'){
p=mj*mi;
s=s+p;
		}
		if(a[i]=='I'){
			mi--;
		}
	}

	mj=1;
    mi=ci;
    maxj=0;
	for(int i=0;i<n;i++){
		if(a[i]=='J'){
			mj++;
		}
		if(a[i]=='O'){
p=mj*mi;
maxj=maxj+p;
		}
		if(a[i]=='I'){
			mi--;
		}
	}

	mj=0;
    mi=ci+1;
    maxi=0;
	for(int i=0;i<n;i++){
		if(a[i]=='J'){
			mj++;
		}
		if(a[i]=='O'){
p=mj*mi;
maxi=maxi+p;
		}
		if(a[i]=='I'){
			mi--;
		}
	}

	mj=0;
    mi=ci;
    max=0;
	for(int i=0;i<n;i++){
		if(a[i]=='J'){
			mj++;
		}
		if(a[i]=='I'){
			mi--;
		}
		p=mj*mi;
		if(max<p)max=p;
	}
if(maxj>maxi){
	if(maxj>s+max)printf("%lld\n",maxj);
	else printf("%lld\n",s+max);
}
else {
	if(maxi>s+max)printf("%lld\n",maxi);
	else printf("%lld\n",s+max);
}
	return 0;
}

Measured memory_bytes: 2848
import itertools


N = int(input())
L = list(map(int, input().split()))

C = list(itertools.combinations(L, 3))
count = 0
for c in C:
    a, b, c = sorted(c)
    if a == b or a == c or b == c:
        continue
    if c < a + b and a < b + c and b < c + a:
        count += 1
print(count)

Measured memory_bytes: 20616
//
#include <vector>
#include <set>
#include <map>
#include <array>
#include <iostream>
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <numeric>
#include <bitset>
#include <iomanip>
//
using ll = long long;
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);++i)
#define outD(fpv) cout << setprecision(20) << fpv << endl;
inline void yes(bool cond) { cond ? puts("Yes") : puts("No"); }
inline void YES(bool cond) { cond ? puts("YES") : puts("NO"); }
template<typename Type> inline void chmin(Type& a, Type b) { if (a > b) a = b; }
template<typename Type> inline void chmax(Type& a, Type b) { if (a < b) a = b; }
template<typename Arr, typename Type> inline void fill(Arr& arr, Type v) { std::fill(arr.begin(), arr.end(), v); }
template<typename Type> inline void sort(Type& arr) { std::sort(arr.begin(), arr.end(), std::less<>()); }
template<typename Type> inline void rsort(Type& arr) { std::sort(arr.begin(), arr.end(), std::greater<>()); }
template<typename Type> inline Type nth(vector<Type>& arr, size_t pos) { std::nth_element(arr.begin(), arr.begin() + pos, arr.end()); return arr[pos]; }
template<typename Type> inline void unique(Type& arr) { sort(arr); arr.erase(unique(arr.begin(), arr.end()), arr.end()); }
constexpr ll INF_I = std::numeric_limits<ll>::max();
constexpr ll BIGP = 1000000007L;
constexpr double INF_F = std::numeric_limits<float>::infinity();
constexpr double PI = 3.14159265358979323846L;
using P = pair<ll, ll>;
//
int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    //
    ll N;
    cin >> N;
    vector<ll> H(N);
    ll pre = INF_I;
    ll cur = -1;
    ll maxh = 0;
    rep(i, N)
    {
        ll h;
        cin >> h;
        pre < h ? cur = 0 : ++cur;
        chmax(maxh, cur);
        pre = h;
    }
    chmax(maxh, cur);
    cout << maxh;
    //
    return 0;
}

Measured memory_bytes: 3544
def main():
    INF = 10 ** 5
    n, ma, mb = map(int, input().split())
    dp = [[INF] * 401 for _ in range(401)]
    dp[0][0] = 0
    for _ in range(n):
        a, b, c = map(int, input().split())
        for i in range(400 - a, -1, -1):
            dpi = dp[i]
            dpia = dp[i + a]
            for j in range(400 - b, -1, -1):
                if dpia[j + b] > dpi[j] + c:
                    dpia[j + b] = dpi[j] + c
    
    ans = INF
    for i in range(1, 400):
        if ma * i > 400 or mb * i > 400:
            break
        ans = min(ans, dp[ma * i][mb * i])
    if ans >= INF:
        print(-1)
    else:
        print(ans)

main()

Measured memory_bytes: 4840
#include <bits/stdc++.h>
#include<cmath>
#define N 100005
#define A 1000005
#define MOD 998244353 //1000000007 // //100000000
#define INF 1000000000000000000
#define ll long long
//#define M_PI 3.141592653589793238463

using namespace std;

#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define ft first
#define sd second
#define pb push_back
#define rep(i, n) for(ll i = 0; i < n; i++)
#define repr(i, n) for(ll i = n-1; i >= 0; i--)
#define itr(it, x) for(auto it = x.begin(); it != x.end(); it++)
#define mem(a, b) memset(a, (ll)b, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define edge(v, x, y) v[x].pb(y); v[y].pb(x);
#define popcount __builtin_popcount
#define ANS(s) {cout << s << "\n"; return;}

//~~~~~~fancy print~~~~~~~//
#define printpii(a) cout << a.ft << " " << a.sd << endl;
#define printpiii(a) cout << a.ft << " " << a.sd.ft << " " << a.sd.sd << endl;
#define print(a, n) rep(i, n) cout << a[i] << " "; cout << endl;
#define printv(v) for(auto x: v)cout << x << " "; cout << endl;
#define printm(a, n, m) rep(i, n) { rep(j, m) cout << a[i][j] << "\t"; cout << endl;}

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

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


void solve(){
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    ll dx = (x2-x1), dy = (y2-y1);
    ll x4 = x1 - dy, y4 = y1 + dx;
    ll x3 = x2 - dy, y3 = y2 + dx;
    cout << x3 << " " << y3 << " " << x4 << " " << y4 ;
}
main(){
    fast();
    ll t = 1; //cin >> t;
    while(t--){
        solve();
    }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define fint register int
#define h 5001
#define int unsigned long long
using namespace std;
const int mods=1e9+7;
inline int fpow(int b,int p,int k);
signed main()
{
	int n;
	cin>>n;
	cout<<(((fpow(10,n,mods)-fpow(9,n,mods)+mods)%mods-fpow(9,n,mods)+mods)%mods+fpow(8,n,mods))%mods;
	return 0;
}
 
inline int fpow(int b,int p,int k)
{
	int ans=1;
	while(p)
	{
		if(p&1LL)
		ans=ans*b%k;
		b=b*b%k;
		p>>=1LL;
	}
	return ans;
}

Measured memory_bytes: 3632
#include <stdio.h>
int main(void)
{
	int a,b,c,d,e[1000],f,i,j,toal;
	scanf("%d",&a);
	scanf("%d %d",&b,&c);
	scanf("%d",&d);
	for(i=0;i<a;i++){
		scanf("%d",&e[i]);
	}
	for(i=0;i<a;i++){
		for(j=i;j<a;j++){
			if(e[i]<e[j]){
				f=e[i];
				e[i]=e[j];
				e[j]=f;
			}
		}
	}
	toal=d/b;
	for(i=0;i<a;i++){
		d=d+e[i];
		b=b+c;
		f=d/b;
		if(f>toal){
			toal=f;
		}
		else if(f<toal){
		}
	}
	printf("%d\n",toal);
	return 0;
}

Measured memory_bytes: 2692
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;

int main(){
  int n,a,b;cin>>n>>a>>b;
  int res;
  res = (a*n>b)? b:a*n;
  cout<<res<<endl;
}

Measured memory_bytes: 256
num_prob = map(int, raw_input().split())
a_b_list = map(int, raw_input().split())
a = a_b_list[0]
b = a_b_list[1]
prob_list = map(int, raw_input().split()) 

num_1 = 0
num_2 = 0
num_3 = 0

for i in range(len(prob_list)):
    if prob_list[i] <= a:
        num_1 += 1
    elif a < prob_list[i] <= b:
        num_2 += 1
    else:
        num_3 += 1

result = min(num_1, num_2, num_3)

print(result)



Measured memory_bytes: 2568
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
	
	int day, ja, math, doja, doma;
	int cnt_m = 0;
	int cnt_j = 0;
	cin >> day >> ja >> math >> doja >> doma;


	while(ja > 0){
		ja -= doja;
		cnt_j++;
	}
	while(math > 0){
		math -= doma;
		cnt_m++;
	}
	cout << day-max(cnt_m, cnt_j) << endl;
	

	return 0;
}

Measured memory_bytes: 1164
#include <stdio.h>

int main(void)
{
  int i, j, k, s, count = 0;
  
  scanf("%d %d", &k, &s);
  
    for( i = 0; i <= k; i++ ){
      for( j = 0; j <= k; j++ ){
        if( s-k <= i+j && i+j <= s ){
          count += 1;
        }
      }
    }
  
  printf("%d\n", count);
  
  return 0;
}

Measured memory_bytes: 128
#include <stdio.h>

int main(void)
{
	int b, r, g, c, s, t;
	int medal;
	
	while (1){
		scanf("%d %d %d %d %d %d", &b, &r, &g, &c, &s, &t);
		
		if (!b && !r && !g && !c && !s && !t){
			break;
		}
		
		medal = 100;
		
		medal = medal + (b * (6 * 15 + 5)) + (r * (4 * 15 + 3))
		+ (g * 7) + (c * 2) + (s * 3) - (t * 3);
		
		printf("%d\n", medal);
	}
	
	return (0);
}

Measured memory_bytes: 600
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder.Contest.B
{
	static class Program
	{
		public static void Main(string[] args)
		{
			var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
			Console.SetOut(sw);

			var cin = new Scanner();

			int h = cin.NextInt();
			int w = cin.NextInt();
            for (int i = 0; i < h; i++)
            {
				string c = cin.Next();
				Console.WriteLine(c);
				Console.WriteLine(c);
            }

			Console.Out.Flush();
		}
	}

	class Scanner
	{
		string[] s;
		int i;

		char[] cs = new char[] { ' ' };

		public Scanner()
		{
			s = new string[0];
			i = 0;
		}

		public string Next()
		{
			if (i < s.Length) return s[i++];
			string st = Console.ReadLine();
			while (st == "") st = Console.ReadLine();
			s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
			if (s.Length == 0) return Next();
			i = 0;
			return s[i++];
		}

		public int NextInt()
		{
			return int.Parse(Next());
		}

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++)
			{
				Array[i] = NextInt() + add;
			}
			return Array;
		}

		public long NextLong()
		{
			return long.Parse(Next());
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++)
			{
				Array[i] = NextLong() + add;
			}
			return Array;
		}

		public double NextDouble()
		{
			return double.Parse(Next());
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++)
			{
				Array[i] = NextDouble() + add;
			}
			return Array;
		}
	}
}

Measured memory_bytes: 13140
#include <bits/stdc++.h>
//#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define popcount(n) __builtin_popcount(n)
#define popcountll(n) __builtin_popcountll(n)

using namespace std;
//using namespace __gnu_cxx;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI = acos(-1.0), EPS = 1e-6;
const long long inf = 2e12 + 12;
const int MAXN = 200005, MAXM = 3003, Mod = 1000000007 , MAXLog = 20;



int main() {
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "rt", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed;

    string s;
    cin >> s;
    int ch1 = 0, ch2 = 0;
    for (int i = 0; i < s.size(); ++i) {
        if (i & 1) {
            ch1 += s[i] != '0';
            ch2 += s[i] != '1';
        } else {
            ch1 += s[i] != '1';
            ch2 += s[i] != '0';
        }
    }

    cout << min(ch1, ch2) << '\n';

    return 0;
}

Measured memory_bytes: 512
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

int f(int a, int b, int c, int d, int n) {
  int p = (10*a + 50*b + 100*c + 500*d) - n;
  if (p < 0) return 100;

  int q, r, s, t;
  q = p / 500;
  p %= 500;
  r = p / 100;
  p %= 100;
  s = p / 50;
  p %= 50;
  t = p / 10;

  if ((a && t) || (b && s) || (c && r) || (d && q)) return 100;
  return q+r+s+t;
}

int testcase_ends(bool first) {
  int n;
  scanf("%d", &n);
  if (n == 0) return 1;

  if (!first) puts("");

  std::vector<int> a(4);
  for (auto& ai: a) scanf("%d", &ai);

  int res = 100;
  std::vector<int> u{10, 50, 100, 500};
  std::vector<int> b;
  for (int i = 0; i <= a[0]; ++i)
    for (int j = 0; j <= a[1]; ++j)
      for (int k = 0; k <= a[2]; ++k)
        for (int l = 0; l <= a[3]; ++l) {
          int cur = f(i, j, k, l, n);
          if (res > cur-(i+j+k+l)) {
            b = {i, j, k, l};
            res = cur-(i+j+k+l);
          }
        }

  for (int i = 0; i < 4; ++i)
    if (b[i]) printf("%d %d\n", u[i], b[i]);

  return 0;
}

int main() {
  bool first = true;
  while (!testcase_ends(first)) first = false;
}



Measured memory_bytes: 2612
#include<stdio.h>
int main(){
	int a[3],tmp,i,j;
	for(i=0;i<3;i++)scanf("%d",&a[i]);
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(a[i]<a[j]){
				tmp=a[i];a[i]=a[j];a[j]=tmp;
			}
		}
	}
	for(i=0;i<3;i++){
		printf("%d",a[i]);
		if(i!=2)printf(" ");
		else printf("\n");
	}
	return 0;
}

Measured memory_bytes: 600
N = gets.to_i
A = N.times.map{gets.to_i}

max = A.max
second_max = A.sort[-2]
max_cnt = A.count(max)

if max_cnt >= 2
  A.each do |a|
    puts max
  end
  exit
end

A.each do |a|
  if a == max
    puts second_max
  else
    puts max
  end
end


Measured memory_bytes: 8864
#include<iostream>
#include<deque>
#include<cstring>
using namespace std;

const int N = 100005;
int k,dist[N];
deque<int> dq;

int main() {
	int n, x, t;
	cin >> k;
	memset(dist, 63, sizeof(dist));
	dq.push_front(1);
	dist[1] = 1;
	while (!dq.empty()) {
		x = dq.front();
		dq.pop_front();
		t = (x * 10) % k;
		if (dist[t] > dist[x]) {
			dist[t] = dist[x];
			dq.push_front(t);
		}
		t = (x + 1) % k;
		if (dist[t] > dist[x] + 1) {
			dist[t] = dist[x] + 1;
			dq.push_back(t);
		}
	}
	cout << dist[0] << endl;

	return 0;
}

Measured memory_bytes: 896
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define all(x) (x).begin(),(x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {

	int n, m;
	cin >> n >> m;

	V<V<P>> e(n);

	rep(i, m) {
		int l, r, d;
		cin >> l >> r >> d;
		l--;r--;
		e[l].push_back(P(d, r));
		e[r].push_back(P((-1) * d, l));
	}

	queue<int> que;
	vi x(n, INF);

	rep(i, n) {
		if (x[i] != INF)continue;
		x[i] = 0;
		que.push(i);

		while (!que.empty()) {
			int v = que.front();que.pop();
			for (auto p : e[v]) {
				int vv = p.second;
				int d = p.first;
				if (x[vv] == INF) {
					x[vv] = x[v] + d;
					que.push(vv);
				}
				else if (x[vv] != x[v] + d) {
					cout << "No" << endl;
					return 0;
				}
			}
		}
	}

	cout << "Yes" << endl;

}

Measured memory_bytes: 9344
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
    //cout.precision(10);
    int a, b;
    cin >> a >> b;
    cout << (a - 1) * ((b - 1)) << endl;
    return 0;
}

Measured memory_bytes: 256
A, B = list(map(int, input().split()))
ans = 0

def judge(s):
    n = len(s)
    for i in range(n // 2):
        if s[i] != s[n-1-i]:
            return False
    return True

for i in range(A, B+1):
    if judge(str(i)):
        ans += 1
print(ans)

Measured memory_bytes: 3060
#include<vector>
#include<string>
#include <iostream>
using namespace std;
int main()
{
	char str[4];
	cin>>str;
	if (str[0] == str[1] || str[1] == str[2] || str[2] == str[3])
	{
		cout<<"Bad\n";
	}
	else
	{
		cout<<"Good\n";
	}
    return false; 
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
using namespace std;
typedef long long ll;
int main(){
   #ifndef ONLINE_JUDGE 
      FILE_READ_IN 
      FILE_READ_OUT 
   #endif
    int n; cin>>n;
    int k; cin>>k;
    int x; cin>>x;
    int y; cin>>y;

    if(k>=n)
    {
        cout<<x*n<<"\n";
    }
    else{
        cout<<(x*k+(n-k)*y)<<"\n";
    }
   return 0;
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
#define REP(i, n) for(int i=0; i<(int)(n); i++)
#define REP1(i,a,b) for(int i=a; i<=(int)(b); i++)
#define ALL(x) begin(x),end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int,int> PII;

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}

const int MAX_N = 505;
int A[MAX_N][MAX_N];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n, m, Q;
    cin >> n >> m >> Q;

    REP(i, m) {
        int a, b; cin >> a >> b;
        A[a][b]+=1;
    }

    REP1(i, 1, n) REP1(j, 1, n) {
        A[i][j] += A[i][j-1];
    }

    REP1(j, 1, n) REP1(i, 1, n) {
        A[i][j] += A[i-1][j];
    }

    REP(i, Q) {
        int a, b; cin >> a >> b;
        cout << A[b][b]-A[b][a-1]-A[a-1][b]+A[a-1][a-1] << endl;
    }

    return 0;
}

Measured memory_bytes: 1920
# Problem B
N, M = map(int, input().split())
A_list = list(map(int, input().split()))

# initialization
a_sum = sum(A_list)
a_limit = a_sum / (4*M)
a_count = 0
is_ok = False

# search
for a in A_list:
    if a>=a_limit:
        a_count += 1

# output
if a_count>=M:
    print("Yes")
else:
    print("No")


Measured memory_bytes: 38384
#include <bits/stdc++.h>
#define ll long long
#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 pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int> >
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define INF 1000000000
#define eps 1e-7
#define mod 1000000007
#define int ll
using namespace std;

signed main(void) {
    int N, K;
    cin >> N >> K;
    if (N >= 2 * K - 1) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP2(i, a, b) for(int i = a;i <= b;i++)
#define REPR(i, a, b) for(int i = a; i >= b; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define PI 3.14159265358979323846
#define vi vector<int>
#define vll vector<ll>
#define vi2 vector<vector<int>>
#define eb emplace_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define sz(x) int(x.size())
using namespace std;
using P = pair<ll,ll>;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};
#define INT(name) int name;cin >> name;
#define VEC(type,name,n) vector<type> name(n);REP(i,n) cin >> name[i];

template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge{ll to, cost;};
typedef pair<ll,ll> P;
struct graph{
  ll V;
  vector<vector<edge> > G;
  vector<ll> d;

  graph(ll n){
    init(n);
  }

  void init(ll n){
    V = n;
    G.resize(V);
    d.resize(V);
    REP(i,V){
      d[i] = INF;
    }
  }

  void add_edge(ll s, ll t, ll cost){
    edge e;
    e.to = t, e.cost = cost;
    G[s].push_back(e);
  }

  void dijkstra(ll s){
    REP(i,V){
      d[i] = INF;
    }
    d[s] = 0;
    priority_queue<P,vector<P>, greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()){
      P p = que.top(); que.pop();
      ll v = p.second;
      if(d[v]<p.first) continue;
      for(auto e : G[v]){
        if(d[e.to]>d[v]+e.cost){
          d[e.to] = d[v]+e.cost;
          que.push(P(d[e.to],e.to));
        }
      }
    }
  }
};
int main()
{
  ll N,t,a;
  cin >> N >> t >> a;
  t--;
  a--;
  vector<P> A(N-1);
  REP(i,N-1){
    cin >> A[i].first >> A[i].second;
    A[i].fi--;
    A[i].se--;
  }
  graph g(N);
  REP(i,N-1){
    g.add_edge(A[i].fi,A[i].se,1);
    g.add_edge(A[i].se,A[i].fi,1);
  }
  g.dijkstra(t);
  vector<ll> taka=g.d;
  g.dijkstra(a);
  vector<ll> aoki=g.d;

  // REP(i,N){
  //   cout << taka[i] << " " << aoki[i] << endl;
  // }

  

  ll MAX2=-1;
  REP(i,N){
    if(taka[i]<aoki[i]){
      MAX2=max(MAX2,aoki[i]);
    }
  }

  cout << MAX2-1 << endl;
}


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

int main() {
	long long int A, B; cin >> A >> B;
	for (long long int K = 0; K < max(A,B); K++) {
		if (abs(A - K) == abs(B - K)) {
			cout << K << endl;
			return 0;
		}
	}

	cout << "IMPOSSIBLE" << endl;
}	

Measured memory_bytes: 256
import sys

# import bisect
from collections import Counter, deque

# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math


# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


class Edge:

    def __init__(self, index, u, v):
        self.index = index
        self.u = u
        self.v = v

    def __str__(self):
        return "{} {}".format(self.u, self.v)


class Vertex:

    def __init__(self, index):
        self.index = index
        self.connected_edge_index = []
        self.color = -1

    def __str__(self):
        return str(self.index)


class Graph:
    # 1-indexed

    def __init__(self, vertices=0):
        self.vertices = [Vertex(x) for x in range(vertices + 1)]
        self.edges = []
        self._vertex_count = vertices
        self._edges_count = 0

    def edges_count(self):
        return self._edges_count

    def vertices_count(self):
        return self._vertices_count

    def add_edge(self, u, v):
        edge = Edge(self._edges_count, u, v)
        self.vertices[edge.u].connected_edge_index.append(edge.index)
        self.vertices[edge.v].connected_edge_index.append(edge.index)
        self.edges.append(edge)
        self._edges_count += 1

    # 連結している節点を求める
    def neighbors(self, index):
        return [self.edges[e].u if self.edges[e].u != index else self.edges[e].v for e in
                self.vertices[index].connected_edge_index]

    # 連結要素数を求める
    def neighbors_count(self, index):
        return len(self.vertices[index].connected_edge_index)

    def __str__(self):
        desc = ""
        for i in range(self._edges_count):
            desc += str(self.edges[i]) + "\n"
        return desc


def main():
    n, k = list(map(int, readline().split()))
    g = Graph(n)

    for i in range(n-1):
        a, b = list(map(int, readline().split()))
        g.add_edge(a, b)

    que = deque()
    que.append((1, 0, k))

    while que:
        current, parent, c = que.popleft()
        g.vertices[current].color = c
        next_c = k - 1 - parent
        for i in g.neighbors(current):
            if g.vertices[i].color == -1:
                que.append((i, 1, next_c))
                next_c -= 1

    ans = 1

    for i in range(1, n + 1):
        ans *= g.vertices[i].color
        ans %= MOD

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 96800
MODE = "stdio"
require 'matrix'
line = Array.new

if(defined? MODE)
  line = STDIN.read.split("\n")
else
  tmp = "\
  3 2 3
  1 2
  0 3
  4 5
  1 2 1
  0 3 2
  ".split("\n")
  tmp.each{|s|
    line << s.strip
  }
end
n,m,l=line[0].split(" ").map(&:to_i)
a = []
b = []
(1..n).each do |n|
  a << line[n].split(" ").map(&:to_i)
end

(1+n..n+m).each do |m|
  b << line[m].split(" ").map(&:to_i)
end

A = Matrix[*a]
B = Matrix[*b]
  
  
(A*B).to_a.each do |s|
  puts s.join(" ")
end

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

typedef long long LL;

int n, m, cnt[200000];

int main() {
	scanf("%d%d", &n, &m);
	if (n & 1) {
		for (int i = 1; i <= m; i++) {
			printf("%d %d\n", i, n - i + 1);
		}
	} else {
		if ((n - 2) % 4 == 0) {
			int L = 1, R = n;
			for (int i = 1; i <= m; i++) {
				if (R - L == n / 2) R--;
				printf("%d %d\n", L, R);
				L++, R--;
			}	
		} else {
			int L = 1, R = n;
			for (int i = 1; i <= m; i++) {
				int d1 = R - L - 1, d2 = n - 2 - d1;
				if (cnt[d1] || cnt[d2]) R--;
				d1 = R - L - 1, d2 = n - 2 - d1;
				cnt[d1] = 1, cnt[d2] = 1;
				printf("%d %d\n", L, R);
				L++, R--;
			}	
		}
		
	}
	
	return 0;
}

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

using namespace std;

int main() {
    vector<long long int> A(3);
    bool flag=false;
    for(int i=0; i<3; i++) {
        cin >> A[i];
        if(A[i]%2==0) {
            flag=true;
        }
    }
    if(flag) {
        cout << 0 << endl;
    } else {
        sort(A.begin(), A.end());
        cout << A[0]*A[1] << endl;
    }



    return 0;
    
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std; 
#define mod 1000000007
#define N 100005
#define ll long long
#define ull unsigned long long
#define loop1(i, j, k, step) for (int i=j; i<k; i+=step)
#define loop(i, j, k) for (int i=j; i<k; i++) 
#define rloop1(i, j, k, step) for (int i=j; i>=k; i-=step)
#define rloop(i, j, k) for(int i=j; i>=k; i--)
#define rep(i, k) for(int i = (0); i < (k); i++ )
#define rrep(i, k) for(int i = k-1; i >= 0; i-- )

#define forall(it, l) for(auto it=l.begin(); it != l.end(); it++)
#define mp make_pair
#define pb push_back
#define pop pop_back
#define F first
#define S second
#define sz(x) (int)(x).size()
#define endl "\n"
#define Max (int)INT_MAX
#define Min (int)INT_MIN
#define MEM(a, b) memset(a, (b), sizeof(a))
#define fast_io ios_base::sync_with_stdio(0), cin.tie(NULL)
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;


void solve()
{
	ll n,i,j,k,l,z=0,flag=0,a,b;
	cin>>k;
	cin>>a>>b;
	loop(i,a,b+1){
		if(i%k==0){
			cout<<"OK"<<endl;
			return;
		}
	}
	cout<<"NG"<<endl;
}
int  main() 
{
	auto start=clock();
	fast_io;
    int t=1;
    //cin>>t;
    while(t--)
    	solve();
    auto stop =clock();
    cerr<<"time taken:"<<double(stop-start)/double(CLOCKS_PER_SEC)<<" s"<<endl;
	return 0; 
}

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

using namespace std;

bool ok(vector<int>&a,int x,int y){
	int cnt=0;
	for(int i=0;i<a.size();i++){
		if(a[i]==x || a[i]==y)cnt++;
	}
	return cnt==2;
}

int main(){
	int x,y;
	cin>>x>>y;
	vector<int> a={1,3,5,7,8,10,12},b={4,6,9,11},c={2};
	if(ok(a,x,y) || ok(b,x,y) || ok(c,x,y)){
		cout<<"Yes";
	}
	else cout<<"No";
	
}

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

#define rep(i,a,N) for(int i=0+a;i<N;i++)
#define lint long long int
#define SIZE 100005
#define pb push_back

using namespace std;
int main(){
	string s;
	string s1="erase",s2="dream",s3="dreamer";
	cin>>s;
	lint i=0;
	while(i+1<s.size()){
		/*if(s.size()-i<4){
			cout<<"NO";
			return 0;
		}*/
		if(s.substr(i,5)==s1){
			i+=5;
			if(s.size()-i<1)continue;
			if(s[i]=='r'){
				i++;
				continue;
			}
		}
		else if(s.substr(i,5)==s2){
			i+=5;
			if(s.size()-i<2)continue;
			if(s.substr(i,2)=="er"){
				i+=2;
				continue;
			}
		}
		else{
			if(i-7>=0)
			if(s.substr(i-7,7)==s3){
				i-=2;continue;
			}
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";

return 0;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
 
 
 long long int dp[100005];
 long long int call( int arr[],int n,int i)
 {
 	if(i>=n-1)
 	return 0;
 	
 	if(dp[i]!=-1)
 	return dp[i];
 	
 	if(i+2<n)
 	return dp[i]=min(call(arr,n,i+2)+abs(arr[i+2]-arr[i]),call(arr,n,i+1)+abs(arr[i+1]-arr[i]));
 	if(i+1<n)
 	return dp[i]=call(arr,n,i+1)+abs(arr[i+1]-arr[i]);
 }
 
 
 int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
     cin>>arr[i];
     
    for(int i=0;i<=n;i++)
     dp[i]=-1;
     
    cout<<call(arr,n,0);
	return 0;
} 

Measured memory_bytes: 9216
import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] arr = new int[3];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = sc.nextInt();
		}
		Arrays.sort(arr);
		int count = arr[2] - arr[1];
		arr[0] += count;
		int add = (arr[2] - arr[0] + 1) / 2;
		if (arr[0] + add * 2 > arr[2]) {
			count++;
		}
		count += add;
		System.out.println(count);
	}
}


Measured memory_bytes: 23380