akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
typedef long long ll;

int main(){
	int n; cin>>n;
	vector<int> v(n);
	rep(i,n) cin>>v[i];
	int cnt=0;
	rep(i,n){
		if(i==v[v[i]-1]-1) cnt++;
	}
	cout<<cnt/2<<endl;
}

Measured memory_bytes: 3784
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define lper(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[4]={1,0,-1,0};
const ll dx[4]={0,-1,0,1};
const ll ddx[8]={ 0, 1, 0,-1, 1, 1,-1,-1};
const ll ddy[8]={ 1, 0,-1, 0, 1,-1, 1,-1};
ll modfac(ll a){
    ll ans=1;
    while(a>1){
        ans*=a;
        ans%=1000000007;
        a--;
    }
    return ans;
}
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;
}
struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) {}

    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};
ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    double l;cin >>l;
    l/=3;
    CST(7);
    cout << l*l*l <<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }


using namespace std;

int main(){
    ll N, A, B;
    cin >> N >> A >> B;
    vector<ll> X(N);
    rep(i, N) cin >> X[i];

    vector<ll> diff(N-1);
    rep(i, N-1) diff[i] = X[i+1] - X[i];
    sort(diff.rbegin(), diff.rend());

    ll dist = X.back() - X.front();
    ll ans = dist * A;

    for (ll i = 1; i < N; i++) {
        dist -= diff[i-1];
        ll d = dist * A + i * B;
        ans = min(ans, d);
    }

    cout << ans << endl;
    return 0;

}

Measured memory_bytes: 1792
import sys
import heapq
import bisect
import itertools

mod = 10**9+7
dd = ((-1,0),(1,0),(0,-1),(0,1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def LI_(): return([int(x)-1 for x in sys.stdin.readline().split()])
def S(): return(sys.stdin.readline()[:-1])
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a * b // GCD(a,b)

def Eratosthenes(N):
    r = [True]*(N+1)
    r[0] = False
    r[1] = False
    i = 2
    while i*i<=N:
        if r[i]: 
            j = i
            while i*j<=N:
                prime[i*j]=False
                j+=1
        i+=1
    return(r)

def main():
    N = I()
    if N%2==0:
        return(0.5)
    else:
        return(((N+1)//2)/N)

if __name__ == "__main__":
    print(main())


Measured memory_bytes: 38384
#include<cstdio>
#include<bits/stdc++.h>
typedef long long int ll;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for(int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))
#define BIT(b, i) (((b) >> (i)) & 1)
#define LOG_F 1
#define LOG(...) if(LOG_F) fprintf(stderr, __VA_ARGS__)

using namespace std;

ll pow(int x, int n){
  return n == 0 ? 1 : x * pow(x, n - 1);
}


/*

 */
int a,b;

int main(){
  cin>>a>>b;
  if(a%3==0 || b%3==0 || (a+b)%3==0)
    cout<<"Possible\n";
  else
    cout<<"Impossible\n";

  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
long long ggcd(long long a,long long b) {
  if(a < b) return ggcd(b, a);
  long long r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}
int main(){
  long long n;
  cin >> n;
  long long aaa=0;
  long long aa=0,bb=0;
  long long ans=1;
  long long p=1000000007;
  map<pair<long long,long long>,long long> k;
  map<pair<long long,long long>,long long> l;
  rep(i,n){
    long long a,b;
    cin >> a >> b;
    if(a==0&&b==0){
      aaa++;
      continue;
    }
    if(a==0&&b!=0){
      aa++;
      continue;
    }
    if(a!=0&&b==0){
      bb++;
      continue;
    }
    long long c=ggcd(abs(a),abs(b));
    a/=c;
    b/=c;
    if(a<0){
      a=-1*a;
      b=-1*b;
    }
    k[make_pair(a,b)]++;
    if(b<0){
      a=-1*a;
      b=-1*b;
    }
    l[make_pair(b,a)]++;
  }
  long long u[200001];
  u[0]=1;
  rep(i,200000){
    u[i+1]=u[i]*2%p;
  }
  set<pair<long long ,long long>> s;
  for(pair<pair<long long,long long>,long long> w: k){
    pair<long long,long long> o=w.first;
    long long z=w.second,x=o.first,y=o.second;
    long long xx=x,yy=y;
    if(yy<0){
      xx=-1*xx;
      yy=-1*yy;
    }
    if(s.count(make_pair(yy,xx)))continue;
    s.insert(make_pair(x,-y));
    ans=ans*((u[z]+u[l[make_pair(x,-y)]]-1)%p)%p;
  }
  ans=ans*((u[aa]+u[bb]-1)%p)%p;
  ans=(ans+aaa-1+p)%p;
  cout << ans << endl;
}


Measured memory_bytes: 55084
def solve():
    left = {}
    right = {}
    for i in range(n):
        r = - a[i] + (i + 1)
        l = a[i] + (i + 1)
        if not str(r) in right:
            right[str(r)] = 1
        else:
            right[str(r)] += 1
        if not str(l) in left:
            left[str(l)] = 1
        else:
            left[str(l)]+= 1
    ans = 0
    for i in range(n):
        ii = str(i)
        if ii in right and ii in left:
            ans += right[ii] * left[ii]
    print(ans)
    return


n = int(input())
a = [int(s) for s in input().split(" ")]
solve()


Measured memory_bytes: 142652
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <set>
#include <map>

using namespace std;

const long long oo = 1000000000000ll;
const int maxN = 200;

#define foru(i, l, r) for (int i = l; i <= r; ++i)
#define ford(i, r, l) for (int i = r; i >= l; --i)
#define repu(i, r) for (int i = 0; i < r; ++i)
#define ll long long
#define F first
#define S second
#define mp make_pair
#define pint pair < ll, int >

int n, a[maxN], f[maxN][maxN * maxN];

int dp(int i, int s) {
    if (f[i][s] != -1) return f[i][s];
    if (i > n) return (s % 10) ? s : 0;
    f[i][s] = max(
        dp(i + 1, s),
        dp(i + 1, s + a[i]));
    return f[i][s];
}

int main() {
    cin >> n;
    foru(i, 1, n) cin >> a[i];
    memset(f, 255, sizeof(f));
    cout << dp(1, 0);
}


Measured memory_bytes: 31488
S = input()
N = len(S)

def f(k):
    s = S[-k:k]
    if s == "":return False
    return s.count(s[0])!=len(s)

def binary_search(l,r,func):
    if func(l):return l-1
    if not func(r):return r
    while l+1<r:
        i = (l+r)//2
        if func(i):
            r = i
        else:
            l = i
    return l

print(binary_search(0,N,f))

Measured memory_bytes: 3188
N,M = map(int,input().split())

a = [0 for i in range(N)]
b = [0 for i in range(N)]
c = [0 for i in range(M)]
d = [0 for i in range(M)]

for i in range(N):
    a[i],b[i] = map(int,input().split())

for i in range(M):
    c[i],d[i] = map(int,input().split())

for i in range(N):
    dist = []

    for j in range(M):
        tmp = abs(a[i]-c[j]) + abs(b[i]-d[j])
        dist.append(tmp)

    print(dist.index(min(dist)) + 1)

Measured memory_bytes: 3064
n1 = int(input())
n2 = [int(i) for i in input().split()]

c1 = 0
for i1 in range(n1):
    if i1 == 0:
        c1 += 1
        continue
    if max(n2[0:i1]) <= n2[i1] :
        c1 += 1

print(c1) 

Measured memory_bytes: 3060
#include<iostream>
#include<string.h>
using namespace std;
int N;
double ar[3003];
int dp[3030][3003];
double ans[3003][3003];

double rec(int pos, int mi) {
	// printf("%d %d\n", pos, mi);
	if(pos == -1 && !mi)
		return 1;
	if(mi-1>pos)
		return 0;
	if(dp[pos][mi] != -1)
		return ans[pos][mi];
	double bty = rec(pos-1, mi) * (1 - ar[pos]);
	if(mi) {
		bty = bty + rec(pos-1, mi-1) * (ar[pos]);
	}
	dp[pos][mi] = 1;
	ans[pos][mi] = bty;
	return bty;
}

int main() {
	memset(dp, -1,sizeof(dp));
	scanf("%d", &N);
	for(int i=0;i<N;++i)
		scanf("%lf", &ar[i]);
	double bty = 0.0;
	for(int i=N;i>N/2;--i) {
		bty += rec(N-1, i);
	}
	printf("%.10lf\n", bty);
	return 0;
}

Measured memory_bytes: 104832
// Daritys Morgho ban Rovegrie
// Math = ♥
// Sometimes it is people that no one imagines anything of who do things that no one can imagine
// After all this time? Always

#include <bits/stdc++.h>                        // uncomment before submission
//#include <ext/pb_ds/assoc_container.hpp>		// uncomment before submission
//#include <ext/pb_ds/tree_policy.hpp>			// uncomment before submission
//using namespace __gnu_pbds;					// uncomment before submission
using namespace std;
//<---------------------------------------------------Template----------------------------------------------------------->
#define int long long
#define ll long long
#define ld long double
const int INF = 1e15 + 7;
const int MAX = 310;
const int MOD = 1e9 + 7;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;                  // Vector of long long
typedef vector<vi> vvi;                 // Vector of vi
typedef vector<ii> vii;                 // Vector of pairs
typedef vector<vii> vvii;               // Vector of Vector of pairs
typedef vector<bool> vb;                // Vector of bool
#define pq priority_queue               // Max heap (To convert to min heap, use negative sign before every value)
#define ff first                        // For pairs
#define ss second                       // For pairs
#define pb push_back                    // Push back to vector
#define mp make_pair                    // Makes pairs to be stored as pair
#define all(c) (c).begin(), (c).end()   // Mainly used by me in sorting
#define run(x, c) (x)=(c).begin(); (x)!=(c).end(); (x)++ // Mainly used by me for range based loops
// ordered_set adds two new functions to set - (set).find_by_order([kth element based on zero indexing]) and order_of_key()
// order_of_key returns number of elements less that parameter. If element exists, that order is its index
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//<----------------------------------------------------------------------------------------------------------------------->

vector<vector<vector<ld> > > f(310, vector<vector<ld> >(310, vector<ld>(310, -10)));
int n = 0;

ld solve(int i, int j, int k)
{
    if (i == 0 && j == 0 && k == 0) return 0;
    if (i < 0 || j < 0 || k < 0) return 0;
    else if (f[i][j][k] >= 0) return f[i][j][k];

    f[i][j][k] = (((ld) n) / ((ld) (i + j + k))) + (((ld) i) / ((ld) (i + j + k))) * (solve(i - 1, j, k))
                 + (((ld) j) / ((ld) (i + j + k))) * (solve(i + 1, j - 1, k))
                 + (((ld) k) / ((ld) (i + j + k))) * (solve(i, j + 1, k - 1));

    return f[i][j][k];
}

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

    cin >> n;

    int x = 0, y = 0, z = 0, a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        if (a[i] == 1) x++;
        else if (a[i] == 2) y++;
        else z++;
    }

    ld ans = solve(x, y, z);
    printf("%0.9Lf", ans);
    return 0;
}


Measured memory_bytes: 471168
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf (ll)(1e18)
#define mod 1000000007




int main()
{
    int t=1;
    //cin>>t;

    while( t-- )
    {
        ll n,d;
        cin>>n>>d;
        ll ans=0;

        for( int i=0 ; i<n ; i++ )
        {
            ll p,q;
            cin>>p>>q;
            ll sqr=( (p*p)+(q*q) );
            if( d*d>=sqr )
                ans++;
        }
        cout<<ans;
    }

    return 0;
}












Measured memory_bytes: 3532
/*Shivam Bhalla */
/* f2016941@pilani.bits-pilani.ac.in */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>
#include <bits/stdc++.h>
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
long long maxv = 1e9;

vector<int>arr(100005);
int main(){
	int N,Q;
	cin>>N>>Q;
	string str;
	cin>>str;
	for(int i=0;i<N-1;i++){
		if(str[i]=='A' && str[i+1]=='C')
			arr[i+1]=arr[i]+1;
		else
			arr[i+1]=arr[i];
	}
	int A,B;
	// for(int i=0;i<N;i++)
	// 	cout<<arr[i]<<" ";
	for(int i=0;i<Q;i++){
		cin>>A>>B;
		A--;
		B--;
		cout<<(arr[B]-arr[A])<<"\n";
	}
	return 0;
}


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

const long long INF = 1LL << 60;

int main() {
    int N; cin >> N;
    vector<long long> h(N);
    for(int i = 0; i < N; ++i) cin >> h[i];
    
    vector<long long> dp(N, INF);
    dp[0] = 0;
    for(int i = 0; i < N; ++i) {
        dp[i+1] = min(dp[i+1], dp[i] + abs(h[i+1] - h[i]));
        dp[i+2] = min(dp[i+2], dp[i] + abs(h[i+2] - h[i]));
    }
    
    cout << dp[N-1] << endl;
}


Measured memory_bytes: 1792
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

signed main(){
    vector<int> a(3);
    cin >> a[0] >> a[1] >> a[2];
    sort(a.begin(), a.end());

    cout << abs(a[2] - a[0]) << endl;

    return 0;
}


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

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0;}
template<class T>bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0;}

using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r) {return (r > EPS) - (r < -EPS);}
inline R sq(R x) {return sqrt(max(x, 0.0L));}

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// Problem Specific Parameter:

const int limit = 4010;
int num[2][limit][limit];

int calc(int t, int a, int b, int c, int d) {
	int ret = 0;
	ret += num[t][c - 1][d - 1];
	if (a - 1 >= 0) ret -= num[t][a - 1][d - 1];
	if (b - 1 >= 0) ret -= num[t][c - 1][b - 1];
	if (a - 1 >= 0 and b - 1 >= 0) ret += num[t][a - 1][b - 1];
	return ret;
}

int main(void) {
	int n, k;
	cin >> n >> k;
	const int unit = 2 * k;

	rep(i, n) {
		int cx, cy;
		char c;
		cin >> cx >> cy >> c;
		cx %= unit;
		cy %= unit;
		num[c == 'B'][cx][cy]++;
		num[c == 'B'][cx + unit][cy]++;
		num[c == 'B'][cx][cy + unit]++;
		num[c == 'B'][cx +  unit][cy + unit]++;
	}

	rep(t, 2) {
		rep(i, limit)rep(j, limit) {
			if (i - 1 >= 0) num[t][i][j] += num[t][i - 1][j];
			if (j - 1 >= 0) num[t][i][j] += num[t][i][j - 1];
			if (i - 1 >= 0 and j - 1 >= 0) num[t][i][j] -= num[t][i - 1][j - 1];
		}
	}

	int ans = 0;
	rep(i, unit)rep(j, unit) {
		int cur = 0;
		cur += calc(0, i, j, i + k , j + k);
		cur += calc(1, i + k, j, i + 2 * k, j + k);
		cur += calc(1, i, j + k, i + k, j + 2 * k);
		cur += calc(0, i + k, j + k, i + 2 * k, j + 2 * k);
		//cerr << i << " " << j << " " << cur << endl;
		chmax(ans, cur);
	}

	cout << ans << endl;

	return 0;
}

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

struct P {
  string name;
  int time;
};

P p[MAX];
int head, tail;

// O(1)
void enqueue(P p1) {
  p[tail] = p1;
  tail = (tail + 1) % MAX;
  // tail == MAXだったら0
  // それ以外はtail + 1
}

// O(1)
P dequeue() {
  P rtn = p[head];
  head = (head + 1) % MAX;
  // head == MAXだったら0
  // それ以外はtail + 1
  return rtn;
}

// O(n)
int main() {
  int n, q;
  int cnt = 0;
  P buf;
  cin >> n >> q;
  rep(i, n) cin >> p[i].name >> p[i].time;
  head = 0;  // 要素が入っている先頭インデックス
  tail = n;  // 要素が入ってない先頭インデックス（次要素を入れるところ）
  // キューにあるプロセスが無くなるまで(つまり、head==tailになるまで)続ける
  while (head != tail) {
    buf = dequeue();
    // min(q,buf.time)はbufプロセスの処理時間
    cnt += min(q, buf.time);  // 累計処理時間に加算
    buf.time -= min(q, buf.time);  // bufプロセスの残りの処理時間を算出
    if (buf.time > 0) {
      enqueue(buf);  // bufプロセスが未処理であればキューに追加
    } else {
      cout << buf.name << " " << cnt << endl;
    }
  }
  return 0;
}


Measured memory_bytes: 6960
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
int main (){
    int a, b, c, k;
    cin >> a >> b >> c >> k;
    int m = max(a, max(b, c));
    int ans = a+b+c;
    for (int i = 0; i < k; i++){
        ans += m;
        m *= 2;
    }
    cout << ans << endl;
    return 0;
    }
    


Measured memory_bytes: 256
import os
import sys
from io import BytesIO, IOBase
from sys import stdin
def input():
    return stdin.readline()

def main():
    N, S = map(int,input().split())
    A = list(map(int,input().split()))
    mod = 998244353
    dp = [[0 for j in range(S + 1)] for i in range(N + 1)]
    dp[0][0]=1
    for i in range(1,N+1) : 
        for j in range(S + 1) : 
            if(j==0):
                dp[i][j]=pow(2,i,mod)
            else:
                dp[i][j]+=2*dp[i-1][j]
                dp[i][j]%=mod
                if(j-A[i-1]>=0):
                    dp[i][j]+=dp[i-1][j-A[i-1]]
            dp[i][j]%=mod
    print(dp[N][S]%mod)
    


# fast-io region

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)


def input():
    return sys.stdin.readline().rstrip("\r\n")


def write(*args, end="\n"):
    for x in args[:-1]:
        sys.stdout.write(str(x) + " ")
    sys.stdout.write(str(args[-1]))
    sys.stdout.write(end)


def r_array():
    return [int(x) for x in input().split()]


def r_int():
    return int(input())


if __name__ == "__main__":
    main()



Measured memory_bytes: 146904
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))


Measured memory_bytes: 12588
<?php
fscanf(STDIN, "%d", $N);
$A = array_map('intval', explode(' ', trim(fgets(STDIN))));
rsort($A);
$p2 = intdiv($N - 2, 2);
$p1 = $N - 1 - $p2;
$ans = array_sum(array_slice($A, 0, intdiv($N + 1, 2))) * 2;
$ans -= $A[0];
if ($N % 2 === 1) $ans -= $A[intdiv($N - 1, 2)];
printf("%d", $ans);


Measured memory_bytes: 38172
// #define DEBUGRUN

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

#include<list>

// size, push_back, pop_back, insert, erase

const int MAX_N = 3000000;
char cmd[MAX_N];
int arg[MAX_N];

int main() {

	int N;
	cin >> N;

	rep(i, N){

		// string command;
		// cin >> command;
		char command[20];
		scanf("%s", command);

#ifdef DEBUGRUN
		cerr << "X";
#endif
		// cerr << "X";
		if (command[0] == 'i') {
			cmd[i] = 'I';
			cin >> arg[i];
		}
		else if (command[6] == 'F') {
			cmd[i] = 'F';
		}
		else if (command[6] == 'L') {
			cmd[i] = 'L';
		}
		else {
			cmd[i] = 'D';
			cin >> arg[i];
		}
	}
	
	list<int> lst;
	rep(i, N){
		if (cmd[i] == 'I'){ lst.push_front(arg[i]); }
		if (cmd[i] == 'D'){
			for (list<int>::iterator it = lst.begin(); it != lst.end(); it++){
				if (*it == arg[i])
				{
					lst.erase(it);
					break;
				}
			}
		}
		if (cmd[i] == 'F'){ lst.pop_front(); }
		if (cmd[i] == 'L'){ lst.pop_back(); }
	}

	for (list<int>::iterator it = lst.begin(); it != lst.end(); it++){
		if (it != lst.begin()) { cout << ' '; }
			cout << *it;
	}
	cout << endl;

}

Measured memory_bytes: 40588
import java.util.Scanner;
import java.awt.Graphics;

class Main{

    //private Graphics g = new Graphics();

    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	Point p1 = new Point();
	p1.Point(0.0,0.0);	
	Point p2 = new Point();
	p2.Point(100.0,0.0);	
	p1.printPoint();
	Koch(n,p1,p2);
	p2.printPoint();
    }

    public static void Koch(int n, Point p1, Point p2){
	if(n==0){
	    return;
	}else{
	    Point s = new Point();
	    s.Point((2*p1.x+p2.x)/3, (2*p1.y+p2.y)/3);
	    Point t = new Point();
	    t.Point((p1.x+2*p2.x)/3, (p1.y+2*p2.y)/3);
	    Point u = new Point();
	    u.Point((t.x-s.x)*Math.cos(60*Math.PI/180)
		    -(t.y-s.y)*Math.sin(60*Math.PI/180)+s.x, 
		    (t.x-s.x)*Math.sin(60*Math.PI/180)
		    +(t.y-s.y)*Math.cos(60*Math.PI/180)+s.y);
	    Koch(n-1, p1, s);
	    s.printPoint();
	    Koch(n-1,s,u);
	    u.printPoint();
	    Koch(n-1,u,t);
	    t.printPoint();
	    Koch(n-1,t,p2);
	}
    }
    
    static class Point{
	public double x;
        public double y;

	public void Point(double x1, double y1){
	    x = x1;
	    y = y1;
	}

	public void printPoint(){
	    System.out.println(String.format("%02.8f",x)+" "+String.format("%02.8f",y));
	}
    }
}



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

int main(){
    string s;
    cin >> s;
    int n=s.size();
    int ctr=0;
    int ans=0;
    rp(i,n){
        if(s.at(i)=='A'||s.at(i)=='T'||s.at(i)=='G'||s.at(i)=='C')
        ctr++;
        else ctr=0;
        ans = max(ans,ctr);
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;

typedef pair<int, int> pi;
typedef vector<pi> vpi;

#define REPI(x) for(int i = 0; i < x; i++)
#define REPJ(x) for(int j = 0; j < x; j++)
#define H_MAX 300
#define W_MAX 300

vpi A;
int cost[H_MAX*W_MAX];

int ABS(const pi& a, const pi& b)
{
	return (abs(a.first - b.first) + abs(a.second - b.second));
}

int main(void)
{
	int H, W, D, Q, L, R, a;
	cin >> H >> W >> D;

	A.resize(H*W);

	REPI(H){
		REPJ(W){
			cin >> a;
			a--;
			A[a] = {i, j};
		}
	}

	for(int i = D; i < H*W; i++){
		cost[i] = cost[i-D] + ABS(A[i], A[i-D]);
	}

	cin >> Q;

	while(Q--)
	{
		cin >> L;
		cin >> R;
		cout << (cost[R-1] - cost[L-1]) << endl;
	}
	return 0;
}

Measured memory_bytes: 2176
from bisect import bisect_left,bisect_right,insort

n = int(input())
s = list("-" + str(input()))
lis = [[] for i in range(26)]
for i in range(1,n+1):
    num = ord(s[i])-ord("a")
    lis[num].append(i)

q = int(input())
for turn in range(q):
    query = list(map(str,input().split()))
    if query[0] == "1":
        a = int(query[1])
        if s[a] == query[2]:
            continue
        num = ord(s[a])-ord("a")
        lis[num].pop(bisect_left(lis[num],a))
        next = ord(query[2])-ord("a")
        insort(lis[next],a)
        s[a] = query[2]
    else:
        a,b = int(query[1]),int(query[2])
        ans = 0
        for alphabet in range(26):
            if bisect_right(lis[alphabet],b)-bisect_left(lis[alphabet],a) >= 1:
                ans += 1
        print(ans)

Measured memory_bytes: 29088
#include <bits/stdc++.h>
#include<cmath>
using namespace std;
int main(void)
{
    int n,k;
    cin >> n>>k;
    vector<int>a;
    int ans=0;
    int ba;
    vector<int>press;
    for(int i=0;i<n;i++){
        cin >> ba;
        a.push_back(ba);
    }
    sort(a.begin(),a.end());
    
    for(int i = 0; i < n; i++)
    {
        if(i==0){
            press.push_back(1);
        }else if(a.at(i-1)==a.at(i)){
           //cout <<*press.end()<<endl;
            //(*press.end())+=1;
            press.back()+=1;
        }else{
            press.push_back(1);
        }
    }
    sort(press.begin(), press.end(), greater<int>());
    for(int i=k;i<press.size();i++){
        ans+=press.at(i);
    }
    
    cout << ans <<endl;
}


Measured memory_bytes: 2548
#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main(){
    int n;
    cin >> n;
    vector<int> h(n);
    rep(i, n) cin >> h[i];
    int min_num = 1e9;
    int l = 0;
    int r = 0;
    int ans = 0;
    while(1) {
//        cout << l << " " << r << " " << min_num << endl;
        if ( l == n ) break;
        if ( h[r] == 0 || r == n ) {
            if ( min_num == 1e9 ) {
                if ( h[l] == 0) ++l;
                r = l;
                continue;
            }
            for ( int i = l; i < r; ++i ) {
                h[i] -= min_num;
            }
            ans += min_num;
            min_num = 1e9;
            r = l;
        }
        else {
            min_num = min(min_num, h[r]);
            ++r;
        }
        if ( h[l] == 0 ) {
            ++l;
            r = l;
        }
    }
    cout << ans << endl;
    return 0;
}


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

typedef long long LL;

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ALL(x) (x).begin(),(x).end()
const int IINF = 1e9;
const LL LINF = 1e18;
const LL mod = 1e9+7;

int main() {
    string s;
    int K;
    cin >> s;
    cin >> K;
    int N = s.size();
    vector<int> nums(N, 0);
    reverse(ALL(s));
    REP(i, N) {
        nums[i] = s[i] - '0';
    }
    vector<vector<LL>> dpLess(N, vector<LL>(K + 1, 0));
    vector<vector<LL>> dpEq(N, vector<LL>(K + 1, 0));
    auto f = [](auto x){return max(x, 0);};
    REP(i, N) {
        dpLess[i][0] = 1;
    }
    dpLess[N - 1][1] = nums[N - 1] - 1;
    dpEq[N - 1][1] = 1;
    for(int i = N - 2;0 <= i;i--) {
        for(int k = 1;k <= K;k++) {
            dpLess[i][k] = dpLess[i + 1][k] + dpLess[i + 1][k - 1]*9 + dpEq[i + 1][k - 1]*f(nums[i] - 1);
            if(0 < nums[i]) {
                dpLess[i][k] += dpEq[i + 1][k];
            }
            if(nums[i] == 0) {
                dpEq[i][k] = dpEq[i + 1][k];
            }
            if(nums[i] != 0) {
                dpEq[i][k] = dpEq[i + 1][k - 1];
            }
        }
    }
    cout << dpLess[0][K] + dpEq[0][K] << endl;
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define BUF 50
#define INF 1<<30
using namespace std;
ll MOD = 1e9 + 7;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S;
ll ans = 0;
int main() {
	cin >> N;
	vector<int>a(N);
	vector<pair<int, int>>res;
	for (int i = 0; i < N; i++)cin >> a[i];
	auto mxit = max_element(a.begin(), a.end());
	auto mnit = min_element(a.begin(), a.end());
	if (*mnit < 0 && *mxit < abs(*mnit)) {
		for (int i = 1; i <= N; i++)res.push_back({ distance(a.begin(),mnit) + 1, i });
		for (int i = N; i > 1; i--)res.push_back({ i, i - 1 });
	}
	else {
		for (int i = 1; i <= N; i++)res.push_back({ distance(a.begin(),mxit) + 1,i });
		for (int i = 1; i < N; i++)res.push_back({ i,i + 1 });
	}
	cout << res.size() << endl;
	for (int i = 0; i < res.size(); i++)cout << res[i].first << " " << res[i].second << endl;
}

Measured memory_bytes: 256
#include <stdio.h>

int n, A[20], q, M[200];

bool solve(int index, int num) {
    if (num == 0) return true;
    if (num < 0 || index >= n) return false;

    if (solve(index + 1, num)) return true;
    if (solve(index + 1, num - A[index])) return true;

    return false;
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &A[i]);
    }
    scanf("%d", &q);
    for (int i = 0; i < q; ++i) {
        int m;
        scanf("%d", &m);
        if (solve(0, m)) {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
    return 0;
}

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

#define endl '\n'
#define int long long

const int N = 3009;
int cache[N][N], ls, lt;
string s, t;

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	cin >> s >> t;
	ls = s.size(); lt = t.size();
	for(int i = 1; i <= ls; i ++) {
		for(int j = 1; j <= lt; j ++) {
			if(s[i-1] == t[j-1]) {
				cache[i][j] = 1 + cache[i-1][j-1];
			}
			else {
				cache[i][j] = max(cache[i-1][j], cache[i][j-1]);
			}
		}
	}
	string ans;
	for(int i = ls, j = lt; i > 0 and j > 0;) {
		if(s[i-1] == t[j-1]) {
			ans = s[i-1] + ans;
			i --, j --;
		}
		else if(cache[i-1][j] == cache[i][j]) {
			i --;
		}
		else {
			j --;
		}
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 70784
import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		long ans = 0;
		long b = 0;
		for(int i=0; i<s.length(); i++)
			if(s.charAt(i)=='B') 
				b += 1;
			else
				ans += b;
		System.out.println(ans);
	}
}

Measured memory_bytes: 26068
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long N,K;
    cin >> N >> K;
    long long plus=0;
    long long minus=0;
    vector<long long>A,B;
    for(int i=0;i<N;i++){
        int x;
        cin >> x;
        if(x>0){plus++;A.push_back(x);}
        else if(x<0){minus++;B.push_back(-x);}
    }
    long long zero=N*(N-1)/2-plus*minus-(minus*(minus-1)/2)-(plus*(plus-1)/2);
    long long L=0;
    long long R=(long long)(1e18)+10;
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    if(K<=plus*minus){//答えが負の場合
       while (R-L>1)
       {
           long long mid=(L+R)/2;
           long cnt=0;
           int i=0;
           int j=B.size()-1;
           while(i<A.size()&&j>=0){
               long long Y=abs(A[i]*B[j]);
               if(Y>=mid){
                   cnt+=A.size()-i;
                   j--;
               }
               else{
                   i++;
               }
           }
           if(cnt<K){
               R=mid;
           }
           else{
               L=mid;
           }
       }
       cout << -L << endl;
    }
    else if(plus*minus<K&&K<=plus*minus+zero){
        cout << 0 << endl;
    }
    else{//答えが正の場合
       K-=minus*plus+zero;
       while(R-L>1){
           long long mid=(L+R)/2;
           long long cnt=0;
           int i=0;
           int j=A.size()-1;
           while (i<A.size()&&j>=0)
           {
               long long Y=abs(A[i]*A[j]);
               if(Y<=mid){
                   cnt+=j+1;if(i<=j)cnt--;
                   i++;
               }
               else{
                   j--;
               }
           }
           cnt/=2;
           i=0;j=B.size()-1;
           long long cnt2=0;
           while (i<B.size()&&j>=0)
           {
               long long Y=abs(B[i]*B[j]);
               if(Y<=mid){
                   cnt2+=j+1;if(i<=j)cnt2--;
                   i++;
               }
               else{
                   j--;
               }
           }
           cnt+=(cnt2)/2;
           if(cnt<K){
               L=mid;
           }
           else{
               R=mid;
           }
       }
       cout << R << endl;
    }
    
    return 0;
}

Measured memory_bytes: 2420
h, w = map(int, input().split())

up = (w + 1) // 2
down = w // 2

if (h == 1) or (w == 1):
    print(1)
else:
    if h % 2 == 0:
        print((up + down) * (h // 2))
    else:
        print((up + down) * (h // 2) + up)

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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl




typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6 + 5;





int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);
    ll x;cin>>x;
    ll i=1;
    while (i*(i+1)/2 < x) i++;
    out(i);

    return 0;
}


Measured memory_bytes: 256
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define mfill(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(), v.end()
#define in(x,y,h,w) if(0<=x&&x<h&&0<=y&&y<w)
#define y0 y12345
#define y1 y54321

#ifdef LOCAL
#define debug(...) fprintf(stderr,__VA_ARGS__)
#else
#define debug(...) 42
#endif

using ul = unsigned long;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;using vvint = vector<vector<int>>;
using vll = vector<ll>;using vvll = vector<vector<ll>>;
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(a>b){a=b;return 1;}return 0;}
template <class T>void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}
template <class T>T gcd(T &a, T &b){if(a<b){swap(a,b);} T r = a%b; while(r!=0){a=b;b=r;r=a%b;} return b;}

vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1};

int main() {
	cin.tie(0);
   	ios::sync_with_stdio(false);
	debug("debug test\n");
   	int n,m;cin>>n;
	cout << n*n << endl;
	return 0;
}


Measured memory_bytes: 256
#AGC027A
n,x = list(map(int,input().split()))
a = list(map(int,input().split()))
a.sort()
k = 0
for i in range(n-1):
    if a[i] <= x:
        k += 1
        x -= a[i]
if a[n-1] == x:
    k += 1
print(k)

Measured memory_bytes: 3060
import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String args[]) {
		FastScanner scanner = new FastScanner();
		int n=scanner.nextInt();
		int a[]=new int[n+2];
		a[0]=0;
		a[n+1]=0;
		int sum=0;
		for(int i=1;i<=n;i++) {
			a[i]=scanner.nextInt();
			sum+=Math.abs(a[i-1]-a[i]);
		}
		sum+=Math.abs(a[n]);
		StringBuilder buf=new StringBuilder();
		for(int i=1;i<=n;i++) {
			int ans=0;
			if(a[i-1]<=a[i]&&a[i]<=a[i+1]) {
				ans=sum;
			}else {
				ans=sum;
				ans-=Math.abs(a[i-1]-a[i])+Math.abs(a[i]-a[i+1]);
				ans+=Math.abs(a[i-1]-a[i+1]);
			}
			buf.append(ans+"\n");
		}
		System.out.print(buf);
	}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}


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

using namespace std;

int n, h[100010], inc = 1;

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &h[i]);
        if(i == 0 || h[i] > h[i-1]) h[i]--;
        if(i && h[i] < h[i-1]) inc = 0;
    }
    puts(inc ? "Yes" : "No");
    return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
 
#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i,n) for( int i = 0; i < n; ++i )
#define repi(i,n) for( int i = 1 ; i <= n; ++i )
#define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
#define repf(j,i,n) for( int j = i ; j < n ; ++j )
#define die(s) {std::cout << s << nl;}
#define dier(s) {std::cout << s; return 0;}
#define vi vector<int>
typedef long long ll;
using namespace std;
#define MOD 1000000007
 
 
struct CNK{
    const int mod = 1000000007;
    int mx;
   
private:
    vi fac , ifac;
       
public:
    CNK(int max){
        mx = max;
       
        fac.resize(max + 1);
        ifac.resize(max + 1);
       
        fac[0] = ifac[0] = 1;
       
        for(int i = 1 ; i <= max ; ++i){
            fac[i] = 1LL * fac[i - 1] * i % mod;
            ifac[i] = fpow(fac[i], mod - 2);
        }
    }
   
    int fpow(int a , int b){
        int ans = 1;
        while(b > 0){
            if(b & 1){
                ans = (1LL * ans * a) % mod;
            }
            a = (1LL * a * a) % mod;
            b /= 2;
        }
        return ans;
    }
   
    int cnk(int n , int k){
        assert(mx >= n);
        if (n < 0 || k < 0 || k > n) return 0;
        return 1LL * fac[n] * ifac[n - k] % mod * ifac[k] % mod;
    }
};
 
pii sec(vi & v){
	vi used(sz(v) + 1 , 0);
	rep(i , sz(v)){
		int e = v[i];
		if(used[e])
			return {used[e] - 1 , i};
		used[e] = i + 1;
	}
}
 
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.precision(0); 
 
	int n;
	cin >> n;
	
	vi v(n + 1);
	rep(i , n + 1){
		cin >> v[i];
	}
	
	auto m = sec(v);
	
	ll ans = 0;	
	
	CNK cnk(n + 1);
	
	repi(i , n + 1){
		ans = 0;
		if(i == 1){
			ans = n;
		}else if(i == n + 1){
			ans = 1;
		}else{
			ans = cnk.cnk(n + 1 , i) % MOD;
 
			ans -= cnk.cnk(n - m.se + m.fi , i - 1);
 
			ans %= MOD;
			while(ans < 0)ans += MOD;
		}
		cout << ans << nl;
	}	
 
	return 0;
}

Measured memory_bytes: 2424
#include <iostream>

using namespace std;

int main() {
 	int n;
  	cin >> n;
  	long long arr[n];
  
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
  	long long sum = 0;
  	long long mx = 0;
  	for (int i = 0; i < n; i++) {
     	mx = max(mx, arr[i]);
     	if (arr[i] >= mx) mx = arr[i];
     	else sum += (mx - arr[i]);
    }
  
  	cout << sum << endl;
}

Measured memory_bytes: 5188
#include<stdio.h>
#include<ctype.h>

int main(void)
{
char ddcnt[27]="abcdefghijklmnopqrstuvwxyz";
char ch;
int dcnt[27]={0};


while ( scanf("%c", &ch) != EOF ){
	ch=tolower(ch);
	int num=ch-'a';
    dcnt[num]++;
	// ここで ch がアルファベットなら、数える
}
for(int i=0;i<26;i++)
printf("%c : %d\n",ddcnt[i],dcnt[i]);

return 0;
}

Measured memory_bytes: 972
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w",stdout);
    #endif
     
     int th,ts,ah,as;
     cin>>th>>ts>>ah>>as;

     while(true){

         ah -= ts;

         if(ah<=0){
             cout<<"Yes"<<endl;
             break;
         }


         th-= as;

         if(th<=0){
             cout<<"No"<<endl;
             break;
         }
     }
}

Measured memory_bytes: 3648
#include<bits/stdc++.h>
using namespace std;
int main(){

    string s;cin >> s;

    for( int i = 0 ; i < 1<<3 ; ++i  ){
        string op = "***";
        for( int j = 0 ; j < 3; ++j ){
            if( i&(1<<j) ){
                op[j] = '+';
            }else{
                op[j] = '-';
            }
        }
        int a = s[0]-'0';
        int b = s[1]-'0';
        int c = s[2]-'0';
        int d = s[3]-'0';

        if( op[0] == '-' ){
            b*=-1;
        }
        if( op[1] == '-' ){
            c*=-1;
        }
        if( op[2] == '-' ){
            d*=-1;
        }

        if( a+b+c+d == 7 ){
            cout << a;
            if( b >= 0 ) cout << '+';
            cout << b;
            if( c >= 0 ) cout << '+';
            cout << c;
            if( d >= 0 ) cout << '+';
            cout << d;
            cout << "=7\n";
            return 0;
        }

    }
}


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

using namespace std;

using ll = long long;
using ld = long double;

const int SINF = 1000000009;
const ll INF =  1000000000000000018;

#define sp          cout << " "
#define nl          cout << "\n"
#define all(c)      (c).begin(),(c).end()
#define prec(n)     cout << fixed << setprecision(n)
#define PI          3.14159265358979323846264338327951
#define ifalse      ios_base::sync_with_stdio(false), cin.tie(NULL)

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

template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for(auto x: v) {
    os << x << " ";
  }
  return os;
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}

/********************* MODULAR ARITHMETIC *******************************/

ll MOD = 1000000007;

void mod(ll &a){ if(a > MOD) a %= MOD; }
ll add(ll a, ll b) { a += b; mod(a); return a; }
ll mul(ll a, ll b) { mod(a); mod(b); a *= b; mod(a); return a; }
ll sub(ll a, ll b) { mod(a); mod(b); a -= b; a += MOD; mod(a); return a; }

ll powr(ll a, ll b) {
  ll x = 1LL;
  while(b) {
    if(b & 1) x = mul(x, a);
    a = mul(a, a), b >>= 1;
  }
  return x;
}

ll inv(ll a) { return powr(a, (MOD - 2)); }

/******************************** START *********************************/
const int N = 3005;
vector < ld > v;
ld dp[N][N];

ld func(int n, int k) 
{
    if(n == 0) {
        if(k == 0) {
            return 1;
        }
        return 0;
    }
    if(k < 0) {
        return 0;
    }

    if(dp[n][k] > -1) {
        return dp[n][k];
    }
    ld a = v[n - 1] * func(n - 1, k - 1);
    ld b = (1 - v[n - 1]) * func(n - 1, k);
    return dp[n][k] = a + b;
}

int main()
{
  ifalse;
  int n;
  cin >> n;
  v.resize(n);
  cin >> v;

  int d = (n + 1) / 2;
  memset(dp, -1, sizeof(dp));
  ld res = 0;
  for(int i = n; i >= d; i --) {
      res += func(n, i);
  }
  prec(14);
  cout << res, nl;
  return 0;
}

Measured memory_bytes: 141696
n, k = map(int, raw_input().split())

s = (n - 1) * (n - 2) / 2

if k > s:
    print -1
    exit(0)

ans = []
for i in xrange(2, n + 1):
    ans.append([1, i])

for i in xrange(2, n + 1):
    for j in xrange(2, i):
        if s > k:
            s -= 1
            ans.append([i, j])
print len(ans)
for x in ans:
    print x[0], x[1]


Measured memory_bytes: 3204
#include "bits/stdc++.h"
using namespace std;
int main()
{
    int P;
    int Q;
    int R;

    cin >> P;
    cin >> Q;
    cin >> R;

    int ans1;
    int ans2;
    int ans3;
    int ans4;
    int ans5;
    int ans6;

    ans1 = P + Q;
    ans2 = P + R;
    ans3 = Q + R;
    ans4 = Q + P;
    ans5 = R + Q;
    ans6 = R + P;

    int array[6] = {ans1, ans2, ans3, ans4, ans5, ans6};

    int min;
    min = 1000;
    
    for (int i = 0; i < 6;  ++i){
       if (array[i] < min) 
       min = array[i];
    }

    cout << min << endl;

}

Measured memory_bytes: 256
def main():
    n = int(input())
    s = input()
    white = s.count(".")
    ans, black = white, 0
    for i in range(n):
        if s[i] == ".":
            white -= 1
        else:
            black += 1
        tmp = black + white
        if tmp < ans:
            ans = tmp
    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 9468
#include<stdio.h>
#include<string.h>

int down(const void *a , const void *b){
  return *(int *)b - *(int *)a;
}

int main(void){
  int n , k;
  scanf("%d %d",&n ,&k);

  int h[n];
  for(int i = 0;i < n;i ++){
    scanf("%d",&h[i]);
  }
  qsort(h,n,sizeof(int),down);

  long long ans = 0;

  for(int i = k;i < n;i ++){
    ans += h[i];
  }

  printf("%lld\n",ans);

  return 0;

}

Measured memory_bytes: 1788