akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <tuple>
#include <cmath>
#include <algorithm>

using namespace std;


int main() {

    int W, H;
    cin >> W >> H;
    int N;
    cin >> N;

    bool rect[W][H];

    for (int w = 0; w < W; w++) {
        for (int h = 0; h < H; h++) {

            rect[w][h] = false;
        }
    }

    for (int i = 0; i < N; i++) {

        int x, y, a;
        cin >> x >> y >> a;

        if (a == 1) {
            for (int w = 0; w < x; w++) {
                for (int h = 0; h < H; h++) {
                    rect[w][h] = true;
                }
            }
        } else if (a == 2) {

            for (int w = x; w < W; w++) {
                for (int h = 0; h < H; h++) {
                    rect[w][h] = true;
                }
            }

        } else if (a == 3) {

            for (int w = 0; w < W; w++) {
                for (int h = 0; h < y; h++) {
                    rect[w][h] = true;
                }
            }

        } else if (a == 4) {

            for (int w = 0; w < W; w++) {
                for (int h = y; h < H; h++) {
                    rect[w][h] = true;
                }
            }

        }
    }

    int result = 0;

    for (int w = 0; w < W; w++) {
        for (int h = 0; h < H; h++) {

            if (!rect[w][h]) {
                result += 1;
            }
        }
    }

    cout << result << "\n";

    return 0;

}

Measured memory_bytes: 256
#include<stdio.h>
#define N 100
#define MAX 1000

int main() {
  int n,i,j,k,key;
  int data[N];
  scanf("%d",&n);
  for(i = 0; i < n; i++) {
    scanf("%d",&data[i]);
  }

  for(i = 1; i < n; i++) {
    
    for(k = 0; k < n; k++) {
      printf("%d",data[k]);
      if(k != n - 1) {
	printf(" ");
      }
    }
    printf("\n");
    
    key = data[i];
    j = i - 1;
    while(j >= 0 && data[j] > key) {
      data[j + 1] = data[j];
      j--;
      data[j + 1] = key;
    }
  }

  for(i = 0; i < n; i++) {
    printf("%d",data[i]);
    if(i != n - 1) {
      printf(" ");
    }
  }
  printf("\n");

   return 0;
}
  
    
  



Measured memory_bytes: 2084
#include<bits/stdc++.h>
#define in scanf
#define ll long
#define F(i,n) for(int i =0;i<n;++i)
#define v vector
using namespace std;

void max_self(ll& a, ll b){
    a = max(a, b);
}

int main(){
    int n, W;
    in("%d %d",&n, &W);
    v<ll> dp(W+1,0);  
    for(int i =0;i<n;++i){
        int w, value;
        in("%d %d",&w,&value);
        for(int weight_added = W-w;weight_added >=0;--weight_added){
            max_self(dp[weight_added + w],dp[weight_added] + value);
        }
    }
    ll answer = 0;
    for(int i = 0;i<=W;++i){
        max_self(answer,dp[i]);
    }
    
    cout << answer << endl;

    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
int main(void){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if((c-1)/b<=(a-1)/d) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MOD 1000000007
#define PI 3.141592653589793


int main(){
    int n, m;
    cin >> n >> m;
    vector<tuple<int, int>> s;
    vector<tuple<int, int>> c;
    for(int i = 0; i < n; i++){
        int x, y;
        cin >> x >> y;
        s.emplace_back(x, y);
    }
    for(int i = 0; i < m; i++){
        int x, y;
        cin >> x >> y;
        c.emplace_back(x, y);
    }
    vector<int> ans;
    for(auto i: s){
        int min_md = 1000000000;
        int tmp = 0;
        for(int j = 0; j < m; j++){
            int cx = get<0>(c[j]);
            int cy = get<1>(c[j]);
            int md = abs(get<0>(i) - cx) + abs(get<1>(i) - cy);
            if(min_md> md){
                min_md = md;
                tmp = j;
            }
        }
        ans.emplace_back(tmp + 1);
    }
    for(auto i: ans)
        cout << i << endl;
}


Measured memory_bytes: 256
p=input()
a=len(p)
count=0
for i in range(((a-1)//2)-1):
	if p[i]!=p[a-i-1]:
		count += 1
for i in range(((a-1)//2)-1):
	if p[i]!=p[((a-1)//2)-1-i]:
		count += 1
for i in range(((a-1)//2)-1):
	if p[((a-1)//2)+1+i]!=p[a-1-i]:
		count += 1
if count==0:
	print("Yes")
else:
	print("No")

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define vi vector<int>
template <class t, class u>
bool chmax(t &a, u b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class t, class u>
bool chmin(t &a, u b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}

int main()
{
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    rep(i, n)
    {
        if ((n - i - 1) * c - i * d <= b - a && b - a <= (n - i - 1) * d - i * c)
        {
            cout << "YES" << endl;
            return 0;
        }
    }
    cout << "NO" << endl;
    return 0;
}

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

using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    if(b % a == 0){
        cout<<(a+b)<<endl;
    }
    else{
        cout<<(b-a)<<endl;
    }
    return 0;
}


Measured memory_bytes: 3596
#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 REPR(i,n) for(ll i = n;i >= 0;--i)
#define FOR(i,m,n) for(ll i = m, i##_len=(n);i <i##_len; ++i)
#define all(x) (x).begin(),(x).end()
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; }
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
const int INF = 1e9;
const ll LLINF = 1e16;

int main(void)
{
    string s;
    cin >> s;
    if(s == "ABC"){
        cout << "ARC" << endl;
    }
    else{
        cout << "ABC" << endl;
    }
    return 0;
}

Measured memory_bytes: 3628
import java.util.Scanner;

public class Main {


	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		String str = reader.nextLine();

		if (str.length() == 2) {
			System.out.println(str);
		} else {
			System.out.print(str.substring(2));
			System.out.print(str.substring(1, 2));
			System.out.println(str.substring(0, 1));
		}

		reader.close();

	}

}


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

int N, T, ans = 2e9;
int main(){
    cin >> N >> T ;
    vector<int> c(N);
    vector<int> t(N);
    for(int i=0; i<N; i++){
	cin >> c[i] >> t[i];
	if(t[i]<=T)
	    ans = min(ans, c[i]);
    }    
    if(ans == 2e9)
	cout << "TLE" << "\n";
    else
	cout << ans << "\n";
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000005
#define MAX 5001
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int dp[4005][4005];
int main(){
	string s1, s2;
	while(cin >> s1 >> s2){
	s1 = " " + s1;s2 = " " + s2;
	fill((int*)dp, (int*)(dp+4005), 0);
	int ans = 0;
	for(int i = 1;i < s1.size();i++){
		for(int j = 1;j < s2.size();j++){
			if(s1[i] == s2[j])dp[i][j] = dp[i-1][j-1]+1;
			ans = max(ans, dp[i][j]);
		}
	}
	cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 65908
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    int n;
    cin >> n;
    vector<int> C(n + 1, 0), D(n + 1, 0), skD(n + 1), sD(n + 1), f(n + 1);
    for (int i = 0; i < n; i++) {
        int A;
        cin >> A;
        C[A]++;
    }
    for (int i = 0; i <= n; i++) {
        D[C[i]]++;
    }
    skD[0] = 0;
    sD[0] = D[0];
    for (int i = 1; i <= n; i++) {
        skD[i] = skD[i - 1] + i * D[i];
        sD[i] = sD[i - 1] + D[i];
    }
    f[0] = n;
    for (int i = 1; i <= n; i++) {
        f[i] = (skD[i] + i * (sD[n] - sD[i])) / i;
    }
    reverse(f.begin(), f.end());
    for (int i = 1; i <= n; i++) {
        int j = lower_bound(f.begin(), f.end(), i) - f.begin();
        cout << n - j << endl;
    }
    return 0;
}

Measured memory_bytes: 6784
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.HashMap;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      	HashMap<String, Integer> hs = new HashMap();
      	int n = sc.nextInt();
      	sc.nextLine();
      	String s;
        int a;
      	while(n-- > 0){
            s = sc.nextLine();
            if(hs.containsKey(s)){
            	a = hs.get(s);
                hs.replace(s, a+1);
            }else{
                hs.put(s, 1);
            }
        }
        
        int m = sc.nextInt();
        sc.nextLine();
        while(m-- > 0){
            s = sc.nextLine();
            if(hs.containsKey(s)){
            	a = hs.get(s);
                hs.replace(s, a-1);
            }else{
                hs.put(s, -1);
            }
        }
        
        ArrayList<Integer> res = new ArrayList();
        hs.forEach((k,v) -> res.add(v));
        Collections.sort(res, new Comparator<Integer>(){
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1.compareTo(o2)*-1;
                }
            
        });
        
        a = res.get(0);
        System.out.println(a < 0 ? 0 : a);
    }
}

Measured memory_bytes: 28756
/* ex9_2.c "Unification"
    dada__              */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 100001

typedef struct node{
    char elem;
    struct node *next;
}node;
//スタックにデータを追加
void push(node **first, char elem){
    node *new = calloc(1,sizeof(node));
    if(new == NULL) exit(1);
    new->elem = elem;
    new->next = *first;
    *first = new;
}
//スタックからデータを取り出す
char pop(node **first){
    char elem;
    node *tmp = *first;
    if(tmp == NULL) exit(1);
    elem = tmp->elem;
    *first = tmp->next;
    //取り出したノードのメモリを解放
    free(tmp);
    return elem;
}

int main(void){
    node *first = NULL;
    char cube[SIZE];
    char *read=cube;
    int count=0;
    //入力
    scanf("%s", cube);

    while(*read){
        //スタックがNULLならpush
        if(first==NULL){
            push(&first, *read);
            read++;
        }
        //スタックの先頭と読み込む文字の比較 
        //異なるとき
        else if((first->elem=='0' && *read=='1') || (first->elem=='1' && *read=='0')){
            pop(&first);
            count+=2;
            read++;
        }
        //同じとき
        else{
            push(&first, *read);
            read++;
        }
    }
    //答えの出力
    printf("%d", count);

    return 0;
}

Measured memory_bytes: 4876
n = int(input())
lis = list(map(int,input().split()))
lis.sort()
li = [0]
for num in lis:
    li.append(li[-1]+num)
def slime(x):
    for i in range(x,n):
        if li[i] * 2 < lis[i]:
            return False
    return True

low = 0
high = n
while high > low+1:
    mid = (high + low) // 2
    if slime(mid):
        high = mid
    else:
        low = mid
print(n-low)

Measured memory_bytes: 64664
n,a,b=map(int,input().split())
ans=0
for i in range(n+1):
    sumi=0
    stri=list(str(i))
    for stri_i in stri:
        sumi+=int(stri_i)

    if(a<=sumi and sumi<=b):
        ans+=i
print(ans)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T>
bool maxi(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool mini(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------


//--MAIN-----------------------------------
void Main() {

    ll N; cin>>N;

    vector<ll> A(N);
    rep(i,N) cin>>A[i];

    ll ans[N+1]={};

    ll mai[N+1]={};
    ll sum[N+1]={};

    unordered_map<ll,ll> mp;

    rep(i,N){
        ++mp[A[i]];
    }
    for(auto& x: mp){
        ++mai[x.second];
    }
    ll syurui=mp.size();
    ll ct=0;
    reps(i,1,N+1){
        sum[i]=sum[i-1]+syurui;
        syurui-=mai[i];
    }

    rep(i,N){
        ll K=i+1;
        /*bis*/ ll low= -1 , high= N+1 ; // min-1, max+1
        while( abs(high-low)>1 ){ ll mid=(high+low)/2;
            ( sum[mid]>=mid*K ? low : high ) = mid;
        }// if ok, okdir = mid
        ans[i]=low;
    }

    rep(i,N){
        cout<< ans[i] <<"\n";
    }

}   
//--START----------------------------------
int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
}
//-----------------------------------------


Measured memory_bytes: 23368
#include<stdio.h>
int main(void)
{
	int a = 0, ch;
	while((ch = getchar()) != EOF)
	{
		if (ch == 'C') a = 1;
		else if (ch == 'F' && a)
		{
			printf("Yes");
			return 0;
		}
	}
	printf("No");
	return 0;
}


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

int main() {

  int N;

  cin >> N;

  string S[N+1];

  for (int i = 0; i < N;i++)
  {
    cin >> S[i];
  }

  sort(S, S + N);
  S[N] = "";
  string tmp = S[0];
  vector<string> out;
  int max = 0;
  int count = 0;
  for (int i = 0; i < N + 1;i++)
  {
    //cout << S[i] << endl;
    if(tmp == S[i])
    {
      count++;
    }
    else
    {
      if(max == 0)
      {
        max = count;
        out.push_back(tmp);
        count = 1;
        tmp = S[i];
      }
      else if(max == count)
      {
        out.push_back(tmp);
        count = 1;
        tmp = S[i];
      }
      else if(max < count)
      {
        max = count;
        out.clear();
        out.push_back(tmp);
        count = 1;
        tmp = S[i];
      }
      else
      {
        count = 1;
        tmp = S[i];
      }
      
    }
  }

  for (int i = 0; i < out.size();i++)
  {
    cout << out[i] << endl;
  }
}

Measured memory_bytes: 15988
#pragma GCC optimize "O3"

#include<bits/stdc++.h>

using namespace std;

constexpr int N = 40;

typedef long long LL;

int n, m, c[N], ans = INT_MAX;
bitset<N> adj[N];

void dfs(int u, bitset<N> &ad) {
  if(u == n - 1) {
    ad[u] = true;
    bitset<N> aad;
    for(int i = 0; i < n; ++i)
      if(ad[i])
        aad |= adj[i];
    int tans = 0;
    for(int i = 0; i < n; ++i)
      if(aad[i])
        tans += c[i];
    ans = min(ans, tans);
    ad[u] = false;
    return;
  }
  for(int v = 0; v < n; ++v) 
    if(adj[u][v] && (adj[v] & ad).none()) {
      ad[u] = true;
      dfs(v, ad);
      ad[u] = false;
    }
}

int main() {

  scanf("%d %d", &n, &m);
  for(int i = 0; i < n; ++i)
    scanf("%d", c+i);
  for(int i = 0; i < m; ++i) {
    int u, v; scanf("%d %d", &u, &v); --u, --v;
    adj[u][v] = adj[v][u] = true;
  }
  bitset<N> ad;
  dfs(0, ad);
  printf("%d\n", ans);

}

Measured memory_bytes: 3220
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 1LL << 60;

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll dp[n+1][2];
    dp[0][0] = 0;
    dp[0][1] = -inf;
    for (int i = 0; i < n; i++) {
        dp[i+1][0] = max(dp[i][0] + a[i], dp[i][1] - a[i]);
        dp[i+1][1] = max(dp[i][0] - a[i], dp[i][1] + a[i]);
    }
    cout << dp[n][0] << endl;
    return 0;
}

Measured memory_bytes: 5040
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
	ll x, n;
	cin >> x >> n;
	vector<ll> p(n);
	for (ll i = 0; i < n; ++i)cin >> p[i];
	sort(p.begin(), p.end());
	for (ll i = 0;; ++i) {
		if (!binary_search(p.begin(), p.end(), x - i)) {
			cout << x - i << endl;
			return 0;
		}
		if (!binary_search(p.begin(), p.end(), x + i)) {
			cout << x + i << endl;
			return 0;
		}
	}
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long 
using namespace std;
 
ifstream fin("date.in");
ull freq[100005];
 
int main() {
    ull n, v[100005], q, x, y;
  	ull sum = 0;
 
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i];
        sum += v[i];
        freq[v[i]]++;
    }
    cin >> q;
    for (int i = 1; i <= q; i++) {
        cin >> x >> y;
 
        ull rem = freq[x];
        freq[x] = 0;
        sum -= x * rem;
        freq[y] += rem;
        sum += y * rem;
        
        cout << sum << "\n";
    }   
    
    return 0;
}

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



int main(){
  bool so[100001];
  int rui[100001] = {};
  for(int i = 1;i <= 100000;i++) so[i] = true;

  so[1] = false;
  for(int i = 2;i <= 100000;i++){
    if(so[i]){
      for(int j = i*2;j <= 100000;j+=i){
        so[j] = false;
      }
    }
  }

  for(int i = 1;i <= 100000;i+=2){
    if(so[i] && so[(i+1)/2]) rui[i] = 1;
  }

  for(int i = 0;i < 100000;i++){
    rui[i+1] = rui[i] + rui[i+1];
  }

  int q;
  cin >> q;

  int l, r;
  for(int i = 0;i < q;i++){
    cin >> l >> r;
    cout << rui[r] - rui[l-1] << endl;
  }

  return 0;
}


Measured memory_bytes: 1152
#include<iostream>
#include<string>

using namespace std;

int main(){
    string s;
    cin>>s;
    int a[4];
    for(int i=0;i<4;++i){
        a[i]=s[i]-'0';
    }

    char ans[3];
    for(int bit=0;bit<(1<<4);++bit){
        int temp=a[0];
        for(int i=0;i<3;++i){
            if(bit&(1<<i)){
                temp+=a[i+1];
                ans[i]='+';
            }
            else{
                temp-=a[i+1];
                ans[i]='-';
            }
        }
        if(temp==7){
            cout<<s[0];
            for(int i=0;i<3;++i){
                cout<<ans[i]<<s[i+1];
            }
            cout<<"=7"<<endl;
            return 0;
        }
    }
}

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <limits.h>
#include <math.h>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'

using namespace std;

typedef long long ll;
typedef pair<int,int> Pii;
typedef vector<int> vint;
const int INF = INT_MAX;

int main() {
  int N;cin >> N;
  vector<ll> A(N);
  ll minA=INF,maxA=-INF;
  repeat(i,N){
    cin >> A[i];
    A[i]-=(i+1);
    //debug(A[i]);
    minA=min(minA,A[i]);
    maxA=max(maxA,A[i]);
  }

  ll low = minA-1,high = maxA+1;
  while(high-low>1){
    ll mid = (high+low)/2;
    ll a[2]={0,0};
    repeat(i,N){
      a[0]+=abs(A[i]-mid);
      a[1]+=abs(A[i]-mid-1);
    }
    if(a[1]-a[0]<0){
      low=mid;
    }else{
      high=mid;
    }
  }
  ll ans=0;
  repeat(i,N){
    ans += abs(A[i]-high);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 1792
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: usize
    }
    for a in (1..(n + 1)).rev() {
        let b = a as f64;
        let sqrt = b.sqrt();
        if sqrt == sqrt.floor() {
            println!("{}", b as u64);
            std::process::exit(0);
        }
    }
}


Measured memory_bytes: 4352
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

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

const ll mod=1e9+7;

struct mint {

  ll x; 

  mint(ll x=0):x((x%mod+mod)%mod){} //コンストラクタを呼んで自動的にmodを取っている。

  mint operator-() const { return mint(-x);}

  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    //メンバ関数にはthisポインタがある。メンバ変数の値を返す。
    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(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1); //a=x/2
    a *= a; //繰り返し二乗法
    if (t&1) a *= *this; //tが奇数のときはxを1回かける
    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;
  }
    
};

vector<mint> f(200005);

mint choose(ll n,ll r){
  return f[n]/(f[n-r]*f[r]);
}

int main()
{
    ll n,k;
    cin >> n >> k;
    f[0]=1;
    for(ll i=1;i<=n;i++){
        f[i]=f[i-1]*i;
    }
    vector<ll> a(n);
    rep(i,n){
        cin >> a[i];
    }
    sort(a.begin(),a.end());
    ll ans=0;
    for(ll i=k;i<=n;i++){
        mint s=choose(i-1,k-1);
        ans+=(s.x*a[i-1])%mod;
        ans%=mod;
    }
    for(ll i=n-k;i>=0;i--){
        mint s=choose(n-i-1,k-1);
        ans+=mod;
        ans-=(s.x*a[i])%mod;
        ans%=mod;
    }
    cout << ans << endl;
    return 0;
} 

Measured memory_bytes: 2560
#include <iostream>
#include <vector>
#include <queue>
#define INF (int)1e9

int dijk(std::vector<std::vector<std::pair<int, int> > > fee, int s, int g); 

int main(void) {
    int n;
    int m;
    std::cin >> n >> m;
    std::vector<std::vector<std::pair<int, int> > > fee(n); //cost, to
    int a;
    int b;
    int c;
    int d;
    char temp;

    for (int i = 0; i < m; i++) {
        std::cin >> a >> temp >> b >> temp >> c >> temp >> d;    
        a--;
        b--;
        fee[a].push_back(std::make_pair(c, b));
        fee[b].push_back(std::make_pair(d, a));
    }

    int s;
    int g;
    int v;
    int p;
    std::cin >> s >> temp >> g >> temp >> v >> temp >> p;
    --s;
    --g;

    int go_cost = dijk(fee, s, g);
    int back_cost = dijk(fee, g, s);

    std::cout << v - p - go_cost - back_cost << std::endl;

    return 0;
}

int dijk(std::vector<std::vector<std::pair<int, int> > > fee, int s, int g) {
    std::vector<int> cost(fee.size(), INF);
    std::vector<int> search(fee.size(), 0);
    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >, std::greater<std::pair<int, int> > > pq;
    int first;
    int second;
    std::pair<int, int> target;

    pq.push(std::make_pair(0, s));
    cost[s] = 0;

    while(1) {
        if (pq.empty() == true) {
            break;
        }
        first = pq.top().first;
        second = pq.top().second;
        pq.pop();
        if (search[second] != 0) {
            continue;
        }
        search[second] = 1;
        for (int i = 0; i < fee[second].size(); i++) {
            target = fee[second][i];
            pq.push(std::make_pair(target.first + cost[second], target.second)); 
            if (target.first + cost[second] < cost[target.second]) {
                cost[target.second] = target.first + cost[second];
            }
        }
    }

    return cost[g];
}

Measured memory_bytes: 3024
#include<bits/stdc++.h>
typedef long long i64;
const i64 M=(1ll<<40)-1;
int n,a[407][4],t[407],tx[407];
i64 f[407],f0[407],_cnt,ans=0;
i64 min(i64 a,i64 b){return a<b?a:b;}
i64 cal(i64 a,i64 b,i64 c,i64 d){
	i64 ab=a<<10|b,abc=ab<<10|c;
	i64 cd=c<<10|d,bcd=b<<20|cd;
	return min(min(abc<<10|d,d<<30|abc),min(cd<<20|ab,bcd<<10|a));
}
int $(i64 x){
	int p=std::lower_bound(f,f+n,x)-f;
	return f[p]==x?p:-1;
}
void dec(i64 x){
	int p=$(x);
	if(p!=-1)--t[p];
}
void inc(i64 x){
	int p=$(x);
	if(p!=-1)++t[p];
}
void cal(i64 x){
	int p=$(x);
	if(p!=-1){
		_cnt*=t[p]*tx[p];
		--t[p];
	}else _cnt=0;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;++i){
		int*A=a[i];
		for(int j=0;j<4;++j)scanf("%d",A+j);
		f0[i]=f[i]=cal(A[0],A[1],A[2],A[3]);
	}
	std::sort(f,f+n);
	f[n]=M;
	for(int i=0,j=0;i<n;i=j){
		for(++j;f[i]==f[j];++j);
		t[i]=j-i;
		tx[i]=1;
		i64 x=f[i];
		if(x==((x>>20|x<<20)&M)){
			tx[i]=2;
			if(x==((x>>30|x<<10)&M))tx[i]=4;
		}
	}
	for(int i=1;i<n;++i){
		dec(f0[i]);
		for(int j=0;j<i;++j){
			dec(f0[j]);
			int*A=a[i],*B=a[j];
			for(int k=0;k<4;++k){
				i64 vs[4];
				_cnt=1;
				for(int t=0;t<4;++t){
					vs[t]=cal(A[k-t&3],A[k-t-1&3],B[t&3],B[t-1&3]);
					cal(vs[t]);
				}
				for(int t=0;t<4;++t)inc(vs[t]);
				ans+=_cnt;
			}
			inc(f0[j]);
		}
		inc(f0[i]);
	}
	printf("%lld\n",ans/3);
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
 int main(){
   int b,i,j,N,v;
   scanf("%d",&N);
   int A[N];
   if(N<0&&100<N)exit(1);
  
   for(b=0;b<N;b++){
     scanf("%d",&A[b]);
   }
   printf("%d",A[0]);
   for(b=1;b<N;b++)printf(" %d",A[b]);
   printf("\n");
   for(i=1;i<=N-1;i++){
     v=A[i];
     j=i-1;
     while(j>=0&&A[j]>v){
       A[j+1]=A[j];
       j--;
       A[j+1]=v;
     }
     printf("%d",A[0]);
     for(b=1;b<N;b++)printf(" %d",A[b]);
     printf("\n");
   }
   return 0;
 }



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

using namespace std;

struct rect{
    int x;
    int y;
    int area() const{
        return x * y;
    }
    bool operator<(const rect r) const{
        return this->area() < r.area();
    }
};

int dir(int s, rect& f){
    if(s - f.x < 0)                     //top
        return 0;
    else if(s - f.x - f.y < 0)          //right
        return 1;
    else if (s - (f.x << 1) - f.y < 0)   //bottom
        return 2;
    else                                //left
        return 3;
}

int main(){

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

    int n,w,h;
    vector<rect> v;
    rect r1,r2;

    while (cin >> n >> w >> h,w) {

        v.resize(1);
        v[0].x=w;
        v[0].y=h;

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

            int p,s;
            cin >> p >> s;

            p--;

            rect& f = v[p];

            s %= (f.x + f.y) << 1;

            switch(dir(s,f)){

              case 0:
                r1.x = s;
                r1.y = f.y;
                r2.x = f.x - s;
                r2.y = f.y;
                break;

              case 1:
                r1.x = f.x;
                r1.y = s - f.x;
                r2.x = f.x;
                r2.y = f.y - s + f.x;
                break;

              case 2:
                r1.x = s - f.x - f.y;
                r1.y = f.y;
                r2.x = (f.x << 1) - s + f.y;
                r2.y = f.y;
                break;

              case 3:
                r1.x = f.x;
                r1.y = s - (f.x << 1) - f.y;
                r2.x = f.x;
                r2.y = ((f.y + f.x) << 1) - s;

            }

            v.erase(v.begin() + p);

            pair<rect, rect> t = minmax(r1, r2);

            v.push_back(t.first);
            v.push_back(t.second);

        }

        vector<int> ans(v.size());
        for(int i = 0; i < v.size(); i++)
            ans[i] = v[i].area();

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

        for(int i = 0; i < ans.size(); i++){
            if(i)
                cout << " ";
            cout << ans[i];
        }

        cout << endl;

    }

    return 0;

}

Measured memory_bytes: 3044
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,m,n) for(int i=m;i<n;i++)
#define endre getchar(); getchar();return 0;
#define pie 3.141592653589793238462643383279
#define INF 1000000007

signed main() {
	int a, b, c;
	cin >> a >> b >> c;
	if ((a - 2)*(c - b) + 1 < 0)cout << 0 << endl;
	else	cout << (a - 2)*(c - b) + 1<<endl;
}

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

#define MAX 100002
int n;

vector<int> v[MAX];

vector<int> stk;

vector<int> pat;
inline void dfs(int b,int pr=-1){
	stk.push_back(b);
	if (b == n - 1){
		pat = stk;
	}
	for (int i = 0; i < v[b].size(); i++){
		int go = v[b][i];
		if (go == pr)continue;
		dfs(go, b);
	}
	stk.pop_back();
}
int col[MAX];
inline int dfs2(int b,int pr=-1){
	int A = 1;
	for (int i = 0; i < v[b].size(); i++){
		int go = v[b][i];
		if (go == pr)continue;
		if (col[go] == 0)continue;
		A += dfs2(go, b);
	}
	return A;
}
int main(){
	cin >> n;
	for (int i = 1; i < n; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		a--;
		b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	dfs(0);
	memset(col, -1, sizeof(col));
	col[0] = 1;
	col[n - 1] = 0;
	int lef = 1;
	int rig = pat.size()-2;
	bool t = false;
	while (lef <= rig){
		if (t == false){
			col[pat[lef]] = 1;
			lef++;
		}
		else{
			col[pat[rig]] = 0;
			rig--;
		}
		t ^= true;
	}
	int A = dfs2(0);
	int B = n - A;
	t = false;
	while (1){
		if (t == false){
			if (A == 0){
				puts("Snuke");
				return 0;
			}
			A--;
		}
		else{
			if (B == 0){
				puts("Fennec");
				return 0;
			}
			B--;
		}
		t ^= true;
	}
	return 0;
}

Measured memory_bytes: 8952
fun main(args: Array<String>) {
    val (sx, sy, tx, ty) = readLine()!!.split(" ").map(String::toInt)
    val w = tx - sx
    val h = ty - sy
    val r = StringBuilder(((w + h) shl 2) + 8)
    for (i in 1..h) r.append('U')
    for (i in 1..w) r.append('R')

    for (i in 1..h) r.append('D')
    for (i in 1..w) r.append('L')

    r.append('L')
    for (i in 1..h + 1) r.append('U')
    for (i in 1..w + 1) r.append('R')
    r.append('D')

    r.append('R')
    for (i in 1..h + 1) r.append('D')
    for (i in 1..w + 1) r.append('L')
    r.append('U')
    println(r)
}


Measured memory_bytes: 37892
# -*- coding: utf -8 -*-
# C - X: Yet Another Die Game
# 標準入力の取得
x = int(input())

# yは6⇒5⇒6⇒5・・・とすればよいので、11で割った余りを場合分けすればよい
quotient = x // 11
remainder = x % 11
if remainder == 0:
    print(quotient * 2)
elif remainder <= 6:
    print(quotient * 2 + 1)
else:
    print(quotient * 2 + 2)

Measured memory_bytes: 3060
//If you are trying to hack me I wish you can get it, Good Luck :D.
#include <bits/stdc++.h>
using namespace std;

#define debug(args...) fprintf (stderr,args)
#define pb push_back
#define mp make_pair

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

const int MAX = 1312;
const int INF = 0x3f3f3f3f;
const ll  MOD = 1000000007;

string s;


int main() {
   cin >> s;
   int a = INF, z = -1;

   for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'A') a = min(a, i);
      if (s[i] == 'Z') z = max(z, i);      
   }

   cout << z - a + 1 << endl;
}


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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
#define dump(x) cout << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

int main() {
	int s = 0;
	int N, K; cin >> N >> K;
	vi A(N);
	assert(1 <= N && N <= 500);
	assert(1 <= K && K <= TEN(9));
	rep(i, N) {
		cin >> A[i];
		s += A[i];
		assert(1 <= A[i] && A[i] <= TEN(6));
	}

	vi dvs;
	for (int i = 1; i * i <= s; ++i) {
		if (s % i == 0) {
			dvs.pb(i);
			if (s != i * i) {
				dvs.pb(s / i);
			}
		}
	}

	sort(ALL(dvs));

	int ans = 1;

	for (int i : dvs) {
		vi vm;
		rep(j, N) if (A[j] % i) {
			vm.pb(A[j] % i);
		}
		sort(ALL(vm));

		int sz = vm.size();

		vector<ll> ps(sz + 1);
		rep(j, sz) ps[j + 1] = ps[j] + vm[j];

		bool ok = false;

		for (int j = 0; j <= sz; ++j) {
			ll sl = ps[j];
			ll sr = (ll)i * (sz - j) - (ps[sz] - ps[j]);
			assert((sr - sl) % i == 0);
			if (max(sl, sr) <= K) {
				ok = true;
			}
		}

		if (ok) {
			ans = i;
		}
	}

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
	int n;
	cin>>n;
	int A[n];
	for(int i=0; i<n; i++)
		cin>>A[i];
	int q;
	cin>>q;
	while(q--){
		int count=0;
		int b,e,k;
		cin>>b>>e>>k;
		for(int i=b; i<e; i++)
			if(A[i]==k)
				count++;
		cout<<count<<endl;
	}
	return 0;
}


Measured memory_bytes: 3120
#include <bits/stdc++.h>
using namespace std;
struct Fast {
    Fast() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} fast;
template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
// long long mod = 998244353;
#define REP(i, n) for (long long i = 0; i < (n); i++)
#define rep(i, a, n) for (long long i = a; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
long long mod = 1000000007;
long long modpow(long long m, long long n, long long modvalue) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        long long t = modpow(m, n / 2, modvalue);
        return t * t % modvalue;
    } else {
        return modpow(m, n - 1, modvalue) * m % modvalue;
    }
}

long long moddiv(long long a, long long b, ll modvalue) { return (a * modpow(b, mod - 2, modvalue)) % modvalue; }

const ll INF = 1LL << 60;

string change_binary(long long a) {
    string ret;
    for (int i = 31; i >= 0; i--) {
        ret += (a >> i & 1) + '0';
    }
    return ret;
}

ll nCkmod(ll n, ll k, ll modvalue) {
    ll numera = 1;
    for (int i = n; i > n - k; i--) {
        numera *= i;
        numera %= modvalue;
    }
    ll denomi = 1;
    REP(i, k) {
        denomi *= (i + 1);
        denomi %= modvalue;
    }
    return moddiv(numera, denomi, modvalue);
}

map<ll, ll> cnt;
void primeFactors(long long n) {
    while (n % 2 == 0) {
        cnt[2]++;
        n = n / 2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2) {
        while (n % i == 0) {
            cnt[i]++;
            n = n / i;
        }
    }
    if (n > 2) {
        cnt[n]++;
    }
}
int main() {
    string s;
    cin >> s;
    if (s[0] == 'R' && s[1] == 'R' && s[2] == 'R') {
        cout << 3 << endl;
    } else if (s[0] == 'R' && s[1] == 'R' && s[2] == 'S') {
        cout << 2 << endl;
    } else if (s[0] == 'S' && s[1] == 'R' && s[2] == 'R') {
        cout << 2 << endl;
    } else if (s[0] == 'R' || s[1] == 'R' || s[2] == 'R') {
        cout << 1 << endl;
    } else {
        cout << 0 << endl;
    }
}

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


int main(){
	long long W, H, x, y; cin >> W >> H >> x >> y;
	cout << W * H * 0.5 << ' ';
	if ((long double)x == W / 2.0 && (long double)y == H / 2.0) cout << 1 << endl;
	else cout << 0 << endl;
	return 0;
}

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

using namespace std;

string S;
bool check(string str, char c)
{
  for (int i = 0; i < str.size(); i++) {
    if (str[i] != c) return false;
  }
  return true;
}

int main()
{
  cin >> S;
  string slist = S;
  sort(slist.begin(), slist.end());
  slist.erase(unique(slist.begin(), slist.end()), slist.end());

  int ans = 1100000000;
  for (auto c : slist) {
    string str = S;
    int cnt = 0;
    while (1) {
      if (check(str, c)) break;
      cnt++;
      for (int i = 0; i < str.size(); i++) {
        if (i > 0 && str[i] == c) str[i - 1] = c;
      }
      str.pop_back();
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}


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

int main(){
  int a,b;
  cin >> a >> b;
  cout << (a+b+1)/2 << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define LL long long 
#define LLU long long unsigned
#define rarr(arr, n) for(int aab = 0; aab < n; aab++){cin >> arr[aab];}
#define parr(arr, n) for(int aab = 0; aab < n; aab++){cout << arr[aab] << " ";}
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n";

#define freopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);ios_base::sync_with_stdio(0);cin.tie(0);

int main(){
	long long int n, q, sum = 0, in, a, b;
	int arr[100001] = {0};
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> in;
		arr[in]++;
		sum += in;
	}
	cin >> q;
	while(q--){
		cin >> a >> b;
		sum -= arr[a] * a;
		arr[b] += arr[a];
		sum += arr[a] * b;
		arr[a] = 0;
		cout << sum << "\n";
	}
} 

Measured memory_bytes: 3992
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1 << 21;
const ll INF = 1LL << 60;
const ll mod = 1e9+7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

// 0107c
int main(){
    ll n, k;
    cin >> n >> k;
    ll x[n];
    rep(i, n) cin >> x[i];
    ll ans = 1LL << 61;
    rep(i, n-k+1){
        ll tmp = 0LL;
        if ((x[i] >= 0 and x[i+k-1] <= 0) or (x[i] <= 0 and x[i+k-1] >= 0)){
            tmp = abs(x[i]) + abs(x[i+k-1]) + min(abs(x[i]), abs(x[i+k-1]));
        }else{
            tmp = max(abs(x[i]), abs(x[i+k-1]));
        }
        chmin(ans, tmp);
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1024
// This amazing code is by Eric Sunli Chen.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<typename T> void get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'; t=getchar());
	if(t=='-')neg=true,t=getchar();
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) get1(a),get1(b)
#define get3(a,b,c) get1(a),get2(b,c)
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

const int mod=1e9+7;

int power(int x,int y)
{
	int ret=1;
	while(y)
	{
		if(y&1)ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return ret;
}

int f[1111],invf[1111],dp[1111][1111];
int n,a,b,c,d;
int main()
{
	f[0]=invf[0]=1;for(int i=1;i<1111;i++){f[i]=1ll*i*f[i-1]%mod;invf[i]=power(f[i],mod-2);}
	
	get3(n,a,b);get2(c,d);
	dp[a][0]=1;
	for(int i=a;i<=b;i++)
	{
		int prod=1;
		for(int j=1;j<=c;j++)prod=1ll*prod*invf[i]%mod;
		for(int j=0;j<=n;j++)dp[i+1][j]=dp[i][j];
		for(int k=i*c;k<=i*d;k+=i)
		{
//			printf("hehe %lld\n",power(prod,mod-2));
			for(int j=0;j<=n-k;j++)
				dp[i+1][j+k]=(dp[i+1][j+k]+1ll*dp[i][j]*prod%mod*invf[k/i])%mod;
			prod=1ll*prod*invf[i]%mod;
		}
	}
	printendl(1ll*dp[b+1][n]*f[n]%mod);
	return 0;
}

Measured memory_bytes: 4608
#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;
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; }
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif

int main(int argc, const char * argv[]) {
	std::cout << std::fixed << std::setprecision(15);

	int S, W; cin >> S >> W;
	if(S <= W){
		cout << "unsafe" << endl;
	}
	else{
		cout << "safe" << endl;
	}
	return 0;
}


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

#define MAX 1000000
typedef long long ll;

int a[MAX];
ll sum = 0;

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

  for(int i=0; i<3*N; ++i)
   cin >> a[i];

  sort(a, a+(3*N));

  for(int i=1; i<=N; ++i) {
    sum += a[N*3-2*i];
  }

  cout << sum << endl;

  return 0;
}

Measured memory_bytes: 1536
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()

print(1000*math.ceil(N/1000)-N)

Measured memory_bytes: 65436
#include<bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
//#define cccgift
//#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
template<typename T>
inline void read(T &x)
{
    static char ch;bool f=1;
    for(x=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') f=0;
    for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=getchar());x=f?x:-x;
}
template<typename T>
void print(T x)
{
    if (x<0) x=-x,putchar('-');
    if (x>9) print(x/10);
    putchar(x%10+48);
}
template<typename T>
inline void print(T x,char ap) {print(x);if (ap) putchar(ap);}
template<typename T>
inline void chkmax(T &x,const T &y) {x=x<y?y:x;}
template<typename T>
inline void chkmin(T &x,const T &y) {x=x<y?x:y;}
int n,mod,k,f[2601][5001],g[5001][5001],tot;
#define inc(x,y) x+=y,x>=mod?x-=mod:0
int main()
{
	read(n),read(mod),k=n+1>>1,f[0][0]=g[0][0]=1;
	for(res i=1;i<=k;++i)
	  for(res j=0;j<n;++j)
	    for(res l=j;l<=n;l+=i) inc(f[i][l],f[i-1][j]);
	for(res i=k-1;i;--i)
	  for(res j=1;j<=n;++j)
	    for(res l=i*j;l<=n;l++) inc(g[j][l],g[j-1][l-i]);
	for(res i=1;i<=n;++i)
	  for(res j=0;j<=n;++j) inc(g[i][j],g[i-1][j]);
	for(res i=0;i<=n;++i)
	  for(res j=1;j<=n;++j) inc(g[i][j],g[i][j-1]);
	if(!(n&1)) {
		for(res i=1;i<=n;++i)
		  for(res j=0;j<i;++j) inc(tot,(ll)f[k][j]*g[n-i][i-j-1]%mod); //这里是n-i而不是n-j！因为最大的数不超过n，而刚开始每个数都是i，所以加的次数不能超过n-i 
	}
	else {
		for(res i=1;i<=n;++i)
		  for(res j=0;j<i;++j) inc(tot,(ll)f[k-1][j]*g[n-i][i-j-1]%mod);
	}
	print(tot,'\n');
	return 0;
}
/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do something instead of nothing and stay organized
*/ 

Measured memory_bytes: 147712
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define a first
#define b second
#define pb push_back
const int MAX = 202020;
int a[MAX];
vector<P> block;

int main(){
    
    // 1. 入力情報.
    int N;
    scanf("%d", &N);
    rep(i, N){
        scanf("%d", a + i);
        block.pb({a[i], i});
    }
    
    // 2. sort.
    sort(block.begin(), block.end());
    // for(auto& p : block) printf("%d %d\n", p.a, p.b);
    
    // 3. レンガ を 1 2 ... K のように残すには？
    int res = 0, cur = 1, index = -1;
    for(auto& p : block){
        if(cur == p.a && p.b > index){
            // 残すレンガの数をインクリメント.
            res++;
            // 次のレンガを探すため, index更新.
            index = p.b;
            // 次のレンガを探すため, インクリメント.
            cur++;
        }
    }
    
    // 4. 出力.
    int ans = N - res;
    printf("%d\n", (ans == N) ? -1 : ans);
    return 0;
    
}

Measured memory_bytes: 3956
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define RPT(i, b, e) for (int i = (b); i <= (e); i++)
#define RPT0(i, n) RPT(i, 0, n - 1)

void resolve()
{
    int A[3][3];
    RPT0(i, 3) RPT0(j, 3) cin >> A[i][j];

    int N;
    cin >> N;
    vector<int> b(N);
    RPT0(i, N) cin >> b[i];

    bool Z[3][3];
    RPT0(i, 3) RPT0(j, 3) Z[i][j] = false;

    RPT0(k, N) { RPT0(i, 3) RPT0(j, 3) if (A[i][j] == b[k]) Z[i][j] = true; }

    bool hit = false;
    RPT0(i, 3) if (Z[i][0] && Z[i][1] && Z[i][2]) hit = true;
    RPT0(j, 3) if (Z[0][j] && Z[1][j] && Z[2][j]) hit = true;
    if ((Z[0][0] && Z[1][1] && Z[2][2]) || Z[0][2] && Z[1][1] && Z[2][0])
        hit = true;

    string ans = (hit) ? "Yes" : "No";

    cout << ans << endl;
}

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


Measured memory_bytes: 256