akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;

#define REP(i,n) for(ll (i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(),v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define Decimal fixed << setpresicion(20)


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

ll N, X;
ll twopow[61];

void makepow()
{
  twopow[0] = 1;
  REP(i, 60) twopow[i + 1] = twopow[i] * 2;
}

ll rec(ll X, ll L)
{
  if (L == 0) return 1;
  if (X == 1) return 0;
  if (1 < X && X < twopow[L+1] - 1) return rec(X-1, L-1);
  if (X == twopow[L+1] - 1) return twopow[L];
  if (twopow[L+1] - 1 < X) return twopow[L] + rec(X - (twopow[L+1] - 1), L - 1);
}

int main()
{
  makepow();
  cin >> N >> X;
  cout << rec(X, N) << endl;
  return 0;
}


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

using namespace std;
int main()
{
    long long n,a[10009],b[10009];
    cin >> n;
    for(int i=0;i<n;i++) cin >> a[i];
    for(int i=0;i<n;i++) cin >> b[i];
    for(int i=0;i<n;i++) a[i]=b[i]-a[i];
    long long m1,m2;
    m1=m2=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]>0) m2+=a[i]/2;
        if(a[i]<0) m1-=a[i];
    }
    if(m1<=m2) printf("Yes\n");
    else printf("No\n");
    return 0;
}


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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
using P = pair<ll,ll>;
using ld=long double;

int main(){
    ll n;cin >>n;
    vector<ll>dp(n+1,INF);
    vector<ll>yen;
    yen.push_back(1);
    ll k=1;
    while(k<=n){
        k*=6;
        yen.push_back(k);
    }
    k=1;
    while(k<=n){
        k*=9;
        yen.push_back(k);
    }
    dp[0]=0;
    for (int i = 0; i < n; ++i) {
        for(auto u:yen){
            if(i+u>n)continue;
            dp[i+u]=min(dp[i+u],dp[i]+1);
        }
    }
    cout <<dp[n]<<endl;
    return 0;
}

Measured memory_bytes: 3904
#include<iostream>

using namespace std;

int a,b,c,s,t,miti[20][20],memo[20][20],sum;

long long int zi(int x,int y){
        if(x==1&&y==1)return 1;

        if(memo[x][y]!=0)return memo[x][y];

        if(miti[x][y]==-1)return 0;

        if(x==1)return zi(x,y-1);
        if(y==1)return zi(x-1,y);

        int ans= zi(x-1,y)+zi(x,y-1);
        memo[x][y]=ans;
        return ans;
    }

int main(){
    while(1){

    cin >> a >> b;
    if(a==0&&b==0)break;

    cin >> c;
    for(int i=0;i<c;i++){
        cin >> s >> t;
        miti[s][t]=-1;
    }

    sum=zi(a,b);
    cout << sum << endl;
    sum=0;
    for(int i=0;i<20;i++){
        for(int j=0;j<20;j++){
            memo[i][j]=0;
            miti[i][j]=0;
        }
    }


    }
return 0;
}

Measured memory_bytes: 1164
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

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

inline int Read()
{
	int x = 0, f = 1, c = getchar();
	for (; !isdigit(c); c = getchar())
		if (c == '-')
			f = -1;
	for (;  isdigit(c); c = getchar())
		x = x * 10 + c - '0';
	return x * f;
}

const int MAXN = 100005;

struct Point
{
	LL x, y;
	int i;
	bool operator < (const Point &b) const { return x < b.x || (x == b.x && y < b.y); }
} p[MAXN];

int n, x, y, vis[MAXN], a[MAXN], b[MAXN];
LL d, ans, siz[MAXN];
vector <int> adj[MAXN];

inline void Solve(int u, int v)
{
	sort(p + 1, p + n + 1);
	mset(a, 0), mset(b, 0);
	for (int i = 1, l = 0, r = 0; i <= n; i ++)
	{
		while (p[l + 1].x < p[i].x - u)
			l ++;
		while (p[r + 1].x <= p[i].x - u)
			r ++;
		int L = l, R = l;
		for (int j = 16; ~j; j --)
			if (L + (1 << j) <= r && p[L + (1 << j)].y < p[i].y - v)
				L += 1 << j;
		for (int j = 16; ~j; j --)
			if (R + (1 << j) <= r && p[R + (1 << j)].y <= p[i].y + v)
				R += 1 << j;
		L ++;
		if (L > R)
			continue;
		a[L] ++, a[R] --;
		b[L] ++, b[R + 1] --;
		siz[p[i].i] += R - L + 1;
		adj[p[i].i].pb(p[L].i), adj[p[L].i].pb(p[i].i);
	}
	for (int i = 1; i <= n; i ++)
		b[i] += b[i - 1], siz[p[i].i] += b[i];
	for (int i = 1; i < n; i ++)
		if (a[i] += a[i - 1])
			adj[p[i].i].pb(p[i + 1].i), adj[p[i + 1].i].pb(p[i].i);
}

inline void Dfs(int x)
{
	vis[x] = 1, ans += siz[x];
	for (auto y : adj[x])
		if (!vis[y])
			Dfs(y);
}

int main()
{
#ifdef wxh010910
	freopen("data.in", "r", stdin);
#endif
	n = Read(), x = Read(), y = Read();
	for (int i = 1; i <= n; i ++)
		p[i].x = Read(), p[i].y = Read(), p[i].x += p[i].y, p[i].y = p[i].x - (p[i].y << 1), p[i].i = i;
	d = max(abs(p[x].x - p[y].x), abs(p[x].y - p[y].y));
	Solve(d, d);
	for (int i = 1; i <= n; i ++)
		swap(p[i].x, p[i].y);
	Solve(d, d - 1);
	Dfs(x);
	return printf("%lld\n", ans >> 1), 0;
}


Measured memory_bytes: 14720
n, y = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
ans = 0
if y == sum(a):
  ans = n
elif y > sum(a):
  ans = n -1
else:
  for i in range(n):
    if y - a[i] >= 0:
      y -= a[i]
      ans += 1
    else:
      break
print(ans)

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


int solve(vector<int> A){
	int ans;
	if((A[1]-A[0])%2==0) {
		ans = (A[1]-A[0])/2 + A[2]-A[1];
	}
	else{
		ans = (A[1]-A[0])/2 + A[2]-A[1] + 2;
	}

	return ans;
}	


int main(){
	vector<int> N(3);
	cin >> N[0] >> N[1] >> N[2];
	sort(N.begin(),N.end());
	cout << solve(N) << endl;
}














Measured memory_bytes: 3636
#include <bits/stdc++.h>
  using namespace std;
#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) int ab;cin>>ab;(p).push_back(ab);
#define vecpl2(p) long long ab;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define set0(k,n) for(int nn=0;nn<(n);nn++){ (k).push_back(0); }
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define vecsize(b,size) vector<int> (b)((size));
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsl(s) vector<pair<ll,ll>> (s);
#define pairss(s) vector<pair<string,string>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairspl(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairsps(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairsREV(s) (s).push_back(make_pair(bb,aa));
#define pairslREV(s) (s).push_back(make_pair(bb,aa));
#define pairssREV(s) (s).push_back(make_pair(bb,aa));
#define MOD 1000000007

int main() {
	
	int A,B;
	cin>>A>>B;
	if(A%3==0||B%3==0||(A+B)%3==0){
		ce("Possible")
	}else{
		ce("Impossible")
	}
	
}

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

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a,b; cin>>a>>b;
  int L=b*10,R=(b+1)*10;
  int ans=-1;
  rep(i,R-L) if((L+i)*8/100==a){
    ans=L+i;
    break;
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
import numpy as np

N = list(map(int, input()))
K = int(input())
l = len(N)
if l < K:
    print(0)
    exit()

# dp[d][k] := d 桁目まで見て 0 でない数字が k 個ある場合の数
dp_small = np.zeros((l+1, K+1), dtype=np.int64)
dp_large = np.zeros((l+1, K+1), dtype=np.int64)
dp_large[0, 0] = 1
for d, digit in enumerate(N, 1):
    for k in range(K+1):
        # 0 を配置する
        dp_small[d, k] += dp_small[d-1, k]
        if digit != 0:
            dp_small[d, k] += dp_large[d-1, k]
            dp_large[d, k] = 0
        else:
            dp_large[d, k] = dp_large[d-1, k]
        if k!=0:
            # 0 以外を配置する
            dp_small[d, k] += dp_small[d-1, k-1] * 9
            if digit >= 1:
                dp_small[d, k] += dp_large[d-1, k-1] * (digit-1)
                dp_large[d, k] += dp_large[d-1, k-1]
print(dp_small[l, K] + dp_large[l, K])


Measured memory_bytes: 27148
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

X, Y, Z, K = LI()
A_li = LI()
B_li = LI()
C_li = LI()

A_li = sorted(A_li, reverse=True) + [-INF]
B_li = sorted(B_li, reverse=True) + [-INF]
C_li = sorted(C_li, reverse=True) + [-INF]

from functools import lru_cache

@lru_cache(maxsize=None)
def calc_result(a,b,c):
    return (A_li[a]+B_li[b]+C_li[c])


search_list = [(0,0,0)]
calc_list = [calc_result(0,0,0)]
for i in range(K):
    maximum = max(calc_list)
    print(maximum)
    index = calc_list.index(maximum)
    a, b, c = search_list[index]
    search_list.pop(index)
    calc_list.pop(index)
    if (a+1, b, c) not in search_list:
        search_list.append((a+1,b,c))
        calc_list.append(calc_result(a+1,b,c))
    if (a, b+1, c) not in search_list:
        search_list.append((a,b+1,c))
        calc_list.append(calc_result(a,b+1,c))
    if (a, b, c+1) not in search_list:
        search_list.append((a,b,c+1))
        calc_list.append(calc_result(a,b,c+1))


Measured memory_bytes: 6064
N = int(input())
D, X = map(int,input().split())

sum = 0
for i in range(N):
    a = int(input())
    d = 0
    i = 0
    while d <= D:
        d = a*i + 1
        i += 1
        if d <= D:
	        sum += 1

print(sum + X) 

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

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

	int kari = a;
	a = b;
	b = kari;
	kari = a;
	a = c;
	c = kari;

	cout << a << " " << b << " " << c << endl;
	return 0;
}


Measured memory_bytes: 3596
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>

using namespace std;

int main() {
	int N; cin >> N;
	cout << N * N * N << endl;
	return 0;
}


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

using namespace std;


void solve() {
    string S; cin >> S;
    int K; cin >> K;
    int N = S.size();
    
    int dp[105][4][2] = {};

    dp[0][0][0] = 1;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < 2; k++) {
                int nd = S[i] - '0';
                for (int d = 0; d < 10; d++) {
                    int ni = i + 1, nj = j, nk = k;
                    if (d != 0) nj++;
                    if (nj > K) continue;
                    if (k == 0) {
                        if (d > nd) continue;
                        if (d < nd) nk = 1;
                    }
                    dp[ni][nj][nk] += dp[i][j][k];
                }
            }
        }
        
    }
    int ans = dp[N][K][0] + dp[N][K][1];
    cout << ans << endl;
}


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

Measured memory_bytes: 256
#include<bitset>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

using Bits = bitset<448>;
int N, M;
int X[100011], Y[100011];
Bits B[411];
char op[100011];

void MAIN() {
    scanf("%d%d", &N, &M);
    REP (i, M) {
	int x, y;
	scanf("%d%d", &x, &y);
	x--; y--;
	X[i] = x;
	Y[i] = y;
    }

    REP (i, N) {
	bool ok = true;
	for (int j=M; j--;) {
	    int x = X[j], y = Y[j], z = x^y^i;
	    if (x == i || y == i) {
		if (B[i].test(z)) {
		    ok = false;
		    break;
		}
		if (x == i) op[j] = 'y';
		else op[j] = 'x';
		B[i].set(z);
	    } else if (B[i].test(x)) {
		if (B[i].test(y)) {
		    ok = false;
		    break;
		} else {
		    op[j] = 'y';
		    B[i].set(y);
		}
	    } else {
		if (B[i].test(y)) {
		    op[j] = 'x';
		    B[i].set(x);
		} else {
		    op[j] = '.';
		}
	    }
	}
	if (ok) {
	    REP (j, M) if (op[j] == '.') {
		int x = X[j], y = Y[j];
		if (B[i].test(x)) B[i].set(y);
		else if (B[i].test(y)) B[i].set(x);
	    }
	}
	if (!ok) {
	    B[i].set(i);
	}
    }

    int ans = 0;
    REP (j, N) REP (i, j) {
	if (!B[i].test(i)
		&& !B[i].test(j)
		&& !B[j].test(i)
		&& !B[j].test(j)) {
	    ans++;
	}
    }
    printf("%d\n", ans);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



Measured memory_bytes: 1152
#include<bits/stdc++.h>
using namespace std;
int n,a,b,i;
int main()
{
	cin>>n>>a>>b;
	cout<<min(a,b)<<' ';
	if(a+b<n)
		cout<<"0"<<'\n';
	else
		cout<<abs(n-a-b)<<'\n';
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
using namespace __gnu_pbds; 
#define ll          long long int
#define ull         unsigned long long int
#define pb          push_back
#define pii         pair<int,int>
#define F           first
#define S           second
#define vi          vector<int>
#define vli          vector<ll>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define M_PI        3.14159265358979323846
#define mp          make_pair
#define sz(x)       (int)x.size()
#define matrix      vector<vector<ll>>
#define forn(i,a,b)  for(int i=a;i<b;i++)
#define eps 0.0000000001
#define all(a)      (a).begin(),(a).end()
#define ordered_set     tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> 

const long long INF = (long long) 1e11+1;
const long long mod = (long long) 998244353 ;
const long long hell = (long long) 1000000007;


ll power(ll x,ll y)
{
  ll res=1;
  while(y>0){
    if(y%2) res=(x*res);
    x=(x*x);    
    y=y/2;
  }
  return res;
}

ll modinv(ll x){
  return power(x,hell-2);
}


void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll gcd(ll a,ll b)
{
    while( b != 0)
	{
		ll  t = b;
		b= a %t;
		a = t;
	}
	return a;
}

void solve(){  
    string s;
    cin>>s;
    if(s=="ABC")
    cout<<"ARC";
    else
    cout<<"ABC";
}

int main(){
   fastIO();
   int test;
   //cin>>test;
   test=1;    
   
   while(test--)
   solve();
}

///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1

/*
  For freq or prime factors

for(auto pri:prime){
  while(k%pri==0){
    pcnt[pri]++;
    k/=pri;
  }
}*/

// gcd of strings
/*
string getgcd(string s1,string s2){
    
    while(true){
        if(s1.length()<s2.length()){
            swap(s1,s2);
            continue;
        }
        if(s2.empty())
        return s1;
        
        if(s1.substr(0,s2.length())!=s2)
        return "";
        
        s1=s1.substr(s2.length());
    }
}
*/
/*
// for user defined DS hashmap and set
 
struct hash_pair{
      size_t operator()(const pair<int,pair<int,int>>& p) const{
          auto hash1=hash<int>()(p.F) ;
          auto hash2=hash<int>()(p.S.F) ;
          auto hash3=hash<int>()(p.S.S) ;
          return (hash1^hash2)^hash3;
      }  
};
struct pair_hash
{
	template <class T1, class T2>
	std::size_t operator () (std::pair<T1, T2> const &pair) const
	{
		std::size_t h1 = std::hash<T1>()(pair.first);
		std::size_t h2 = std::hash<T2>()(pair.second);

		return h1 ^ h2;
	}
};
*/



Measured memory_bytes: 3644
@{$_ x3}=1for A..D;
for$i(1..<>-1){
	$x=0;
	for$j(glob'{A,B,C,D}'x3){
		$j=~/../,($$j[$i]+="$_$j"!~/ABC|A.*C.*B|CAB/&&${$_.$&}[$i-1])%=$%=1e9+7for A..D;
		$x+=$$j[-1]
	}
}
print$x%$%


Measured memory_bytes: 1024
h,w=map(int,input().split())
if h%3==0 or w%3==0:
  print(0)
else:
  temp=[w,h]
  if h%2==0 and w%2==0:
    temp.append(abs(int(w//3+1)*h-int(w-w//3-1)*int(h//2)))
    temp.append(abs(int(w//3)*h-int(w-w//3)*int(h//2)))        
    temp.append(abs(int(h//3+1)*w-int(h-h//3-1)*int(w//2)))
    temp.append(abs(int(h//3)*w-int(h-h//3)*int(w//2)))  
  elif h%2==0:
    temp.append(max(int(h//3*w),int(h-h//3)*int(w//2),int(h-h//3)*int(w//2+1))-min(int(h//3*w),int(h-h//3)*int(w//2),int(h-h//3)*int(w//2+1)))
    temp.append(max(int(h//3+1)*w,int(h-h//3-1)*int(w//2),int(h-h//3-1)*int(w//2+1))-min(int(h//3+1)*w,int(h-h//3-1)*int(w//2),int(h-h//3-1)*int(w//2+1)))
    temp.append(abs(int(w//3+1)*h-int(w-w//3-1)*int(h//2)))
    temp.append(abs(int(w//3)*h-int(w-w//3)*int(h//2)))             
  elif w%2==0:
    temp.append(max(int(w//3*h),int(w-w//3)*int(h//2),int(w-w//3)*int(h//2+1))-min(int(w//3*h),int(w-w//3)*int(h//2),int(w-w//3)*int(h//2+1)))
    temp.append(max(int(w//3+1)*h,int(w-w//3-1)*int(h//2),int(w-w//3-1)*int(h//2+1))-min(int(w//3+1)*h,int(w-w//3-1)*int(h//2),int(w-w//3-1)*int(h//2+1)))
    temp.append(abs(int(h//3+1)*w-int(h-h//3-1)*int(w//2)))
    temp.append(abs(int(h//3)*w-int(h-h//3)*int(w//2)))                
  else:
    temp.append(max(int(h//3*w),int(h-h//3)*int(w//2),int(h-h//3)*int(w//2+1))-min(int(h//3*w),int(h-h//3)*int(w//2),int(h-h//3)*int(w//2+1)))
    temp.append(max(int(h//3+1)*w,int(h-h//3-1)*int(w//2),int(h-h//3-1)*int(w//2+1))-min(int(h//3+1)*w,int(h-h//3-1)*int(w//2),int(h-h//3-1)*int(w//2+1)))
    temp.append(max(int(w//3*h),int(w-w//3)*int(h//2),int(w-w//3)*int(h//2+1))-min(int(w//3*h),int(w-w//3)*int(h//2),int(w-w//3)*int(h//2+1)))
    temp.append(max(int(w//3+1)*h,int(w-w//3-1)*int(h//2),int(w-w//3-1)*int(h//2+1))-min(int(w//3+1)*h,int(w-w//3-1)*int(h//2),int(w-w//3-1)*int(h//2+1)))
  print(min(temp))

Measured memory_bytes: 3320
/*
BY: SAFWAAN SHAIKH
PROBLEM:
KEY IDEAS: 
TIME COMPLEXITY: 
*/

#define r(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
#define f first
#define endl "\n"
#define s second
#define pb push_back
#define ms(a,b) memset(a,b,sizeof(a))
#define fori(a,b) for(int i=a;i<b;i++)
#define forj(a,b) for(int j=a;j<b;j++)
typedef long long ll;
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
char _;
ll x,k,d,num;
int main(){
//	freopen("problemname.in", "r", stdin);
//  freopen("problemname.out", "w", stdout);
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>x>>k>>d;
	x = abs(x);
	num = (x +(d/2))/d;
	if(k<=num){
		cout << abs(x-k*d) << endl;
		return 0;
	}
	if(k&1 && num&1){
		cout << abs(x-num*d) << endl;
		return 0;
	}
	if(!(k&1) && !(num&1)){
		cout << abs(x-num*d) << endl;
		return 0;
	}
	ll close = min(abs(x-num*d + d),abs(x-(d*num)-d));
	cout << close << endl;
	return 0;
}



Measured memory_bytes: 3644
n=int(input())
xd=[list(map(int,input().split())) for _ in range(n)]
xd.sort(key=lambda x:x[0])
aryx=[x for x,d in xd]
mod=998244353
dp=[1]*(n+1)
#dp[n-1]=2
r=list(range(n))
r[n-1]=n-1
from bisect import bisect_left

class SegmentTree:
    # 初期化処理
    # f : SegmentTreeにのせるモノイド
    # default : fに対する単位元
    def __init__(self, size, f=lambda x,y : max(x,y), default=0):
        self.size = 2**(size-1).bit_length() # 簡単のため要素数Nを2冪にする
        self.default = default
        self.dat = [default]*(self.size*2) # 要素を単位元で初期化
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres) # モノイドでは可換律は保証されていないので演算の方向に注意
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res
st=SegmentTree(n)
for i in range(n):
    st.update(i,i)

for i in range(n):
    x,d=xd[n-1-i]
    tmp=bisect_left(aryx,x+d)
    sti=st.query(n-1-i,tmp)
    st.update(n-1-i,sti)
    dp[n-1-i]=dp[n-1-i+1]+dp[sti+1]
    dp[n-1-i]%=mod
print(dp[0])


Measured memory_bytes: 133460
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    X = LI()
    ans = float("inf")
    for x in range(101):
        m = 0
        for xi in X:
            m += (xi-x)**2
        if m < ans:
            ans = m
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()


Measured memory_bytes: 38896
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int a,b;
    int c;
    int e;
    cin>>a>>b;
    c=b*2;
    e=a-c;
    if(c>=a)
    {
        cout<<0;
    }
    if(a>c)
    {
        cout<<e;
    }
 
 
 
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#include<list>
#include<stack>
#include<cstdio>
#include<fstream>
#include<numeric>
#include<functional>
#include<utility>
#include<memory>
#include<cstring>
#include<cmath>
using namespace std;

#define CLEAN(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;

int main()
{	
	int a,b;
	cin>>a>>b;
	if (a>8 || b>8) cout<<":("<<endl;
	else cout<<"Yay!"<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>


int main(void){
	int a,b,x;
	while(scanf("%d %d",&a,&b)!=EOF){
		x=a+b;
		if(x/1000000){
			printf("7\n");
		} else if (x/100000!=0){
			printf("6\n");
		} else if (x/10000!=0){
			printf("5\n");
		} else if (x/1000!=0){
			printf("4\n");
		} else if (x/100!=0){
			printf("3\n");
		} else if (x/10!=0){
			printf("2\n");
		} else {
			printf("1\n");
		}
	}
}

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

using namespace std;

#define int long long
#define ll long long
#define double long double
#define f first
#define s second
#define all(el) el.begin(), el.end()
#define allr(el) el.rbegin(), el.rend()
//#define x first
//#define y second
#define pb push_back
#define in insert
#define mp make_pair
#define sz(el) (int)el.size()
#define endl "\n"

signed main() {
    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n;
    cin >> n;
    if (n % 10 == 3) {
        cout << "bon";
        return 0;
    }
    if (n % 10 == 0 || n % 10 == 1 || n % 10 == 6 || n % 10 == 8) {
        cout << "pon";
        return 0;
    }
    cout << "hon";
    
    return 0;
    
}


Measured memory_bytes: 3608
#include <bits/stdc++.h>
#define INF 900000000000
#define MAXN 110
#define MAXV 110000
using namespace std;

long long N, W;
long long dp[MAXV];
int v[MAXN];
int w[MAXN];

int main()
{
    fill(dp, dp + MAXV, INF);
    cin >> N >> W;
    int maxx = 0;
    for (int i = 1; i <= N; i++)
    {
        cin >> w[i] >> v[i];
        maxx = max(v[i], maxx);
    }
    int maxv = maxx * N + 1;
    dp[0] = 0;
    for (int i = 1; i <= N; i++)
    {
        for (int j = maxv; j >= v[i]; j--)
        {
            dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
        }
    }
    for (int i = maxv; i >= 0; i--)
    {
        if (dp[i] <= W)
        {
            cout << i;
            return 0;
        }
    }
}

Measured memory_bytes: 1152
#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;
}

int main()
{
  string N;
  cin >> N;
  ll ans = 0;
  REP(i,sz(N)){
    ans+=N[i]-'0';
  }

  if(ans%9==0){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}

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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

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

    map<char, char> m0, m1;
    ll n = s.length();
    bool result = true;
    bool used[26] = {};
    rep(i, n) {
        if (m0[s[i]] == '\0') {
            m0[s[i]] = t[i];
        } else if (m0[s[i]] != t[i]) {
            result = false;
            break;
        }
        if (m1[t[i]] == '\0') {
            m1[t[i]] = s[i];
        } else if (m1[t[i]] != s[i]) {
            result = false;
            break;
        }
    }

    if (result) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}



Measured memory_bytes: 904
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class Hello
{
    public static void Main()
    {
        int[] n = Console.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        int ans = (n[0] + n[1]) / 2;
        if ((n[0] + n[1]) % 2 != 0) ans += 1;
        Console.WriteLine(ans);
    }
}


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

using namespace std;

int n,m;
int a[310][310];
int pp[310],np[310],xz[310];
int nu[310];
int ans;
vector<int> tmp[310];
void change(int j,int x)
{
	pp[j]=1;
	for (int k=0;k<tmp[j].size();k++)
	{
		int d=tmp[j][k];
		if (np[tmp[j][k]]!=j) continue;
		np[tmp[j][k]]=0;
		for (int l=1;l<=m;l++)
			if (!pp[a[d][l]])
			{
				tmp[a[d][l]].push_back(d);
				nu[a[d][l]]++;
				np[d]=a[d][l];
				if (nu[a[d][l]]>x) change(a[d][l],x);
				break;
			}
	}
	tmp[j].clear();
}
int check(int x)
{
	memset(pp,0,sizeof(pp));
	memset(np,0,sizeof(np));
	memset(nu,0,sizeof(nu));
	for (int i=1;i<=m;i++)
		tmp[i].clear();
	for (int i=1;i<=m;i++)
	{
		for (int j=1;j<=n;j++)
			if (np[j]) continue;
			else if (!pp[a[j][i]])
			{
				nu[a[j][i]]++;
				tmp[a[j][i]].push_back(j);
				np[j]=a[j][i];
				if (nu[a[j][i]]>x) change(a[j][i],x);
			}
	}
	int res=n;
	for (int i=1;i<=m;i++)
		res-=tmp[i].size();
	return res==0;
}
		
int main()
{
	scanf("%d %d",&n,&m);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			scanf("%d",&a[i][j]);
	int l=1,r=n;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if (check(mid)) ans=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%d\n",ans);
	return 0;
}

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

int main() {
  long long n,a,b;
  cin>>n>>a>>b;
  long long shou=a+b+(n-2)*a;
  long long dai=a+b+(n-2)*b;
  if(n==1){
    if(a!=b)cout<<0<<endl;
    else cout<<1<<endl;
  }else if(n==2)cout<<a+b<<endl;
  else cout<<max((long long) 0,dai-shou+1)<<endl;
  return 0;
}


Measured memory_bytes: 256
# -*- coding: utf-8 -*-
import math
N = int(input())
graph = [{} for _ in range(N)]

for _ in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    graph[u][v] = w
    graph[v][u] = w

d = [-1 for _ in range(N)]
src = 0
d[src] = 0
buf = [src]

while buf:
    src = buf.pop()
    for dst, w in graph[src].items():
        if d[dst] == -1:
            d[dst] = d[src] + w
            buf.append(dst)

for n in d:
    if n % 2:
        print(0)
    else:
        print(1)

Measured memory_bytes: 86432
#include <bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define x first
#define y second
#define pb push_back
#define ll long long
#define int ll
#define ld long double
#define ii pair<int, int>
#define iii pair<int, ii>
#define vi vector<int>
#define vii vector<ii>
#define pq priority_queue
using namespace std;
const int ms = 4000;
const int mod = 1e9+7;
const int inf = 1e9 + 100;
const int sigma = 30;
int n, m, k, t = 1;
string a, b;

int dp[ms][ms];
int seen[ms][ms];

int sol(int i, int j){
  if(i<0 || j<0) return 0;
  
  if(seen[i][j]) return dp[i][j];
  seen[i][j] = 1;

  if(a[i]==b[j]) return dp[i][j] =  1 + sol(i-1, j-1);
  return dp[i][j] = max(sol(i-1, j), sol(i, j-1));
}


void solve(){
  cin>>a>>b;
  n = a.size(), m = b.size();
  string ans;
  sol(n-1, m-1);
  int i = n-1, j = m-1;
  while(i>=0 && j>=0){
    if(a[i]==b[j]){
      ans+=a[i];
      i--;
      j--;
    }else{
      if(i>0 && dp[i][j]==dp[i-1][j]){
        i--;
      }else{
        j--;
      }
    }
  }
  reverse(all(ans));
  cout<<ans<<endl;
}

int32_t main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  while(t--){
    solve();
  }
}

Measured memory_bytes: 188544
#include <iostream>
#include <stdio.h>

using namespace std;

int main(void){
  int deta;
  //データ数をもらう
  scanf("%d", &deta);

  //データ数だけ３辺の値をもらう
  for(int i = 0; i < deta; i++){
    int hen1, hen2, hen3;
    scanf("%d", &hen1);
    scanf("%d", &hen2);
    scanf("%d", &hen3);

    //直角三角形の判定
    if(hen1*hen1 + hen2*hen2 == hen3*hen3 ||
       hen2*hen2 + hen3*hen3 == hen1*hen1 ||
       hen3*hen3 + hen1*hen1 == hen2*hen2){
      printf("YES\n");
    }else{
      printf("NO\n");
    }

    
  }


  return 0;
}

Measured memory_bytes: 1168
#include <iostream>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
using namespace std;

int main()
{
	string s, t;
	cin >> s >> t;
	if(s == t.substr(0,s.size()))
		cout << "Yes";
	else
		cout << "No";

}

Measured memory_bytes: 3624
#include<iostream>
#include<string>
#include<math.h>
#include<vector>
#include<algorithm>

#define ll long long
#define vl vector<ll>
#define vvl vector< vector<ll> >
using namespace std;

int main(){
	ll a, b, ans; cin >> a >> b;
	if (max(a, b) > min(a, b)) ans += 2 * max(a, b) - 1;
	else ans = 2 * max(a, b);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <bitset>
#include <numeric>
#include <complex>
#include <cassert>
#include <random>
using namespace std;
using ll = long long;


int main() {
	int n; cin >> n;
	vector<ll> a(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	vector<vector<ll>> dp(n + 2, vector<ll>(3));
	for (int i = 0; i < n; i++) {
		dp[i][0] = (i >= 2 ? dp[i - 2][0] : 0) + a[i];
		dp[i][1] = max({
			(i >= 2 ? dp[i - 2][1] : 0) + a[i],
			(i >= 3 ? dp[i - 3][0] : 0) + a[i]
		});
		dp[i][2] = max({
			(i >= 2 ? dp[i - 2][2] : 0) + a[i],
			(i >= 3 ? dp[i - 3][1] : 0) + a[i],
			(i >= 4 ? dp[i - 4][0] : 0) + a[i]
		});
	}
	if (n & 1)
		cout << max({ dp[n - 1][0] - min(a[0], a[n - 1]), dp[n - 1][1], dp[n - 1][2],
			dp[n - 2][0], dp[n - 2][1] }) << endl;
	else
		cout << max({ dp[n - 1][0], dp[n - 1][1], dp[n - 2][0] }) << endl;
	return 0;
}

Measured memory_bytes: 15632
#include<stdio.h>
int main(){
  int n,i,m,d[10110]={1};
  scanf("%d",&n);
  while(n--){
    scanf("%d",&m);
    for(i=10010;i--;)d[i+m]|=d[i];
  }
  for(i=m=0;i<10010;i++){
    if(d[i]==0||i%10==0)continue;
    m=m<i?i:m;
  }
  printf("%d\n",m);
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
ans=0

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

ndivs = make_divisors(n)

n1divs = make_divisors(n-1)
ans += len(n1divs)-1

for i in range(1,len(ndivs)):
    tmp = n
    k = ndivs[i]
    while tmp%k==0:
        tmp //=k
    if tmp%k==1:ans+=1

print(ans)


Measured memory_bytes: 3188
def solve():
    N,M = map(int,input().split())
    if N == 1 or M == 1:
        if N==2 or M ==2:
            print(0)
        else:
            print(max(1, max(N,M)-2))
    else:
        print(N*M - (N+M+M+N-4))



if __name__ == '__main__':
    solve()


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

int main() {
	string s;
	int size, k;
	cin >> size >> k >> s;
	transform(s.begin() + k - 1, s.begin() + k, s.begin() + k - 1, ::tolower);
	cout << s << endl;
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>


typedef struct student {
	int x;
	int y;
} STUDENT;

typedef struct point {
	int x;
	int y;
} POINT;


int calculateDistance( STUDENT s, POINT p );
int searchPoint( STUDENT student, POINT *pointList , int M );


int main(){
	
	int N, M;
	scanf("%d %d", &N, &M);
	
	STUDENT studentList[N+1];
	POINT pointList[M+1];
	
	int i;
	for( i = 1; i < N+1; i++){
		scanf("%d %d", &(studentList[i].x), &(studentList[i].y) );
	}
	
	for( i = 1; i < M+1; i++){
		scanf("%d %d", &(pointList[i].x), &(pointList[i].y) );
	}
	
	for( i = 1; i < N+1; i++){
		printf("%d\n", searchPoint( studentList[i], pointList , M ) );
	}
	
	return 0;
	
}


int searchPoint( STUDENT student, POINT *pointList , int M ){
	
	int result = 1;
	int min = calculateDistance( student, pointList[1] );
	int i;
	for( i = 2; i < M+1; i++){
		
		int value = calculateDistance( student, pointList[i] );
		if( value < min ){
			result = i;
			min = value;
		}
		
	}
	
	return result;
	
}


int calculateDistance( STUDENT s, POINT p ){
	return ( abs( s.x - p.x ) + abs( s.y - p.y ) );	
}

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

#----------
N,M = list(map(int, input().rstrip().split()))
#----------
M_dic={}

for i in range(1,M+1):
    M_dic[i]=0

for i in range(N):
    K,*A_list = list(map(int, input().rstrip().split()))
    
    for a in A_list:
        M_dic[a] += 1

count=0
for m_v in M_dic.values():
    if m_v == N:
        count += 1

print(count)


Measured memory_bytes: 3060
#include<iostream>  
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>   
#include<cmath> 
#include<vector>  
#include <bitset>  
#include<algorithm>    
#include <queue>  
#include<map> 
#define inf 9999999; 
using namespace std;

long long int MIN,a[200005],cnt[200005];
int i,n;
int main()
{
	cin>>n;
	for(i=1;i<=n;i++) cin>>a[i];
	for(i=1;i<=n;i++)
	{
		cnt[i]=cnt[i-1]+a[i];
	}
	MIN=10000000000;
	//cout<<MIN<<endl;
	for(i=1;i<n;i++)
	{
		//cout<<cnt[i]<<" "<<cnt[n]-cnt[i]<<endl;
		MIN=min(MIN,abs(cnt[i]-(cnt[n]-cnt[i])));
	}
	cout<<MIN<<endl;
}

Measured memory_bytes: 3328
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <iomanip>

#include <map>
#include <set>
#include <queue>
#include <stack>

#define ALL(a) (a).begin(),(a).end()
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define rrep(i,n) RFOR(i,n,0)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdbl vector<double>
#define vstr vector<string>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

	ll N,T;
	cin >> N >> T;

	ll t[N];
	rep(i,N) cin >> t[i];

	ll ans = 0;

	rep(i,N-1){
		ll d = t[i+1] - t[i];
		ans += min(d,T);
	}
	ans += T;

	cout << ans << endl;
}

Measured memory_bytes: 1792
n = int(input())
kotae = 0
if n == 100000:
  kotae = 90909
elif n >= 10000:
  kotae = 909 + (n - 10000 + 1)
elif n >= 1000:
  kotae = 909
elif n >= 100:
  kotae = 9 + (n- 100 +1)
elif n >= 10:
  kotae = 9
else:
  kotae = n
print(kotae)

Measured memory_bytes: 3060
#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<int,int>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
const int MAX_V = 20010;
struct Flow{
//辺の構造体　
struct edge{
    //行き先、容量、逆辺の場所
    ll to,cap,rev;
};
//グラフの隣接リスト
V<edge> G[MAX_V];//vectorの"配列"
//DFSで調べた頂点を記録
bool used[MAX_V];
//辺の情報を代入する関数
void add(ll from,ll to,ll cap){
    G[from].push_back((edge){to,cap,(ll)G[to].size()});
    G[to].push_back((edge){from,0,(ll)G[from].size()-1});
}
//増加パスをDFSで探す
ll dfs(ll v,ll t,ll f){
    if(v==t)return f;
    used[v]=true;
    for(int i=0;i<G[v].size();i++){
        edge &e=G[v][i]; //辺の容量を更新するためのアドレス
        if(!used[e.to]&&e.cap>0){
            ll d=dfs(e.to,t,min<ll>(f,e.cap));
            if(d>0){
                e.cap-=d;//使った分減らす
                G[e.to][e.rev].cap+=d;//使った分逆辺の容量を増やす
                return d;
            }
        }
    }
    return 0;
}
//sからtへの最大流を求める
ll fmax(ll s,ll t){
   ll flow=0;
   while(1){
       for(int i=0;i<MAX_V;i++)used[i]=false;
       ll f=dfs(s,t,10e9);
       if(f==0)return flow;
       flow+=f;
   }
}
};
ll h,w;
ll in_id(ll x,ll y){
    return (x*w+y)*2;
}
ll out_id(ll x,ll y){
    return (x*w+y)*2+1;
}
int main(){
    cin>>h>>w;
    vector<string> s(h);
   int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
    for(int i=0;i<h;i++)cin>>s[i];
    Flow mf;
    ll S=h*w*2,T=S+1;
     for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
           if(s[i][j]=='X'){
                if(i==0||j==0||i==h-1||j==w-1){
                      cout<<-1<<endl;
                      return 0;
                  }
                mf.add(out_id(i,j),T,1e5);
                mf.add(in_id(i,j),out_id(i,j),1e5);
            }
           else{ 
              mf.add(in_id(i,j),out_id(i,j),1);
           }
             for(int k=0;k<4;k++){
                int nx=dx[k]+i,ny=dy[k]+j;
                 if(nx>=0&&ny>=0&&nx<h&&ny<w){
                      mf.add(out_id(i,j),in_id(nx,ny),1e5);
                 } 
                  else mf.add(S,in_id(i,j),1);
            }
          }
 }
   ll ans=mf.fmax(S,T);
   if(ans>=1e5)cout<<-1<<endl;
   else cout<<ans<<endl;
}

Measured memory_bytes: 4736
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> T;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
template<class T> void chmin(T& a,const T& b){if(a>b)a=b;}
template<class T> void chmax(T& a,const T& b){if(a<b)a=b;}

#define MOD 1000000007ll // prime
// How to devide :
// ModInt a(6ll);
// ModInt b(2ll);
// a *= b.exp(MOD-2ll);  -> a/=b;  result: a = 3
struct ModInt{
	ll val;
	ModInt():val(0ll){}
	ModInt(ll v):val(((v%MOD)+MOD)%MOD){}
	ModInt exp(ll y)const{
		if(!y)return ModInt(1ll);
		ModInt a = exp(y/2ll);
		a *= a;
		if(y&1)a*=(*this);
		return a;
	}
	bool operator==(const ModInt& x)const{return val==x.val;}
	inline bool operator!=(const ModInt& x)const{return !(*this==x);}
	bool operator<(const ModInt& x)const{return val<x.val;}
	bool operator>(const ModInt& x)const{return val>x.val;}
	inline bool operator>=(const ModInt& x)const{return !(*this<x);}
	inline bool operator<=(const ModInt& x)const{return !(*this>x);}
	ModInt& operator+=(const ModInt& x){if((val+=x.val)>=MOD)val-=MOD;return *this;}
	ModInt& operator-=(const ModInt& x){if((val+=MOD-x.val)>=MOD)val-=MOD;return *this;}
	ModInt& operator*=(const ModInt& x){(val*=x.val)%=MOD;return *this;}
	ModInt operator+(const ModInt& x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt& x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt& x)const{return ModInt(*this)*=x;}
};
istream& operator>>(istream&i,ModInt&x){i>>x.val;return i;}
ostream& operator<<(ostream&o,const ModInt&x){o<<x.val;return o;}
ModInt pow(ModInt a,ll x){
	ModInt res = ModInt(1ll);
	while(x){
		if(x&1)res *= a;
		x >>= 1;
		a = a*a;
	}
	return res;
}
const int SIZE = 200100;
ModInt inv[SIZE+10],fac[SIZE+10],facinv[SIZE+10];
// notice: 0C0 = 1 
ModInt nCr(int n,int r){
	assert(!(n<r));
	assert(!(n<0||r<0));
	return fac[n]*facinv[r]*facinv[n-r];
}
void init(){
	fac[0]=ModInt(1ll);
	for(int i=1;i<=SIZE;i++)fac[i]=fac[i-1]*ModInt(i);
	inv[1]=ModInt(1ll);
	for(int i=2;i<=SIZE;i++)inv[i]=ModInt(0ll)-ModInt(MOD/i)*inv[MOD%i];
	facinv[0]=ModInt(1ll);
	for(int i=1;i<=SIZE;i++)facinv[i]=facinv[i-1]*inv[i];
	return;
}
int N,M,K;
int main(){
	init();
	cin >> N >> M >> K;
	ModInt ans;
	for(int i=1;i<N;i++){
		ans += ModInt((ll)i*(ll)(N-i))*ModInt((ll)M*(ll)M)*nCr(N*M-2,K-2);
	}
	for(int i=1;i<M;i++){
		ans += ModInt((ll)i*(ll)(M-i))*ModInt((ll)N*(ll)N)*nCr(N*M-2,K-2);
	}
	cout << ans << endl;
    return 0;
}

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

#define fastio 			ios::sync_with_stdio(0);	cin.tie(0); cout.tie(0);  cout<<fixed;  cout<<setprecision(12);
#define randomINT 		mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define	newl 			cout<<"\n"
#define DISP(as)		for(auto it : as) cout<<it<<" ";newl;
#define all(x)         	(x).begin(),(x).end()
#define mset(x,val)    	memset(x,val,sizeof(x))
#define newl           	cout<<"\n"
#define pb             	push_back
#define mp             	make_pair
#define f 				first
#define s 				second
#define dline          	cerr<<"///REACHED///\n";
#define deb1(x)        	cerr<<#x<<" = "<<x<<'\n';
#define deb2(x,y)      	cerr<<'['<<#x<<','<<#y<<"] = "<<'['<<x<<','<<y<<']'<<'\n';
#define deb3(x,y,z)    	cerr<<'['<<#x<<','<<#y<<','<<#z<<"] = "<<'['<<x<<','<<y<<','<<z<<']'<<'\n';
//#define N 1000000007 // prime modulo value 

typedef long long 				ll;
typedef long double 			ld;
typedef vector<ll> 				vll;
typedef pair<ll , ll> 			pll;
typedef pair<ld, ld> 			pld;
typedef unordered_map<ll, ll> 	um;
typedef vector<pll> 			vpll;

const ll 	MAX5 	= 	1e+5 + 7;
const ll 	MAX7 	= 	1e+7 + 7;
const ll 	MAXN 	= 	MAX7;
const ll   	INF   	=  	0x7f7f7f7f7f7f7f7f;
const ll   	N   	=  	2*MAX5;
const int  	INFi 	=  	0x7f7f7f7f;
const ll 	MOD		=  	1e9 + 7;


// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, x, k, m, y;
string s, t;
 vector<ld> a;
// <------------- Implement Functions Here ----------> //

ll bs() {
	ll l = 1, r = 1e9;
	while(l < r) {
		ll m = (l+r)/2;
		ll ans = 0;
		for(ll i=0;i<n;i++) {
			ans += max((ld)0,ceil(a[i]/m)-1);
		}
		//cout<<m<<" "<<ans;newl;
		if(ans > k) l=m+1;
		else r=m;
	}
	return l;
}

// <------------- Start of main() -------------------> //

void MAIN() {
	cin >> n >> k;
	//multiset<ld,greater<ld>> val;
	a = vector<ld>(n);
	ll sum = 0;
	for(ll i=0;i<n;i++) {
		cin >> a[i];
	}
	
	cout<<bs();
}	

int main() {
	fastio; randomINT;
	//cin >> T;
	while (T--) {
		MAIN();
	}
	return 0;
}

Measured memory_bytes: 6324
#include <iostream>
#include <cstdlib>
#include <utility>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
using Graph = vector< vector< pair<int, int> > >;

const int INF = 1000010;
struct UnionFind {
private:
  int uf[110];

public:
  UnionFind() {
    memset(uf, -1, sizeof(uf));
  }

  int find(int x) {return (uf[x] < 0) ? x : uf[x] = find(uf[x]);}
  bool same(int x, int y) {return find(x) == find(y);}
  int size(int x) {return -uf[find(x)];}
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return;
    if(uf[y] < uf[x]) swap(x, y);
    uf[x] += uf[y]; uf[y] = x;
  }
};

int main() {
  int N, M;
  while(cin >> N >> M, N) {
    UnionFind uf;
    Graph G(10010); // categorized by cost
    int ma = 0;
    for(int i=0; i<M; i++) {
      int a, b, c; cin >> a >> b >> c;
      a--; b--;
      uf.unite(a, b);
      G[c].push_back(make_pair(a, b));
      ma = max(ma, c);
    }
    if(uf.size(0) != N) cout << -1 << endl;
    else {
      // solve
      int ans = INF;
      for(int min_cost=1; min_cost<=ma; min_cost++) {
	int lb = min_cost - 1, ub = ma + 10;
	while(ub - lb > 1) {
	  int mid = (lb + ub) / 2;
	  UnionFind uni;
	  for(int i=min_cost; i<=mid; i++) {
	    for(auto x : G[i]) {
	      int from = x.first, to = x.second;
	      uni.unite(from, to);
	    }
	  }
	  
	  // if(uni.size(0) == N) printf("min_cost = %d, mid = %d, size = %d\n", min_cost, mid, uni.size(0));
	  if(uni.size(0) == N) ub = mid;
	  else lb = mid;
	}
	if(ub == ma + 10) continue;
	ans = min(ans, ub - min_cost);
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 3436
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = INT32_MAX;
const ll INFL = INT64_MAX;
const int mod = 1e9 + 7;

template <typename T>
void print_vec(vector<T> v) {
  for (T i : v) cout << i << " ";
  cout << endl;
}
void printp(vector<P> v) {
  for (P i : v) printf("%d,%d ", i.first, i.second);
  cout << endl;
}

const int MAX_N = 1e5 + 1;
vector<P> to[MAX_N];

int main() {
  int n;
  cin >> n;
  vector<int> colors = vector<int>(n, -1);
  rep(i, n - 1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    to[u].push_back(P(v, w));
    to[v].push_back(P(u, w));
  }
  colors[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (P p : to[u]) {
      int v = p.first;
      int w = p.second;
      if (colors[v] != -1) continue;
      if (w % 2 == 0)
        colors[v] = colors[u];
      else
        colors[v] = (colors[u] + 1) % 2;
      q.push(v);
    }
  }
  rep(i, n) cout << colors[i] << endl;
  return 0;
}

Measured memory_bytes: 9616
import java.util.Scanner;
class Main{
	public static void main(String[]args) {
	Scanner scan=new Scanner(System.in);
	String str=scan.next();
	long b=scan.nextLong();
	int count=0;
	for(int i=0;i!=str.length();i++) {
		char ch=str.charAt(i);
		if(ch!='1') {
		break;
		}
		count++;
	}
	if(b<=count) {
		System.out.println(1);
		return;
	}
	for(int i=0;i<str.length();i++) {
		char ch=str.charAt(i);
		if(ch!='1') {
			System.out.println(ch);
			return;
		}
	}
	System.out.println(1);
	}
}

Measured memory_bytes: 23252