akhauriyash/Code-Regression huggingface dataset:
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import Data.List
-- import qualified Data.Vector.Unboxed as VU
-- import qualified Data.Vector.Unboxed.Mutable as VUM
-- import Control.Monad
-- import Control.Monad.ST
-- import Debug.Trace
trace _ = id

solve :: Int -> [Int] -> Int
solve n as = answer
  where
  posLst :: [Int]
  posLst = trace ("posLst " ++ show posLst0) posLst0
  posLst0 = costLst as
  negLst :: [Int]
  negLst = trace ("negLst " ++ show negLst0) negLst0
  negLst0 = reverse $ costLst $ reverse as
  answer = minimum $ zipWith3 f posLst negLst [0,1..]
    where f p n w = 2 * (p + n) + w

costLst :: [Int] -> [Int]
costLst xs = scanr (+) 0 $ snd $ mapAccumR f initAcc $
                zip xs (tail xs ++ [big])
  where
    initAcc :: [(Int, Int)]
    initAcc = [(0, big)]
    f :: [(Int,Int)] -> (Int,Int) -> ([(Int,Int)], Int)
    f steps@((len, gap):rem) (x, xNext)
      | 4 * x <= xNext = ((1, costF xNext x):steps, 0)
      | x <= xNext = ((len+1, gap):rem, 0)
      | otherwise = g (costC x xNext) 0 1 steps
      where
        g :: Int -> Int -> Int -> [(Int,Int)] -> ([(Int,Int)], Int)
        g !h !cost !len ((tLen,gap):rem)
          | h <= gap = ((newLen, gap-h):rem, newCost)
          | otherwise = g (h-gap) newCost newLen rem
          where newCost = cost + h*tLen
                newLen = len + tLen

big = 10 ^ 10

epsilon = 2 ** (-64)

log4 :: Int -> Double
log4 x = logBase 4 (fromIntegral x)

costC x y = ceiling (log4 x - log4 y - epsilon)
costF x y = floor (log4 x - log4 y + epsilon)

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

tmain :: B.ByteString -> Int
tmain cont =
  let remLines0 = map B.words (B.lines cont)
      [bs_n]:remLines1 = remLines0
      n = readBInt bs_n
      line2:remLines2 = remLines1
      as = map readBInt line2
  in solve n as

outAnswer :: Int -> IO ()
outAnswer = putStrLn . show

main :: IO ()
main = outAnswer . tmain =<< B.getContents


Measured memory_bytes: 216444
#include <stdio.h>

int calMax(int Senbei[],int R,int C);

int main(){

	int i,j;
	int R,C;	//1<=R<=10,1<=C<=10000;
	int Senbei[10001];
	int S;
	int max;
	
	while(1){
		scanf("%d %d\n",&R,&C);
		if(R==0 && C==0)break;
		for(i=0;i<10001;i++)Senbei[i]=0;
		
		for(j=0;j<R;j++){
		for(i=0;i<C;i++){
			scanf("%d",&S);
			Senbei[i]=Senbei[i]|(S*(0x1<<j));
		}
		}
		
		max=calMax(Senbei,R,C);
		
		printf("%d\n",max);
	}
	return 0;
}



int calMax(int Senbei[],int R,int C){
	int max=0;
	int cnt,cntsum;

	int i,j;
	int mask;
	int calSenbei[10001];
	
	for(mask=0;mask<(0x1<<R);mask++){
		for(i=0;i<C;i++){
			calSenbei[i]=mask^Senbei[i];
		}
		cntsum=0;
		for(i=0;i<C;i++){
			cnt=0;
			for(j=0;j<R;j++){
				cnt+=(0x1& (calSenbei[i]>>j));
			}
			if(cnt<R-cnt)cnt=R-cnt;
			cntsum+=cnt;
		}
		if(cntsum>max)max=cntsum;
	}
	return max;
}

Measured memory_bytes: 676
#include<stdio.h>

#include<string.h>

#define LEN 100005

int min(int a, int b) {return a < b ? a : b;}



typedef struct pp{

  char name[100];

  int t;

}P;



P Q[LEN+1];

int head, tail, n;



void enqueue(P x){

  Q[tail] = x;

  if ( tail == LEN ) tail = 1;

  else tail++;

}



P dequeue(){

  P x = Q[head];

  if ( head == LEN ) head = 1;

  else head++;

  return x;

}



int main(){

  int elaps = 0, c, i, q;

  P u;

  scanf("%d %d", &n, &q);

  for ( i = 1; i <= n; i++) scanf("%s %d", Q[i].name, &Q[i].t);



  head = 1; tail = n+1;



  while( head != tail ){

    u = dequeue();

    c = min(q, u.t);

    u.t -= c;

    elaps += c;

    if ( u.t > 0 ) enqueue(u);

    else {

      printf("%s %d\n",u.name, elaps);

    }

  }



  return 0;

}

Measured memory_bytes: 10756
// User: el_heffeh
// Date: 6/16/20

#include <bits/stdc++.h>

using namespace std;

int x, n, ans, diff;

void solve() {
	cin >> x >> n;
	vector<int> a(n, 0);
	set<int> st;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		st.insert(a[i]);
	}
	ans = diff = 2147483647;
	for (int i = x; i < 1000; ++i) {
		if (st.find(i) == st.end()) {
			ans = i, diff = i - x;
			break;
		}
	}
	for (int i = x - 1; i > -1000; --i) {
		if (x - i > diff) {
			break;
		}
		if (st.find(i) == st.end()) {
			ans = i;
			diff = x - i;
		}
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);    
    //freopen("input.txt", "r", stdin);
	solve();
	return 0;
}

/* 
	When writing code, be sure to look out for:
 	* Array out of bounds access
 	* Integer overflow
 	* Variable scopes and names
 	* Be proactive and make sure to completely think a whole problem through before writing any code
 	Coding is a fun activity, and it should be treated that way. Don't get mad over some failed test cases or time limit exceeded verdicts. Keep your cool and stay calm!
*/


Measured memory_bytes: 3660
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vs vector<string>
#define all(v) (v).begin(),(v).end()
#define ll long long
#define ull unsigned long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define ld long double
#define mod 10000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int main() 
{ 
    fastio
    int n;
    cin>>n;

    map<int, ll> imap;
    vector<int> vec(n);
    for(int i = 0; i<n; ++i)
    {
        cin>>vec[i];
        imap[vec[i]]++;
    }

    ll ans = 0;
    for(auto& it : imap)
        ans += it.second*(it.second-1)/2;
    
    for(int i = 0; i<n; ++i)
    {
        ll cur = ans;
        ll cur2 = imap[vec[i]];

        cur -= (cur2*(cur2-1))/2;
        ll temp = ((cur2-1)*(cur2-2))/2;
        cur += max((ll)0, temp);
        cout<<cur<<endl;
    }
} 

Measured memory_bytes: 10368
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=200005,mod=998244353;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int qidian;
int huan;
int a[max_n];
int ci[max_n];
int bu;
int count1;
int ans[max_n];
vector<int>v;
int main()
{
	int n;ll k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	int dang=1;ci[dang]=1;
	while(1)
	{
		dang=a[dang];
		count1++;
		ans[count1]=dang;
		if(ci[dang]==0)ci[dang]=1;
		else
		{
			qidian=dang;
			ci[dang]++;
			if(bu==0)bu=count1;
		}
		if(qidian)
		{
			huan++;
			v.pb(dang);
		}
		if(ci[dang]==3)
		{
			break;
		}
	}
	if(k<bu)
	{
		cout<<ans[k];
	}
	else
	{
		huan--;
	k-=(bu-huan);
	cout<<v[k%huan];
	}
	
}

Measured memory_bytes: 6564
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=998244353;
const int MAXN=500010, LOG=20;

ll n, m, k, u, v, x, y, t, a, b, ans;
ll F[MAXN], I[MAXN];

ll powmod(ll a, ll b){
	ll res=1;
	for (; b; b>>=1, a=a*a%mod) if (b&1) res=res*a%mod;
	return res;
}

ll nCr(ll n, ll r){
	if (r<0 || r>n) return 0;
	return F[n]*I[r]%mod*I[n-r]%mod;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	F[0]=1;
	for (int i=1; i<MAXN; i++) F[i]=F[i-1]*i%mod;
	I[MAXN-1]=powmod(F[MAXN-1], mod-2);
	for (int i=MAXN-1; i; i--) I[i-1]=I[i]*i%mod;
	cin>>n>>m;
	for (int i=0; i<=min(n, m); i++){
		ll tmp=nCr(n, i)*nCr(m, i)%mod*F[i]%mod;
		tmp=tmp*powmod(n+1, m-i)%mod*powmod(m+1, n-i)%mod;
		if (i&1) tmp*=-1;
		ans=(ans + tmp)%mod;
	}
	if (ans<0) ans+=mod;
	cout<<ans<<"\n";
	
	return 0;
}


Measured memory_bytes: 11448
#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)

//block linked list
#define Bsize 900 /*2sqrt(n)*/
typedef struct block{
	int cnt;
	ll a[Bsize+1];
	struct block*pre;
	struct block*nxt;
}block;
void Bpush(block*b,ll x){
	//リストにxを追加する
	if((*b).nxt&&(*(*b).nxt).a[0]<x){
		//次へ
		Bpush((*b).nxt,x);
		return;
	}
	int i=(*b).cnt;
	for(;i>0&&(*b).a[i-1]>x;i--)(*b).a[i]=(*b).a[i-1];
	(*b).a[i]=x;
	(*b).cnt++;
	if((*b).cnt>=Bsize){
		//分割
		block*nn=(block*)malloc(sizeof(block));
		(*nn).cnt=Bsize-Bsize/2;
		rep(i,0,Bsize-Bsize/2)(*nn).a[i]=(*b).a[i+Bsize/2];
		(*nn).pre=b;
		(*nn).nxt=(*b).nxt;
		if((*b).nxt)(*(*b).nxt).pre=nn;
		(*b).cnt=Bsize/2;
		(*b).nxt=nn;
	}
}
ll Bans(block*b,int k){
	//リストのk番目を返す
	//リストにk個未満しかなければ適当に返す
	if((*b).cnt<=k){
		if((*b).nxt)return Bans((*b).nxt,k-(*b).cnt);
		else return -1;
	}
	return (*b).a[k];
}
int Bdel(block*b,int k){
	//リストからkを(存在すれば)1個削除する
	//存在すれば1、しなければ0を返す
	if((*b).cnt==0||(*b).a[(*b).cnt-1]<k){
		if((*b).nxt)return Bdel((*b).nxt,k);
		else return 0;
	}
	int i=0;
	for(;(*b).a[i]<k;i++);
	if((*b).a[i]!=k)return 0;
	for(;i<(*b).cnt-1;i++)(*b).a[i]=(*b).a[i+1];
	(*b).cnt--;
	
	if((*b).cnt==0&&(*b).pre){
		(*(*b).pre).nxt=(*b).nxt;
		if((*b).nxt)(*(*b).nxt).pre=(*b).pre;
		free(b);
	}else if((*b).pre&&(*(*b).pre).cnt+(*b).cnt<Bsize/4){
		//まとめる
		rep(i,0,(*b).cnt)(*(*b).pre).a[(*(*b).pre).cnt+i]=(*b).a[i];
		(*(*b).pre).cnt+=(*b).cnt;
		(*(*b).pre).nxt=(*b).nxt;
		if((*b).nxt)(*(*b).nxt).pre=(*b).pre;
		free(b);
	}else if((*b).nxt&&(*(*b).nxt).cnt+(*b).cnt<Bsize/4){
		//まとめる
		block*p=b,*q=(*b).nxt;
		rep(i,0,(*q).cnt)(*p).a[(*p).cnt+i]=(*q).a[i];
		(*p).cnt+=(*q).cnt;
		(*p).nxt=(*q).nxt;
		if((*q).nxt)(*(*q).nxt).pre=p;
		free(q);
	}
	return 1;
}


ll a[400010],x[200010];
int c(const void*p,const void*q){
	if(a[*(ll*)p]<a[*(ll*)q])return -1;
	return 1;
}
block b;
ll idx[400010];
int main(){	
	ll n,q;
	scanf("%lld%lld",&n,&q);
	rep(i,0,n){
		ll s,t;
		scanf("%lld%lld%lld",&s,&t,x+i);
		a[2*i  ]=s-x[i];
		a[2*i+1]=t-x[i];
	}
	rep(i,0,2*n)idx[i]=i;
	qsort(idx,2*n,sizeof(ll),c);
	
	ll crr=0;
	rep(_,0,q){
		ll d;
		scanf("%lld",&d);
		while(crr<2*n&&a[idx[crr]]<=d){
			if(idx[crr]%2)Bdel(&b,x[idx[crr]/2]);
			else Bpush(&b,x[idx[crr]/2]);
			crr++;
		}
		printf("%lld\n",Bans(&b,0));
	}
}


Measured memory_bytes: 11132
# 全部試す
# 1つあたりO(M)
# O(M^2) = 10^6くらい
import numpy as np
from scipy.sparse.csgraph import dijkstra
N,M = map(int,input().split())
edge = [[int(x) for x in input().split()] for _ in range(M)]

graph = [[0]*(N+1) for _ in range(N+1)]
for a,b in edge:
  graph[a][b] = 1

answer = 0
for a,b in edge:
  graph[a][b] = np.inf
  dist = dijkstra(graph, indices = a, directed = False)
  if dist[b] == np.inf:
    answer += 1
  graph[a][b] = 1

print(answer)

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

#define pb insert
#define eb emplace_back
#define ff first
#define ss second
typedef long long int ll;
typedef long double ld;

#define all(a) a.begin(), a.end()
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;
typedef map<pp, ll> ip;
typedef map<pp, char> ic;
typedef map<ll, ll> ii;
typedef set<ll> sl;
typedef set<pair<ll, ll>> sp;
const ll mod = 1e9 + 7;

//main code
ll fast_expo(ll x, ll y, ll m)
{
	//x %= m;
	ll res = 1;
	while (y > 0)
	{
		if (y & 1)
		{
			res = ((res % m) *(x % m)) % m;
		}

		x = ((x % m) *(x % m)) % m;
		y >>= 1;
	}

	return res;
}

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

	ll x, y;
	cin >> x >> y;
	vl dp(y + 1, 0);
	ll ans = 0;
	for (ll i = y; i > 0; i--)
	{
		dp[i] = fast_expo((y / i), x, mod);
		for (ll j = i + i; j <= y; j += i)
		{
			dp[i] = ((dp[i]%mod - dp[j]%mod+mod)%mod);
			
		}
		

		ans = ((ans % mod) + ((dp[i] % mod) *(i % mod)) % mod) % mod;
	}
  
	cout << ans << "\n";
	return 0;
}

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

class Union_Find{
private:
  vector<int> par; vector<int> rank; vector<int> num;
public:
  Union_Find(int n);
  int number(int x); int find(int x); void unite(int x, int y); bool same(int x, int y);
};

Union_Find::Union_Find(int n){
  par = vector<int>(n); rank = vector<int>(n); num = vector<int>(n);
  rep(i,n) par[i] = i, rank[i] = 0, num[i] = 1; 
}

int Union_Find::find(int x){
  if(par[x] == x) return x;
  else  return par[x] = find(par[x]);
}

int Union_Find::number(int x){
  return num[find(x)];
}

void Union_Find::unite(int x, int y){
  x = find(x); y = find(y);
  if (x == y) return;
  if (rank[x] < rank[y]) swap(x, y);
  if (rank[x] == rank[y]) rank[x]++;
  par[y] = x;
  num[x] += num[y];
}

bool Union_Find::same(int x, int y){
  return find(x) == find(y);
}


int main(){
  int n, m;
  cin >> n >> m;
  Union_Find uf(n);
  map<int,vector<int>> mp;
  rep(i,n){
    int k;
    cin >> k;
    rep(j,k) {
      int l;
      cin >> l; l--;
      mp[l].push_back(i);
    }
  }
  for(auto x : mp){
    rep(j,x.second.size()) {
      uf.unite(x.second[0],x.second[j]);
    }
  }
  cout << (uf.number(0) == n ? "YES" : "NO") << endl;
  return 0;
}

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

int main() {
  int N , A , B ;
  cin >> N >> A >> B ;

  if ((N * A) > B) {
    cout << B << endl;
  }
  else if ((N * A) < B) {
    cout << N * A << endl;
  }
  else {
    cout << B << endl;
  }
}


Measured memory_bytes: 256
import sys
input = lambda: sys.stdin.readline().rstrip()

#MOD
P = 10**9 + 7
N = 200001 #使う最大値+1以上にする、値に注意3*10^5とかにしとくと安心
inv = [0] + [1] # 1/x
finv = [1] + [1] # 1/x!
fac = [1] + [1] # x!
for i in range(2, N):
  inv += [inv[P % i] * (P - int(P / i)) % P]
  fac += [(fac[i-1] * i) % P]
  finv += [(finv[i-1] * inv[i]) % P]

def comb(a, b): return (fac[a] * ((finv[b] * finv[a-b]) % P)) %P

#全方位木dp
#-------------------------------------------------------------------
#扱うクラス
class T:
    num = 1
    child = 0

    def __init__(self, a, b):
        self.num = a
        self.child = b

#零元
zero = T(1, 0)
#一個上に遷移した時の変化
def step(x):
    return T(x.num, x.child+1)
#マージ
def merge(x, y):
    return T((x.num*y.num*comb(x.child+y.child, x.child))%P, x.child+y.child)

G = [[] for _ in range(N)]
down_data = [zero] * N
dat = [zero] * N
rev_data = [zero] * N

class p:
    num = -1
    parent = -1

    def __init__(self, x, y):
        self.num = x
        self.parent = y
node_list = []
def makeNodeList(root):
    #dfsでの探索順の作成
    stack = []
    stack.append(p(root, -1))
    while len(stack) != 0:
        now = stack.pop()
        node_list.append(now)
        for to in G[now.num]:
            if to == now.parent: continue
            stack.append(p(to, now.num))
    node_list.reverse()

def dfs(root):
    for now in node_list:
        for to in G[now.num]:
            if to == now.parent: continue
            down_data[now.num] = merge(down_data[now.num], step(down_data[to]))

def dfs_rev(root):
    for now in node_list:
        #累積
        rev = rev_data[now.num]
        left = []
        right = []
        cnt = 0
        for to in G[now.num]:
            if to==now.parent: continue
            if len(left)==0: left.append(step(down_data[to]))
            else: left.append(merge(left[cnt-1], step(down_data[to])))
            cnt += 1
        cnt = 0
        for to in reversed(G[now.num]):
            if to==now.parent: continue
            if len(right)==0: right.append(step(down_data[to]))
            else: right.append(merge(right[cnt-1], step(down_data[to])))
            cnt += 1
        dat[now.num] = merge(rev, down_data[now.num])
        cnt = 0
        for to in G[now.num]:
            if to==now.parent: continue
            x = zero if cnt==0 else left[cnt-1]
            y = zero if cnt==len(right)-1 else right[len(right)-2-cnt]
            rev_data[to] = step(merge(rev, merge(x, y)))
            cnt += 1

def calc(data_num):
    root = -1
    for i in range(1, data_num+1):
        if len(G[i]) == 1:
            root = i
            break
    makeNodeList(root)
    dfs(root)
    node_list.reverse()
    dfs_rev(root)
#-------------------------------------------------------------------
def main():
    n = int(input())
    for i in range(n-1):
        a, b = map(int, input().split())
        G[a].append(b)
        G[b].append(a)
    calc(n)

    for i in range(1, n+1): print(dat[i].num)

if __name__ == "__main__": main()


Measured memory_bytes: 179308
// template version 1.6
// {{{ define basic macro
using namespace std;
#include <iostream>
#include <bits/stdc++.h>
// varibable settings
#define int long long
#define INF 1000000000000000000LL
// #define INF 2147483647
#define MOD 1000000007LL
// #define MOD 998244353LL
#define infile "../test/sample-1.in"

#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)(b-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ten(n) ((int)1e##n)
template<class T, size_t N> size_t sza(const T (&array)[N]) { return N; }
template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }
template<class T=int> T in() {T x; cin>>x; return (x);}
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

// dump macro
#ifdef PCM
  #include "dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
#define fi first
#define se second

// }}}

int solve(){
  int n,k;cin>>n>>k;
  int h[n]; rep(i, n) {cin>>h[i];}
  sort(h, h+n);

  int ans = INF;
  set<int> s;
  rep(i, k){
    s.insert(h[i]);
  }
  int M = *s.rbegin();
  int m = *s.begin();
  // dump(M-m);
  chmin(ans, M-m);

  rep(i, k, n){
    s.erase(h[i-k]);
    s.insert(h[i]);
    int M = *s.rbegin();
    int m = *s.begin();
    chmin(ans, M-m);
  }

  cout << ans << endl;

  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}


Measured memory_bytes: 4992
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <queue>
#include <map>
#include <set>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2>& p) { os << p.first << " " << p.second; return os; }
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) { for(int i = 0; i < (int) v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; }

void solve()
{
    int n; cin >> n;
    map<char, ll> mp;
    const string key = "MARCH";
    for(int i = 0; i < n; i++)
    {
        string s; cin >> s;
        int pos = key.find(s.front());
        if(pos == -1) continue;
        mp[s.front()]++;
    }

    ll ans = 0;
    for(int bit = 0; bit < (1<<(int)key.size()); bit++)
    {
        if(__builtin_popcount(bit) != 3) continue;
        ll cnt = 1;
        for(int i = 0; i < (int)key.size(); i++)
        {
            if(bit & (1<<i)) cnt *= mp[key[i]];
        }
        ans += cnt;
    }
    cout << ans << endl;
}

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

#define ULL unsigned long long

#define MOD 1000000007
#define MAX 10000000

ULL RepSq(ULL n, ULL m, ULL mod);
void nCk_init(int mod);
ULL nCk(int n, int k, int mod);

ULL fac[MAX], finv[MAX], inv[MAX];

int main() {
	nCk_init(MOD);

	int K, a, iCnt = 0;
	scanf("%d", &K);
	
	getchar();
	while ((a = getchar()) != '\n')
		iCnt++;
	ULL ans = 0;

	for (int i = 0; i <= K; i++) {
		//inserted place patterns
		ULL tmpChairs4OtherLetters = iCnt + K - i - 1;
		ULL insPlacePatterns = nCk((int)tmpChairs4OtherLetters, iCnt - 1, MOD);
		
		//inserted letter patterns
		ULL insLetsBeforeLastLet = K - i;
		ULL insLetPatterns = RepSq((ULL)26, (ULL)i, (ULL)MOD);
		insLetPatterns *= RepSq((ULL)25, insLetsBeforeLastLet, (ULL)MOD);
		insLetPatterns %= MOD;

		ans += (insPlacePatterns * insLetPatterns % MOD);
		ans %= MOD;
	}
	
	printf("%lld\n", ans);

	return 0;
}

ULL RepSq(ULL x, ULL n, ULL mod){
	if (n == 0)	return 1;
	
	ULL ret = 1;
	while (0 < n) {
		if (n & 1)
			ret = ret * x % mod;
		x = x * x % mod;
		n >>= 1;
	}
	return ret;
}

void nCk_init(int mod) {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++){
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}

ULL nCk(int n, int k, int mod){
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	//Fermat's little theorem
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}



Measured memory_bytes: 236076
#pragma GCC optimize("Ofast")   //Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>

using namespace std;

#define  fastio         ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(10);
#define  randomINIT     mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define  foo(i,a,n)     for(ll i = (a); i <= n; i++)
#define  frr(i,a,n)     for(ll i = (a); i >= n; i--)
#define  fo(i,n)        for(ll i = 0; i < n; i++)
#define  all(x)         (x).begin(),(x).end()
#define  mset(x,val)    memset(x,val,sizeof(x))
#define  newl           cout<<"\n"
#define  pb             push_back
#define  mp             make_pair
#define  s              second
#define  f              first
#define  dline          cerr<<"///REACHED///\n";
#define  deb1(x)        cerr<<#x<<" = "<<x<<'\n';
#define  deb2(x,y)      cerr<<'['<<#x<<','<<#y<<"] = "<<'['<<x<<','<<y<<']'<<'\n';
#define  deb3(x,y,z)    cerr<<'['<<#x<<','<<#y<<','<<#z<<"] = "<<'['<<x<<','<<y<<','<<z<<']'<<'\n';

typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   MOD   =  1e+9+7;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   MAXN  =  1e+5+7;

vll adj[MAXN];ll visit[MAXN]={};
int dx8[]={0,1,1,1,0,-1,-1,-1}, dy8[]={1,1,0,-1,-1,-1,0,1};
int dx4[]={0,1,0,-1}, dy4[]={1,0,-1,0};

//<<-----Declare Variable Here------->>//
int t=1;
ll n;
ll a,b;
//<<-----Implement Functions Here---->>//


//<<-----Start of Main--------------->>//
void MAIN(){
		cin>>a>>b;
		cout<<a*b<<endl;
}	

int main(){
	fastio;randomINIT;

	//cin>>t;
	while(t--){
		MAIN();
	}

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
    #endif
}

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

using namespace std;

int main()
{
    int n, t, i, a, b, mincost=10000;
    vector<pair<int, int> > ct;

    cin>>n>>t;

    for(i=0;i<n;i++)
    {
        cin>>a>>b;
        ct.push_back(make_pair(a, b));
    }

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

    for(i=0;i<n;i++)
    {
        if(ct[i].second<=t && ct[i].first<mincost)
        {
            mincost=ct[i].first;
        }

    }

    if(mincost!=10000)
        cout<<mincost<<endl;
    else
        cout<<"TLE"<<endl;

    return 0;
}


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

const int MAX = 1e6 + 10;
const int MOD = 1e9 + 7;

long long f[MAX], finv[MAX], inv[MAX];

void CombInit() {
  f[0] = f[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    f[i] = f[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long Perm(int n, int k) {
  if (n < k || k < 0) return 0;
  return f[n] * finv[n - k] % MOD;
}

long long Comb(int n, int k) {
  if (n < k || k < 0) return 0;
  return f[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

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

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

int main(){
  CombInit();
  int N, K; cin >> N >> K;
  vector<int> A(N); rep(i, N) cin >> A[i];
  sort(A.begin(), A.end());

  mint maxX, minX;
  rep(i, N) {
    maxX += (mint)A[i] * Comb(i, K - 1);
    minX += (mint)A[N - 1 - i] * Comb(i, K - 1);
  }

  cout << (maxX - minX).x << endl;
}


Measured memory_bytes: 24064
n, k = map(int, input().split())

r, s, p = map(int, input().split())

t = list(input())
ans = 0
for i in range(k):
    A = []
    j = i
    l = 0
    while(k*l+j < n):
        A.append(t[k * l + j])
        l += 1
    # print(A)
    begin = 0
    A.append('o')
    while begin < len(A)-1:
        if A[begin] == A[begin + 1]:
            if A[begin] == 'r':
                ans += p
            elif A[begin] == 's':
                ans += r
            else:
                ans += s
            begin += 2
        else:
            if A[begin] == 'r':
                ans += p
            elif A[begin] == 's':
                ans += r
            else:
                ans += s
            begin += 1
print(ans)

Measured memory_bytes: 49904
#include<stdio.h>
#include<stdlib.h>
int main(){
	int n,distinct=0;
	scanf("%d",&n);
	char c,nl,prev;scanf("%c",&nl);scanf("%c",&prev);distinct=1;
	for(int i=0;i<n-1;i++){
		scanf("%c",&c);
		if(c!=prev){
			distinct+=1;
			prev = c;
		}
	}

	printf("%d\n",distinct );
}



Measured memory_bytes: 128
#include<iostream>
#include<string>
#include<algorithm>	
#include<cmath>
using namespace std;
int main() {
	long long a, b; 
	while (true) {
		cin >> a >> b;
		if (a == 0 && b == 0) return 0;
		for (int h = 1; h <= a; h++) {
			for (int i = 1; i <= b; i++) {
				cout << '#';
			}
			cout << endl;
		}
		cout << endl;
	}
	cin >> a;
}


Measured memory_bytes: 3096
//https://atcoder.jp/contests/abc139/tasks/abc139_b 

#include<bits/stdc++.h>

using namespace std;

int main(){
	int a,b;
	cin >> a >> b;
	/*int x;
	if(b%a==0){
		x = b/a;
	}
	else{
		x = b/a + 1;
	}
	cout << x << endl;*/
	int ans = 0;
	int c = 1;
	while (c < b){
		c += a -1;
		ans++;
	}

	cout << ans << endl;

}


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

long long fact(long long n) {
  long long ans = 1;
  for (int i = 2; i < n + 1; i++) {
    ans *= i % 1000000007;
    ans %= 1000000007;
  }
  return ans;
}

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main() {
  int mod = 1000000007;
  int S;
  cin >> S;
  long long count = 0;
  for (int n = 1; S >= 3 * n; n++) {
    count += fact(S - 2 * n - 1) * modinv(fact(S - 3 * n) * fact(n - 1), mod);
    count %= mod;
  }
  count = (count + mod) % mod;
  cout << count;
}


Measured memory_bytes: 3600
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#define bye return 0
#define speedOne ios::sync_with_stdio(false)
#define speedtwo cin.tie(0)
typedef long long int lli;
typedef long long ll;
using namespace std;
int main()
{
    speedOne;
    speedtwo;
    string num;
    int sum = 0;
    cin >> num;
    for(int i =0; i<num.size(); ++i){
        int toNum = num[i] - '0';
        sum = sum+toNum;
    }
    if(sum%9==0) cout << "Yes" << endl;
    else cout << "No" << endl;
	bye;
}

Measured memory_bytes: 3776
#include <bits/stdc++.h>
#define SORT(a) sort(a.begin(), a.end())
#define RSORT(a) sort(a.rbegin(), a.rend())
#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n; 0 <= i; --i)
#define FOR(i, start, end) for (int i = start; i < end; ++i)
#define RFOR(i, start, end) for (int i = start; end <= i; --i)
#define ALL(a) a.begin(), a.end()
#define MOD(a) a %= 1'000'000'007
#define INF32 1'050'000'000
#define INF64 4'000'000'000'000'000'000
using ll = long long;
using namespace std;
template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }

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

    int N; cin >> N;
    int sum = 0;
    int A[N], X[N]; REP(i, N) {
        cin >> A[i];
        sum += A[i];
    }
    X[0] = sum;
    for (int i = 1; i<N-1; i += 2) {
        X[0] -= 2 * A[i];
    }

    FOR (i, 0, N-1) {
        X[i+1] = 2 * A[i] - X[i];
    }
    REP (i, N) cout << X[i] << ' ';
    cout << '\n';
    return 0;
}




Measured memory_bytes: 2176
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <iterator>
#include <cmath>
#include <set>
#include <deque> 
#include <string>


using namespace std;

struct Node
{
	std::vector<int> connects;
	int parent;
	std::vector<int> next;
	int depth;
	int maxDepth;
	int pos;
	
	bool isUsed;
};
Node nodes[100010];

int n;
int u, v;
int root;

void makeTree(int start)
{
	int curNode;
	std::queue<int> q;
	q.push(start);
	
	while (!q.empty()) {
		curNode = q.front();
		q.pop();
		nodes[curNode].isUsed = true;
		
		for (int i = 0 ; i < nodes[curNode].connects.size() ; i++) {
			int newNode = nodes[curNode].connects[i];
			if (true == nodes[newNode].isUsed)
				continue;
			if (newNode == nodes[curNode].parent)	//不回头 
				continue;
			nodes[curNode].next.push_back(newNode);
			nodes[newNode].parent = curNode;
			nodes[newNode].depth = nodes[curNode].depth + 1;
			q.push(newNode);
			nodes[newNode].isUsed = true;
		}
	}
}

int main()
{
	scanf("%d%d%d", &n, &u, &v);
	
	int a, b;
	for (int i = 0 ; i < n - 1 ; i++)
	{
		scanf("%d%d", &a, &b);
		nodes[a].connects.push_back(b);
		nodes[b].connects.push_back(a);
		nodes[a].maxDepth = -1;
		nodes[b].maxDepth = -1;		
		nodes[a].pos = a;
		nodes[b].pos = b;
		
		nodes[a].isUsed = false;
		nodes[b].isUsed = false;
	}
	
	root = v;
	nodes[root].parent = -1;
	nodes[root].depth = 1;
	makeTree(root);
	
	for (int i = 1 ; i <= n ; i++)
	{
		if (nodes[i].next.size() != 0)
			continue;
			
		int p = i;
		int curDepth = nodes[p].depth;
		while (p != -1) {
			if (nodes[p].maxDepth < curDepth)
				nodes[p].maxDepth = curDepth;
			else
				break;
			
			p = nodes[p].parent;
		}
	}
	
	//a追b逃 
	//b先行动 
	int pa = root;	//v
	int pb = u;
	
	std::vector<int> routeAToB;
	while (pb != pa) {
		routeAToB.push_back(pb);
		pb = nodes[pb].parent;
	}
	reverse(routeAToB.begin(), routeAToB.end());

	//第一阶段：两人相向 
	int count = 0;
	int pNextA = 0;
	pb = u;
	while ((nodes[pb].depth - nodes[pa].depth) > 2) {
		pb = nodes[pb].parent;
	
		pa = routeAToB[pNextA];
		count++;
		pNextA++;
	}
	
	//第二阶段：一逃一追 
	//第三阶段：走投无路 	
	int curABDist = nodes[pb].depth - nodes[pa].depth;
	int distLeft = nodes[pb].maxDepth - nodes[pb].depth;
	count = count + distLeft;
	
	if (curABDist == 2) {
		count = count + 1;
	}
	
	
	printf("%d\n", count);

	return 0;
}


Measured memory_bytes: 13440
#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define RREP(i,a,b) for(int i=(a);i>=(b);i--)
#define pq priority_queue
#define P pair<int,int>
#define P2 pair<int,P>
#define P3 pair<int,P2>
typedef long long ll; typedef long double ld;
using namespace std;
const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};
const int vx[]={1,0,-1,0}, vy[]={0,1,0,-1};
const ll LINF=1e18;
const ld PI=abs(acos(-1));

int n,m;
int li[100010], dp[100010];
vector<int> g[100010];

void color(int s, int d, int c){
	queue<P> qu; qu.push({s, d});
	
	while(!qu.empty()){
		auto p = qu.front(); qu.pop();
		if(!li[p.first]) li[p.first] = c;
		if(!p.second) continue; dp[p.first] = max(dp[p.first], p.second);
		
		for(auto e:g[p.first]){
			if(dp[e] and dp[e] >= p.second - 1) continue;
			qu.push({e, p.second - 1});
		}
	}
}

int a[100010] = {}, b[100010] = {}, c[100010] = {};

int main(){
	cin >> n >> m;
	REP(i,0,m){
		int u,v; cin >> u >> v;
		g[u - 1].push_back(v - 1);
		g[v - 1].push_back(u - 1);
	}
	int q; cin >> q;
	REP(i,0,q) cin >> a[i] >> b[i] >> c[i];
	reverse(a, a + q); reverse(b, b + q); reverse(c, c + q);
	
	REP(i,0,q) color(a[i] - 1, b[i], c[i]);
	REP(i,0,n) cout << li[i] << endl;
	return 0;
}


Measured memory_bytes: 8912
#include <cstdio>
#include <climits>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
#include <functional>
#include <tuple>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define FOR(x,xs) for(auto &x: xs)

using namespace std;
typedef long long ll;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;

int main() {
  ios::sync_with_stdio(false);
  int N, M, K;
  cin >> N >> M >> K;
  bool pos = false;
  REP(i,0,N+1) {
    REP(j,0,M+1) {
      if(i * (M - j) + j * (N - i) == K) {
        pos = true;
      }
    }
  }
  cout << (pos ? "Yes" : "No") << endl;
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
int R,C,s[10000][10],f[10],N,i,j,n,m;void rec(int k){if(k==R){for(n=i=0;i<C;i++,n+=m>R-m?m:R-m)for(m=j=0;j<R;j++)m+=s[i][j]^f[j];N=N>n?N:n;return;}f[k]=0;rec(k+1);f[k]=1;rec(k+1);}main(){for(;std::cin>>R>>C,R;rec(0),printf("%d\n",N))for(j=R;j-->0;)for(i=C;i-->0;)std::cin>>s[i][j];}

Measured memory_bytes: 1256
#include <bits/stdc++.h>
#define PREP(i, s, x) for(ll i = (s); i < (x); i++)
#define MREP(i, s, x) for(ll i = (s); i >= (x); i--)
#define REP(i, x) PREP(i, 0, x)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template<typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }

// variable -------------------------------------------

constexpr ll INF = (1ll << 59);
constexpr ll MOD = 1000000007ll;

// structure ------------------------------------------

// function -------------------------------------------

// main -----------------------------------------------

int main() {
	ll n;
	cin >> n;
	vector<ll> a(n);
	REP(i, n){
		cin >> a[i];
	}

	ll ans = 0;
	REP(i, n-1){
		if(a[i] <= a[i+1]){
			continue;
		}else{
			ans += a[i] - a[i+1];
			a[i+1] = a[i];
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 4756
# coding: utf-8
# Your code here!
 
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意
 
s = input()
l = len(s)
MOD = 10**9+7
 
dp = [0]*13
dp[0] = 1
for i in s:
    new = [0]*13
    if i == "?":
        for i in range(10):
            for j in range(13):
                new[(10*j+i)%13] += dp[j]
    else:
        i = int(i)
        for j in range(13):
            new[(i+10*j)%13] += dp[j]
    
    for i in range(13):
        new[i] %= MOD
    dp,new = new,dp
#    print(dp)
 
print(dp[5]%MOD)

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

int main(){
  int N;
  cin>>N;
  //osの配列
  vector<pair<int,int>> os(N);
  for(int i = 0;i<N;i++){
      int A,B;
      cin>>A>>B;
      os[i]=pair(A,B);
  }
  sort(os.begin(), os.end());
  reverse(os.begin(), os.end());
  
  cout << os[0].first + os[0].second << endl;
  
  
 
}





Measured memory_bytes: 4040
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <unordered_map>
#include <queue>
#include <cstring>

using namespace std;

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

    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }

    queue<int> q;
    sort(v.begin(), v.end(), greater());

    unsigned long long ans = 0;
    q.push(v[0]);
    for (int i = 1; i < n; i++) {
        q.push(v[i]);
        ans += q.front();
        if (v[i] < q.front()) {
            q.pop();
            q.push(v[i]);
        }
    }
    cout << ans;
}

Measured memory_bytes: 4820
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
int dy[] = {1, 0, 0, -1};
int dx[] = {0, 1, -1, 0};
int b[110][110];
int w, h;
int xs, ys, xg, yg;
int color;
bool flag = false;
bool used[110][110];

void dfs(int y, int x, int py, int px){
	rep(i, 4){
		int ny = y + dy[i], nx = x + dx[i];
		if(used[ny][nx]) continue;//???????????¨???????????????????????????
		if(!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
		if(b[ny][nx] != color) continue;
		if(ny == py && nx == px) continue;
		if(ny == yg && nx == xg){
			flag = true;
		}
		used[ny][nx] = true;
		dfs(ny, nx, y, x);
	}
	return;//????????¢?????¨???????????????
}

int main(void){
	while(1){
		rep(i, 110)rep(j, 110){
			b[i][j] = -1; used[i][j] = false;
		}
		cin >> w >> h;
		if(w == 0 && h == 0) return 0;
		cin >> xs >> ys >> xg >> yg;
		xs--; ys--; xg--; yg--;
		int n; cin >> n;
		rep(i, n){
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--; y--;
			if(d == 0){//?¨?
				b[y][x] = b[y][x + 1] = b[y][x + 2] = b[y][x + 3] = c;
				b[y + 1][x] = b[y + 1][x + 1] = b[y + 1][x + 2] = b[y + 1][x + 3] = c;
			}else{//???
				b[y][x] = b[y][x + 1] = c;
				b[y + 1][x] = b[y + 1][x + 1] = c;
				b[y + 2][x] = b[y + 2][x + 1] = c;
				b[y + 3][x] = b[y + 3][x + 1] = c;
			}
		}
		color = b[ys][xs];
		if(color == -1) {//?????????????????????????????????
			printf("NG\n"); continue;
		}
		used[ys][xs] = true; flag = false;
		dfs(ys, xs, -1, -1);
		if(flag) printf("OK\n");
		else printf("NG\n");
	}
}

Measured memory_bytes: 1176
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <cmath>
#include <string>
#include <sstream>
#include <queue> 
#include <list>
#include <stack>
#include <unordered_map>
#include <algorithm> 
#include <numeric>  
#include <functional>
#include <cassert>
#include <bitset>
#include <cctype>  
#include <iomanip> 
#include <limits>  
#include <regex>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt, less) : 
// if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*")) ) std::cout << "YES" << std::endl;
 
#define rep(i, n) for(int i = 0; i < (int)(n);    i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;
typedef std::pair<int,int> P;

ll gcd ( ll a, ll b ) { if ( b == 0 ) return a; else return gcd ( b, a%b ); }
ll lcm ( ll a, ll b ) { return a * b / gcd(a, b); }

int H, W;
std::vector<std::vector<char>> G;
std::array<P,4> dirs = { P(-1,0), P(1,0), P(0,-1), P(0,1) };

int solve(P start, P end) {
  std::vector<std::vector<int>> used(H);
  rep(i,H) used[i].assign(W, -1);
  std::queue<P> q;
  used[start.first][start.second] = 0;
  q.push(start);

  while(!q.empty()) {
    P cur = q.front();
    q.pop();
    
    for(auto&& dir : dirs) {
      P next;
      next.first = cur.first + dir.first;
      next.second = cur.second + dir.second;
      if ( next.first >= 0 && next.first < H && next.second >= 0 && next.second < W ) {
        if ( G[next.first][next.second] != '.' ) continue;
        if ( used[next.first][next.second] == -1 ) {
          used[next.first][next.second] = (used[cur.first][cur.second] + 1);
          q.push(next);
        }
      }
    }
  }
  return used[end.first][end.second];
}

int main (void) {
  std::cin.tie(0);  std::ios::sync_with_stdio(false);
  std::cin >> H >> W;
  G.resize(H); rep(i,H) G[i].resize(W);
  rep(i,H) rep(j,W) std::cin >> G[i][j];

  int ans = -1;
  rep(i,H) rep(j,W) rep(m,H) rep(n,W) {
    P s = {i,j};
    P e = {m,n};
    if ( G[i][j] == '#' || G[m][n] == '#') continue;
    ans = std::max(ans, solve(s,e));
  }
  std::cout << ans << std::endl;
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
using ll = long long;
#define int ll

signed main(){
    int n; cin >> n;
    vector<int>v(n);
    for(auto & e : v) cin >>e;
    // to adapt cosed range
    v.push_back(0LL);
    int ans = 0;
    while(true){
        if(*max_element(v.begin(), v.end()) == 0LL)break;
        int cnt = 0;
        for(int r = 0; r <= n; r++){
            if(v[r] == 0){
                // before reach r, all elements can be reduce
                ans += (cnt > 0);
                cnt = 0;
            } else {
                v[r]--;
                ++cnt;
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
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; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }


bool solve(string S) {
    string T = S;
    reverse(T.begin(), T.end());
    if (S != T) return false;

    int N = S.size();
    string S2 = S.substr(0, N/2);
    string T2 = S2;
    reverse(T2.begin(), T2.end());
    if (S2 != T2) return false;
    return true;
}

int main() {
    string S;
    while (cin >> S) {
        if (solve(S)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}

















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

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++) 
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = int(1e9) + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int N = 100 * 1000 + 13;

struct exam{
	int b, l, u;
	exam(){};
};

int n, x;
exam a[N];

bool read () {
	if (scanf("%d%d", &n, &x) != 2)
		return false;
	forn(i, n)
		scanf("%d%d%d", &a[i].b, &a[i].l, &a[i].u);
	return true;
}

li sum;
pair<li, int> val[N];
li pr[N];

pair<li, int> get(int x){
	int l = 0, r = n;
	while (l < r - 1){
		int m = (l + r) / 2;
		li tmp = pr[m];
		if (m - 1 < x)
			tmp += val[x].x;
		if (tmp >= sum)
			r = m;
		else
			l = m;
	}
	fore(i, l, r + 1){
		li tmp = pr[i];
		if (i - 1 < x)
			tmp += val[x].x;
		if (tmp >= sum)
			return mp(tmp - val[x].x, i - (i - 1 >= x));
	}
	return mp(INF64, INF64);
}

void solve() {
	sum = 0;
	forn(i, n)
		sum += a[i].b * li(a[i].l);
	
	if (sum == 0){
		puts("0");
		return;
	}
	
	forn(i, n)
		val[i] = mp(x * li(a[i].u) - a[i].b * li(a[i].u) + a[i].b * li(a[i].l), i);
	sort(val, val + n, greater<pair<li, int>>());
	pr[0] = 0;
	forn(i, n)
		pr[i + 1] = pr[i] + val[i].x;
	
	//cerr << vector<pair<li, int>>(val, val + n) << endl;
	//cerr << vector<li>(pr, pr + n + 1) << endl;
	//cerr << sum << endl;
	
	li ans = INF64;
	
	forn(i, n){
		auto tmp = get(i);
		li cur = tmp.x;
		int cnt = tmp.y;
		
		//cerr << tmp << " " << val[i].y << endl;
		
		if (cur == INF64)
			continue;
		int zi = val[i].y;
		
		li pr;
		if (cur + a[zi].b * li(a[zi].l) >= sum)
			pr = a[zi].l;
		else if (cur + val[i].x >= sum)
			pr = a[zi].u;
		else
			continue;
		
		int l = 0, r = x;
		while (l < r - 1){
			int m = (l + r) / 2;
			if (cur + m * li(pr) - a[zi].b * li(pr) + a[zi].b * li(a[zi].l) >= sum)
				r = m;
			else
				l = m;
		}
		
		if (cur + l * li(pr) - a[zi].b * li(pr) + a[zi].b * li(a[zi].l) >= sum)
			r = l;
		if (cur + r * li(pr) - a[zi].b * li(pr) + a[zi].b * li(a[zi].l) >= sum)
			ans = min(ans, cnt * li(x) + r);
	}
	
	printf("%lld\n", ans);
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	
	int tt = clock();
#endif
	
	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

#ifdef _DEBUG
	while(read()) {	
#else
	if(read()) {
#endif
		solve();
		
#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}

Measured memory_bytes: 3712