akhauriyash/Code-Regression huggingface dataset:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

YES = "Yes"  # type: str
NO = "No"  # type: str

def solve(N: int, A: "List[int]", B: "List[int]"):
    p = []
    for i in range(N):
        p.append([A[i], B[i]])
    p.sort(key=lambda x: x[1])
    tmp = 0
    ret = YES
    for i in range(N):
        tmp += p[i][0]
        if tmp > p[i][1]:
            ret = NO
            break
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]" 
    B = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)

if __name__ == '__main__':
    main()


Measured memory_bytes: 41256
use std::vec::Vec;
use std::collections::HashMap;
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

fn read_touple<T: std::str::FromStr + Copy>() -> (T, T) {
  let v: Vec<T> = read_vec();
  assert_eq!(v.len(), 2);
  (v[0], v[1])
}

fn main() {
    let mut x: usize = read();
    let mut ans = 0;
    while true {
      let hash = trial_division(x);
      if hash.len() == 1 {
        let count = hash.iter().fold(0, |acc, (&key, &val)| acc + val);
        ans = x;
        if count == 1 {
          break
        }
      }
      x += 1;
    }
    println!("{}", x);
}

// 試し割り
// O(√N)
fn trial_division(n :usize) -> HashMap<usize, usize>{
    let mut primes = HashMap::new();
    let mut i = 2;
    let mut n = n;

    //  n を root n 以下の整数で割り切れるまで割っていく
    while i * i <= n {
        while n % i == 0 {
            n /= i;
            // match primes.get_mut(&i) {
            //   None => { primes.insert(i, 1); },
            //   Some(x) => {
            //       *x += 1;
            //   },
            // }

            match primes.get(&i) {
              Some(&val) => {
                primes.insert(i, val + 1);
              },
              _  => { primes.insert(i, 1); },
            }
        }
        i+=1;
    }

    // 最後にnが素数になっている場合はそれ自身も素因数に含めて終了
    // match primes.get_mut(&n) {
    //   None => { primes.insert(n, 1); },
    //   Some(x) => {
    //     *x += 1;
    //   },
    // }

     match primes.get(&n) {
          Some(&val) => {
            primes.insert(n, val + 1);
          },
          _  => { primes.insert(n, 1); },
      }

    primes
}

Measured memory_bytes: 4352
def cmb(n, r, mod):
  if (r<0 or r>n):
    return 0
  r = min(r, n-r)
  return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**6
g1 = [1, 1] # 元テーブル
g2 = [1, 1] # 逆元テーブル
inverse = [0, 1] # 逆元テーブル計算用テーブル

x, y = map(int, input().split())
if (x + y) % 3 != 0:
  print(0)
else:
  n = (x + y) // 3

  for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

  print(cmb(n, x - n, mod))

Measured memory_bytes: 122016
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long LL;
const int maxn = 2005;
const int mod = 1e9+7;

int dp[maxn][maxn],s[maxn][maxn],n,K; 
LL fpm(LL p,LL k) {
	LL res=1ll;
	while (k) {
		if (k&1) (res*=p)%=mod;
		(p*=p)%=mod;k>>=1;
	}
	return res;
}
void inc(int &x,int y) {
	x+=y;x>=mod?x-=mod:0;
}
int main()
{
	#ifdef Amberframe
		freopen("arc068f.in","r",stdin);
		freopen("arc068f.out","w",stdout);
	#endif
	scanf("%d %d",&n,&K);
	if (n==1) {printf("1");return 0;}
	dp[0][0]=s[0][0]=1;
	for (int i=1;i<=n;i++)
	for (int j=n;j>=0;j--)
	{
		if (i!=n) dp[i][j]=dp[i][j+1];
		//for (int k=max(0,i-j-1);k<i;k++)
		//	inc(dp[i][j],dp[k][j-i+k+1]);
		inc(dp[i][j],s[i-1][j]);
		s[i][j]=(s[i-1][j-1]+dp[i][j])%mod;
	}
	int res=(LL)dp[n][n-K]*fpm(2,max(0,n-K-1))%mod;
	printf("%d",res);
	return 0;
}

Measured memory_bytes: 31616
[n,m] = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
#table = [[0]*n for i in range(n)]
ls = [1] * n
for i in range(m):
    [a,b] = [int(j)-1 for j in input().split()]
    if h[a] < h[b]:
        ls[a] = 0
    elif h[a] > h[b]:
        ls[b] = 0
    else:
        ls[a] = 0
        ls[b] = 0
    #print(h[a],h[b])
print(sum(ls))
#print(ls)


Measured memory_bytes: 20120
from fractions import gcd

N, X = map(int, input().strip().split())
x = list(map(int, input().strip().split()))

x_diff = [abs(y-X) for y in x]
for i in range(N):
  if i == 0:
    tmp = x_diff[i]
  else:
    tmp = gcd(tmp, x_diff[i])
print(tmp)


Measured memory_bytes: 16280
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


pair<int,int> seg[800005];
int lz[800005];

void build(int ver,int tl,int tr)
{
	if(tl>tr)
		return ;
	else if(tl==tr)
	{
		lz[ver]=0;
		seg[ver]={0,tl};
	}
	else
	{
		int tm=(tl+tr)/2;
		build(2*ver,tl,tm);
		build(2*ver+1,tm+1,tr);
		seg[ver]=max(seg[2*ver],seg[2*ver+1]);
	}	
}

void push(int ver,int tm,int tr)
{
	lz[2*ver]=lz[ver];
	lz[2*ver+1]=lz[ver];
	seg[2*ver].first=0;
	seg[2*ver].second=tm;
	seg[2*ver+1].first=0;
	seg[2*ver+1].second=tr;
	lz[ver]=0;
}

void upd_pos(int ver,int tl,int tr,int pos,int val,int mid)
{
	if(tl>tr)
		return ;
	else if(tl==tr)
	{
		seg[ver].first+=val;
		if(seg[ver].first==mid)
			seg[ver].second=0;
	}
	else
	{
		int tm=(tl+tr)/2;
		if(lz[ver])
			push(ver,tm,tr);
		if(tm>=pos)
			upd_pos(2*ver,tl,tm,pos,val,mid);
		else
			upd_pos(2*ver+1,tm+1,tr,pos,val,mid);
		if(seg[2*ver+1].first<mid)
			seg[ver]=seg[2*ver+1];
		else
			seg[ver]=seg[2*ver];
	}	
}

void upd_lz(int ver,int tl,int tr,int l,int r,int mid)
{
	if(tl>tr||l>r||tr<l||r<tl)
		return ;
	else if(tl==l&&tr==r)
	{
		seg[ver].first=0;
		seg[ver].second=tr;
		lz[ver]=1;
	}	
	else
	{
		int tm=(tl+tr)/2;
		if(lz[ver])
			push(ver,tm,tr);
		upd_lz(2*ver,tl,tm,l,min(tm,r),mid);
		upd_lz(2*ver+1,tm+1,tr,max(tm+1,l),r,mid);
		if(seg[2*ver+1].first<mid)
			seg[ver]=seg[2*ver+1];
		else
			seg[ver]=seg[2*ver];
	}	
}

int get(int ver,int tl,int tr,int l,int r,int mid)
{
	if(tl>tr||l>r||tr<l||r<tl)
		return 0;
	else if(tl==l&&tr==r)
	{
		if(seg[ver].first==mid)
			return 0;			
		return seg[ver].second;
	}
	else
	{
		int tm=(tl+tr)/2;
		if(lz[ver])
			push(ver,tm,tr);

		if(seg[2*ver+1].first<mid)
			seg[ver]=seg[2*ver+1];
		else
			seg[ver]=seg[2*ver];
		return max(get(2*ver,tl,tm,l,min(tm,r),mid),get(2*ver+1,tm+1,tr,max(tm+1,l),r,mid));
	}	
}


signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);  
cout.tie(0);   

#ifndef ONLINE_JUDGE
	if(fopen("INPUT.txt","r"))
	{
	freopen ("INPUT.txt" , "r" , stdin);
	freopen ("OUTPUT.txt" , "w" , stdout);
	}
#endif  

	int n;
	cin>>n;
	vector<int>temp;
	int a[n+1];
	a[0]=1e10;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]>a[i-1])
			continue;
		temp.push_back(a[i]);
	}

	set<int>s;
	map<int,int>mm;
	for(auto i:temp)
		s.insert(i);
	int cnt=0;
	for(auto i:s)
		mm[i]=++cnt;
	int sz=temp.size();
	// for(int i=0;i<sz;i++)
	// 	temp[i]=mm[temp[i]];
	
	int l=0,r=200000;
	int mn=2000000;
	// cout<<500<<'\n';
	// for(int i=1000;i>=2;i-=2)
	// {
	// 	cout<<i<<" ";		
	// }
	while (l<=r)
	{
		int mid=l+(r-l)/2;
		int sz=temp.size();
		build(1,1,cnt);
		bool flag=true;
		for(int i=1;i<sz;i++)
		{
			int z=get(1,1,cnt,1,temp[i],mid);
			if(z==0)
			{
				flag=false;
				break;
			}
			upd_pos(1,1,cnt,z,1,mid);
			upd_lz(1,1,cnt,z+1,cnt,mid);
		}
		if(flag)
		{
			r=mid-1;
			mn=min(mn,mid+1);
		}
		else
		{
			l=mid+1;
		}		
	}

	cout<<mn;


}


Measured memory_bytes: 39076
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i < (n); i++)
#define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;
int INF = 1001001001;

int main() {
  int a, b;
  cin >> a >> b;
  if(b==1) {
    cout << 0 << endl;
    return 0;
  }
  int t = a;
  int ans = 1;
  while(t<b) {
    t+=a;
    t--;
    ans++;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3552
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
	int N,flag=0;
	cin>>N;
	vector<string> vec(N);
	for (int i = 0; i < N; i++) {
 	 	cin >> vec.at(i);
	}
  	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			if(i!=j&&vec.at(i)==vec.at(j)){
				flag=1;
			}
		}
	}
	for(int i=0;i<N-1;i++){
		if(vec.at(i).at((vec.at(i).size())-1)!=vec.at(i+1).at(0))
		{flag=1;}
	}

	if(flag==1){cout<<"No"<<endl;}else{{cout<<"Yes"<<endl;}}
	return 0;
}





Measured memory_bytes: 256
import java.util.Scanner;

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

		int A = sc.nextInt();
		int B = sc.nextInt();
		String ans = "";

		if ( (A * B) % 2 == 0 ) {
			ans = "No";
		} else {
			ans = "Yes";
		}
		System.out.println( ans );
	}
}

Measured memory_bytes: 21844
#include <stdio.h>

int main(void)
{
	int a, b, hc, fhc, tc;

	while (1) {
		scanf("%d%d", &a, &b);
		if (a == 0 && b == 0)
			break;
		tc = b - a;
		fhc = tc % 1000;
		hc = fhc % 500;
		printf("%d ", hc / 100);
		printf("%d ", fhc / 500);
		printf("%d\n", tc / 1000);
	}

	return 0;
}

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var strarr = Console.ReadLine().Split();
            var a = int.Parse(strarr[0]);
            var b = int.Parse(strarr[1]);
            var c = int.Parse(strarr[2]);


            if (a == b)
            {
                if (b == c) Console.WriteLine("No");
                else Console.WriteLine("Yes");
            }
            else
            {
                if (b == c) Console.WriteLine("Yes");
                else if (a == c) { Console.WriteLine("Yes"); }
                else  Console.WriteLine("No");

            }

        }
    }
}


Measured memory_bytes: 11092
// 見せてやるぜ、奇跡ってやつをよぉ……
#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
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;}
#define DEBUG_VLL(vec) REP(i, vec.size()) cerr<<vec[i]<<(i==vec.size()-1?'\n':' ');

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll i, o, t, j, l, s, z; cin >> i >> o >> t >> j >> l >> s >> z;

    ll ans1;
    if (i>0 && j>0 && l>0) {
        ans1 = 3;
        ans1 += ((i-1)/2)*2 + ((j-1)/2)*2 + ((l-1)/2)*2;
    } else ans1=0;

    ll ans2 = (i/2)*2 + (j/2)*2 + (l/2)*2;
    cout << max(ans1, ans2) + o << '\n';

    return 0;
}


Measured memory_bytes: 256
S = input()
revFlg = False
Q = int(input())
first_append = ""
last_append = ""
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        revFlg = not revFlg
    else:
        if query[1] == "1":
            if revFlg:
                last_append = last_append+query[2]
            else:
                first_append = query[2]+first_append

        else:
            if revFlg:
                first_append = query[2]+first_append
            else:
                last_append = last_append+query[2]

ans = first_append+S+last_append
if revFlg:
    print(ans[::-1])
else:
    print(ans)


Measured memory_bytes: 4616
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        HashMap<Integer,List<Integer>>map=new HashMap<>();
        int[] a=new int[n+1];
        for(int i=1;i<=n;i++)
        {
            int ball=sc.nextInt();
            a[i]=ball;
            if(map.get(ball)==null)map.put(ball,new ArrayList<>());
            map.get(ball).add(i);
        }

        long sum=0;
        for(int ball:map.keySet()){
            long s=map.get(ball).size();
            sum+=s*(s-1)/2;
        }
        for(int k=1;k<=n;k++){
            long res=sum;
            int ball=a[k];
            long s=map.get(ball).size();
            res-=s-1;
            System.out.println(res);
        }
    }
}

Measured memory_bytes: 111372
#include<bits/stdc++.h>
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9+7;
//const ll MOD = 998244353;
//const ll MOD = 924844033;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
const double EPS = 1e-5;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

#ifdef DEBUG
#define OUTPUT(x) (output(x), outendl())
#else
#define OUTPUT(x) (void)0
#endif

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

bool was_output = false;
template<class t>
void output(t a){
	if(was_output)cout << " ";
	cout << a;
	was_output = true;
}
void outendl(){
	was_output = false;
	cout << endl;
}

int n;
int m;
vector<pii> colors;
vvector<int> edges;

void dye(int place,int distance,int color){
    if(distance<0)return;
    if(colors[place].first>0&&colors[place].second>=distance){
        return;
    }
    if(colors[place].first==0){
        colors[place].first = color;
    }
    colors[place].second = distance;
    foreach(i,edges[place]){
        dye(i,distance-1,color);
    }
}

int main(){
    cin >> n >> m;
    colors.resize(n,pii(0,0));
    edges.resize(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        --a;
        --b;
        edges[a].push_back(b);
        edges[b].push_back(a);

    }
    int q;
    cin >> q;
    vvector<int> line(q);
    foreach(i,line){
        vector<int> d(3);
        foreach(j,d){
            cin >> j;
        }
        --d[0];
        i = d;
    }
    reverse(all(line));
    foreach(i,line){
        dye(i[0],i[1],i[2]);
    }
    foreach(i,colors){
        cout << i.first << endl;
    }


	return 0;
}


Measured memory_bytes: 14456
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=5005;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
int ans[N],f[N],a[N],sz[N],n;
void dfs1(int u,int fa){
	f[u]=0;
	go(u)if(v!=fa){
		dfs1(v,u),++sz[u];
		if(a[u]>a[v])f[u]|=!f[v];
	}
}
void dfs2(int u,int fa,int pa){
	pa=(a[u]>a[fa]&&!pa);
	ans[u]=f[u]|pa;
	vector<int>st(sz[u]+2),Pre(sz[u]+2),suf(sz[u]+2);
	int top=0;
	go(u)if(v!=fa)st[++top]=v;
	Pre[0]=0;fp(i,1,top)Pre[i]=Pre[i-1]|(a[u]>a[st[i]]&&!f[st[i]]);
	suf[top+1]=0;fd(i,top,1)suf[i]=suf[i+1]|(a[u]>a[st[i]]&&!f[st[i]]);
	fp(i,1,top)dfs2(st[i],u,Pre[i-1]|suf[i+1]|pa);
}
int main(){
	scanf("%d",&n);
	fp(i,1,n)scanf("%d",&a[i]);
	for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v),add(v,u);
	dfs1(1,0),dfs2(1,0,1);
	fp(i,1,n)if(ans[i])printf("%d ",i);
	return 0;
}

Measured memory_bytes: 896
#include <iostream>
using namespace std;
#define abs(x) (x>0?(x):(-(x)))

int main() {
  int n, a;
  int max, min, maxd, mind;
  max = min = maxd = mind = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a;
    if (a >= max) {
      max = a;
      maxd = i;
    }
    if (a <= min) {
      min = a;
      mind = i;
    }
  }
  cout << 2*n - 1 << endl;
  if (abs(max) >= abs(min)) {
    for (int i = 1; i <= n; ++i) {
      cout << maxd << " " << i << endl;
    }
    for (int i = 1; i < n; ++i) {
      cout << i << " " << i+1 << endl;
    }
  } else if (abs(max) < abs(min)) {
    for (int i = 1; i <= n; ++i) {
      cout << mind << " " << i << endl;
    }
    for (int i = n; i > 1; --i) {
      cout << i << " " << i-1 << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <queue>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <math.h>
#include <stdlib.h>
#include <numeric>
 
 
int main(){
	long double n, k, all, total = 0;
	std::cin >> n >> k;
	for(int i = 1; i <= n; i++){
		all = 0;
		int j = i;
		for(j = i; j < k; j *= 2){
			all++;
		}
		total += 1 / std::pow(2, all);
	}
	long double ans = total;
	ans /= n;
	std::cout << std::setprecision(64)<< ans << std::endl;
	return 0;
}


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

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

}

Measured memory_bytes: 3096
def wab(w,a,b):
    ans = 0
    if w+a >= b and a <= b or a <= b+w and a >= b:
        return ans
    ans = b - (w + a) if a < b else a - (w + b)
    return ans

def main():
    W , a , b = map(int, input().split())
    print(wab(W , a , b))

if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
import java.util.*;
import static java.lang.Math.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        long k = sc.nextLong();
        int memo = 0;
        while(s.length()>memo && s.charAt(memo)=='1'){ 
            memo++;
        }
        System.out.println(k-1<memo? '1':s.charAt(memo));
    }
}

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

int main(){
	int vnum, capacity;
	std::vector<int> vprice;

	while(std::cin >> vnum >> capacity){
		//end
		if(vnum == 0 && capacity == 0) break;

		int tmp,buf = capacity;
		int total = 0;
		std::vector<int> vprice;

		//input
		for(int i = 0; i < vnum; ++i){
			std::cin >> tmp;
			vprice.push_back(tmp);
		}

		//sort
		std::sort(vprice.begin(), vprice.end(), std::greater<int>());

		//sum price
		std::for_each(vprice.begin(),vprice.end(), 
			[&total,buf,&capacity](int x) { capacity != 1 ? (total += x, --capacity) : capacity = buf; } );

		//output
		std::cout << total << std::endl;
	}

	return 0;
}

Measured memory_bytes: 1228
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
import           Control.Monad.ST            (ST)
import           Data.Bits                   (shiftR)
import qualified Data.ByteString.Char8       as B
import           Data.Char                   (isSpace)
import           Data.List                   (unfoldr)
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM

main :: IO ()
main = do
  [n, k] <- unfoldr parseInt <$> B.getLine
  hs <- V.unfoldr parseInt <$> B.getContents
  print $ solve n k hs

solve :: Int -> Int -> V.Vector Int -> Int
solve n k hs = minimum [hs' V.! (i + k - 1) - hs' V.! i | i <- [0 .. n - k]]
  where
    hs' = sort' hs

sort' :: V.Vector Int -> V.Vector Int
sort' = V.modify (\v -> quicksort v 0 (VM.length v))

-- https://github.com/as-capabl/haskell-qsort-pfm/blob/master/src/UV.hs
quicksort :: VM.STVector s Int -> Int -> Int -> ST s ()
quicksort work iStart iEnd
    | iEnd - iStart <= 1 = return ()
    | otherwise = do
        pivot <- VM.unsafeRead work (((iEnd - iStart) `shiftR` 1) + iStart)
        med <- divide pivot iStart (iEnd - 1)
        quicksort work iStart med
        quicksort work med iEnd
  where
    divide pivot iS' iE'
        | iS' > iE' = return iS'
        | otherwise = do
            lower <- shrinkLower pivot iS' iE'
            higher <- shrinkHigher pivot lower iE'
            if lower >= higher
              then do
                return lower
              else do
                swapWork lower higher
                divide pivot (lower + 1) (higher - 1)

    shrinkLower pivot iS' iE'
        | iS' > iE' = return iS'
        | otherwise = do
            x <- VM.unsafeRead work iS'
            if x >= pivot
              then
                return iS'
              else
                shrinkLower pivot (iS' + 1) iE'

    shrinkHigher pivot iS' iE'
        | iS' > iE' = return iE'
        | otherwise = do
            x <- VM.unsafeRead work iE'
            if x <= pivot
              then
                return iE'
              else
                shrinkHigher pivot iS' (iE' - 1)

    swapWork i j = VM.unsafeSwap work i j

parseInt :: B.ByteString -> Maybe (Int, B.ByteString)
parseInt = B.readInt . B.dropWhile isSpace


Measured memory_bytes: 6396
#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define vi vector<int>
#define lvi vector<long long>
#define pi pair<int,int>
#define lpi pair<long long ,long long>
#define mp make_pair
#define pb push_back
#define ll long long 
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>multiordered_set;
ll int pw(ll int a, ll int b,ll md) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2,md);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
vector<int> pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.push_back(p);
}
bool cmp(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first>b.first;
}
ll int gcd(ll int a,ll int b)
{
    if(!a)return b;
    return gcd(b%a,a);
}
bool isPrime(int n) 
{ 
    if (n <= 1) 
        return false; 
    for (int i = 2; i < n; i++) 
        if (n % i == 0) 
            return false; 
    return true; 
} 
map<int,int>pa;
int find(int i){
    if(pa[i]==i)return i;
    return pa[i]=find(pa[i]);
}     
void join(int i,int j){
    int a=find(i);
    int b=find(j);
    if(b>=a)pa[b]=a;
    else pa[a]=b;
}               
int ddx[]={-1,-1,-1,0,1,1,1,0};
int ddy[]={-1,0,1,1,1,0,-1,-1};
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
int main()
{
    faltu;
    int t=1;
   // cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int ans=0;
        for(char ch:s){
            if(ch=='+')ans++;
            else ans--;
        }
        cout<<ans;
    }
}

Measured memory_bytes: 3548
import java.util.*;
import java.math.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long H = sc.nextLong();
    long W = sc.nextLong();
    sc.close();

    long ans = 1;
    if (1 < H && 1 < W) ans = (H * W + 1) / 2L;
    System.out.println(ans);
  }
}


Measured memory_bytes: 22996
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

struct Point {
  int x, y;
  Point(): x(0), y(0) {}
  Point(int _x, int _y): x(_x), y(_y) {}
  bool operator<(const Point& a) const {
    if (x == a.x) return y < a.y;
    return x < a.x;
  }
};


int main() {
  int n;
  scanf("%d", &n);
  vector<Point> red(n);
  vector<Point> blue(n);
  for (int i = 0; i < n; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    red[i] = Point(a, b);
  }
  for (int i = 0; i < n; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    blue[i] = Point(a, b);
  }

  sort(blue.begin(), blue.end());
  vector<bool> used(n, false);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    Point b = blue[i];
    int valid_idx = -1;
    int ma = -1;
    for (int j = 0; j < n; ++j) {
      if (used[j]) continue;
      if (b.x > red[j].x && b.y > red[j].y && red[j].y > ma) {
	valid_idx = j;
	ma = red[j].y;
      }
    }
    if (valid_idx != -1) {
      ans++;
      used[valid_idx] = true;
    }
  }
  printf("%d\n", ans);
  return 0;
}



Measured memory_bytes: 256
// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n);      // 各桁の和を計算する。
string upper(string str);       // 英字を大文字に変換する。
string lower(string str);       // 英字を小文字に変換する。

// define
// ------------------------------------------------
#define all(a)        (a).begin(),(a).end()
#define rall(a)       (a).rbegin(),(a).rend()
#define pb            push_back
#define sz(a)         int((a).size())
#define rep(i,n)      for(int(i)=0;(i)<(n);(i)++)
#define repe(i,n)     for(int(i)=0;(i)<=(n);(i)++)
#define vsort(v)      sort((v).begin(),(v).end())
#define rvsort(v)     sort(rall((v)))
#define vi            vector<int>

const int INF = 1e9;

// code
// ------------------------------------------------
int main() {

  int a,b,c;

  cin >> a >> b >> c;

  if(a <= c && c <= b)  cout << "Yes" << endl;
  else cout << "No" << endl;  



  return 0;
}

// funcの実体
// ------------------------------------------------
int CalcSumOfDigit(int n)
{
  int s = 0;
  while(n)
  {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(97 <= *itr && *itr <= 122)
    {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(65 <= *itr && *itr <= 90)
    {
      *itr = *itr + 32;
    }
  }
  return str;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;
 
int main()
{
    int n;
    while(1){
	cin>>n;
	if(cin.eof()) break;
	vector <int> a;
	while(n>1){
		a.push_back(n%2);
		n=n/2;
	}
	a.push_back(n);
	for(int i=0;i<a.size()-1;i++){
		if(a[i]*pow(2,i)==0) continue;
		cout<<a[i]*pow(2,i)<<" ";
	}
	cout<<a[a.size()-1]*pow(2,a.size()-1)<<endl;
    }
    return 0;
}

Measured memory_bytes: 1324
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_MOD			1000000007								// 除数(10の9乗+7)
#define D_CHAR_MAX		100005									// 最大文字数
#define D_DIV			13										// 除数

// 内部変数
static FILE *szpFpI;											// 入力
static char sc1Str[D_CHAR_MAX];									// 文字列
static int si2PCnt[D_CHAR_MAX][D_DIV];							// パターン数[文字数][余り]

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// 出力
int
fOut(
	char *pcpLine				// <I> １行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// 実行メイン
int
fMain(
)
{
	int i, j, k;

	// 文字列 - 取得
	fgets(sc1Str, sizeof(sc1Str), szpFpI);

	// パターン数 - 初期値
	si2PCnt[0][0] = 1;

	// 桁数でループ
	for (i = 0; ; i++) {

		// 対象文字
		int liNum;
		if ('0' <= sc1Str[i] && sc1Str[i] <= '9') {
			liNum = sc1Str[i] - '0';
		}
		else if (sc1Str[i] == '?') {
			liNum = -1;
		}
		else {
			break;
		}

		// 数字の種類でループ
		for (j = 0; j < 10; j++) {

			// 対象文字
			if (liNum >= 0 && liNum != j) {
				continue;
			}

			// 余りの種類でループ
			for (k = 0; k < D_DIV; k++) {

				// 次の余り - 取得
				int liNext;
				switch (k) {
					case 0:		liNext = 0;		break;
					case 1:		liNext = 10;	break;
					case 2:		liNext = 7;		break;
					case 3:		liNext = 4;		break;
					case 4:		liNext = 1;		break;
					case 5:		liNext = 11;	break;
					case 6:		liNext = 8;		break;
					case 7:		liNext = 5;		break;
					case 8:		liNext = 2;		break;
					case 9:		liNext = 12;	break;
					case 10:	liNext = 9;		break;
					case 11:	liNext = 6;		break;
					case 12:	liNext = 3;		break;
				}
				liNext = (liNext + j) % D_DIV;

				// パターン数 - 加算
				si2PCnt[i + 1][liNext] = (si2PCnt[i + 1][liNext] + si2PCnt[i][k]) % D_MOD;
			}
		}
	}

	return si2PCnt[i][5];
}

// １回実行
int
fOne(
)
{
	int liRet;
	char lc1Buf[1024];

	// データ - 初期化
	memset(si2PCnt, 0, sizeof(si2PCnt));						// パターン数

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	liRet = fMain();

	// 出力
	sprintf(lc1Buf, "%d\n", liRet);
	fOut(lc1Buf);

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}



Measured memory_bytes: 5376
#include<bits/stdc++.h>
using namespace std;
int main(void){
    long long int a,b,c,k,ans=0;
    cin>>a>>b>>c>>k;
    if(k-a>=0){
        ans+=a;
    }
    if(k-a<0){
        cout<<k<<endl;
        return 0;
    }
    if(k-a-b<0){
        cout<<ans<<endl;
        return 0;
    }else{
        ans-=(k-a-b);
        cout<<ans<<endl;
        return 0;
    }
}


Measured memory_bytes: 3596
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define rep(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define print(v) for (const auto itr : v){cout << itr << ' ';} cout << endl;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define endl "\n"
#define inf 1e18
using namespace std;
const ll INF = 1e18L + 5;
const ll maxn = 1e5+5, maxn1=2e5+3,dft=1e7;
#define MAXN 200005
ll ceel(ll a, ll b)
{
   if (a % b == 0)
       return a / b;
   return a / b + 1;
}
ll modneg(ll a,ll m)
{
    a = a % m;
    if(a<0)
        a=a+m;
    return a;
}
ll powermod(ll x, unsigned ll y, ll p){ll res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); };
 
 
 
int main()
{

ll n,i,j,k,a,b,c,d,test;
cin>>a>>b;
cout<<a*b;
return 0;
}

Measured memory_bytes: 3576
#include<stdio.h>
main(){
  int a[5],i,j,temp;
  scanf("%d %d %d %d %d",&a[0],&a[1],&a[2],&a[3],&a[4]);

  for(i=4;i>0;i--){
    for(j=0;j<i;j++){
      if(a[j]<a[j+1]){
	temp=a[j];
	a[j]=a[j+1];
	a[j+1]=temp;
      }
    }
  }
  printf("%d %d %d %d %d\n",a[0],a[1],a[2],a[3],a[4]);
  return 0;
}

Measured memory_bytes: 0
#define inf 9223372036854775807
#define ll long long
#define pii pair<ll,ll>
#define m_p  make_pair
#define ull unsigned long long
#define spd  ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
ll par[N];
ll cnt[N];
ll Findparent(ll a)
{
    if(par[a]==a)return a;

    par[a] = Findparent(par[a]);

    return par[a];
}

void Union(ll a,ll b)
{
    a = Findparent(a);
    b = Findparent(b);

    par[b] = a;
}
int main()
{
    spd
    ll n,m,a,b,ans=0,i;

    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
        par[i] = i;
        cnt[i] = 1;
    }
    for(i=0;i<m;i++)
    {
        cin>>a>>b;
        ll x = Findparent(a);
        ll y = Findparent(b);
        if(x!=y)
        {
            if(cnt[par[a]]<cnt[par[b]])swap(a,b);
            cnt[par[a]]+=cnt[par[b]];
            Union(a,b);
        }
    }

    for(i=1;i<=n;i++)
    {
        ans = max(ans,cnt[i]);
    }

    cout<<ans<<endl;
}


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

#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(int i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define sz(x) ((int)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair
using ll  = long long;
using vi  = vector<int>;
using vvi = vector<vi>;
using vs  = vector<string>;
using pii = pair<int,int>;
constexpr ll inf = 1ll<<61;
constexpr ll mod = 1e9+7;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>void add(T &a, const T &b){a += b; a %= mod;}

signed main(){
  int n,m; cin >> n >> m;
  vi x(n+2);
  rep(i,m){
    int l, r; cin >> l >> r; l--; r--;
    x[l]++; x[r+1]--;
  }
  rep(i,n+1){
    x[i+1] += x[i];
  }
  int ans = 0;
  rep(i,n+2){
    if(x[i] == m) ans++;
  }
  cout << ans << endl;
}


Measured memory_bytes: 1024
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native")
//#pragma comment(linker, "/stack:200000000")
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
 
#define X first
#define Y second
 
//#include <boost/unordered_map.hpp>
//using namespace boost;
 
/*
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
rbtree T;
*/
 
using i32 = int;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
//using i128 = __int128_t;
//using u128 = __uint128_t;
using i128 = i64;
using u128 = u64;
 
ll power(ll a, ll b, ll p)
{
	if (!b) return 1;
	ll t = power(a, b/2, p);
	t = t*t%p;
	if (b&1) t = t*a%p;
	return t;
}
 
ll exgcd(ll a, ll b, ll &x, ll &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll px, py;
	ll d = exgcd(b, a%b, px, py);
	x = py;
	y = px-a/b*py;
	return d;
}
 
template<class T>
inline void freshmin(T &a, const T &b)
{
	if (a > b) a = b;
}
 
template<class T>
inline void freshmax(T &a, const T &b)
{
	if (a < b) a = b;
}
 
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
 
int inp() {
	int x = 0, f = 0; char ch;
	for(ch = getchar(); !isdigit(ch); ch = getchar())
		if(ch == '-') f = 1;
	for(; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
	return f ? -x : x;
}
 
ll inp_ll() {
	ll x = 0; int f = 0; char ch;
	for(ch = getchar(); !isdigit(ch); ch = getchar())
		if(ch == '-') f = 1;
	for(; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
	return f ? -x : x;
}
 
template<class T>
bool read(T &x)
{
	x = 0;
	char ch = getchar();
	if (ch == EOF) return 0;
	for(; !isdigit(ch); )
	{
		ch = getchar();
		if (ch == EOF) return 0;
	}
	for(; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
	return 1;
}
 
template<class T>
void write(T x)
{
	static char s[22];
	static char *it = s+20;
	static char *end = s+20;
	if (!x)
		*-- it = '0';
	while (x)
	{
		*-- it = x%10+'0';
		x /= 10;
	}
	for (; it < end; ++ it)
		putchar(*it);
}
 
template<class T>
void writeln(T x)
{
	write(x);
	putchar('\n');
}
 
const int MAXN = 200010;
const int INF = 1000000000;
const int MOD = 1000000007;

int n;
int a[MAXN], b[MAXN];

bool cmp(int *a, int *b)
{
	for (int i = 1; i <= n; ++ i)
		if (a[i] != b[i]) return 0;
	return 1;
}

int main()
{
	
	n = inp();
	for (int i = 1; i <= n; ++ i)
		a[i] = b[i] = inp();
	sort(b+1, b+n+1);
	for (int i = 1; i <= n; ++ i)
		for (int j = i; j <= n; ++ j)
		{
			swap(a[i], a[j]);
			if (cmp(a, b))
			{
				puts("YES");
				return 0;
			}
			swap(a[i], a[j]);
		}
	puts("NO");
	
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define mod 1000000007
#define MOD 998244353
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
vector<string> solve(vector<string> V) {
    vector<string> res;
    for(auto s : V) {
        int count = 0;
        rep(i, s.size()) count = max(count, (int)s[i] - 'a');
        rep(j, count + 2) {
            res.push_back(s + (char)('a' + j));
        }
    }
    return res;
}
signed main(void) {
    int n;
    cin >> n;
    vector<string> V;
    V.push_back("a");
    rep(i, n-1) V = solve(V);
    sort(all(V));
    for(auto x : V) cout << x << endl;
}


Measured memory_bytes: 10916
import java.util.*;
import java.awt.*;
import static java.lang.System.*;
import static java.lang.Math.*;
public class Main {
    public static void main(String[] $) {
        Scanner sc = new Scanner(in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        long[] f=new long[k+1];
        Point[] sushi = new Point[n];
        for (int i = 0; i < n; i++) {
            sushi[i]=(new Point(sc.nextInt(), sc.nextInt()));
        }
        HashSet<Integer> S=new HashSet<>();
        Arrays.sort(sushi,(p1, p2) -> p2.y - p1.y);
        long temp=0;
        int[] s=new int[n+1];
        for (int i = 0; i < k; i++) {
            temp += (long) sushi[i].y;
            S.add(sushi[i].x);
            s[sushi[i].x]++;
        }
        int size=S.size();
        f[size]=temp;
        int left=k-1,right=k;
        while (0<=left&&right<n){
            while (0<=left&&s[sushi[left].x]==1){
                left--;
            }
            while (right<n&&s[sushi[right].x]>0){
                right++;
            }
            if(0<=left&&right<n){
                f[size+1]=f[size]-sushi[left].y+sushi[right].y;
                size++;
                s[sushi[right].x]++;
                s[sushi[left].x]--;
              left--;
            }
        }
        long ans=0;
        for (long i = 1; i <= k; i++) {
            if(f[(int)i]>0)ans=max(ans,f[(int)i]+i*i);
        }
        out.println(ans);
    }
}

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

int main() {
    int N, Alice = 0, Bob = 0;
    cin >> N;
    vector<int> v(N);

    for (int i = 0; i < N; i++) {
        cin >> v.at(i);
    }
    sort(v.begin(), v.end(), greater<int>());

    for (int i = 0; i < N; i++) {
        if ((i % 2) == 0)
            Alice += v.at(i);
        else
            Bob += v.at(i);
    }

    cout << Alice - Bob << endl;
}

Measured memory_bytes: 3648
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
#include <set>
#include <bitset>

#define rep(i,j,k) for(int i=j;i<k;i++)
#define Sort(x) sort((x).begin(),(x).end())
#define fi first
#define se second
#define vi vector<int>
#define INF 1000000000
#define MOD 1000000009
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef std::pair<int,int> pii;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;


int main(){
    vector<string> v;
    vector<vector<int>> count;
    string s;
    int n;
    for(int i=0;cin>>s>>n;i++){
        if(i==0){
            v.pb(s);
            count.pb(vector<int>(1,n));
        }else {
            bool f=true;
            rep(i,0,v.size()){
                if(v[i]==s){
                    count[i].pb(n);
                    f=false;
                }
            }
            if(f){
                v.pb(s);
                count.pb(vector<int>(1,n));
            }
        }
    }
    
    rep(i,0,count.size())Sort(count[i]);
    
    rep(i,0,v.size()-1){
        rep(j,i+1,v.size()){
            if(v[i]<v[j]){
                string temp=v[i];
                v[i]=v[j];
                v[j]=temp;
                
                count[i].swap(count[j]);
            }
        }
    }
    
    for(int i=v.size()-1;i>=0;i--){
        cout<<v[i]<<endl;
        rep(j,0,count[i].size()){
            if(j!=0)cout<<" ";
            cout<<count[i][j];
        }
        cout<<endl;
    }
    
    return 0;
}




Measured memory_bytes: 3032
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

int main(){
  string s;
  cin>>s;
  long ans = 0;
  //ans = stol(s);
  for(int i = 0;i<(1<<(s.size()-1));i++){
    // cout<<"i:"<<i<<endl;
    long tmp = 0;
    for(int j = 0;j<s.size();j++){
      tmp *= 10;
      tmp += s[j] - '0';
      if((i & 1<<j)!= 0){
	//足す
	ans += tmp;
	//cout<<"tmp:"<<tmp<<endl;
	tmp = 0;
      }
    }
    ans += tmp;
    //cout<<"now:"<<ans<<endl;
  }
  cout<<ans<<endl;
    
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
const int N = 2e5+6;
int dp[N];
int a[N];
int b[N];
void solve(){
	memset(dp,0x3f,sizeof dp);
	int h,n; 
	cin>>h>>n; 
	for(int i=1;i<=n;i++){
		cin>>a[i]>>b[i];
	}
	dp[0] = 0;
	for(int i=1;i<=1e4+h;i++){
		for(int j=1;j<=n;j++){
			if(i>=a[j]){
				dp[i] = min(dp[i],dp[i-a[j]]+b[j]);
			}
		}
	}
	int ans = 1e9;
	for(int i=h;i<1e4+h;i++)ans =min(ans,dp[i]);
	cout<<ans<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1; 
//	cin>>t; 
	while(t--){
		solve();
	}
	return 0;
}


Measured memory_bytes: 4460
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x)          (x).begin(), (x).end()
#define rep(i, n)       for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t)   for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> make_v(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initValue, args...))>(sz, make_v<T>(initValue, args...));
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail&... tail) { cin >> head; read(tail...); }

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) out << e << delim[&e == &vec.back()];
    return out;
}
// }}} End Header

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

    int N;
    read(N);

    vector<pii> p[20];
    rep(i, N) {
        int A; read(A);
        p[i].resize(A);

        rep(j, A) {
            int x, y;
            read(x, y);
            --x;
            p[i][j] = pii(x, y);
        }
    }

    const auto ok = [&](bitset<16> b) -> bool {
        rep(i, N) {
            // i is honest
            if (b[i] == 1) {
                for (auto e : p[i]) {
                    const int person = e.first;
                    const int type = e.second;
                    if (b[person] != type) return false;
                }
            }
        }
        return true;
    };

    int ans = 0;
    rep(b, 1 << N) {
        if (ok(b)) {
            chmax(ans, __builtin_popcount(b));
        }
    }

    print(ans);

    return 0;
}


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

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

  int value = N % 10;
  if(value==3){
    cout << "bon" << endl;
  }else if(value==0||value==1||value==6||value==8){
    cout << "pon" << endl;
  }else{
    cout << "hon" << endl;
  }
}


Measured memory_bytes: 3640
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll k;
ll n = 50;
ll a[55];

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

    cin >> k;
    for (int i = 0; i < n; ++i)
        a[i] = n - 1 + k / n;
    k %= n;
    if (k) {
        for (int i = 0; i < n - k; ++i)
            a[i] -= k;
        for (int i = n - k; i < n; ++i)
            a[i] += n - (k - 1);
    }

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

    return 0;
}


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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod =(1e9)+7;
using P = pair<ll,ll>;

int main(){
   ll n;cin >>n;
   vector<ll>l;
   vector<ll>r;
    for (int i = 0; i < n; ++i) {
        ll a;cin >>a;
        if(i%2==0){
            r.push_back(a);
        }
        else {
            l.push_back(a);
        }
    }
    if(n%2==1)swap(r,l);
    reverse(all(l));
    for (int j = 0; j < l.size(); ++j) {
        cout <<l[j]<<" ";
    }
    for (int i = 0; i < r.size(); ++i) {
        cout <<r[i]<<" ";
    }
    cout <<endl;
}



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

using namespace std;

long d[111][111111];
int main() {
	int n, w;
	cin >> n >> w;
	fill(d[0] + 1, d[0] + 111111, 1e18);
	for (int i = 0; i < n; i++) {
		int u, v;
		cin >> u >> v;
		for (int j = 0; j <= 100000; j++)
			d[i + 1][j] = min(d[i][j], j < v ? (long)1e18 : d[i][j - v] + u);
	}
	long r = 1e5;
	while (d[n][r] > w) r--;
	cout << r << endl;
	return 0;
}


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

int main(){
    string s;
    cin >> s;
    bool flag = true;
    for (int i = 0; i < s.size(); i++){
        if (i % 2){
            if (s[i] == 'R') flag = false;
        }
        else {
            if (s[i] == 'L') flag = false;
        }
    }
    if (flag) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 256