akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define SORT(v,n) sort(v,v+n)
#define rep(i,n) FOR(i,0,n)
#define EPS (1e-7)
#define INF (1e9)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef priority_queue<int> PQI;
typedef priority_queue<int,vector<int>,greater<int> > PQSI;
typedef priority_queue<ll> PQL;
typedef priority_queue<ll,vector<ll>,greater<ll> > PQSL;

const int MOD = 1000000007;
struct mint { int n; mint(int n_ = 0) : n(n_) {} };
mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; }
mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
istream &operator>>(istream &i, mint &a) { return i >> a.n; }
ostream &operator<<(ostream &o, mint a) { return o << a.n; }

vector<mint> F_{1, 1}, R_{1, 1}, I_{0, 1};
void check_fact(int n) {
  for (int i = I_.size(); i <= n; i++) {
    I_.push_back(I_[MOD % i] * (MOD - MOD / i));
    F_.push_back(F_[i - 1] * i);
    R_.push_back(R_[i - 1] * I_[i]);
  }
}

mint I(int n) { check_fact(abs(n)); return n >= 0 ? I_[n] : -I_[n]; }
mint F(int n) { check_fact(n); return n < 0 ? 0 : F_[n]; }
mint R(int n) { check_fact(n); return n < 0 ? 0 : R_[n]; }
mint C(int n, int r) { return F(n) * R(n - r) * R(r); }
mint P(int n, int r) { return F(n) * R(n - r); }
mint H(int n, int r) { return n == 0 ? (r == 0) : C(n + r - 1, r); }

template<typename T>
T gcd(T x, T y){
    if (y == 0) return x;
    else return gcd(y, x % y);
}

template<typename T>
T lcm(T x, T y){
    return (x / gcd(x, y)) * y;
}

template<typename T>
T ceil(T x, T y){
    return (x + (y - 1)) / y;
}

int N;
int A[100000];
int main(void){
    cin >> N;
    rep(i,N) scanf("%d",&A[i]);
    vector<int> v;
    v.push_back(A[0]);
    for(int i=1;i<N;i++){
        int index = lower_bound(v.begin(), v.end(), A[i]) - v.begin() - 1;
        if(index == -1) {
            v.insert(v.begin(), A[i]);
        }else if(index == v.size() - 1 and A[i] == v[index]){
            v.push_back(A[i]);
        }
        else{
            v[index] = A[i];
        }
        /*
        int tmp = 0;
        while(true){
            if(tmp >= v.size()){
                v.push_back(A[i]);
                break;
            }
            if(v[tmp] < A[i]){
                v[tmp] = A[i];
                break;
            }
            tmp++;
        }
        */
    }
    cout << v.size() << endl;

    return 0;
}











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

int N,M,a[59],b[59],c[59],d[59];

int main(){
	cin>>N>>M;
	for(int i=1;i<=N;i++) cin>>a[i]>>b[i];
	for(int j=1;j<=M;j++) cin>>c[j]>>d[j];
	for(int i=1;i<=N;i++){
		int minx=1000000000,maxid=0;
		for(int j=1;j<=M;j++){
			int sum=abs(a[i]-c[j])+abs(b[i]-d[j]);
			if(minx>sum){minx=sum;maxid=j;}
		}
		cout<<maxid<<endl;
	}
	return 0;
}


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


using namespace std;

int main(int argc, char *argv[])
{
    cout<<setprecision(6)<<fixed;
	double x[1002],y[1002],num=0;
	int cant;
	cin>>cant;
	for(int i=0;i<cant;i++)
	{
		cin>>x[i];
	}
	for(int i=0;i<cant;i++)
	{
		cin>>y[i];
	}
	
	for(int i=0;i<cant;i++)//1
	{
		num += abs(x[i]-y[i]);
	}	
	//printf("%.6lf\n",num);
	cout<< num<<endl;
    num=0;
	
	for(int i=0;i<cant;i++)//2
	{
		num += pow(x[i]-y[i],2);
	}	
	//printf("%.6lf\n",sqrt(num));
	cout << sqrt(num)<<endl;
	num=0;
	
	for(int i=0;i<cant;i++)//3
	{
		num += pow(abs(x[i]-y[i]),3);
	}		
	//printf("%.6lf\n",pow(num,(double)1/3));
	cout<< pow(num,(double)1/3)<<endl;
    num=0;
	
	double max=abs(x[0]-y[0]);
	for(int i=1;i<cant;i++)//oo
	{
		if(abs(x[i]-y[i]) > max)
		max=abs(x[i]-y[i]);
	}	
	//printf("%.6lf\n",max);
	cout<< max<<endl;
	num=0;
//	system("pause");
	return 0;
}



Measured memory_bytes: 3500
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF 1<<30
#define LINF 1LL<<60
 
class WarshallFloyd{
public:
    ll V;
    vector<vector<ll>> d;
    WarshallFloyd(ll V):V(V){
        d.assign(V, vector<ll>(V,LINF));
        for(int i = 0; i < V;i++){
            d[i][i] = 0;
        }
    }
     
    void add_edge(ll u,ll v,ll cost,bool undirected = false){
        d[u][v] = min(d[u][v],cost);
        if(undirected){
            d[v][u] = min(d[v][u],cost);
        }
    }
     
    void calc(){
        for (int i = 0; i < V; i++)      // ?????±????????????
            for (int j = 0; j < V; j++)    // ????§???????
                for (int k = 0; k < V; k++)  // ??????
                {
                     
                    if(d[j][i] == LINF || d[i][k] == LINF)continue;
                    d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
                }
    }
     
    inline ll get_dist(ll s,ll t) {return d[s][t];} // // ????????? := d[i][i] < 0
};
 
int main(){
    ll V,E; cin >> V >> E;
    WarshallFloyd WF(V);
    for(int i = 0; i < E;i++){
        ll s,t,d; cin >> s >> t >> d;
        WF.add_edge(s, t, d);
    }
    WF.calc();
     
    for(int i = 0; i < V;i++){
        if(WF.get_dist(i, i) < 0){
            cout << "NEGATIVE CYCLE" << endl; return 0;
        }
    }
     
    vector<ll> ans(V);
    for(int i = 0; i < V;i++){
        for(int j = 0; j < V;j++){
            if(WF.get_dist(i, j) == LINF){
                cout << "INF";
            }else{
               cout << WF.get_dist(i, j);
            }
            if(j != V - 1)cout << " ";
        }
        cout << endl;
    }
    return 0;
}


Measured memory_bytes: 3232
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[10010];
int main(){
    int n;
    string s;
    cin>>n>>s;
    int sum=0;
    for(int i=1;i<n;i++)if(s[i]=='E')sum++;
    int ans=sum;
    for(int i=1;i<n;i++){
        if(s[i]=='E')sum--;
        if(s[i-1]=='W')sum++;
        if(ans>sum)ans=sum;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 900
from collections import*
import sys
input=sys.stdin.readline

h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#")for x in range(h)]+[["#"]*(w+2)]

stack=deque()
for y in range(1,h+1):
    for x in range(1,w+1):
        if grid[y][x]=="#":
            grid[y][x]=0
            stack.append((y,x))

for i in range(h*w):
    tmp=deque()
    while stack:
        my,mx=stack.pop()
        for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
            if grid[my+y][mx+x]==".":
                grid[my+y][mx+x]=i+1
                tmp.append([my+y,mx+x])
    stack=tmp
    if not tmp:
        print(i)
        break

Measured memory_bytes: 164872
//#include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
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;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin>>s;
    string ans="";
    for(int i=0;i<s.size();i+=2){
        ans+=s[i];
    }
    cout<<ans<<"\n";
}


Measured memory_bytes: 592
#include<bits/stdc++.h>
using namespace std;
int main(){
  int h,w,n;
  cin >> h >> w >> n;
  vector<int> a(n),c(h*w);
  for(int i=0;i<n;i++){
    cin >> a[i];
  }
  int cnt=0;
  for(int i=0;i<n;i++){
    for(int j=1;j<=a[i];j++){
      c[cnt]=i+1;
      cnt++;
    }
  }
  for(int i=0;i<h;i++){
    if(i%2==0){
      for(int j=0;j<w;j++){
        cout << c[w*i+j] << " ";
      }
    }
    else{
      for(int j=0;j<w;j++){
        cout << c[w*(i+1)-j-1] << " ";
      }
    }
    cout << endl;
  }
}

Measured memory_bytes: 384
a = gets.to_i

ary = []
a.times.each do |i|
  ary << i + 1
end

cnt = 0
ary.combination(2) do |num|
  cnt += 1 if num[0].odd? && num[1].even?
  cnt += 1 if num[1].odd? && num[0].even?
end
puts cnt

Measured memory_bytes: 1788
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	sc.Split(bufio.ScanWords)

	x := nextInt()
	a := nextInt()

	if x < a {
		fmt.Println(0)
	} else {
		fmt.Println(10)
	}
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(false),cin.tie(0);
#define F first
#define S second
using namespace std;
typedef long long ll;
ll a,b,c,sum,t;
const ll maxn=200005;
const ll mod=1e9+7;
typedef pair<ll,ll> pii;
ll num[105];
ll n,k,x;

int main()
{
    //IOS
    while(cin>>n>>x)
    {
        rep(i,n)
        cin>>num[i];
        ll sum=0;
        ll pos=n+1;
        rep(i,n)
        {
            sum=sum+num[i];
            //cout<<i<<" "<<sum<<"\n";
            if(sum>x) {pos=i+1;break;}
        }
        cout<<pos<<"\n";
    }


}


Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

typedef long long int64;

const int MAX_N = 2000 + 1;
const int64 MOD = (int64)(1e9 + 7);

int N, C, target, ord;

int parent[MAX_N+1];
vector<int> graph[MAX_N+1];

int V[MAX_N+1];
int S[MAX_N+1];
int64 T[MAX_N+1];

int64 dp[MAX_N+1][MAX_N+1];
bool visited[MAX_N + 1];
int64 comb[MAX_N+1][MAX_N+1];

void init(){
	scanf("%d%d%d%d", &N, &C, &target, &ord);
	ord = N - ord + 1;
	++N;
	parent[0] = -1;
	for(int i=0; i<C; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		parent[a] = b;
		graph[b].push_back(a);
	}

	for(int i=0; i<N; i++)if(parent[i] == 0){
		graph[0].push_back(i);
	}

	for(int i=0; i<=N; i++){
		comb[i][i] = comb[i][0] = 1;
		for(int j=1; j<i; j++){
			comb[i][j] = ( comb[i-1][j-1] + comb[i-1][j] ) % MOD;
		}
	}
}

void calc(int v){
	T[v] = S[v] = 1;
	for(int i=0; i<(int)graph[v].size(); ++i){
		const int u = graph[v][i];
		calc(u);
		S[v] += S[u];
	}

	int total = S[v] - 1;
	for(int i=0; i<(int)graph[v].size(); i++){
		const int u = graph[v][i];
		T[v] = T[v] * comb[total][S[u]] % MOD * T[u] % MOD;
		total -= S[u];
	}
}

int solve(){
	calc(0);
	if(S[0] != N){
		return 0;
	}

	const int dummy = N;
	S[dummy] = 0;
	parent[dummy] = target;

	int depth = 0;
	for(int v = target; v!=-1 ;V[depth++]=v, v=parent[v]);
	reverse(V, V+depth);
	V[depth] = dummy;

	for(int i=0; i<depth; i++){
		int cur = V[i], nxt = V[i+1], all = N - S[nxt];
		int64 sum = 0, prod = 1;
		int total = S[cur] - S[nxt] - 1;
		for(int j=0; j<(int)graph[cur].size(); j++){
			const int u = graph[cur][j];
			if(nxt != u){
				prod = prod * T[u] % MOD * comb[total][S[u]] % MOD;
				total -= S[u];
			}
		}
		if(i == 0){
			dp[0][0] = prod;
			continue;
		}
		for(int j=1; j<N; j++){
			sum = (sum + dp[i-1][j-1]) % MOD;
			int rem = all - j - 1, child = S[cur] - S[nxt] - 1;
			if(rem >= child && rem >= 0 && child >= 0){
				dp[i][j] = sum * comb[rem][child] % MOD * prod % MOD;
			}
		}
	}

	return (int)dp[depth-1][ord];
}

int main(){
	init();
	printf("%d\n", solve());

	return 0;
}

Measured memory_bytes: 52000
import java.util.*;

class Main{
    public static final int MOD = 1000000007;

    Main(){
        int N, A, B;

        Scanner SC = new Scanner(System.in);
        N = SC.nextInt();
        A = SC.nextInt();
        B = SC.nextInt();

        long R = modpow(2, N)-1;
        R = (R - C(N,A))%MOD;
        if(R < 0) R += MOD;
        R = (R - C(N,B))%MOD;
        if(R < 0) R += MOD;

        System.out.println(R);
    }

    public static void main(String[] args){
        Main main = new Main();
    }

    long modpow(long a, long b){
        long R = 1L;

        a %= MOD;
        while(b > 0){
            if( (b & 1) == 1)
                R = (R * a) % MOD;
            a = (a * a) % MOD;
            b = b >> 1;
        }
        return R;
    }

    long C(long n, long r){
        if(n < r) return 0;
        if(r == 0) return 1;;

        long numer = 1, denom = 1;
        for(long i=n-r+1;i<=n;i++){
            numer = numer * (i % MOD) % MOD;
        }

        for(long i=1;i<=r;i++){
            denom = denom * modinv(i % MOD) % MOD;
        }

        return numer * denom % MOD;
    }

    long modinv(long a){
        return modpow(a, MOD-2);
    }
}


Measured memory_bytes: 23252
#include <bits/stdc++.h>
#define int long long
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
signed main(){
	int n;
	cin>>n;
	int a[n];
	r(i,n)cin>>a[i];
	int s=0;
	r(i,n){
		s+=a[i]-1;
	}
	cout<<s<<endl;
}

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

using namespace std;

#define REP(i,n) for(int i=0;i<n;i++)
#define lli long long int
#define NUM 1000000000


map<lli, int> cnt;
map<int, lli> ans;

int main(){

	//cout << endl;

	lli h, w, n;
	cin >> h >> w >> n;

	REP(i,n){
		lli a, b;
		cin >> a >> b;
		REP(j,3){
			REP(k,3){
				if ((a + j - 2) <= 0 || (b + k - 2) <= 0 || (a + j - 2) >= h || (b + k - 2) >= w){
					continue;
				}
				//cout <<"x="<<(a + j - 2) << "y=" << b + k - 2 << endl;
				cnt[(a + j - 2)*NUM + (b + k - 2)]++;
			}
		}
	}

	for (auto x : cnt){
		//cout << x.first << " " << x.second << endl;
		if (x.first / NUM + 2 <= h && x.first%NUM + 2 <= w){
			ans[x.second]++;
		}
	}
	lli ans0 = (h - 2) * (w - 2);
	for (auto x : ans){
		ans0 -= x.second;
	}
	cout << ans0 << endl;
	
	for (int i = 1; i < 10; i++){
		cout << ans[i] << endl;
	}

	cin >> h;

	return 0;

}


Measured memory_bytes: 56448
n = int(input())
list1 = [int(x) for x in input().split()]
list2 = list(reversed(sorted(list1)))

fflag = 0
ans = []
for i in range(len(list2) - 1):
  if fflag ==0:
    if list2[i] == list2[i + 1]:
      ans.append(list2[i])
      fflag = 1
  elif fflag == 1:
    fflag = 0
    continue
  if len(ans) == 2:
    break

if len(ans) == 2:
  print(ans[0] * ans[1])
else:
  print(0)
  

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

using namespace std;
typedef long long ll;

int main(){
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;

    vector<int> nums;

    int now=1; //今見ている数
    int cnt=0; //nowがいくつ並んでいるのか

    for (int i = 0; i < n; i++)
    {
        if(s[i]==(char)('0'+now)) cnt++;
        //0と1が切り替わる
        else{
            nums.push_back(cnt);
            now = 1-now;
            cnt = 1;
        }
    }
    if(cnt!=0) nums.push_back(cnt);

    //1_0_1_0_1の配列がいい
    //1_0_1_0だったら配列を1増やす
    if(nums.size() % 2 == 0) nums.push_back(0);

    //足す項の数
    int add = 2*k+1;

    //累積和
    vector<int> sum(nums.size()+1, 0);
    for (int i = 0; i < nums.size(); i++)
    {
        sum[i+1] = sum[i]+nums[i];
    }
    
    int ans = 0;
    //1_0_1_0....なので、1だけをみる
    for (int i = 0; i < nums.size(); i+=2)
    {
        //[left, right)
        int left = i;
        int right = min(i+add, (int)nums.size());
        int tmp = sum[right] - sum[left];
        ans = max(tmp, ans);
    }
    
    cout << ans << endl;

}

Measured memory_bytes: 1272
use std::io::*;
use std::str::FromStr;

fn main() {
    let cin = stdin();
    let mut sc = Scanner::new(cin.lock());

    loop {
        let mut v = sc.vec::<u32>(2);
        v.sort();
        if v[1] == 0 {
            break;
        }
        println!("{} {}", v[0], v[1]);
    }
}

/* ========== Scanner ========== */

struct Scanner<R: Read> {
    reader: R,
}

#[allow(dead_code)]
impl<R: Read> Scanner<R> {
    fn new(reader: R) -> Scanner<R> {
        Scanner { reader: reader }
    }

    fn read<T: FromStr>(&mut self) -> Option<T> {
        let token = self
            .reader
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            token.parse::<T>().ok()
        }
    }

    fn next<T: FromStr>(&mut self) -> T {
        if let Some(s) = self.read() {
            s
        } else {
            writeln!(stderr(), "Terminated with EOF").unwrap();
            std::process::exit(0);
        }
    }

    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.next()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.next::<String>().chars().collect()
    }
}



Measured memory_bytes: 5048
import sys


def calc(n):
    i = 1
    sm = 0
    st = set()
    if n <= 2:
        st.add(n)
        return st
    while sm < n:
        st.add(i)
        sm += i
        i += 1
    st = st - calc(sm - n)
    return st


sys.setrecursionlimit(10000000)
n = int(input())

c = calc(n)

for cc in c:
    print(cc)

Measured memory_bytes: 3788
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			String line = sc.next();
			if (line.equals(".")) {
				sc.close();
				break;
			}
			int ans = 0;
			for (int p = 0; p <= 2; p++) {
				for (int q = 0; q <= 2; q++) {
					for (int r = 0; r <= 2; r++) {
						String tmp = line.replaceAll("P", String.valueOf(p));
						tmp = tmp.replaceAll("Q", String.valueOf(q));
						tmp = tmp.replaceAll("R", String.valueOf(r));
						while (tmp.length() > 1) {
							while (true) {
								int k = tmp.length();
								tmp = tmp.replaceAll("-0", "2");
								tmp = tmp.replaceAll("-1", "1");
								tmp = tmp.replaceAll("-2", "0");
								if (tmp.length() == k) {
									break;
								}
							}
							while (true) {
								int k = tmp.length();
								tmp = tmp.replaceAll("\\(2\\*2\\)", "2");
								tmp = tmp.replaceAll("\\([12]\\*1\\)", "1");
								tmp = tmp.replaceAll("\\(1\\*[12]\\)", "1");
								tmp = tmp.replaceAll("\\([012]\\*0\\)", "0");
								tmp = tmp.replaceAll("\\(0\\*[012]\\)", "0");
								tmp = tmp.replaceAll("\\(0\\+0\\)", "0");
								tmp = tmp.replaceAll("\\(1\\+[01]\\)", "1");
								tmp = tmp.replaceAll("\\([01]\\+1\\)", "1");
								tmp = tmp.replaceAll("\\([012]\\+2\\)", "2");
								tmp = tmp.replaceAll("\\(2\\+[012]\\)", "2");
								if (tmp.length() == k) {
									break;
								}
							}
						}

						if (tmp.equals("2")) {
							ans++;
						}
					}
				}
			}
			System.out.println(ans);
		}

	}

}

Measured memory_bytes: 86820
# -*- coding: utf-8 -*-

N = int(input())
h = list(map(int, input().split()))


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        return min_height

    sum_cnt = 0
    left_list = list_h[:zero_indexes[0]]
    right_list = list_h[zero_indexes[0]+1:]
    sum_cnt += calc(left_list) + calc(right_list)

    return sum_cnt + min_height


print(calc(h))


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

#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f

#define fi first
#define se second
#define pb push_back
#define ende '\n'

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))

using namespace std; 

using ll = long long;
using ii = pair<int,int>;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << setprecision(10) << fixed;

  int n; cin >> n;
  double cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (i % 2) cnt++;
  cout << cnt / n << ende;

  return 0;
}


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

using namespace std;

long long n,k,x,counter=0;
vector<int> g[1<<20];

int dfs(int s, int p) {

    if(s == -1) return 0;

    int ans = 0;
    for(int i=0;i<g[s].size();i++) {
        ans = max(ans, dfs(g[s][i], s));
    }

    if(s != 1 and ans == (k-1) and p != 1) {counter++;return 0;}

    return ans+1;
}

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

    for(int i=1;i<=n;i++) {
        cin>>x;
        if(i != 1) {
            g[x].push_back(i);
        } else if(x != 1) counter++;
    }

    for(int i=2;i<=n;i++) if(g[i].size() == 0) g[i].push_back(-1);

    dfs(1,-1);

    cout<<counter;
}


Measured memory_bytes: 34176
#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 bit(n) (1LL<<(n))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
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 namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int inf = INT_MAX;
const ll INF = LLONG_MAX;
const ll MOD = 1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b, const ll mod=MOD) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b, const ll mod=MOD) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b, const ll mod=MOD) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b, const ll mod=MOD) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n; cin >> n;
    cout << (1 << (int)log2(n)) << endl;

    return 0;
}


Measured memory_bytes: 512
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
string itos(int a){
	string s="";
	while(a){
		s=(char)(a%10+'0')+s;
		a/=10;
	}
	while(s.size()!=4)s="0"+s;
	return s;
}
int main(){
	string s;
	while(cin>>s,s!="0000"){
		int co=0;
		while(co!=1000){
			if(s=="6174")break;
			sort(all(s));
			string tmp=s;
			reverse(all(tmp));
			int sa=atoi(&tmp[0])-atoi(&s[0]);
			s=itos(sa);
			co++;
		}
		if(co!=1000)cout<<co<<endl;
		else cout<<"NA"<<endl;
	}
}

Measured memory_bytes: 1228
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
aaa = na()
m = 1
for a in aaa:
    m *= a
s= 0
for a in aaa:
    s += (m/a)
print(m/s)

Measured memory_bytes: 38256
import math
import copy
import sys

def rotation(s):
    rot = []
    for i in range(1, len(s)):
        rot.append(s[i])

    rot.append(s[0])

    return rot

inputa = input().split()
inputb = input().split()
# inputc = input().split()

# a = int(inputa[0])
# b = int(inputa[1])
# c = int(inputa[2])

# x = int(inputb[0])
# y = int(inputb[1])

# inputaList = [int(n) for n in inputa]

# inputb = input().split()
# inputbList = [int(n) for n in inputb]
#
# inputList = []
# inputNumList = []
# for i in range(a):
#     inputNum = input()
#     inputNumList = list(inputNum)
#     inputList.append(inputNumList)

# inputList = [int(n) for n in inputList]

# b = [int(n) for n in inputb]
# c = [int(n) for n in inputc]

a = str(inputa[0])
inputaList = list(a)

b = str(inputb[0])
inputbList = list(b)

tmpinput = []
tmpinput = copy.deepcopy(inputaList)
tmp = []

for i in range(len(inputaList)):
    tmpinput = copy.deepcopy(rotation(tmpinput))

    flg = True
    for j in range(len(inputaList)):
        if tmpinput[j] != inputbList[j]:
            flg = False

    if flg:
        print("Yes")
        break

if flg == False:
    print("No")


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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
	ll X; cin >> X;
	vector<ll> sum(1000000);
	for(int i = 0; i < sum.size(); i++) {
		if(i == 0) sum[0] = 0;
		else sum[i] = sum[i - 1] + i;
	}
	ll ans = 0;
	for(int i = 0; i < sum.size(); i++) {
		if(sum[i] >= X) {
			ans = i;
			break;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 11148
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
#define lf long double
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef pair <ll,ll > ii;
typedef vector <ll> vi;
typedef vector <ii> vii;
typedef vector <bool> vb;
typedef vector <vector <ll> > vv;
typedef vector <vector <vector<ll> > > vvv;
typedef vector <string> vs;
typedef vector <lf> vf;
#define sz(x) (ll)(x).size()
#define tr1(x) cerr << #x << ": " << x << endl
#define tr2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define tr3(x,y,z) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define tr4(x,y,z,w) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<" | "<<#w<<": "<<w<<endl
#define trv(a) cerr<<#a<<": ";for(ll zz=0;zz<sz(a);zz++) cerr<<a[zz]<<" ";cerr<<endl
#define trii(x) cerr<<#x<<": ("<<x.F<<","<<x.S<<")\n";
#define trvii(a) cerr<<#a<<": ";for(ll zz=0;zz<sz(a);zz++) cerr<<"("<<a[zz].F<<","<<a[zz].S<<") ";cerr<<nl
#define trset(s) cerr<<#s<<": ";for(auto &i:s) cerr<<i<<" ";cerr<<"\n"
#define trsetii(s) cerr<<#s<<": ";for(auto &i:s) trii(i);
#define epsilon 0.000000001L
#define mod 1000000007ll
#define inf 9999999999999999ll// ~ 10^16
#define nl endl
#define all(x) x.begin(), x.end()
#define r_all(x) x.rbegin(), x.rend()
#define fast()  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define modulo 998244353ll


int main()
{
    fast();
    ll n;
    cin>>n;
    vi a(n+1);
    for(ll i=0;i<n+1;i++)
        cin>>a[i];
    if(n==0)
    {
        if(a[0]==1)
            cout<<1<<endl;
        else
            cout<<-1<<endl;
        return 0;
    }
    vii numnodes(n+1,{0,0});
    numnodes[n]={a[n],a[n]};
    for(ll i=n-1;i>=0;i--)
    {
        numnodes[i]={numnodes[i+1].F/2 + a[i], numnodes[i+1].S+a[i]};
    }
    numnodes[0]={1,1};
    for(ll i=1;i<=n;i++)
    {
        ll upper=(numnodes[i-1].S-a[i-1])*2;
        ll lower=numnodes[i-1].F-a[i-1];
        numnodes[i].F=max(numnodes[i].F,lower);

        numnodes[i].S=min(numnodes[i].S,upper);
    }
    for(ll i=0;i<=n;i++)
    {
        if(numnodes[i].F>numnodes[i].S)
        {
            cout<<-1<<nl;
            return 0;
        }
    }
    ll sum=0;
    for(ll i=0;i<=n;i++)
    {
        sum+=numnodes[i].S;
    }
    cout<<sum<<nl;
}


Measured memory_bytes: 5572
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const int inf=1e9+7;
const ll INF=1e18;

int main(){
    int h,w;cin>>h>>w;
    vstr s(h);rep(i,h)cin>>s[i];
    vvin lineleft(h,vin(w)),lineright(h,vin(w)),rowup(w,vin(h)),rowdown(w,vin(h));
    int tmpll,tmplr,tmpru,tmprd;
    rep(i,h){
        tmpll=-1;tmplr=w;
        rep(j,w){
            if(s[i][j]=='#'){
                lineleft[i][j]=j;
                tmpll=j;
            }
            else lineleft[i][j]=tmpll;
            if(s[i][w-1-j]=='#'){
                lineright[i][w-1-j]=w-1-j;
                tmplr=w-1-j;
            }
            else lineright[i][w-1-j]=tmplr;
        }
    }
    rep(i,w){
        tmpru=-1;tmprd=h;
        rep(j,h){
            if(s[j][i]=='#'){
                rowup[i][j]=j;
                tmpru=j;
            }
            else rowup[i][j]=tmpru;
            if(s[h-1-j][i]=='#'){
                rowdown[i][h-1-j]=h-1-j;
                tmprd=h-1-j;
            }
            else rowdown[i][h-1-j]=tmprd;
        }
    }
    int ans=0;int tmp;
    rep(i,h)rep(j,w){
        if(s[i][j]!='#'){
            tmp=(lineright[i][j]-lineleft[i][j]-1)+(rowdown[j][i]-rowup[j][i]-1)-1;
            ans=max(ans,tmp);
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 67200
import java.util.Scanner;
 
class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int H = sc.nextInt();
    int M = sc.nextInt();
    
    int i = 0;
    while(H > 0){
      H = H - M;
      i++;
    }
    System.out.println(i);
  }
}


Measured memory_bytes: 22996
#include<iostream>
#include<cstdlib>
#include<cstdio>

using namespace std;

struct Node {
  int data;
  Node *left, *right;
} *NIL, *root = NIL;
int in[500001], pre[500001], _in, _pre;

void insert (Node *node) {
  Node *y = NIL;
  Node *x = root;

  while (x != NIL) {
    y = x;

    if (node->data < x->data)
      x = x->left;
    else
      x = x->right;
  }

  if (y == NIL)
    root = node;
  else if (node->data < y->data)
    y->left = node;
  else
    y->right = node;
}

void inorder (Node *node) {
  if (node == NIL)
    return;

  inorder(node->left);

  in[_in++] = node->data;

  inorder(node->right);
}

void preorder (Node *node) {
  if (node == NIL)
    return;
  
  pre[_pre++] = node->data;

  preorder(node->left);
  preorder(node->right);
}

void printArrary (int arr[], int length) {
  for (int i = 0; i < length; i++)
    cout << " " << arr[i];

  cout << endl;
}

void print () {
  _in = 0;
  _pre = 0;
  inorder(root);
  printArrary(in, _in);
  preorder(root);
  printArrary(pre, _pre);
}

int main () {
  int n, m;
  char s[100];

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

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

    if (s[0] == 'i') {
      cin >> m;

      Node *node = (Node *)malloc(sizeof(Node));

      node->left = NIL;
      node->right = NIL;
      node->data = m;

      insert(node);
    } else {
      print();
    }
  }

  return 0;
}


Measured memory_bytes: 22672
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
#define ll long long
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	int temp = (n%1000);
	if(n>=1000 && n%1000 != 0)
		cout << 1000 - temp;
	else if(n%1000 == 0)
		cout << 0;
	else
		cout << 1000 - n;
}



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

int main() {
  int n; cin >> n;
  vector<int> v(n);
  vector<int> c(n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> c.at(i);
    ans += max(0, v.at(i) - c.at(i));
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-
n,x = map(int, input().split())
xl = list(map(int, input().split()))

def gcd(a,b):
    if b>a:
        a,b = b,a
    while b!=0:
        a,b = b,a%b
    return a

xl.append(x)
xl.sort()
res = None
for i in range(n):
    d = xl[i+1] - xl[i]
    if res is None:
        res = d
    else:
        res = gcd(res, d)
print(res)


Measured memory_bytes: 14252
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
using Graph = vector< vector<int> >;

int main() {
    int N,M; cin >> N >> M;
    Graph G(N);
    for (int i = 0; i < M; i++)
    {
        int a,b; cin >> a >> b;
        G[a-1].push_back(b-1);
        G[b-1].push_back(a-1);
    }
    vector<int> dist(N, -1);
    queue<int> que;
    dist[0] = 0;
    que.push(0);
     while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (int nv : G[v]) {
            if (dist[nv] != -1) continue; 

            dist[nv] = v+1;
            que.push(nv);
        }
    }
    for (int i = 0; i < N; i++)
    {
        if(dist[i]==-1){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    for (int v = 1; v < N; ++v) cout << dist[v] << endl;
}

Measured memory_bytes: 10320
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

#define rep(i,x) for(int i=0;i<x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define FIND(x, target) find(x.begin(),x.end(),target)
#define pb(x) push_back(x)
#define output(x) cout<<x<<endl
#define ERASE(x, n) erase(x.begin(), x.begin+n)
// #define int long long
#define P pair<int, int>
typedef long long ll;

const long long int MOD = 1e9+7;
const int INF = -1;

signed main(void) {
  	int N;
  	cin >> N;
  	vector<int> A;

  	// align in max -> min order
  	vector<int> Color;
  	rep(i, N) {
  		int tmp;
  		cin >> tmp;
  		A.pb(tmp);
  	}
  	
  	for(int i=0; i < N; i++) {
  		bool find = false;
  		int index = -1;
  		for (int j=Color.size(); j>0; j--) {
  			if (Color[j-1] < A[i]) {
  				if (j == Color.size()) {
  					find = true;
  					index = j-1;
  				} else if (j == 1) {
  					index = 0;
  				}
  				continue;
  			} else {
  				index = j;
  				break;
  			}
  		} 

  		if (find) {
  			Color[index] = A[i];
  		} else {
  			Color.pb(A[i]);
  		}

  		/*rep(j, Color.size()) {
  			cout << j << ": " << Color[j] << " ";
  		}
  		cout << endl;*/
  	}

  	cout << Color.size() << endl;
 	
	return 0;
}


Measured memory_bytes: 1400
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

using pi = pair<int,int>;
using P = pair<int,pi>;

struct edge{int to,cost,time,company;};

const int V=100;
const int INF=123456789;

vector<edge> G[V];

int dp[300];
int d[100][25];

int main()
{
    int N,M,H,K;
    while(scanf(" %d %d %d %d", &N, &M, &H, &K),N)
    {
        rep(i,V) G[i].clear();

        rep(i,M)
        {
            int a,b,c,h,r;
            scanf(" %d %d %d %d %d", &a, &b, &c, &h, &r);
            --a;
            --b;
            --r;
            G[a].pb({b,c,h,r});
            G[b].pb({a,c,h,r});
        }

        int S,T;
        scanf(" %d %d", &S, &T);
        --S;
        --T;

        int PASS;
        scanf(" %d", &PASS);

        vector<int> pass_cost(PASS),mask(PASS);
        rep(i,PASS)
        {
            int l;
            scanf(" %d %d", &l, &pass_cost[i]);

            rep(j,l)
            {
                int k;
                scanf(" %d", &k);
                --k;

                mask[i] |= 1<<k;
            }
        }

        fill(dp,dp+300,INF);
        dp[0]=0;
        rep(i,1<<K)rep(j,PASS)
        {
            int nx = i|mask[j];
            dp[nx] = min(dp[nx],dp[i]+pass_cost[j]);
        }

        int ans=INF;
        rep(i,1<<K)
        {
            fill(d[0],d[100],INF);
            d[S][0]=0;

            priority_queue<P,vector<P>,greater<P>> pq;
            pq.push(P(0,pi(S,0)));
            while(!pq.empty())
            {
                P now = pq.top();
                pq.pop();

                int v = now.se.fi, h = now.se.se;
                if(now.fi>d[v][h]) continue;

                for(const auto &e:G[v])
                {
                    int nh = h+e.time;
                    if(nh>H) continue;

                    int COST = e.cost;
                    if(i>>e.company&1) COST = 0;

                    if(d[e.to][nh] > d[v][h]+COST)
                    {
                        d[e.to][nh] = d[v][h]+COST;
                        pq.push(P(d[e.to][nh],pi(e.to,nh)));
                    }
                }
            }

            rep(j,H+1) ans = min(ans,d[T][j]+dp[i]);
        }

        if(ans==INF) ans=-1;
        printf("%d\n", ans);
    }
    return 0;
}

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

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

  if (x >= 30) cout << "Yes" << endl;
  else cout << "No" << endl;

  return 0;
}



Measured memory_bytes: 3632
import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {
  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val distance = Array.tabulate(n){_ ⇒ readLine.trim.split(' ').map(_.toLong)}
  var left = 1L
  var right = distance.map{_.max}.max
  while(left < right) {
    val mid = (left + right) / 2
    if (pushFlow(makeGraph(distance, mid)) == m) right = mid
    else left = mid + 1
  }
  println(if (m % n == 0) 0 else 1)
  println(right)
  def makeGraph(distance: Array[Array[Long]], maxDistance: Long): Array[List[Edge]] = {
    val smaller = m / n
    val rest = m % n
    val sourceForSmaller = n * 2 + m
    val sourceForRest = sourceForSmaller + 1
    val source = sourceForRest + 1
    val sink = source + 1
    val result = Array.fill(sink + 1){Nil: List[Edge]}
    def setEdge(from: Int, to: Int, flow: Int): Unit = {
      result(from) ::= Edge(to, flow)
      result(to) ::= Edge(from, 0)
      result(from).head.pair = result(to).head
      result(to).head.pair = result(from).head
    }
    for (i ← 0 until n) {
      setEdge(sourceForSmaller, i, smaller)
      setEdge(sourceForRest, i + n, 1)
      for (j ← 0 until m if distance(i)(j) <= maxDistance){
        setEdge(i, 2 * n + j, 1)
        setEdge(i + n, 2 * n + j, 1)
      }
    }
    for (j ← 0 until m) {
      setEdge(2 * n + j, sink, 1)
    }
    setEdge(source, sourceForSmaller, smaller * n)
    setEdge(source, sourceForRest, rest)
    result
  }
  def pushFlow(nodes: Array[List[Edge]]): Int = {
    val source = nodes.length - 2
    val sink = nodes.length - 1
    val depth = Array.fill(nodes.length){-1}
    val searching = Array.tabulate(nodes.length)(nodes)
    def bfs() = {
      depth.indices.foreach(i ⇒ depth(i) = -1)
      searching.indices.foreach(i ⇒ searching(i) = nodes(i))
      val queue = mutable.Queue[Int](source)
      depth(source) = 0
      while(queue.nonEmpty) {
        val current = queue.dequeue()
        if (current != sink) for (Edge(to, flow) ← nodes(current) if flow > 0 && depth(to) == -1) {
          depth(to) = depth(current) + 1
          queue.enqueue(to)
        }
      }
    }
    def dfs(current: Int): Boolean = {
      if (current == sink) true
      else {
        var result = false
        while (!result && searching(current).nonEmpty) {
          searching(current) match {
            case (e@Edge(to, flow))::rest if flow > 0 && depth(to) > depth(current) ⇒
              result = dfs(to)
              if (result) {
                e.flow -= 1
                e.pair.flow += 1
              }else searching(current) = rest
            case _::rest ⇒
              searching(current) = rest
            case _ ⇒
          }
        }
        result
      }
    }
    var count = 0
    var hasFlow = true
    while(hasFlow) {
      hasFlow = false
      bfs()
      while(dfs(source)) {
        count += 1
        hasFlow = true
      }
    }
    count
  }
}
case class Edge(to: Int, var flow: Int) {
  private var mPair = this
  def pair: Edge = mPair
  def pair_=(edge: Edge) = mPair = edge
}


Measured memory_bytes: 65984
#include<iostream>

using namespace std;

int main(void) {

    int A, B, C, K;
    cin >> A >> B >> C >> K;
    int max_current = K;
    if (max_current >= A) {
        max_current = A;
        K -= A;
    }

    if (K > 0) {
        K -= B;
    }

    if (K > 0) {
        for (int i = 1; i <= K; i++) {
            max_current -= 1;
        }
    }
    cout << max_current << endl;
    return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;

template <typename T>
struct edge
{
    int src, to;
    T cost;

    edge(int to, T cost) : src(-1), to(to), cost(cost) {}

    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

    edge &operator=(const int &x)
    {
        to = x;
        return *this;
    }

    operator int() const { return to; }
};

//Weighted edge set
template <typename T>
using Edges = vector<edge<T>>;

//Weighted graph
template <typename T>
using WeightedGraph = vector<Edges<T>>;

//Unweighted graph (adjacency matrix)
//Initialize : vector<vector<int>>(N, vector<int>(N, 0))
template <typename T>
using UnweightedGraph = vector<vector<T>>;

//distance matrix
template <typename T>
using Matrix = vector<vector<T>>;

int N;
UnweightedGraph<int> G;
vector<bool> visited;
vector<char> color;

void bfs()
{
    queue<pair<int, char>> que;
    que.emplace(0, 'B');
    que.emplace(N - 1, 'W');
    while (!que.empty())
    {
        auto p = que.front();
        int now = p.first;
        char c = p.second;
        que.pop();
        if (visited[now] == true)
            continue;
        visited[now] = true;
        color[now] = c;
        for (auto &e : G[now])
        {
            if (visited[e] == false)
                que.emplace(e, c);
        }
    }
}

int main()
{
    cin >> N;
    G = vector<vector<int>>(N);
    visited = vector<bool>(N, false);
    color = vector<char>(N);
    color[0] = 'B', color[N - 1] = 'W';
    for (int i = 0; i < N - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    bfs();
    int b = 0, w = 0;
    for (int i = 0; i < N; i++)
    {
        if (color[i] == 'B')
            b++;
        else
            w++;
    }
    if (w >= b)
        cout << "Snuke" << endl;
    else
        cout << "Fennec" << endl;
}

Measured memory_bytes: 6016
#include <iostream>
#include <cstdio>

using namespace std;
#define long long long

int const N = 1e5 + 10;
long const INF = 1e18;

int n, total;
long w[110], v[110], f[N];

int main()
{
//    freopen("tam.inp", "r", stdin);
    cin.tie(nullptr); ios_base::sync_with_stdio(false);

    cin >> n >> total;
    for (int i = 1; i <= n; ++i) cin >> w[i] >> v[i];

    fill(f, f + N, INF); f[0] = 0;
    for (int i = 1; i <= n; ++i)
//        for (int value = 1; value < N; ++value) {
        for (int value = N - 1; value; --value) {
            if (v[i] <= value) f[value] = min(f[value], f[value - v[i]] + w[i]);
        }

    long res = 0;
    for (int value = N - 1; value && !res; --value)
        if (f[value] <= total) res = value;
    cout << res;

    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <complex>

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;

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

    ll n, k;
    cin >> n >> k;
    ll full = 0LL;
    for (int a = k; a <= n; a += k) {
        ++full;
    }
    // cout << "full " << full;
    ll ans = 1LL * full * full * full;
    if (k % 2 == 0) {
        ll half = 0;
        k >>= 1;
        for (int a = k; a <= n; a += 2 * k) {
            ++half;
        }
        // cout << "half " << half;
        ans += 1LL * half * half * half;
    }

    cout << ans << '\n';

    return 0;
}


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

map<ll,ll> mp;
ll n,a,b,c,d;

ll dfs(ll x)
{
	if(!x)return 0;
	
	if(mp[x]!=0)return mp[x];
	if(x==1)return mp[1] = d;

	ll now ;
	if(x>1e18/d)now = LLONG_MAX;
	else now = x*d;
	ll tmp = x%2;
	now = min(now,dfs((x-tmp)/2)+a+tmp*d);
	now = min(now,dfs((x+tmp)/2)+a+tmp*d);
	tmp = x%3;
	now = min(now,dfs((x-tmp)/3)+b+tmp*d);
	tmp = 3 - tmp;
	now = min(now,dfs((x+tmp)/3)+b+tmp*d);
	tmp = x%5;
	now = min(now,dfs((x-tmp)/5)+c+tmp*d);
	tmp = 5 - tmp;
	now = min(now,dfs((x+tmp)/5)+c+tmp*d);
	return mp[x] = now;

}
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		cin >> n >> a >> b >> c >> d;
		cout <<dfs(n)<<endl;
		mp.clear();
	}
}


Measured memory_bytes: 4652
#include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;



int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	string S;
	cin >> S;
	if (S.size() == 3) {
		swap(S[0], S[2]);
	}
	cout << S << "\n";

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <tuple>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repdm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)

int N, A, B, C;
int seq[4] = {0, 1, 2, 3};
int ANS = 100000;
int l[8];

int calcMP(vector<int> m)
{
  if (m[1] == 0 || m[2] == 0 || m[3] == 0)
    return 10000;
  return abs(A - m[1]) + abs(B - m[2]) + abs(C - m[3]);
}

int dfs(int depth, vector<int> m, int mp)
{
  if (depth == 0)
  {
    mp += calcMP(m);
    return mp;
  }

  for (auto s : seq)
  {
    int tmp = mp;
    vector<int> mm = m;
    if (s != 0)
    {
      if (mm[s] == 0)
      {
        mm[s] = l[depth - 1];
      }
      else
      {
        tmp += 10;
        mm[s] += l[depth - 1];
      }
    }
    tmp = dfs(depth - 1, mm, tmp);
    ANS = min(ANS, tmp);
  }

  return ANS;
}

int main()
{
  cin >> N >> A >> B >> C;
  rep(i, N) cin >> l[i];
  int mp = 0;

  vector<int> m(4, 0);

  dfs(N, m, mp);

  cout << ANS;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

typedef long long ll;
#define READ(a) int a; cin >> a;
#define READARR(a, n) int a[(n) + 1]; FOR(i, 1, (n)) {cin >> a[i];}
#define READMAT(a, n, m) int a[n + 1][m + 1]; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define pb(x) push_back(x)
#define vi vector <int>
#define pi pair <int, int>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	READ(n);
	READARR(a, n);
	
	multiset <int> head;
	FOR (i, 1, n)
	{
		auto it = head.lower_bound(a[i]);
		if (it == head.begin()) head.insert(a[i]);
		else 
		{
			head.erase(--it);
			head.insert(a[i]);
		}
	}
	
	cout << head.size();
}

Measured memory_bytes: 5760