akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
using namespace std;
int n;
vector < int > ans[(1<<10)];

void genAns ( int snum, int kleft, int sy ) {
    if ( kleft == 0 ) { return; }

    for ( int i = 0 ; i < kleft ; i ++ ) { ans[sy].push_back( snum +i ); }
    for ( int y = 1; y <= kleft ; y ++ ) { ans[sy+y].push_back( snum+y-1 ); }

    genAns ( snum + kleft, kleft-1, sy+1 );
}

int main () {

    cin >> n;
    int k;
    for ( k = 1 ; k*(k+1) < 2*n ; k ++ ) {}

    if ( k*(k+1) != 2*n ) { cout << "No\n"; return 0; }

    cout << "Yes\n";
    genAns( 1, k, 0 );
    cout << k+1 << "\n";
    for ( int i = 0 ; i < k+1 ; i ++ ) {
        cout << ans[i].size() << " ";
        for ( int j = 0 ; j < ans[i].size() ; j ++ ) {
            cout << ans[i][j] << " ";
        }
        cout << "\n";
    }


    return 0;
}


Measured memory_bytes: 2304
n, p = map(int, input().split())
a = list(map(int, input().split()))

b0 = 0
b1 = 0

fib = [1]
num = 1
for i in range(1, n+1):
    num *= i
    fib.append(num)

def com(n, r):
    return fib[n] // fib[r] // fib[n - r]

for i in range(n):
    if a[i] % 2 == 0:
        b0 += 1
    else:
        b1 += 1

c0 = 0
c1 = 0

for i in range(b0+1):
    c0 += com(b0, i)

if p == 0:
    for i in range(b1+1):
        if i % 2 == 1:
            continue
        c1 += com(b1, i)
    print(c0 * c1)
else:
    for i in range(b1+1):
        if i % 2 == 0:
            continue
        c1 += com(b1, i)
    print(c0 * c1)

Measured memory_bytes: 3064
//
// Created on 2020/06/06.
//

#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 <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;


int main() {
    int h,w;
    cin>>h>>w;
    int h_t,w_t;
    cin>>h_t>>w_t;
    int ans = (h-h_t)*(w-w_t);
    cout<<ans<<endl;
    return 0;
}



Measured memory_bytes: 256
flag = False
n, y = map(int, input().split())
for i in range(n, -1, -1):
    if i * 10000 > y:
        continue
    else:
        for j in range(n-i+1):
            if i*10000+j*5000+(n-i-j)*1000 == y:
                print('{} {} {}'.format(i, j, n-i-j))
                flag = True
                break
    if flag:
        break
if not flag:
    print('{} {} {}'.format(-1, -1, -1))


Measured memory_bytes: 3060
using System;

namespace AtCoderCrackers
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            ulong n = ulong.Parse(input[0]);
            ulong k = ulong.Parse(input[1]);

            Console.WriteLine((n % k) == 0 ? "0" : "1");
        }
    }
}


Measured memory_bytes: 13140
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cmath>
 
using namespace std;

struct node{
	int id;
	queue<node*> adjacentNodeList;
};

int d[100] = {0};
int f[100] = {0};

int bfs(node* nd,int time){
	//cout<<nd->id<<" is called"<<endl;
	if(f[nd->id] != 0){
		return time;
	}
	if(d[nd->id] != 0){
		return time;
	}
	time = time + 1;
	if(d[nd->id] ==0){
		d[nd->id] = time;
	}
	while( !nd->adjacentNodeList.empty() ){
		if(nd->id == nd->adjacentNodeList.front()->id){
			nd->adjacentNodeList.pop();
		}else{
			time = bfs(nd->adjacentNodeList.front(),time);
			nd->adjacentNodeList.pop();
		}
	}
	time++;
	if( f[nd->id] == 0) f[nd->id] = time;
	return time;
}

int main()
{
	int n;
	cin>>n;
	struct node *nodeList = new node[n];
	for(int i = 0; i < n; i++){
		nodeList[i].id = i;
	}

	for(int i = 0; i < n;i++){
		int u,k;
		cin>>u>>k;
		u--;		//0 origin
		for(int j = 0; j < k; j++){
			int index;
			cin>>index;
			index--;		//0 origin
			nodeList[u].adjacentNodeList.push(&nodeList[index]);
		}
	}
	int time = 0;
	for(int i = 0; i < n; i++){
		time = bfs(&nodeList[i],time);	
	}

	for(int i = 0; i <n;i++){
		cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
	}

	return 0;
}


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

int main() {
	ll n, k;
	cin >> n >> k;
	vector<ll> p(n), c(n);
	rep(i, n) cin >> p[i], p[i]--;
	rep(i, n) cin >> c[i];
	const ll INF = -1*(1LL<<60);
	ll mx = INF;
	vector<pair<ll, ll>> score(n); //各点の１周したときに得られるスコア、一周の長さ
	rep(i, n) {
		ll ns = c[i];
		int now = i;
		int syuki = 1;
		while (p[now] != i) {
			now = p[now];
			ns += c[now];
			syuki++;
		}
		score[i] = {ns, syuki};
		// cout << score[i].first << " " << score[i].second << endl;
	}
	rep(i, n) {
		ll now = 0;
		ll lef_mx = INF;
		ll sum = 0;
		int np = i;
		if (score[i].second > 1 && score[i].first > 0) {
			ll nk = k%score[i].second + score[i].second;
			now = (k/score[i].second - 1) * score[i].first;
			rep(j, nk) {
				np = p[np];
				sum += c[np];
				lef_mx = max(lef_mx, sum);
			}
		} else {
			rep(j, min(k, score[i].second)) {
				np = p[np];
				sum += c[np];
				lef_mx = max(lef_mx, sum);
			}
		}
		
		now += lef_mx;
		mx = max(mx, now);
	}
	cout << mx << endl;
	return 0;
}

Measured memory_bytes: 3772
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int h[100010],n,k,m=2e9;
  	cin>>n>>k;
	for(int i=0;i<n;i++)cin>>h[i];
	sort(h,h+n);
	for(int i=0;i<n-k+1;i++)
	{
		m=min(m,h[i+k-1]-h[i]);
	}
	cout<<m<<endl;
}

Measured memory_bytes: 640
// abc167_d
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))
#define ALL(x) x.begin(), x.end()

#define INF ((1 << 29)-1)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int i=0;i<(n-1);i++) cout << (vec)[i] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << endl;}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    ll N, K;
    cin >> N >> K;
    vector<int> A(N);
    REP(i, N) {cin >> A[i]; A[i]--;}

    vector<int> loop;
    loop.emplace_back(0);
    set<int> st;
    st.insert(0);
    int x = 0;
    while (true){
        x = A[x];
        if (st.find(x) != st.end()){
            break;
        } else {
            loop.emplace_back(x);
            st.insert(x);
        }
    }

    int idx = 0;
    REP(i, loop.size()){
        if (loop[i] == x){
            idx = i;
            break;
        }
    }

//    print(loop);
//    print(idx);

    int ans;
    if (K <= idx){
        ans = loop[K];
    } else {
        ans = loop[idx + (K-idx) % (loop.size()-idx)];
    }
    print(ans+1);

    return 0;
}


Measured memory_bytes: 13796
# from collections import defaultdict
import heapq
N, M = map(int, input().split())

W = []
for _ in range(N):
    W.append(tuple(map(int, input().split())))
W = sorted(W, key=lambda x: (x[0], -x[1]))


a = []
heapq.heapify(a)  # リストaのheap化

ans = 0
c = 0
for day in range(1, M+1):
    day_c = 0
    while c != N and W[c][0] == day:
        if day_c <= M-day:
            x = W[c][1]
            heapq.heappush(a, -x)  # heap化されたリストaに要素xを追加
            day_c += 1
        c += 1
    if len(a) >= 1:
        ans += -heapq.heappop(a)  # heap化されたリストaから最小値を削除＆その最小値を出力

print(ans)


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

const int MAX_VALUE = 55556;

vector<int> make_primes(int n){
  vector<int>ret;
  bool used[n];
  for(int i = 0 ; i < n ; i++)used[i] = false;
  for(int i = 2 ; i < n ; i++){
    if(used[i])continue;
    ret.push_back(i);
    for(int j = i ; j < n ; j += i){
      used[j] = true;
    }
  }
  return ret;
}

int main(){
  int n;
  cin >> n;
  vector<int> vec = make_primes(MAX_VALUE);
  vector<int> vec2;
  for(int i = 0 ; i < vec.size() ; i++){
    if(vec[i]%5 == 1)vec2.push_back(vec[i]);
  }

  for(int i = 0 ; i < n ; i++){
    cout << vec2[i] << ' ';
  }
  cout << endl;
  
  return 0;
}


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

#define ll long long
#define INF 99999999
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()

template < typename T > std::string to_string( const T& n )
{
    std::ostringstream stm ;
    stm << n ;
    return stm.str() ;
}

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

    set<ll> key;
    multiset<ll> KEY;

    vector<ll>A(N);
    rep(i,N)
    {
        cin >> A[i];
        key.insert(A[i]);
        KEY.insert(A[i]);
    }

    vector<ll> ans;
    for(set<ll>::iterator itr = key.begin(); itr != key.end(); ++itr) 
    {
        ll tmp = KEY.count( *itr );
        while( tmp > 1 )
        {
            ans.push_back(*itr);
            tmp -= 2;
        }
    }

    sort(all(ans),greater<ll>());

    if( ans.size() < 2 ) cout << 0 << endl;
    else cout << ans[0] * ans[1] << endl;


}


Measured memory_bytes: 10368
IDENTIFICATION DIVISION.
PROGRAM-ID. 170C.
DATA DIVISION.
*>
WORKING-STORAGE SECTION.
01 INP        PIC X(400).
01 X          PIC 9(03) COMP.
01 N          PIC 9(03) COMP.
01 P1         PIC 9(04) COMP.
01 P2         PIC 9(04) COMP.
01 TBL1.
   03 P       PIC 9(03) OCCURS 102.
01 I          PIC 9(03) COMP.
01 IDX        PIC 9(03) COMP.
01 CNT        PIC 9(03) COMP.
01 OUT        PIC ZZ9.
*>
PROCEDURE DIVISION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ' '
      INTO X N.
*>
  ACCEPT INP.
  MOVE ZERO TO TBL1.
  MOVE 1    TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO IDX
    MOVE 1               TO P(IDX + 1)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  IF (N = 0) OR P(X + 1) = 0
    MOVE X TO CNT    
  ELSE      
    MOVE 1 TO IDX 
    PERFORM VARYING I FROM 1 BY 1
              UNTIL P(X - I + 1) = 0 OR P(X + I + 1) = 0
    END-PERFORM  
    IF (P(X- I + 1) = 0)
      COMPUTE CNT = X - I 
    ELSE
      COMPUTE CNT = X + I
    END-IF
  END-IF.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT)
  STOP RUN.


Measured memory_bytes: 3876
// Created by new_sm
//
#include<bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
s.find_by_order(x) xth element in set
x.order_of_key(x) number of elements <x*/
#define ll long long
#define vi vector<int>
#define si set<int>
#define mii map<int,int>
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define extract_word(s)  stringstream str(s); while(str>>word)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define SET(s) cout<<fixed<<setprecision(s)
#define chotu 1000000007
#define set0(a) memset(a,0,sizeof(a))
#define endl "\n"
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define lower_string(s) transform(all(s),s.begin(),::tolower())
#define upper_string(s) transform(all(s),s.begin(),::toupper())
#define size(s) s.size()
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
template<typename T,typename U>
bool compare(T x,U y){return(abs(x-y)<=1e-9);}
void solve()
{
fastio
int a,b;
cin>>a>>b;
cout<<max(a+b,max(a-b,a*b));
}
int main()
{
//code
fastio

int t;
t=1;
while(t--)
{
solve();
cout<<endl;
}
}

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

using namespace std;

const int Nmax = 404, Mmax = 1e5 + 5;

int x[Mmax], y[Mmax], i, n, m, j, ans = 0;
bool pos[Nmax], A[Nmax], ok;
vector<int> v[Nmax];

int main()
{
  //  freopen("input", "r", stdin);
    cin.sync_with_stdio(false);

    cin >> n >> m;
    for(i=1; i<=m; ++i)
        cin >> x[i] >> y[i];

    for(i=1; i<=n; ++i)
    {
        memset(A, 0, sizeof(A));
        A[i] = 1;
        v[i].push_back(i);
        pos[i] = 1;

        for(j=m; j && pos[i]; --j)
            if(A[x[j]] && A[y[j]])  pos[i] = 0;
                else if(A[x[j]]) A[y[j]] = 1, v[i].push_back(y[j]);
                    else if(A[y[j]]) A[x[j]] = 1, v[i].push_back(x[j]);
    }

    for(i=1; i<=n; ++i)
        for(j=i+1; j<=n; ++j)
            if(pos[i] && pos[j] && v[i].size() + v[j].size() <= n)
            {
                memset(A, 0, sizeof(A));
                for(auto it : v[i]) A[it] = 1;
                ok = 1;
                for(auto it : v[j])
                    if(A[it])
                    {
                        ok = 0;
                        break;
                    }

                if(ok) ++ans;
            }
    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 1152
def main():
    from sys import stdin
    input = stdin.readline
    n, m, r = map(int, input().split())
    root = list(map(int, input().split()))
    abc = [list(map(int, input().split())) for _ in [0]*m]

    import numpy as np
    import scipy.sparse.csgraph as sp
    inf = float('inf')
    d = np.full((n, n), inf)
    for i in range(n):
        d[i][i] = 0
    for a, b, c in abc:
        d[a-1][b-1] = c
        d[b-1][a-1] = c
    # indices=xでxからの単一始点に,return_predecessors=Trueで経路が出る
    s = sp.shortest_path(d)

    from itertools import permutations

    p = list(permutations(root))

    D = 10**10
    for i in p:
        d = 0
        for j in range(len(i)-1):
            d += s[i[j]-1][i[j+1]-1]
        D = min(D, d)
    print(int(D))


main()


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

long long int N, i, j;
int a[114514], c[114514];
char s[114514];

int main(){
    scanf("%lld%s", &N, s);
    for(i = 0; i < N; ++i)c[i] = (s[i]=='o')?0:1;
    c[N] = c[0];
    //for(i = 0; i < N; ++i)cout << c[i] << " ";
    //cout << endl;
    for(i = 0; i < 4; ++i){
        *a = i % 2;
        a[1] = i / 2;
        //cout << a[0] << " " << a[1] << endl;
        for(j = 1; j <= N; ++j){
            a[j + 1] = (a[j - 1] + a[j] + c[j]) % 2;
        }
        if((a[N] == a[0]) && (a[N + 1] == a[1])){
            for(j = 0; j < N; ++j){
                cout << (a[j]?"W":"S");
            }
            //cout << endl;
            return 0 & puts("");
        }
    }
    puts("-1");
    return 0;
}


Measured memory_bytes: 1280
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
const ll mo=1000000007;
ll ans=0;
ll dp[0][0];

int main()
{ 
ll n,m;
cin >> n>>m; 
ll table[m][2]={};
rep(i,m) {
    cin >> table [i][0]>> table [i][1];;
}
ll ma[n]={};
rep(i,m) {
    if(ma[table [i][0]]==0)ma[table [i][0]]=table [i][1];
    else if(ma[table [i][0]]>table [i][1])ma[table [i][0]]=table [i][1];
}
bool a=1;
ll st=0;

while(a){
    ll min=100009;
    for(ll i = st; i < (ll)(n); i++){
        if(ma[i]>0&&ma[i]<min)min=ma[i];
        if(i+1>=min){
            a=1;
            st=min;
            ans++;
            break;
            }
        a=0;
    }
    if(min==100009)a=0;
}

/* for (int i = 0; i < n; ++i) {
    cout << ma[i] << " ";
}cout  << endl; */

cout << ans << endl;
return 0;
} 

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

int main()
{
    int H, W, h, w;
    cin >> H >> W >> h >> w;
    int ans = H * W;

    ans -= h * W;
    ans -= w * H;
    ans += h * w;

    cout << ans << "\n";
}

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

using namespace std;

using ll = long long;

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

    int N;
    cin >> N;
    int A[N];
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    int lgcds[N];
    int rgcds[N];
    lgcds[0] = rgcds[N - 1] =  0;
    lgcds[1] = A[0];
    rgcds[N - 2] = A[N - 1];
    for (int i = 1; i <= N - 2; ++i) {
        lgcds[i + 1] = __gcd(lgcds[i], A[i]);
        rgcds[N - i - 2] = __gcd(rgcds[N - i - 1], A[N - i - 1]);
    }
    int max_gcd = 1;
    for (int i = 0; i < N; ++i) {
        int gcd = __gcd(lgcds[i], rgcds[i]);
        max_gcd = max(max_gcd, gcd);
    }

    cout << max_gcd << '\n';

    return 0;
}


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



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

	int N, M;
	cin >> N >> M;
	vector<int> a(N), b(N);
	REP(i, N) cin >> a[i] >> b[i];
	vector<int> c(M), d(M);
	REP(j, M) cin >> c[j] >> d[j];
	REP(i, N) {
		int res = 0;
		FOR(j, 1, M) {
			if (abs(a[i] - c[j]) + abs(b[i] - d[j]) < abs(a[i] - c[res]) + abs(b[i] - d[res])) {
				res = j;
			}
		}
		cout << ++res << "\n";
	}

	return 0;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace _2_C
//{
class Program
{
static void Main(string[] args)
{
//????????????
string a = Console.ReadLine();

string[] b = a.Split(' ');
//int x = int.Parse(b[0]);
//int y = int.Parse(b[1]);
//int z = int.Parse(b[2]);


    Array.Sort(b);
    Console.WriteLine(b[0] + " " + b[1] + " " + b[2]);
}
}
//}

Measured memory_bytes: 9944
#include <bits/stdc++.h>
using namespace std;
#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++)
typedef long long ll;

int main() {
    string s, t;
    cin >> s >> t;
    vector<char> S, T;
    for (int i = 0; i < s.size(); i++) {
        S.push_back(s[i]);
    }
    for (int i = 0; i < t.size(); i++){
        T.push_back(t[i]);
    }
    sort(S.begin(), S.end());
    sort(T.begin(), T.end());
    string newS, newT;
    for (int i = 0; i < s.size(); i++) {
        newS += S[i];

    }
    for (int i = t.size()-1; i >= 0; i--) {
        newT += T[i];
    }
    if (newS < newT) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}


Measured memory_bytes: 256
d, g = map(int, input().split())
p = []
c = []

for i in range(d):
  p_i, c_i = input().split()
  p.append(int(p_i))
  c.append(int(c_i))

ans = float("inf")

for bit in range(2 ** d):
  count = 0
  sum = 0
  nokori = set(range(1, d + 1))


  for i in range(d):
    if ((bit >> i) & 1):
      sum += p[i] * (i + 1) * 100 + c[i]
      count += p[i]
      nokori.discard(i + 1)

  if sum < g:
    use = max(nokori)
    n = min(p[use - 1], -(-(g - sum) // (use * 100)))
    count += n
    sum += n * use * 100

  if sum >= g:
    ans = min(ans, count)

print(ans)



Measured memory_bytes: 41616
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define chkmin(a,b) a=min(a,b)
#define chkmax(a,b) a=max(a,b)
#define mid (l+r>>1)
#define N 300010
using namespace std;
int w,h,n,m,z[N],hl[N],hr[N],ans,topl,topr;
int read()
{
	int x=0,f=1;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*f;
}
struct pt
{
	int x,y;
}a[N],sl[N],sr[N];
struct tree
{
	int add,mx;
	tree *ls,*rs;
	tree(){ls=rs=0;mx=add=0;}
	void update()
	{
		mx=max(ls->mx,rs->mx);
	}
	void cal(int d)
	{
		mx+=d;add+=d;
	}
	void pushdown()
	{
		ls->cal(add);rs->cal(add);
		add=0;	
	}
	void build(int l,int r)
	{
		if(l==r) return ;
		(ls=new tree)->build(l,mid);
		(rs=new tree)->build(mid+1,r);
	}
	void mdf(int l,int r,int lx,int rx,int d)
	{
		if(lx>rx||d==0) return ;
		if(l==lx&&r==rx) {cal(d);return ;}
		pushdown();
		if(rx<=mid) ls->mdf(l,mid,lx,rx,d);
		else if(lx>mid) rs->mdf(mid+1,r,lx,rx,d);
		else ls->mdf(l,mid,lx,mid,d),rs->mdf(mid+1,r,mid+1,rx,d);
		update();
	}
}*xtr;
void work()
{
	for(int i=1;i<=n;i++)
		z[i]=a[i].y;
	sort(z,z+n+1);z[n+1]=h;
	m=unique(z,z+n+2)-z-1;
	for(int i=1;i<=n;i++)
		a[i].y=lower_bound(z,z+m+1,a[i].y)-z;
	(xtr=new tree)->build(0,m);	
	for(int i=0;i<=m;i++)
		hl[i]=0,hr[i]=w;	
	for(int i=1;i<=n;i++)
		if(a[i].x<=(w>>1)) chkmax(hl[a[i].y],a[i].x);
		else chkmin(hr[a[i].y],a[i].x);			
	topl=topr=0;
	xtr->mdf(0,m,0,m,w);
	for(int i=1;i<=m;i++)
	{
		xtr->mdf(0,m,0,i-1,z[i]-z[i-1]);
		chkmax(ans,xtr->mx);
		for(;topl&&hl[i]>=sl[topl].x;topl--)
			xtr->mdf(0,m,sl[topl-1].y,sl[topl].y-1,sl[topl].x-hl[i]);	
		for(;topr&&hr[i]<=sr[topr].x;topr--)
			xtr->mdf(0,m,sr[topr-1].y,sr[topr].y-1,hr[i]-sr[topr].x);
		xtr->mdf(0,m,i-1,i-1,hr[i]-hl[i]-w);
		sl[++topl]=(pt){hl[i],i};sr[++topr]=(pt){hr[i],i};
		
	}	
}
int main()
{
	w=read();h=read();n=read();
	for(int i=1;i<=n;i++)
		a[i].x=read(),a[i].y=read();
	work();
	for(int i=1;i<=n;i++)
		a[i].y=z[a[i].y];
	swap(w,h);
	for(int i=1;i<=n;i++)
		swap(a[i].x,a[i].y);	
	work();
	printf("%d",ans<<1);			
	return 0;
}


Measured memory_bytes: 46848
import heapq
import math

N,M=map(int,input().split())
h=[]

for i in input().split():
	heapq.heappush(h, -int(i))

for i in range(M):
	t=heapq.heappop(h)
	heapq.heappush(h, math.ceil(t/2))

print(-sum(h))

Measured memory_bytes: 14532
#include <bits/stdc++.h>
using namespace std;;
#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 F first
#define S second
#define mp make_pair
#define pb push_back
const int MOD = 1000000007;
 
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
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> a(n);
  rep(i,n){
    cin >> a[i];
  }
  rep(i,k){
    vector<ll> v(n+1,0);
    rep(i,n){
      ll mi = max(0ll,i-a[i]);
      ll ma = min(n,i+a[i]+1);
      v[mi]++;
      v[ma]--;
    }
    rep(i,n+1){
      v[i+1] += v[i];
    }
    rep(i,n){
      a[i] = v[i];
    }
    if(v[0] == n){
      break;
    }
  }
  rep(i,n){
    cout << a[i] << " ";
  }
  cout << endl;
}

Measured memory_bytes: 6368
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}
ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]
mp = lambda :map(int,input().split())

k = inp()
a,b = mp()
#print(a//k , a//k)
if a%k == 0 or b%k == 0 or b//k - a//k > 0:
    print("OK")
else:
    print("NG")

Measured memory_bytes: 61712
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> v(n);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        v[i] = {b, a};
    }
    sort(v.begin(), v.end(), greater<>());

    int ret = 0;
    set<vector<int>> use;
    set<vector<int>> notuse;
    long long x = 0; // keep under zero
    int limit = v[0][0];
    for (int i = 0; i < n; i++) {
        x += use.size() * (limit - v[i][0]);
        limit = v[i][0];

        use.insert({v[i][1], i});
        x -= limit - v[i][1];

        auto itr = --use.end();
        while (x > 0) {
            auto now = *itr;
            x += limit - now[0];
            itr = use.erase(itr);
            if (!use.empty())--itr;
            notuse.insert(now);
        }
        itr = notuse.begin();
        while (!notuse.empty()) {
            auto now = *itr;
            if (x - (limit - now[0]) <= 0) {
                x -= limit - now[0];
                itr = notuse.erase(itr);
                use.insert(now);
            } else {
                break;
            }
        }

        ret = max(ret, (int) use.size());
    }

    cout << ret << endl;
    return 0;
}


Measured memory_bytes: 47480
import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        int N = scan.nextInt();
        int R = scan.nextInt();
        if(N < 10) {
            os.println(R+100*(10-N));
        } else {
            os.println(R);
        }
    }
}

Measured memory_bytes: 21844
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
#include <ctime>
#include <numeric>
#include <fstream>
#include <valarray>
using namespace std;
static const double EPS = 1e-6;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int(a.size()))
#define F first
#define S second
const long double pi=acos(-1.0);
int dx[]={0,1,0,-1,1,1,-1,-1,0},dy[]={1,0,-1,0,1,-1,1,-1,0};


vector<int> ans;
int num[15];
int dist[15];
int sen[15];
int n;

double dp[1<<15][15];
double vis[1<<15][15];

double rec(int st,int la,int we){
  if(st==(1<<la))return 0;
  if(vis[st][la])return dp[st][la];
  vis[st][la]=true;
  double& ret=dp[st][la]=1LL<<30LL;
  rep(i,n){
    if((~st&(1<<i)) || i==la)continue;
    int tdi=abs(dist[i]-dist[la]);
    int twe=we-sen[la]*20;
    double sp=2000/(70.0+twe);
    ret=min(ret,rec(st^(1<<la),i,twe)+tdi/sp);
  }
  return ret;
}

void trace(int st,int la,int we){
  ans.pb(la);
  if(st==(1<<la))return;
  rep(i,n){
    if((~st&(1<<i)) || i==la)continue;
    int tdi=abs(dist[i]-dist[la]);
    int twe=we-sen[la]*20;
    double sp=2000/(70.0+twe);
    if(abs(dp[st][la]-dp[st^(1<<la)][i]-tdi/sp)<EPS){
      trace(st^(1<<la),i,twe);
      return;
    }
  }
}

main(){
  cin>>n;
  int we=0;
  rep(i,n){
    cin>>num[i]>>dist[i]>>sen[i];
    we+=sen[i]*20;
  }
  double mint=rec((1<<n)-1,0,we);
  int st=0;
  rep(i,n)
    if(mint>rec((1<<n)-1,i,we)){
      mint=rec((1<<n)-1,i,we);
      st=i;
    }
  trace((1<<n)-1,st,we);
  reverse(ALL(ans));
  rep(i,SZ(ans)){
    if(i)cout<<' ';
    cout<<num[ans[i]];
  }
  cout<<endl;

}

Measured memory_bytes: 0
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	N, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	scanner.Scan()
	M, _ := strconv.ParseInt(scanner.Text(), 10, 0)

	A := [50]string{}
	B := [50]string{}

	var i, j, y, x int64
	for i = 0; i < N; i++ {
		scanner.Scan()
		A[i] = scanner.Text()
	}
	for i = 0; i < M; i++ {
		scanner.Scan()
		B[i] = scanner.Text()
	}

	ret := false

	for i = 0; i < N-M+1; i++ {
		for j = 0; j < N-M+1; j++ {
			flg := true
			for y = 0; y < M; y++ {
				for x = 0; x < M; x++ {
					if A[i+y][j+x] != B[y][x] {
						flg = false
					}
				}
			}
			if flg {
				ret = true
			}
		}
	}

	if ret {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


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

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 2005;
const ll inf = (ll)1e14;
ll dp[N][N];

int main(){
  fastIO;
  int n;
  cin >> n;
  for(int i = 0; i <= n; i ++ ){
    for(int j = 0 ; j <= n; j ++ ){
      dp[i][j] = -inf;
    }
  }
  vector<pii> t(n);
  for(int i = 0 ; i < n; i ++ ){
    cin >> t[i].fi;
    t[i].se = i + 1;
  }
  sort(t.begin(), t.end());
  reverse(t.begin(), t.end());
  dp[0][0] = 0;
  int las;
  int low;
  for(int i = 0 ; i < n; i ++ ){
    for(int j = 0 ; j <= i ; j ++ ){
      las = n - (i-j);
      low = j + 1;
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + abs(t[i].se - low) * 1ll * t[i].fi);
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(t[i].se - las) * 1ll * t[i].fi);
    }
  }
  ll ans = 0;
  for(int i = 0 ; i <= n; i ++ )
    ans = max(ans, dp[n][i]);
  cout << ans << "\n";
  return 0;
}

Measured memory_bytes: 35032
from math import ceil
A, B = map(int, input().split())
min8 = A/0.08
max8 = (A+1)/0.08-1
min10 = B/0.1
max10 = (B+1)/0.1-1
if min8>max10 or min10>max8:
  print(-1)
else:
  print(max(ceil(min8),ceil(min10)))

Measured memory_bytes: 3064
def resolve():
    N,M,C = map(int,input().split())
    *B, = map(int,input().split())
    count=0
    for i in range(N):
        A = map(int,input().split())
        if sum(map(lambda x,y:x*y,A,B))+C>0:
            count+=1
    print(count)

resolve()

Measured memory_bytes: 9180
#include <bits/stdc++.h>
#define endl        '\n'
#define db          double
#define ld          long double
#define ll          long long
#define ull         unsigned long long
#define sqr(x)      (x) * (x)
#define gcd(a, b)   __gcd(a, b)
#define lcm(a, b)   ((a/gcd(a,b)) * b)
#define pf(x) 		push_front(x)
#define pb(x)       push_back(x)
#define eb(x)		emplace_back(x)
#define all(x)      (x).begin(), (x).end()
#define rall(x) 	(x).rbegin(), (x).rend()
#define sz(x) 		(int)x.size()
#define debug(x) 	cerr << #x << " = " << (x) <<endl
#define unsyncIO    ios_base::sync_with_stdio(0); cin.tie(0)

const ld PI = acos((ld)-1);
const int MOD = 1e9+7;
const ll INF = 1e18; 

using namespace std;

int main()
{
    //freopen("/home/taalpatar_shepai/Documents/my_codes/out.txt", "w", stdout);
    int n;
    cin >> n;
    ll power = 1;
    for (int i = 1; i <= n; i++) {
        power = ( power * i ) % MOD;

    }
    cout << power % MOD << endl;
    return 0;
}

Measured memory_bytes: 3608
#include<stdio.h>
int main()
{
	int x;
	int a;
	while(~scanf("%d %d",&x,&a)){
		if(x<a){
			printf("0\n");
		}
		if(x>=a){
			printf("10\n");
		}
	}
	
	return 0;
 } 

Measured memory_bytes: 128
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll s[3*100000];
int main(void){
    int n;
    cin>>n;
    for(int i=0;i<n*3;i++){
        cin>>s[i];
    }
    sort(s,s+n*3);
    reverse(s,s+n*3);
    ll ans=0;
    for(int i=0;i<n;i++){
        ans+=s[2*i+1];
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 2560
#include<bits/stdc++.h>
using namespace std;
int main(){
    int s;
    cin >> s;
    map<int,int>Map;
    for(int i = 0; i < 1000000; i++) {
        if(Map[s] > 0) {
            cout << i+1 << endl;
            return 0;
        }
        Map[s]++;
        if(s%2 == 0) {
            s/=2;
        }
        else {
            s+=s*2+1;
        }
    }
    cout << s << endl;
}



Measured memory_bytes: 256
#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;



int main(){
    ll a,b,n;
    cin>>a>>b>>n;
    ll ans=E;
    for(ll i=1;i<=n;i++){
        for(ll t=1;t<=n;t++){
            if(b%t){continue;}
            ll A=b/t*i;
            ans=min(ans,abs(a-A));
        }
    }
    cout<<ans<<endl;
    
    return 0;
}



Measured memory_bytes: 3108
/**
 *  A - Sheep and Wolves
 * https://atcoder.jp/contests/abc164/tasks/abc164_a
 */
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    // 羊が S 匹、狼が W匹います。
    // 狼の数が羊の数以上のとき、羊は狼に襲われてしまいます
    int S, W;
    cin >> S >> W;
    // 羊が狼に襲われるなら unsafe、襲われないなら safe を出力してください。
    cout << (W >= S ? "unsafe" : "safe") << endl;
}
 

Measured memory_bytes: 3552
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) {if(a<b) a=b;}
#define chmin(a, b) {if(a>b) a=b;}
#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF=1e18;
static const ll MAX=101010;
static const ll MOD=1e9+7;


int main(void) {
  ll N;
  cin >> N;
  ll a[MAX];
  ll i;
  
  for(i=0; i<N; i++) {
    cin >> a[i];
  }
  
  deque<ll> q;
  q.push_front(INF);
  for(i=0; i<N; i++) {
    
    if(a[i]<=q[0]) {
      q.push_front(a[i]);
    }else {
      *(lower_bound(q.begin(), q.end(), a[i])-1)=a[i];
    }
    
  }
  
  pt(q.size()-1);
  
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int n,m;
priority_queue<P> q;

int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		q.push({-x,1});
	}
	for(int i=0;i<m;i++){
		int t,x,cnt=0;
		cin>>t>>x;
		while(-q.top().first<=x){
			P p=q.top();q.pop();
			cnt+=p.second;
			if(cnt>=t){
				if(cnt>t) q.push({p.first,cnt-t});
				cnt=t;
				break;
			}
		}
		q.push({-x,cnt});
	}
	ll res=0;
	while(!q.empty()){
		P p=q.top();q.pop();
		res-=(ll)p.first*p.second;
	}
	cout<<res<<endl;
}

Measured memory_bytes: 1400
def digitsum(n):
    tmp = 0
    while n > 0:
        tmp += n % 10
        n //= 10
    return tmp


N = int(input())
m = 10**5+1
for i in range(1, N//2+1):
    A = digitsum(i)
    B = digitsum(N-i)
    m = min(m, A+B)
print(str(m))

Measured memory_bytes: 9104
use proconio::{
   fastout, input,
   marker::{Bytes, Chars, Isize1, Usize1},
};

use num::integer::*;

fn f(a: isize, b: isize, i: isize) -> isize {
   return ((a * i) / b) - a * (i / b);
}

fn main() {
   input! {
      a:isize,b:isize,n:isize
   }
   let mut ans = 0_isize;
   ans = std::cmp::max(ans, f(a, b, n));
   let e = lcm(a, b) / a;
   let g = lcm(a, b) / b;
   for i in -100..=100 {
      if 0 <= (n / b) * b - i && (n / b) * b - i <= n {
         ans = std::cmp::max(ans, f(a, b, (n / b) * b - i));
      }
      if 0 <= (n / e) * e - i && (n / e) * e - i <= n {
         ans = std::cmp::max(ans, f(a, b, (n / e) * e - i));
      }
      if 0 <= (n / g) * g - i && (n / g) * g - i <= n {
         ans = std::cmp::max(ans, f(a, b, (n / g) * g - i));
      }
   }
   for i in 0..=std::cmp::min(1000000, n) {
      ans = std::cmp::max(ans, f(a, b, i));
   }

   println!("{}", ans);
}


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

#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple

using namespace std;

struct struk1
{
    int x;
};
struct struk2
{
    int x;
};
inline bool operator<(const struk1& a, const struk1& b)
{
    return a.x < b.x;
}
inline bool operator<(const struk2& a, const struk2& b)
{
    return a.x > b.x;
}

priority_queue<struk2> maxi;
priority_queue<struk1> mini;
ll sumMax = 0;
ll sumMin = 0;
ll sumB = 0;

void balans()
{
    if(abs(mini.size() - maxi.size()) <= 1) return;
    if(mini.size() > maxi.size())
    {
        //while(!mini.empty()) cout << mini.top().x << " ", mini.pop();
        ll broj = mini.top().x;
        mini.pop();
        sumMin -= broj;
        sumMax += broj;
        maxi.push({broj});
    }
    else
    {
        ll broj = maxi.top().x;
        maxi.pop();
        sumMax -= broj;
        sumMin += broj;
        mini.push({broj});
    }
}

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

    int q;
    cin >> q;
    int cnt = 0;
    for(int i = 0; i < q; i++)
    {
        int t;
        cin >> t;
        if(t == 1)
        {
            cnt++;
            int a, b;
            cin >> a >> b;
            sumB += b;
            if(mini.size() == 0 || mini.top().x >= a) mini.push({a}), sumMin += a;
            else maxi.push({a}), sumMax += a;
            balans();
        }
        else
        {
            //cout << sumMin << " " << sumMax << "\n";
            ll mul = 0;
            if(mini.size() > maxi.size()) mul = 1;
            else if(mini.size() < maxi.size()) mul = -1;
            ll broj = 0;
            if(mini.size() >= maxi.size()) cout << mini.top().x << " ", broj = mini.top().x;
            else cout << maxi.top().x << " ", broj = maxi.top().x;

            cout << mul*broj + sumMax - sumMin + sumB << "\n";
        }
    }

    return 0;
}


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

typedef long long ll;

int main()
{
    ll N;
    cin >> N;
    vector<ll> A;
    vector<ll> B;
    for (ll i = 0; i < N + 1; ++i)
    {
        ll temp;
        cin >> temp;
        A.push_back(temp);
    }

    for (ll i = 0; i < N; ++i)
    {
        ll temp;
        cin >> temp;
        B.push_back(temp);
    }
    ll ans = 0;

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

    cout << ans << endl;
}


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

#define MAX 1400+1
#define BLOCK 1

struct Rectangle{ int height;  int pos; };

int H, W;

int getLargestRectangle( int size, int buffer[] )
{
	stack<Rectangle> S;
	int maxv = 0;
	buffer[size] = 0;

	for( int i = 0; i <= size; i++ ) {
		Rectangle rect;
		rect.height = buffer[i];
		rect.pos = i;
		if ( S.empty() ) {
			S.push( rect );
		} else {
			if ( S.top().height < rect.height ) {
				S.push( rect );
			} else if ( S.top().height > rect.height ) {
				int target = i;
				while ( !S.empty() && S.top().height >= rect.height ) {
					Rectangle pre = S.top(); S.pop();
					int area = pre.height * (i - pre.pos);
					maxv = max( maxv, area );
					target = pre.pos;
				}
				rect.pos = target;
				S.push(rect);
			}
		}
	}
	return maxv;
}

int buffer[MAX][MAX];
int T[MAX][MAX];

int getLargestRectangle()
{
	for( int i = 0; i < H; i++ ) {
		for( int j = 0; j < W; j++ ) {
			T[i][j] = 0;
		}
	}

	for( int j = 0; j < W; j++ ) {
		int sequence = 0;
		for( int i = 0; i < H; i++ ) {
			if( buffer[i][j] == BLOCK ) {
				sequence = T[i][j] = 0;
			}
			else {
				T[i][j] = ++sequence;
			}
		}
	}

	int maxv = 0;
	for( int i = 0; i < H; i++ ) {
		maxv = max( maxv, getLargestRectangle( W, T[i] ) );
	}

	return maxv;
}

int main()
{
	cin >> H >> W;
	for ( int i = 0; i < H; i++ ){
		for ( int j = 0; j < W; j++ ){
			cin >> buffer[i][j];
		}
	}

	cout << getLargestRectangle() << endl;;

	return 0;
}



Measured memory_bytes: 18448
// Copyright lzt
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<iostream>
#include<queue>
#include<string>
#include<ctime>
using namespace std;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef std::pair<long long, long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, j, k)  for (register int i = (int)(j); i <= (int)(k); i++)
#define rrep(i, j, k) for (register int i = (int)(j); i >= (int)(k); i--)
#define Debug(...) fprintf(stderr, __VA_ARGS__)

inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int mod = 1e9 + 7;
const int maxn = 100100;
int n;
vector<int> G[maxn];
int f[maxn][2];

void dfs(int u, int pa) {
  f[u][0] = f[u][1] = 1;
  rep(i, 0, G[u].size() - 1) {
    int v = G[u][i];
    if (v == pa) continue;
    dfs(v, u);
    f[u][0] = f[u][0] * 1ll * (f[v][0] + f[v][1]) % mod;
    f[u][1] = f[u][1] * 1ll * f[v][0] % mod;
  }
}

void work() {
  n = read();
  rep(i, 1, n - 1) {
    int x = read(), y = read();
    G[x].pb(y); G[y].pb(x);
  }
  dfs(1, 0);
  cout<<(f[1][0] + f[1][1]) % mod <<endl;
}

int main() {
  #ifdef LZT
    freopen("in", "r", stdin);
  #endif

  work();

  #ifdef LZT
    Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
  #endif
}


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

H,W = map(int, input().split())
Ch,Cw = map(int, input().split())
Dh,Dw = map(int, input().split())
S = [input() for _ in range(H)]

A = [[-1 for _ in range(W)] for _ in range(H)]
A[Ch-1][Cw-1] = 0

def isValid(h,w):
  if h>=0 and h<H and w>=0 and w<W and S[h][w]=='.' and A[h][w]==-1:
    return True
  else:
    return False

warp = 0
BFS_now = [(Ch-1, Cw-1)]
BFS_search = [(Ch-1, Cw-1)]
ij = [(-1,0),(0,-1),(0,1),(1,0)]
while len(BFS_now)>0:
  while len(BFS_search)>0:
    BFS_search_tmp = []
    for h,w in BFS_search:
      for i,j in ij:
        if isValid(h+i,w+j):
          BFS_search_tmp.append((h+i,w+j))
          A[h+i][w+j] = warp
    BFS_now += BFS_search_tmp
    BFS_search = BFS_search_tmp
  BFS_now_tmp = []
  for h,w in BFS_now:
    for i in range(-2,3):
      for j in range(-2,3):
        if isValid(h+i,w+j):
          BFS_now_tmp.append((h+i,w+j))
          A[h+i][w+j] = warp + 1
  BFS_now = BFS_now_tmp
  BFS_search = BFS_now_tmp
  warp += 1

print(A[Dh-1][Dw-1])

Measured memory_bytes: 90456
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define rrep(i,n) for (int i = n-1; i >= 0; i--)
#define MOD (1000000007)
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pb push_back

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

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

ll keta(ll n){
  string s = to_string(n);
  ll num = s.size();
  return num;
}

const ll INF = 1LL << 60;

const int dh[4] = {1,0,-1,0};
const int dw[4] = {0,1,0,-1};

struct Edge{
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w){}
};
using Graph = vector<vector<Edge>>;
using P = pair<ll, int>;

const int MAX = 5e5;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(ll n, ll k){
  if(n >= MAX){
    ll tmp = 1;
    rep(i,k){
      tmp *= (n-i);
      tmp %= MOD;
    }
    return tmp*finv[k]%MOD;
  }

  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll POW(ll x, ll n){
  ll ret = 1;
  while(0 < n){
    if(n%2 == 0){
      x = x*x%MOD;
      n = n >> 1;
    }
    else{
      ret = ret*x%MOD;
      n--;
    }
  }
  return ret;
}

int main(){
  int n,Q; cin >> n >> Q;
  vector<tuple<int,int,int>> a;
  rep(i,n) {
    int s,t,x; cin>>s>>t>>x;
    a.pb(make_tuple(x,s-x,t-x));
  }
  sort(all(a));
  set<pii> s;
  rep(q,Q) {
    int d; cin >> d;
    s.insert(pii(d,q));
  }

  vi ans(Q,-1);
  rep(i,n) {
    int x,l,r;
    tie(x,l,r) = a[i];
    auto it = s.lower_bound(pii(l,0));
    while(it != s.end()){
      if(it->first >= r) break;
      ans[it->second] = x;
      s.erase(it++);
    }
  }

  rep(q,Q) cout << ans[q] << endl;
}


Measured memory_bytes: 14320
#include <bits/stdc++.h>
using namespace std;
#define ll          long long int
#define pb          push_back
#define pll         pair<ll,ll>
#define pq          priority_queue
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod         1000000007
#define inf         1000000000000000000LL
#define vl          vector<ll>
#define vll         vector<pll>
#define F           first
#define S           second
#define db          long double
#define sz(x)       (ll)x.size()
#define fix(n)      cout<<fixed<<setprecision(n)
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define all(x)      x.begin(),x.end()
#define mset(x)     memset(x,0,sizeof x)
#define pi          3.14159265358979323

ll a[200],ans=0;

int sol(ll l, ll r)
{
    if(l==r) { ans+=a[l], a[l]=0; return 0; }
    if(l>r) return 0;
    ll mn=inf;
    rep(i,l,r+1) mn=min(mn,a[i]);
    ans+=mn;
    rep(i,l,r+1) a[i]-=mn;
    rep(i,l,r+1)
    {
        if(a[i]==0){
            sol(l,i-1);
            sol(i+1,r);
            break;
        }
    }
    return 0;
}

void solve()
{
    ll n; cin>>n;
    rep(i,1,n+1) cin>>a[i];
    sol(1,n);
    cout<<ans;
}

int main()
{
    speed_up
    ll T=1;
    //cin>>T;
    while(T--)
        solve();
    return 0;
}


Measured memory_bytes: 256