akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int n,m,arr[N],Max,ans[N];
int Find(int x)
{
    if(x == arr[x]) return x;
    return arr[x] = Find(arr[x]);
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    cin >> n >> m;
    for(int i = 1;i <= n;i++) arr[i] = i;
    while(m--)
    {
        int a,b;
        cin >> a >> b;
        if(a > b) swap(a,b);
        if(Find(a) != Find(b))
            arr[Find(a)] = arr[Find(b)];
    }
    for(int i = 1;i <= n;i++)
    {
        int x = Find(arr[i]);
        ans[x]++;
        Max = max(Max,ans[x]);
    }
    cout << Max << endl;
}

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

using namespace std;
typedef long long ll;

int main() {
  int N, Z, W;
  cin >> N >> Z >> W;
  ll a[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  int ans = 0;
  if (N == 1) {
    ans = abs(a[N - 1] - W);
  } else {
    ans = max(abs(a[N - 1] - W), abs(a[N -2] - a[N - 1]));
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
using namespace std;


int gositigo[5] = {5,7,5,7,7};

int main(){
    int n;
    while(cin >> n && n != 0){
        int w[n],pos = 0;
        string str;
        REP(i,n){
            cin >> str;
            w[i] = str.length();
        }
        REP(i,n){
            int a,s;
            s = i;
            REP(j,5){
                a = 0;
                int l = w[s];
                while(l < gositigo[j]){
                    a++;
                    l += w[s + a];
                }
                if(l != gositigo[j]){goto NEXT;}
                s += a + 1;
            }
            pos = i+1;
            break;
            NEXT: continue;
        }
        cout << pos << endl;
    }
    return 0;
}

Measured memory_bytes: 1208
import fractions
import sys
input = sys.stdin.readline

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a%b)

n,m = map(int,input().split())
a = list(map(int,input().split()))
g = a[0]

for i in range(1, n):
    g = g * a[i] // gcd(g, a[i])

for i in a:
    if g // 2 % i == 0:
        print(0)
        exit()

print(-(-(m * 2 // g)//2))


Measured memory_bytes: 85228
const MOD: i64 = 1000000007;

fn main() {
	let mut s = String::new();
	use std::io::Read;
	std::io::stdin().read_to_string(&mut s).unwrap();
	let mut s = s.split_whitespace();
	let n: usize = s.next().unwrap().parse().unwrap();
	let mut c: Vec<i64> = s.take(n).map(|a| a.parse().unwrap()).collect();
	c.sort();
	let mut ans = 0;
	for i in 0..c.len() {
		let m = i + 1;
		ans =
			(ans + c[i] as i64 * pow_mod(4, n - 1, MOD) % MOD * ((n - m) as i64 + 2)) % MOD;
	}
	println!("{}", ans);
}

pub fn pow_mod<T, U>(a: T, mut b: U, modulus: T) -> T
where
	T: num::PrimInt,
	U: num::PrimInt + num::Integer + num::Unsigned,
{
	let mut r = T::one();
	let mut a = a % modulus;
	while b > U::zero() {
		if b.is_odd() {
			r = r * a % modulus;
		}
		a = a * a % modulus;
		b = b / (U::one() + U::one());
	}
	r
}


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

using namespace std;

int main() {
    int n;
    cin>>n;
    long long  a[n];
    long long  v[200001]={0};
    
    for(int i=0;i<n;i++)
    {
    cin>>a[i];    
    v[a[i]]++;
    
    }
    

    long long  sum=0;
    for(int i=1;i<200001;i++)
    {
    sum+=(v[i]*(v[i]-1))/2;
    
    
    }




    for(int i=0;i<n;i++)
    {
        long long ans;
        ans=sum-((v[a[i]]*(v[a[i]]-1))/2)+(((v[a[i]]-1)*(v[a[i]]-2))/2);
        cout<<ans<<endl;
    }
    
    
    
    
    
    
    
}

Measured memory_bytes: 5760
while True:
    n = input()
    if n == 0: break

    d = {}
    for _ in range(n):
        a, b = raw_input().split()
        d[a] = b

    m = input()

    a = []

    for _ in range(m):
        c = raw_input().strip()
        if c in d:
            a.append(d[c])
        else:
            a.append(c)

    print ''.join(a)

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

#define LL long long
#define REP(i, n) for(LL i = 0; i < (LL)(n); i++)
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ALL(x) (x).begin(),(x).end()
const int IINF = 1e9;
const LL LINF = (LL)(2*IINF + 1)*(2*IINF + 1);
const LL mod = 1e9+7;

int main() {
    LL N, K;
    cin >> N >> K;
    LL X[N], Y[N];
    REP(i, N)cin >> X[i] >> Y[i];
    LL s = LINF;
    REP(u, N) {
        REP(d, N) {
            REP(l, N) {
                REP(r, N) {
                    int count = 0;
                    REP(n, N) {
                        if(X[l] <= X[n] && X[n] <= X[r] && Y[u] <= Y[n] && Y[n] <= Y[d]) {
                            count++;
                        }
                        if(K <= count) {
                            s = min(s, (X[r] - X[l])*(Y[d] - Y[u]));
                        }
                    }
                }
            }
        }
    }
    cout << s << endl;
    return 0;
}


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

using namespace std;

template <typename T> T gcd(T x, T y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <typename T> T lcm(T x, T y) {
  if (x == 0 || y == 0)
    return 0;
  return x / gcd(x, y) * y;
}

int main() {
  int a, b;
  cin >> a >> b;
  cout << (a > b ? a - 1 : a) << endl;
}


Measured memory_bytes: 256
# -*- coding:utf-8 -*-
import math

def solve():
    menus = []
    for i in range(0, 5):
        menus.append(int(input()))
    
    # １の位が最も小さいものを最後に注文する。あとはなんでもいい
    amari = 10
    last = 0
    for i in range(0, 5):
        if menus[i]%10 < amari and menus[i]%10 != 0:
            last = i
            amari = menus[i]%10
    
    ans = 0
    for i in range(0, 5):
        if i == last:
            continue

        ans += menus[i]
        ans = math.ceil(ans/10) * 10
    
    ans += menus[last]
    print(ans)



if __name__ == "__main__":
    solve()


Measured memory_bytes: 3064
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
typedef long long ll;
using namespace std;
const ll mod = 1000000007;
void SpecialTrains() {
	int N;
	cin >> N;
	int t[501][501];
	int c[501], s[501], f[501];
	for (size_t i = 0; i < N; i++)
	{
		if(i < N - 1)
		cin >> c[i] >> s[i] >> f[i];
		else c[i] = 0, s[i] = 0, f[i] = 0;
		for (int j = 0; j < i; j++)
		{
			int bef = t[j][i - 1];
			t[j][i] = bef + (bef <= s[i - 1] 
								? s[i - 1] - bef :
								((bef - s[i - 1]) % f[i - 1] == 0 
									? 0 : f[i - 1] - (bef - s[i - 1]) % f[i - 1])) + c[i - 1];
		}
		t[i][i] = 0;
	}
	for (size_t i = 0; i < N; i++)
	{
		cout << t[i][N - 1] << endl;
	}
}
int main() {
	SpecialTrains();
	return 0;
}

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

int main() {
  ll n;
  cin >> n;
  if(n==0) {
    cout << 0 << endl;
    return 0;
  }
  string ans;
  ll temp=1;
  while(n!=0) {
    if(n%(temp*-2)!=0) {
      ans='1'+ans;
      n-=temp;
    }
    else {
      ans='0'+ans;
    }
    temp*=-2;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
object Main extends App {
  val sc = new java.util.Scanner(System.in)
  var cnt = false
  do {
      val x, y = sc.nextInt
      cnt = x != 0 || y != 0
      val res = if(x < y) s"$x $y"
      else s"$y $x"
      if(cnt) println(res)
  } while(cnt)
}


Measured memory_bytes: 53060
import sys

n = int(input())
a = list(map(int,input().split()))
s = ((sum(a))//n)+1
t = sum(a)//n
u = ((sum(a))//n)-1
l = []
l.append(s)
l.append(t)
l.append(u)
ans = []
ans2 = []
aa = set(a)

if len(aa) == 1:
    print(0)
    sys.exit()

for j in range(len(l)):
    ans = []
    for i in range(n):
        if a[i] == l[j]:
            continue
        else:
            b = (a[i]-l[j])**2
            ans.append(b)
    ans2.append(sum(ans))


print(min(ans2))

Measured memory_bytes: 3064
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] l = new int[n];
		for(int i = 0; i < n; i ++) {
			l[i] = sc.nextInt();
		}
		sc.close();
		int max = 0;
		int sum = 0;
		for(int i = 0;i < n; i ++) {
			if(max < l[i]) {
				max = l[i];
			}
			sum += l[i];
		}
		if(sum - 2 * max > 0) {
			System.out.println("Yes");
		}else {
			System.out.println("No");
		}
	}
}


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

n, k = map(int, input().split())
h = [0] * n
for i in range(n):
    h[i] = int(input())
h = sorted(h)

ans = 10 ** 10
for i in range(n-k+1):
    ans = min(ans, h[i+k-1] - h[i])
print(ans)


Measured memory_bytes: 75964
#include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template<class T> struct edge { int from, to; T cost;};
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;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }
template<class T> inline T power(T a,T b,T m){ if (b==0) return 1;
  T tmp = power(a,b/2,m); if (b%2==0) return tmp*tmp%m; else return tmp*tmp%m*a%m; }
template<class T> inline T gcd(T a, T b){if (b==0) return a; return gcd(b, a%b);}
template<class T> inline T lcm(T a, T b){return a / gcd(a,b) * b;}
// ax+by=gcd(a,b)を解く
template<class T> inline T extgcd(T a,T b,T &x,T &y){if (b==0){x=1; y=0; return a;} T d=extgcd(b,a%b,y,x); y -= a/b*x; return d;}
void hey(){ cout <<"hey" <<endl; }


int a,b;
char op;

bool input() {
  cin >>a >>op >>b;
  if (op == '?') return false;
  return true;
}

void solve() {
  if (op == '+') cout <<a+b;
  if (op == '-') cout <<a-b;
  if (op == '*') cout <<a*b;
  if (op == '/') cout <<a/b;
  cout <<endl;
}

int main() {
  while (input()){
    solve();
  }
}



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


N,M = map(int, input().split())
graph = [set() for _ in range(N)]
for _ in range(M):
  a,b = map(int, input().split())
  graph[a-1].add(b-1)
  graph[b-1].add(a-1)


def dfs(p, visited):
  if len(visited) == N:
    return 1
  else:
    ret = 0
    for q in graph[p]:
      if q not in visited:
        ret += dfs(q, visited+[q])
    return ret

print(dfs(0,[0]))


Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1e9+7;
const LL LINF=1LL<<62;
const int INF=1<<30;


int main(){
    int n,k;cin >> n >> k;
    string s;cin >> s;
    vector<int> a;
    int num=0;
    char c=s[0];
    if(c=='0') a.pb(0);
    for (int i = 0; i < s.length(); i++) {
        if(c==s[i]){
            num++;
        }
        else{
            a.pb(num);
            num=1;
            c=s[i];
        }
    }
    a.pb(num);
    if(c=='0') a.pb(0);
    vector<int> cum;
    cum.pb(0);
    for (int i = 0; i < a.size(); i++) {
        cum.pb(cum[i] + a[i]);
    }
    int ans = 0;
    for (int i = 0; i < cum.size(); i+=2) {
        if(i+2*k+1>=cum.size()) break;
        ans = max(ans,cum[i+2*k+1] - cum[i]);
    }
    if(ans==0) cout << n << endl;
    else cout << ans << endl;
    return 0;
}



Measured memory_bytes: 1528
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF= 1e18;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int,int> P;

int main() {
    ll H,W;
    cin >> H >> W;
    if(H == 1 || W == 1) {
        cout << 1 << endl;
    }
    else if(H % 2 == 0 && W % 2 == 0) {
        cout << H / 2 * W << endl;
    }
    else if(H % 2 == 1 && W % 2 == 0) {
        cout << W / 2 * H << endl;
    }
    else if(H % 2 == 0 && W % 2 == 1) {
        cout << H / 2 * W << endl;
    }
    else {
        cout << (H * W) / 2 + 1 << endl;
    }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
char negate_(char x) {
  return x=='S'?'W':'S';
}
bool test(string& ox, string& sw){
  for(int i=0;i<ox.size();++i){
    int left=(ox.size()+i-1)%ox.size();
    int right=(i+1)%ox.size();
    if(sw[i]=='S'&&ox[i]=='o'||sw[i]=='W'&&ox[i]=='x'){
      if(sw[left]=='?'){
        sw[left]=sw[right];
      }else if(sw[right]=='?'){
        sw[right]=sw[left];
      }else{
        if (sw[left]!=sw[right])return false;
      }
    }else{
      if(sw[left]=='?'){
        sw[left]=negate_(sw[right]);
      }else if(sw[right]=='?'){
        sw[right]=negate_(sw[left]);
      }else{
        if (sw[left]==sw[right])return false;
      }
    }
  }
  return true;
}
main(){
  int N;
  string s;
  cin>>N>>s;
  string sw;
  bool done=false;
  auto f=[&s,&sw,&done](char c0, char c1){
    if(done)return;
    sw=string(s.size(),'?');
    sw[0]=c0;
    sw[1]=c1;
    if(test(s,sw)){
      cout<<sw<<endl;
      done=true;
    }
  };
  f('S','S');
  f('S','W');
  f('W','S');
  f('W','W');
  if(!done)cout<<-1<<endl;
}


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

//int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0};

int main(){
	int x, y, s;
	int board[10][10] = {{0}};
	while( ~scanf("%d,%d,%d", &x, &y, &s) ){
		if( s == 3 ){
			for(int i=-2; i<3; i++){
				for(int j=-2; j<3; j++){
					if( abs(i)+abs(j) < 3 && 0 <= x+j && x+j < 10 && 0 <= y+i && y+i < 10 ) board[y+i][x+j]++;
				}
			}
		}else if( s == 2 ){
			for(int i=-1; i<2; i++){
				for(int j=-1; j<2; j++){
					if( 0 <= x+j && x+j < 10 && 0 <= y+i && y+i < 10 ) board[y+i][x+j]++;
				}
			}
		}else{
			for(int i=-1; i<2; i++){
				for(int j=-1; j<2; j++){
					if( abs(i)+abs(j) < 2 && 0 <= x+j && x+j < 10 && 0 <= y+i && y+i < 10 ) board[y+i][x+j]++;
				}
			}
		}
	}
	int max_c = 0, white = 0;
	for(int i=0; i<10; i++){
		for(int j=0; j<10; j++){
			if( board[i][j] == 0 ) white++;
			max_c = max(max_c, board[i][j]);
		}
	}
	cout << white << endl;
	cout << max_c << endl;
}

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

int main(){
  int n;
  cin>>n;
  vector<int> a(n);
  rep(i,n) cin>> a[i];
  sort(a.begin(),a.end());
  int i=0;
  while(true){
    if(i>n-2) break;
    if(a[i]+a[i+1]<0){a[i]*=-1; a[i+1]*=-1;}
    i+=2;
  }
  ll ans=0;
  rep(i,n) ans+=a[i];
  cout << ans;
}

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

#define MAX 2000001
#define VMAX 10000

int A[MAX],B[MAX];

int main(){
  int n,i,j;
  int C[VMAX+1];

  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&A[i+1]);
    if(A[i]<0 || A[i]>VMAX){
      exit(1);
    }
  }

  for(i=0;i<VMAX+1;i++){
    C[i]=0;
  }

  //C[i]にiの出現数を記録する
  for(i=0;i<n;i++){
    C[A[i+1]]++;
  }

  //C[i]にi以下の数の出現数を記録する
  for(i=1;i<VMAX+1;i++){
    C[i]=C[i]+C[i-1];
  }

  for(j=n;j>=1;j--){
    B[C[A[j]]]=A[j];
    C[A[j]]--;
  }

  for(i=1;i<=n-1;i++){
    printf("%d ",B[i]);
  }
  printf("%d\n",B[n]);

  return 0;
}

Measured memory_bytes: 16264
#include<iostream>
#include<string>
#include<vector>
#include<numeric>
#include<math.h>
#include<algorithm>
#include<iomanip>
using namespace std;

int main() {
	int A;
	int B;
	int C;

	cin >> A;
	cin >> B;
	cin >> C;

	if (A == B && B == C) {
		cout << "No" << endl;
	}
	else if (A != B && B != C && A != C) {
		cout << "No" << endl;
	}
	else {
		cout << "Yes" << endl;
	}

	return  0;
}


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



int main(void){
    int n;
    int price;
    while(1){
        cin>>n;
        if(n==0) return 0;
        double price=1000000;


        for(int a=0;a<=(5000/200)+1;a++){
            for(int b=0;b<=(5000/300)+1;b++){
                for(int c=0;c<=(5000/500)+1;c++){
                    if(a*200+b*300+c*500 !=n) continue;

                    price=min(price,(a%5)*380+(a/5)*5*380*0.8+(b%4)*550+(b/4)*4*550*0.85+(c%3)*850+(c/3)*3*850*0.88);

                }
            }
        }
        cout<<price<<endl;

    }
}



Measured memory_bytes: 3256
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
 
import java.util.*;
class Main {
 
    static char[] xx = new char[] {'S','H','C','D'};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int n = sc.nextInt();
            int x = sc.nextInt();
            if(n == 0 && x == 0) break;
            int count = 0;
            for (int i = 1; i <= n; i++) {
                for (int j = i + 1; j <= n; j++) {
                    for (int k = j + 1; k <= n; k++) {
                        if (i + j + k == x) {
                            count++;
                        }
                    }
                }
            }
            System.out.println(count);
        }
 
    }
}



Measured memory_bytes: 26660
#include<iostream>
#include<string>
using namespace std;
 
int main()
{
  string N; 
  int sum = 0;
  cin >> N;
  
  int size = N.size();
  for(int i = 0; i < size; ++i)
  	sum += (N[i] - '0');
  
  if(sum % 9 == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  
  return 0;
}

Measured memory_bytes: 3484
def kai():
    N=int(input())
    A=list(map(int,input().split()))
    
    if  0 in A:
        print(0)
        return
    
    ans=1
    for i in A:
        ans*=i
        if ans>10**18:
            print(-1)
            return
    print(ans)
    return
kai()


Measured memory_bytes: 21660
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define CMAX(m, x)  m = max(m,x);
#define CMIN(m, x)  m = min(m,x);
#define itn int
typedef long long lint;
const int INF = 1e9;
const lint LINF = 1e18;
int main(){
    int n,m;
    cin >> n >> m;
    lint sum = 0;
    REP(i,m){
        int a;
        cin >> a;
        sum += a;
    }
    if(n >= sum)
        cout << n - sum << endl;
    else
    {
        cout << -1 << endl;
    }
    
    return 0;
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
#define fi first
#define se second
#define LL long long
#define mp make_pair
using namespace std;
const int maxn = 2e5 + 5;
typedef pair<int, int> P;
set<P> L, R;
int l[maxn], r[maxn];
int n;
int main() {
#ifdef TT
    freopen("xxx.in", "r", stdin);
#endif // TT
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d%d", &l[i], &r[i]);
        L.insert(mp(l[i], i));
        R.insert(mp(r[i], i));
    }
    int now = 0;
    LL ans = 0;
    for(int i = 0; i < n; i += 2) {
        int id = R.begin() -> se;
        L.erase(mp(l[id], id));
        R.erase(mp(r[id], id));
        if(now < l[id]) {
            ans += l[id] - now;
            now = l[id];
        } else if(now > r[id]) {
            ans += now - r[id];
            now = r[id];
        }
        if(i + 1 == n)
            continue;
        id = L.rbegin() -> se;
        L.erase(mp(l[id], id));
        R.erase(mp(r[id], id));
        if(now < l[id]) {
            ans += l[id] - now;
            now = l[id];
        } else if(now > r[id]) {
            ans += now - r[id];
            now = r[id];
        }
    }
    ans += abs(now);
    LL ans1 = 0;
    now = 0;
    for(int i = 0; i < n; ++i){
        L.insert(mp(l[i], i));
        R.insert(mp(r[i], i));
    }
    for(int i = 0; i < n; i += 2) {
        int id = L.rbegin() -> se;
        L.erase(mp(l[id], id));
        R.erase(mp(r[id], id));
        if(now < l[id]) {
            ans1 += l[id] - now;
            now = l[id];
        } else if(now > r[id]) {
            ans1 += now - r[id];
            now = r[id];
        }
        if(i + 1 == n)
            continue;
        id = R.begin() -> se;
        L.erase(mp(l[id], id));
        R.erase(mp(r[id], id));
        if(now < l[id]) {
            ans1 += l[id] - now;
            now = l[id];
        } else if(now > r[id]) {
            ans1 += now - r[id];
            now = r[id];
        }

    }
    ans1 += abs(now);
    cout << max(ans, ans1) << endl;
    return 0;
}


Measured memory_bytes: 10368
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL<<60;

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

ll LCM(ll a,ll b){
    return a / GCD(a,b) * b;
}

const int MOD = 1000000007;



int main() {
    string s; cin >> s;
    string r = s;reverse(r.begin(),r.end());
    string f = s.substr(0,s.length()/2);
    string rf = f;reverse(rf.begin(),rf.end());
    string l = s.substr(s.length()/2+1,s.length());
    string rl = l;reverse(rl.begin(),rl.end());
    string ans = "No";
    if(f == rf && l == rl && s == r) ans = "Yes";
    cout << ans;
}


Measured memory_bytes: 3640
h, w, k = map(int, input().split())
S = [list(input()) for _ in range(h)]
ans = []
piece_num = 1
cnt = 0
for i in range(h):
    if "#" in S[i]:
        ans = []
        for j in range(w):
            if S[i][j] == "#":
                stroke = j + 1 - len(ans)
                for _ in range(stroke):
                    ans.append(str(piece_num))
                piece_num += 1
        if len(ans) < w:
            x = ans[-1]
            stroke = w - len(ans)
            for _ in range(stroke):
                ans.append(x)
    if ans:
        pre_loop = i - cnt
        for _ in range(pre_loop):
            print(*ans)
            cnt += 1

        print(*ans)
        cnt += 1


Measured memory_bytes: 78364
void solve();

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

//////////////////////////////////////////////////
//////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void solve() {
	int N, X;
	vector<int> L;
	cin >> N >> X;
	L.resize(N + 1);
	for (int n = 1; n <= N; n++)cin >> L[n];
	int count = 1;
	int x = 0;
	while (x <= X && count <= N) {
		x += L[count];
		if(x <= X)count++;
	}
	cout << count << endl;
	return;
}

Measured memory_bytes: 256
import numpy as np
words = lambda t : list(map(t, input().split()))

str = input()

first = int(str[:2])
second = int(str[2:])

firstY = True
firstM = 1 <= first and first <= 12
secondY = True
secondM = 1 <= second and second <= 12

if firstM and secondM:
    print("AMBIGUOUS")
elif firstM:
    print("MMYY")
elif secondM:
    print("YYMM")
else:
    print("NA")


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

using namespace std;

int a,b;
string s;

int main(){
    cin>>a>>b>>s;

    if(s[a]!='-'){
        cout<<"No"<<endl;
        return 0;
    }

    for(int i=0;i<a;i++){
        if(s[i]<'0'||s[i]>'9'){
            cout<<"No"<<endl;
            return 0;
        }
    }
    for(int i=a+1;i<a+b+1;i++){
        if(s[i]<'0'||s[i]>'9'){
            cout<<"No"<<endl;
            return 0;
        }
    }
    cout<<"Yes"<<endl;
}


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

int main()
{
	int n;
	int a[100000];
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int b;
		scanf("%d",&b);
		a[b]=i; 
	}
	
	for(int i=1;i<=n;i++)
	{
		cout<<a[i]<<" ";
	}

}


Measured memory_bytes: 1152
#include <bits/stdc++.h>
using namespace std;
 
int main(){
	string s, t;
  	cin >> s >> t;
  	int a = s.size();
  	int e = 1;
  	for(int i=0;i<a;i++){
    	if(s.at(i) != t.at(i)) e = 0;
    }
  	if(e == 1) cout << "Yes" << endl;
  	else cout << "No" << endl;
}

Measured memory_bytes: 3584
#include<cstdio>
#include<iostream>

using namespace std;


const int N = 111;

int main(){
	
	
	while(1){
		
		int h,w;
		cin>>w>>h;
		
		if(w==0 && h==0)break;
		
		int n;
		cin>>n;
		
		
		int board[N][N] = {0};
		
		for(int i=0;i<n;i++){
			int a,b;
			cin>>a>>b;
			
			board[b][a] = 1;
		}
		
		
		int dp[N][N] = {0};
		
		dp[1][1] = 1;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(board[i][j]==1) dp[i][j] = 0;
				if(board[i][j]==0) dp[i][j] = dp[i][j] + dp[i-1][j] + dp[i][j-1];
			}
		}
		
		printf("%d\n",dp[h][w]);
	}
}

Measured memory_bytes: 1248
#include<stdio.h>
int main(){
  int n,i,ans=0;
  char s[60];
  scanf("%d\n",&n);
  for(i=0;i<n;i++){
    scanf("%c",&s[i]);
  }
  for(i=2;i<n;i++){
    if(s[i-2]=='A' && s[i-1]=='B' && s[i]=='C'){
      ans++;
    }
  }
  printf("%d\n",ans);
  return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
 ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
 ll n,i,m,w,j,k,l;
 cin>>n>>m>>w;
 char arr[n+2][m+2];
 l=0;
 for(i=0;i<n;i++)
  for(j=0;j<m;j++)
   {
       cin>>arr[i][j];
   }
  
  ll ans=0;
  for(i=0;i<(1<<n);i++)
   for(j=0;j<(1<<m);j++){
    ll tmp=0;
    for(k=0;k<n;k++)
     for(l=0;l<m;l++)
       if( (i&(1<<k))==0 && (j&(1<<l))==0 && arr[k][l]=='#')tmp++;
    if(tmp==w)ans++;
   }
   cout<<ans;
}


Measured memory_bytes: 3636
#include <iostream>
#include <assert.h>
#include <vector>
#include <unordered_map>
#include <queue>
#include <climits>
#include <cmath>
#include <algorithm>
using namespace std;
constexpr int64_t MOD = 1e9 + 7;

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

  int32_t A,B,C,D;
  cin >> A >> B >> C >> D;
  while (A > 0 && C > 0) {
    C -= B;
    if (C <= 0) break;
    A -= D;
  }

  if (A > 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;

  }
  return 0;
}


Measured memory_bytes: 3608
using System;
using System.Linq;
using System.Collections.Generic;

namespace Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var v = Console.ReadLine().Split().Select(double.Parse).ToArray();

            Array.Sort(v);

            var total = (v[0] + v[1]) / 2;
            for (var i = 2; i < N; i++)
            {
                total = (v[i] + total) / 2;
            }

            Console.WriteLine(total);
        }
    }
}


Measured memory_bytes: 13524
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <list>
#include <iomanip>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
typedef pair<int,pair<int,int> >PP;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
int T,t[35],N,M[105];
int dp[105][35];
int mxday;
int solve(int day,int cycle)
{
  if(day==mxday+1) return 0;
  if(dp[day][cycle]!=-1) return dp[day][cycle];
  int res=inf;
  for(int i=0;i<T;i++)
    {
      if(t[cycle]<=M[day])
	{
	  res=min(res,solve(day+1,(cycle+1)%T));
	}
      res=min(res,solve(day+1,1)+1);
    }
  return dp[day][cycle]=res;
}

int main(int argc,char const* argv[])
{
  while(cin >> T && T)
    {
      for(int i=0;i<105;i++)
	{
	  M[i]=inf;
	}
      memset(dp,-1,sizeof(dp));
      for(int i=0;i<T;i++)
	{
	  cin >> t[i];
	}
      cin >> N;
      mxday=0;
      for(int i=0;i<N;i++)
	{
	  int day,time;
	  cin >> day >> time;
	  day--;
	  mxday=max(day,mxday);
	  M[day]=min(M[day],time);
	}
      cout << solve(0,0) << endl;
    }
  return 0;
}



Measured memory_bytes: 3108
#include<bits/stdc++.h>
#define mod 998244353
using namespace std;
int power(int a, int n) {
	int ans = 1;
	while (n) {
		if (n & 1) ans = 1ll * ans * a % mod;
		a = 1ll * a * a % mod;
		n >>= 1;
	}
	return ans;
}
int cnt[100001];
int r[1<<17];
struct Poly {
	vector<int> a;
	void NTT(int op, int len) {
		for (int i = 0; i < len; i++)
			if (i < r[i]) swap(a[i], a[r[i]]);
		for (int L = 2; L <= len; L <<= 1) {
			int p = L >> 1, g = op?power(3, (mod-1)/L):power(3,mod-1-(mod-1)/L);
			for (int i = 0; i < len; i += L) {
				int now = 1;
				for (int j = i; j < i + p; j++) {
					int tmp = 1ll*now*a[j+p]%mod;
					a[j+p] = (mod+a[j]-tmp)%mod;
					a[j] = (a[j]+tmp)%mod;
					now = 1ll*now*g%mod;
				}
			}
		}
		if (!op) {
			int invlen = power(len, mod-2);
			for (int i = 0; i < len; i++) a[i] = 1ll*a[i]*invlen%mod;
		}
	}
	int size() {
		return a.size();
	}
	int& operator[](int n) {
		return a[n];
	}
	void resize(int n) {
		while (a.size() < n) a.push_back(0);
		while (a.size() > n) a.pop_back();
	}
	void push_back(int n) {
		a.push_back(n);
	}
};
Poly operator*(Poly a, Poly b) {
	int la = a.size(), lb = b.size();
	int len = la + lb - 1, bac = len;
	while (len != (len&-len)) len += len & -len;
	a.resize(len), b.resize(len);
	for (int i = 1; i < len; i++) r[i] = (r[i>>1]>>1)|(i&1?len>>1:0);
	a.NTT(1, len), b.NTT(1, len);
	for (int i = 0; i < len; i++) a[i] = 1ll*a[i]*b[i]%mod;
	a.NTT(0, len);
	a.resize(bac);
	return a;
}
int inv[100001];
Poly v[100001];
Poly solve(int l, int r) {
	if (l == r) return v[l];
	int mid = (l + r) / 2;
	Poly l0 = solve(l, mid), r0 = solve(mid + 1, r);
	return l0 * r0;
}
int main() {
	int n, w = 100000;
	cin >> n;
	for (int i = 1; i <= n * 2; i++) {
		int h;
		cin >> h;
		++cnt[h];
	}
	inv[1] = 1;
	for (int i = 2; i <= w; i++) inv[i] = 1ll*(mod-mod/i)*inv[mod%i]%mod;
	v[0].push_back(1);
	for (int i = 1; i <= n; i++) v[0].push_back(1ll*v[0][i-1]*(2*i-1)%mod); 
	for (int i = 1; i <= w; i++) {
		int l = 1;
		v[i].push_back(1);
		for (int j = 1; j * 2 <= cnt[i]; j++) {
			l = 1ll*l*(cnt[i]-2*j+2)%mod*(cnt[i]-2*j+1)%mod;
			l = 1ll*l*inv[2*j-1]%mod*inv[2*j]%mod;
			v[i].push_back(1ll*l*v[0][j]%mod);
		}
	}
	for (int i = n - 1; i >= 0; i -= 2) v[0][i] = mod - v[0][i];
	Poly ans = solve(0, w);
	cout << ans[n] << endl;
}

Measured memory_bytes: 13352
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
int A[3][3];
bool f[3][3];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            cin >> A[i][j];
        }
    }
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        int b;
        cin >> b;
        for(int j = 0; j < 3; j++){
            for(int k = 0; k < 3; k++){
                if(A[j][k] == b) f[j][k] = true;
            }
        }
    }
    for(int i = 0; i < 3; i++){
        if(f[i][0] && f[i][1] && f[i][2]){
            cout << "Yes" << endl;
            return 0;
        }
    }
    for(int i = 0; i < 3; i++){
        if(f[0][i] && f[1][i] && f[2][i]){
            cout << "Yes" << endl;
            return 0;
        }
    }
    if(f[0][0] && f[1][1] && f[2][2]){
        cout << "Yes" << endl;
        return 0;
    }
    if(f[0][2] && f[1][1] && f[2][0]){
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<map>
#include<utility>
#include<algorithm>
typedef long long ll;
using namespace std;
int main(void) {
	ll n, m,d=0;
	cin >> n >> m;
	map<ll, ll>a;
	a[0] = 1;
	a[1] = -1;
	vector<ll>x(n), y(n), z(n), b(n);
	ll ans = 0;
	for (ll i = 0;i < n;i++)cin >> x[i] >> y[i] >> z[i];
	for (ll i = 0;i < 2;i++) {
		for (ll j = 0;j < 2;j++) {
			for (ll k = 0;k < 2;k++) {
				d = 0;
				for (ll l = 0;l < n;l++) {
					b[l] = a[i] * x[l] + a[j] * y[l] + a[k] * z[l];
				}
				sort(b.begin(), b.end());
				for (ll h = n - 1;h >= n - m;h--)d += b[h];
				ans = max(ans, d);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.math.BigInteger;
     
public class Main{
 
static final InputStream in=System.in;
static final PrintWriter out=new PrintWriter(System.out);
static final int INF=Integer.MAX_VALUE/2;
static final long LINF=Long.MAX_VALUE/2;
 
public static void main(String[] args) throws IOException{
InputReader ir=new InputReader(in);
while(true){
int N=ir.nextInt();
if(N==0) break;
int acnt,bcnt;
acnt=bcnt=0;
while(N-->0){
int a=ir.nextInt();
int b=ir.nextInt();
if(a>b) acnt+=(a+b);
else if(a==b){
acnt+=a;
bcnt+=b;
}
else bcnt+=(a+b);
}
out.println(acnt+" "+bcnt);
}
out.flush();
}
 
static class InputReader {
private InputStream in;
private byte[] buffer=new byte[1024];
private int curbuf;
private int lenbuf;
 
public InputReader(InputStream in) {this.in=in;}
   
public int readByte() {
if(lenbuf==-1) throw new InputMismatchException();
if(curbuf>=lenbuf){
curbuf= 0;
try{
lenbuf=in.read(buffer);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(lenbuf<=0)
return -1;
}
return buffer[curbuf++];
}
 
public boolean isSpaceChar(int c){return !(c>=33&&c<=126);}
 
private int skip() {int b; while((b = readByte())!=-1&&isSpaceChar(b)); return b;}
 
public String next() {
int b=skip();
StringBuilder sb=new StringBuilder();
while(!isSpaceChar(b)){
sb.appendCodePoint(b);
b=readByte();
}
return sb.toString();
}
  
public int nextInt() {
int c=readByte();
while (isSpaceChar(c)) c=readByte();
boolean minus=false;
if (c=='-') {
minus=true;
c=readByte();
}
int res=0;
do{
if(c<'0'||c>'9') throw new InputMismatchException();
res=res*10+c-'0';
c=readByte();
}while(!isSpaceChar(c));
return (minus)?-res:res;
}
 
public long nextLong() {
int c=readByte();
while (isSpaceChar(c)) c=readByte();
boolean minus=false;
if (c=='-') {
minus=true;
c=readByte();
}
long res = 0;
do{
if(c<'0'||c>'9') throw new InputMismatchException();
res=res*10+c-'0';
c=readByte();
}while(!isSpaceChar(c));
return (minus)?-res:res;
}
 
public int[] toIntArray(int n){
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=nextInt();
return a;
}
}
}

Measured memory_bytes: 19344
//A - Number of Multiples
//Hiia
#include <stdio.h>
#include <stdlib.h>//rannsuu, zettaichi(absolute)
#include <time.h>
//srand((unsigned int) time(0));
#include <math.h>//koudona keisann
#include <string.h>//hairetsu
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
typedef long long ll;
//#define N 998244353
#define pi 3.1415926535897932384626433832

int gcd(int m,int n){if((0==m)||(0==n))return 0;while(m!=n)if(m>n)m=m-n;else n=n-m;return m;}
int lcm(int m,int n){if((0==m)||(0==n))return 0;return(m*n/gcd(m, n));}
int fact(int x){if(x==1) return(x);else return(x*fact(x-1));}//Kaijou x!
int ruijou(int base,int exponent){int i,answer=1;i=exponent;while(i!=0){answer=answer*base;--i;}return answer;}//ruijou base^exponent

long long int llgcd(long long int m,long long int n){if((0==m)||(0==n))return 0;while(m!=n)if(m>n)m=m-n;else n=n-m;return m;}
long long int lllcm(long long int m,long long int n){if((0==m)||(0==n))return 0;return(m*n/llgcd(m, n));}
long long int llfact(long long int x){if(x==1) return(x);else return(x*llfact(x-1));}//Kaijou
int llruijou(long long int base,long long int exponent){long long int i,answer=1;i=exponent;while(i!=0){answer=answer*base;--i;}return answer;}//ruijou base^exponent

int main()
{
  int a,b,d;
  scanf("%d %d %d", &a,&b,&d);
  printf("%d\n", b/d-(a-1)/d);
  /*
  if(a%d==0){
    printf("%d\n", b/d-a/d+1);
  }
  else{
    printf("%d\n", b/d-a/d);
  }
  */
  return 0;
}


Measured memory_bytes: 1684
#include <stdbool.h>
#include <stdio.h>
bool isHarshad(int x) {
	int y = x;
	int sum = 0;
	while (x != 0) {
		sum += x % 10;
		x /= 10;
	}
	if (y % sum == 0) {
		return true;
	} else {
		return false;
	}
}
int main() {
	int n;
	scanf("%d", &n);
	if (isHarshad(n)) {
		printf("Yes\n");
	} else {
		printf("No\n");
	}
	return 0;
}

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

template <typename T>
class BinarySearchTree {

  struct Node {
    T key;
    Node *par, *lhs, *rhs;
  };

  Node *root;

  // 入力：現在のノード curr, 中間順巡回のノードの順番を保存した配列 order
  // 出力：なし
  void inorder_dfs(Node *curr, std::vector<int> &order) {
    if (!curr) return;
    inorder_dfs(curr->lhs, order);
    order.push_back(curr->key);
    inorder_dfs(curr->rhs, order);
  }

  // 入力：現在のノード curr, 中間順巡回のノードの順番を保存した配列 order
  // 出力：なし
  void preorder_dfs(Node *curr, std::vector<int> &order) {
    if (!curr) return;
    order.push_back(curr->key);
    preorder_dfs(curr->lhs, order);
    preorder_dfs(curr->rhs, order);
  }

  // 入力：値 x、親のポインタ prev、左の子のポインタ l、右の子のポインタ r
  // 出力：新たに作成したノードのポインタ
  Node *make_node(const T &x, Node *p, Node *l = 0, Node *r = 0) {
    Node *res = new Node;
    res->key = x;
    res->par = p;
    res->lhs = l;
    res->rhs = r;
    return res;
  }

public:

  BinarySearchTree() : root(0) {}

  // 入力：なし
  // 出力：中間順巡回のノードの順番を保存した配列
  std::vector<int> inorder() {
    std::vector<int> res;
    inorder_dfs(root, res);
    return res;
  }

  // 入力：なし
  // 出力：先行順巡回のノードの順番を保存した配列
  std::vector<int> preorder() {
    std::vector<int> res;
    preorder_dfs(root, res);
    return res;
  }

  // 入力：値 x
  // 出力：なし
  void insert(const T &x) {
    Node *curr = root, *prev = 0;
    while (curr) {
      prev = curr;
      if (curr->key > x) curr = curr->lhs;
      else if (curr->key < x) curr = curr->rhs;
      else return;
    }
    (!prev ? root : (prev->key > x) ? prev->lhs : prev->rhs) = make_node(x, prev);
  }

};

void insert(BinarySearchTree<int> &BST) {
  int x;
  std::cin >> x;
  BST.insert(x);
}

void print(BinarySearchTree<int> &BST) {
  std::vector<int> inorder = BST.inorder();
  std::vector<int> preorder = BST.preorder();
  for (size_t i = 0; i < inorder.size(); i++) {
    std::cout << " " << inorder[i];
  }
  std::cout << std::endl;
  for (size_t i = 0; i < preorder.size(); i++) {
    std::cout << " " << preorder[i];
  }
  std::cout << std::endl;
}

void ALDS1_8_A() {
  BinarySearchTree<int> BST;
  size_t N;
  std::cin >> N;
  for (size_t i = 0; i < N; i++) {
    std::string s;
    std::cin >> s;
    if (s == "insert") insert(BST);
    else print(BST);
  }
}

int main() {

  ALDS1_8_A();

}


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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SZ(x) ((int)(x).size())
using ll = long long;
using P = pair<int, int>;
using llP = pair<ll, ll>;
using DoP = pair<double, double>;
const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1 << 29;
const ll INF64 = 1ll << 40;
const int mod = 1000000007;
//const int mod = 998244353;

template <class T>inline bool chmin(T &a, const T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline bool chmax(T &a, const T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline void line_out(const vector<T> vec) {
  int n = SZ(vec);
  rep(i, n) {
    cout << vec[i];
    if(i < n-1) cout << " ";
  }
  cout << endl;
  return;
}

struct mint
{
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a)
  {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a)
  {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a)
  {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const
  {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const
  {
    mint res(*this);
    return res -= a;
  }
  mint &operator++()
  {
    if ((x += 1) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator--()
  {
    if ((x += mod - 1) >= mod)
      x -= mod;
    return *this;
  }
  mint operator*(const mint a) const
  {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const
  {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const
  {
    return pow(mod - 2);
  }
  mint &operator/=(const mint a)
  {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const
  {
    mint res(*this);
    return res /= a;
  }
};



int main()
{
  ll n, k;
  cin >> n >> k;
  vector<vector<ll>> sushi(n);
  rep(i, n) {
    int t;
    ll d;
    cin >> t >> d;
    --t;
    sushi[t].push_back(d);
  }
  rep(i, n) if(sushi[i].size() == 0) sushi[i].push_back(-INF64);
  for(auto &v : sushi) {
    sort(rALL(v));
    if(v.empty()) v.push_back(-INF64);
  }
  auto comp = [&](vector<ll> a, vector<ll> b) {
    return a[0] > b[0];
  };
  sort(sushi.begin(), sushi.end(), comp);
  priority_queue<ll> pq;
  ll cur = 0;
  rep(i, k) {
    cur += sushi[i][0];
    for(int j = 1; j < sushi[i].size(); ++j) pq.push(sushi[i][j]);
  }
  for(int i = k; i < n; ++i) for(int j = 0; j < sushi[i].size(); ++j) pq.push(sushi[i][j]);

  ll res = cur + k*k;
  for(ll x = k-1; x >= 1; --x) {
    ll v = sushi[x][0];
    ll w = pq.top();
    if(v < w) {
      pq.pop(); cur += w;
      pq.push(v); cur -= v;
    }
    chmax(res, cur+x*x);
  }
  cout << res << endl;
}


Measured memory_bytes: 8696
class MaxHeap
  def initialize
    @heap = [nil]
    @size = 0
  end
  
  def insert(key)
    @heap << key
    @size += 1
    up_heap(@size / 2)
  end
  
  def up_heap(i)
    return if i.zero?
    l, r = i * 2, i * 2 + 1
    largest = (l <= @size and @heap[l] > @heap[i]) ? l : i
    largest = r if r <= @size and @heap[r] > @heap[largest]
    unless largest == i
      @heap[i], @heap[largest] = @heap[largest], @heap[i]
      up_heap(i / 2)
    end
  end
  
  def remove_root
    a = @heap[1]
    @heap[1] = @heap.pop
    @size -= 1
    down_heap(1)
    a
  end
  
  def down_heap(i)
    return if i > @size
    l, r = i * 2, i * 2 + 1
    largest = (l <= @size and @heap[l] > @heap[i]) ? l : i
    largest = r if r <= @size and @heap[r] > @heap[largest]
    unless largest == i
      @heap[i], @heap[largest] = @heap[largest], @heap[i]
      down_heap(largest)
    end
  end
end


h = MaxHeap.new

$<.readlines.map {|l| l.chomp.split}.each do |command|
  case command[0]
  when "insert" then h.insert(command[1].to_i)
  when "extract" then puts h.remove_root
  when "end" then break
  else raise "error"
  end
end



Measured memory_bytes: 418364
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

long long cnt[200000];

class UnionFind{
  int v[200000];
  int deep[200000];
  public:
  UnionFind(){
    for(int i=1;i<200000;i++){
      v[i]=i;
      deep[i] = 0;
    }
  }

  int root(int x){
    if(v[x]==x)return x;
    else return v[x] = root(v[x]);
  }

  bool same(int x, int y){
    return (root(x) == root(y));
  }

  void unite(int x,int y){
    x = root(x);
    y = root(y);
    if(deep[x] > deep[y]){
            v[y] = x;
    }else{
      if(deep[x] == deep[y])deep[y]++;
      v[x] = y;
    }
    return;
  }
};

int main(){
  long long n,m,i,j,k;
  cin >> n >> m;
  long long ret[m+1]={};
  int a[m],b[m];
  for(i=0;i<m;i++){
    cin >> a[i] >> b[i];
  }
  for(i=0;i<n+1;i++)cnt[i]=1;
  UnionFind uf;
  ret[m]=n*(n-1)/2;
  for(i=m-1;i>=0;i--){
    int x,y;
    if(!uf.same(a[i],b[i])){
      ret[i]=ret[i+1]-cnt[uf.root(a[i])]*cnt[uf.root(b[i])];
      x = uf.root(a[i]);
      y = uf.root(b[i]);
      uf.unite(a[i],b[i]);
      if(x!=uf.root(a[i]))cnt[uf.root(b[i])]+=cnt[x];
      else cnt[uf.root(a[i])]+=cnt[y];
    }else{
      ret[i]=ret[i+1];
    }
  }
  for(i=1;i<=m;i++)cout << ret[i] << endl;
  return 0;
}


Measured memory_bytes: 5248
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
int go(int index,int row,vector<vector<int>>&v,vector<vector<int>>&memo)
{
    if(row==v.size())
    {
        return 0;
    }
    if(memo[row][index]!=-1)
    {
        return memo[row][index];
    }
    int sum=v[row][index];
    int o=0;
    if(index==0)
    {
       o=o+max(go(1,row+1,v,memo),go(2,row+1,v,memo));
    }
    else if(index==1)
    {
        o=o+max(go(0,row+1,v,memo),go(2,row+1,v,memo));
    }
    else if(index==2)
    {
        o=o+max(go(0,row+1,v,memo),go(1,row+1,v,memo));
    }
    sum=sum+o;
    return memo[row][index]=sum;
}
main()
{
  int i,j;
  int k;
  cin>>k;
  vector<vector<int>>v(k,vector<int>(3));
  for(i=0;i<k;i++)
  {
      int m,n,o;
      cin>>m>>n>>o;
      v[i][0]=m;
      v[i][1]=n;
      v[i][2]=o;
  }
  vector<vector<int>>memo(k,vector<int>(3));
  for(i=0;i<k;i++)
  {
      for(j=0;j<3;j++)
      {
          memo[i][j]=-1;
      }
  }
  cout<<max(go(0,0,v,memo),max(go(1,0,v,memo),go(2,0,v,memo)));
}

Measured memory_bytes: 21932
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
typedef long long int ll;

#define PI (acos(-1))
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define dumparr(x) cerr << #x ; for(auto elem: x){cerr << " " << elem;} cerr << endl;
#define dumppairs(x) cerr << #x ; for(auto elem: x){cerr << "(" << elem.first << " " << elem.second << ") ";} cerr << endl;
#define dumpline cerr << "**********************" << endl;
#define dumpwline(x) cerr << "******** " << #x << "=" << (x) << " ********" << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

int main(void){
    ll x; cin >> x;
    cout << (2200 - x - 1) / 200 << endl;
    return 0;
}

Measured memory_bytes: 3648
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
double ac;
double TLE(){return (double)(clock() - ac) / CLOCKS_PER_SEC;}
void debug_out() { cerr << endl; } 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main(){
 ios::sync_with_stdio(0),cin.tie(0);
 int x,n;
 cin>>x>>n;
 set<int> s;
 for(int i=0;i<n;i++){
    int k;
    cin>>k;
    s.insert(k);
 }
 if(s.empty()||!s.count(x))cout<<x<<endl;
 else{
 int y = x-1;
 while(s.count(y)){
 	y--;
 }
 int z = x+1;
 while(s.count(z)){
 	z++;
 }
 int m1 = x-y;
 int m2 = z-x;
 if(m1<=m2)cout<<y<<endl;
 else{
 
 	cout<<z<<endl;
 }
}
}

Measured memory_bytes: 3620
#include <algorithm>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;


typedef pair<LL, int> P;  // cost, vertex_idx
struct Edge {
  int to;
  LL cost;
  Edge(LL a, int b) : to(a), cost(b) {}
};

int main() {
  int N, X, Y; cin >> N >> X >> Y;

  map<LL, LL> m;
  for (int s = 1; s <= N; ++s) {
    for (int t = s + 1; t <= N; ++t) {
      LL d1 = t - s;
      LL d2 = abs(X-s) + abs(t-Y) + 1;
      m[min(d1, d2)]++;
    }
  }

  for (int i = 1; i < N; ++i) cout << m[i] << endl;
}


Measured memory_bytes: 384