akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const int nmax = 8;
bool graph[nmax][nmax];
const int INF = 1e9;
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
void warshall_floyd(ll n)
{
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < n; j++)
        {
            for (size_t k = 0; k < n; k++)
            {
                dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
            }
        }
    }
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

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

ll mulMod(ll a, ll b)
{
    return (((a % MOD) * (b % MOD)) % MOD);
}

ll powMod(ll a, ll p)
{
    if (p == 0)
    {
        return 1;
    }
    else if (p % 2 == 0)
    {
        ll half = powMod(a, p / 2);
        return mulMod(half, half);
    }
    else
    {
        return mulMod(powMod(a, p - 1), a);
    }
}

void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B)
{
}

int main()
{
    long long N;
    scanf("%lld", &N);
    long long M;
    scanf("%lld", &M);
    std::vector<pair<ll, ll>> A(N);
    for (int i = 0; i < N; i++)
    {
        ll a, b;
        cin >> a >> b;
        A[i] = make_pair(a, b);
    }

    SORT(A);
    ll bought = 0;
    ll cost = 0;
    for(auto&& i : A)
    {
        if(bought + i.second < M){
            bought += i.second;
            cost += i.first * i.second;
        }else{
            cost += i.first * (M-bought);
            break;
        }
    }
    cout << cost << endl;
        return 0;
}


Measured memory_bytes: 1792
#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge> > pqlE;

template<typename T>
using v = vector<T>;

int main() {
	int N;
	cin >> N;
	vi num(9, 0);
	rep(i, N) {
		int a;
		cin >> a;
		if (a >= 3200) {
			num[8]++;
		}
		else {
			num[a / 400]++;
		}
	}
	int ans = 0;
	rep(i, 8) {
		if (num[i] > 0)ans++;
	}
	if (ans == 0) {
		cout << 1 << ' ' << num[8] << endl;
	}
	else {
		cout << ans << ' ' << ans + num[8] << endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int n = 0;
    cin >> n;
    vector<int> a(n, 0);
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    
    sort(a.begin(), a.end());
    int result = 0;
    for(int i = 0; i < n - 2; ++i) {
        for(int j = i + 1; j < n - 1; ++j) {
            for(int k = j + 1; k < n; ++k) {
                if(a[i] + a[j] <= a[k]) {
                    break;
                }
                if(a[i] < a[j] + a[k] && a[j] < a[i] + a[k] && a[k] < a[i] + a[j]) {
                    result += 1;
                }
            }
        }
    }
    cout << result << endl;
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char s[110][110];
int H, W, f[110][110];

int main() {
    scanf("%d%d", &H, &W);
    for (int i = 1; i <= H; ++i) scanf("%s", s[i] + 1);

    f[1][1] = s[1][1] == '#';
    for (int i = 2; i <= W; ++i) f[1][i] = f[1][i - 1] + (s[1][i - 1] == '.' && s[1][i] == '#');
    for (int i = 2; i <= H; ++i) f[i][1] = f[i - 1][1] + (s[i - 1][1] == '.' && s[i][1] == '#');
    for (int i = 2; i <= H; ++i)
        for (int j = 2; j <= W; ++j)
            f[i][j] = min(f[i - 1][j] + (s[i - 1][j] == '.' && s[i][j] == '#'),
                          f[i][j - 1] + (s[i][j - 1] == '.' && s[i][j] == '#'));
    printf("%d\n", f[H][W]);
    return 0;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    int P[N + 1];
    P[0] = 0;
    int ans = 0;
    for (int i = 0; i < N; i++) {
      cin >> P[i + 1];
    }
    vector<int> v;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        if (P[i] + P[j] <= M) v.push_back(P[i] + P[j]); 
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      int res = M - v[i];
      if (res == 0) {
        ans = M;
        break;
      }
      int left = -1;
      int right = v.size();
      while (right - left > 1) {
        int mid = (right + left) / 2;
        if (res >= v[mid]) left = mid;
        else right = mid;
      }
      ans = max(ans, v[i] + v[left]);
    }
    cout << ans << "\n";
  }
  return 0;
}


Measured memory_bytes: 10684
a,b,c,d=map(int,input().split())
ans=0
kouho1=a*c
kouho2=a*d
kouho3=b*c
kouho4=b*d
ans=max(kouho1,kouho2,kouho3,kouho4)
if a*b<=0 or c*d<=0:
    ans=max(ans,0)
print(ans)

Measured memory_bytes: 9048
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

typedef long long cat;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int K =38;
	int N =K*(K-1)+1;
	cout << N << " " << K << "\n";
	vector< vector<int> > V(N,vector<int>(K));
	for(int i =0; i < K; i++) V[i][0] =1;
	for(int i =0; i < K; i++) for(int j =1; j < K; j++) V[i][j] =(K-1)*i+j+1;
	for(int i =K; i < N; i++) V[i][0] =V[0][(i-1)/(K-1)];
	for(int i =1; i < K; i++) for(int j =K; j < N; j +=K-1) {
		int a =(j-1)/(K-1)-1;
		for(int l =0; l < K-1; l++) V[j+l][i] =V[i][((i-1)*a+l)%(K-1)+1];
	}

	for(int i =0; i < N; i++) {
		for(int j =0; j < K; j++) cout << V[i][j] << ((j == K-1)?"\n":" ");
	}

/*	vector<int> occ(N+1,0);
	for(int i =0; i < N; i++) {
		bool iden =false;
		for(int j =0; j < K; j++)
			for(int l =0; l < j; l++) iden |=(V[i][j] == V[i][l]);
		assert(!iden);
		for(int j =0; j < K; j++) occ[V[i][j]]++;
	}
	for(int j =1; j <= N; j++) assert(occ[j] == K);
	for(int i =0; i < N; i++) for(int j =0; j < i; j++) {
		int hits =0;
		for(int a =0; a < K; a++) for(int b =0; b < K; b++)
			hits +=(V[i][a] == V[j][b]);
		assert(hits == 1);
	}
*/	return 0;}

// look at my code
// my code is amazing


Measured memory_bytes: 768
n = int(input())
lst = list(map(int, input().split()))
for i in range(n):
    li = lst[i]
    j = i-1
    while j >= 0 and lst[j] > li:
        lst[j:j+2] = lst[j+1], lst[j]
        j -= 1
    print(*lst)

Measured memory_bytes: 8104
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include<bitset>

using namespace std;
#define loop(i,n) for(int i = 0; i < (n); ++i)
#define loops(i,f,n) for(int i = (f); i < (n); ++i)
#define INF 9999999
#define in(a) cin>>a
#define out(a) cout<<a<<endl

int main()
{
	string s;
	cin >> s;
	long long ans = 0;
	loop(bit, (1 << s.size() - 1))
	{
		long long num = s[0] - '0';
		loop(j, s.size() - 1)
		{
			if (bit&(1 << j))
			{
				ans += num;
				num = 0;
			}
			num = num * 10 + s[j +1] - '0';
		}
		ans += num;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#C
N=int(input())
Ans=[0 for i in range(N+1)]
for x in range(1,10**2):
    for y in range(1,10**2):
        for z in range(1,10**2):
            k=(x+y)**2+(y+z)**2+(z+x)**2
            if k<=2*N and k%2==0:
                Ans[int(k/2)]+=1
for i in range(1,N+1):
    print(Ans[i])

Measured memory_bytes: 69336
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
// typedef __int128_t Int;
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
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 (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

  int N;
  cin >> N;
  vector<int> v(N + 1);
  rep(i, 0, N) { cin >> v[i]; }

  int ans = 0;
  int i = 0;
  while (i < N) {
    int c = 1;
    while (i < N and v[i + 1] == v[i]) {
      c++;
      i++;
    }
    dump(c);
    ans += c / 2;
    i++;
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <math.h>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>

using namespace std;
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() {
  int n;
  cin >> n;
  if (n % 1000 == 0) {
    cout << 0;
  } else
  {
    cout << (n / 1000 + 1) * 1000 - n;
  }
  
  
  return 0;
}


Measured memory_bytes: 3192
// Delete the dynamically allocated memory

#include<bits/stdc++.h>
using namespace std;
 
#define ff              first
#define ss              second
#define pb              push_back
#define pr              pair<long long,long long>
#define mp(a,b)         make_pair(a,b)
#define vr              vector<long long>
#define mod1            1000000007
#define mod2            998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define fill(a,b)       memset(a, b, sizeof(a))
#define mk(arr,n,type)  type *arr=new type[n];
#define ll              long long
#define ld	            long double
#define w(x)            int x; cin>>x; while(x--)
ll power(ll base, ll exp) {
    ll res=1;
    while(exp>0) {
       if(exp%2==1) res=(res*base);
       base=(base*base);
       exp/=2;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,k;
	cin>>n>>k;
	mk(a,n,ll)
	for(ll i=0;i<n;i++){
		cin>>a[i];
	}
	for(ll i=k;i<n;i++){
		if(a[i]>a[i-k]){
			cout<<"Yes"<<"\n";
		}else{
			cout<<"No"<<"\n";
		}
	}
	delete[] a;
	return 0;
	}

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

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

    int len_a, len_b;
    len_a = a.length();
    len_b = b.length();

    if(a.substr(len_a - 1,1) == b.substr(0,1) && b.substr(len_b - 1, 1) == c.substr(0,1)){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}

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

int main() {
	vector<int>data( 7 );
	map<int, int>d;
	for( size_t i = 1; i <= 6; i++ ) {
		cin >> data[i];
		d[data[i]] = i;
	}
	int N;
	cin >> N;
	for( size_t i = 0; i < N; i++ ) {
		int a, b;
		cin >> a >> b;
		int x = d[a], y = d[b];
		if( x == 1 ) {
			switch( y ) {
			case 2:cout << data[3] << endl; break;
			case 3:cout << data[5] << endl; break;
			case 5:cout << data[4] << endl; break;
			case 4:cout << data[2] << endl; break;
			}
			continue;
		}
		if( x == 6 ) {
			switch( y ) {
			case 2:cout << data[4] << endl; break;
			case 4:cout << data[5] << endl; break;
			case 5:cout << data[3] << endl; break;
			case 3:cout << data[2] << endl; break;
			}
			continue;
		}
		if( y == 1 ) {
			switch( x ) {
			case 2:cout << data[4] << endl; break;
			case 4:cout << data[5] << endl; break;
			case 5:cout << data[3] << endl; break;
			case 3:cout << data[2] << endl; break;
			}
			continue;
		}
		if( y == 6 ) {
			switch( x ) {
			case 2:cout << data[3] << endl; break;
			case 3:cout << data[5] << endl; break;
			case 5:cout << data[4] << endl; break;
			case 4:cout << data[2] << endl; break;
			}
			continue;
		}
		if( (x == 2 && y == 3) || (x == 3 && y == 5) || (x == 5 && y == 4) || (x == 4 && y == 2) ) {
			cout << data[1] << endl;
		} else {
			cout << data[6] << endl;
		}
	}

}

Measured memory_bytes: 3016
#include <iostream>
#include <string>
using namespace std;
int main (){
    string s,t;cin>>s>>t;
    int a,b;cin>>a>>b;
    string u;cin>>u;
    if(u==s){
        a-=1;
    } if(u==t){
       b-=1;  
    } cout<<a<<" "<<b;}

























Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> h(n);
    for (int i=0; i<n; i++){
        int m;
        cin >> m;
        h[i] = m;
    }
    sort(h.begin(), h.end());

    int ans = abs(h[k-1]-h[0]);
    for (int i=0; i+k-1<n; i++){
        ans = min(ans, abs(h[i+k-1]-h[i]));
    }
    cout << ans << endl;
    return 0;
}

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

const int MAX = 25;

struct Node {int parent, left, right; };
Node node[MAX];

void init(int n) {
	for (int i = 0; i < n; i++) {
		node[i].parent = -1;
		node[i].left = -1;
		node[i].right = -1;
	}
}

void preParse(int i) {
	if (i == -1) return;
	cout << " " << i;
	preParse(node[i].left);
	preParse(node[i].right);
}

void inParse(int i) {
	if (i == -1) return;
    inParse(node[i].left);
	cout << " " << i;
    inParse(node[i].right);
}

void postParse(int i) {
	if (i == -1) return;
    postParse(node[i].left);
    postParse(node[i].right);
	cout << " " << i;
}

int main() {
	int n;
	cin >> n;
	init(n);
	for (int i = 0; i < n; i++) {
		int id, l, r;
		cin >> id >> l >> r;
		if (l != -1) {
			node[id].left = l;
			node[l].parent = id;
		}
		if (r != -1) {
			node[id].right = r;
			node[r].parent = id;
		}
	}
	int root = -1;
	for (int i = 0; i < n; i++) {
		if (node[i].parent != -1) continue;
		root = i;
		break;
	}
	cout << "Preorder" << endl;
	preParse(root);
	cout << endl << "Inorder" << endl;
	inParse(root);
	cout << endl << "Postorder" << endl;
	postParse(root);
	cout << endl;
}



Measured memory_bytes: 3100
#Divisor
N=int(input())
ans=0
# 105からNまでの奇数の数
for i in range(105,N+1,2):
    cnt = 0
    # 1からNまでの約数をみつける
    for j in range(1,i+1):
        if i%j == 0:
            cnt += 1
    if cnt == 8:
        ans += 1
print(ans)

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

int main(){
  int N,M;
  cin >> N >> M;
  
  vector<int> L(M); 
  vector<int> R(M);
  vector<int> C(N);
  int maxl=0;
  int minr=N;
  
  for (int i = 0; i < M; i++){
    cin >> L.at(i) >> R.at(i);
    if(L.at(i) > maxl){
      maxl = L.at(i);
    }
    if(R.at(i) < minr){
      minr = R.at(i);
    }
  }

  
  int k=0;//全部通れるカードの枚数
  
  if(minr >= maxl){
    k=minr-maxl+1;
  }

  cout << k;
}

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

int main() {
    int N, D;
    cin >> N >> D;
    vector<vector<int>> X(N, vector<int>(D));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < D; j++) {
            cin >> X.at(i).at(j);
        }
    }

    double dis2 = 0;
    int ans = 0;

    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            for (int k = 0; k < D; k++) {
                dis2 += (X.at(i).at(k) - X.at(j).at(k))*(X.at(i).at(k) - X.at(j).at(k));
            }
            for (int l = 0; l <= dis2; l++) {
                if (l*l == dis2) {
                    ans++;
                }
            }
            dis2 = 0;
        }
    }

    cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>

using namespace std;

typedef long long int ll;
typedef vector<ll> V;
typedef vector<V> VV;

int main() {
  // Happy hacking!
  string s;
  ll ones=0, zs=0;
  cin >> s;

  for(ll i=0; i<s.length(); i++) {
    if(s[i] == '0') zs++;
    else if(s[i] == '1') ones++;
  }
  cout << min(ones, zs) * 2 << endl;

  return 0;
}


Measured memory_bytes: 512
def nPk(n, k):
  return fact[n] * invfact[n-k] % MOD

def nCk(n, k):
  return fact[n] * invfact[k] * invfact[n-k] % MOD

N, M = map(int, input().split())
MOD = 10 ** 9 + 7

fact = [0] * 1100000
invfact = [0] * 1100000

fact[0] = 1

for i in range(1, 1100000):
  fact[i] = fact[i-1] * i % MOD

invfact[-1] = pow(fact[-1], MOD-2, MOD)

for i in range(1100000-1, 0, -1):
  invfact[i-1] = invfact[i] * i % MOD

ans = 0

for k in range(N + 1):
  temp = nCk(N, k) * nPk(M, k) * (nPk(M-k, N-k) ** 2) % MOD
  if k % 2 == 0:
    ans += temp
  else:
    ans -= temp
  ans %= MOD

print(ans)


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

// while[x][y] = マス(x, y)を右上とした長方形に
//               含まれている(白マス-黒マス)の総数
int16_t white[1000][1000];
int main() {
    int N, K;
    cin >> N >> K;

    // 黒マスを指定する条件の数
    int bnum = 0;
    // まずは2K * 2Kの範囲に絞る
    for (int i = 0; i < N; ++i) {
        int x, y, z;
        char sc;
        cin >> x >> y >> sc;
        x %= K * 2;
        y %= K * 2;
        z = (sc == 'W');
        // K * Kの範囲に絞る
        // 座標をKで割った値の偶奇で色が変わる
        z += x / K + y / K;
        z %= 2;
        x %= K;
        y %= K;
        white[x][y] += (z ? 1 : -1);
        if (z == 0)
            ++bnum;
    }

    // 2次元imosの要領で展開
    for (int x = 0; x < K; ++x) {
        for (int y = 1; y < K; ++y) {
            white[x][y] += white[x][y - 1];
        }
    }

    for (int x = 1; x < K; ++x) {
        for (int y = 0; y < K; ++y) {
            white[x][y] += white[x - 1][y];
        }
    }

    int ans = 0;
    for (int x = 0; x < K; ++x) {
        for (int y = 0; y < K; ++y) {
            // (x, y)を右上の頂点とした長方形を白く塗るパターン
            int w = white[K - 1][K - 1] - white[x][K - 1] - white[K - 1][y] +
                    white[x][y] * 2 + bnum;

            ans = max(ans, max(w, N - w));
        }
    }

    cout << ans << endl;
    return 0;
}

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

int N,H,d[100000],h[100000],sum[100000];
signed main(){
    scanf("%lld%lld",&N,&H);
    rep(i,N-1)scanf("%lld%lld",&d[i],&h[i]);

    sum[0]=H;
    rep(i,N-1)sum[i+1]=sum[i]-d[i];
    int D=0;

    int ans=0,idx=0;
    deque<int>deq;
    rep(i,N-1){

        if(H-(sum[i]-D)<=h[i]){
            deq.clear();
            idx=i;
        }
        else{
            while(deq.size()&&h[deq.back()]<=h[i])deq.pop_back();
            deq.push_back(i);
        }

        while(sum[i+1]-D<=0){
            if(deq.size()&&H-(sum[idx]-D)<=h[deq.front()]){
                int j=deq.front();
                int t=min((H-(sum[j]-D))/h[j],(D-sum[i+1])/h[j]+1);
                ans+=t;
                D-=t*h[j];
            }
            else{
                ans++;
                D-=H-(sum[idx]-D);
            }
            while(deq.size()&&H-(sum[deq.front()]-D)<h[deq.front()]){
                    idx=deq.front();
                    deq.pop_front();
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}

Measured memory_bytes: 3556
#include <iostream>
#include <cstdlib>
#include <stack>
typedef long long int ll;
using namespace std;
int main() {
	stack<int> C;
	string S;
	int A, B;
	while (cin >> S) {
		if (S[0] == '+') {
			A = C.top(); C.pop();
			B = C.top(); C.pop();
			C.push(A + B);
		}
		else if (S[0] == '-') {
			B = C.top(); C.pop();
			A = C.top(); C.pop();
			C.push(A - B);
		}
		else if (S[0] == '*') {
			A = C.top(); C.pop();
			B = C.top(); C.pop();
			C.push(A * B);
		}
		else {
			C.push(atoi(S.c_str()));
		}
	}
	cout << C.top() << endl;
}



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

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

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

const int MAX = 1e5 + 5;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

class Combination{
    private:
        vector<long long> fac, finv, inv;
    
    public:
        Combination(){
            fac = finv = inv = vector<long long> (MAX);

            fac[0] = fac[1] = 1;
            finv[0] = finv[1] = 1;
            inv[1] = 1;
            for (int i = 2; i < MAX; i++){
                fac[i] = fac[i - 1] * i % MOD;
                inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
                finv[i] = finv[i - 1] * inv[i] % MOD;
            }
        }

        long long getCom(int n, int k){
            if (n < k) return 0;
            if (n < 0 || k < 0) return 0;
            return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
        }
};

int main(){
    int N, K;
    cin >> N >> K;

    vector<int> A(N);
    rep(i, N) cin >> A[i];

    sort(A.begin(), A.end());

    ll ans = 0;

    Combination com = Combination();
    rep(i, N) {
        // 最小値として利用
        ll B = com.getCom(N - 1 - i, K - 1);
        ans -= B * A[i] % MOD;
        ans %= MOD;
        if (ans < 0) ans += MOD;


        // 最大値として利用
        B = com.getCom(i, K - 1);
        ans += B * A[i] % MOD;
        ans %= MOD;
        if (ans < 0) ans += MOD;
    }

    cout << ans << endl;
}

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

ll f(ll x)
{
    switch (x % 4) {
        case 0:
            return x;
        case 1:
            return 1;
        case 2:
            return x + 1;
        case 3:
        default:
            return 0;
    }
}

int main()
{
    ll a, b; cin >> a >> b;
    cout << (f(a - 1) ^ f(b)) << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
using namespace std;
#define int long long


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

    int a,b,c; cin >> a >> b >> c;

    int d = 0;

    for(;;){
        d += a%b;
        d %= b;
        if(d == c){
            cout << "YES" << endl;
            return 0;
        }
        if(d == 0){
            cout << "NO" << endl;
            return 0;
        }
    }
}


Measured memory_bytes: 256
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int N = scanner.nextInt();
        int M = scanner.nextInt();

        HashMap<Integer, Integer> hashMap = new HashMap<>();

        for(int i=0; i<M; i++){
            int a = scanner.nextInt();
            int b = scanner.nextInt();

            if(hashMap.containsKey(a)){
                int tmp = hashMap.get(a);
                tmp += 1;
                hashMap.put(a, tmp);
            }else{
                hashMap.put(a, 1);
            }

            if(hashMap.containsKey(b)){
                int tmp = hashMap.get(b);
                tmp += 1;
                hashMap.put(b, tmp);
            }else{
                hashMap.put(b, 1);
            }
        }

        for(int i=1; i<=N; i++){
            if(hashMap.containsKey(i)){
                System.out.println(hashMap.get(i));
            }else{
                System.out.println(0);
            }
        }
    }
}


Measured memory_bytes: 21844
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
# stdin = open("sample.txt")

MOD = 1000000007
INF = float('inf')

def keta(kazu):
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def combination(m,n):
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  # (x**n)の計算の高速化
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x
    
def main():
    x,n = [int(x) for x in stdin.readline().rstrip().split()]
    
    if n == 0:
        print(x)
    else:
        table = [i - 1 for i in range(103)]

        target = INF
        p = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
        for i in range(len(p)):
            table.remove(p[i])
        for i in range(len(table)):
            target = min(target,abs(x-table[i]))
        ans_a = x-int(target)
        ans_b = x+int(target)
        if ans_a in table:
            print(ans_a)
        else:
            print(ans_b)
        
main()

Measured memory_bytes: 9992
#include <bits/stdc++.h>
#define DUMP(x)  cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define IFOR(i, m, n) for(int i = n - 1; i >= m; i-- )
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define FOREACH(x,a) for(auto& (x) : (a) )
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
const ll INF = 1e18;
/* テンプレートここまで */

int main() {
  ll n; cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  sort(ALL(a));
  cout << a[n-1] - a[0] << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
using namespace std;
int main()

{
    int n;
    cin>>n;
    int a,b,c;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b>>c;
        int d=a*a;
        int e=b*b;
        int f=c*c;
        if((d==e+f)||(e==d+f)||(f==d+e))cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


Measured memory_bytes: 3064
/*
 * Analyzing Login/Logout Records
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148
 */

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define REP(i, n) for(int i=0; i<(int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  int N, M;
  while(cin >> N >> M, N | M) {
    vector<vector<pii>> use(M);
    vi tim(M, 2000);
    vi num(M, 0);
    int r;
    cin >> r;
    REP(i, r){
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m--;
      tim[m] = min(tim[m], t);
      num[m]+= s * 2 - 1;
      if(num[m] == 0) {
        use[m].push_back({tim[m], t});
        tim[m] = 2000;
      }
    }

    int q;
    cin >> q;
    REP(i, q){
      int a, b, m;
      cin >> a >> b >> m;
      m--;
      int ans = 0;
      for(pii t : use[m]) {
        ans += max(min(t.second, b) - max(t.first, a), 0);
      }
      cout << ans << endl;
    }
  }
}

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

ll N, A, B, C, D;

int main() {
    scanf("%lld %lld %lld %lld %lld", &N, &A, &B, &C, &D);
    for (ll i = 0; i < N; i++) {
        ll s1 = A + i*C, e1 = A + i*D;
        ll s2 = B + (N-i-1)*C, e2 = B + (N-i-1)*D;
        if (s1 <= e2 && s2 <= e1) {
            printf("YES\n");
            return 0;
        }
    }
    printf("NO\n");
}

Measured memory_bytes: 256
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <bits/stdc++.h>  
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define vl vector<ll>
#define N 100005
#define mod 1000000007
ll prime[1000001];

int main()
{
    ll n;cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];
    for(ll i=1;i<=1000000;i++)
    {
        prime[i]=i;
    }
    for(ll i=2;i<=1000000;i++)
    {
        for(ll j=i;j<=1000000;j+=i)
        {
            prime[j]=min(prime[j],i);
        }
    }
    ll p=0;map<ll,ll>m;
    for(ll i=n-1;i>=0;i--)
    {
        if(a[i]!=1)
        {
            ll x=a[i];
            while(x!=1)
            {
                if(m[prime[x]]>0)
                {
                    p=1;
                }
                x/=prime[x];
            }
            x=a[i];
            while(x>1)
            {
                m[prime[x]]=1;
                x/=prime[x];
            }
        }
    }
    if(!p)
    cout<<"pairwise coprime"<<"\n";
    else
    {
        ll g=0;
        for(ll i=0;i<n;i++)
        {
            g=__gcd(g,a[i]);
        }
        if(g==1)
        cout<<"setwise coprime"<<"\n";
        else
        cout<<"not coprime"<<"\n";
    }
    return 0;
}

Measured memory_bytes: 24008
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let s: String = read();
    println!("{}", s.chars().filter(|&c| c == '1').count());
}

Measured memory_bytes: 4352
#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/integer/common_factor.hpp>
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using std::string;
using std::vector;
using std::set;
using std::multiset;
using std::unordered_set;
using std::map;
using std::multimap;
using std::unordered_map;
using std::pair;
using std::cin;
using std::cout;

using boost::multiprecision::cpp_int;
using cpp_dec_float_1000 = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1000>>;

typedef uintmax_t    ull;
typedef intmax_t     ll;
typedef uint64_t     ul;
typedef uint32_t     ui;
typedef uint8_t      uc;

constexpr char CRLF  = '\n';
constexpr char SPACE = ' ';
constexpr char VECTOR_COUT_SEPARATOR = SPACE;
constexpr ll INF = 1000'000'007;
constexpr int MOD = 1000'000'007;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> std::ostream& operator<< (std::ostream& os, const std::vector<T>& vc) { for(auto it = vc.begin(); it != vc.end(); ++it) { if (std::next(it) == vc.end()) os << *it; else os << *it << VECTOR_COUT_SEPARATOR; } return os; }
template<class T1, class T2> inline std::ostream & operator<< (std::ostream & os, const std::pair<T1, T2> & p) { return os << p.first << ' ' << p.second; }
template<class T> T modinv(T a, T m) { T b = m, u = 1, v = 0; while (b) { T t = a / b; a -= t * b; boost::swap(a, b); u -= t * v; boost::swap(u, v); } u %= m; if (u < 0) u += m; return u; }

struct Tmp
{
public:
  Tmp(int N);
};

Tmp::Tmp(int N){}

void solve(void)
{
  int X, A, B; cin >> X >> A >> B;
  X -= A;
  X %= B;
  cout << X << CRLF;
  return;
}

int main(void)
{
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);

  solve();

  return 0;
}


Measured memory_bytes: 256
#include <vector>
#include <iostream>
#include <algorithm>
#define INF (1LL << 60)
using namespace std;
int n, u[2], mode, tar; long long d, x[30], y[30], ret, seg[16777216], val[8388608]; pair<long long, long long> t[2][8388608];
inline void rec(int pos, long long a, long long b) {
    if (pos == tar) {
        if(b <= 0) t[mode][u[mode]++] = make_pair(a, b);
        return;
    }
    rec(pos + 1, a + x[pos], b + y[pos]);
    rec(pos + 1, a - x[pos], b - y[pos]);
    if(pos % 5 != 0) rec(pos + 1, a, b);
}
inline long long query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b) return seg[k];
    long long lch = query(a, b, k << 1, l, (l + r) >> 1);
    long long rch = query(a, b, (k << 1) + 1, (l + r) >> 1, r);
    return min(lch, rch);
}
int main() {
    scanf("%d%lld", &n, &d);
    for (int i = 0; i < n; i++) scanf("%lld%lld", &x[i], &y[i]);
    mode = 0; tar = n / 2; rec(0, 0, 0);
    mode = 1; tar = n; rec(n / 2, 0, 0);
    sort(t[1], t[1] + u[1]);
    for (int i = 0; i < u[1]; i++) val[i] = t[1][i].first;
    for (int i = 0; i < u[1]; i++) seg[i + 8388608] = t[1][i].second;
    for (int i = u[1]; i < 8388608; i++) seg[i + 8388608] = INF;
    for (int i = 8388608; i >= 1; i--) seg[i] = min(seg[2 * i], seg[2 * i + 1]);
    for (int i = 0; i < u[0]; i++) {
        int pl = lower_bound(val, val + u[1], -t[0][i].first - d) - val;
        int pr = upper_bound(val, val + u[1], -t[0][i].first + d) - val;
        long long res = query(pl, pr, 1, 0, 8388608);
        ret = max(ret, -t[0][i].second - res);
    }
    printf("%lld\n", ret);
    return 0;
}

Measured memory_bytes: 217328
N, M  = map(int, input().split())

d = dict.fromkeys(range(1, N+1), 0)

for i in range(M):
    x, y  = map(int, input().split())
    d[x] += 1
    d[y] += 1

for k, v in sorted(d.items()):
    print(v)

Measured memory_bytes: 3060
#include<iostream>
#include<vector>
#include<unordered_map>

using namespace std;

class UnionFind {
public:
  	vector<int> parents, ranks;
    UnionFind(int n) {
      	parents.resize(n);
      	ranks.resize(n);
        for (int i = 0; i < n; ++i)
            parents[i] = i;
    }
    
    int find(int x) {
        if (x != parents[x])
            parents[x] = find(parents[x]);
        return parents[x];
    }
    
    bool unite(int x, int y) {
        int px = find(x);
        int py = find(y);
        if (px == py) return false;
      
        if (ranks[px] > ranks[py])
            parents[py] = px;
        else if (ranks[px] < ranks[py])
            parents[px] = py;
        else {
            parents[py] = px;
            ranks[px]++;
        }
      
      	return true;
    }
};

int main() {
  int n, m, k, a, b;
  cin >> n >> m >> k;
  int ans[n] = {};
  UnionFind uf(n);
  vector<int> friendships(n), blockships(n);
  
  while(m--) {
    cin >> a >> b;
    friendships[a-1]++;
    friendships[b-1]++;
    uf.unite(a-1, b-1);
  }
  
  while(k--) {
    cin >> a >> b;
    if (uf.find(a-1) == uf.find(b-1)) {
      blockships[a-1]++;
      blockships[b-1]++;
    }
  }
  
  unordered_map<int, int> infos;
  for (int i = 0; i < n; ++i) infos[uf.find(i)]++;
  for (int i = 0; i < n; ++i) {
    if (infos[uf.parents[i]] >= 3) 
    	ans[i] = infos[uf.parents[i]] - friendships[i] - blockships[i] - 1;
    cout << ans[i] << ' ';
  }
  cout << '\n';
  
  return 0;
}

Measured memory_bytes: 6436
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int pow3(int N){
    int res = 1;
    for(int i=0;i<N;i++) res *= 3;
    return res;
}

pair<vector<int>, vector<int>> solve(int N, const string& T){
    if(N==0){
        return make_pair(vector<int>(1, 0), vector<int>(T.size(), 0));
    }
    auto prev = solve(N-1, T);
    auto& P = prev.first;
    auto& W = prev.second;
    vector<int> D(3*P.size());
    for(int i=0;i<D.size();i++) D[i] = i/P.size();
    int salsa = 0;
    vector<int> lastSalsa(3*P.size(), 0);
    vector<int> curW(W.size());
    for(int i=0;i<T.size();i++){
        for(int j=W[i];j<D.size();j+=P.size()){
            if(salsa != lastSalsa[j]) D[j] = (3-D[j])%3;
            lastSalsa[j] = salsa;
            if(D[j] == 2) curW[i] = j;
        }
        if(T[i] == 'S'){
            salsa = 1 - salsa;
        } else {
            for(int j=W[i];j<=D.size();j+=P.size()) D[j] = (D[j]+1)%3;
        }
    }
    vector<int> curP(D.size());
    for(int i=0;i<curP.size();i++){
        if(salsa != lastSalsa[i]) D[i] = (3-D[i])%3;
        curP[i] = P.size() * D[i] + P[i%P.size()];
    }
    return make_pair(curP, curW);
}

int main(){
    int N; cin >> N;
    string T; cin >> T;
    auto res = solve(N, T).first;
    cout << res[0];
    for(int i=1;i<res.size();i++) cout << " " << res[i];
    cout << endl;
}

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

class Dice {
    public:
    int a, s, e, w, n, b;
    int r[6] = {1, 2, 3, 4, 5, 6};
    void setint(int a1, int s1, int e1, int w1, int n1, int b1);
    int geta();
    int gets();
    int getn();
    int gete();
    int getw();
    int getb();
    void moveS();
    void moveN();
    void moveE();
    void moveW();
};

void Dice::setint(int a1, int s1, int e1, int w1, int n1, int b1) {
    a = a1;
    s = s1;
    n = n1;
    e = e1;
    w = w1;
    b = b1;
}

int Dice::geta() {
    return a;
}
int Dice::gets() {
    return s;
}
int Dice::getn() {
    return n;
}
int Dice::gete() {
    return e;
}
int Dice::getw() {
    return w;
}
int Dice::getb() {
    return b;
}
void Dice::moveS() {
    r[0] = getn();
    r[1] = geta();
    r[4] = getb();
    r[5] = gets();
    setint(r[0], r[1], r[2], r[3], r[4], r[5]);
}
void Dice::moveE() {
    r[0] = getw();
    r[2] = geta();
    r[3] = getb();
    r[5] = gete();
    setint(r[0], r[1], r[2], r[3], r[4], r[5]);
}
void Dice::moveW() {
    r[0] = gete();
    r[2] = getb();
    r[3] = geta();
    r[5] = getw();
    setint(r[0], r[1], r[2], r[3], r[4], r[5]);
}
void Dice::moveN() {
    r[0] = gets();
    r[1] = getb();
    r[4] = geta();
    r[5] = getn();
    setint(r[0], r[1], r[2], r[3], r[4], r[5]);
}

int main(void){
    // Here your code !
    Dice dice;
    int n[6];
    for(int i=0;i<6;i++) {
        cin >> n[i];
    }
    dice.setint(n[0], n[1], n[2], n[3], n[4], n[5]);
    int p, q;
    int k1, k2;
    int t[4];
    cin >> p;
    for(int i=0;i<p;i++) {
        cin >> k1 >> k2;
        for(int j=0;j<6;j++) {
            if(k1 == n[j]) {
                q = j+1;
            }
        }
        switch(q) {
                case 1:
                    t[0] = n[1];
                    t[1] = n[3];
                    t[2] = n[4];
                    t[3] = n[2];
                    break;
                case 2:
                    t[0] = n[5];
                    t[1] = n[3];
                    t[2] = n[0];
                    t[3] = n[2];
                    break;
                case 3:
                    t[0] = n[5];
                    t[1] = n[1];
                    t[2] = n[0];
                    t[3] = n[4];
                    break;
                case 4:
                    t[0] = n[1];
                    t[1] = n[5];
                    t[2] = n[4];
                    t[3] = n[0];
                    break;
                case 5:
                    t[0] = n[0];
                    t[1] = n[3];
                    t[2] = n[5];
                    t[3] = n[2];
                    break;
                case 6:
                    t[0] = n[4];
                    t[1] = n[3];
                    t[2] = n[1];
                    t[3] = n[2];
                    break;
            }
        for(int j=0;j<4;j++) {
            if(k2 == t[j]) {
                cout << t[(j+3)%4] << endl;
            }
        }
    }
}

Measured memory_bytes: 3076
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>

#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long int
#define MAX 1000000007

using namespace std;

int main(void){
  int k,n;
  cin >> k >> n;
  int a[n+2];
  rep(i,n) cin >> a[i+1];
  a[n+1] = k+a[1];
  a[0] = a[n]-k;

  int ans = k;
  rep(i,n+1){
    if(k-(a[i+1]-a[i]) < ans)ans = k-(a[i+1]-a[i]);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 1024
#include <stdio.h>
int main(){
int n,i,sum=0;
scanf("%d",&n);
int v[20],c[20];
for(i=0;i<n;i++)
scanf("%d",&v[i]);
for(i=0;i<n;i++)
scanf("%d",&c[i]);
for(i=0;i<n;i++){
if(v[i]>c[i])
sum+=v[i]-c[i];
}
printf("%d",sum);
return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<stack>
#include<iostream>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define MOD 1000000007
#define vec(a) vector<int>a
using namespace std;


int main()
{
#ifndef ONLINE_JUDGE
	freopen("output.txt", "w", stdout);
	freopen("input.txt", "r", stdin);
#endif
	fastio
	ll n,i,j,k,l,m;
	cin>>n;
	ll arr[n];
	k=0;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		if(arr[i]%2==0)
		{
			if(arr[i]%3==0 || arr[i]%5==0)
				continue;
			k=1;
			break;
		}
	}
	if(k==1)
		cout<<"DENIED\n";
	else
		cout<<"APPROVED\n";

	return 0;
}
	

Measured memory_bytes: 3656
#include <iostream>

using namespace std;

int main()
{
    string s;
    cin >> s;
    int c = 0;
    int c_max = 0;
    for (int i = 0; i < 3; i++)
    {
        if (s[i] == 'R')
        {
            c++;
        }
        else
        {
            c = 0;
        }
        if (c > c_max)
        {
            c_max = c;
        }
    }
    cout << c_max << endl;
    return 0;
}

Measured memory_bytes: 3556
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=200005;
int n,t[MAXN];
char c[MAXN];
long long ans;
int main(){
	scanf("%s",c+1);n=strlen(c+1);
	if(c[n]=='B')t[n]=n;else t[n]=n+1;
	for(int i=n-1;i>=1;i--){
		if(c[i]=='B')t[i]=i;else t[i]=t[i+1];
		if(c[i]=='B'&&c[i+1]=='W')
			c[i]='W',c[t[i+1]-1]='B',ans+=t[i+1]-i-1,t[i]=t[i+1]-1;
	}
	printf("%lld\n",ans);
}


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

int main() {
    int n; cin >> n;
    vector<int> a(n);
    int sum = 0;
    rep(i, n) {
        cin >> a[i];
        sum += a[i];
    }
    int avg_s = sum/n;
    int avg_b = (sum + (n-1)) / n;
    
    int ans_s = 0;
    int ans_b = 0;
    rep(i, n) {
        ans_s += abs(a[i] - avg_s) * abs(a[i] - avg_s);
        ans_b += abs(a[i] - avg_b) * abs(a[i] - avg_b);
    }
    cout << (min(ans_s, ans_b)) << endl;
}


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

#define ll long long
#define tc(t) int t;cin >> t; while(t--)
#define pb push_back
#define fi first
#define se second
#define debug1(x) cerr << #x << " = " << x << '\n';
#define debug2(x, y) cerr << #x << " = " << x << "   " << #y << " = " << y << "\n";
#define _ ios_base::sync_with_stdio(0);cin.tie(NULL);

void solve() {
    int n, r;
    cin >> n >> r;
    if(n >= 10) {
        cout << r << '\n';
        return;
    }
    else {
        int ans = r + 100 * (10 - n);
        cout << ans << '\n';
    }
}

int main() {_
    int t = 1;
    //cin >> t;
    // Please remember to uncomment for taking test cases
    for(int i = 1; i<=t; i++) {
        solve();
    }
    return 0;
}


Measured memory_bytes: 256
n = gets.to_i
h = gets.split.map(&:to_i)
dp = Array.new(n,0)
dp[1] = (h[0]-h[1]).abs
(2...n).each do |i|
    a = dp[i-1]+(h[i]-h[i-1]).abs
    b = dp[i-2]+(h[i]-h[i-2]).abs
    dp[i] = (a < b ? a : b)
end
puts dp[n-1]

Measured memory_bytes: 9604
#include  <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1000000007
//P pa(114,514);   ,vector<P>vec(N,0);
//vector<vector<int>> vec(縦, vector<int>(横));
#define REP(i,N) for(int i=0;i<N;i++)
int gcd(int a, int b) {
    if(a>b){
        swap(a,b);
        }
    while(b%a!=0){
        b%=a;
        swap(a,b);
    }
    return a;
}
//cout << setprecision(123);
int main(){
  int a,b;
  cin>>a>>b;
  if((a+b)%2==0){
    cout<<abs((a+b)/2)<<endl;
  }
  else{
    cout<<"IMPOSSIBLE"<<endl;
  }
}

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

#define PI 3.141592653589
#define ll long long int
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll,ll>
#define vv vector
int MOD=1e9+7;
ll power(ll a, ll b){//a^b
    ll res=1;
    a=a%MOD;
    while(b>0){
        if(b&1){res=(res*a)%MOD;b--;}
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll fermat_inv(ll y){return power(y,MOD-2);}
ll gcd(ll a, ll b){return (b==0)?a:gcd(b,a%b);}
ll min(ll a,ll b){return (a>b)?b:a;}
ll max(ll a,ll b){return (a>b)?a:b;}
bool prime[1000001];
vi primes;
void SieveOfEratosthenes(int n) 
{ 
    memset(prime, true, sizeof(prime)); 
    for (int p=2; p*p<=n; p++) 
    { 
        if (prime[p] == true) 
        { 
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
    for(int p=2;p<1000001;p++)
        if(prime[p])
            primes.pb(p);
}
ll fact[1001];
void factorial(int n){
    fact[0]=1;
    for(int i=1;i<=n;i++)
        fact[i]=fact[i-1]*i,fact[i]%=MOD;
}
ll ncr(ll n,ll r)
{
    if(n<r)
        return -1;
    else{
        ll x=fact[r]*fact[n-r]%MOD;
        return fact[n]*fermat_inv(x)%MOD;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t=1;
    //cin>>t;
    //SieveOfEratosthenes(1000000);
    //factorial(1000);
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
            cin>>a[i];
        ll ans=0,mn=a[0];
        for(int i=1;i<n;i++)
            ans+=max(0,mn-a[i]),mn=max(mn,a[i]);
        cout<<ans;
    }
}

Measured memory_bytes: 4420