akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<limits>
#include<sstream> 
#include<unordered_map>      

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
typedef vector<int> vi;
typedef vector<ll> vl;

const int inf = numeric_limits<int>::max() >> 2;
const ll linf = numeric_limits<ll>::max() >> 2;
const ull ulinf = numeric_limits<ull>::max() >> 2;
const double pi = acos(-1);
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const int dx8[8]={-1,0,1,-1,1,-1,0,1};
const int dy8[8]={-1,-1,-1,0,0,1,1,1};

#define p_queue(i) priority_queue<i> 
#define rp_queue(i) priority_queue<i, vector<i>, greater<i>> 
#define umap(i,j) unordered_map<i,j>
#define P(p) cout<<(p)<<endl
#define PS(p) cout<<(p)<< " "
#define IN cin >> 
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i = (m-1); i >= (int)(n); i--)
#define inrep(n,a) for(int i = 0; i < (int)(n); i++)cin >> a[i];
#define mod(i) ((i)%(ll)(1e9 + 7))
#define divm(a,b) (mod(a * modpow((ll)b,(ll)(1e9+5))))
#define rsort(a,b,c) sort(a, b, greater<c>())
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v,c) sort(v.begin(), v.end(), greater<c>()) 
#define ft first
#define sd second
#define pb push_back
#define it insert
#define sz(x) ((int)(x).size())
#define lb(a,n,k) (lower_bound(a,a+n,k) - a) 
#define vlb(a,k)  (lower_bound(a.begin(),a.end(),k) - a.begin())
#define ub(a,n,k) (upper_bound(a,a+n,k) - a) 
#define vub(a,k)  (upper_bound(a.begin(),a.end(),k) - a.begin())
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return

ll modpow(ll i,ll j){ ll tmp=1; while(j){ if(j%2)tmp=mod(tmp*i);i=mod(i*i);j/=2;}return tmp;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<string> split(const string &str, char sep){
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while( getline(ss, buffer, sep) ) {
        v.push_back(buffer);
    }
    return v;
}

//ll ncr[100][100];
//ll nCr(int n, int r){if(n==r) ret ncr[n][r] = 1; if(r==0) ret ncr[n][r] = 1; if(r==1) ret ncr[n][r] = n;if(ncr[n][r]) ret ncr[n][r]; ret ncr[n][r] = nCr(n-1,r) + nCr(n-1,r-1);}

//ll npr[100][100]={};
//ll nPr(int n,int r){if(npr[n][r])ret npr[n][r];if(r==0)ret npr[n][r] = 1;if(r==1)ret npr[n][r] = n;ret npr[n][r] = n * nPr(n-1,r-1);}

//ll nHr(int n,int r){ret nCr(n+r-1,r);}

///////////////////////////////////////////////////////////////////////////


int main(){

    ll n;
    cin >> n;
    ll a[n];
    inrep(n,a);
    ll ans[n] = {};

    rep(i,0,n)ans[0] += a[i];
    rep(i,1,n-1){
	ans[0] -= 2*a[i];
	i++;
    }
   
    
    a[0] -= ans[0]/2;
    a[n-1] -= ans[0]/2;
    
    rep(i,1,n){
	ans[i] = a[i-1]*2;
	
	a[i] -= ans[i]/2;
	a[i-1] -= ans[i]/2;
    }
    rep(i,0,n)PS(ans[i]);
    cout << endl;
    return  0;
}


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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll INF = 1ll<<60;

#define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

struct grid
{
	ll to, cost;
};

vector<vector<grid>> G;
ll N, M;

int main(int argc, char **argv)
{
	cin >> N >> M;
	G.resize(N);
	REP(i, M)
	{
		ll a, b, c; cin >> a >> b >> c;
		--a, --b;
		G[a].push_back(grid{b, c});
		G[b].push_back(grid{a, -c});
	}

	vector<ll> dist(N, INF);
	vector<bool> mark(N, false);
	queue<ll> que;
	REP(i, N)
	{
		if (mark[i]) continue;
		que.push(i);
		dist[i] = 0;
		while (!que.empty())
		{
			ll v = que.front(); que.pop();
			for (auto &p : G[v])
			{
				ll nv = p.to;
				ll c = p.cost;

				if (dist[nv] == INF)
					dist[nv] = dist[v] + c;
				if (dist[nv] != dist[v] + c)
				{
					std::cout << "No" << std::endl;
					return 0;
				}
				if (!mark[nv])
				{
					mark[nv] = true;
					que.push(nv);
				}
			}
		}
	}

	std::cout << "Yes" << std::endl;
}


Measured memory_bytes: 14080
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
string a,b;
int n,m;

string solve(){
    
    int dp[n+1][m+1];
    for(int i=m;i>=0;--i) dp[0][i] = 0;
    for(int i=n;i>=0;--i) dp[i][0] = 0;
    
    for(int i=1;i<=n;i++){
        for(int j = 1;j<=m;j++){
            
            if(a[i-1]==b[j-1]){
                dp[i][j] = 1+dp[i-1][j-1];
                
            }else{
                dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            }
            
            
            
        }
    }
    //cout<<dp[0][0];
   // char ans[dp[0][0]+1];
    string ans;
    //int index = dp[0][0];
    int i = n,j = m;
    
    while(i>0 and j>0){
            if(a[i-1]==b[j-1]){
                ans.push_back(a[i-1]);
                //ans[index-1] = a[i-1];
                //index--;
                i--;j--;
            }
            
            else{
                
                if(dp[i-1][j]>dp[i][j-1]){
                    i--;
                }else{
                    j--;
                }
                
            
        }
    }
    

    
    return ans;
}



int main() {
	cin>>a>>b;
	n = a.size();
	m = b.size();
	
	string ans = solve();
	reverse(ans.begin(),ans.end());
	cout<<ans;
	
	return 0;
}

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

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <climits>
#include <bitset>
#include <array>
#include <deque>
#include <queue>
#include <map>
#define all(x) (x).begin(),(x).end()

void solve(){

}
int main(){
    int N;
    cin >> N;
    vector<int> a(N);
    for (int i = 0; i < N; i++){
        cin >> a[i];
    }
    sort(all(a));

    int i = 0;
    int ans = 0;
    while(i < N){
        int tmp = a[i],cnt = 0;
        while(i < N && a[i] == tmp){
            i++;
            cnt++;
        }
        if (cnt >= tmp){
            ans += cnt - tmp;
        } else {
            ans += cnt;
        }
    }
    cout << ans << endl;



    return 0;
}


Measured memory_bytes: 768
n=input()
n=int(n)
l=[]
for i in range(n):
    x=input()
    x=int(x)
    l.append(x)
ans=max(l)
x=l.index(ans)
del l[x]
sec=max(l)
for i in range(n):
    if i==x:
        print(sec)
        continue
    print(ans)
    
  


Measured memory_bytes: 12512
#include<bits/stdc++.h>
#define n 500000
#define mem(arr) memset(arr, 0, sizeof(arr))
using namespace std;
typedef long long lli;
typedef double llf;
typedef pair<int, int> P;
void solve();
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int times = 1;
//	cin >> times;
	for(int i=0; i<times; i++)
	{
		solve();
		cout << '\n';
	}
	return 0;
}

int a, b, c, d, e, f, g, h;
int arr[n + 50], arr1[n + 50], arr2[n + 50];

//type here

void solve()
{
	map<string, int> M;
	string a, b; cin >> a >> b;
	cin >> M[a] >> M[b];
	string tmp; cin >> tmp;
	M[tmp] --;
	cout << M[a] << ' ' << M[b];
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <string>
using ll = long long;
using namespace std;
int main() {
    int a,b,c;
    cin >> a >> b;
    if (a>=13) c = b;
    else if (a>=6) c = b/2;
    else c = 0;
    cout << c << endl;
    return 0;
}

Measured memory_bytes: 256
from bisect import bisect_right


N, C, K = map(int, input().split())
t = [int(input()) for _ in range(N)]
t = list(sorted(t))

ans = 0
idx = 0
while idx < N:
    in_time_idx = bisect_right(t, t[idx] + K)
    if (in_time_idx - 1) - idx + 1 <= C:
        idx = in_time_idx
    else:
        idx += C
    ans += 1
print(ans)


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

int main() {
	int x, y;
	cin >> x >> y;
	for (int i = 0; i <= x; i++) {
		if (2 * i == y - 2 * x) {
			cout << "Yes" << endl;
			return 0;
		}
	}
	cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 3632
/*   _/                _/                 _/_/_/   _/
  _/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
   _/     _/    _/   _/     _/    _/  _/_/_/     _/
  _/     _/    _/   _/     _/    _/  _/    _/   _/
   _/_/   _/_/       _/_/   _/_/      _/_/     _/ */
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
using ll=long long;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=2e9;
const ll INF=1e18;
using P=pair<int,int>;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  double a,b;
  cin >> a >> b;
  cout << fixed << setprecision(10) << (a*b)/(a+b) << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <iomanip>
#include <fstream>
#include <stdint.h>
#include <cmath>
#include <algorithm>
#include <utility>
#include <numeric>
using namespace std;

constexpr int INF       = 1000000000;/* 1e+9a */
constexpr int MODULO    = 1000000007;

#define REP(i,f,n) for(int i=(f); i < (n); ++i)
#define PER(i,f,n) for(int i=(n-1); i >= f; --i)

constexpr int evx[4] = { 1, 0, -1, 0 };
constexpr int evy[4] = { 0, 1, 0, -1 };

//----------------------------------
//----------------------------------

signed main()
{
    ios::sync_with_stdio(false);
	int n, a, b;
	cin >> n >> a >> b;
	int in = 0;
	int ab = 0;
	string ans;
	REP(i, 0, n)
	{
		char tmp;
		cin >> tmp;
		bool kk = false;
		switch(tmp){
			case 'a': if(in < a + b)
					  {
						  in++;
						  kk = true;
					  }
					  break;
			case 'b': if(ab <  b && in < a + b)
					  {
						  ab++;
						  in++;
						  kk = true;
					  }
					  break;

			case 'c': break;
		}
		ans += kk ? "Yes\n" : "No\n";
	}
	cout << ans;

}



Measured memory_bytes: 1156
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int angle = 0;
		double px = 0.0;
        double py = 0.0;
		while(true){
			String xy=sc.next();
			if(xy.equals("0,0")){
				break;
			}else{
				String[] xyArr=xy.split(",");
				double x=Integer.parseInt(xyArr[0]);
				double y=Integer.parseInt(xyArr[1]);
		        px+=x*Math.sin(angle*Math.PI/180);
	            py+=x*Math.cos(angle*Math.PI/180);
	            angle+=y;
			}
		}
		System.out.printf("%d\n%d\n", (int)px, (int)py);
	}
}

Measured memory_bytes: 17152
import Foundation

let N = Int(readLine()!)!
let A = readLine()!.components(separatedBy: " ").map { Int($0)! }

if N == 0 {
    if A[0] == 1 {
        print(1)
    } else {
        print(-1)
    }
    exit(EXIT_SUCCESS)
}


var minTop = Array<Int>.init(repeating: 0, count: N + 2)
var maxTop = Array<Int>.init(repeating: 0, count: N + 2)

for i in 0...N {
    minTop[N - i] = A[N - i] + Int(round((Double(minTop[N - i + 1]) + 0.5) / 2.0))
    maxTop[N - i] = A[N - i] + maxTop[N - i + 1]
}

var topCount = 0
for j in 0...N {
    if j == 0 {
        maxTop[0] = 1
    } else {
        let top = (maxTop[j - 1] - A[j - 1]) * 2
        if minTop[j] <= top {
            if top < maxTop[j] {
                maxTop[j] = top
            }
        } else {
            topCount = -1
            break
        }
    }
    topCount += maxTop[j]
}

print(topCount)


Measured memory_bytes: 17536
import bisect


def main():
    n = int(input())
    l = sorted(list(int(i) for i in input().split()))

    cnt = 0

    for i in range(n - 2):
        a = l[i]
        for j in range(i + 1, n-1):
            b = l[j]
            cnt += bisect.bisect_left(l, a+b)-(j+1)

    print(cnt)


if __name__ == "__main__":
    main()


Measured memory_bytes: 9324
package main

import (
	"fmt"
)

func find(target int, ary []int) (bool, []int) {
	for i, a := range ary {
		if a == target {
			ary = append(ary[:i], ary[i+1:]...)
			return true, ary
		}
	}
	return false, ary
}

func main() {
	var a, b, c int
	fmt.Scan(&a, &b, &c)

	ary := []int{a, b, c}
	// fmt.Printf("ary = %v\n", ary)

	var result bool
	bunsetsu := []int{5, 7, 5}
	for _, b := range bunsetsu {
		result, ary = find(b, ary)
		// fmt.Printf("result = %v, ary = %v\n", result, ary)
		if !result {
			fmt.Println("NO")
			return
		}
	}

	fmt.Println("YES")
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
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 Forr(i,m,n) for(int i=m;i>n;i--)
#define Sort(a) sort(a.begin(),a.end())
#define pb push_back
#define vl vector<ll>
#define vll vector<vector<ll>>
#define INF 999999999999

int main(){
  int N;
  cin>>N;
  cout<<N*N*N<<endl;
  return 0;
}


Measured memory_bytes: 256
#!/usr/bin/env python3
import  sys

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    s = sys.stdin.readline().strip()
    t = sys.stdin.readline().strip()
    words = list()

    for i in range(len(s)-len(t)+1):
        u = s[i:i+len(t)]
        u = "".join([ p if q is "?" else q for p,q in zip(t,u) ]).strip()

        if t == u:
            v = s[0:i] + t + s[i+len(t):]
            words.append( v.replace("?", "a") )

    words.sort()
    if len(words):
        print(words[0])
    else:
        print("UNRESTORABLE")

if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
myread :: String->[[Int]]
myread = map (map (read::String->Int).words).tail.lines

rowsum :: [Int]->[Int]
rowsum = (\xs->xs++[sum xs])

colsum :: [[Int]]->[[Int]]
colsum [x] = [x]
colsum (x:y:xs) = colsum $ (zipWith (+) x y) : xs  

colsum2 :: [[Int]]->[[Int]]
colsum2 xs = xs ++ (colsum xs)

myprint :: [[Int]]->IO ()
myprint = mapM_ $ putStrLn.unwords.(map show)

main=getContents >>= myprint.colsum2.map rowsum.myread

Measured memory_bytes: 5428
INFTY = 1000000001

def merge(A, left, mid, right):
    global cnt

    L = A[left:mid]
    R = A[mid:right]

    L.append(INFTY)
    R.append(INFTY)

    i = 0
    j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
          A[k] = L[i]
          i += 1
        else:
          A[k] = R[j]
          j += 1

        cnt += 1



def merge_sort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2

        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)



n = int(input())
S = list(map(int, input().split()))

cnt = 0

merge_sort(S, 0, len(S))


print(*S)
print(cnt)



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

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

    

    int64_t l1 = 1;
    int64_t l2 = 2;
    int64_t anser = N == 1 ? l1 : l1 + l2 ;

    for (int64_t i = 2; i < N; i++)
    {
        l2 = l1;
        l1 = anser;
        anser = l1 + l2;
    }

    cout << anser << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
//#include <ext/rope>
using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define for0(i, a, b) for(int i = a; i < b; ++i)
#define for1(i, a, b) for(int i = a; i <= b; ++i)
#define forw0(i, a, b) for(int i = a - 1; i >= b; --i)
#define forw1(i, a, b) for(int i = a; i >= b; --i)
#define fora(v, a) for(auto v : a)
#define bp __builtin_popcount
#define bpll __builtin_popcountll

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;

const ll mod1 = 1e9 + 7, mod2 = 998244353, K = 26, maxn = 1 << 18;
const double pi = acos(-1);
ll N, n, ans[maxn], a[maxn];
vll dp[maxn];

void solve(){
	cin >> N; n = 1 << N;
	for0(i, 0, n){
		cin >> a[i]; dp[i].pb(a[i]);
	}
	for0(bit, 0, N){
		for0(i, 0, n){
			if(((i >> bit) & 1) == 0){
				fora(v, dp[i]){
					dp[i | (1 << bit)].pb(v);
				}
				sort(dp[i | (1 << bit)].rbegin(), dp[i | (1 << bit)].rend());
				while(dp[i | (1 << bit)].size() > 2){
					dp[i | (1 << bit)].pop_back();
				}
			}
		}
	}
	for0(i, 1, n){
		ans[i] = max(ans[i], ans[i - 1]);
		ans[i] = max(ans[i], dp[i][0] + dp[i][1]);
	}
	for0(i, 1, n) cout << ans[i] << '\n';
}

signed main(){
	//freopen(".inp", "r", stdin); freopen(".out", "w", stdout);
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
}



Measured memory_bytes: 26764
package main

import(
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	
	//ans := make([]int, n)

	a := make([]int,n)
	for i:=0; i<n; i++{
		fmt.Scan(&a[i])
	}

	count:= make([]int,n)
	for _,v := range(a){
		count[v-1]++
	}

	sum := 0
	for _,v := range(count){
		sum += combination(v,2)
	}

	var tmp int
	for _,v:=range(a) {
		tmp = count[v-1] - 1
		fmt.Println(sum-tmp)
	}
}

func permutation(n int, k int) int {
    v := 1
    if 0 < k && k <= n {
        for i := 0; i < k; i++ {
            v *= (n - i)
        }
    } else if k > n {
        v = 0
    }
    return v
}

func factorial(n int) int {
    return permutation(n, n - 1)
}

func combination(n int, k int) int {
    return permutation(n, k) / factorial(k)
}

func homogeneous(n int, k int) int {
    return combination(n + k - 1, k)
}

Measured memory_bytes: 10496
N = int(input())
A = list(map(int, input().split()))
tmp = 0
lr = [0]
for a in A:
    tmp += a
    lr.append(tmp)

ans = float('inf')

f=1
g=3
for i in range(2, N - 1):

    while abs(lr[i] - lr[f] - lr[f]) > abs(lr[i] - lr[f + 1] - lr[f + 1]):
        f += 1
    while abs((lr[-1] - lr[g]) - (lr[g] - lr[i])) > abs((lr[-1] - lr[g + 1]) - (lr[g + 1] - lr[i])):
        g += 1
    l = (lr[f], lr[i] - lr[f], lr[-1] - lr[g], lr[g] - lr[i])
    ans = min(ans, max(l) - min(l))

print(ans)

Measured memory_bytes: 104232
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>

using namespace std;

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

    vector<pair<long long, int>> jewels(n);
    vector<vector<long long>> colors(k + 1);

    for (int i = 0; i < n; i++) {
        long long c, v;
        cin >> c >> v;
        colors[c].push_back(v * 2);
    }

    multiset<long long> top_one;
    multiset<long long> top_two;
    multiset<long long> top_three;
    for (int color = 1; color <= k; color++) {
        sort(colors[color].begin(), colors[color].end(), greater<long long>());
        int nc = colors[color].size();
        top_two.emplace(colors[color][0] + colors[color][1]);
        if (nc > 2) {
            top_three.emplace(colors[color][0] + colors[color][1] + colors[color][2]);
        }
        long long avg = (colors[color][0] + colors[color][1]) / 2;
        jewels.emplace_back(avg, color);
        jewels.emplace_back(avg, color);
        for (int i = 2; i < nc; i++) {
            jewels.emplace_back(colors[color][i], color);
        }
    }

    sort(jewels.begin(), jewels.end(), greater<pair<long long, int>>());


    long long sum = 0;
    vector<int> cnts(k + 1, 0);
    long long min_pair = 1LL<<60;
    long long min_one = 1LL<<60;
    for (int i = 0; i < n; i++) {
        long long color = jewels[i].second;
        long long value = jewels[i].first;
        long long ans = -2;
        if (cnts[color] == 0) {
            if (!top_one.empty()) {
                ans = max(ans, sum + *top_one.rbegin());
            }

            if (!top_two.empty()) {
                ans = max(ans, sum - min_one + *top_two.rbegin());
            }

            if (!top_three.empty()) {
                ans = max(ans, sum - min_pair + *top_three.rbegin());
            }

            
            // erase from top two and three
            top_two.erase(top_two.find(colors[color][0] + colors[color][1]));
            if (colors[color].size() > 2) {
                top_three.erase(top_three.find(colors[color][0] + colors[color][1] + colors[color][2]));
            }
        }
        else {
            ans = sum + value;
            if (cnts[color] == 1) {
                
                for (int i = 2; i < colors[color].size(); i++) {
                    top_one.emplace(colors[color][i]);
                }
                min_pair = min(min_pair, colors[color][0] + colors[color][1]);
            }
            else {
                top_one.erase(top_one.find(value));
                min_one = min(min_one, value);
            }
        }
        cnts[color]++;
        sum += value;
        cout << ans / 2 << endl;
    }
}

Measured memory_bytes: 21064
#[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Bytes, Chars, Isize1, Usize1},
};

fn main() {
    input! {
        _n:usize,a:usize,b:usize,
        s:Chars,
    }
    let mut rank = 0;
    let mut count = 0;
    for i in s {
        if i == 'b' {
            rank += 1;
            if rank <= b && count < a + b {
                count += 1;
                println!("Yes");
            } else {
                println!("No");
            }
        } else if i == 'a' {
            if count < a + b {
                count += 1;
                println!("Yes");
            } else {
                println!("No");
            }
        } else {
            println!("No");
        }
    }
}


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

using namespace std;
typedef long long int ll;
ll MOD = 1000000007;

int main() {
    ll N;
    cin >> N;
    unordered_map<ll, ll> L;
    unordered_map<ll, ll> R;
    for (ll i = 0; i < N; i++) {
        ll A;
        cin >> A;
        L[i + A]++;
        R[i - A]++;
    }
    ll ans = 0;
    for (ll i = 1; i < 2 * N; i++) {
        ans += L[i] * R[i];
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 50692
/**
 * HowManyDivisors.cpp
 * Create:20180419
 * Description:整数a,b,cを読み込みaからbまでの整数の中に、cの約数がいくつあるかを求める。
 */
#include <iostream>

int main(int argc, char* argv[]){
  int a, b, c;
  int count = 0;
  std::cin >> a >> b >> c;

  for (int i = a; i <= b; i++){
    if (c % i == 0){
      count++;
    }
  }

  std::cout << count << std::endl;

  return 0;
}



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

int main()
{
	int n, a, b;
	cin >> n >> a >> b;
	int sm,bi;

	/*
	if ( (a==1 && b==2) || (a==n && b==n-1) || (b == 1 && a == 2) || (b == n && a == n - 1))
	{
		cout << "Borys\n";
	}
	*/

//	else
//	{
		if ((max(a, b) - min(a, b) - 1) % 2 == 0)
		{
			cout << "Borys\n";
		}

		else
		{
			cout << "Alice\n";
		}
//	}

	return 0;
}


Measured memory_bytes: 256
N = int(input())
A = map(int, input().split())
MOD = 1000000007
ans = 1
cnt = [3 if i == 0 else 0 for i in range(N + 1)]

for a in A:
    ans = ans * cnt[a] % MOD
    if ans == 0:
        break
    cnt[a] -= 1
    cnt[a+1] += 1

print(ans)


Measured memory_bytes: 10764
#!/usr/bin/env python3
N = int(input())
lists = list(map(int, input().split()))
if lists[0] % N == 0 or lists[1] % N == 0:
    print("OK")
elif ((lists[0]//N) * N) + N < lists[1]:
    print("OK")
else:
    print("NG")


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

int main() {
	int k, s;
	cin >> k >> s;
	ll ans = 0;
	for(int x = 0; x <= k; x++) {
		for(int y = 0; y <= k; y++) {
			int z = s - x - y;
			if(z < 0 || z > k) continue;
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
let counting_sort a b k n =
  let c = Array.make (k + 1) 0 in
  Array.iter (fun e -> c.(e) <- c.(e) + 1) a;
  for i = 1 to k do
    c.(i) <- c.(i) + c.(i-1)
  done;
  for i = n - 1 downto 0 do
    b.(c.( a.(i) ) - 1) <- a.(i);
    c.( a.(i) ) <- c.( a.(i) ) - 1;
  done

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> Scanf.scanf "%d " (fun e -> e)) in
  let b = Array.make n 0 in
  counting_sort a b 10000 n;
  print_int b.(0);
  for i = 1 to n - 1 do
    print_string " "; print_int b.(i);
  done;
  print_newline ()

Measured memory_bytes: 36156
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
using namespace std;
int main(void){
    ll n;cin>>n;
    map<ll,ll> mp;
    ll s=0;
    rep(i,n){
        ll a;cin>>a;
        mp[a]++;
        s+=a;
    }
    ll q;cin>>q;
    rep(i,q){
        ll b,c;cin>>b>>c;
        ll buf=mp[b];
        s+=(c-b)*buf;
        mp[b]=0;
        mp[c]+=buf;
        cout<<s<<endl;
    }
}

Measured memory_bytes: 9472
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

int par[100005];
int find(int x) {
  return par[x] == x ? x : (par[x] = find(par[x]));
}
bool merge(int x, int y) {
  x = find(x);
  y = find(y);
  if(x == y) return false;
  par[x] = y;
  return true;
}

void solve() {
  int n, m;
  cin >> n >> m;
  for(int i = 1; i <= n; i++) par[i] = i;
  int ret = n;
  while(m--) {
    int a, b, c;
    cin >> a >> b >> c;
    if(merge(a, b)) ret--;
  }
  cout << ret << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  /*
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
  */
  solve();
}


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

int main() {
  int n,k;
  cin >> n >> k;
  vector<int> p(n);
  for(int i= 0; i < n; i++){
    cin >> p.at(i);
  }
  sort(p.begin(), p.end());
  int sum = 0;
  int a = 0;
  while(1){
    sum += p.at(a);
    a++;
    if(a == k) break;
  }
  cout << sum << endl;
}


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

using namespace std;

#define int long long

inline void chkmax(int &x, int y){x<y?(x=y):0;}

const int N=100000+5;

struct node{int t, d;}a[N];

bool cmp(node a, node b){return a.d>b.d;}

stack <int> q; 
set <int> s;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
	int n, k, now=0, ans=0;
	cin >> n >> k;
	for (int i=1; i<=n; i++) cin >> a[i].t >> a[i].d;
	sort(a+1, a+n+1, cmp);
	for (int i=1; i<=k; i++) 
	{
	    if (s.count(a[i].t)) q.push(a[i].d);
		s.insert(a[i].t); now+=a[i].d;
	}
	ans=now+(int)(s.size()*s.size()); 
	for (int i=k+1; i<=n; i++) 
	{
		if (q.empty()) break;
		if (s.count(a[i].t)) continue;
		s.insert(a[i].t); now=now-q.top()+a[i].d; q.pop();
		chkmax(ans, now+(int)(s.size()*s.size())); 
		
	}
	cout << ans;
	return 0;
}


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

using ll = long long int;
const int mx = 1e5+1;

int main() {
    #ifdef LU_SERIOUS
        freopen( "in.txt", "r", stdin );
    #endif // LU_SERIOUS
    string s;
    bool fl;
    string ans[] = { "Yes", "No" };
    map < char, int > mp;
    while( cin >> s ) {
        mp.clear();
        fl = 0;
        for( char c: s ) {
            mp[c]++;
        }
        for( auto xx: mp ) {
//            cerr << xx.first << " " << xx.second << "\n";
            if( xx.second & 1 ) fl = 1;
        }
        cout << ans[fl] << "\n";
    }
    return 0;
}


Measured memory_bytes: 256
import math
import itertools
N=int(input())
K=[list(map(int,input().split())) for i in range(N)]
point = list(range(N))

#distance of all edges
dist_list = []
for v in itertools.combinations(point,2):
    dist = math.sqrt((K[v[0]][0] - K[v[1]][0])**2 + (K[v[0]][1] - K[v[1]][1])**2)
    dist_list.append([list(v),dist])

sum = 0
for v in itertools.permutations(point,N):
    for i in range(N-1):
        p1,p2 = v[i],v[i+1]

        for item in dist_list:
            if [p1,p2] in item or [p2,p1] in item:
                sum += item[1]
                break
        

ans = sum / math.factorial(N)

print(ans)




Measured memory_bytes: 3064
input = gets.chomp                                                                                                                                              
input += 'o' * 15                                                                                                                                               
victories = input[0..14]                                                                                                                                        
if 8 <= victories.split(//).select{|v| v == 'o'}.count                                                                                                          
  puts 'YES'                                                                                                                                                    
else                                                                                                                                                            
  puts 'NO'                                                                                                                                                     
end     

Measured memory_bytes: 1788
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if ((abs(a- b) <= d && abs(b - c) <= d) || abs(a - c) <= d) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll) (x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = (1 << 30 ) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] ={1,0,-1,0,1,-1,1,-1};
ll dy[8] ={0,1,0,-1,1,-1,-1,1};
 
template<class T> 
inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> 
inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
 
struct Timer{
    chrono::system_clock::time_point start, end;
    Timer(){ start = chrono::system_clock::now(); }
    ~Timer(){
        end = chrono::system_clock::now();
        auto msec = chrono::duration_cast<chrono::milliseconds>(end - start).count();
        cerr<<"time : "<<msec<<" ms"<<endl;
    }
};
 
 
void solve(){
    ll n;
    cin>>n;
    ll a[1010];
    rep(i,n) cin>>a[i];
    sort(a,a+n);
    cout<<a[n-1]-a[0]<<endl;
} 
 
signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    // Timer TM;
     
    ll Q=1;
    // cin>>Q;
    rep(_,Q){
 
        solve();
 
    }
   
 
 
    return 0;
}

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

const double PI = acos(-1.0);

class Point
{
public:
    double y, x;
    Point(){
        y = x = 0.0;
    }
    Point(double y0, double x0){
        y = y0;
        x = x0;
    }
    Point operator+(const Point& p) const{
        return Point(y + p.y, x + p.x);
    }
    Point operator-(const Point& p) const{
        return Point(y - p.y, x - p.x);
    }
    Point operator*(double a) const{
        return Point(y * a, x * a);
    }
    Point operator/(double a) const{
        return Point(y / a, x / a);
    }
    double length() const{
        return sqrt(y * y + x * x);
    }
    double dist(const Point& p) const{
        return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2));
    }
    Point rotate(double theta) const{
        return Point(x * sin(theta) + y * cos(theta), x * cos(theta) - y * sin(theta));
    }
    bool operator< (const Point& p) const{
        return x < p.x;
    }
};

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<pair<Point, double> > v(n);
        for(int i=0; i<n; ++i)
            cin >> v[i].second >> v[i].first.x >> v[i].first.y;

        double ret = DBL_MAX;
        for(int i=0; i<30; ++i){
            sort(v.begin(), v.end());
            for(int j=1; j<n; ++j)
                ret = min(ret, v[j].first.dist(v[j-1].first) - v[j].second - v[j-1].second);
            for(int j=0; j<n; ++j)
                v[j].first = v[j].first.rotate(2*PI/100);
        }
        printf("%.10f\n", ret);
    }
}

Measured memory_bytes: 3600
#include<bits/stdc++.h>
using namespace std;
int main(){
  int i,j;
  vector<int>a(9);
  for(i=0;i<9;i++)cin>>a[i];
  int n;
  cin >> n;
  int b;
  for(i=0;i<n;i++){
    cin >> b;
    for(j=0;j<9;j++){
      if(b==a[j])a[j]=-1;
    }
  }
  bool chk = false;
  for(i=0;i<3;i++){
    if(a[i]==-1 && a[i+3]==-1 && a[i+6]==-1)chk=true;
    if(a[3*i]==-1 && a[3*i+1]==-1 && a[3*i+2]==-1)chk=true;
  }
  if(a[0]==-1 && a[4]==-1 && a[8]==-1)chk=true;
  if(a[2]==-1 && a[4]==-1 && a[6]==-1)chk=true;

  if(chk)cout<<"Yes"<<endl;
  else cout<<"No"<<endl;

}


Measured memory_bytes: 256
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        StringBuilder builder = new StringBuilder();
        while (n > 0) {
            long e = (n - 1) % 26;
            char v = (char) (e + 97);
            builder.insert(0, v);
            if (e == 25) {
                n--;
            }
            n /= 26;
        }
        System.out.println(builder);
    }
}

Measured memory_bytes: 35636
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,n,s) for(int i=(s);i<(n);i++)
#define MOD 1000000007
typedef long long int ll;
using namespace std;
using Graph = vector<vector<int> >;

int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string S;
cin>>S;
string next[3]{"Sunny","Cloudy","Rainy"};
rep(i,3){
    if(S==next[i]){
        cout<<next[(i+1)%3]<<endl;;
        break;
    }
}

//system("pause");
return 0;
}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}


int nextInt() { int x; scanf("%d", &x); return x;}



int g3(int X, int K) {
  // cout << X << " " << K << endl;
  int res = 0;
  if (X < K) {
    res = 0;
  } else if (X % K == 0) {
    res = X / K;
  } else {
    int p = X/K;
    res = g3(X - (p+1) * max(1, (X - p*K + p) / (p+1)), K);
  }
  return res;
}


int main2() {
  int N = nextInt();
  ll ans = 0;
  REP(i, N) {
    int A = nextInt();
    int K = nextInt();
    ans ^= g3(A, K);
  }
  cout << (ans ? "Takahashi":"Aoki") << endl;
  return 0;
}


int memo[100][100];
int memo2[100][100];

int g(int X, int K) {
  if (memo[X][K] != -1) { return memo[X][K]; }

  set<int> s;
  for (int i = 1; i <= X / K; i++) {
    int nxt = g(X - i, K);
    s.insert(nxt);
  }
  int res = 0;
  while (s.count(res) > 0) res++;
  return memo[X][K] = res;
}

int g2(int X, int K) {
  if (memo2[X][K] != -1) { return memo2[X][K]; }
  int res = 0;
  res = (X % K == 0 ? X / K : g2(X - X/K - 1, K));
  return memo2[X][K] = res;
}
int main() {
  // CLR(memo, -1);
  // CLR(memo2, -1);
  // for (int i = 0; i <= 30; i++) cout << g(i, 1) << " "; cout << endl;
  // for (int i = 0; i <= 30; i++) cout << g2(i, 1) << " "; cout << endl;
  // for (int i = 0; i <= 30; i++) cout << g(i, 2) << " "; cout << endl;
  // for (int i = 0; i <= 30; i++) cout << g2(i, 2) << " "; cout << endl;
  // for (int i = 0; i <= 30; i++) cout << g(i, 3) << " "; cout << endl;
  // for (int i = 0; i <= 30; i++) cout << g2(i, 3) << " "; cout << endl;
  // for (int i = 0; i < 100; i++) {
    // for (int k = 1; k < 100; k++) {
      // if (g(i, k) != g3(i, k)) {
        // cout << i << " " << k << " " << g(i,k) << " " << g3(i,k) << endl;
      // }
    // }
  // }
  for (;!cin.eof();cin>>ws)
    main2();
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x7fffffff
const int N = 100000+5;
const int Mod = 1e9+7;
map<string,int>mp;
int n,m,T,t;
stringstream ss;
string s;
vector<int>G[N];

 int main() {
    cin>>n;
    int a[n],b[n];
    int cnt = 0;
    int ret = -inf;
    for(int i = 0;i < n;i++) {
        cin>>a[i]>>b[i];
        if(a[i]==b[i]) cnt++;
        else ret = max(cnt,ret),cnt = 0;
    }
    ret = max(cnt,ret);
    cout<<(ret>=3?"Yes\n":"No\n");
    return 0;
 }

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

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,k;
    long long ans=0;
    cin>>n>>k;
    vector<int>h(n);
    for(int i=0;i<n;i++){
        cin>>h[i];
    }
    if(k>=n){cout<<0;return 0;}
    sort(h.begin(),h.end());
    for(int i=0;i<n-k;i++){
        ans+=h[i];
    }
    cout<<ans;
}

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

int main() {
  int n,m,k;
  cin >> n >> m >> k ;
  vector<int>a(n),b(m);
  rep(i,n) cin >> a.at(i);
  rep(i,m) cin >> b.at(i);
  int ans =0;
  ll sumb=0;
  rep(i,m) sumb+=b.at(i);
  ll t=sumb;
  int j=m;
  rep(i,n+1) {
    while (t>k && j>0) {
      j--;
      t-=b.at(j);
    }
    if (t>k) break;
    ans=max(ans,i+j);
    if(i==n) break;
    t+=a[i];
  }
  cout << ans << endl;
}



Measured memory_bytes: 4832
from queue import Queue


def main():
    n = int(input())
    S = [list(map(int, input())) for _ in range(n)]


    def bfs(s):
        WHITE = 0
        GRAY = 1
        BLACK = 2

        color = [WHITE] * n
        d = [-1] * n
        q = Queue()
        q.put(s)
        color[s] = GRAY
        d[s] = 0
        while not q.empty():
            u = q.get()
            color[u] = BLACK
            for v, edge in enumerate(S[u]):
                if edge and color[v] == BLACK and d[v] + 1 != d[u]:
                    return -1
                if edge and color[v] == WHITE:
                    q.put(v)
                    color[v] = GRAY
                    d[v] = d[u] + 1

        return max(d) + 1

    cnt = -1
    for i in range(n):
        cnt = max(cnt, bfs(i))

    print(cnt)


if __name__ == "__main__":
    main()


Measured memory_bytes: 4464
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<map>
#include<functional>
#include<set>
#include<numeric>

#pragma region
using namespace std;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define rep(i,n) FOR(i,0LL,n)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define rrep(i,n) RFOR(i,0LL,n)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define COUNT(a,y,x) upper_bound(all(a), y) - lower_bound(all(a), x)
#define UNIQUE(a) sort(all(a)); a.erase(unique(all(a)), a.end())
#define SUM(a) accumulate(all(a),0LL)
#define pb push_back
#define endl '\n'
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef map<ll, ll> MAP;
const ll inf = 2222222222222222222LL;
const ll mod = 1000000007LL;

ll n = 0, m = 0, ans = 0, cnt = 0, tmp = 0, ma = -inf, mi = inf;
string s;
bool ok = true, flag = false;
ll dx[5] = { 1,-1,0,0,0 }, dy[5] = { 0,0,1,-1,0 };
ll ddx[9] = { 1,-1,0,0,1,1,-1,-1,0 }, ddy[9] = { 0,0,1,-1,1,-1,1,-1,0 };
#pragma endregion
#define MAX 222222

ll a[MAX], b[MAX];


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

  cin >> n >> m;
  rep(i, n) cin >> a[i] >> b[i];
  rep(i, n) {
	ok = true;
	rep(bit, 30) if ((a[i] >> bit & 1) && !(m >> bit & 1)) ok = false;
	if (ok) ans += b[i];
  }
  rep(bit, 30) {
	if (!(m >> bit & 1)) continue;
	tmp = 0;
	rep(i, n) {
	  ok = true;
	  if (a[i] >> bit & 1) ok = false;
	  RFOR(k,bit+1,31) if ((a[i] >> k & 1) && !(m >> k & 1)) ok = false;
	  if (ok) tmp += b[i];
	}
	ans = max(ans, tmp);
  }


  cout << ans << endl;


  return 0;
}


Measured memory_bytes: 1792
import java.io.*;


public class Main {

  static int N;

  static void dfs(String S, char ma){
    if(S.length() == N){
      System.out.println(S);
      return;
    }

    for(char c = 'a'; c < ma; c++){
      dfs(S + c, ma);
    }
    dfs(S + ma, ++ma);
    
    return;
  }
  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String[] in = br.readLine().split(" ");
    N = Integer.parseInt(in[0]);

    br.close();

    dfs("", 'a');
    
  }
}


Measured memory_bytes: 47788
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <vector>
#include <functional>
#include <cassert>
#include <cmath>
#include <map>
#include <set>

using namespace std;
unordered_map<int, int> M;

void Work() {
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		int x;
		cin >> x;
		if (!M.count(x)) {
			M[x] = 0;
		}
		M[x]++;
	}

	if (M.size() > 3) {
		cout << "No" << endl;
		return;
	}

	if (M.size() == 1) {
		cout << (M.count(0) ? "Yes" : "No") << endl;
		return;
	}

	else if (M.size() == 2) {
		int x, y;
		int cnt = 0;
		for (auto& p : M) {
			if (cnt == 0) {
				x = p.first;
				cnt++;
			}	
			else
				y = p.first;
		}
		if (x != 0 && y != 0) {
			cout << "No" << endl;
			return;
		}
		if (N % 3 != 0) {
			cout << "No" << endl;
			return;
		}
		if (y == 0) {
			swap(x, y);
		}
		if (M[x] != N / 3) {
			cout << "No" << endl;
			return;
		}
		cout << "Yes" << endl;
	}
	else {
		int x, y, z;
		int cnt = 0;
		for (auto& p : M) {
			if (cnt == 0) {
				x = p.first;
				cnt++;
			}
				
			else if (cnt == 1) {
				y = p.first;
				cnt++;
			}
			else {
				z = p.first;
			}
		}
		if ((x ^ y) != z) {
			cout << "No" << endl;
			return;
		}
		if (N % 3 != 0) {
			cout << "No" << endl;
			return;
		}
		if (M[x] != N / 3 || M[y] != N / 3) {
			cout << "No" << endl;
			return;
		}
		cout << "Yes" << endl;
	}
}

int main() {
	ios_base::sync_with_stdio(false);

	Work();

	return 0;
}

Measured memory_bytes: 3724
#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int W,H,x,y,r;
    cin >> W >> H >> x >> y >> r;
    
    if(0<=x-r && x+r<=W && 0<=y-r && y+r<=H){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
    
    return 0;
}


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

using namespace std;
struct city{
  int id;
  int year;
  int pref;
  int order;
};
bool compyear(city l, city r) {
    return l.year < r.year;
}
bool compid(city l, city r) {
    return l.id < r.id;
}

int main(){
    int n,m;
    cin>>n>>m;
    city cities[m];

    rep(i,m){
      int y,p;
      cin>>p>>y;
      city c={i,y,p,-1};
      cities[i]=c;
    }
    sort(cities,cities+m,compyear);
    int count[n+1]={};
    rep(i,m){
      count[cities[i].pref]++;
      cities[i].order=count[cities[i].pref];
    }
    sort(cities,cities+m,compid);
    rep(i,m){
      printf("%06d%06d\n",cities[i].pref,cities[i].order);
    }
    return 0;
}


Measured memory_bytes: 3456
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;

int main()
{
    int n;
    cin >> n;
    int p[100005], q[100005];
    for(int i = 0; i < n; i++){
        cin >> p[i];
        p[i]--;
        q[p[i]] = i;
    }
    set<int> st;
    ll l[100005], l2[100005], r[100005], r2[100005];
    for(int i = 0; i < n; i++){
        l[i] = -1;
        l2[i] = -1;
        r[i] = n;
        r2[i] = n;
    }
    for(int i = n - 1; i >= 0; i--){
        auto itr = st.lower_bound(q[i]);
        if(itr != st.end()){
            r[i] = *itr;
            if(++itr != st.end()){
                r2[i] = *itr;
            }
            itr--;
        }
        if(itr != st.begin()){
            l[i] = *--itr;
            if(itr != st.begin()){
                l2[i] = *--itr;
            }
        }
        st.insert(q[i]);
    }
    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans += (i + 1) * ((r2[i] - r[i]) * (q[i] - l[i]) + (r[i] - q[i]) * (l[i] - l2[i]));
    }
    cout << ans << endl;
}


Measured memory_bytes: 12152