akhauriyash/Code-Regression huggingface dataset:
#include<iostream>

using namespace std;

long INFTY = 20000000000;
int cnt;

void merge(long A[], long left, long mid, long right){
    long n1 = mid -left;
    long n2 = right - mid;
    long L[n1 + 1];
    long R[n2 + 1];
    for(int i = 0; i < n1; i++){
        L[i] = A[left + i];
    }
    for(int i = 0; i < n2; i++){
        R[i] = A[mid + i];
    }
    L[n1] = INFTY;
    R[n2] = INFTY;
    int i = 0;
    int j = 0;
    for(long k = left; k < right; k++){
        cnt++;
        if(L[i] <= R[j]){
            A[k] = L[i];
            i = i + 1;
        }else{
            A[k] = R[j];
            j = j + 1;
        }
    }
}

void mergeSort(long A[], long left, long right){
    if(left + 1 < right){
        long mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

int main(){
    int n;
    cin >>n;
    cnt = 0;
    long A[n];
    for(int i = 0; i < n; i++){
        cin >>A[i];
    }
    mergeSort(A, 0, n);

    for(int i = 0; i < n; i++){
        if(i)cout << " ";
        cout << A[i];
    }
    cout << endl;

    cout << cnt << endl;

    return 0;


}


Measured memory_bytes: 10804
n = int(input())
ab=[list(map(int,input().split())) for i in range(n)]

sm=0
bmin=10**10
for itm in ab:
    a,b=itm
    sm+=a
    if a>b:
        bmin=min(bmin,b)
if bmin==10**10:
    print(0)
else:
    print(sm-bmin)

Measured memory_bytes: 48024
N = int(input())
S1 = list(input())
S2 = list(input())
i = 0


if S1[i] != S2[i]:
    res = 6
    i+=2
    rear = 1
else :
    res = 3
    i+=1
    rear = 0

while i != N:
    if S1[i] != S2[i]:
        if rear == 1:
            res *= 3
        else:
            res *= 2
        rear = 1
        i += 2

    else:
        if rear == 0:
            res *= 2
        i += 1
        rear = 0


print(res%1000000007)
        
        


Measured memory_bytes: 3064
#define ENABLE_DEBUG 1
// Kana's kitchen {{{
#include<bits/stdc++.h>
#define ALL(v) std::begin(v),std::end(v)
#define LOOP(k) for(i64 ngtkana_is_a_genius=0; ngtkana_is_a_genius<(i64)k; ngtkana_is_a_genius++)

using i32 = std::int_least32_t;
using i64 = std::int_least64_t;
using u32 = std::uint_least32_t;
using u64 = std::uint_least64_t;
using usize = std::size_t;

template <class T, class U> using pair = std::pair<U, T>;
template <class T> using diag_pair = std::pair<T, T>;
template <class... Args> using tuple = std::tuple<Args...>;
template <class T> using vec = std::vector<T>;
template <class T> using numr = std::numeric_limits<T>;

#ifdef NGTKANA
#include<debug.hpp>
#else
#define DEBUG(...)(void)0
#endif
/*}}}*/

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::setprecision(15) << std::fixed;

    i64 sx, sy, tx, ty;
    std::cin >> sx >> sy >> tx >> ty;
    i64 dx = tx - sx;
    i64 dy = ty - sy;

    std::string s;
    LOOP (dx) s.push_back('R');
    LOOP (dy) s.push_back('U');
    LOOP (dx) s.push_back('L');
    LOOP (dy) s.push_back('D');
    s.push_back('D');
    LOOP (dx+1) s.push_back('R');
    LOOP (dy+1) s.push_back('U');
    s.push_back('L');
    s.push_back('U');
    LOOP (dx+1) s.push_back('L');
    LOOP (dy+1) s.push_back('D');
    s.push_back('R');
    std::cout << s << '\n';
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <queue>
using namespace std;
#define loop(i,N) for(ll (i)=0; (i)<(N);++(i))
struct TEMP_OMAZINAI_ { TEMP_OMAZINAI_() { std::ios::sync_with_stdio(false); std::cin.tie(0); } } TEMP__omazinai_instance__;
#define INF ((1LL<<61) + (1<<29))
#define YesNo(x) {std::cout << ((x)?"Yes":"No") << std::endl;}
#define YESNO(x) {std::cout << ((x)?"YES":"NO") << std::endl;}
#define OddEven(x) {std::cout << ((x)%2?"Odd":"Even") << std::endl;}
#define ODDEVEN(x) {std::cout << ((x)%2?"ODD":"EVEN") << std::endl;}
typedef long long int ll;
ll my_gcd(ll a, ll b) { return b ? my_gcd(b, a%b) : a; };
void TEMP_INPUT_IMPL_() {};
template <class T, class... Args> void TEMP_INPUT_IMPL_(T& dest, Args&... args) { cin >> dest; TEMP_INPUT_IMPL_(args...); }
void TEMP_OUTPUT_IMPL_() {};
template <class T, class... Args> void TEMP_OUTPUT_IMPL_(T dest, Args... args) { cout << dest << (sizeof...(Args) ? " " : ""); TEMP_OUTPUT_IMPL_(args...); }
#define in(type, ...) type __VA_ARGS__; TEMP_INPUT_IMPL_(__VA_ARGS__)
#define in_v(type, name, cnt) vector<type> name(cnt); loop(i, cnt) cin >> name[i];
#define sort_v(v) std::sort(v.begin(), v.end())
#define unique_v(v) v.erase(std::unique(v.begin(), v.end()), v.end()) //必ずソート後に実行
#define out(...) (TEMP_OUTPUT_IMPL_(__VA_ARGS__), std::cout<<std::endl)
#define set_fix(x) ((std::cerr<<std::fixed<<std::setprecision(x)),(std::cout<<std::fixed<<std::setprecision(x)))
//小さい方に寄る
#define space_op(x, y, a, b, c) ([](auto x_, auto y_, auto a_, auto b_, auto c_){if(x_<y_)return a_;if(x_>y_)return c_;return b_;}(x,y,a,b,c))
#define space_out(x, y, a, b, c) out(space_op(x, y, a, b, c))
 
 // ^^^ Template ^^^ //

#include <stack>
int main() {
	in(ll, N);
	in_v(ll, A, N);
	if(N) sort_v(A);
	stack<ll> t;
	for(ll a : A){
		if(!t.empty() && t.top() == a)
			t.pop();
		else
			t.push(a);
	}
	cout << t.size() << endl;
}



Measured memory_bytes: 1920
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
#include <ctime>

using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

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

vector<int> v(4e5, 1);
vector<int> s(4e5, 0);

void f(){
	v[0]=v[1] = 0;
	for(int i = 2; i < 4e5; i++){
		if(v[i]){
			for(int j = i + i; j < 4e5; j+=i){
				v[j] = 0;
			}
		}
		s[i] = s[i-1] + v[i];
	}
}

int main(){
	f();
	int n;
	while(cin>>n && n > 0){
		cout<<s[2*n]-s[n]<<endl;
	}
}

Measured memory_bytes: 5740
#include<stdio.h>
  
int main(){
   int a=0, b=0;      // input,  -1000 <= a, b <= 1000
  
   scanf("%d %d", &a, &b);   // input
 
   if(a<b) printf("a < b\n");
   else if(a>b) printf("a > b\n");
   else printf("a == b\n");
  
   return 0;
}

Measured memory_bytes: 580
#include<iostream>

using namespace std;

int main() {
  int m;

  cin>>m;
  for(int i=0; i<m; i++) {
    int start, years, n;
    cin>>start>>years>>n;
    int maxProfit = 0;
    for(int j=0; j<n; j++) {
      int fukuri, charge;
      double nenri;
      cin>>fukuri>>nenri>>charge;
      //cout<<"limit: "<<fukuri<<" "<<nenri<<" "<<charge<<endl;

      int a=start, b;
      if(fukuri) {
	for(int k=0; k<years; k++) {
	  //cout<<a<<" ";
	  b = (int)(a*nenri); 
	  a = a+b-charge;
	  //cout<<b<<" "<<a<<endl;
	}
      }
      else {
	int totalCharge = 0;
	for(int k=0; k<years; k++) {
	  //cout<<a<<" ";
	  b = (int)(a*nenri);
	  a -= charge;
	  totalCharge += b;
	  //cout<<b<<" "<<a<<" "<<totalCharge<<endl;
	}
	a += totalCharge;
      }
      //cout<<"total: "<<a<<endl;
      if(a>maxProfit) {
	maxProfit = a;
      }
    }
    cout<<maxProfit<<endl;
  }
}

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

typedef std::pair< unsigned int, unsigned int > P;

unsigned int n;

int main() {
    unsigned int id;
    unsigned int m, s;
    while ( std::cin >> n, n ) {
        std::vector< P > vec;
        for (int i=0; i<n; ++i) {
            unsigned int time = 0;
            std::cin >> id;
            for (int j=0; j<4; ++j) {
                std::cin >> m >> s;
                time += m*60 + s;
            }
            vec.push_back( std::make_pair( time, id ) );
        }
        std::sort( vec.begin(), vec.end() );
        // std::vector< P >::iterator itr=vec.begin();
        std::cout << vec[0].second << '\n'
                  << vec[1].second << '\n'
                  << vec[n-2].second
                  << std::endl;
    }
    return 0;
}



Measured memory_bytes: 3108
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cctype>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template<class T>inline T next_num(){
	T i=0;char c;
	while(!isdigit(c=getchar())&&c!='-');
	bool flag=c=='-';
	flag?c=getchar():0;
	while(i=i*10-'0'+c,isdigit(c=getchar()));
	return flag?-i:i;
}
template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;}
template<class T1,class T2>inline void apmin(T1 &a,const T2 &b){if(b<a)a=b;}
const int N=100010;
const lint LINF=0x7f7f7f7f7f7f7f7f;
lint dval[N],ddval[N];
int main(){
	int n=ni,m=ni;
	memset(dval,0,sizeof(dval));
	memset(ddval,0,sizeof(ddval));
	int last=ni;
	for(int i=2;i<=n;i++){
		int cur=ni;
		if(last<cur){
			dval[1]+=cur-last;
			ddval[last+2]--;
			ddval[cur+1]++;
			dval[cur+1]+=cur-last-1;
		}else if(cur<last){
			dval[1]+=cur;
			ddval[2]--;
			ddval[cur+1]++;
			dval[cur+1]+=cur+m-last-1;
			ddval[last+2]--;
		}else assert(false);
		last=cur;
	}
	lint val=0,d=0,ans=LINF;
	for(int i=1;i<=m;i++){
		d+=ddval[i];
		val+=d+dval[i];
		apmin(ans,val);
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 1792
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9+7
result = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            if i < j:
                result += K * (K+1) // 2
            else:
                result += (K - 1) * K // 2
            result %= mod
print(result)


Measured memory_bytes: 3188
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
#define fs first
#define sc second
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
// 多倍長
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using boost::rational;
using boost::multiprecision::cpp_int;

const double PI = acos(-1);
const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

 

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}


// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;


vector<string> split(const string &s, char delim) {
    vector<string> elems;
    string item;
    for (char ch: s) {
        if (ch == delim) {
            if (!item.empty())
                elems.push_back(item);
            item.clear();
        }
        else {
            item += ch;
        }
    }
    if (!item.empty())
        elems.push_back(item);
    return elems;
}


ll f(string s){
    ll sum = 0;
    for(ll i = 0, j = 0; i < sz(s); i = j){
        while(j < sz(s) && s[i] == s[j]) j++;
        sum += (j - i) / 2;
    }
    return sum;
}

int main(void)
{
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    string s;
    ll k;
    cin >> s >> k;
    bool same = true;
    rep(i, sz(s)) if(s[0] != s[i]) same = false;
    if(same){
        print(sz(s) * k / 2);
        return 0;
    }
    ll d1 = f(s);
    ll d2 = f(s + s);

    ll a = d1;
    ll b = d2 - d1;
    print(a + (k - 1) * b);


}


Measured memory_bytes: 3604
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,a,b) for(ll i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()

int main(void){
    ll n, a, b;
    cin >> n >> a >> b;

    if(abs(a - b) % 2 == 0){
        cout << "Alice" << endl;
    }else{
        cout << "Borys" << endl;
    }
    return 0;
}


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

int main() {
  int N,k;
  cin >> N >>k;
  int sum;
  if(k==1){
	sum = N+1;
  }
  else if(k==2||k==3||k==4){
    if (N==1){
      sum = 2;
    }
    else{
    sum = 4 + k*(N-2);
    }
  }
  else if(k==5||k==6||k==7){
    if (N==1){
      sum = 2;
    }
    else if(N==2){
      sum = 4;
    }
    else{
    sum = 8 + k*(N-3);
    }
  }
  else{
    if (N==1){
      sum = 2;
    }
    else if (N==2){
      sum = 4;
    }
    else if (N==3){
      sum = 8;
    }
    else{
    sum = 16 + k*(N-4);
    }
  }
  cout<<sum<<endl;
}


Measured memory_bytes: 3644
#include<iostream>
#include<cstdint>

int const MOD = 1e9+7;
int const MAXK = 1e+5;

template<int64_t Prime>
struct mod_int {
  int64_t v_;
  mod_int(int64_t x=0) : v_(x) { normalize(); }
  void normalize() { ((v_ %= Prime) += Prime) %= Prime; }
  mod_int operator+=(mod_int const& r){ (v_ += r.v_) %= Prime; return *this; }
  mod_int operator-=(mod_int const& r){ (v_ += Prime - r.v_) %= Prime; return *this; }
  mod_int operator*=(mod_int const& r){ (v_ *= r.v_) %= Prime; return *this; }
  mod_int operator+(mod_int const& r) { mod_int res(*this); return res += r; }
  mod_int operator-(mod_int const& r) { mod_int res(*this); return res -= r; }
  mod_int operator*(mod_int const& r) { mod_int res(*this); return res *= r; }
  mod_int pow(int x) const {
    int64_t res = 1, v = v_;
    while(x > 0) {
      if(x&1) (res *= v) %= Prime;
      x/=2; (v *= v) %= Prime;
    }
    return mod_int(res);
  }
  mod_int inv() const { return pow(Prime-2); }
  operator int64_t() const { return v_; }
};
typedef mod_int<MOD> mint;

mint count[MAXK+1];

int main() {
  int N, k_i; std::cin >> N >> k_i;
  mint K(k_i);
  for(int k = k_i; k >= 1; --k) {
    count[k] = mint(k_i/k).pow(N);
    for(int j = k*2; j <= k_i; j += k) count[k] -= count[j];
  }
  mint ans;
  for(int k = 1; k <= k_i; ++k) ans += count[k]*mint(k);
  std::cout << ans << std::endl;
  return 0;
}


Measured memory_bytes: 4412
from collections import deque

N = int(input())
mod = 2
sign = 1
stack = deque()

while True:
    if N % mod != 0:
        stack.append("1")
        N -= mod // 2 * sign
    else:
        stack.append("0")
    mod *= 2
    sign *= -1
    # print("N = " + str(N))
    if N == 0:
        break

for i in range(len(stack)):
    print(stack.pop(), end="")


Measured memory_bytes: 38384
#include <cstdio>

int gcd (int a, int b) {
	if (b == 0) {
		return a;
	}
	else {
		gcd (b, a%b);
	}
}

int main() {
	int a = 0;
	int b = 0;

	scanf("%d %d", &a, &b);
	printf("%d\n", gcd(a, b));
}



Measured memory_bytes: 2716
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const int INF = 1 << 28;
const ll MOD = 1'000'000'007;
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; }

int main() {
    int n;
    cin >> n;
    vl a(n);
    for (auto &aa : a) {
        cin >> aa;
    }
    int ret = 1;
    int up = -1;
    for (int i = 1; i < n; ++i) {
        if (a[i] > a[i - 1] && up != 1) {
            if (up != -1) {
                ret++;
                up = -1;
            } else
                up = 1;
        } else if (a[i] < a[i - 1] && up != 0) {
            if (up != -1) {
                ret++;
                up = -1;
            } else
                up = 0;
        }
    }
    cout << ret << endl;
    return 0;
}

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

int main() {
	int a, b;
	while (cin >> a >> b, a | b) {
		vector<vector<int>>c(a, vector<int>(a, 1 << 29));
		for (int d = 0; d < b; d++) {
			int e, f;
			cin >> e >> f;
			e--; f--;
			c[e][f] = c[f][e] = 1;
		}
		for (int g = 0; g < a; g++) {
			for (int h = 0; h < a; h++) {
				for (int i = 0; i < a; i++) {
					if (h == i)c[h][i] = 0;
					else c[h][i] = (c[h][i]<c[h][g]+c[g][i]) ? (c[h][i]) : (c[h][g] + c[g][i]);
				}
			}
		}
		int s = 0;
		for (int j = 1; j < a; j++) {
			if (c[0][j] < 3)s++;
		}
		cout << s << endl;
	}
}

Measured memory_bytes: 3796
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Node{
    int key;
    Node *prev, *next;
}Node;

Node *nil;

Node *listSearch(int key) {
    Node *cur = nil->next;
    while (cur != nil && cur->key != key) {
        cur = cur->next;
    }
    return cur;
}

void init() {
    nil = new Node;
    nil->next = nil;
    nil->prev = nil;
}

void printList() {
    Node *cur = nil->next;
    int isf = 0;
    while (true) {
        if ( cur == nil) break;
        if ( isf++ > 0 ) printf(" ");
        printf("%d", cur->key);
        cur = cur->next;
    }
    printf("\n");
}

void deleteNode(Node *t) {
    if (t == nil) return;
    t->prev->next = t->next;
    t->next->prev = t->prev;
    free(t);
}

void deleteFirst() {
    deleteNode(nil->next);
}

void deleteLast() {
    deleteNode(nil->prev);
}

void deleteKey(int key) {
    deleteNode(listSearch(key));
}

void insert(int key) {
    Node *x = new Node;
    x->key = key;
    x->next = nil->next;
    nil->next->prev = x;
    nil->next = x;
    x->prev = nil;
}

int main() {
    int key, n, i;
    int size = 0;
    char com[20];
    int np = 0, nd = 0;
    scanf("%d" , &n);
    init();
    for ( i = 0; i < n; i++ ) {
        scanf("%s%d", com, &key);
        if (com[0]=='i'){insert(key); np++; size++;}
        else if ( com[0]=='d'){
            if ( strlen(com) > 6 ) {
                if ( com[6] == 'F') deleteFirst();
                else if (com[6] == 'L') deleteLast();
            } else {
                deleteKey(key); nd++;
            }
            size--;
        }
    }
    printList();
    return 0;
}


Measured memory_bytes: 33936
#include <bits/stdc++.h>
using namespace std;;
#define ll long long
#define rep(i, n) for(int i = 0;i < n;i++)
#define repr(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define INF 1<<30
#define LINF 1LL<<62
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
const int MOD = 1000000007;
 
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n,k;cin >> n >> k;
  vector<ll> p(n);
  vector<ll> cnt(n);
  cnt[0] = 1;
  set<ll> s;
  ll ans= 1;
  rep(i,n){
      cin >> p[i];
      if(i<k){
          s.insert(p[i]);
      }
      if(i>0){
          if(p[i]>p[i-1]){
              cnt[i] = cnt[i-1] + 1;
          }else{
              cnt[i] = 1;
          }
      }
  }
  ll minus = 0;
  rep(i,n){
      if(cnt[i]==k){
          minus++;
      }
  }
  rep(i,n-k){
      if(s.lower_bound(p[i+k])!=s.end() || s.lower_bound(p[i])!=s.begin())ans++;
      s.insert(p[i+k]);
      s.erase(p[i]);
  }
  cout << ans - max(0ll,minus-1)<< endl;

  
}

Measured memory_bytes: 12800
#include <stdio.h>
#include <stdlib.h>
typedef long long ll;

int main()
{
    ll n;
    ll ans = 0;

    scanf("%lld", &n);

    for (ll mod = 1; mod * mod <= n; mod++) {

        ll valid = n / mod;
        ll invalid = n / (mod + 1);

        while (llabs(valid - invalid) > 1) {
            ll mid = (valid + invalid) / 2;

            if (n % mid <= mod) {
                valid = mid;
            }
            else {
                invalid = mid;
            }
        }

        if (n % valid == mod) {
            ans += valid;
        }
    }

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

    return 0;
}


Measured memory_bytes: 128
use proconio::input;
use std::collections::VecDeque;
use std::process::exit;

fn inf() {
    println!("INF");
    exit(0);
}

fn main() {
    input! {
        n: usize, m: usize,
        mut vxxy: [(i64, i64, i64); n],
        mut hxyy: [(i64, i64, i64); m],
    }

    let mut x: Vec<i64> = Vec::new();
    let mut y: Vec<i64> = Vec::new();
    for e in vxxy.iter() {
        x.push(e.0);
        x.push(e.1);
        y.push(e.2);
    }
    for e in hxyy.iter() {
        x.push(e.0);
        y.push(e.1);
        y.push(e.2);
    }
    
    x.push(0);
    y.push(0);
    x.sort_unstable();
    y.sort_unstable();
    x.dedup();
    y.dedup();

    let mut mp = vec![vec![false; y.len()*2+1]; x.len()*2+1];
    for f in vxxy.iter() {
        let a = x.binary_search(&f.0).unwrap();
        let b = x.binary_search(&f.1).unwrap();
        let c = y.binary_search(&f.2).unwrap();
        for i in (a*2+1)..(b*2+2) {
            mp[i][c*2+1] = true;
        }
    }
    for f in hxyy.iter() {
        let c = x.binary_search(&f.0).unwrap();
        let a = y.binary_search(&f.1).unwrap();
        let b = y.binary_search(&f.2).unwrap();
        for i in (a*2+1)..(b*2+2) {
            mp[c*2+1][i] = true;
        }
    }
    let mut ans:i64 = 0;
    let xx = x.binary_search(&0).unwrap();
    let yy = y.binary_search(&0).unwrap();
    let mut q = VecDeque::new();
    mp[xx*2][yy*2] = true;
    q.push_back( [xx,yy] );
    while !q.is_empty() {
        let [xx,yy] = q.pop_front().unwrap();
        if xx == 0 || xx == x.len() || yy == 0 || yy == y.len() {inf()};
        let x2 = xx * 2;
        let y2 = yy * 2;
        mp[x2][y2] = true;
        ans += (x[xx]-x[xx-1])*(y[yy]-y[yy-1]);
        if !mp[x2+1][y2] && !mp[x2+2][y2] {q.push_back([xx+1,yy]) ; mp[x2+2][y2]=true;};
        if !mp[x2-1][y2] && !mp[x2-2][y2] {q.push_back([xx-1,yy]) ; mp[x2-2][y2]=true;};
        if !mp[x2][y2-1] && !mp[x2][y2-2] {q.push_back([xx,yy-1]) ; mp[x2][y2-2]=true;};
        if !mp[x2][y2+1] && !mp[x2][y2+2] {q.push_back([xx,yy+1]) ; mp[x2][y2+2]=true;};
    }
    println!("{}",ans);
}


Measured memory_bytes: 37728
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;

class UF
{
	vector<int> pr;
public:
	UF(int n) {
		pr.resize(n);
		for (int i = 0; i < n; ++i) pr[i] = i;
	}
	int root(int x) {
		if (x == pr[x]) return x;
		else {
			return pr[x] = root(pr[x]);
		}
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	void unite(int x, int y) {
		x = root(pr[x]);
		y = root(pr[y]);
		if (x == y) return;
		pr[x] = y;
	}
};

int main()
{
	int N, M; cin >> N >> M;
	vector<int> P(N);
	for (int i = 0; i < N; ++i) {
		int p; cin >> p; --p;
		P[i] = p;
	}
	auto uf = UF(N);
	for (int i = 0; i < M; ++i) {
		int x, y; cin >> x >> y; --x; --y;
		uf.unite(x, y);
	}
	int ans = 0;
	for (int i = 0; i < N; ++i) {
		if (uf.same(i, P[i])) {
			++ans;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3532
using System;
using System.Text; 
using System.Collections.Generic;
using System.Linq;

public class Remainder
{
    static long N;
    static long K;
    static public void Main ()
    {
        var NK = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        N = NK[0];
        K = NK[1];
        var X = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        
        var LeftLi  = new List<long>();
        var RightLi = new List<long>();
        
        for(int i = 0;i < N;i++){
            if(X[i] >= 0){
                RightLi.Add(X[i]);
            }else{
                LeftLi.Add(-1*X[i]);
            }
        }
        
        var Left  = LeftLi.ToArray();
        var Right = RightLi.ToArray();
        var ans = long.MaxValue;
        
        for(int i = Right.Length;i >= 0;i--){
            
            
            var L = 0L;
            var R = 0L;
            var res = 0L;
            
            if(i >= 1){
                R = Right[i-1];
                
            }
           
            if((K-i) >= 1){
                if((K-i-1) < Left.Length){
                L = Left[Left.Length-1-(K-i-1)];
                res = Math.Min(L,R);
                }else{
                    break;
                }
                
            }
            res += (L+R);
            if(res < ans){
                ans = res;
            }
        }
        
        Console.WriteLine(ans);

    }

}


Measured memory_bytes: 25824
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<queue>
#include<deque>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<sstream>
#include<list>
#include<climits>

using namespace std;
#define rep(i,i0,n) for(int i=(i0);i<(int)(n);++i)
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vii;
typedef long long ll;


int main(){
    int n;cin>>n;
    string ret= n<1000?"ABC":"ABD";
    cout<<ret<<endl;
	return 0;
}



Measured memory_bytes: 256
//Author : pakhandi
//
using namespace std;

#include <bits/stdc++.h>

#define wl(n) while(n--)
#define fl(i,a,b) for(i=a; i<b; i++)
#define rev(i,a,b) for(i=a; i>=b; i--)

#define si(n) scanf("%d", &n)
#define sll(l) scanf("%lld",&l)
#define ss(s) scanf("%s", s)
#define sc(c) scanf("%c", &c)
#define sd(f) scanf("%lf", &f)

#define pi(n) printf("%d\n", n)
#define pll(l) printf("%lld\n", l)
#define ps(s) printf("%s\n", s)
#define pc(c) printf("%c\n", c)
#define pd(f) printf("%lf\n", f)

#define debug(x) cout<<"\n#("<<x<<")#\n"
#define nline printf("\n")

#define mem(a,i) memset(a,i,sizeof(a))

#define MOD 1000000007
#define ll long long int
#define u64 unsigned long long int

#define PB push_back
#define SZ size
#define MP make_pair

int main()
{
	int i, j;

	string str1;

	cin >> str1;

	int limit = str1.SZ();

	fl(i,0,4)
		cout << str1[i];
	cout << " ";
	fl(i,4,limit)
		cout << str1[i];
	nline;

	return 0;
}
/*
	Powered by Buggy Plugin
*/


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

int main()
{
	int n,t=0,s=0;
	cin>>n;
	int a[n+1];
	
	for(int i=1;i<=n;i++)
		cin>>a[i];
		
	for(int i=1;i<=n;i++){
		if(a[i]%2==0)
		{
			s++;
			if(a[i]%3==0||a[i]%5==0)
				t++;
		}
	}
	if(t==s)
		cout<<"APPROVED";
	else
		cout<<"DENIED";
	return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int w = Integer.parseInt(sc.next());
        long[] wary = new long[n];
        long[] vary = new long[n];
        for (int i = 0; i < n; i++) {
            wary[i] = Long.parseLong(sc.next());
            vary[i] = Long.parseLong(sc.next());
        }
        long[][] dp = new long[n + 1][w + 1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= w; j++) {
                if (j < wary[i]) {
                    dp[i + 1][j] = dp[i][j];
                } else {
                    dp[i + 1][j] = Math.max(dp[i][j], dp[i][(int) (j - wary[i])] + vary[i]);
                }
            }
        }
        System.out.println(dp[n][w]);
    }
}

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

int main() {
  int A, B;
  cin >> A >> B;
 
  int X = 0;
  int Y = B - A;
  
  for(int i = 1; i <= Y; i++){
    X += i;
  }
  cout << X - B << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<limits>
#include<iomanip>
#include<cmath>
#include<climits>
using namespace std;

typedef long long ll;
typedef string S;
typedef pair<int,int> P;
typedef vector<int> vi;
const int mod=1e9+7;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll k,a,b;
    cin >> k >> a >> b;
    cout << (a+2<=b&&k-a+1>=0?a+((k-a+1)/2)*(b-a)+(k-a+1)%2:k+1) << endl;
    return 0;
}

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

int main() {
  int A, B, C;
  cin >> A >> B >> C;
  
  if ((A < C && C < B) || (A > C && C > B)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

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

using namespace std;

vector<pair<int, int>> tree[50];
vector<unsigned int> paths(49);

bool dfs_mem(long c, long v, long p, long r) {
  if (c == v)
    return true;
  for (auto t : tree[c]) {
    if (t.second != p and dfs_mem(t.second, v, c, r)) {
      paths[t.first] ^= 1ull << r;
      return true;
    }
  }
  return false;
}

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

  int N; cin>>N;
  for (int i = 0; i < N-1; i++) {
    int a,b; cin>>a>>b;
    a--; b--;
    tree[a].emplace_back(i, b);
    tree[b].emplace_back(i, a);
  }
  int M; cin>>M;
  for (int i = 0; i < M; i++) {
    int u,v; cin>>u>>v;
    u--; v--;
    dfs_mem(u, v, -1, i);
  }

  vector<long> dp(1ull << M);
  dp[0] = 1;
  for (int i = 0; i < N-1; i++) {
    for (int j = (1ull<<M)-1; j >= 0; j--) {
      dp[j | paths[i]] += dp[j];
    }
  }
  cout << dp[(1ull<<M)-1] << endl;

  return 0;
}


Measured memory_bytes: 8448
def solve():
    N, *P = map(int, open(0).read().split())
    
    ans = 0
    tmp = 10 ** 6
    for i in range(N):
        tmp = min(tmp, P[i])
        if P[i] <= tmp:
            ans += 1
    print(ans)


if __name__ == '__main__':
    solve()


Measured memory_bytes: 25116
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
vector <int> a;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    a.resize(n);
    for(auto &i : a){
        cin >> i;
        i--;
    }
    vector <int> p(n);
    for(int i = 0 ; i < n ; i++){
        p[a[i]] = i;
    }
    int ans = 1;
    int cur = 1;
    for(int i = n - 2 ; i >= 0 ; i--){
        if(p[i + 1] < p[i]) cur = 1;
        else cur++;
        ans = max(ans, cur);
    }
    cout << n - ans << endl;
}


Measured memory_bytes: 1792
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <cstdlib>
#define rep(i,x) for(int i=0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 10010010010;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
ll gcd(ll a, ll b) {//a>b
    if (a < b)swap(a, b);
    if (a % b == 0) return b;
    else return gcd(b, a % b);
}

int main() {
    string o, e;
    cin >> o >> e;
    ll s = o.size();
    if (s == e.size()) {
        for (int i = 0; i < s; i++) {
            cout << o[i] << e[i];
        }
        cout << endl;
    }
    else {
        for (int i = 0; i < s - 1; i++) {
            cout << o[i] << e[i];
        }
        cout << o[s - 1] << endl;
    }
}


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

int main(){
  int n, m, a;
  while (cin >> n >> m >> a, a||n||m) {
    int h, p, q;
    int amida[n][1001];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 1001; ++j) {
	amida[i][j] = -1;
      }
    }
    for (int i = 0; i < m; ++i) {
      cin >> h >> p >> q;
      amida[p-1][h] = q-1;
      amida[q-1][h] = p-1;
    }
    h = 1000;
    --a;
    while (h > 0) {
      if (amida[a][h] > -1) {
	a = amida[a][h];
	--h;
      }else --h;
    }
    cout << a+1 << endl;
  }
  return 0;
}

Measured memory_bytes: 3460
// 2020-07-18 10:56:41
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }

const ll INF = 1e18;
void answer() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int k = n%2+1;
  vector<ll> dp(k+1, -INF);
  dp[0] = 0;
  rep(i, n) {
    vector<ll> p(k+1, -INF);
    swap(p, dp);
    repc(j, k) {
      if(j<k) chmax(dp[j+1], p[j]);
      if((i+j)%2) {
        chmax(dp[j], p[j]);
      } else {
        chmax(dp[j], p[j] + a[i]);
      }
    }
  }
  cout << dp[k] << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

struct ball { ll x; ll y; };

int main() {
  int N; cin >> N;
  ball b[N]; rep(i,N) { ll x, y; cin >> x >> y; if (x>y) swap(x,y); b[i] = {x,y}; }  
  if (N==1) { cout << 0 << endl; return 0; }

  sort(b,b+N,[](const ball &x, const ball &y){ return x.x<y.x; });
  sort(b+1,b+N,[](const ball &x, const ball &y){ return x.y<y.y; });
  sort(b+1,b+N-1,[](const ball &x, const ball &y){ return x.x<y.x||(x.x==y.x&&x.y<y.y); });
  // rep(i,N) cout << b[i].x << " " << b[i].y << endl;

  ll xmax = 0, xmin = INF, ymax = 0, ymin = INF;
  rep(i,N) { 
    xmax = max(xmax,b[i].x); xmin = min(xmin,b[i].x);
    ymax = max(ymax,b[i].y); ymin = min(ymin,b[i].y);
  }
  ll r1 = (xmax-xmin)*(ymax-ymin);
  // cout << r1 << endl;

  ll xdiff = b[N-1].y-b[0].x, ydiff = INF;
  multiset<ll> yset;
  yset.insert(b[0].y); yset.insert(b[N-1].x);
  repr(i,1,N-1) yset.insert(b[i].x);
  ydiff = min(ydiff,*(yset.rbegin())-*(yset.begin()));
  repr(i,1,N-1) {
    yset.erase(yset.find(b[i].x)); yset.insert(b[i].y);
    ydiff = min(ydiff,*(yset.rbegin())-*(yset.begin()));
  }
  ll r2 = xdiff * ydiff;
  // cout << r1 << endl;

  ll result = min(r1, r2);
  cout << result << endl;
  return 0;
}

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

using namespace std;

#define ll long long
#define endl '\n'
#define pb push_back

const ll mod = 1e9+7;
const ll m = 1e9+7;

ll expo_mod(ll x, ll n) //Exponenciação rápida com módulo
{
    if (n == 0)
        return 1;

    ll t = expo_mod(x, n/2); //O resultado já é em módulo

    ll result = (t * t)%mod;

    if (n%2 == 1) //Se n é ímpar
        return (result * (x%mod))%mod; //(a * b) mod m = (a mod m * b mod m) mod m

    return result;
}

int N, T, dp[3005][3005];

vector< pair<int, int> > v;

int knapsack(int i, int t)
{
    if (i >= N) //Não há mais itens
        return 0;

    if (2 * t > 2 * T - 1) //Depois de T - 0.5 min não pode pedir mais
        return 0;

    if (dp[i][t] != -1)
        return dp[i][t];

    int tempo = v[i].first;
    int happiness = v[i].second;

    int take = knapsack(i+1, t+tempo) + happiness;
    int notake = knapsack(i+1, t);

    return dp[i][t] = max(take, notake);
}

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

    memset(dp, -1, sizeof dp);

    for (int i = 0; i < N; i++)
    {
        int a, b; cin >> a >> b;
        v.push_back( {a, b} );
    }

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

    cout << knapsack(0, 0) << endl;

    return 0;
}


Measured memory_bytes: 35712
a = int(input())
b = int(input())
c = int(input())
x = int(input())
ans=0
for i in range(a+1):
    for j in range(b+1):
        for k in range(c+1):
            if (i*500)+(j*100)+(k*50)==x:
                ans+=1
print(ans) 

Measured memory_bytes: 3060
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repo(i,N) for(int i = 1; i < (N); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
//#define num 1000000007
#define pi acos(-1.0)
//cout << fixed << setprecision (20);   小数点以下2０桁まで
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'0'+=16;        で大文字に
//'大文字'+=32;    で小文字に
//s[i]-'0'でchar文字→int数に;
//string s = to_string(int数);
//int n = stoi(string文字列)


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

  
  cout << (n==1||n==2||n==3||n==5||n==6||n==9||n==10||n==13||n==17?"No":"Yes") << endl;

}

Measured memory_bytes: 256
import itertools

def countstrs(s):
    return [(k, len(list(g))) for k, g in itertools.groupby(s)]

n, k = map(int, input().split())
s = input()
c = countstrs(s)

total = [0] * (len(c) + 1)
cur = 0
for i in range(len(c)):
    cur += c[i][1]
    total[i+1] = cur


res = 0
l = 0
r = (2*k) + 0
while l < len(c):
    if c[l][0] == "0":
        rr = r
    else:
        rr = r+1
    if rr > len(c):
        rr = len(c)
    res = max(res, total[rr] - total[l])
    l += 1
    r += 1

print(res)

Measured memory_bytes: 87252
#include<stdio.h>
#include<stdlib.h>
#define NIL NULL

typedef struct node{
  struct node *left;
  struct node *right;
  struct node *parent;
  int key;
}*Node;

Node root;

/*Node treeMinimum(Node x){

}
*/
Node treeSearch(Node u, int k){
  while(u!=NIL && k!=u->key){
    if(k < (u->key)){
      u = u->left;
    }
    else{
      u = u->right;
    }
  }
  return u;
}
/*
    
Node treeSuccessor(Node x){


}

void treeDelete(Node z){
  Node y; // node to be deleted
  Node x; // child of y



}
*/
void insert(int k){
  Node y = NIL;
  Node x = root;
  Node z;
  z = malloc(sizeof(struct node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;
  while(x!=NIL){
    y = x;
    if(z->key<x->key){
      x = x->left;
    }
    else{
      x =x->right;
    }
  }
  z->parent= y;
  if(y==NIL){
    root = z;
  }
  else if(z->key<y->key){
    y->left =z;
  }
  else{
    y->right =z;
  }
  
  }

void inorder(Node u){
  if(u != NIL){
    inorder(u->left);
    printf(" %d",u->key);
    inorder(u->right);
  }
}

void preorder(Node u){
  if(u!=NIL){
    printf(" %d",u->key);
    preorder(u->left);
    preorder(u->right);
  }
}


int main(){
  int n, i, x;
  char com[20];
  scanf("%d", &n);
  
  for ( i = 0; i < n; i++ ){
    scanf("%s", com);
    if ( com[0] == 'f' ){
      scanf("%d", &x);
      Node t = treeSearch(root, x);
      if ( t != NIL ) printf("yes\n");
      else printf("no\n");
    } else if ( com[0] == 'i' ){
      scanf("%d", &x);
      insert(x);
  
      
    } else if ( com[0] == 'p' ){
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }/* else if ( com[0] == 'd' ){
        scanf("%d", &x);
        treeDelete(treeSearch(root, x));
        }*/
  }
  
  return 0;
}

Measured memory_bytes: 19364
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (int64_t)(n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)

int D;
vector<int> c(26);
vector<vector<int>> s;

int64_t calc_score(const vector<int>& t) {
  int64_t result = 0;
  vector<int> last(26, -1);
  rep(i, D) {
    const int j = t[i];
    result += s[i][j];
    last[j] = i;
    rep(k, 26) { result -= c[k] * (i - last[k]); }
  }
  return result;
}

int main() {
  cin >> D;

  rep(i, 26) { cin >> c[i]; }

  s.resize(D, vector<int>(26));
  rep(i, D) rep(j, 26) { cin >> s[i][j]; }

  vector<vector<int>> last;
  vector<int> parent;
  vector<int> kind;

  using P = pair<int64_t, int>;
  priority_queue<P, vector<P>, greater<P>> q;

  last.push_back(vector<int>(26, -1));
  parent.push_back(-1);
  kind.push_back(-1);
  q.push(P(0, 0));

  const int width = 500;
  rep(i, D) {
    priority_queue<P, vector<P>, greater<P>> nxt;

    while (!q.empty()) {
      int64_t z;
      int idx;
      tie(z, idx) = q.top();
      q.pop();

      int64_t penalty = 0;
      rep(j, 26) { penalty += c[j] * (i - last[idx][j]); }

      rep(j, 26) {
        int nidx = last.size();
        int64_t w = z + s[i][j] - penalty + c[j] * (i - last[idx][j]);
        nxt.push(P(w, nidx));
        if (nxt.size() > width) {
          nxt.pop();
        }

        last.push_back(last[idx]);
        last[nidx][j] = i;
        parent.push_back(idx);
        kind.push_back(j);
      }
    }

    q = move(nxt);
  }

  while (q.size() > 1) {
    q.pop();
  }

  int64_t score;
  int idx;
  tie(score, idx) = q.top();
  vector<int> sln;
  while (idx != 0) {
    sln.push_back(kind[idx]);
    idx = parent[idx];
  }
  rrep(i, D) { cout << sln[i] + 1 << "\n"; }
  cerr << score << "\n";

  return 0;
}

Measured memory_bytes: 691108
// https://atcoder.jp/contests/dp/tasks/dp_g
#include<iostream>
#include <cstdlib>
#include <algorithm>
#include <limits.h>
#include <vector>

#define MOD 1000000007

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	int n,m;
	cin >> n >> m;
	int dp[n][m];
	char arr[n][m];
	for(int i=0;i<n;i++)
	{
		string s;
		cin >> s;
		for(int j=0;j<m;j++)
			arr[i][j]=s[j];
	}
	dp[0][0] = 1;	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(arr[i][j]=='#')
			{
				dp[i][j]=0;
				continue;
			}
			if(j==0 && i==0)
				continue;
			else if(i==0)
				dp[i][j]=dp[i][j-1];
			else if(j==0)
				dp[i][j]=dp[i-1][j];
			else
				dp[i][j] = (dp[i-1][j]+dp[i][j-1])%MOD;
		}
	}
	cout << dp[n-1][m-1] << endl;	
}

Measured memory_bytes: 8016
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(void)
{
	int l,N,i;
	int max=0;
	int min = 0;
	cin >> N;
	for (i=0;i<N;i++)
	{
		cin >> l; 

		if (max < l)
		{
			min += max;
			max = l;

		}
		else if(max>=l||max!=max)
		{
			min += l;
		}

	}

	if (max < min)
	{
		cout << "Yes";
	}
	else
	{
		cout << "No";
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
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; }
//(a+b-1)/b

signed main(){
	int n,a; cin>>n>>a;
	vl vec(n);
	rep(i,0,n){
		ll k; cin>>k;
		vec[i]=a-k;
	}
	vvl dp(n+10,vl(40000));
	dp[0][10000]=1;
	rep(i,0,n){
		rep(j,0,20000){
			dp[i+1][j]=dp[i][j];
			ll k=j-vec[i];
			if(k>=0 && k<40000)dp[i+1][j]+=dp[i][k];
		}
	}
	/*rep(i,0,n+1){
		rep(j,0,40000){
			if(dp[i][j]!=0)cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
		}
	}*/
	cout<<dp[n][10000]-1<<endl;
	return 0;
}

Measured memory_bytes: 22516
//#include "std_lib_facilities.h"
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout << '>' << #x << ':' << x << endl;

int main() {
	int n;
	scanf("%d", &n);
	int arr[n + 1];
	int vis[n + 1] = { 0 };
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
	int ans = 0, pos = 1;
	while (pos != 2) {
		if (vis[pos])
			return printf("-1\n"), 0;
		vis[pos] = 1;
		pos = arr[pos];
		ans++;

	}

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

}



Measured memory_bytes: 4592
mylist= []
N, K = map(int,input().split())
for i in range(K):
    d = int(input())
    array = list(map(int, input().strip().split()))
    mylist.extend(array)

a = 0
for l in range(1, N+1):
    if l not in mylist:
        a = a + 1

print(a)

Measured memory_bytes: 9200
#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std;
//using namespace atcoder;
using usize = ::std::size_t;
using u64 = ::std::int_least64_t;
//using u64 = long long;
static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;




int main(int argc, char *argv[])
{
    
    
    cin.tie(0) ;
    ios::sync_with_stdio(false) ;
    
    u64 N;
    cin >> N;
    
    cout << N*(N-1)/2 << endl;
    
    return 0;
    
}


Measured memory_bytes: 3600
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)

# ----------

INF = float("inf")
MOD = 10 ** 9 + 7
# ----------

N, M, X = map(int, input().split())
books = []
for i in range(N):
    books.append(list(map(int, input().split())))

ans = INF
for s in (range(1 << N)):
    A = defaultdict(int)
    cost = 0
    for i in range(N):
        if s >> i & 1:
            book = books[i]
            cost += book[0]
            for i in range(1, M + 1):
                A[i] += book[i]
    ok = True
    for i in range(1, M + 1):
        if A[i] < X:
            ok = False
            break
    if ok:
        ans = min(ans, cost)

if ans == INF:
    print(-1)
else:
    print(ans)

Measured memory_bytes: 9472
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N, x, d = map(int, input().split())

    if d == 0:
        if x:
            print(N+1)
        else:
            print(1)
        exit()

    if d < 0:
        x = x + d * (N-1)
        d *= -1
    dic = defaultdict(list)

    for i in range(N+1):
        mod = (x * i) % d
        st = (i * (i+1)) // 2
        ed = (i * (N + N-i+1)) // 2
        st = st * d + (x-d) * i
        ed = ed * d + (x-d) * i
        dic[mod].append((st, ed))
    ans = 0
    for mod in dic:
        L = dic[mod]
        L.sort(key=lambda t: t[0])
        ed_prev = L[0][0] - 1
        for st, ed in L:
            if st > ed_prev:
                ans += (ed - st) // d + 1
                ed_prev = ed
            elif ed > ed_prev:
                ans += (ed - ed_prev) // d
                ed_prev = ed
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 83868