akhauriyash/Code-Regression huggingface dataset:
#include <iostream>

using namespace std;

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

    int ans = 0;
    for(int i = 0; i < s.size() - 2; i++) {
        if(s.substr(i, 3) == "ABC") ans++;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
N, M, X = map(int,input().split())
C = []
A = []
for _ in range(N):
    c, *a = map(int, input().split())
    C.append(c)
    A.append(a)

ans = -1
for i in range(2**N):
    c = 0
    R = [0 for _ in range(M)]
    for j in range(N):
        if (i>>j) % 2:
            c += C[j]
            for m in range(M):
                R[m] += A[j][m]
    flag = True
    for m in range(M):
        if R[m] < X:
            flag = False
            break
    if flag:
        if ans == -1:
            ans = c
        elif ans > c:
            ans = c

print(ans)

Measured memory_bytes: 9172
#include<bits/stdc++.h>
#define ll long long
#define ii pair<int, int>
#define pll pair<ll, ll>
#define N 200123
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define vpll vector<pll>
#define pb emplace_back
#define fs first
#define sc second
#define sz(x) ((int)((x).size()))
#define oo 1000000123
#define mod 1000000007LL
#define M 10000123

using namespace std;


int main(){
    ios_base::sync_with_stdio(0);
    ll a, b;
    cin >> a >> b;
    cout << a*b << '\n';
    return 0;
}


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

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

    vector<pair<int,int>> ball(N);
    for (int i = 0; i < N; i++) {
        int x,y; cin >> x >> y;
        ball[i] = make_pair(x,y);
    }
    sort(ball.begin(), ball.end());

    map<pair<int,int>,int> cnt;
    for (int i = 0; i < N-1; i++) {
        for (int j = i+1; j < N; j++) {
            int x = ball[j].first - ball[i].first;
            int y = ball[j].second - ball[i].second;
            //cout << "x = " << x << "y = " << y << endl;
            cnt[make_pair(x,y)]++;
            //cout << cnt[make_pair(x,y)] << endl;
        }
    }

    int ans = 0;
    for(auto it = cnt.begin(); it != cnt.end(); it++) {
        ans = max(ans, it->second);
    }
    ans = N - ans;
    cout<< ans <<endl;
}

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

int main() {
  int N;
  cin >> N;
  1 <= N <= 10000;
  int A;
  cin >> A;
  0 <= A <= 1000;
  int a;
  a = N % 500;
  if( a <= A ) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
}


Measured memory_bytes: 256
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def main():
    A, B, C = map(int, input().split())

    ans = INF
    if A % 2 == 0 or B % 2 == 0 or C % 2 == 0:
        ans = 0

    ans = min(ans, B * C)
    ans = min(ans, A * C)
    ans = min(ans, A * B)

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3316
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <climits>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

#define INF INT_MAX/3
#define REP(i,n) for(int i=0;i<n;i++)

ll mod_num = 998244353;

//ll dp2[200001][200001] = {0};

ll repeatSquare(ll a,ll n){
	ll answer = 1;

	while(n > 0){
		if(n & 1){	//nが奇数なら先に答えの方にa^1をかけておく事で残りのa^n-1を2分割できるようにしている
			answer = answer * a % mod_num;
		}
		a = a * a % mod_num;
		n = n>>1;	//nを2で割ると同じ
	}
	return answer;
}

ll junretu(ll num,ll p){
//	if(dp1[num][p] != 0)return dp1[num][p];
	if(num < p) return 0;
	ll ans = 1;
	REP(i,p){
		ans *= (num - i);
		ans = ans % mod_num;
	}
//	return dp1[num][p] = ans;
	return ans;
}

ll conv(ll num,ll p){
//	if(dp2[num][p] != 0)return dp2[num][p];
	if(num < p) return 0;
	ll ans = 1;
	for(ll i = 0; i < p; i++){
		ans = ans * (num-i) % mod_num;
		ans = ans * (repeatSquare(i+1,mod_num-2) % mod_num) % mod_num;
	}
//	return dp2[num][p] = ans;
	return ans;
}

int main(){
	ll N,M,K;
	cin>>N>>M>>K;
	ll ans = 0;
	ll conv_res = 1;//N-1,i
	ans = (M * repeatSquare(M-1,N-1))  % mod_num;
	for(ll i = 1; i <=K; i++){
		ll ans1 = (M * repeatSquare(M-1,N-i-1))  % mod_num;
		conv_res = (conv_res * (N - 1 - (i-1)) )% mod_num;
		conv_res = (conv_res * repeatSquare(i,mod_num-2) % mod_num) % mod_num;
		ans = (ans + ans1 * conv_res) % mod_num;
		
	}
	cout<<ans<<endl;
	return 0;
}



Measured memory_bytes: 3636
let n = stdin.ReadLine() |> int
let dx = stdin.ReadLine().Split(' ') |> Array.map int

let mutable restChoco = dx.[1]

for i in [1..n] do
    let span = stdin.ReadLine() |> int
    restChoco <- restChoco + 1 + ((dx.[0]-1)/span)
done

restChoco
|> printfn "%d"

Measured memory_bytes: 14044
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
	ll n; cin >> n;
	bool a = 1;
	for(ll i = 0; i < n; ++i){
		ll b; cin >> b;
		a ^= (b & 1);
	}
	cout << (a ? "YES\n" : "NO\n");
	return 0;
}

Measured memory_bytes: 256
H, W, A, B = map(int, input().split())
mod = int(1e+9 + 7)
p = mod - 2
X, Y = H-A, W-B
L = []
while p != 0:
  L = [p%2] + L[:]
  p //= 2
S = 0
fL = [1]
invfL = []
for i in range(H+W):
  fL.append(((i+1)*fL[i])%mod)
for i in range(H):
  invi = 1
  for j in range(len(L)):
    if L[j] == 1:
      invi *= fL[i]
      invi %= mod
    if j != len(L)-1:
      invi *= invi
      invi %= mod
  invfL.append(invi)
invB = 1
invW = 1
for i in range(len(L)):
  if L[i] == 1:
    invB *= fL[B-1]
    invB %= mod
    invW *= fL[W-B-1]
    invW %= mod
  if i != len(L)-1:
    invB *= invB
    invB %= mod
    invW *= invW
    invW %= mod
for i in range(X):
  S += (fL[i+B-1]*fL[H+Y-i-2]*invfL[i]*invfL[H-i-1])%mod
  S %= mod
print((S*invB*invW)%mod)

Measured memory_bytes: 15036
#include <bits/stdc++.h>
using namespace std;
 
vector<int> sieve(int n) {
  vector<int> p(n);
  for(int i=2; i<n; i++) p[i]=i;
  for(int i=2; i*i<n; i++) {
    if(p[i]) {
      for(int j=i*i; j<n; j+=i) p[j]=0;
    }
  }
  p.erase(remove(p.begin(),p.end(),0),p.end());
  return p;
}
 
int main() {
  vector<int> p=sieve(1000000);
  int a[2];
  while(cin >> a[0] >> a[1]&&a[0]) {
    int b[2]={0,0};
    for(int j=0; j<2; j++) {
      int k=1;
      for(int i=p.size()-1; i>=0; i--) {
        if(a[j]%p[i]==0) {
          b[j]+=p[i]*k;
          k=-1;
        }
      }
    }
    if(b[0]>b[1]) cout << 'a' << endl;
    else cout << 'b' << endl;
  }
  return 0;
}


Measured memory_bytes: 6536
package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

func main() {
	fmt.Println(resolve(parseProblem(os.Stdin)))
}

func parseProblem(r io.Reader) int {
	const (
		initialBufSize = 100000
		maxBufSize     = 1000000
	)
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	n := scanInt(sc)
	return n
}

func resolve(n int) int {
	if n < 2 {
		return 0
	}
	num := n
	terms := map[int]int{}
	former := int(math.Sqrt(float64(n))) + 1
	for p := range primes(former) {
		pNum := numP(n, p)
		if pNum != 0 {
			terms[p] = pNum
			num /= pow(p, pNum)
		}
	}
	if num != 1 {
		terms[num] = 1
	}
	result := 0
	for k, iN := range terms {
		if iN == 0 {
			continue
		}
		for j := 1; j <= iN; j++ {
			d := pow(k, j)
			if n%d != 0 {
				break
			}
			n /= d
			result++
		}
	}
	return result
}

func pow(n, i int) (res int) {
	res = 1
	for i > 0 {
		if i&1 == 1 {
			res *= n
		}
		i >>= 1
		n *= n
	}
	return res
}

func isPrime(n int) bool {
	max := int(math.Sqrt(float64(n)))
	for i := 2; i <= max+1; i++ {
		if n%i == 0 {
			return false
		}
	}
	return true
}

func numP(n, p int) (i int) {
	for n > 0 {
		if n%p != 0 {
			break
		}
		i++
		n = n / p
	}
	return i
}

func eratosthenes(n int) []bool {
	isPrime := make([]bool, n)
	for i := 2; i < len(isPrime); i++ {
		isPrime[i] = true
	}
	for i := 2; i < len(isPrime); i++ {
		if !isPrime[i] {
			continue
		}
		for j := 2; j*i < len(isPrime); j++ {
			isPrime[j*i] = false
		}
	}
	return isPrime
}

func primes(n int) chan int {
	ch := make(chan int)
	go func() {
		isPrime := eratosthenes(n)
		for i := 2; i < len(isPrime); i++ {
			if !isPrime[i] {
				continue
			}
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}


Measured memory_bytes: 2804
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <numeric>
#include <queue>
#include <map>
#include <math.h>
using namespace std;
typedef long long int ll;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
    ll n; cin >> n;
    ll a,b,c,d; cin >> a >> b >> c >> d;
    bool ok=0;
    ll p=b-a;
    for(ll k=0;k<n;k++){
        if(k*c-(n-1-k)*d<=p&&p<=k*d-(n-1-k)*c)ok=1;
    }
    if(ok)printf("YES\n");
    else printf("NO\n");
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,s,n) for(int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  
  double ans = 0;
  rep(i, 0, n) {
    double x;
    string u;
    cin >> x >> u;
    if(u == "BTC")
      x *= 380000;
    ans += x;
  }    
  
  cout << ans << "\n";

}


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

using namespace std;

const int INF = 1 << 30;

int d, n;
int a[100], e[100], r[100];

int ans;
int dp[101][101];

int solve(int hp, int exp) {

    if(hp <= 0) {
        return 0;
    }

    if(dp[hp][exp] != -1) {
        return dp[hp][exp];
    }

    int ret = INF;
    for(int i = 0; i < n; ++i) {
        if(exp >= r[i]) {
            if(a[i] == 0 && (exp >= 100 || e[i] == 0)) continue;
            ret = min(ret, solve(max(0, hp - a[i]), min(100, exp + e[i])) + 1);
        }
    }

    return dp[hp][exp] = ret;
}

int main() {

    while(cin >> d >> n, d || n) {

        for(int i = 0; i < n; ++i) {
            cin >> a[i] >> e[i] >> r[i];
        }

        memset(dp, -1, sizeof(dp));

        ans = solve(d, 0);

        if(ans == INF) {
            cout << "NA" << endl;
        } else {
            cout << ans << endl;
        }

    }

}

Measured memory_bytes: 3100
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1LL << 60;
const int MAX = 1000000010;
const ll MOD = 1000000007;

int main() {
    int N; cin >> N;
    vector<ll> A(N);
    bool flag2 = false;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        if (A[i] == 0) {
            flag2 = true;
        }
    }
    ll ans = 1;
    
    bool flag = true;
    for (int i = 0; i < N; i++) {
        ll x = ans;
        ans *= A[i];
        if (ans < x || ans > 1000000000000000000) {
            flag = false;
            break;
        }
    }

    if (flag2) {
        cout << 0 << endl;
    } else {
        if (flag) {
            cout << ans << endl;
        } else {
            cout << -1 << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 4048
#include <algorithm>
#include <cstdio>

const int N=100005;
int X[N],Y[N],fst[N],id[N],cnt[N],n,D,tot=0;
bool u[N];
long long ans=0;
struct Edge{int to,nxt;}e[N*8];

void addedge(int x,int y){
	e[++tot].to=y;e[tot].nxt=fst[x];fst[x]=tot;
	e[++tot].to=x;e[tot].nxt=fst[y];fst[y]=tot;
}

bool cmp(int i,int j){
	return X[i]!=X[j]?X[i]<X[j]:Y[i]<Y[j];
}

bool check(int i,int j,int x){
	return X[i]<X[j]+D || (X[i]==X[j]+D && Y[i]<Y[j]+x);
}

void work(int d){
	std::sort(id+1,id+n+1,cmp);
	for (int i=1,j=1,l=1,r=1;i<=n;i++){
		for (;l<=n && check(id[l],id[i],-d);l++);
		for (;r<=n && check(id[r],id[i],d+1);r++);
		cnt[id[i]]+=r-l;
		j=std::max(l,j);
		if (j==r) continue;
		addedge(id[i],id[j]);
		for (;j+1<r;j++) addedge(id[j],id[j+1]);
	}
}

void dfs(int x){
	if (u[x]) return;
	u[x]=1;ans+=cnt[x];
	for (int i=fst[x];i;i=e[i].nxt) dfs(e[i].to);
}

int main(){
	int a,b;
	scanf("%d%d%d\n",&n,&a,&b);
	for (int i=1;i<=n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		X[i]=x+y;Y[i]=x-y;
	}
	D=std::max(abs(X[a]-X[b]),abs(Y[a]-Y[b]));
	for (int i=1;i<=n;i++) id[i]=i;
	work(D);
	for (int i=1;i<=n;i++) std::swap(X[i],Y[i]);
	work(D-1);
	for (int i=1;i<=n;i++) u[i]=0;
	dfs(a);
	printf("%lld\n",ans);
}

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

int main() {
  int N,K;
  cin >> N >> K;
  vector<int>vec(N);
  for(int i=0; i<N; i++){
    cin >> vec[i];
  }
  int ans = 1000000000;
  sort(vec.begin(),vec.end());
  for(int i=0; i<N-K+1; i++){
    ans = min(ans,vec[i+K-1]-vec[i]);
  }
  cout << ans << endl;
}


Measured memory_bytes: 3712
#include<iostream>
#include<string.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
int main(){
    char s[3];
    rep(i, 3){
        cin >> s[i];
    }    
    if(strncmp(&s[0], &s[1],1)==0 && strncmp(&s[1], &s[2],1) ==0){
        cout << "No" << endl;
    }else{
        cout << "Yes" << endl;
    }
}

Measured memory_bytes: 256
#include <iostream>
#include <queue>
using namespace std;
int N, M;
int A[100100];

int main() {
    cin >> N >> M;
    for (int i = 0; i < N; i++) cin >> A[i];
    priority_queue<int> que;
    for (int i = 0; i < N; i++) que.push(A[i]);

    for (int i = 0; i < M; i++) {
        int cost = que.top(); que.pop();
        cost /= 2; 
        que.push(cost);
    }

    long long ans = 0;
    for (int i = 0; i < N; i++) {
        ans += que.top(); que.pop(); 
    }
    cout << ans << endl;
}

Measured memory_bytes: 4260
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll md = 1000000007;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
vl p, fac(2, 1), ifac(2, 1);

ll add(ll a, ll b) {
    return (a%md + b%md + md) % md;
}

ll mul(ll a, ll b) {
    return ( (a%md) * (b%md) ) % md;
}

ll power(ll x, ll n, ll mod = md) {
    ll res = 1;
    x %= mod;
    while (n) {
        if (n & 1)  res = ((res%mod) * (x%mod))%mod;
        n = (n >> 1);
        x = ((x%mod) * (x%mod))%mod;
    }
    return res;
}

void cachefact(ll lim, ll mod = md) {
    for(int i = 2; i <= lim; i++) {
        ifac.push_back((ifac[mod%i] * (mod - mod/i) % mod)%mod);
    }
    for(ll i = 2; i <= lim; i++) {
        fac.push_back((fac[i-1] * i)%mod);
        ifac[i] = (ifac[i-1] * ifac[i])%mod;
    }
}

ll C(int n, int r) {
    if(n < r) return 0;
    return mul(fac[n], mul(ifac[r], ifac[n-r]));
}

ll P(int n, int r) {
	if(n < r) return 0;
	return mul(fac[n], ifac[n-r]);
}

ll mod_fac(ll n, ll p) {
	return fac[n] * power(power(p, n/p), md - 2) % md * power(fac[n/p], md - 2) % md;
}

void multiply(vvl &F, vvl &R, ll N) {
	vvl temp(N, vl (N, 0));
	for(int i = 0; i < N; i++) {
		for(int j = 0; j < N; j++) {
			for(int k = 0; k < N; k++) {
				temp[i][j] = add(temp[i][j], mul(F[i][k], R[k][j]));
			}
		}
	}
	F = temp;
}

void mpow(vvl &F, ll N, ll n) {
	vvl res(N, vl (N, 0));
	for(int i = 0; i < N; i++) res[i][i] = 1;
	while(n) {
		if(n & 1) multiply(res, F, N);
		multiply(F, F, N);
		n /= 2;
	}
	F = res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, cnt = 0;
    long double d;
    cin >> n >> d;
    while(n--) {
    	ll x, y;
    	cin >> x >> y;
    	long double rt = (long double)sqrt(x*x + y*y);
    	if(rt <= d) {
    		cnt++;
    	}
    }
    cout << cnt << "\n";
    return 0;
}

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

void b_hina_arare(void) {
	int n;
	char s[110];
	int flag = 0;
	string judge = "Three";
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		if (s[i] == 'P')      flag |= (0x1 << 0);
		else if (s[i] == 'W') flag |= (0x1 << 1);
		else if (s[i] == 'G') flag |= (0x1 << 2);
		else                  flag |= (0x1 << 3);
	}

	if (flag == 0xf) judge = "Four";
	cout << judge << endl;
}

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

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


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

    int N;
    cin >> N;

    int ans = N%1000;

    if(ans>0) {
        ans = 1000 - ans;
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 3604
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
using ll = long long;

int main(){
    double a, b, x;
    cin >> a >> b >> x;
    double ans = 0;
    if(a * a * b / 2.0 >= x){
        ans = M_PI / 2.0 - atan( 2*x / (a*b*b));
    } else {
        ans = atan(2 * (a*a*b - x) / (a*a*a));
    }
    cout << setprecision(10) << 180.0 * ans / M_PI <<  endl;
    return 0;
}


Measured memory_bytes: 384
import java.util.*;
public class Main
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in); 
    int[] n  = new int[5];
    int r = 0;
    
    for(int i = 0; i < 5; i++)
    {
    	n[i] = sc.nextInt();
    	if(n[i] == 0)
    	{
    		r = i + 1;
    	}
    }
    System.out.println(r);
	//sc.close();
  }
}                          


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


    class Program
    {
        static void Main(string[] args)
        {
            string[] Input = Console.ReadLine().Split(' ');
            int X = int.Parse(Input[0]);
            int Y = int.Parse(Input[1]);
            string ans = "No";
            for(int i = 0; i <= X; i++){
                if((2 * i) + 4 * (X - i) == Y){
                    ans = "Yes";
                    break;
                }
            }
            Console.WriteLine(ans);
        }
    }

Measured memory_bytes: 17912
def comp(a, b):
	c = cmp(len(a), len(b))
	if c == 0:
		c = cmp(a, b)
	return c

n = input()
results = {}
for i in xrange(n):
	name, value = raw_input().split()
	value = int(value)
	results.setdefault(name, 0)
	results[name] += value
for name in sorted(results.keys(), comp):
	print name, results[name]

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

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

  bool ans = false;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      int cnt = M * i + N * j - (i * j * 2);
      if (cnt == K) {
        ans = true;
        break;
      }
    }
  }

  cout << (ans ? "Yes" : "No") << endl;
}

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;


signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  ll n,m; cin>>n>>m;
  priority_queue<pll> q;
  rep(i,n){
    ll a; cin>>a;
    q.emplace(a,1);
  }
  rep(i,m){
    ll b,c; cin>>b>>c;
    q.emplace(c,b);
  }
  
  ll cnt=0;
  ll ans=0;
  while(true){
    auto p=q.top(); q.pop();
    ll tmp=min(n-cnt,p.second);
    ans+=tmp*p.first;
    cnt+=tmp;
    if(cnt>=n) break;
  }
  
  cout<<ans<<endl;
  
 }

Measured memory_bytes: 6384
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
//#include<assert.h>
#include<algorithm> 
#include<iostream>
//#include<bitset>
using namespace std;

int q,a,b,c,d,len;

int calc(int a,int b)
{
	if (!a || !b) return a^b;
	if (a<b) a^=b,b^=a,a^=b;
	return (a-1)/(b+1)+1;
}

bool check(int x)
{
	int cnta=x/(len+1)*len+x%(len+1),cntb=x/(len+1)-(x%(len+1)==0);
//	cout<<x<<' '<<cnta<<' '<<cntb<<endl;
	if (cnta>a) return 0;
	return calc(a-cnta,b-cntb)<=len;
}

void workleft(int left,int right)
{
	for (int i=left;i<=right;i++)
	{
		if (i%(len+1)==0) putchar('B');
		else putchar('A');
	}
}
void workright(int left,int right)
{
	for (int i=left;i<=right;i++)
	{
		if ((a+b-i+1)%(len+1)==0) putchar('A');
		else putchar('B');
	}
}

int main()
{
	scanf("%d",&q);
	while (q--)
	{
		scanf("%d%d%d%d",&a,&b,&c,&d);
		len=calc(a,b);
//		cout<<len<<endl;
		int L=0,R=a+b;
		while (L<R)
		{
			int mid=(L+R+1)>>1;
//			cout<<mid<<endl;
			if (check(mid)) L=mid;
			else R=mid-1;
		}
		int pos=L;
//		cout<<pos<<endl;
		if (d<=pos) workleft(c,d);
		else if (c>pos) workright(c,d);
		else workleft(c,pos),workright(pos+1,d);
		puts("");
	}
	return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
vector<int> v[100005];
long long cnt[2];
void dfs(int node,int pnode,int color)
{
    cnt[color]++;
    for (int i=0;i<v[node].size();i++)
    {
        if (v[node][i]!=pnode)
        dfs(v[node][i],node,!color);
    }
}
int main()
{
   int k,res;
   cin>>k>>res;
   if(k>=10) cout<<res<<endl;
   else{
       int ans=(res)+100*(10-k);
       cout<<ans<<endl;
   }
   
   return 0;
}

Measured memory_bytes: 2560
//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

fn run() {
    input! {
        n: usize,
        e: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for (a, b) in e {
        g[a].push(b);
        g[b].push(a);
    }
    let mut parent = vec![0; n];
    let mut stack = vec![0];
    let mut q = vec![];
    while let Some(v) = stack.pop() {
        q.push(v);
        for &u in g[v].iter() {
            if u != parent[v] {
                parent[u] = v;
                stack.push(u);
            }
        }
    }
    let mut cond = vec![0; n];
    for &v in q.iter().rev() {
        if cond[v] > 0 {
            continue;
        }
        let p = parent[v];
        if cond[p] == 1 || p == v {
            println!("First");
            return;
        }
        assert!(cond[p] == 0);
        cond[p] = 1;
        cond[v] = 2;
    }
    println!("Second");
}

fn main() {
    run();
}


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

int main(){
    
    int m, f, r;
    while(1){
        cin >> m >> f >> r;
        if(m==-1 && f==-1 && r==-1) break;
        
        else if(m==-1 || f==-1) cout << "F" <<endl;
        else if((m+f) >= 80) cout << "A" <<endl;
        else if(((m+f) >= 65) && ((m+f) < 80)) cout << "B" <<endl;
        else if(((m+f) >= 50) && ((m+f) < 65)) cout << "C" <<endl;
        else if(((m+f) >= 30) && ((m+f) < 50)){
            if(r >= 50) cout << "C" <<endl;
            else cout << "D" <<endl;
        } else cout << "F" <<endl;
    }
    return 0;
}


Measured memory_bytes: 3088
def parent(i):
    return i // 2


def left(i):
    return i * 2


def right(i):
    return i * 2 + 1


h = int(input())
a = [-1]
a.extend([int(v) for v in input().split()])

for i in range(1, h+1):
    result = 'node {0}: key = {1}, '.format(i, a[i])
    if parent(i) >= 1:
        result += 'parent key = {0}, '.format(a[parent(i)])
    if left(i) <= h:
        result += 'left key = {0}, '.format(a[left(i)])
    if right(i) <= h:
        result += 'right key = {0}, '.format(a[right(i)])
    print(result)


Measured memory_bytes: 5628
class Point
        attr_accessor :pos,:def_pos
        def initialize(num)
                @def_pos=num
                @pos=num
        end

        def moveto(n)
                @pos=n
        end
end
w=gets.to_i
n=gets.to_i
points=[]

w.times{|i|
        points<<Point.new(i+1)
}
ways=[]
n.times{
        ways << gets.split(',').map(&:to_i)
}
ways.each{|way|
        a=way[0] ; b=way[1]
        pa = points.find_index{|p|p.pos==a}
        pb = points.find_index{|p|p.pos==b}
        points[pa].moveto(b)
        points[pb].moveto(a)
}
points.sort!{|p1,p2|
        p1.pos<=>p2.pos
}
points.each{|p|
        puts p.def_pos
}

Measured memory_bytes: 8660
#ifndef _WIN32
#include<iostream>
#endif
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<string>
#include<stdlib.h>

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

using namespace std;
int M, T, P, R;
int score[51];
int times[51];
int pen[51][11];//
int con[51][11];
int team[11]; 

#define SCORE 0
#define TIME 1
#define ID 2
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(30);
	while (1){
		vector<vector<int>> all;
		cin >> M >> T >> P >> R;
		if (M == 0 && T == 0)break;
		REP(i, 51){
			REP(j, 11){
				pen[i][j] = 0;
				con[i][j] = -1;
			}
			score[i] = 0;
			times[i] = 0;
		}

		REP(i, R){
			int m, t, p, j;
			cin >> m >> t >> p >> j;
			t--;
			if (m <= M){
				if (j == 0){
					con[t][p] = m;
				}
				else{
					pen[t][p] ++;
				}
			}
		}
		REP(i, T){
			REP(j, 11){
				if (con[i][j] != -1){
					score[i]++;
					times[i] += con[i][j] + 20 * pen[i][j];
				}
			}
			all.push_back({ score[i], -times[i], i+1 });
		}
		sort(all.begin(), all.end());
		reverse(all.begin(), all.end());
		REP(i, T){
			cout << all[i][ID];
			if (i == T - 1){
				cout << endl;
			}else{
				if (all[i][SCORE] == all[i + 1][SCORE] && all[i][TIME] == all[i+1][TIME]){
					cout << "=";
				}
				else{
					cout << ",";
				}
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3160
#include <bits/stdc++.h>
 
// defines
#define boost_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define casti static_cast<int>
#define cast static_cast
#define ll long long int
#define vi vector<int>
#define vpi vector<pair<int,int >>
#define v vector
#define forl(n) for(int i=0;i<n;i++)
#define fora(a,b,c) for(int i=a;i<b;i+=c)
 
using namespace std;
 
ll modF=1e18l+1l;
 
// topcoder
 
 
 
//codechef and codeforces and atcoder
 
void solve(){
	ll n;
	cin >> n;
	ll a[n];
	ll prod = 1;
	ll result;
	int h0 = 0;
	forl(n){
		cin >> a[i];
		if(a[i] == 0){
			h0 = 1;
		}
	}
	if(h0){
		cout << "0\n";
		return; 
	}
	forl(n){
		result = (prod * a[i]) % modF;
		if(prod == result / a[i])
			prod = result;
		else{
			prod = -1;
			break;
		}
	}
	cout << prod << "\n";
}
 
 
int main(){
    boost_io;
    solve();
} 

Measured memory_bytes: 4360
import math
n = int(input())
ans = ''
for i in range(math.floor(n / 4) + 1):
  if ans == '':
    m = n
    m -= 4 * i
    if m % 7 == 0:
      ans = 'Yes'
    else:
      next
if ans == '':
  ans = 'No'
print(ans)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
#define rg register
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
const int Maxn = 505;
typedef int arr[Maxn];
//ll Add(ll x, ll y) { return (x += y) >= Mod ? x - Mod : (x < 0 ? x + Mod : x); }
ll min(ll x, ll y) { return x < y ? x : y; }
ll max(ll x, ll y) { return x > y ? x : y; }
inline ll read()
{
	rg ll res = 0, sign = 1;
	rg char ch = getchar();
	while (!isdigit(ch))
	{
		if (ch == '-')
			sign = -1;
		ch = getchar();
	}
	do
	{
		res = (res << 3) + (res << 1) + (ch ^ 48);
		ch = getchar();
	} while (isdigit(ch));
	return res * sign;
}
int n;
arr posn;
int ans[Maxn * Maxn];
struct str
{
	int pos, cnt;
	bool operator<(str A) const { return pos < A.pos; }
} a[Maxn];
pair<int, int> q[Maxn];
int main()
{
	rg int tmp;
	cin >> n;
	for (int i = 1; i <= n; i++)
		a[i] = {read(), i};
	sort(a + 1, a + n + 1);
	for (int i = 1, j = 0; i <= n; i++)
	{
		tmp = a[i].cnt - 1;
		if (tmp)
		{
			for (j++; j < a[i].pos; j++)
				if (!ans[j])
				{
					ans[j] = a[i].cnt;
					if (!--tmp)
						break;
				}
		}
		if (tmp)
		{
			puts("No");
			return 0;
		}
		ans[a[i].pos] = a[i].cnt;
	}
	rg int h = 1, t = 0;
	for (int i = 1, j = 1; i <= n * n; i++)
		if (a[j].pos == i)
		{
			if (n - a[j].cnt > 0)
				q[++t] = mp(a[j].cnt, n - a[j].cnt);
			j++;
		}
		else if (!ans[i])
		{
			if (h > t)
			{
				puts("No");
				return 0;
			}
			ans[i] = q[h].first;
			q[h].second--;
			if (!q[h].second)
				h++;
		}
	puts("Yes");
	for (int i = 1; i <= n * n; i++)
		cout << ans[i] << ' ';
	putchar('\n');
	return 0;
}


Measured memory_bytes: 2176
#include <bits/stdc++.h>
using namespace std;
#define REP(name, stop) for (ll(name) = 0; (name) < (stop); (name)++)
using ll = long long;
using vi = vector<ll>;
constexpr ll K_MAX = 200;
ll dp[K_MAX + 1][K_MAX + 1] = {0};

ll gcd(ll a, ll b) {
    if (a < b) swap(a, b);
    if (dp[a][b]) return dp[a][b];
    while (a % b) {
        ll t = a;
        a = b;
        b = t % b;
    }
    dp[a][b] = b;
    return b;
}

int main(void) {
    ll K;
    cin >> K;
    ll ans = 0;
    for (ll a = 1; a <= K; a++) {
        for (ll b = 1; b <= K; b++) {
            for (ll c = 1; c <= K; c++) {
                ans += gcd(gcd(a, b), c);
            }
        }
    }
    cout << ans << endl;

    return 0;
}


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

using namespace std;
typedef long long LL;
const int mod=1000000007;

int main(){
  int N;
  cin >> N;
  LL ans=1;
  rep(i,N){
    ans*=i+1;
    ans%=mod;
  }
  cout << ans << endl;

  return 0;
}


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

using ll = long long;

const int INF = 1001001001;
const ll INF_LL = 1001001001001001001LL;

int main(void){
  vector<int> a(3);rep(i,3) cin >> a[i];
  int test = 0;
  rep(i,3) test+=a[i];
  if(test>=22) cout << "bust" << endl;
  else cout << "win" << endl;
    
  return 0;
}


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

int main()
{
    int N;
    cin >> N;
    int A[N];
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
    }
    int index = 0;
    for (int i = 0; i < N; i++)
    {
        if (A[i] == index + 1)
            index++;
    }
    if (index == 0)
        cout << -1 << endl;
    else
        cout << N - index << endl;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin>>s;
  string ans="";
  int j=0;
  for(int i=0;i<s.length();i++){
    if(s[i]=='B'){
      if(ans.length()>1){
       string temp;
        for(int k=0;k<ans.length()-1;k++){
          temp+=ans[k];
         }
        ans=temp;
      }
      else{
        ans="";
      }
    }
    else{
      ans+=s[i];
    }
  }
  cout<<ans;
}

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

using namespace std;
map<int,int> a;
int main() {
	int n, k, q, t;
	cin >> n >> k >> q;
	for(int i = 0; i < q; i ++){
		cin >> t;
		a[t] ++;
	}
	for(int i = 1; i <= n; i ++){
		if(k - (q - a[i]) > 0) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}

Measured memory_bytes: 5376
import javax.swing.plaf.IconUIResource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.IllegalCharsetNameException;
import java.util.*;
public class Main {

     public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int n=sc.nextInt();
        if (n==0){
            System.out.println(1);
        }
        else if (n==1){
            System.out.println(0);
        }






     }

    static void sort(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }
    static void sortReverse(long[] a) {
        ArrayList<Long> l=new ArrayList<>();
        for (long i:a) l.add(i);
//        Collections.sort.(l);
        Collections.sort(l,Collections.reverseOrder());
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long[] readArrayLong(long n) {
            long[] a=new long[(int)n];
            for (int i=0; i<n; i++) a[i]=nextLong();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }


}

Measured memory_bytes: 32668
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <chrono>
#include <stack>
#include <string>
#include <bitset>
#include <unordered_map>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <tuple>
#include <iomanip>
#include <random>
#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

#define all(x) (x).begin(),(x).end()
#define ll long long
#define sz(x) (int)(x.size())
#define pb push_back
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define endl "\n"
#define int long long

const int N = (int)(1e6) + 322;
const long long inf = (long long)(1e18) + 123;
const int mod = (int)(1e9) + 7;
const double EPS = 1e-9;

int n, full;
vector <pair <int, bool>> v[N];

bool bit(int mask, int i) {
    return (mask >> i) & 1;
}

signed main() {
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    full = (1 << n) - 1;
    for(int i = 0; i < n; ++i) {
        int cnt; cin >> cnt;
        for(int j = 1; j <= cnt; ++j) {
            int x, y; cin >> x >> y;
            x--;
            v[i].pb({x, y});
        }
    }
    int mx = 0;
    for(int mask = 0; mask <= full; ++mask) {
        bool flag = true;
        for(int i = 0; i < n; ++i) {
            if (bit(mask, i)) {
                for(auto it : v[i]) {
                    if (bit(mask, it.first) != it.second) flag = false;
                }
            }
        }
        if (flag) mx = max(mx, (int)__builtin_popcount(mask));
    }
    cout << mx;

    return 0;
}

Measured memory_bytes: 23680
#include <bits/stdc++.h>
using namespace std;
 //Compiler version g++ 6.3.0

 int main()
 {
  int n;
  int ans=0;
  string s;
  cin >> n >> s;
  for(int i=1;i<s.size();i++){
  	map<char,int> P;
 		int p=0;
 		for(int j=0;j<i;j++){
 		char a=s[j];
 		P[a]=1;
 		}
  	for(int k=i;k<s.size();k++){
 		char b=s[k];
 		if(P[b]==1){
		 p++;
 		P[b]=0;
 			}
 		}
 		ans=max(ans,p);
 	}
 	cout<<ans<<endl;
}

Measured memory_bytes: 256
/**
 *      purpose : 
 *      author  : kyomukyomupurin
 *      created : 
**/

// input/output
#include <iostream>
#include <fstream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstring>
#include <chrono>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = int64_t;
using pii = pair<int, int>;
using pll = pair<int64_t, int64_t>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
// variadic template
template<typename T> inline void chmin(T &a, T b) {if (a > b) a = b; return;}
template<typename T> inline void chmax(T &a, T b) {if (a < b) a = b; return;}

const int MOD = 1e9 + 7;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w; cin >> h >> w;
  string a[h];
  for (int i = 0; i < h; ++i) cin >> a[i];
  int dp[1010][1010];
  fill(dp[0], dp[1010], 0);

  dp[1][1] = 1;

  for (int i = 2; i <= h; ++i) {
    if (a[i - 1][0] == '#') break;
    else dp[i][1] = 1;
  }

  for (int i = 2; i <= w; ++i) {
    if (a[0][i - 1] == '#') break;
    else dp[1][i] = 1;
  }

  for (int i = 2; i <= h; ++i) {
    for (int j = 2; j <= w; ++j) {
      if (a[i - 1][j - 1] == '#') dp[i][j] = 0;
      else dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
    }
  }

  print(dp[h][w] % MOD);

  return 0;
}

Measured memory_bytes: 5248
#include <bits/stdc++.h>
using namespace std; typedef long double ld; typedef long long ll;
typedef unsigned long long ull;
#define MP make_pair
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(x,arr) for(auto& x:arr)
#define VI vector<int>
#define PII pair<int, int>
#define FI first 
#define SE second
#define ALL(x) (x).begin(), (x).end()
const int INF=1<<30; const ll LINF=1LL<<60 ; const ll MOD=1e9+7 ;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//-------------------
int n;
int A[100000+5];
deque<ll> dq;
vector<pair<ll, ll>> v;

void solve(){
	//& = dq.front();
	ll l = dq.front(); dq.pop_front();
	ll r = dq.back(); dq.pop_back();
	ll nt_l = l-r;
	ll nt_r = r-l;
	if(dq.back()-nt_l >= nt_r-dq.front()){
		dq.push_front(nt_l);
		//cout << nt_l ;
		v.push_back(MP(l,r));
	}else{
		dq.push_back(nt_r);
		v.push_back(MP(r,l));
	}
}

int main(){
	cin >> n;
	FOR(i,0,n-1){
		cin >> A[i];
	}
	sort(A,A+n);
	FOR(i,0,n-1){
		dq.push_back(A[i]);
	}
	FOR(i,1,n-2){
		solve();
	}
	v.push_back(MP(dq.back(),dq.front()));
	cout << dq.back()-dq.front() << endl;
	FORR(x,v){
		cout << x.FI << ' ' << x.SE << endl;
	}
    return 0;
}


Measured memory_bytes: 4852
#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int check(vector<long long> & T, long long P, const int k) {
	int n = T.size();
	int i = 0;
	for (int j = 0; j < k; j++) {
		long long s = 0;
		while (s + T[i] <= P) {
			s += T[i];
			i++;
			if (i == n) return n;
		}
	}
	return i;
}

int solve(vector<long long> & T, const int n, const int k) {
	long long left = 0;
	long long right = 100000 * 10000;
	long long mid;
	while (right - left > 1) {
		mid = (left + right) / 2;
		int v = check(T, mid, k);
		if (v >= n) right = mid;
		else left = mid;
	}
	return right;
}

int main(void) {
	int n, k; cin >> n >> k;
	vector<long long> T(n);
	for (int i = 0; i < n; i++) scanf("%d", &T[i]);
	cout << solve(T, n, k) << endl;
}



Measured memory_bytes: 3632
#include<stdio.h>
int main()
{
	int N,K,X,Y,sum;
	scanf("%d%d%d%d",&N,&K,&X,&Y);
	if(N<=K)
	{
		sum=X*N;
	}
	else
	{
		sum=X*K+(N-K)*Y;
	}
	printf("%d",sum);
	return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back 
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
using namespace std;

int main()
{ 
  fast;


    ll n,ans=0;
    cin>>n;
    ll d[n];

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

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
            ans+=(d[i]*d[j]);
    }

    cout<<ans<<endl;

}


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

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;

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;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n;
  string s;
  cin >> n >> s;
  REP(j,4){
    // int32 ans = j;
    vector<bool> wolf(n,false);
    REP(i,2)if(j>>i&1)wolf[i] = true;
    FOR(i,2,n){
      if(s[i-1] == 'o'){
        // ans |= ((ans>>(i-2)&1) ^ (ans>>(i-1)&1)) << i;
        wolf[i] = wolf[i-2] != wolf[i-1];
      }else{
        // ans |= ((ans>>(i-2)&1) ^ (ans>>(i-1)&1) ^ 1) << i;
        wolf[i] = wolf[i-2] == wolf[i-1];
      }
    }
    // if(s[n-2] == 'o' && (ans>>(n-3)&1) ^ (ans>>(n-2)&1) ^ (ans>>(n-1)&1))continue;
    if(s[n-2] == 'o' && (wolf[n-3] != wolf[n-2]) != wolf[n-1])continue;
    // if(s[n-2] == 'x' && (ans>>(n-3)&1) ^ (ans>>(n-2)&1) ^ (ans>>(n-1)&1) ^ 1)continue;
    if(s[n-2] == 'x' && (wolf[n-3] != wolf[n-2]) == wolf[n-1])continue;
    // if(s[n-1] == 'o' && (ans>>(n-2)&1) ^ (ans>>(n-1)&1) ^ (ans&1))continue;
    if(s[n-1] == 'o' && (wolf[n-2] != wolf[n-1]) != wolf[0])continue;
    // if(s[n-1] == 'x' && (ans>>(n-2)&1) ^ (ans>>(n-1)&1) ^ (ans&1) ^ 1)continue;
    if(s[n-1] == 'x' && (wolf[n-2] != wolf[n-1]) == wolf[0])continue;
    // cout << bitset<6>(ans) << endl;
    if(s[0] == 'o' && (wolf[n-1] != wolf[0]) != wolf[1])continue;
    if(s[0] == 'x' && (wolf[n-1] != wolf[0]) == wolf[1])continue;
    REP(i,n){
      if(wolf[i]){
        cout << 'W';
      }else{
        cout << 'S';
      }
    }
    cout << endl;
    return 0;
  }
  ANS(-1);
  return 0;
}

Measured memory_bytes: 592
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	h,w = readline() |> split |> parseMap
	a = zeros(Int,h,w)
	b = zeros(Int,h,w)
	ms = 80
	for i in 1:h
		a[i,:] = readline() |> split |> parseMap
	end
	for i in 1:h
		b[i,:] = readline() |> split |> parseMap
	end
	base = ms*(h+w+1)
	dp = zeros(BigInt,h,w)
	dp[1,1] += BigInt(2)^(base+a[1,1]-b[1,1])
	dp[1,1] = dp[1,1]|BigInt(2)^(base-a[1,1]+b[1,1])
	for x in 1:h
		for y in 1:w
			if y<w
				v1 = abs(a[x,y+1]-b[x,y+1])
				dp[x,y+1] = dp[x,y+1]|dp[x,y]>>v1|dp[x,y]<<v1
			end
			if x<h
				v2 = abs(a[x+1,y]-b[x+1,y])
				dp[x+1,y] = dp[x+1,y]|dp[x,y]>>v2|dp[x,y]<<v2
			end
		end
	end
	z = base
	for i in 0:2*base
		if (dp[h,w]>>i)%2==1
			z = min(z,abs(base-i))
		end
	end
	println(z)
end

main()


Measured memory_bytes: 158820
//
//  main.cpp
//  at coder 149
//
//  Created by RISHAV SINGH MEHTA on 29/12/19.
//  Copyright © 2019 RISHAV SINGH MEHTA. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <string>
#define ll long long
using namespace std;
int main(int argc, const char * argv[]) {
    // insert code here...
    string T,S;

    cin>>T>>S;
    cout<<S<<T;
    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//Get the highest value and key from a map.
template<typename KeyType, typename ValueType>
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>;
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  });
}
using namespace std;
using P = pair<int,int>;
using ll = long long;
const ll INF = 1LL<<60;


int main() {
  string s0;
  cin >> s0;

  set<string> strs;
  for (int bit = 0; bit < 1 << s0.size(); bit++) {
    string s1 = s0;
    int cnt = 0;
    for (int i = 0; i < s1.size(); i++) {
      if (bit & 1 << i && i != (s0.size() - 1)) {
        ++cnt;
        s1.insert(i + cnt, "+");
      }
    }
    strs.insert(s1);
  }

  ll ans = 0;
  for (auto s : strs) {
    if (s.find('+') == string::npos) {
      ans += stoll(s);
    } else {
      string tmp = "";
      for (int i = 0; i < s.size(); i++) {
        if (s[i] != '+') {
          tmp += s[i];
        } else {
          ans += stoll(tmp);
          tmp = "";
        }
      }
      ans += stoll(tmp);
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
  public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder sb = new StringBuilder();

    int i = 1;

    while(true){
      int line = Integer.parseInt(br.readLine());
      if (line == 0){
        break;
      }
      sb.append("Case ").append(i).append(": ").append(line).append("\n");
      i++;
    }
      System.out.print(sb);
  }
}

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

int main(){
  int N,X,Y; cin >> N >> X >> Y;
  int count[2850];
  for(int i=1; i<=N-1; i++){
    for(int j=i+1; j<=N; j++){
      count[min(j-i,abs(X-i)+1+abs(Y-j))]++;
    }
  }
  
  for(int k=1; k<=N-1; k++) cout << count[k] << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N = 0;
    cin >> N;
    int ans = 0;
    string S;
    cin >> S;
    int i = 0;
    int tmp = 0;
    int muki[N];
    for (i = 0; i < N; i++) {
        if(S[i]=='W'){
            muki[i]=0;
        }else{
            muki[i]=1;
            ans++;
        }
    }
    if(muki[0]==1) ans--;
    tmp = ans;
    for(i=1;i<N;i++){
        if(muki[i-1]==0){
            tmp++;
        }
        if(muki[i]==1){
            tmp--;
        }
        ans = min(ans,tmp);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1812