akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;

/*
<url:https://arc068.contest.atcoder.jp/tasks/arc068_b>
問題文============================================================
 すぬけくんはカードゲームで遊ぶことにしました。
 N 枚からなるカードの山があり、上から i 枚目のカードには整数 Ai が書かれています。
 
 すぬけくんはこのカードの山に対し 0 回以上、以下の操作を行い、
 残ったカードに書かれた値が互いに異なるようにしたいです。
 最大で何枚のカードを残すことが可能か求めなさい。
 なお、N は奇数であり、少なくとも 1 枚のカードを残すことが可能であることが保証されます。
 
 操作：カードの山から任意の 3 枚のカードを抜き出す。
 抜き出したカードのうち書かれた値が最大であるようなカード 1 枚と
 最小であるようなカード 1 枚の合計 2 枚を選んで食べる。その後残った 1 枚をカードの山に戻す。
=================================================================

解説=============================================================

 a,b,cをそれぞれ異なる数値(a<b<c)だとすると
 今回の問題で考えられるパターンは
 a,b,c => b
 a,a,b => a
 a,a,a => a
 
 といったパターンである。
 ここで数値が重複しているパターンについて考えると
 aに関していえば、 aが２個あるならば上のようにして、2個をそのまま用いることによって１個にできる
 また、３個以上ある場合は、すべて同じ値にすることによって最終的に２個または１個にできる。
 よって、結果的に２個同じ数字がある場合について考察を進めればよく、
 
 ２個同じ数字があるのであれば、１個別の数字を使って１個にすれば良いということになる。
 ここで、行動を最適にするために、用いるもう１個の数字をできるだけ同じく２個重複している数字を
 使っていけば良い
 
================================================================
*/
int main(void) {
	cin.tie(0); ios::sync_with_stdio(false);
    ll N; cin >> N;
    vector<ll> A(N);
    for(auto& in:A) cin >> in;
    const int MAX_N = 1e5+1;
    vector<ll> C(MAX_N,0);
    for(auto &v:A) C[v]++;
    sort(C.begin(),C.end(),greater<ll>());
    for(int i = 0; i < (int)C.size();i++){
        if(C[i] >= 3){
            if(C[i]%2 == 1) C[i] = 1;
            else C[i] = 2;
        }
    }
    sort(C.begin(),C.end(),greater<ll>());
    for(int i = 0; i < (int)C.size();i++)if(C[i] == 2){ C[i] = 1; C[i+1]--; }
    cout << accumulate(C.begin(),C.end(),0LL) << endl;
	return 0;
}


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

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define int3(x, y, z) Int(x); int2(y, z);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second

//int dxy[5] = {0, 1, 0, -1, 0};
// assign avl ncm dijkstra geo2 kruskal graph uf lca BIT
// matrix dinic next_combination topcoder lcm

//最大公約数
int gcd(int x, int y) { return y ? gcd(y, x%y) : x; }
//最小公倍数
int lcm(int x, int y) { return x / gcd(x, y) * y; }
//最大公約数 複数個
int gcd(const vector<int> &v) {
    int ret = v[0];
    for (int i = 1; i < v.size(); i++)
        ret = gcd(ret, v[i]);
    return ret;
}
//最小公倍数 複数個
int lcm(const vector<int> &v) {
    int ret = v[0];
    for (int i = 1; i < v.size(); i++)
        ret = lcm(ret, v[i]);
    return ret;
}

signed main()
{
    Int(n);
    int ans = 1;
    rep(i, n) {
        Int(tmp);
        ans = lcm(ans, tmp);
    }
    std::cout << ans << std::endl;



    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <utility>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <array>
#include <math.h>
#include <numeric>
#include <sstream>
#include <queue>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
int main(int argc, char const *argv[]) {
    string s;
    cin >> s;
    ll ans = (s.length()-1)*9+(s[0]-'0');
    for (int i = 1; i < s.length(); ++i) {
        if (s[i] != '9') {
            ans--;
            break;
        }
    }
    cout << ans << std::endl;
    return 0;
}

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

const ll m = 1e9+7;
ll sm[400005];
void sm_bulid() {
	sm[0]=1;for(ll i=1;i<400005;i++) sm[i]=sm[i-1]*i%m;
}
ll mod_pow(ll x, ll a) {
	ll an = 1;
	while(a > 0) {
		if (a&1) an = an * x % m;
		x = x * x % m; a >>= 1;
	}
	return an;
}
ll ncr0(ll n, ll r) {
  return sm[n] * mod_pow(sm[n-r], m-2) % m * mod_pow(sm[r], m-2) % m;
}
ll ncr(ll n, ll r) {
  // n*(n-1)*(n-2)*...(n-r+1) / r*(r-1)*...*1
  if (r>n/2) r=n-r;
  ll res=mod_pow(sm[r],m-2) % m;
  for(ll x=n,i=0;i<r;x--,i++) {
    res=res*x%m;
  }
  return res;
}

int main(){
	sm_bulid();
	// nC0+nC1+...+nCn - nC0: 不要分
  // 花束の作り方：a,b,n-a,n-b (n==a+bのとき、a,b)
	ll n,a,b; cin>>n>>a>>b;
	ll an=(mod_pow(2,n)-ncr(n,0)+m*10)%m;
  //an=(an-ncr(n,a)-ncr(n,b)+m)%m;
  //cout<<an<<endl;
  
  an=(an-ncr(n,a)+m)%m;
  //cout<<an<<endl;
  an=(an-ncr(n,b)+m)%m;

  cout<<an<<endl;

}

Measured memory_bytes: 3328
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;
using ll = long long;
using ull = unsigned long long;

void ABC141C()
{
	int N, K, Q;
	cin >> N >> K >> Q;
	vector<int> Score(N);
	for (int i = 0; i < Q; ++i)
	{
		int x;
		cin >> x;
		++Score[x-1];
	}

	int rem = Q - K;
	for (int i = 0; i < N; ++i)
	{
		if (Score[i] > rem)
		{
			cout << "Yes" << endl;
		}
		else
		{
			cout << "No" << endl;
		}
	}
}
int main()
{
	ABC141C();
	return 0;
}


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

#define INF ((1<<30)-1)  //int32_t
#define MOD 1000000007
#define MOD2 

#define all(a)  (a).begin(),(a).end()

#define mkp make_pair
#define pb push_back

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

int main(){

    bool flag=false;
    ll ans=0,sum=0;

    ll k,a,b;
    cin >>k>>a>>b;

    if(b-a > 2){
        
        k -= a-1;ans=a;
        ans += (b-a)*max(0LL,(k/2));
        if(k%2==1)++ans;

    }else{
        ans = k+1;
    }

    //cout <<fixed<<setprecision(16)<< << endl;
    cout << ans << endl;

    //if(flag)cout << "Yes" <<endl;
    //else cout << "No" <<endl;
    
    return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

int main()
{
  int a, b;
  int sum = 0, ave = 0, cnt = 0;
  while (scanf("%d,%d", &a, &b) != EOF){
    sum += a * b;
    ave += b;
    cnt++;
  }
  ave = ave * 10 / cnt;
  if (ave % 10 >= 5)  ave += 10 - ave % 10;
  cout << sum << "\n" << ave / 10 << endl;
}

Measured memory_bytes: 1208
D = gets.to_i
c = gets.split.map &:to_i
s = Array.new
D.times do
  s << gets.split.map(&:to_i)
end
# t = []
# D.times do
#   t << gets.to_i
# end
#
# last = Array.new(26){[0, false]}
#
#
#
# def cal_dec(c, last, day)
#   sum = 0
#   for i in 0..25
#     sum += c[i] * (day - last[i][0])
#   end
#   sum
# end
#
#
#
# point = 0
# for day in 1..D
#   point += s[day-1][t[day-1]-1]
#   last[t[day-1]-1][1] = true
#   last[t[day-1]-1][0] = day
#   point -= cal_dec(c, last, day)
#   puts point
# end
for i in 0..D-1
  max = s[i].max
  puts s[i].index(max) + 1
end


# D.times do |i|
#   puts (i+1) % 26 + 1
# end


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for(int i=0; i<(n); ++i)
#define FOR(i, a, b) for(int i=(a); i<(b); ++i)
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)

#define DEBUG(x) cout<<#x<<": "<<(x)<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";REP(i, v.size())cout<<' '<<v[i];cout<<'\n'
#define ALL(a) (a).begin(), (a).end()

template<typename T> inline void CHMAX(T& a, const T b) {if(a<b) a=b;}
template<typename T> inline void CHMIN(T& a, const T b) {if(a>b) a=b;}

constexpr ll MOD=1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

const double EPS=1e-9;
#define EQ0(x) (abs((x))<EPS)
#define EQ(a, b) (abs((a)-(b))<EPS)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10)<<fixed;
  string n;
  cin>>n;
  bool ok=false;
  REP(i, 3){
    if(n[i]=='7'){
      ok=true;
      break;
    }
  }
  cout<<(ok ? "Yes" : "No")<<'\n';
  return 0;
}


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

int main(){
  long long int a, b, k;
  cin >> a >> b >> k;
  if( a >= k )
    cout << a - k << " " << b << endl;
  else if( a + b >= k )
    cout << "0 " << (a + b) - k << endl;
  else
    cout << "0 0" << endl;
  return(0);
}

Measured memory_bytes: 3592
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 3.14159
#define mod 1000000007
int main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);
    ll n,k;cin>>n>>k;
    ll sum[n+1];
    sum[0]=0;
    for(int i=0;i<=n;++i)
    {
    	if(i)
    	{
    		sum[i]=(i+sum[i-1]+mod)%mod;
    	}
    	//cout<<sum[i]<<endl;
    }
    ll ans=0;
    for(int i=k;i<=n;++i)
    {
    	ans=(ans+sum[n]-sum[n-i]-sum[i-1]+1)%mod;
    	//cout<<sum[n]<<" "<<sum[n-i]<<" "<<sum[i-1]<<"\n";
    	//cout<<ans<<endl;
    }
    cout<<(ans+1+mod)%mod<<"\n";



    return 0;
}

Measured memory_bytes: 5172
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<vector>
using namespace std;

int main() {
	int n, t;
	cin >> n >> t;
	int c=1001;
	for (int i = 0; i < n; i++) {
		int cc, tt;
		cin >> cc >> tt;
		if (tt <= t)c = min(c, cc);
	}
	if (c == 1001)cout << "TLE" << endl;
	else cout << c << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define MAX_N 100001
#define INF_INT 2147483647
#define INF_LL 9223372036854775807
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
void init(int n);
int find(int n);
void unite(int x,int y);
bool same(int x, int y);
ll bpow(ll,ll,ll);
typedef vector<int> vec;
typedef vector<vec> mat;
mat mul(mat &A,mat &B);
mat pow(mat A,ll n);
int dx[9] = {-1,0,1,-1,0,1,-1,0,1};
int dy[9] = {-1,-1,-1,0,0,0,1,1,1};


bool cmp_P(const P &a,const P &b){
  return a.second < b.second;
}
int main()
{
  ll N,H,W,a,b,res[10]={0},t=0;
  map<P,bool> mp,visited;
  vector<P> v(0);
  cin >> H >> W >> N;
  REP(i,N){
    cin >> a >> b;
    mp[P(a,b)] = true;
    v.push_back(P(a,b));
  }
  REP(i,N){
    REP(d,9){
      int cnt = 0;
      int x=v[i].second+dx[d],y=v[i].first+dy[d];
      if(visited[P(y,x)] || y >= H || x >= W || x <= 1 || y <= 1)
        continue;
      visited[P(y,x)] = true;
      REP(j,9){
        cnt += mp[P(y+dy[j],x+dx[j])];
      }
      res[cnt]++;
    }
  }
  REP(i,10)t += res[i];
  t = (H-2)*(W-2)-t;
  res[0] = t;
  REP(i,10)cout << res[i] << endl;
  
  return 0;
}

int par[MAX_N];
int ranks[MAX_N];

//n要素で初期化
void init(int n){
  REP(i,n){
    par[i] = i;
    ranks[i] = 0;
  }

}

//木の根を求める
int find(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(x == y) return ;
  if(ranks[x] < ranks[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if(ranks[x] == ranks[y]) ranks[x]++;
  }
}

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

ll bpow(ll a, ll n,ll mod){
  int i = 0;
  ll res=1;
  while(n){
    if(n & 1)
      res = (res*a) % mod;
    a = (a*a) % mod;
    n >>= 1;
  }
  return res;
}
const int MOD = 1000000007;
mat mul(mat &A, mat &B){
  mat C(A.size(),vec(B[0].size()));
    REP(i,A.size())REP(k,B.size())REP(j,B[0].size()){
    C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
  }
  return C;
}

mat pow(mat A,ll n)
{
  mat B(A.size(),vec(A.size()));
  REP(i,A.size()){
    B[i][i] = 1;
  }
  while(n > 0){
    if ( n & 1) B = mul(B,A);
    A = mul(A,A);
    n >>= 1;
  }
  return B;
}


Measured memory_bytes: 214256
#include <string>
#include <string.h>
#include <vector>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
	string s,k;
	cin>>s>>k;
	s = s + s;
	int l;
	l = s.length();
	int  j;
	int m;
	m = k.length();
	//printf("%d\n",m);
	for( int i =0 ; i<l ; i++){
		 j = 0;
		for( j = 0 ; j<m && i+j<l; j++){
			//printf("%d\t",j);
			 if(s[i + j] != k[j]) { 
				 break;
			 }
		 }
		 if(j == m){
			printf("Yes\n");
			return 0;
		}
	}
		printf("No\n");
			return 0;
	}


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

int main() {
	int n,m;
	cin>>n>>m;
	/*string s;
	cin>>s;*/
	int a[n];
	for(int i=0;i<n;++i)
	cin>>a[i];
	vector<int> v;
	v.push_back(0);
	int d = 0;
	for(int i=0;i<n;++i) {
		d += a[i];
		v.push_back(d);
	}
	for(int i=0;i<v.size();++i) {
		if(v[i]>m) {
			cout<<i;
			return 0;
		}
	}
	cout<<n+1;
	return 0;
}

Measured memory_bytes: 256
// Crt. 2019-12-02  22:18:17
#include <algorithm>
#include <iostream>
#include <cstdio>
#define N 302
using namespace std;

int n, m, a[N][N], num[N], ban[N], pos[N];

int main() {
#ifdef LOCAL_JUDGE
    freopen("work.in", "r", stdin);
    freopen("work.out", "w", stdout);
#endif

    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j)
            scanf("%d", a[i] + j);
        ++num[a[i][1]], pos[i] = 1;
    }

    int ans = n;
    for (int i = 1; i <= m; ++i) {
        int x = 0;
        for (int j = 1; j <= m; ++j)
            if (num[j] > num[x]) x = j;
        ans = min(ans, num[x]);
        ban[x] = 1;
        for (int j = 1; j <= m; ++j) num[j] = 0;
        for (int j = 1; j <= n; ++j) {
            while (ban[a[j][pos[j]]]) ++pos[j];
            ++num[a[j][pos[j]]];
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const ll inf = 1e18;
const int mod = 1e5;
const int MAX = 1e6;

int main() {
    int r; cin >> r;
    if (r < 1200) cout << "ABC" << endl;
    else if (r < 2800) cout << "ARC" << endl;
    else cout << "AGC" << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

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

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

int main() {
  int n;
  ll l, t;
  cin >> n >> l >> t;
  ll x[n];
  int w[n];
  REP(i, n) {
    cin >> x[i] >> w[i];
    w[i]--;
  }
  vector<ll> vec;
  REP(i, n) {
    if (w[0] != w[i]) {
      vec.push_back(x[i]);
    }
  }
  ll cnt = 0;
  REP(i, vec.size()) {
    if (w[0] == 0) {
      cnt += (2 * t) / l;
      cnt += ((2 * t) % l >= (vec[i] - x[0]));
    } else {
      cnt -= (2 * t) / l;
      cnt -= ((2 * t) % l >= (l - vec[i] + x[0]));
    }
  }
  cnt %= n;
  if (cnt < 0) cnt += n;
  vector<P> ans;
  REP(i, n) {
    if (w[i] == 0) {
      ans.push_back(P(-1, (x[i] + t) % l));
    } else {
      ans.push_back(P(-1, (x[i] + (-t % l) + l) % l));
    }
  }
  sort(ALL(ans));
  int pos = -1;
  REP(i, n) {
    if (w[0] == 0 && ans[i].second == (x[0] + t) % l) {
      pos = i;
      // break;
    } else if (w[0] == 1 && ans[i].second == (x[0] + (-t % l) + l) % l) {
      pos = i;
      break;
    }
  }
  for (int i = pos, j = 0; i >= 0; --i, ++j) {
    ans[i].first = (cnt - j + n) % n;
  }
  for (int i = pos, j = 0; i < n; ++i, ++j) {
    ans[i].first = (cnt + j) % n;
  }
  sort(ALL(ans));
  REP(i, n) cout << ans[i].second << endl;
  return 0;
};


Measured memory_bytes: 4464
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main 
{
	static class C implements Comparator<Job>
	{

		@Override
		public int compare(Job o1, Job o2) 
		{
			return (int)o1.del - (int)o2.del;
		}
	}
	
	static class Job
	{
		long rew;
		long del;
		
		public Job(int r, int d)
		{
			rew = r;
			del = d;
		}
	}

	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		Job[] job = new Job[n];
		
		for(int i = 0; i < n; i++)
		{
			int a = sc.nextInt();
			int b = sc.nextInt();
			job[i] = new Job(b,a);
		}
		C c = new C();
		Arrays.sort(job,c);
		int cur = 0;
		PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());
		long sum = 0;
		for(int x = m; x >= 1; x--)
		{
			int accdel = m-x+1;
//			System.out.println("Day " + x + ", accdel " + accdel);
//			if(cur<n)System.out.println("Nextacc " + job[cur].del);
			while(cur < n && job[cur].del <= accdel)
			{
//				if(cur<n)System.out.println("Add new job");
				pq.add(job[cur].rew);
				cur++;
			}
			if(!pq.isEmpty())
			{
				sum += pq.poll();
//				System.out.println("Take " + sum);
			}
		}
		System.out.println(sum);

	}

}


Measured memory_bytes: 93316
a, b = map(int, input().split())
ans = 0
for i in range(a, b + 1):
    s = str(i)
    f = True
    for j in range(len(s) // 2):
        if s[j] != s[-j-1]:
            f = False
            break
    if f:
        ans += 1
print(ans)

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

using namespace std;

int main()
{
    int n, a, s=0;
    cin>>n;
    int v[n];
    for(int i=0; i<n; i++){
        cin>>v[i];
    }
    sort(v, v+n);
    for(int i=0; i<n; i++){
        s+=v[i];
    }
    cout<<s-n;

    return 0;
}


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


ll n,k,dist;
int lef,rig,x[100010];
const int INF=1<<29;

int main() {
  int ans=INF;
  cin >> n >> k;
  rep(i,n){
    cin >> x[i];
  }
  rep(i,n-k+1){
    lef=x[i];
    rig=x[i+k-1];
    if(rig<0 or lef>0){
      dist=max(abs(lef),abs(rig));
    }
    else{
      dist=abs(lef-rig)+min(abs(lef),abs(rig));
    }
    if (dist<ans)ans=dist;
  }
  cout << ans << endl;
  return 0;
}



Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)


int main(){

  int n,m,x,y;
  cin >> n >> m >> x >> y;
  int xc[n];
  rep(i,n) cin >> xc[i];
  sort(xc, xc+n);
  int yc[m];
  rep(i,m) cin >> yc[i];
  sort(yc, yc+m);
  if(xc[n-1]<yc[0] && xc[n-1]<y &&yc[0]>x) cout << "No War" << endl;
  else cout << "War" << endl;
  
  return 0;

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define sz(x) (int)((x).size())
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define FOR(i, a, n, step) for (int (i) = (a); (i) < (int)(n); (i) += (step))
#define ROF(i, a, n, step) for (int (i) = (a); (i) >= (int)(n); (i) -= (step))
#define FOREACH(it, v) for (auto (it) : (v))

using namespace std;

typedef unsigned long long ull;
typedef long long ll ;
typedef long double ld;
typedef pair <int, int> pii;

template <class T>
const bool is_impar(const T &x) {return (bool) (x & 1);}

int n, a, b, rez = 1e9, t;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> t;
    FOR (i, 0, n, 1) {
        cin >> a >> b;
        if (b <= t)
            rez = min(rez, a);
    }
    if (rez != 1e9)
        cout << rez;
    else cout << "TLE";
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
double a[54];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
    sort(a+1,a+n+1);
    double sum=0;
    for(int i=1;i+1<=n;i++){
        sum=(a[i]+a[i+1])/2.0;
        a[i+1]=sum;
    }
    printf("%.6f\n",sum);
    return 0;
}


Measured memory_bytes: 256
import java.util.*;
import java.io.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main{
    static final long mod=1000000007;
//    static int dx[]={-1,0,1,0};
//    static int dy[]={0,-1,0,1};

    public  static void main(String[] args)   throws Exception, IOException{        
        Reader sc = new Reader(System.in);
        //110110011
        PrintWriter out=new PrintWriter(System.out);
        // int n=sc.nextInt();
        int n=sc.nextInt();
        int m=sc.nextInt();
        int c=sc.nextInt();
        int ans=0;
        int b[]=sc.nextIntArray(m);
        for (int i = 0; i < n; i++) {
        	int r=c;
			for (int j = 0; j < m; j++) {
				r+=b[j]*sc.nextInt();
			}
			if(r>0)ans++;
		}
        

        out.println(ans);
        out.flush();
    }



    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }  
}

//class P {
//    int id, d;
//    P(int  id, int d) {
//        this.id=id;
//        this.d=d;
//    }
//}

class P implements Comparable<P>{
    int id,T;
    P(int id, int T) {
        this.id=id;
        this.T=T;
    }
    public int compareTo(P p){
        return id-p.id; //des
    }
}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public int[] nextIntArray(int size) throws IOException{
    	int r[] = new int[size];
    	for (int i = 0; i < size; i++) {
			r[i] = this.nextInt(); 
		}
    	return r;
    }
    public long[] nextLongArray(int size) throws IOException{
    	long r[] = new long[size];
    	for (int i = 0; i < size; i++) {
			r[i] = this.nextLong(); 
		}
    	return r;
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}


Measured memory_bytes: 22996
package main

import "fmt"

func main() {
	s := make([]int, 5)
	for i := range s {
		fmt.Scan(&s[i])
	}

	min := 0
	lastDiff := 0
	for i := range s {
		if s[i]%10 != 0 && lastDiff > (s[i]%10-10) {
			lastDiff = (s[i]%10 - 10)
		}
		min += s[i]
		for min%10 != 0 {
			min++
		}
	}

	fmt.Println(min + lastDiff)
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
string s,t;
int main(void)
{
	int i;
	int cnta[26]={0};
	int cntb[26]={0};
	cin>>s>>t;
	for(i=0;i<s.size();i++)
	{
		cnta[s[i]-'a']++;
	}
	for(i=0;i<t.size();i++)
	{
		cntb[t[i]-'a']++;
	}
	sort(cnta,cnta+26);
	sort(cntb,cntb+26);
	for(i=0;i<26;i++)
	{
		if(cnta[i]!=cntb[i])
		{
			cout<<"No";
			return 0;
		}
	}
	cout<<"Yes";
	return 0;
}


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

int main()
{
    int n;
    cin >> n;
    vector<int> d(n);
    rep(i, n) cin >> d[i];
    sort(all(d), greater<int>());
    int half = d.size() / 2;
    int count = 0;

    for (int k = 1; k <= d[0]; k++)
    {
        if (d[half - 1] >= k && d[half] < k)
            ++count;
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 640
def convolution(a, b, MOD=10**9+7):
    def butterfly(arr):
        n = len(arr)
        h = (n - 1).bit_length()
        sum_e = (911660635, 509520358, 369330050, 332049552, 983190778, 123842337, 238493703, 975955924, 603855026, 856644456, 131300601,
                 842657263, 730768835, 942482514, 806263778, 151565301, 510815449, 503497456, 743006876, 741047443, 56250497, 0, 0, 0, 0, 0, 0, 0, 0, 0)
        for ph in range(1, h + 1):
            w = 1 << (ph - 1)
            p = 1 << (h - ph)
            now = 1
            for s in range(w):
                offset = s << (h - ph + 1)
                for i in range(p):
                    l = arr[i + offset]
                    r = arr[i + offset + p] * now
                    arr[i + offset] = (l + r) % MOD
                    arr[i + offset + p] = (l - r) % MOD
                now *= sum_e[(~s & -~s).bit_length() - 1]
                now %= MOD


    def butterfly_inv(arr):
        n = len(arr)
        h = (n - 1).bit_length()
        sum_ie = (86583718, 372528824, 373294451, 645684063, 112220581, 692852209, 155456985, 797128860, 90816748, 860285882, 927414960, 354738543,
                  109331171, 293255632, 535113200, 308540755, 121186627, 608385704, 438932459, 359477183, 824071951, 0, 0, 0, 0, 0, 0, 0, 0, 0)
        for ph in range(1, h + 1)[::-1]:
            w = 1 << (ph - 1)
            p = 1 << (h - ph)
            inow = 1
            for s in range(w):
                offset = s << (h - ph + 1)
                for i in range(p):
                    l = arr[i + offset]
                    r = arr[i + offset + p]
                    arr[i + offset] = (l + r) % MOD
                    arr[i + offset + p] = (MOD + l - r) * inow % MOD
                inow *= sum_ie[(~s & -~s).bit_length() - 1]
                inow %= MOD


    n = len(a)
    m = len(b)
    if not n or not m: return []
    if min(n, m) <= 50:
        if n < m:
            n, m = m, n
            a, b = b, a
        res = [0] * (n + m - 1)
        for i in range(n):
            for j in range(m):
                res[i + j] += a[i] * b[j]
                res[i + j] %= MOD
        return res
    z = 1 << (n + m - 2).bit_length()
    a += [0] * (z - n)
    b += [0] * (z - m)
    butterfly(a)
    butterfly(b)
    for i in range(z):
        a[i] *= b[i]
        a[i] %= MOD
    butterfly_inv(a)
    a = a[:n + m - 1]
    iz = pow(z, MOD - 2, MOD)
    for i in range(n + m - 1):
        a[i] *= iz
        a[i] %= MOD
    return a

import sys
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

print(*convolution(A, B, 998244353))


# def atcoder_practice2_f():
#     import sys
#     input = sys.stdin.buffer.readline

#     N, M = map(int, input().split())
#     A = [int(x) for x in input().split()]
#     B = [int(x) for x in input().split()]

#     print(*convolution(A, B, 998244353))


# if __name__ == "__main__":
#     atcoder_practice2_f()


Measured memory_bytes: 196536
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力
		int N = sc.nextInt();
        int n = N;
        int m, s = 0;

		for(int i=10; i>0; i--){
          m = n/(int)(Math.pow(10,i));
          s = s + m;
          if(m != 0){
            n = n - (int)(Math.pow(10,i))*m;
          }

        }
        s = s + n % 10;
		if(N % s == 0){
		  System.out.println("Yes");
        } else {
          System.out.println("No");
        }
	}
}

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

int main() {
    int K, N;  // 円周, 軒数
    cin >> K >> N;
    vector<int> A(N);
    rep(i, N) { cin >> A[i]; }
    A.push_back(A[0] + K);
    int l = 0;
    rep(i, N) { l = max(l, A[i + 1] - A[i]); }
    cout << K - l << endl;
    return 0;
}

Measured memory_bytes: 1792
# include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i, n) for(int i=((int)(n)); i>0; --i)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define ll long long
# define ln cout<<'\n'
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
 
ll lcm(ll m, ll n)
{
    if ((0 == m) || (0 == n))
        return 0;
    return ((m / gcd(m, n)) * n);
}

int factorial(int n)
{
    int x = 1; // 0! = 1

    for (int i = 1; i <= n; ++i)
        x *= i;

    return x;
}


using namespace std;

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


vector<float> X(10),Y(10);

double dist(int i, int j) {
    double dx = X[i] - X[j];
    double dy = Y[i] - Y[j];
    return pow(dx * dx + dy * dy, 0.5);
}

int main(){
    ll N;
    cin >> N;
    
    //int cnt = factorial(N);
    rep(i,N){
        cin >> X[i] >> Y[i];  
    }
    long double ans = 0;
    long double cnt = factorial(N);
    vector<int> kouho(N);
    rep(i,N){
        kouho[i] = i;
    }
    do{
        for(int i = 0; i < N-1; i++) ans += dist(kouho[i], kouho[i+1]);
    }while(next_permutation(kouho.begin(),kouho.end()));
    
    

    cout << setprecision(10)<<ans/cnt << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define mod2 998244353 
#define PI 3.14159265
#define ll long long
#define ld long double
#define pi pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define sz size()
#define fi first
#define se second
#define lz length()
#define all(x) (x).begin(),(x).end()
#define scf(x) scanf("%lld",&x)
#define rep(i,n) for(ll i=0;i<n;i++)
const int INF = (int)1e9;
const ll IINF=1LL<<62;
const int maxn=10000005;
//int divr[maxn];
/**/
/*void divisor()
{for(int i=2;i<sqrt(maxn);i++)
     {
          if(!divr[i])
          {
               for(int j=i*i;j<maxn;j+=i)if(!divr[j])divr[j]=i;     
          }
     }
     for(int i=1;i<maxn;i++)if(!divr[i])divr[i]=i;
}*/
 
/*const int N=1000005;
ll n;
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); }
};
vector<pair<pi,ll>> rect;
deque<line> dq;
*/
 
 
 
 


ll modexpo(ll a,ll b)
{
     ll res=1LL;
     while(b>0)
     {
          if(b&1)res=(res*a)%mod;
          a=(a*a)%mod;
          b>>=1;
     }
     return res;
}



int main()
{
     int n;
     cin>>n;
     int a[n];
     int ct[100005]={0};
     for(int i=0;i<n;i++)
     {
          cin>>a[i];ct[a[i]]++;
     }
     for(int i=n-1;i>=0;i-=2)
     {
          if(n%2==1&&i==0&&ct[i]!=1){
               cout<<0;return 0;
          }
          else if(i!=0&&ct[i]!=2){
               //cout<<i<<"\n";
               cout<<0;return 0;
          }
     }
     ll res=modexpo(2LL,1LL*n/2);
     cout<<res;
}















 














 
 

















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

int main() {
  int A,B,C,ans=0;
  int a=0,b=0,c=0;
  cin >> A >> B >> C;
  if (A==B && B==C && A%2==1){
   cout << 0 << endl; 
  }
  else if (A==B && B==C){
   cout << -1 << endl; 
  }
  else {
  for (int i=0;;i++){
  if (A%2==1 || B%2==1 || C%2==1){
   break; 
  }
  else {
  a=A/2;
  b=B/2;
  c=C/2;
  A=b+c;
  B=c+a;
  C=a+b;
  ans++;
  }
  }
   cout << ans << endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    int n, a[100005] = {}, b[100005] = {};
    ll z = 0, y = 1ll * mod * mod, x = 0, d[100005] = {};
    char c = '+';
    cin >> n;
    for (int i = 1; i <= n; i++) {
        if (i > 1) cin >> c;
        if (c == '+') b[i] = 1;
        else b[i] = -1;
        cin >> a[i];
        z += a[i];
    }
    for (int i = 1; i <= n; i++) if (b[i] == -1) {
        d[i] = a[i];
        for (int j = i + 1; j <= n && b[j] == 1; j++) d[i] += a[j];
    }
    for (int i = 1; i <= n; i++) if (b[i] == -1) {
        y = min(y, x + d[i]);
        x += a[i];
    }
    if (x == 0) cout << z;
    else cout << z - y - y;
}


Measured memory_bytes: 5228
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("avx") 
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893
                         
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }
template<class T> T gcd(T a, T b) { re a ? gcd (b % a, a) : b; }
template<class T> T sqr(T a) { re a * a; }
template<class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

const double pi = acos(-1.0);

#define filename ""

int n;

const int M = 1e6 + 10;
set <ii, greater<ii>> w[M];
ii t[4 * M];

ii upd(int x, int lc, int rc, int pos)
{
    if (pos < lc || pos > rc) return t[x];
    if (lc == rc) {
        if (sz(w[pos])) {
            int cur = w[pos].begin()->fi;
            return t[x] = mp(cur, pos);
        } else {
            return t[x] = mp((int)-1e9, -1);
        }
    }
    int mc = (lc + rc) / 2;
    t[x] = max(upd(2 * x + 1, lc, mc, pos), upd(2 * x + 2, mc + 1, rc, pos));
    return t[x];
}

ii get(int x, int lc, int rc, int r)
{
    if (lc > r) return mp(-1e9, -1);
    if (rc <= r) return t[x];
    int mc = (lc + rc) / 2;
    return max(get(2 * x + 1, lc, mc, r), get(2 * x + 2, mc + 1, rc, r));
}

int main () {
    for (auto & x : t) x = mp(-1e9, -1);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        int bal = 0, m = 0;
        for (char c : s) {
            bal += c == '(';
            bal -= c == ')';
            m = min(bal, m);
        }
        m = abs(m);
        w[m].insert(mp(bal == -m ? -M : bal, i));
        upd(0, 0, M, m);
    }
    int bal = 0;
    for (int i = 0; i < n; i++) {
        ii cur = get(0, 0, M, bal);
        if (cur.fi == -M) cur.fi = -cur.se;
        bal += cur.fi;

        if (bal < 0) {
            cout << "No" << endl;
            re 0;
        }
        w[cur.se].erase(w[cur.se].begin());
        upd(0, 0, M, cur.se);
    }
    if (bal == 0) { cout << "Yes" << endl; }
    else { cout << "No" << endl; }
    
//    cerr << clock () << endl;
    return 0;
}


Measured memory_bytes: 128628
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(int)(n); i++)
int main(){
long N, M, K, ans=0; cin >> N >> M >> K; long A[N], B[M];
long MAX=1e16+1;
set<pair<long, int>> SB;
cin >> A[0]; 
REP(i, N-1){ cin >> A[i+1]; A[i+1]+=A[i]; }
cin >> B[0]; SB.insert(make_pair(B[0], 0));
REP(i, M-1){ cin >> B[i+1]; B[i+1]+=B[i]; SB.insert(make_pair(B[i+1], i+1)); }
SB.insert(make_pair(MAX, M));
for(long sum=0, i=0; sum<=K&&i<=N; i++){
if(i){ sum=A[i-1]; if(sum>K){break;} }else{ sum=0; }
pair<long, int> p=*SB.upper_bound(make_pair(K-sum, 100000000));
ans=max(ans, i+p.second);
}
cout << ans << "\n";
}

Measured memory_bytes: 19220
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c,d,x,z,v=0;
	cin>>a>>b>>c>>d;
	if(a>=c&&b>=d&&d>=a)
	{
		v=d-a;
		cout<<v;
	}
	else if(a>=c&&d>=b&&b>=a)
	{
		v=b-a;
		cout<<v;
	}
	else if(c>=a&&d>=b&&b>=c)
	{
		v=b-c;
		cout<<v;
	}
	else if(c>=a&&b>=c&&d>=c)
	{
		v=d-c;
		cout<<v;
	}
	else
	{
		cout<<v;
	}
	return 0;
}

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

def permutation_formula(n, r):
    return factorial(n) // factorial(n-r)

def combination_formula(n, r):
    return factorial(n) // (factorial(r)*factorial(n-r))

def solve():
    N, K = list(map(int, input().split()))
    B, R = K, N-K
    MOD = 10**9+7

    for i in range(1, K+1):
        if R+1-i < 0:
            print(0)
            continue
        
        ans = combination_formula(R+1, i)
        ans %= MOD

        if B-i > 0:
            ans *= combination_formula(B-i+i-1, i-1)
            ans %= MOD
        print(ans)



if __name__ == "__main__":
    solve()


Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int N;
int a[300010];
ll L[300010], R[300010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  cin >> N;
  for (int i = 0; i < 3 * N; i++) {
    cin >> a[i];
  }  
  priority_queue<int, vector<int>, greater<int>> que;
  ll tmp = 0;
  for (int i = 0; i < N; i++) {
    tmp += a[i];
    que.emplace(a[i]);
  }
  L[N - 1] = tmp;
  for (int i = N; i < 2 * N; i++) {
    tmp += a[i];
    que.emplace(a[i]);
    tmp -= que.top();
    que.pop();
    L[i] = tmp;
  }
  priority_queue<int> que2;
  tmp = 0;
  for (int i = N - 1; i >= 0; i--) {
    tmp += a[i + 2 * N];
    que2.emplace(a[i + 2 * N]);
  }
  R[2 * N] = tmp;
  for (int i = N - 1; i >= 0; i--) {
    tmp += a[i + N];
    que2.emplace(a[i + N]);
    tmp -= que2.top();
    que2.pop();
    R[i + N] = tmp;
  }
  ll ans = -(1LL << 50);
  for (int i = 0; i <= N; i++) {
    ans = max(ans, L[i + N - 1] - R[i + N]);
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 5240
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    map<ll, ll> par;
    rrep(i, n) {
        ll v = i - a[i];
        if (v < 0) {
            cout << -1 << endl;
            return 0;
        }
        if (i > 0) {
            if ((a[i] - 1) > a[i - 1]) {
                cout << -1 << endl;
                return 0;
            }
        }
        if (par.count(v) == 0) {
            par[v] = a[i];
        }
    }
    ll ans = 0;
    for(auto x : par) ans += x.second;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 14336
/*
 * a.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

/* typedef */

/* global variables */

/* subroutines */

/* main */

int main() {
  for (;;) {
    int h, w;
    cin >> h >> w;
    if (h == 0) break;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) putchar('#');
      putchar('\n');
    }
    putchar('\n');
  }
}

Measured memory_bytes: 1108
#include <iostream>
#include <array>
#include <map>
#include <algorithm>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <random>
#include <queue>
#include <iomanip>
#include <functional>
#include <climits>
#include <tuple>
#include <unordered_map>
#include <limits>
#include <functional>
#include <cassert>
#include <numeric>
#include <bitset>
#include <set>

#define FOR(x,n) for(int x = 0; x < n; x++)
#define INPUT(x) scanf("%d",&x)
#define PUT(x) printf("%d\n",x)
#define DPUT(x) printf("%.15lf\n",x)
#define GET(x,y) std::get<x>(y)
#define CHMAX(x,y) x = std::max(x,y)
#define CHMIN(x,y) x = std::min(x,y)
#define SP_END (i != n - 1 ? ' ' : '\n') ;

using namespace std;

using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vll = std::vector<ll>;
using vpll = std::vector<pll>;
using vpii = std::vector<pii>;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vvpii = std::vector<vpii>;
using vch = std::vector<char>;
using vvch = std::vector<vch>;
using tiii = std::tuple<int, int, int>;

const ll mod197 = 1000000007LL;
const ll INF = 10000000000LL;

const double PI11 = 3.14159265359;

const int dis[5] = { 0,1,0,-1,0 };

const int disD[4] = { 0,1,1,0 };
const int disLD[3] = { 0,1,0 };

const char disC[4] = { 'U','D','R','L' };
const char disY[4] = { -1,1,0,0 };
const char disX[4] = { 0,0,1,-1 };

ll x[100000] = {}, v[100000] = {};
ll right[100000] = {}, left[100000] = {};

int main(void)
{
	ll N, C;
	cin >> N >> C;
	ll answer = 0;

	FOR(i, N) {
		cin >> x[i] >> v[i];
	}

	ll v_sum = 0;
	FOR(i, N) {
		v_sum += v[i];
		if(i == 0)
			::right[i] = max(::right[i], v_sum - x[i]);
		else
			::right[i] = max(::right[i - 1], v_sum - x[i]);
		CHMAX(answer, ::right[i]);
	}

	v_sum = 0;
	for (int i = N - 1; i >= 1; i--) {
		v_sum += v[i];
		CHMAX(answer, ::right[i - 1] + v_sum - (C - x[i]) * 2);
	}

	v_sum = 0;
	for (int i = N - 1; i >= 0; i--) {
		v_sum += v[i];
		if (i == N - 1)
			::left[i] = max(::left[i], v_sum - (C - x[i]));
		else
			::left[i] = max(::left[i + 1], v_sum - (C - x[i]));
		CHMAX(answer, ::left[i]);
	}

	v_sum = 0;
	for (int i = 0; i <= N - 2; i++) {
		v_sum += v[i];
		CHMAX(answer, ::left[i + 1] + v_sum - (x[i]) * 2);
	}

	cout << answer << endl;

	return 0;
}


Measured memory_bytes: 3328
class UnionFind:
    def __init__(self, n):
        self.parent = [-1 for _ in range(n)]

    def root(self, x):
        """要素xの根の番号を返す"""
        if self.parent[x] < 0:
            return x
        self.parent[x] = self.root(self.parent[x])
        return self.parent[x]

    def size(self, x):
        """所属するグループの頂点数を調べる"""
        return -self.parent[self.root(x)]

    def marge(self, x, y):
        """xとyを結合する"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False
        if self.size(x) < self.size(y):
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x  # 親を更新
        return True

n, m = map(int, input().split())
bridges = []
for i in range(m):
    bridges.append([int(s) - 1 for s in input().split()])

ans = [n * (n-1) // 2]
uf = UnionFind(n)
for i in range(m-1, 0, -1):
    a, b = bridges[i]
    if uf.root(a) == uf.root(b):
        ans.append(ans[-1])
    else:
        size_a = uf.size(a)
        size_b = uf.size(b)
        ans.append(ans[-1] - size_a * size_b)
        uf.marge(a, b)
ans.reverse()
print('\n'.join(map(str, ans)))

Measured memory_bytes: 34260
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
using namespace std;
long long MOD = 1e9 + 7;

vector<vector<long long>> childs;
typedef pair<long long, long long> pll;
long long N;
vector<long long> X;
vector<long long> DP;

int main() {
	cin >> N;
	childs.resize(N);
	X.resize(N);
	for (long long n = 1; n < N; n++) {
		long long p;
		cin >> p;
		p--;
		childs[p].push_back(n);
	}
	for (long long n = 0; n < N; n++)cin >> X[n];
	DP.resize(N);
	for (long long n = N - 1; n >= 0; n--) {
		if (childs[n].size() == 0) {
			DP[n] = 0;
			continue;
		}
		long long sum = 0;
		vector<vector<bool>> DP2;
		DP2.resize(childs[n].size() + 1, vector<bool>(5000 + 1, false));
		DP2[0][0] = true;
		for (long long c = 1; c <= childs[n].size();c++) {
			for (long long w = 5000; w >= 0; w--) {
				if (w >= DP[childs[n][c-1]]) {
					DP2[c][w] = DP2[c][w] | DP2[c - 1][w - DP[childs[n][c - 1]]];
				}
				if (w >= X[childs[n][c-1]]) {
					DP2[c][w] = DP2[c][w] | DP2[c - 1][w - X[childs[n][c - 1]]];

				}
			}
			sum += X[childs[n][c-1]] + DP[childs[n][c-1]];
		}
		long long w = X[n];
		bool flag = false;
		for (; w >= 0; w--) {
			if (DP2.back()[w]) {
				DP[n] = sum - w;
				flag = true;
				break;
			}
		}
		if (!flag) {
			cout << "IMPOSSIBLE" << endl;
			return 0;
		}
	}
	cout << "POSSIBLE" << endl;
	return 0;
}

Measured memory_bytes: 896
w,b = map(int,input().split())
grid = []
for i in range(50):
    grid.append(["."]*100)
for i in range(50):
    grid.append(["#"]*100)
w -= 1
b -= 1
cnt = 0
i = 0
j = 0
while cnt < b:
    grid[i][j] = "#"
    if j == 98:
        i += 2
        j = 1
    elif j == 99:
        i += 2
        j = 0
    else:
        j += 2
    cnt += 1

cnt = 0
i = 51
j = 0
while cnt < w:
    grid[i][j] = "."
    if j == 98:
        i += 2
        j = 1
    elif j == 99:
        i += 2
        j = 0
    else:
        j += 2
    cnt += 1
print(100,100)
for i in range(len(grid)):
    print("".join(grid[i]))



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

int main() {
  int a, b;
  int add, sub, mul;
  cin >> a >> b;

  add = a + b;
  sub = a - b;
  mul = a * b;
  
  if ((add > sub) && (add > mul))
    cout << add << endl;
  
  else if ((sub > add) && (sub > mul))
    cout << sub << endl;
  
  else if ((mul > add) && (mul > sub))
    cout << mul << endl;
  
  else
    cout << a << endl;
}



Measured memory_bytes: 256
#include <stdio.h>
#include <math.h>
#define SWAP(a,b) a=a+b,b=a-b,a=a-b
int main ()
{
	int n,i;
	int t;
	scanf ("%d",&n);
	for (i=0;i<n;i++)
	{
		scanf ("%d",&t);
		if (t%2==0)
		{
			if (t%3!=0  && t%5!=0)
			{
				printf ("DENIED");
				return 0;
			}
		}
	}
	printf ("APPROVED");
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

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

const int MAX = 110000;
int dp[MAX];

int main(){
    int n; cin >> n;
    for(int i = 0; i < MAX; ++i) dp[i] = n;
    dp[0] = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(make_pair(0,0));//(dist, pos)
    while(!q.empty()){
        int cur = q.top().first;
        int v = q.top().second;
        q.pop();
        if(cur > dp[v]) continue;
        for(int pow6 = 1; v + pow6 <= n; pow6 *= 6){
            int nv = v + pow6;
            if(chmin(dp[nv], dp[v]+1)) q.push(make_pair(dp[nv], nv));
        }
        for(int pow9 = 1; v + pow9 <= n; pow9 *= 9){
            int nv = v + pow9;
            if(chmin(dp[nv], dp[v]+1)) q.push(make_pair(dp[nv], nv));
        }
    } 
    cout << dp[n] << endl;
}

Measured memory_bytes: 1024
maxA = 10**6

X = int(input())

def getIsPrimes(n):
    isPrimes = [False]*2 + [True]*(n-1)
    for x in range(2*2, n+1, 2):
        isPrimes[x] = False
    for d in range(3, int(n**0.5)+1):
        if isPrimes[d]:
            for x in range(d*d, n+1, d):
                isPrimes[x] = False
    return isPrimes

isPrimes = getIsPrimes(maxA)

for i in range(X, maxA+1):
    if isPrimes[i]:
        print(i)
        break



Measured memory_bytes: 24660