akhauriyash/Code-Regression huggingface dataset:
import java.util.Scanner;

public class Main {

	static char[] mcxi={'m','c','x','i'};
	static int[] m={1000,100,10,1};
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int N = cin.nextInt();
		while(N--!=0){
			String s1=cin.next();
			String s2=cin.next();
			System.out.println(toStr(toInt(s1)+toInt(s2)));
		}
	}
	static int toInt(String ss){
		char[] s = (" "+ss+" ").toCharArray();
		int re=0;
		for(int j = 0;j<4;j++){
			for(int i = 0;i<s.length;i++){
				if(s[i]==mcxi[j]){
					if(Character.isDigit(s[i-1])){
						re+=m[j]*(s[i-1]-'0');
					}
					else{
						re+=m[j];
					}
				}
			}			
		}

		return re;
	}
	static String toStr(int a){
		String re = "";
		int b = 0;
		for(int i = 0;i<4;i++){
			b=a/m[i];
			if(b>0){
				if(b==1){
					re+=mcxi[i];
				}
				else{
					re+=""+b+mcxi[i];
				}
			}
			a=a%m[i];
		}

		return re;
	}
}

Measured memory_bytes: 12000
import java.io.*;
   
class Main{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line="";
int count=1;
while(!(line=br.readLine()).equals("0")){
int weight=0;
int W=Integer.parseInt(line);
int n=Integer.parseInt(br.readLine());
int[] v=new int[n];
int[] w=new int[n];
for(int i=0;i<n;i++){
String[] str=br.readLine().split(",");
v[i]=Integer.parseInt(str[0]);
w[i]=Integer.parseInt(str[1]);
}
solve(W,n,v,w,count);
count++;
}
}
private static void solve(final int W,final int n,final int[] v,final int[] w,int count){
int[][] dp=new int[n+1][W+1];
int maxValue=0;
int minWeight=0;
for(int i=0;i<=W;i++){
dp[n][i]=0;
}
for(int i=n-1;i>=0;i--){
for(int j=0;j<=W;j++){
if(j<w[i]){
dp[i][j]=dp[i+1][j];
}
else{
dp[i][j]=Math.max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
}
}
}
maxValue=dp[0][W];
for(int i=W;i>0;i--){
if(maxValue==dp[0][i]){
minWeight=i;
}
}
System.out.println("Case "+count+":");
System.out.println(maxValue);
System.out.println(minWeight);
}
}

Measured memory_bytes: 28416
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] arggs)
        {
            //var i = int.Parse(System.Console.ReadLine());
            //var s = System.Console.ReadLine();
            //var ss = System.Console.ReadLine().Split(new char[] {' '});
            var ii = System.Console.ReadLine().Split(new char[] {' '}).Select(s=>int.Parse(s)).ToArray<int>();
            int x = ii[0], y = ii[1];

            System.Console.WriteLine("{0} {1}",x * y,x*2 + y*2);
        }
    }
}

Measured memory_bytes: 9524
n, m = list(map(int, input().split()))
wars = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]

sorted_wars = sorted(wars, key=lambda x: x[1])

tail = 0
cnt = 0

for i in range(m):
    if sorted_wars[i][0] >= tail:
        tail = sorted_wars[i][1]
        cnt += 1
print(cnt)

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

int main() {
    int n;
    cin >> n;
    deque<int> deq;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        if (n % 2 == 0) {
            if (i % 2 == 0) deq.push_back(a);
            else deq.push_front(a);
        } else {
            if (i % 2 == 0) deq.push_front(a);
            else deq.push_back(a);
        }
    }

    for (int i = 0; i < n; i++)
        cout << deq[i] << " ";
    cout << endl;
    return 0;
}

Measured memory_bytes: 2944
using System;
using System.Linq;

namespace Paiza_D155
{
    class Program
    {
        static void Main(string[] args)
        {
            //int num = int.Parse(Console.ReadLine());
            //var input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            //int limit = input[0];
            //int gFee = input[1];
            //int sFee = input[2];

            //int ans = 0;
            //if (num >= limit) ans = gFee * num;
            //else ans = sFee * num;

            //Console.WriteLine(ans.ToString());

            var orgS = Console.ReadLine().ToCharArray();
            var newS = Console.ReadLine().ToCharArray();

            bool isOK = true;
            int cnt = 0;
            foreach(var c in orgS)
            {
                if (!c.Equals(newS[cnt]))
                {
                    isOK = false;
                    break;
                }

                cnt++;
            }

            if (isOK) Console.WriteLine("Yes");
            else Console.WriteLine("No");
        }
    }
}


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

#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define pii pair < int , int >
#define pll pair < long long, long long >

const int N = 200005;


int main()
{
    #ifdef Estb_probitie
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n;
    cin >> n;
    int a[n + 2];
    ll pref[n + 2] = {};

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

    pref[0] = a[0];
    for (int i = 1; i < n; ++i)
        pref[i] = pref[i - 1] + a[i];

    ll dp[n + 2][n + 2];

    for (int j = 0; j < n; ++j)
        dp[j][j] = a[j];

    for (int i = 1; i < n; ++i)
    {
        for (int j = 0; j < n - i; ++j)
            {
                ll zn = 0;
                if (j)
                    zn = pref[j - 1];

                ll ma1 = pref[i + j - 1] - zn - dp[j][i + j - 1] + a[j + i];
                ll ma2 = pref[i + j] - pref[j] - dp[j + 1][i + j] + a[j];

                dp[j][i + j] = max(ma1, ma2);
            }
    }

    cout << dp[0][n - 1]*2 - pref[n - 1];
}


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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define eb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define output(x) cout<<x<<endl;
#define fre(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define frd(i, a, b) for (int i = (int)(a); i >= b; --i)

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int,int> > vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

const int maxn = 110000;

ll dp[101][maxn];

int main(){
	cin.tie(0);ios_base::sync_with_stdio(0);	
	int n,w;
	cin>>n>>w;
	ll a[n+2];
	ll v[n+2];
	fre(i,1,n){
		cin>>a[i]>>v[i];
	}
	fre(i,1,n){
		fre(j,0,maxn-1){
			dp[i][j]=dp[i-1][j];
		}
		if(dp[i][v[i]]==0) dp[i][v[i]]=a[i];
		else dp[i][v[i]]=min(a[i],dp[i][v[i]]);
		fre(j,1,maxn-10-v[i]){
			if(dp[i-1][j]==0) continue;
			if(dp[i][j+v[i]]==0) dp[i][j+v[i]] = dp[i-1][j]+a[i];
			else dp[i][j+v[i]]=min(dp[i][j+v[i]],dp[i-1][j]+a[i]);
		}
	}
	ll ans=0;
	fre(i,1,maxn-1){
		if(dp[n][i] <= w and dp[n][i]>0) ans=i;
	}
	output(ans);
	return 0;
}

Measured memory_bytes: 86144
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
typedef long double ldb;
const int MAXM = 4e2 + 5;
const int MAXN = 26 + 5;
const int MAXT = 2e7 + 5;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;

int n=26, m;
int c[MAXN];
int a[MAXM][MAXN];

int p[MAXM], beg[MAXM];
set<int> idx[MAXN];
int ans;

void init(void)
{
	for(int i=1; i<=n; ++i)
	{
		idx[i].clear();
		idx[i].insert(0);
		idx[i].insert(m+1);
	}
	
	for(int k=1; k<=m; ++k)
		idx[ p[k] ].insert(k);
}

struct Oper
{
	int pos,x;
}oper[MAXT];
int tcnt = 0;

int recalc(register int pos, register int x)
{
	register int old = p[pos];
	if(old == x) return ans;
	
	oper[++tcnt] = (Oper){pos,x};
	
	register int res = ans;
	
	idx[old].erase(pos);
	
	set<int>::iterator it = idx[old].upper_bound(pos);
	register int nxt = *it;
	register int lst = *(--it);
	
	res -= (pos - lst) * c[old] * (nxt - pos) + a[pos][old];
	
	it = idx[x].upper_bound(pos);
	nxt = *it;
	lst = *(--it);
	
	res += (pos - lst) * c[x] * (nxt - pos) + a[pos][x];
	
	idx[x].insert(pos);
	
	p[pos] = x;
	return res;
}

int main(void)
{
	srand(time(0));
	
	scanf("%d",&m);
	for(int i=1; i<=n; ++i) scanf("%d",&c[i]);
	for(int i=1; i<=m; ++i)
		for(int j=1; j<=n; ++j)
			scanf("%d",&a[i][j]);
	
	static int lst[MAXN];
	
	ans = 0;
	for(int k=1; k<=m; ++k)
	{
		int mx = -inf;
		int mxi = -1;
		for(int i=1; i<=n; ++i)
		{
			int res = a[k][i];
			for(int j=1; j<=n; ++j) if(j!=i)
				res -= c[j] * (k-lst[j]);
			
			if(mxi==-1 || mx < res)
				mxi = i, mx = res;
		}
		lst[mxi] = k;
		
		ans += mx;
		p[k] = mxi;
	}
	
	for(int i=1; i<=m; ++i) beg[i] = p[i];
	
	init();
	
	static int pp[MAXM];
	static int qq[MAXM];
	
	for(int i=1; i<=m; ++i) pp[i] = i;
	for(int i=1; i<=n; ++i) qq[i] = i;
	
	const ldb Delta = 0.97;
	ldb Temp = 10000;
	
	int mxans = ans, mxi = 0;
	
	int T = 400;
	while(T--)
	{
		random_shuffle(pp+1,pp+m+1);
		
		for(int i=1; i<=m; ++i)
		{
			random_shuffle(qq+1,qq+n+1);
			for(int j=1; j<=n; ++j)
			{
				int pos = pp[i];
				int x = qq[j];
				
				int old = p[pos];
				int res = recalc(pos, x);
				
				if(res > ans || (ans-res)/Temp > (ldb)rand()/RAND_MAX)
				{
					if(res > mxans) mxans = res, mxi = tcnt;
					ans = res;
				}
				else recalc(pos, old);
				
				Temp *= Delta;
			}
		}
	}
	
	for(int i=1; i<=m; ++i) p[i] = beg[i];
	for(int i=1; i<=mxi; ++i)
		p[oper[i].pos] = oper[i].x;
	
	for(int i=1; i<=m; ++i) printf("%d\n",p[i]);
	return 0;
}

Measured memory_bytes: 32980
from itertools import product

h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]

ans = float("inf")
for pat in product([1, 0], repeat=h):
    prev = 2
    sm = -1
    for e in pat:
        if e != prev:
            sm += 1
        prev = e
    #print(pat)
    #print(sm)

    ss = [[0] * w for _ in range(h)]
    for wi in range(w):
        acc = 0
        prev = 2
        for hi, e in enumerate(pat):
            if e != prev:
                acc = 0
            if s[hi][wi]:
                acc += 1
            ss[hi][wi] = acc
            prev = e

    sss = [[0] * w for _ in range(h)]
    for hi in range(h):
        for wi in range(w):
            if wi == 0:
                sss[hi][wi] = ss[hi][wi]
            else:
                sss[hi][wi] = sss[hi][wi-1] + ss[hi][wi]

    base = [0] * h
    cp_prev = [0] * h
    for wi in range(w):
        mx = 0
        cp = [0] * h
        for hi in range(h):
            mx = max(mx, sss[hi][wi] - base[hi])
            cp[hi] = sss[hi][wi]
        if mx > k:
            if wi == 0:
                sm = 10000
            sm += 1
            base = cp_prev
        cp_prev = cp

    ans = min(ans, sm)

print(ans)


Measured memory_bytes: 77916
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.141592653589793
#define MOD 1000000007

long int combination(long int n, long int r);

//qsort用、昇順
int compare_int(const void *a, const void *b)
{
	return *(int*)a-*(int*)b;
}
//qsort(data, 10, sizeof(int), compare_int)

long int combination(long int n, long int r)
{
	if(r>n-r) r = n-r;
	if(r == 0) return 1;
	else if(r == 1) return n;
	return (combination(n-1, r) + combination(n-1, r-1));
	//大きくなるとき
	/*
	long int tmp1, tmp2, tmp3;
	tmp1 = combination(n-1, r);
	if(tmp1>=MOD) tmp1 = tmp1%MOD;
	tmp2 = combination(n-1, r-1);
	if(tmp2>=MOD) tmp2 = tmp2%MOD;
	tmp3 = tmp1+tmp2;
	if(tmp3>=MOD) tmp3 = tmp3%MOD;
	return (tmp3);
	*/
}


int main(){
	int h, w;
	scanf("%d%d", &h, &w);
	char masu[h][w];
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			scanf(" %c", &masu[i][j]);
		}
	}

	int count=0;
	for(int i=0; i<h; i++){
		count=0;
		for(int j=0; j<w; j++){
			if(masu[i][j] == '.') count++;
		}
		if(count == w){
			for(int j=0; j<w; j++){
				masu[i][j] = 'x';
			}
		}
	}
	
	for(int i=0; i<w; i++){
		count=0;
		for(int j=0; j<h; j++){
			if((masu[j][i] == '.') || (masu[j][i] == 'x')) count++;
		}
		if(count == h){
			for(int j=0; j<h; j++){
				masu[j][i] = 'x';
			}
		}
	}

	//printf("----------------\n");

	for(int i=0; i<h; i++){
		count=0;
		for(int j=0; j<w; j++){
			if(masu[i][j] != 'x') printf("%c", masu[i][j]);
			else count++;
		}
		if(count != w) printf("\n");
	}
		
	return 0;
}

Measured memory_bytes: 128
def yumiri(L, n):
  for i in range(len(L)):
    if L[i] > n:
      return i - 1

def naobou(d, q):
  newans = ans

  rd = t[d - 1] - 1
  rq = yumiri(ZL[rd], d)
  newans += SUMD[ZL[rd][rq] - ZL[rd][rq - 1] - 1] * c[rd]
  newans += SUMD[ZL[rd][rq + 1] - ZL[rd][rq] - 1] * c[rd]
  newans -= SUMD[ZL[rd][rq + 1] - ZL[rd][rq - 1] - 1] * c[rd] 
  del ZL[rd][rq]

  nd = q - 1
  nq = yumiri(ZL[nd], d)
  newans += SUMD[ZL[nd][nq + 1] - ZL[nd][nq] - 1] * c[nd]
  newans -= SUMD[d - ZL[nd][nq] - 1] * c[nd]
  newans -= SUMD[ZL[nd][nq + 1] - d - 1] * c[nd]
  ZL[nd].insert(nq + 1, d)

  newans -= s[d - 1][rd]
  newans += s[d - 1][nd]

  if newans < ans:
    ZL[rd].insert(rq, d)
    del ZL[nd][nq + 1]
    return ans
  else:
    t[d - 1] = q
    return newans 

def rieshon():
  ans = 0
  for i in range(D):
    ans += s[i][t[i] - 1]
    for j in range(26):
      if t[i] - 1 == j:
        L[j] = 0
        ZL[j].append(i + 1)
      else:
        L[j] += 1
        ans -= L[j] * c[j]
  for i in range(26):
    ZL[i].append(D + 1)
  return ans

import time
import random
startTime = time.time()

D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]

L = [0] * 26
ZL = [[0] for _ in range(26)]
SUMD = [0] * (D + 1)
for i in range(1, D + 1):
  SUMD[i] = SUMD[i - 1] + i

t = [0] * D
for i in range(D):
  ma = s[i][0]
  man = 0
  for j in range(1, 26):
    if s[i][j] == ma:
      if c[j] < c[man]:
        ma = s[i][j]
        man = j
    elif s[i][j] > ma:
      ma = s[i][j]
      man = j
  t[i] = man + 1

ans = rieshon()
while time.time() - startTime < 1.7:
  d = random.randrange(D) + 1
  q = random.randrange(26) + 1
  if t[d - 1] == q: continue
  ans = naobou(d, q)

for i in range(D):
  print(t[i])


Measured memory_bytes: 87656
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define INF 1000000
using namespace std;
int table[10][10];
int cnt,check,flag,tmp_flag;
queue<pair<int,int> > que;
int search(int y,int x){
    que.push(pair<int,int>(y,x));
    cnt++;
    x++;
    if(x < 5){
        if(table[y][x] == check) search(y,x);
    }
    if(cnt >= 3) {
        tmp_flag = true;
        return cnt;
    }
    else return 0;
}
int main(){
    int n;
    while(cin >> n,n){
        rep(i,n){
            rep(j,5){
                cin >> table[i][j];
            }
        }
        int point = 0;
        flag = true;
        while(flag){
            flag = false;
            rep(i,n){
                rep(j,5) {
                    if(table[i][j] == 0) continue;
                    tmp_flag = false;
                    cnt = 0;
                    check = table[i][j];
                    point += check*search(i,j);
                    if(tmp_flag){
                        flag = true;
                        while(!que.empty()){
                            pair<int,int> tmp = que.front();
                            que.pop();
                            table[tmp.first][tmp.second] = 0;
                        }
                    }
                    else while(!que.empty()) que.pop();
                }
            }
            if(!flag) continue;
            for(int i=n-1; i>=1; i--){
                for(int j=4; j>=0; j--){
                    if(table[i][j] != 0) continue;
                    for(int k=i; k>=0; k--){
                        if(table[k][j] == 0) continue;
                        swap(table[i][j],table[k][j]);
                        break;
                    }
                }
            }
        }
        cout << point << endl;
    }
    return 0;
}


Measured memory_bytes: 3012
S = input()

a = ["SUN","MON","TUE","WED","THU","FRI","SAT"]

if S == a[0]:
    print(7)
if S == a[1]:
    print(6)
if S == a[2]:
    print(5)
if S == a[3]:
    print(4)
if S == a[4]:
    print(3)
if S == a[5]:
    print(2)
if S == a[6]:
    print(1)


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

using namespace std;
#define int long long
int MOD = 1000000007;


int gcd(int a,int b){
    if(b==0){
        return a;
    }else{
        return gcd(b,a%b);
    }
}

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

signed main(){
    int A,B;
    cin >> A >> B;
    
    cout << lcm(A,B) << endl;
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
int tintin(int a){return a==0?-1:1;}
char ymdtr(int a){return a==0?'-':'+';}
int main(void){
    char po[10];
    scanf("%s",po);
    int A[4];
    for(int i=0;i<4;i++)A[i]=po[i]-'0';
    for(int i=0;i<=1;i++)for(int j=0;j<=1;j++)for(int k=0;k<=1;k++){
        if(A[0]+A[1]*tintin(i)+A[2]*tintin(j)+A[3]*tintin(k)==7){
            printf("%d%c%d%c%d%c%d=7",A[0],ymdtr(i),A[1],ymdtr(j),A[2],ymdtr(k),A[3]);
            return 0;
        }
    }
    return 0;
}


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

int main() {
  int a, b;

  while(cin >> a >> b) {
    int sum = a+b;
    int ans = 1;
    while(1) {
      sum /= 10;
      if(sum == 0) break;
      ans++;
    }
    cout << ans << endl;
  }
}

Measured memory_bytes: 1156
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

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

    vector<int> X;
    vector<int> Y;
    for(int i=0;i<N;++i)
    {
        int t;
        cin >> t;
        X.push_back(t);
    }
    sort(X.begin(),X.end());

    for(int P=X[0];P<=X[N-1];++P)
    {
        int sum=0;
        for(int j=0;j<N;++j)
        {
            sum = sum + (P-X[j])*(P-X[j]);
        }
        Y.push_back(sum);
    }
    sort(Y.begin(),Y.end());
    cout << Y[0]<< endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
ll GetDigit(ll num){ return log10(num)+1; } //numの桁数を求める

int main()
{
    int a,b,c,d,e,k;
    cin >> a >> b >> c >> d >> e >> k;
    if(e - a <= k) cout << "Yay!" << endl;
    else cout << ":(" << endl;
    return 0;
}

Measured memory_bytes: 3672
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << "\n"
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
typedef long long ll;

int binary_search(ll a[], ll k, int e)
{
  int left = 0;
  int right = e - 1;
  int mid;
  while (left <= right)
  {
    mid = (right + left) / 2;
    if (a[mid] == k)
    {
      return mid;
    }
    else if (a[mid] < k)
    {
      left = mid + 1;
    }
    else
    {
      right = mid - 1;
    }
  }
  return right;
}

int main()
{
  int n, m;
  ll k;
  cin >> n >> m >> k;

  ll a[n];
  ll b[m];

  for (int i = 0; i < n; i++)
  {
    int t;
    cin >> t;

    if (i == 0)
    {
      a[i] = t;
    }
    else
    {
      a[i] = a[i - 1] + t;
    }
  }

  for (int i = 0; i < m; i++)
  {
    int t;
    cin >> t;
    if (i == 0)
    {
      b[i] = t;
    }
    else
    {
      b[i] = b[i - 1] + t;
    }
  }

  int ans = 0;
  for (int i = 0; i <= n; i++)
  {
    int acount = i;
    ll atotal;
    if (i == 0)
    {
      atotal = 0;
    }
    else
    {
      atotal = a[i - 1];
    }
    if (atotal > k)
    {
      continue;
    }

    int bcount = 0;
    if ((k - atotal) >= b[0])
    {
      bcount = binary_search(b, k - atotal, m) + 1;
    }
    if (ans < acount + bcount)
    {
      ans = acount + bcount;
    }
  }

  cout << ans << endl;
}


Measured memory_bytes: 6756
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define pb push_back
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define endl "\n"
#define pii pair<int,int>
#define mii map<int,int>
#define int long long int
#define fbo find_by_order
#define ook order_of_key
#define in insert
#define F first
#define S second
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a) for(auto ws: a)cout<<ws<<" "; cout<<endl
#define print1(a,b) for(int ws=0;ws<b;ws++) {cout<<a[ws]<<" ";} cout<<endl
#define all(a) a.begin(), a.end()
#define si set<int>
#define print2(a) for(auto ws: a)cout<<"("<<ws.F<<","<<ws.S<<") "; cout<<endl

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr (names + 1, ',');
	cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
const int N = 100;
int n;
int val[N], wt[N];
int cache[100][100000];
int dp(int i, int w)
{
	if (i == n)return 0;
	int &ans = cache[i][w];
	if (ans != -1)return ans;
	if (wt[i] <= w)
	{
		return ans = max(dp(i + 1, w - wt[i]) + val[i], dp(i + 1, w));
	}
	else
	{
		return ans = dp(i + 1, w);
	}
}
/*
int dp(int i, int val, int w)
{
    if (i == n)return val;
    int &ans = cache[i][w];
    if (ans != -1)return ans;
    ans = 0;
    if (w + v[i].S > allowed)return ans = dp(i + 1, val, w);
    return ans = max(v[i].F + dp(i + 1, val, w + v[i].S), dp(i + 1, val, w));

}
*/
void solve()
{
	memset(cache, -1, sizeof(cache));
	int w;
	cin >> n >> w;
	for (int i = 0; i < n; i++)
	{
		cin >> wt[i];
		cin >> val[i];
	}
	cout << dp(0, w) << endl;


}
int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cout << setprecision(9) << fixed;
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

Measured memory_bytes: 81616
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 1000005
#define NUM1 1000000007
#define NUM2 99999883

struct Info{
	ull hash_1,hash_2;
};

int T_length;
char buf[SIZE],rev_buf[SIZE];
ull HASH[SIZE][2],rev_HASH[SIZE][2],POW[SIZE][2];

bool IS_SAME(Info left,Info right){
	if(left.hash_1 == right.hash_1 && left.hash_2 == right.hash_2)return true;
	else{
		return false;
	}
}

Info calcHash(int left,int right){

	Info ret;

	int length = right-left+1;

	ret.hash_1 = HASH[right+1][0]-HASH[left][0]*POW[length][0];
	ret.hash_2 = HASH[right+1][1]-HASH[left][1]*POW[length][1];

	return ret;
}

Info rev_calcHash(int left,int right){

	Info ret;

	int length = right-left+1;

	ret.hash_1 = rev_HASH[right+1][0]-rev_HASH[left][0]*POW[length][0];
	ret.hash_2 = rev_HASH[right+1][1]-rev_HASH[left][1]*POW[length][1];

	return ret;
}

int main(){

	scanf("%d",&T_length);
	scanf("%s",buf);

	for(int i = T_length-1; i >= 0; i--){

		rev_buf[(T_length-1)-i] = buf[i];
	}

	//2通りのハッシュ値を計算する
	HASH[0][0] = 0;
	HASH[0][1] = 0;
	rev_HASH[0][0] = 0;
	rev_HASH[0][1] = 0;
	POW[0][0] = 1;
	POW[0][1] = 1;

	//ハッシュ値を求める
	for(int i = 1; i <= T_length; i++){

		HASH[i][0] = buf[i-1] + HASH[i-1][0]*NUM1;
		HASH[i][1] = buf[i-1] + HASH[i-1][1]*NUM2;

		rev_HASH[i][0] = rev_buf[i-1] + rev_HASH[i-1][0]*NUM1;
		rev_HASH[i][1] = rev_buf[i-1] + rev_HASH[i-1][1]*NUM2;

		POW[i][0] = POW[i-1][0]*NUM1;
		POW[i][1] = POW[i-1][1]*NUM2;
	}

	bool FLG;

	int pre,length,mult,minus;
	int ans = -1,L,R;

	Info value,rev_value;

	for(int K = 2; K <= T_length; K++){

		mult = 2;
		minus = 1;
		length = mult*K-minus;

		if(length > T_length){

			pre = (length+1)/2;

			value = calcHash((pre-1)-(T_length-pre),pre-1);
			rev_value = rev_calcHash(0,T_length-pre);

			if(IS_SAME(value,rev_value)){
				ans = K;
				break;
			}

		}else{

			pre = length;

				FLG = true;

				while(length <= T_length){

					pre = length;

					//bufの先頭からlength文字が回文になっているか調べる
					L = 0;
					R = length-1;

					value = calcHash(L,(length+1)/2-1);
					rev_value = rev_calcHash((T_length-1-R),(T_length-1-R-1+(length+1)/2));

					if(!IS_SAME(value,rev_value)){
						FLG = false;
						break;
					}

					mult *= 2;
					minus = minus*2+1;
					length = mult*K-minus;
				}

				if(!FLG)continue;


				if(pre == T_length){
					ans = K;
					break;
				}

				//残った部分が、回文の一部になっているか調べる
				L = 0;
				R = length-1;

				value = calcHash((pre-1)-(T_length-pre),pre-1);
				rev_value = rev_calcHash(0,T_length-pre);

				if(IS_SAME(value,rev_value)){
					ans = K;
					break;
				}
		}
	}

	printf("%d\n",ans);

	return 0;
}



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

int main() {
	int h[10],max;
	for (int i = 0; i < 10; i++) {
		cin >> h[i];
	}
	
	for (int j = 0; j < 3; j++) {
		max = 0;
		for (int i = 1; i < 10; i++) {
			if (h[max] < h[i])
				max = i;
		}
		cout << h[max] << "\n";
		h[max] = -1;
	}
	return 0;
}


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

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

const int dx[] = {1, 0};
const int dy[] = {0, 1};
int a, b;
int n;
int x, y;
bool maze[22][22];

inline bool check(int hx, int hy){ return 0 <= hx && hx < a && 0 <= hy && hy < b; }

int main()
{
  while(cin >> a >> b, a){
    rep(i, a) rep(j, b) maze[i][j] = false;
    cin >> n;
    rep(i, n){
      cin >> x >> y;
      maze[x - 1][y - 1] = true;
    }

    int res = 0;
    queue<pair<int, int> > que;
    que.push(make_pair(0, 0));
    while(!que.empty()){
      pair<int, int> top = que.front();  que.pop();
      int hx = top.first;
      int hy = top.second;
      if(hx == a - 1 && hy == b - 1){
        res += 1;
        continue;
      }
      rep(k, 2){
        int nx = hx + dx[k];
        int ny = hy + dy[k];
        if(!check(nx, ny)) continue;
        if(maze[nx][ny]) continue;
        que.push(make_pair(nx, ny));
      }
    }

    cout << res << endl;
  }

  return 0;
}

Measured memory_bytes: 7480
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5 + 5;
int n;
ll d[maxn], c[maxn];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> d[i] >> c[i];
	}
	ll sum = 0, ans = 0;
	for(int i = 1; i <= n; ++i) {
		ans += c[i];
		sum += d[i] * c[i];
	}
	cout << ans - 1 + (sum - 1) / 9 << '\n';
	return 0;
}

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

int main()
{
    string s;
    cin>>s;
    int n=s.size();
    int bj=1;
    for(int i=0; i<(n+1)/2; i++)
    {
        if(s[i]!=s[n-i-1])
        {
            bj=0;
            //cout<<"cw1";
            break;
        }
    }
    if(bj==1)
    {
        int num1=(n-1)/2;
        int num2=(n+3)/2;
        if(n%2==0)
        {
            num1++;num2++;
        }
        //cout<<num1<<"  "<<num2<<endl;
        string s1=s.substr(0,num1);
        string s2=s.substr(num2-1);
        //int num2_2=s.size();
        for(int i=0; i<num1/2+1; i++)
        {
            if(s1[i]!=s1[num1-i-1])
            {
                bj=0;
               // cout<<"cw2";
                break;
            }
        }
    }
    if(bj==0)
        cout<<"No"<<endl;
    else
        cout<<"Yes"<<endl;
    //cout<<s1<<endl<<s2<<endl;
}


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

//#define int long long
#define ll long long
#define all(v) (v).begin(), (v).end()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);

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> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}

signed main()
{   
    gearup;
    int n; cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    vector<ll> s(n+1,0);
    rep(i,n) s[i+1] = s[i] + a[i];
    map<ll,ll> mp;
    rep(i,n+1) mp[s[i]]++;
    ll res = 0;
    for(auto a:mp){
        res += a.second *(a.second-1)/2;
    }
    cout << res << endl;
}

Measured memory_bytes: 15872
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cctype>
using namespace std;
int main()
{
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	printf("%d",(min((a*b),c)));
	return putchar(10),0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
#define fi first
#define se second
const int INF = 1e15;
using namespace std;

template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;

using pi=pair<int,int>;
using vi=vc<int>;

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

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

    int N,A,B; cin >> N >> A >> B;
    cout << min(N*A,B) << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define INF 1000000
using namespace std;
int main(){
    string s;
    getline(cin,s);
    //scanf("%s",s);
    transform(s.begin(),s.end(),s.begin(),::toupper);
    cout << s << endl;
    return 0;
}


Measured memory_bytes: 3068
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <functional>
#include <array>

using namespace std;

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

    int m = 100000;
    vector<int> A(N);
    for(int i = 0; i < N; ++i){
        cin >> A[i];
        m = min(m, A[i]);
    }
    vector<int> basyo;
    for(int i = 0; i < N; ++i){
        if(m == A[i]){
            basyo.push_back(i);
        }
    }
    int prev = -1;
    int ans = 0;
    for(size_t i = 0; i < basyo.size(); ++i){
        if(basyo[i] > prev){
            int plus = (basyo[i] - prev - 1) / K;
            if((basyo[i] - prev - 1) % K != 0){
                ++plus;
            }
            ans += plus;
            prev = max(basyo[i], plus * K);
        }
    }
    if(N-1 > prev){
        int plus = (N-1 - prev) / K;
        if((N-1 - prev) % K != 0){
            ++plus;
        }
        ans += plus;
    }
    cout << ans << endl;
}

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n[5];
    rep(i,5)
        cin >> n[i];
    rep(i,5)
    {
        if(n[i]==0)
        {
            cout << i+1 << "\n";
            break;
        }
    }
}

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

    int tt=1;
    ///cin >> tt;
    while(tt--)
    {
        solve();
    }
}


Measured memory_bytes: 3548
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#ifdef _debug
#define dout(i) cout << #i << ' ' << i << ' '
#else 
#define dout(i) //
#endif
using ll = long long;
using ull = unsigned long long;
using ul = unsigned;
using db = double;
#define int ll
int n, q;
ll m, c;
vector<pair<int,int>> edge[2];
const int maxn = 100001;
int s[maxn], b[maxn];
string res[2] = {"No\n", "Yes\n"};
int find(int i){
	return i == b[i] ? i : b[i] = find(b[i]);
}
void merge(int x, int y){
	x = find(x), y = find(y);
	if(s[x] < s[y])swap(x, y);
	if(x != y){
		s[x] += s[y];
		b[y] = b[x];
		--c;
	}
}
#define same(a, b) (find(a) == find(b))
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> m >> q;
	for(int i = 0, a, b, c;i < q;++i){
		cin >> a >> b >> c;
		edge[c].emplace_back(a, b);
	}
	if(m == n-1)return cout << res[edge[1].empty()], 0;
	c = n;
	for(int i = 1;i <= n;++i)b[i] = i, s[i] = 1;
	for(auto &p : edge[0])merge(p.first, p.second);
	for(auto &p : edge[1])if(same(p.first, p.second))return cout << res[0] , 0;
	cout << res[m <= n-c + c*(c-1)/2];
}

Measured memory_bytes: 3568
#include"bits/stdc++.h"
using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

void Main() {
	int N, x;
	long total = 0;
	cin >> N;
	for (int a = 1; a <= N; a++) {
		for (int b = 1; b <= N; b++) {
			for (int c = 1; c <= N; c++) {
				x = gcd(b, c);
				x = gcd(a, x);
				total += x;
			}
		}
	}
	cout << total << endl;
}

int main() {
	std::cout << std::fixed << std::setprecision(15);
	Main();
}

Measured memory_bytes: 3608
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int T,E;
	cin>>T>>E;
	vector<double> v1(T),v2(T);
	rep(i,T){
		cin>>v1[T-1-i];
	}
	double b;
	cin>>b;
	rep(i,T){
		cin>>v2[i];
	}
	double a=v1[T-1];
	double c=v2[T-1];
	for(int i=T-2;i>=0;i--){
		int s=T-i;
		// cout<<v1[i]<<endl;
		// cout<<s<<endl;
		if(v1[i]>=a){
			a=(v1[i]+a*(s-1))/(s);
		}
		else{
			a=v1[i];
		}
		if(v2[i]>=c){
			c=(v2[i]+c*(s-1))/(s);
		}
		else{
			c=v2[i];
		}
	}
	// cout<<a<<" "<<b<<" "<<c<<endl;
	double l=0,r=E;
	while(r-l>EPS){
		double mid1=l+(r-l)/3;
		double mid2=l+(r-l)*2/3;
		double h1=(E-(a+c)*mid1)/b;
		double h2=(E-(a+c)*mid2)/b;
		double ans1=min(mid1+h1,T*h1);
		double ans2=min(mid2+h2,T*h2);
		if(ans1<ans2){
			l=mid1;
		}
		else{
			r=mid2;
		}
	}
	// cout<<a<<" "<<c<<endl;
	// cout<<E*(T+1)<<endl;
	double ans1=max(E/b,(double)E*(T+1)/(T*(a+c)+b));
	// cout<<l+h1<<" "<<T*h1<<endl;
	cout<<ans1<<endl;
}


signed main() {
	ios_base::sync_with_stdio(false);
  	cout<<fixed<<setprecision(10);
    mainmain();
}

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

A = list(map(int, input().split()))
B = list(map(int, input().split()))

cumA = [0] * (N+1)
cumB = [0] * (M+1)

for i in range(N):
    cumA[i+1] = cumA[i] + A[i]
for i in range(M):
    cumB[i+1] = cumB[i] + B[i]

def check(X):
    res = 1e9+1
    for i in range(N+1):
        j = X - i
        if j < 0 or M < j: continue
        if(cumA[i] + cumB[j] < res):
            res = cumA[i] + cumB[j]
    return res <= K

ok = 0
ng = N + M + 1

while ng - ok > 1:
    mid = (ok + ng) // 2
    if(check(mid)): ok = mid
    else: ng = mid

print(ok)

Measured memory_bytes: 118256
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

#define MOD 1000000007
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REP1(i, N) for (int i = 1; i <= N; ++i)
#define RREP(i, N) for (int i = N - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int ans = n;
  REP(i, n - 1) {
    if (s[i] != s[i + 1]) {
      ans = min(ans, max(i + 1, n - (i + 1)));
    }
  }
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
const int _ = 4e3 + 10;
const ll mod = 924844033;
int tot, N, K, edge[_];
ll f[_][_][2], fac[_], ans = 0;

int main() {
	scanf("%d%d", &N, &K);
	fac[0] = 1;
	for (int i = 1; i <= N; ++i) fac[i] = fac[i - 1] * i % mod;
	for (int i = 1; i <= K; ++i)
		for (int t = 1; t <= 2; ++t)
			for (int j = i; j <= N; j += K) {
				++tot;
				if (j != i) edge[tot] = 1;
			}
	f[0][0][0] = 1;
	for (int i = 1; i <= N * 2; ++i) {
		for (int j = 0; j <= i / 2; ++j) {
			f[i][j][0] = (f[i][j][0] + f[i - 1][j][0] + f[i - 1][j][1]) % mod;
			if (edge[i] && j > 0) f[i][j][1] = (f[i][j][1] + f[i - 1][j - 1][0]) % mod;
		}
	}
	ll p = 1;
	for (int i = 0; i <= N; ++i) {
		ans = (ans + p * (f[N * 2][i][0] + f[N * 2][i][1]) % mod * fac[N - i] % mod + mod) % mod;
		p *= -1;
	}
	printf("%lld\n", ans);
	return 0;
}


Measured memory_bytes: 248704
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
 
const int inf = (int)1e9; 
 
ll gcd(ll a, ll b) {
	if (b > a) {
		ll tmp = b;
		b = a;
		a = tmp;
	}
	if (a%b == 0)return b;
	else return gcd(b, a%b);
}
 
class UnionFind {
public:
	vector<int>par, rank, node;
	UnionFind(int n) :par(n), rank(n, 0),node(n,1) {
		for (int i = 0;i < n;i++)par[i] = i;
	}
	void init(int n) {
		for (int i = 0;i < n;i++) {
			par[i] = i;
			rank[i] = 0;
			node[i] = 1;
		}
	}
 
	int find(int x) {
		return par[x] == x ? x : par[x] = find(par[x]);
	}
 
	bool same(int x, int y) {
		return find(x) == find(y);
	}
 
	void unite_tree(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return;
		if (rank[x] < rank[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (rank[x] == rank[y])rank[x]++;
		}
	}
	int root_size(int x){
		return rank[find(x)];
	}
 
	int size(int x) {
		return node[find(x)];
	}
 
	void unite_node(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return;
		if (size(x) < size(y))swap(x, y);
		node[x] += node[y];
		par[y] = par[x];
	}
};
 
int main(void){
    int n,m;
    cin>>n>>m;
    UnionFind uf = UnionFind(n);
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        uf.unite_node(a,b);
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        ans = max(ans,uf.size(i));
    }
    cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 5628
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

n=II()
cnt=[0]*(n+1)
for x in range(1,101):
    for y in range(1,101):
        for z in range(1,101):
            cur=x**2+y**2+z**2+x*y+y*z+z*x
            if cur>n:break
            cnt[cur]+=1
for c in cnt[1:]:print(c)


Measured memory_bytes: 9152
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

inline void equalize(set<pair<int, int> >&a, set<pair<int, int> > &b) {

    while (a.size() > 0 && b.size() > 0 && (*a.rbegin() > *b.begin())) {
        auto largest = a.end();
        largest--;

        b.insert(*largest);
        a.erase(largest);
    }

    while (a.size() > b.size()) {
        auto largest = a.end();
        largest--;

        b.insert(*largest);
        a.erase(largest);
    }

    while (a.size() + 1 < b.size()) {
        auto smallest = b.begin();

        a.insert(*smallest);
        b.erase(smallest);
    }
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    set<pair<int, int> > small;
    set<pair<int, int> > large;

    int n;
    cin >> n;

    int X[n];
    vector<pair<int, int> > all;
    for (int i=0; i<n; i++) {
        cin >> X[i];
        all.push_back(make_pair(X[i], i));
    }

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

    for (int i=0; i<n/2; i++) {
        small.insert({ all[i].first, all[i].second });
    }

    for (int i=n/2; i<n; i++) {
        large.insert({ all[i].first, all[i].second });
    }

    small.erase({ X[0], 0 });
    large.erase({ X[0], 0 });

    equalize(small, large);
    cout << (*large.begin()).first << '\n';

    for (int i=0; i + 1 <n; i++) {
        //insert and print next median
        small.insert({ X[i], i });

        small.erase({ X[i+1], i+1 });
        large.erase({ X[i+1], i+1 });
        equalize(small, large);

        cout << (*large.begin()).first << '\n';
    }

    return 0;
}


Measured memory_bytes: 13936
//#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define int long long
#define ll long long
#define str string
#define db double
#define ac() cout<<"Yes"<<endl;
#define wa() cout<<"No"<<endl;
#define ddd cout<<"---debug---"<<endl;
#define ff(a,b) for(signed long long (a)=1;(a)<=(b);(a)++)
#define fff(a,b) for(signed long long (a)=0;(a)<(b);(a)++)
#define re readdd()
#define pr(a) printtt(a)
#define pee puts("");
#define eee putchar(' ');
#define pss(a) push_back(a)
#define ps(a) push(a)
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}//
int ppow(int a,int b,int mod){a%=mod;//
int ans=1%mod;while(b){if(b&1)ans=(long long)ans*a%mod;
a=(long long)a*a%mod;b>>=1;}return ans;}
int readdd(){int x=0,f=1;char c=getchar();//
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c))x=x*10+c-'0',c=getchar();
return f*x;}
void printtt(int x){if(x<0)putchar('-'),x=-x;
if(x>=10)printtt(x/10);putchar(x%10+'0');}
bool addd(int a,int b){return a>b;};
//
const int maxm=2e5+5;
const int mod=1e9+7;
vector<int>g[maxm];
int mul[maxm];
int sz[maxm];
int n;
void dfs1(int x,int fa){
    sz[x]=1;
    for(int v:g[x]){
        if(v==fa)continue;
        dfs1(v,x);
        sz[x]+=sz[v];
        mul[x]=mul[x]*mul[v]%mod;
    }
    mul[x]=mul[x]*sz[x]%mod;
}
void dfs2(int x,int fa){
    for(int v:g[x]){
        if(v==fa)continue;
        //mul[x]是正确的
        int pre=mul[x]*ppow(n,mod-2,mod)%mod*ppow(mul[v],mod-2,mod)%mod*(n-sz[v])%mod;
        mul[v]=mul[v]*ppow(sz[v],mod-2,mod)%mod*pre%mod*n%mod;
        dfs2(v,x);
    }
}
signed main(){
    cin>>n;
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=1;i<=n;i++)mul[i]=1;
    dfs1(1,-1);
    dfs2(1,-1);
    int nn=1;
    for(int i=1;i<=n;i++){//计算阶乘
        nn=nn*i%mod;
    }
    for(int i=1;i<=n;i++){
        cout<<nn*ppow(mul[i],mod-2,mod)%mod<<endl;
    }
    return 0;
}


Measured memory_bytes: 17776
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; }

struct UnionFind {
	vector<int> data;
	void init(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};

int solve(vi a, vi b) {
	int N = (int)a.size();
	if (a == b)
		return 0;

	map<int, int> cnt;
	int xorsum = 0;
	for (int x : a) {
		++ cnt[x];
		xorsum ^= x;
	}
	++ cnt[xorsum];
	for (int x : b)
		-- cnt[x];
	bool ok = true;
	int last = -1;
	for (auto p : cnt) {
		ok &= p.second >= 0;
		if (p.second > 0) {
			ok &= p.second == 1 && last == -1;
			last = p.first;
		}
	}
	if (!ok)
		return INF;
	vector<int> values;
	for (auto p : cnt)
		values.push_back(p.first);
	int X = (int)values.size();
	auto getidx = [&values](int x) {
		return (int)(lower_bound(values.begin(), values.end(), x) - values.begin());
	};
	map<int, vi> remPoses;
	int ans = 0;
	vpii edges;
	rep(i, N) if (a[i] != b[i]) {
		++ ans;
		edges.emplace_back(getidx(b[i]), getidx(a[i]));
	}
	vector<bool> used(X);
	vector<int> deg(X, 0);
	UnionFind uf;
	uf.init(X);
	for (auto e : edges) {
		used[e.first] = used[e.second] = true;
		++ deg[e.second];
		-- deg[e.first];
		uf.unionSet(e.first, e.second);
	}
	int start = getidx(xorsum), end = getidx(last);
	++ deg[start];
	-- deg[end];
	rep(x, X) if (used[x]) {
		if (uf.unionSet(x, start)) {
			++ ans;
		}
	}
	for (int x : deg)
		assert(x == 0);
	return ans;
}

int main() {
	int N;
	while (~scanf("%d", &N)) {
		vector<int> a(N);
		for (int i = 0; i < N; ++ i)
			scanf("%d", &a[i]);
		vector<int> b(N);
		for (int i = 0; i < N; ++ i)
			scanf("%d", &b[i]);
		int ans = solve(a, b);
		printf("%d\n", ans == INF ? -1 : ans);
	}
	return 0;
}


Measured memory_bytes: 8440
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Main main = new Main();
		main.run();
	}

	public void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long a[] = new long[n];
		long sum[] = new long[n+1];
		for(int i=0; i<n; i++) {
			a[i]=sc.nextLong();
			sum[i+1] = sum[i]+a[i];
		}
		long ans = 900100100100100100L;

		for(int i=2; i<=n-2; i++) {
			long min = 900100100100100100L;
			long max = -1;
			int m1=f(sum, 0, i);
			int m2=f(sum, i, n);
			//System.out.printf("%d %d %d %d \n", sum[m1], sum[i]-sum[m1], sum[m2]-sum[i], sum[n]-sum[m2]);
			min = Math.min(min, Math.min(Math.min(sum[m1],  sum[i]-sum[m1]), Math.min(sum[m2]-sum[i], sum[n]-sum[m2])));
			max = Math.max(max, Math.max(Math.max(sum[m1],  sum[i]-sum[m1]), Math.max(sum[m2]-sum[i], sum[n]-sum[m2])));
			ans = Math.min(ans, Math.abs(max-min));
		}
		System.out.println(ans);
		sc.close();
	}

	public int f(long a[],  int s, int t) {
		int p = -1;
		int l=s+1;
		int r=t-1;
		long S = a[t]-a[s];
		while(l<=r) {
			int m=(l+r)/2;
			long x = a[m]-a[s];
			if(x<S-x) {
				l=m+1;
			}
			if(x==S-x) {
				p=m;
				break;
			}
			if(x>S-x) {
				r=m-1;
			}
		}
		if(p==-1) {
			if(Math.abs((a[l]-a[s])-(a[t]-a[l])) < Math.abs((a[r]-a[s])-(a[t]-a[r]))) {
				p=l;
			}else {
				p=r;
			}
		}
		return p;
	}
}


Measured memory_bytes: 88032