akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
  int n,p;
  while(cin>>n>>p, n!=0 || p!=0){
    int A[n];
    memset(A, 0, sizeof(A));
    int t=0;
    while(1){
      if(p==0){
        p=A[t];
        A[t]=0;
      } else {
        p-=1;
        A[t]+=1;
      }
      int f=1;
      rep(i,n) {
        if(t!=i && A[i]!=0) f=0;
      }
      if(p==0 && f) break;
      t=(t+1)%n;
    }
    cout<<t<<endl;
  }
  return 0;
}

Measured memory_bytes: 3056
#include <iostream>

using namespace std;

int main(){
	int N,K;
	
	cin >> N >> K;
	
	
	long int nK=N/K;
	
	
	if(K%2==0){
		
		long int nK_halfK=((int)(N/(K/2)))-nK;
		
		cout << nK*nK*nK + nK_halfK*nK_halfK*nK_halfK;
		
	}else{
		cout << nK*nK*nK;
		
	}
	
	return 0;
}




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

int main(){
    ll w, h, x, y;
    cin >> w >> h >> x >> y;
    ll d = 0;
    if(w/2.0 == x && h/2.0 == y) d = 1;
    printf("%.9lf %lld\n", w*h/2.0, d);
    return 0;
}

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

#define ed cout << "\n";
#define speed                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ll long long
#define ffor(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(i, n) ffor(i, 0, n)
#define All(obj) (obj).begin(), (obj).end()
#define INF ((ll)1e9 + 7)
#define c(a) cout << a << "\n";
using namespace std;
const int N = 100005;

int main()
{
    speed;
    cout << fixed << setprecision(6);
    /*start*/
    int n;
    cin >> n;
    if(n==0||n==1||n==2||n==3||n==5||n==6||n==9||n==10||n==13||n==17){
        c("No");
    }else{
        c("Yes");
    }
    ed;
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
	int a,b,c;
	
	scanf("%d %d %d",&a,&b,&c);
	
	if(c >= a && c <= b){
		printf("Yes\n");
	}
	else {
		printf("No\n");
	}
	
	return(0);
}


Measured memory_bytes: 128
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <iterator>
#include <cmath>
#include <set>
#include <deque> 
#include <string>


using namespace std;

int x, a;

int main()
{
	scanf("%d%d", &x, &a);
	
	if (x >= a)
		printf("10\n");
	else
		printf("0\n");
	
	return 0;
}



Measured memory_bytes: 128
#include <iostream>

using namespace std;
typedef long long ll;
int n = 0;

int PrimeTest(int n)
{
    for (int i = 2; i * i <= n; ++i)
    {
        if(n % i == 0)
        {
            return 0;
        }
    }
    return 1;
}

int main(){
    cin >> n;
    int t = 1;
    if(PrimeTest(n)){
        cout << n << endl;
        return 0;
    }
    ++n;
    while(t != 0){
        if(PrimeTest(n)){
            t = 0;
            continue;
        }
        ++n;
    }
    cout << n << endl;
    
    return 0;
}

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  vector<int> sum(n);
  for (int i = 0; i < k; i++) {
    sum[0] += p[i];
  }
  int smax = sum[0];
  for (int i = 0; i < n - k; i++) {
    sum[i + 1] = sum[i] - p[i] + p[i + k];
    smax = max(smax, sum[i + 1]);
  }
  int ans = smax + k;
  if (ans % 2 == 0) {
    cout << ans / 2 << '\n';
  } else {
    cout << to_string(ans / 2) + ".5" << '\n';
  }
  return 0;
}

Measured memory_bytes: 4760
#include "bits/stdc++.h"

using namespace std;

#define print(s) cout << (s) << endl;
#define sp(x) cout<<setprecision(x);
#define rep(i,n)  for (int i = 0; i < (n); i++)
#define all(a) a.begin(), (a).end()
#define gre(t) greater<t>()
#define inf 9876543210
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    double t, a;
    cin >> n >> t >> a;
    vector<double> h(n);
    rep(i,n){
        cin >> h[i];
    }
    int ans;
    double best = inf;
    double tem = inf;
    rep(i,n){
        tem = abs(a-(t-(h[i]*0.006)));
        if(tem < best){
           best = tem; 
          ans  = i+1;
        }
    }
    print(ans);
}

Measured memory_bytes: 256
#include<stdio.h>
int dif(int a, int b);
int N, M, A[50], B[50], C[50], D[50], min, i, j;
int main(){
  scanf("%d%d", &N, &M);
  for(i=0; i<N; i++){
    scanf("%d%d", A+i, B+i);
  }
  for(j=0; j<M; j++){
    scanf("%d%d", C+j, D+j);
  }
  for(i=0; i<N; i++){
    min = 0;
    for(j=0; j<M; j++){
      if(dif(A[i], C[j])+dif(B[i], D[j]) < dif(A[i], C[min])+dif(B[i], D[min])){
        min=j;
      }
    }
    printf("%d\n", min+1);
  }
}
int dif(int a, int b){
  return a<b?b-a:a-b;
}


Measured memory_bytes: 128
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[5],k,i;
    for(i=0;i<5;i++)
        cin>>a[i];
    cin>>k;
    sort(a,a+5);
    if(a[4]-a[0]<=k)
        cout<<"Yay!";
    else
        cout<<":(";
    return 0;
}


Measured memory_bytes: 256
#クラスカル法(最小全域木)　計算量O(|E|log|V|)
#V:頂点の集合 E:辺の集合

class Kruskal_UnionFind():
    # 無向グラフであるという前提に注意
    def __init__(self, N):
        self.edges = []
        self.rank = [0] * N
        self.par = [i for i in range(N)]
        self.counter = [1] * N

    def add(self, u, v, d):
        """
        u = from, v = to, d = cost
        """
        self.edges.append([u, v, d])

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            z = self.counter[x] + self.counter[y]
            self.counter[x], self.counter[y] = z, z
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def size(self, x):
        x = self.find(x)
        return self.counter[x]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def Kruskal(self):
        """
        return: 最小全域木のコストの和
        """
        edges = sorted(self.edges, key=lambda x: x[2])  # costでself.edgesをソートする
        res = 0
        for e in edges:
            if not self.same(e[0], e[1]):
                self.unite(e[0], e[1])
                res += e[2]
        return res

n=int(input())
XY=[[i]+list(map(int, input().split())) for i in range(n)]

graph=Kruskal_UnionFind(n)

XY=sorted(XY,key=lambda x: x[1])
# [[1, 4, 9], [5, 7, 6], [0, 8, 3], [2, 12, 19], [4, 13, 5], [3, 18, 1]]
X_costs=[[XY[i-1][0],XY[i][0],abs(XY[i-1][1]-XY[i][1])] for i in range(1,n)]
#[[1, 5, 3], [5, 0, 1], [0, 2, 4], [2, 4, 1], [4, 3, 5]]
XY=sorted(XY,key=lambda x: x[2])
Y_costs=[[XY[i-1][0],XY[i][0],abs(XY[i-1][2]-XY[i][2])] for i in range(1,n)]

for i in range(n-1):
    x0,x1,d=X_costs[i]
    graph.add(x0,x1,d)
    y0,y1,d=Y_costs[i]
    graph.add(y0,y1,d)
print(graph.Kruskal())

Measured memory_bytes: 125528
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class State
{
public:
    int y, x, hp, time;
    State(int y0, int x0, int hp0, int time0){
        y = y0;
        x = x0;
        hp = hp0;
        time = time0;
    }
};

int main()
{
    int dy[] = {0, 0, 1, -1};
    int dx[] = {1, -1, 0, 0};

    for(;;){
        int w, h;
        cin >> w >> h;
        if(w == 0)
            return 0;

        int sy, sx;
        vector<vector<int> > grid(h+2, vector<int>(w+2, 0));
        for(int i=1; i<=h; ++i){
            for(int j=1; j<=w; ++j){
                cin >> grid[i][j];
                if(grid[i][j] == 2){
                    sy = i;
                    sx = j;
                    grid[i][j] = 0;
                }
            }
        }

        vector<vector<vector<bool> > > check(h+2, vector<vector<bool> >(w+2, vector<bool>(7, false)));
        queue<State> q;
        check[sy][sx][6] = true;
        q.push(State(sy, sx, 6, 0));
        bool ok = false;
        while(!q.empty()){
            State s = q.front();
            q.pop();

            for(int i=0; i<4; ++i){
                int y = s.y + dy[i];
                int x = s.x + dx[i];
                int hp = s.hp - 1;
                if(hp == 0 || grid[y][x] == 0)
                    continue;
                if(grid[y][x] == 3){
                    cout << (s.time + 1) << endl;
                    ok = true;
                    break;
                }
                if(grid[y][x] == 4)
                    hp = 6;
                if(check[y][x][hp])
                    continue;
                check[y][x][hp] = true;
                q.push(State(y, x, hp, s.time+1));
            }

            if(ok)
                break;
        }

        if(!ok)
            cout << -1 << endl;
    }
}

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

ll gcd(ll x, ll y){
	if(y == 0) return x;
	
	return gcd(y, x%y);
}

ll lcm(ll x, ll y){
	return x*y/gcd(x,y);
}



ll soinsu(ll n){
	int res = 0;
	while(n%2 == 0){
		n /= 2;
		res++;
	}
	return res;
}

int main(){
	ll N,M;
	cin >> N >> M;
	vector<ll> a(N);
	rep(i,N){
		cin >> a[i];
	}
	//全部2で割る。
	rep(i,N){
		a[i] /= 2;
	}
	
	vector<int> temp(N);
	rep(i,N){
		temp[i] = soinsu(a[i]);
	}
	
	bool ok = true;
	rep(i,N){
		if(temp[0] != temp[i]){
			ok = false;
		}
	}
	
	if(ok){
		//答えはa[i]/2の最小公倍数の奇数倍
		ll val = a[0];
		rep(i,N-1){
			val = lcm(val,a[i+1]);
		}
		if(val > M){
			cout << 0 << endl;
		}else{
			cout << (M/val+1)/2 << endl;
		}
		
	}else{
		cout << 0 << endl;
	}
	
	
    return 0;
}

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

template <typename InputIterator>
typename InputIterator::value_type summation(InputIterator first,
                                             InputIterator last) {
  return std::accumulate(first, last, typename InputIterator::value_type());
}
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v) {
  for (auto &i : v) {
    stream >> i;
  }
  return stream;
}
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p) {
  stream >> p.first >> p.second;
  return stream;
}

int main() {
  int a, b;
  std::cin >> a >> b;
  if (13 <= a) {
    std::cout << b << std::endl;
  } else if (6 <= a) {
    std::cout << b / 2 << std::endl;
  } else {
    std::cout << 0 << std::endl;
  }
  return 0;
}


Measured memory_bytes: 256
/* Eat a live frog first thing in the morning,
   and nothing worse will happen to you the rest of the day */

/* You can't connect the dots looking forward 
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define int long long
int x,y,test;

void solve()
{
       cin >> x >> y;

       int pom=x-y;

       if (pom==1 || pom==0 || pom==-1)
              cout << "Brown";
       else
              cout << "Alice";
       return;
}

int32_t main()
{
    boost

    test=1;

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

  return 0;
}


Measured memory_bytes: 256
#! usr/bin/env python
# -*- coding: utf-8 -*-

from collections import deque
import heapq
import math
import bisect


def main():
    N = int(input())
    S = 0
    for i in range(N):
        x, u = input().split()
        x = float(x)
        if u == "JPY":
            S += x
        else:
            S += 380000 * x

    print(S)


if __name__ == '__main__':
    main()


Measured memory_bytes: 38256
#include <stdio.h>

int main(void)
{
    size_t n;
    scanf("%zu", &n);
    size_t as[8] = { 0 };
    size_t bc = 0, cc = 0;
    for (size_t i = 0; i < n; ++i) {
        size_t a;
        scanf("%zu", &a);
        size_t index = a / 400;
        if (a < 3200 && as[index] == 0) {
            ++bc;
            ++as[index];
        } else if (a >= 3200) {
            ++cc;
        }
    }
    printf("%zu %zu\n", bc > 1 ? bc : 1, bc + cc);
    return 0;
}


Measured memory_bytes: 128
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
from heapq import heapify, heappop, heappush

def solve():
    n = II()

    fact = {}
    for i in range(2, n+1):
        # iを素因数分解
        for j in range(2, int(pow(i, 0.5))+1):
            if i % j: continue
            ex = 0
            while i % j == 0:
                i = i // j
                ex += 1
            fact[j] = fact.get(j, 0) + ex
        if i != 1:
            fact[i] = fact.get(i, 0) + 1
    # print(fact)

    mod = 1000000007
    ans = 1
    for (num, ex) in fact.items():
        ans = ans * (ex + 1)
        ans %= mod
    print(ans)


if __name__ == '__main__':
    solve()


Measured memory_bytes: 3316
import sys
from itertools import islice, tee


def resolve(in_):
    H, W = map(int, next(in_).split())
    h0, h1 = divmod(H, 2)
    w0, w1 = divmod(W, 2)

    if H == 1 or W == 1:
        return 1

    answer = h0 * w0 * 2
    if h1:
        answer += w0
    if w1:
        answer += h0
    if h1 and w1:
        answer += 1

    return answer


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()


Measured memory_bytes: 9252
n = int(input())
a = []
b = []
for i in range(n):
    s, t = map(str, input().split())
    a.append(s)
    b.append(int(t))

x = input()

p = a.index(x) + 1
le = 0
for j in range(p):
    le += b[j]

print(sum(b) - le)
    




    




        



Measured memory_bytes: 3316
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<n;++i)
#define FOR(i, a, b) for(int i=a;i<b;++i)
#define RREP(i, n) for(int i=n-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=b-1;i>=a;--i)

#define all(x) begin(x),end(x)
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}


template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
    os << p.first << ":" << p.second;
    return os;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

// edit

int conv(int h, int m){
    return h * 60 + m;
}

void solve() {
    string S, ans;
    cin >> S;

    for(auto c : S){
        if(c == '?') ans += 'D';
        else ans += c;
    }
    
    cout << ans << endl;
    
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();


    return 0;
}


Measured memory_bytes: 3456
#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 ll INF = 1e14;
const int MOD = 1000000007;

int main(){
    int n;
    cin >> n;
    string s;
    cin >> s;
    bool ok = false;
    vector<int> ans(n);
    rep(fi,2)rep(se,2){
        if(ok) break;
        ans[0] = fi,ans[1] = se;
        for(int i=2;i<n;i++){
            ans[i] = abs(ans[i-2]-((s[i-1]=='o') ^ (ans[i-1]==0)));
        }
        if(((s[0]=='o') ^ (ans[0]==0)) - abs(ans[1]-ans[n-1]) == 0){
            if(((s[n-1]=='o') ^ (ans[n-1]==0)) - abs(ans[0]-ans[n-2]) == 0){
                ok = true;
            }
        }
    }
    if(ok){
        rep(i,n) cout << (ans[i]==0 ? 'S' : 'W') ;
        cout << endl;
    }else{
        cout << -1 << endl;
    }

    return 0;
}

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

long GCD(long x, long y) {
    if (x % y == 0) return y;
    else return GCD(y, x % y);
}

int main() {
    long a, b;
    while (cin >> a >> b) {
    long GCD_a, LCM_a;
    GCD_a = GCD(a, b);
    LCM_a = a * b / GCD(a, b);
    cout << GCD_a << ' ' << LCM_a << endl;
    }
}

 


Measured memory_bytes: 3088
// Aizu 1137: Numeral System
// 2017.9.13 bal4u@uu

#include <stdio.h>
#include <ctype.h>

int tr['z'];
char s[2][20];
int tbl[4][2] = { { 1000, 'm' }, { 100, 'c' }, { 10, 'x' }, { 1, 'i' } };

int main()
{
	int n, i, m, a, ans;
	char *p;
	
	for (i = 0; i < 4; i++) tr[tbl[i][1]] = tbl[i][0]; 
	scanf("%d", &n);
	while (n-- > 0) {
		scanf("%s%s", s[0], s[1]);
		ans = 0;
		for (i = 0; i < 2; i++) {
			for (p = s[i], a = 0; *p; ) {
				m = 1; if (isdigit(*p)) m = *p++ - '0';
				if (tr[*p] > 0) m *= tr[*p++];
				a += m;
			}
			ans += a;
		}
		for (i = 0; i < 4; i++) {
			int k = tbl[i][0];
			if (ans >= k) {
				int t = ans/k; ans %= k; 
				if (t > 1) putchar(t+'0');
				putchar(tbl[i][1]);
			}
		}
		putchar('\n');
	}
	return 0;
}

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

using ll = long long;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n); i > 0; i--)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    
    ll n,a,b,c;
    cin >> n >> a >> b >> c;

    vector<string> s(n+1);
    rep(i,n) cin >> s[i];
    vector<int> ans(n+1);

    rep(i,n){
        if(s[i]=="AB"){
            if(a+b==0){
                ans[0]=4;
                break;
            }else if(a==0 || b==0){
                ans[i+1]=1+(b==0);
                a += 2*(a==0)-1;
                b += 2*(b==0)-1;
            }else{
                if(i==n-1 || s[i+1]=="AB"){
                    ans[i+1] = 1;
                    a++; b--;
                }else if(s[i+1]=="BC"){
                    ans[i+1] = 2;
                    a--; b++;
                }else{
                    ans[i+1] = 1;
                    a++; b--;
                }
            }
        }else if(s[i]=="BC"){
            if(b+c==0){
                ans[0]=4;
                break;
            }else if(b==0 || c==0){
                ans[i+1]=2+(c==0);
                b += 2*(b==0)-1;
                c += 2*(c==0)-1;
            }else{
                if(i==n-1 || s[i+1]=="BC"){
                    ans[i+1] = 2;
                    b++; c--;
                }else if(s[i+1]=="AC"){
                    ans[i+1] = 3;
                    b--; c++;
                }else{
                    ans[i+1] = 2;
                    b++; c--;
                }
            }
        }else{
            if(a+c==0){
                ans[0]=4;
                break;
            }else if(a==0 || c==0){
                ans[i+1]=1+2*(c==0);
                a += 2*(a==0)-1;
                c += 2*(c==0)-1;
            }else{
                if(i==n-1 || s[i+1]=="AC"){
                    ans[i+1] = 1;
                    a++; c--;
                }else if(s[i+1]=="BC"){
                    ans[i+1] = 3;
                    a--; c++;
                }else{
                    ans[i+1] = 1;
                    a++; c--;
                }
            }
        }
    }
    vector<string> atos={"A","B","C"};
    if(ans[0]==4) print("No");
    else{
        print("Yes");
        rep(i,n) print(atos[ans[i+1]-1]);
    }

    return 0;
}

Measured memory_bytes: 6644
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iostream>
#include <functional>
#include <bitset>
#include <algorithm>
#include <vector>
#include <forward_list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <numeric>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void)
{
    ll n, k;

    cin >> n >> k;

    vector<ll> sum(n + 2, 0);

    ll ans = 0;

    for (int i = 1; i <= n; i++)
    {
        sum[i + 1] = i + sum[i];
    }

    for (int i = k; i <= n + 1; i++)
    {
        ans += (sum[n + 1] - sum[n + 1 - i]) - (sum[i] - sum[0]) + 1;

        ans %= MOD;
    }

    cout << ans << endl;
}


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

int main(){
	int N, M;
	cin >> N >> M;
	vector<vector<int>>G(N, vector<int>());

	for (int i = 0; i < M; i++){
		int a, b;
		cin >> a >> b;
		a--, b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}

	for (int i = 0; i < N; i++)
		cout << G[i].size() << endl;

	return 0;
}


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

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;


ull N,A,B;
ull ans;

int main()
{
    cin >> N >> A >> B;
    if((B-A)%2==0){
        ans = (B-A)/2 ;
        cout << ans << endl;
        return 0;
    }
    else{
        ans = min(N-B,A-1);
        ans++;
        ans += (B-1-A)/2;
        cout << ans << endl;
        return 0;
    }

}


Measured memory_bytes: 256
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>	tree_set;
typedef long long ll;
typedef long double ld;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

ll mod=1e9 + 7;
ll power(ll n , ll p){
    if(p==0)
        return 1;

    ll ans = power(n,p/2);
    ans = (ans*ans)%mod;
    if(p&1)
        ans = (ans*n)%mod;
    return ans;
}
int main()
{
    fast();
    ll modinv = power(2,mod-2);
    ll n,k;
    cin>>n>>k;

    ll add = ( k*(k+1) )%mod;
    add = ( add*modinv )%mod;

    ll sub =  ( k*(k-1) )%mod;
    sub = ( sub*modinv )%mod;

    ll arr[n];
    for(ll i=0;i<n;i++)
        cin>>arr[i];

    ll ans = 0;
    for(ll i=0;i<n;i++)
    {
        for(ll j = 0; j <n; j++){
            if(i==j || arr[i]<=arr[j])
                continue;

            if(j>i){
               ans = (ans + add)%mod;
            }
            else{
                ans = ( ans + sub)%mod ;
            }
        }
    }

    cout<<ans;

}


Measured memory_bytes: 256
#![allow(unused_imports)]
#![allow(unused_variables)]

use itertools::Itertools;
use proconio::{fastout, input};

#[fastout]
fn main() {
    input! {
        x: u128,
    }
    // annotate for rust-analyzer
    let x: u128 = x;

    let mut ans = 0;
    let mut yen = 100;
    while yen < x {
        yen = yen * 101 / 100;
        ans += 1;
    }

    println!("{}", ans);
}


Measured memory_bytes: 2172
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

#define Rep(i, n) for (int i = 1; i <= n; i ++)
#define Rep0(i, n) for (int i = 0; i <= n; i ++)

using namespace std;

const int N = 200010;

vector<int> t[N];
int t1[N];

priority_queue<int> q;

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	Rep(i, n){
		int l, r;
		scanf("%d%d", &l, &r);
		t[l].push_back(r);
	}
	int tmp = 0;
	for (int i = m; i; i --) {
		if (t[i].size()) Rep0(j, t[i].size() - 1) q.push(t[i][j]);
		if (!q.empty()){ q.pop(); tmp ++;}
	}
	if (t[0].size()) Rep0(i, t[0].size() - 1) q.push(t[0][i]);
	
	while (!q.empty()){
		t1[q.top()] ++;
		q.pop();
	}
	int cnt = 0;
	Rep(i, m) {
		cnt += t1[i];
		if (i > tmp && cnt >= 1) cnt --;
	}
	cnt += t1[m + 1];
	printf("%d\n", cnt);
	
	return 0;
}

Measured memory_bytes: 9216
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <stdio.h>
#include <bitset>
#include <limits.h>
#include <complex>
#include <deque>
#include <iomanip>
#include <list>
#include <cstring>
using namespace std;
#define pi pair<int,int>
#define pl pair<long long,long long>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
#define en cout << endl //セミコロンつけろ
//#define MM 1000000000
//#define MOD MM+7
typedef long long ll;
const int MM = 1e9;
const int MOD = MM+7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1,0,1,0,-1,-1,1,1};
int dy[8] = {0,-1,0,1,-1,1,1,-1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
template<typename T>
T GCD(T u,T v){
    return v ? GCD(v,u%v) : u;
}
template<typename T>
T LCM(T x,T y){
    T gc = GCD(x,y);
    return x*y/gc;
}


//stoi
vector<string> v(0);
ll N;
void function(string s){
    ll tmp = stoll(s);
    if (tmp <= N){
        v.push_back(s);
        function(s+'3');
        function(s+'5');
        function(s+'7');
    }
}

int main(){
    cin >> N;
    function("3");
    function("5");
    function("7");
    int ans = 0;
    for (int i = 0; i < v.size(); i++){
        bool thr = false,fiv = false, sev = false;
        ll tmp = stoll(v[i]);
        while (tmp > 0){
            int tmp2 = tmp%10;
            if (tmp2 == 3) thr = true;
            if (tmp2 == 5) fiv = true;
            if (tmp2 == 7) sev = true;
            tmp /= 10;
        }
        if (thr && fiv && sev) ans++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 2172
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,k,t;
    scanf("%d%d%d",&a,&b,&k);
    t=(b-a)/2+1;
    if(k>t)
    {
        for(int i=a;i<=b;i++) printf("%d\n",i);
    }
    else
    {
        if((a+k-1)==(b-k+1))
        {
            for(int i=a;i<a+k-1;i++) printf("%d\n",i);
        }
        else
        {
            for(int i=a;i<a+k;i++) printf("%d\n",i);
        }
        for(int i=b-k+1;i<=b;i++) printf("%d\n",i);
    }
    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 20)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する


int main() {
    unsigned long long x,y;
    cin >> x >> y;
    int ans = 0;
    while(x <= y){
        x *= 2;
        ans++;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <sstream>
 
#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif

int main(int argc, char **argv){
	int n;
	std::string buf;


#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
	//FILE *fp = fopen("test.txt", "r");
#endif

#if INPUT_FROM_FILE
	while(std::getline(ifs, buf)){
#else
	while(std::getline(std::cin, buf)){
#endif
		std::istringstream stream(buf);
		std::vector<double> stack;
		while(std::getline(stream, buf, ' ')){
			if(buf == "+"){
				double t = stack[stack.size() - 2] + stack.back();
				stack.pop_back();
				stack.pop_back();
				stack.push_back(t);
			}
			else if(buf == "-"){
				double t = stack[stack.size() - 2] - stack.back();
				stack.pop_back();
				stack.pop_back();
				stack.push_back(t);
			}
			else if(buf == "*"){
				double t = stack[stack.size() - 2] * stack.back();
				stack.pop_back();
				stack.pop_back();
				stack.push_back(t);
			}
			else if(buf == "/"){
				double t = stack[stack.size() - 2] / stack.back();
				stack.pop_back();
				stack.pop_back();
				stack.push_back(t);
			}
			else{
				n = stoi(buf);
				stack.push_back(n);
			}
		}
		printf("%.6lf\n", stack[0]);
	}


 
#if INPUT_FROM_FILE
	//fclose(fp);
#endif
    return 0;
}


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

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for(int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for(int i = 1; i <= n; ++i) {
    if(a[i] != i) {
      swap(a[i], a[a[i]]);
      bool ok = true;
      for(int i = 2; i <= n; ++i) {
        ok &= a[i] > a[i - 1];
      }
      cout << (ok ? "YES" : "NO") << '\n';
      return 0;
    }
  }
  cout << "YES" << '\n';
}


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

int main() {
    string A, B, C;
    cin >> A >> B >> C;
    if (A.back() == B.front() && B.back() == C.front()){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    string str;
    ll sum=0, i;
    cin >> str;
    for(i=0; i<str.size(); i++){
        str[i] -= 48;
        sum += str[i];
    }
//    cout << sum << endl;
    if(sum%9==0) cout << "Yes\n";
    else cout << "No\n";
    return 0;
}


Measured memory_bytes: 3700
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
	int n;
	cin >> n;
	vector <long long> a(n), b(n);
	for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
	long long cnt = 0;
	for (int i = n - 1; i >= 0; --i) {
		a[i] += cnt;
		if (a[i] % b[i] > 0) {
			long long num = a[i] % b[i];
			cnt += b[i] - num;
		}
	}
	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 1920
#include <stdio.h>

int main(){
    int cp=0;

    int N;
    scanf("%d",&N);

    int A[N];
    for(int i=0;i<N;i++){
        scanf("%d",&A[i]);
    }

    for(int i=0;i<N;i++){
        if(i==0)
            printf("%d",A[i]);
        else
            printf(" %d",A[i]);
    }
    printf("\n");

    for(int i=0;i<N-1;i++){
        for(int j=i;j>=0&&A[j]>A[j+1];j--){
            cp=A[j];
            A[j]=A[j+1];
            A[j+1]=cp;
        }
        for(int i=0;i<N;i++){
            if(i==0)
                printf("%d",A[i]);
            else
                printf(" %d",A[i]);
        }
        printf("\n");
    }

}


Measured memory_bytes: 2044
n, m = [ int(v) for v in input().split() ]
edge = []
in_list = [0] * n
out_list = [0] * n
dp_list = [0] * n
connect_list = [ [] for i in range(n) ] 

for i in range(m):
    s, t = [ int(v)-1 for v in input().split() ]
    edge.append((s,t))
    in_list[t] += 1
    out_list[s] += 1
    connect_list[t].append(s)

search_list = [ i for i, v in enumerate(out_list) if v == 0 ]
while search_list:
    new_search_list = []
    for i in search_list:
        for j in connect_list[i]:
            out_list[j] -= 1
            if dp_list[j] < dp_list[i] + 1:
                dp_list[j] = dp_list[i] + 1
            if out_list[j] == 0:
                new_search_list.append(j)
    search_list = new_search_list

print(max(dp_list))


Measured memory_bytes: 72204
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
 
 
 
 
int main(void){
    int n,k;
    cin>>n>>k;
    long long int p[400002];
    
    for(int i=0;i<n;i++){
        cin>>p[i];
    }
    long long int amax=0,now=0;
    for(int i=0;i<k;i++){
        now=now+p[i];
    }
    amax=now;
    for(int i=k;i<n;i++){
        now+=p[i]-p[i-k];
        amax=max(amax,now);
    }
    cout<<((amax+k)/2)<<((amax+k)%2?".5":"");
    return 0;
}

Measured memory_bytes: 2560
import java.io.*;

public class Main{
    public static void main(String[]args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String words = br.readLine();
        String[] s = words.split(" ",0);
        int a = Integer.parseInt(s[0]);
        int b = Integer.parseInt(s[1]);
        int c = Integer.parseInt(s[2]);

        if(a < b && b < c){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}

Measured memory_bytes: 24096
import java.awt.Point;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Queue;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	public static int[] dx = {1,0,-1,0};
	public static int[] dy = {0,1,0,-1};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			int n = sc.nextInt();
			if (n == 0) {
				break;
			}
			TreeSet<Integer> xx = new TreeSet<Integer>();
			TreeSet<Integer> yy = new TreeSet<Integer>();
			Square[] s = new Square[n];
			for(int i=0;i<n;i++) {
				int l = sc.nextInt();
				int t = sc.nextInt();
				int r = sc.nextInt();
				int b = sc.nextInt();
				s[i] = new Square(l, t, r, b);
				xx.add(l);
				xx.add(r);
				yy.add(t);
				yy.add(b);
			}
			HashMap<Integer,Integer> zipx = new HashMap<Integer,Integer>();
			HashMap<Integer,Integer> zipy = new HashMap<Integer,Integer>();
			int i_ = 0;
			for(int i:xx) {
				zipx.put(i, i_++);
			}
			i_ = 0;
			for(int i:yy) {
				zipy.put(i, i_++);
			}
			int m = n * 4 + 1;
			boolean[][] map = new boolean[m][m];
			for(int i=0;i<n;i++) {
				int l = zipx.get(s[i].l) * 2 + 1;
				int t = zipy.get(s[i].t) * 2 + 1;
				int r = zipx.get(s[i].r) * 2 + 1;
				int b = zipy.get(s[i].b) * 2 + 1;
				for(int j=l;j<=r;j++) {
					map[j][t] = true;
					map[j][b] = true;
				}
				for(int j=b;j<=t;j++) {
					map[l][j] = true;
					map[r][j] = true;
				}
			}
//			StringBuilder sb = new StringBuilder();
//			for(int y=0;y<m;y++) {
//				for(int x=0;x<m;x++) {
//					sb.append(map[x][y] ? 1 : 0);
//				}
//				sb.append("\n");
//			}
//			System.out.println();
//			System.out.print(sb.toString());
			int ans = 0;
			for(int sx=0;sx<m;sx++) {
				for(int sy=0;sy<m;sy++) {
					if (!map[sx][sy]) {
						map[sx][sy] = true;
						ans++;
						Queue<Point> q = new ArrayDeque<Point>();
						q.offer(new Point(sx,sy));
						while(!q.isEmpty()) {
							Point p = q.poll();
							for(int i=0;i<4;i++) {
								int nx = p.x + dx[i];
								int ny = p.y + dy[i];
								if (nx < 0 || nx >= m || ny < 0 || ny >= m || map[nx][ny]) {
									continue;
								}
								map[nx][ny] = true;
								q.offer(new Point(nx,ny));
							}
						}
					}
				}
			}
			System.out.println(ans);
		}
	}
	
	static class Square {
		int l,t,r,b;
		public Square(int l,int t,int r,int b) {
			this.l = l;
			this.t = t;
			this.r = r;
			this.b = b;
		}
	}

}

Measured memory_bytes: 71844
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <string>
#include <iostream>
#include <set>

using namespace std;

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

    set<string> data;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 1; j <= K; j++) {
        data.insert(s.substr(i, j));
      }
    }

    int count = 0;
    for(auto itr = data.begin(); itr != data.end(); ++itr) {
      count++;
      if (count == K) {
        cout << *itr << endl;
        break;
      }
    }

    return 0;
}


Measured memory_bytes: 1664
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <cmath>
#include <unordered_map>
#include <cstdio>
using namespace std;
#define pie 3.141592653589793238462643383279
#define int long long
#define P pair<int,int>
#define all(vec) vec.begin(),vec.end()

int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
int lcm(int x, int y) {
	return x / gcd(x, y)*y;
}
int kai(int x, int y, int m) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= m;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

int comb(int x, int y, int m) {
	if (y > x)return 0;
	return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

const int mod = 1000000007;

int n, a, b;
int d[5010][5010];
int cnt[5010];
int cnt1;
int ans = 1;

signed main() {
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++)ans = ans * 2 % mod;
	if (a == 1 || b == 1) {
		cout << ans << endl;
		return 0;
	}
	if (a > b)swap(a, b);
	d[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j < b; j++)d[i][j] = d[i - 1][j - 1];
		d[i][1] = (d[i][1] + cnt1) % mod;
		for (int j = 1; j < min(i + 1, b); j++)d[i][j] = (d[i][j] + cnt[i - j]) % mod;
		for (int j = 0; j < b; j++)cnt1 = (cnt1 + d[i - 1][j]) % mod;
		if (a <= i) {
			for (int j = 0; j < min(i + 1 - a, b); j++)cnt[i - a - j] = (cnt[i - a - j] + d[i - a][j]) % mod;
			for (int j = 0; j < min(i + 1 - a, b); j++)cnt1 = (cnt1 - d[i - a][j] + mod) % mod;
		}
		if (b <= i - 1)for (int j = 0; j < min(i - b + 3, b); j++)cnt[i - b + 2 - j] = (cnt[i - b + 2 - j] - d[i - b + 2][j] + mod) % mod;
		if (b == i - 1)cnt[0] = (cnt[0] - 1 + mod) % mod;
	}
	for (int i = n; i > n - a; i--) {
		for (int j = 0; j < b; j++)ans = (ans - d[i][j] + mod) % mod;
	}
	for (int i = n - a; i > n - b; i--) {
		for (int j = 0; j < b - (n - i); j++)ans = (ans - d[i][j] + mod) % mod;
	}
	cout << ans << endl;
}

Measured memory_bytes: 199356
# -*- coding: utf-8 -*-
s = input()

former = int(''.join(s[0:2]))
latter = int(''.join(s[2:]))

former_mm_flag = False
if former <= 12 and former > 0:
    former_mm_flag = True

latter_mm_flag = False
if latter <= 12 and latter > 0:
    latter_mm_flag = True

if former_mm_flag:
    if latter_mm_flag:
        print('AMBIGUOUS')
    else:
        print('MMYY')
else:
    if latter_mm_flag:
        print('YYMM')
    else:
        print('NA')

Measured memory_bytes: 3064
#include <bits/stdc++.h>
typedef long long ll;
const int INF=1e9,MOD=1e9+7,ohara=1e6;
const ll LINF=1e18;
using namespace std;

#define rep(i,n) for(ll (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(ll i=(a);i<(b);i++)
#define rrrep(i,a,b) for(ll i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define Size(n) (n).size()
#define Cout(x) cout<<(x)<<endl

ll n,cnt,ans=LINF,a[ohara],b[ohara],c,d,tmp,tmpp,m,h,w,x[ohara],y,sum,pos,k;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"),s;
bool fl;
struct edge{int to,cost;};

//-------------------------↓↓↓↓↓↓------------------------

int main(void){
       cin.tie(0);
    ios::sync_with_stdio(false);
      
    cin>>n>>k;
    rep(i,n)cin>>x[i];
    ll cntt=0;
    rep(i,n){
        if(x[i]<=0){
            b[cnt++]=x[i];
        }
        else{
            a[cntt++]=x[i];
        }
    }
    reverse(b,b+cnt);
    if(cnt==0){
        Cout(a[k-1]);
    }
    else if(cntt==0){
        Cout(b[k-1]*-1);
    }
    else{
    //rep(i,cntt)cout<<a[i]<<" ";
    rep(i,cnt){
        ll nokori=k-i-1;
        if(nokori<=0)continue;
        if(nokori-1>=cntt)continue;
        tmp=abs(a[nokori-1]-b[i]*2);
        //tmp=abs(abs(b[i])+abs(a[nokori-1]));
        ans=min(ans,tmp);
        //Cout(tmp);
    }
    rep(i,cntt){
        ll nokori=k-i-1;
        if(nokori<=0)continue;
        if(nokori-1>=cnt)continue;
        tmp=abs(a[i]*2-b[nokori-1]);
        //tmp=abs(abs(a[i])+abs(b[nokori-1]));
        ans=min(ans,tmp);
    }
    Cout(ans);
    }
    return 0;
}


Measured memory_bytes: 7168
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cmath>
#include<stdio.h>
#include<vector>
#include<set>
#include<utility>
#include<ctype.h>
#include<ctime>
# define time cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"
# define ll long long
using namespace std;
int gcd(int a,int b){
    int r;
    while(b){
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}
int GCD(int a,int b)
{
    if(a==0) return b;
    else return GCD(b%a,a);
}
long long int power(long long int a,long long int b){
    long long int r=1;
    while(b){
        if(b & 1){
            r=r*a;
            b--;
        }
        a=a*a;
        b>>=1;
    }
    return r;
}
int palindrome(char *s,int len)
{
    int i,j,k,count=0,c=0;
    for(i=0,j=len-1;i<len,j>=0;i++,j--){
        if(s[i]==s[j]) count++;
    }
	if(count==len) return 1;
    	else return 0;
}
long long int binary(char *s,long long int len)
{
    long long int i,j,k,r=0,p=0,num;
    for(i=len-1;i>=0;i--){
        num=s[i]-'0';
        r+=(num*power(2,p));
        p++;
    }
    return r;
}
int binary_search(int *ara,int size,int n)
{
    int high,mid,low;
    low=0;
    high=size-1;
    while(low<=high){
        mid=(low+high)/2;
        if(ara[mid]==n){
            return 1;
        }
        else if(n>ara[mid]) low=mid+1;
        else if(n<ara[mid]) high=mid-1;
    }
    return -1;
}
ll int ara[100005];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	ll int i,j,k,T,q,x;
	string s1,s2;
	int n;
    cin>>n>>x;
    ll int sum=0;
	for(i=1;i<=n;i++) cin>>ara[i];
	for(i=1;i<=n;i++){
        if((ara[i]+ara[i-1])>x){
            sum+=((ara[i]+ara[i-1])-x);
            ara[i]=x-ara[i-1];
        }
	}
	cout<<sum<<endl;
}


Measured memory_bytes: 1024
c = [list(map(int, input().split())) for _ in range(3)]
ok = True

b = [c[0][0], c[0][1], c[0][2]]
a = [0, c[1][0] - b[0], c[2][0] - b[0]]

for i in range(3):
    for j in range(3):
        if a[i] + b[j] != c[i][j]:
            ok = False

print('Yes') if ok else print('No')


Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <cstring>
 
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
 
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1<<29;


int dat[3][1001][1001];

int get(int x, int y, int idx){
	if(x < 0 || y < 0) return 0;
	return dat[idx][y][x];
}

void query(int y1, int x1, int y2, int x2){
	rep(i, 3){
		int ans = 0;
		ans = get(x2, y2, i) - get(x1-1, y2, i) - get(x2, y1-1, i) + get(x1-1, y1-1, i);
		cout << (i?" ":"") << ans;
	}
	cout << endl;
}

int main(){
	int h, w, k;
	cin >> h >> w >> k;
	vector<string> v(h);
	rep(i, h) cin >> v[i];

	rep(i, h){
		rep(j, w){
			rep(k, 3){
				dat[k][i][j] = get(j-1, i, k)+get(j, i-1, k)-get(j-1, i-1, k);
				if(v[i][j] == "JOI"[k]) dat[k][i][j]++;
			}
		}
	}

	rep(i, k){
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		query(a-1, b-1, c-1, d-1);
	}

    return 0;
}

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

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

int main()
{
  int t;
  while (cin >> t, t){
    int n, sum = 0;
    cin >> n;
    REP(i, n){
      int s, f;
      cin >> s >> f;
      sum += f - s;
    }
    if (t <= sum) cout << "OK" << endl;
    else cout << t - sum << endl;
  }
}

Measured memory_bytes: 1160
n = gets.to_i
l = gets.split.map(&:to_i).sort

i = l.length - 1
ans = 0
while i > 1
  a = l[i]
  j = i - 1
  while j > 0
    b = l[j]
    c = l[0..(j-1)].bsearch_index { |x| a < b + x }
    ans += j - c if c
    j -=1
  end
  i -=1
end
puts ans


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

using namespace std;

#define N_TIMES(i, n)     for ( uint64_t i = 0;     i <  n; ++i )
#define N_TIMES_REV(i, n) for (  int64_t i = n - 1; i >= 0; --i )

bool judge(
  uint64_t N, uint64_t M,
  const vector<uint64_t> &A,
  const vector<uint64_t> &B,
  uint64_t D
)
{
  uint64_t C = 0;

  N_TIMES(n, N) {
    uint64_t diff = D >= A[n] ? D - A[n] : 0;

    auto itr = lower_bound(A.begin(), A.end(), diff, greater_equal<uint64_t>());
    uint64_t d = distance(A.begin(), itr);

    C += d;
    
    if (C >= M) { return true; }
  }

  return C >= M;
}

uint64_t calculate(
  uint64_t N, uint64_t M,
  const vector<uint64_t> &A,
  const vector<uint64_t> &B,
  uint64_t D
)
{
  uint64_t T = 0, m = 0;

  N_TIMES(n, N) {
    uint64_t diff = D >= A[n] ? D - A[n] : 0;

    auto itr = lower_bound(A.begin(), A.end(), diff, greater_equal<uint64_t>());
    uint64_t d = distance(A.begin(), itr);

    T += B[d] + (A[n] * d);
    m += d;
  }
  
  T -= (m - M) * D;

  return T;
}

int main()
{
  uint64_t N, M;
  cin >> N >> M;

  vector<uint64_t> A(N);
  N_TIMES(n, N) { cin >> A[n]; }

  sort(A.begin(), A.end(), greater<uint64_t>());

  vector<uint64_t> B(N + 1, 0);
  N_TIMES(n, N) { B[n + 1] = A[n] + B[n]; }

  uint64_t hi = (A[0] << 1) + 1, lo = 0;

  while (hi - lo > 1) {
    uint64_t mid = (hi + lo) >> 1;

    if (judge(N, M, A, B, mid)) {
      lo = mid;
    } else {
      hi = mid;
    }
  }

  cout << calculate(N, M, A, B, (hi + lo) >> 1) << endl;

  return 0;
}

Measured memory_bytes: 1792