akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5 + 10;
const ll mod = 1e9 + 7;

int n, k;
ll a[maxN];

int minProduct() {
  ll ret = 1;
  if (k == n) {
    for (int i = 1; i <= n; ++i) {
      ret = (ret * a[i] + mod * mod) % mod;
    }
    return ret;
  }
  if (a[1] >= 0) {
    for (int i = n; i > n - k; --i) {
      ret = (ret * a[i] + mod * mod) % mod;
    }
    return ret;
  }
  if (a[n] < 0){
    if (k % 2 == 0) {
      for (int i = 1; i <= k; ++i) {
        ret = (ret * a[i] + mod * mod) % mod;
      }
      return ret;
    } else {
      for (int i = n; i > n - k; --i) {
        ret = (ret * a[i] + mod * mod) % mod;
      }
      return ret;
    }
  }
  int i = 1, j = n;
  if (k % 2 == 1) {
    ret = a[j]; --j; --k;
  }
  while (1) {
    if (k == 0) break;
    k -= 2;
    if (a[i] * a[i+1] > a[j] * a[j-1]) {
      ret = (ret * a[i] + mod * mod) % mod;
      ret = (ret * a[i+1] + mod * mod) % mod;
      i += 2;
    } else {
      ret = (ret * a[j] + mod * mod) % mod;
      ret = (ret * a[j-1] + mod * mod) % mod;
      j -= 2;
    }
  }

  return ret;
}

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  cout << minProduct();

  return 0;
}


Measured memory_bytes: 5260
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define pb push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define ll long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repv(i,a,b) for(int i=a;i>b;i--)
#define mkp(a,b) make_pair(a,b)
#define all(v) v.begin(),v.end()
#define max3(a,b,c) max(a,(max(b,c))
#define min3(a,b,c) min(a,(min(b,c))
#define read(a) cin>>a
#define readt(a,b) read(a),read(b)
#define mod 1000000007
using namespace std;
bool compare(pair<int,int>& p1,pair<int,int>& p2)
{
    return p1.second<p2.second;
}
ll fp(vector<ll>& parents,ll a)
{
    if(parents[a]==a) return a;
    return fp(parents,parents[a]);
}
int main()
{
    fastio
    ll n,m;
    readt(n,m);
    vector<ll> parents(n+1),size(n+1);
    rep(i,1,n+1){
        parents[i]=i;
        size[i]=1;
    }
    rep(i,0,m)
    {
        ll a,b;
        readt(a,b);
        ll pa=fp(parents,a),pb=fp(parents,b);
        if(pa!=pb)
        {
            if(size[pa]>=size[pb])
            {
                size[pa]+=size[pb];
                size[pb]=0;
                parents[pb]=pa;
            }
            else
            {
                size[pb]+=size[pa];
                size[pa]=0;
                parents[pa]=pb;
                
            }
        }
    }
    ll ma=LLONG_MIN;
    rep(i,1,n+1)
    {
       if(parents[i]==i) ma=max(ma,size[i]); 
    }
    cout<<ma;
}

Measured memory_bytes: 6364
#include <iostream>
using namespace std;
typedef long long ll;
ll a[51], p[51];

ll f(ll n, ll x){
    if(x<=0)return 0;
    if(x==a[n])return p[n];
    if(x>=a[n-1]+2){
        return p[n-1]+1+f(n-1, x-=a[n-1]+2);
    }else if(x==a[n-1]+1){
        return p[n-1];
    }
    return f(n-1, x-1);
}
int main() {
    ll n, x;
    cin>>n>>x;
    a[0]=1;
    p[0]=1;
    for(int i=1;i<=n;i++){
        a[i]=a[i-1]*2+3;
        p[i]=p[i-1]*2+1;
    }
    cout<<f(n, x)<<endl;
	return 0;
}

Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_A
 * 
 * @author Kyoro
 *
 */
public class Main {

	BufferedReader reader;
	private int n;
	private long dp[];

	public Main() {
		reader = new BufferedReader(new InputStreamReader(System.in));
	}

	public static void main(String[] args) {
		Main main = new Main();
		try {
			main.read();
			long result = main.solve();
			System.out.println(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void init() {
		dp = new long[50];
	}

	private long solve() {
		// return fib(n);
		dp[0] = 1;
		dp[1] = 1;
		for (int i = 2; i <= n; i++) {
			dp[i] = dp[i-1] + dp[i-2];
		}
		return dp[n];
	}

	/**
	 * Recursion: TLE
	 */
	private long fib(int x) {
		if (x == 0 || x == 1) {
			return 1;
		}
		return fib(x - 1) + fib(x - 2);
	}

	private void read() throws IOException {
		init();
		String line = reader.readLine();
		n = Integer.parseInt(line);
	}

}


Measured memory_bytes: 24348
N = int(input())
T,A = map(int,input().split())
Hn = list(map(int,input().split()))
minnum = 10**9
minindex = 0
for h in range(len(Hn)):
    if minnum>abs((T-Hn[h]*0.006)-A):
        minnum = abs((T-Hn[h]*0.006)-A)
        minindex = h
print(minindex+1)


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

using namespace std;
typedef long long ll;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef map <str,int> mapsi;
typedef map <str,int> :: iterator mapsitr;
typedef map <int , int> mint;
typedef map <ll , ll> mll;
typedef set <int> si;
typedef set <ll> sll;
typedef si :: iterator sitr;
typedef si :: reverse_iterator rsitr;
typedef sll :: iterator sltr;
typedef sll :: reverse_iterator rsltr;
#define mset multiset

typedef mset <int> msi;
typedef mset <ll> msll;
typedef msi :: iterator msitr;
typedef msi :: reverse_iterator msritr;
typedef msll :: iterator msltr;
typedef msll :: reverse_iterator mslritr;

#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'

char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 126)return char(x-32);
    else if( 65 <= int(x) && int(x) <= 90)return x;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 126)return x;
    else if( 65 <= int(x) && int(x) <= 90)return char(x+32);
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57)return int(x-'0');
    else if( 97 <= int(x) && int(x) <= 126)return int(x-96);
    else if( 65 <= int(x) && int(x) <= 90)return int(x-64);
}
bool isect (int l1, int r1, int l2, int r2){
    pii p1,p2;
    p1 = mp(l1,r1); p2 = mp(l2,r2);
    if(p1>p2)swap(p1,p2);
    if(p2.fi <= p1.se)return true;
    else return false;
}
ll fexp (ll num1, ll num2, ll MOD){
    if(num2==0)return 1;
    else if(num2==1)return num1;
    else{
        ll temp = fexp (num1,num2/2,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
        if(num2%2==1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
    }
}
ll invmod (ll num, ll MOD){return fexp (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
    if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
    while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
    return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/gcd(num1,num2))*num2;}


// end of Template
int dua[10],ans,n;
int main(){

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    dua[0] = 1;
    for(int i=1;i<=9;i++)dua[i]=2*dua[i-1];

    cin>>n;
    for(int i=0;i<=9;i++){
        if(dua[i]<=n)ans=dua[i];
        else break;
    }
    cout<<ans<<newl;

}


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

const int N = 2e5 + 5;

int n, mx = 0;
map<string, int> mp;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        mx = max(mx, ++mp[s]);
    }

    for (auto it : mp) {
        if (it.second == mx) {
            cout << it.first << endl;
        }
    }
}


Measured memory_bytes: 19148
N, M = map(int, input().split())
#A = [int(input()) for i in range(M)]
flag = [0]*N
for i in range(M):
    flag[int(input())-1] = 1
#A = [i*3 for i in range(N//3)]
#for i in range(N//3):
    #flag[i] = 1

mod = 10**9+7

dp = [0] * N

if flag[0]==0:
#if 1 not in A:
    dp[0] = 1
    if N>1:
        if flag[1]==0:
    #if (2 not in A) and N > 1:
            dp[1] = 2
elif N > 1:
    if flag[1]==0:
    #if 2 not in A:
        dp[1] = 1
    else:
        dp[1] = 0

loop = []
'''
for i in range(2, N):
    if i not in A:
        loop.append(i)
'''
for i in range(2, N):
    if flag[i]==0:
        dp[i] = (dp[i-2] + dp[i-1]) % mod
        #dp[i] = (sum(dp[i-2:i])) % mod

print(dp[N-1])
#print(dp)

Measured memory_bytes: 7668
using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

class Solve
{
	const int mod = 1000000007;
	static void Main(string[] args)
	{
		var input = Console.ReadLine().Split().Select(long.Parse).ToArray();
		int N = (int)input[0];
		long K = input[1];
		List<Tuple<long, long>> ab = new List<Tuple<long, long>>();

        for(var i = 0; i < N; i++)
		{
			var p = Console.ReadLine().Split().Select(long.Parse).ToArray();
			ab.Add(new Tuple<long, long>(p[0], p[1]));
		}
		ab.Sort();
		long ans = 0;
		long sum = 0;
        for(var i = 0; i < N; i++)
		{
			sum += ab[i].Item2;
			if (sum >= K)
			{
				ans = ab[i].Item1;
				break;
			}
		}
		Console.WriteLine(ans);


	}
}


Measured memory_bytes: 24372
import java.util.*;
	import java.util.Map.Entry;
	 
	 
	 class Main {
		 static int mod =  (int) (Math.pow(10,9)+7);
//		 static int mod =  (int) 998244353;
		 public static void main(String[] args) {
	    	
	        Scanner sc = new Scanner(System.in);

	        double X = sc.nextInt();
	        double x=0;
	        double y=0;
	        int ans=0;
        	x=x+Math.cos(Math.toRadians(X));
        	y=y+Math.sin(Math.toRadians(X));
        	ans++;
        	for(int i=2;Math.abs(x)>0.001 || Math.abs(y)>0.001;i++) {
	        	x=x+Math.cos(Math.toRadians(i*X));
	        	y=y+Math.sin(Math.toRadians(i*X));
	        	//System.out.println(y);
	        	ans++;
	        }
	        
	        System.out.println(ans);
	    }
	}

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

using namespace std;

#define MP make_pair
#define PB push_back
#define f first
#define s second

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

//int dr1[] = {-1, 0, 1, 0};
//int dc1[] = {0, 1, 0, -1};
//int dr3[] = {0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1, -1, 1, 0, 0};
//int dc3[] = {3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1, 2, 0, 0, -1, 1};
const int MOD = 1000000007;
const int INF = 1000000007;

ll modx(ll a)
{
    return (a%MOD+MOD)%MOD;
}

ll times(ll a, ll b)
{
    return modx(modx(a)*modx(b));
}

ll add(ll a, ll b)
{
    return modx(modx(a)+modx(b));
}

ll sub(ll a, ll b)
{
    return modx(MOD+modx(a)-modx(b));
}

ll fastExp(ll base, ll exp)
{
    if(exp == 0LL)
    {
        return 1LL;
    }

    if(exp%2==0)
    {
        return fastExp(times(base, base), exp/2);
    }
    else
    {
        return times(exp, fastExp(times(base, base), exp/2));
    }
}


const int MAXN = 51;
int N;


int main()
{
    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int K, X;
    cin >> K >> X;
    for(int i = X-K+1; i <= X+K-2; i++)
    {
        cout << i << " ";
    }
    cout << X+K-1 << "\n";
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
int main(void) 
{
	int n, a;
	scanf("%d", &n);
	printf("%d: ", n);
	a = 2;
	while (n >= a * a) {
		if (n % a == 0) {
			printf("%d ", a);
			n /= a;
		}
		else 
			a++;
	}
	printf("%d\n", n);
	return 0;
}



Measured memory_bytes: 2144
#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

int main() {
    cin>>N;
    cout<<(N == 0 ? 1 : 0)<<endl;
}

Measured memory_bytes: 3632
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<math.h>
#include<string>
#include <iomanip>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

//マクロ定義
#define vvint(vec,n,m,l) vector<vector<int>> vec(n, vector<int>(m,l));	// lで初期化
#define vvll(vec,n,m,l) vector<vector<ll>> vec(n,vector<ll>(m,l));
#define vint vector<int>
#define pint pair<int,int>
#define rep(i,a) for(int i=0;i<(a);i++)
#define all(x) (x).begin(),(x).end()
#define debug system("pause")				//デバッグ用
#define ret return 0

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

using Graph=vector<vector<ll>>;

#define ketasitei setprecision(15) //15桁表示

const ll MOD = 1000000007;


int main(void)

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

	ll a, b;
	cin >> a >> b;
	ll m;
	cin >> m;
	vector<ll> aa(a), bb(b);
	rep(i, a)
	{
		cin >> aa[i];
	}
	rep(i, b)
	{
		cin >> bb[i];
	}

	vector<ll> x(m), y(m), c(m);
	ll ans = INF;
	rep(i, m)
	{
		cin >> x[i] >> y[i] >> c[i];
		ans = min(ans, aa[x[i] - 1] + bb[y[i] - 1] - c[i]);
	}
	sort(aa.begin(), aa.end());
	sort(bb.begin(), bb.end());
	ans = min( aa[0] + bb[0], ans);

	cout << ans << endl;



	return 0;
}

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

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let m = read::<usize>();
    let n = 1 << m;
    let k = read::<usize>();
    if (m == 1 && k == 1) || k >= n {
        println!("-1");
        return;
    }
    if k == 0 {
        for i in 0..n {
            if i != 0 {
                print!(" ");
            }
            print!("{} {}", i, i);
        }
        println!("");
        return;
    }
    print!("{}", k);
    for i in 1..n {
        if i == k {
            continue;
        }
        print!(" {}", i);
    }
    print!(" 0");
    print!(" {}", k);
    print!(" 0");
    for i in (1..n).rev() {
        if i == k {
            continue;
        }
        print!(" {}", i);
    }
    println!("");
}


Measured memory_bytes: 5884
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
int even = 0;
for (int i = 0; i < N; i++) {
cin >> A[i];
if (A[i] % 2 == 0) even++;
}
cout << pow(3,N) - pow(2,even) << endl;
return 0;
}

Measured memory_bytes: 256
/*
    Author: aksayushx
*/
#include<bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(),a.end()
#define mp make_pair
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;

/*
void compute()
{
	fac[0]=1;
	for(ll i=1;i<=10000;i++)
	fac[i]=(i*fac[i-1])%mod;
}
ll power(ll x, ll y)  
{  
    ll res = 1; 
    x = x % mod;
    while (y > 0)  
    {  
        if (y & 1)  
            res = (res*x) % mod;  
        y = y>>1; 
        x = (x*x) % mod;  
    }  
    return res;  
} 
ll modInverse(ll n) 
{ 
    return power(n, mod-2); 
}
ll nCr(ll n,ll r) 
{ 
    if (r == 0) 
        return 1; 
    return (fac[n] * modInverse(fac[r]) % mod * modInverse(fac[n - r]) % mod) % mod; 
}*/ 
ll n,m;
vector<vector<ll>> adj;
vector<ll> d;
vector<bool> visited;
ll dfs(ll x)
{
    visited[x]=true;
    if(d[x]!=-1)
    return d[x];
    ll depth=0;
    for(auto c:adj[x])
    {
        if(!visited[c])
        depth=max(depth,1+dfs(c));
        else
        depth=max(depth,1+d[c]);
    }
    d[x]=depth;
    return d[x];
}
void aksayushx()
{
    cin>>n>>m;
    adj.assign(n+1,vector<ll>(0));
    ll x,y;
    visited.assign(n+1,false);
    for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        adj[x].pb(y);
    }
    d.assign(n+1,-1);
    ll ans=0;
    for(ll i=1;i<=n;i++)
    {
        if(!visited[i])
        ans=max(ans,dfs(i));
    }
    cout<<ans;
    

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    while(test--)
        aksayushx();
    return 0;
}


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

int main() {
    int h, w, n;
    cin >> h >> w >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    int index = 1;
    vector<vector<int>> grid(h, vector<int>(w, 0));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[index - 1] <= 0) index++;
            grid[i][j] = index;
            a[index - 1]--;
        }
        if (i & 1) continue;
        reverse(grid[i].begin(), grid[i].end());
    }
    for (auto v : grid) {
        for (int k : v) {
            cout << k << " ";
        }
        cout << endl;
    }
}

Measured memory_bytes: 384
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,k = readInts()
    h = [int(input()) for _ in range(n)]
    ans = float("inf")
    h = sorted(h)
    #print(h)
    for i in range(n-k+1):
        #print(h[i+k-1],h[i])
        ans = min(ans,h[i+k-1] - h[i])
    print(ans)
if __name__ == '__main__':
  main()


Measured memory_bytes: 10328
[<AutoOpen>]
module Program

open System

let read f = stdin.ReadLine() |> f
let reada f = stdin.ReadLine().Split() |> Array.map f
let reads f = stdin.ReadLine().Split() |> Array.toList |> List.map f

[<EntryPoint>]
let main _ =
    let [l;r] = reads int
    let xs = [| for i = l to min r (l + 2018) do yield i % 2019 |]
    seq {
        for i = 0 to Array.length xs - 1 do
            for j = i + 1 to Array.length xs - 1 do
                yield xs.[i] * xs.[j] % 2019
    }
    |> Seq.min
    |> printfn "%i"
    0

Measured memory_bytes: 14260
#include <bits/stdc++.h>
  
  long long a[200005];
  
  long long abs(long long in){
    return in > 0 ? in : -in;
  }
  
  int main(){
    int n;
    long long min = 2e9 + 5;
    long long sum = 0;
    scanf("%d", &n);
    for(int i = 0;i < n;++i) {
      scanf("%lld", &a[i]);
      sum += a[i];
    }
    if(n == 2) {
      printf("%lld\n", abs(a[0] - a[1]));
      return 0;
    }
    else {
      for(long long j = 1, x = a[0];j < n - 1;++j){
        x += a[j];
        //for(int j = 1;j <= i;++j) x += a[j];
        //printf("%d %d\n", x, sum - x);
        if(abs(sum - 2 * x) < min) min = abs(sum - 2 * x);
      }
    }
    printf("%lld\n", min);
    return 0;
  }

Measured memory_bytes: 1792
parseInt(x) = parse(Int, x)
readline()
f = 1
for a in split(readline())
    n = parseInt(a)
    if n % 2 == 0 && n % 3 != 0 && n % 5 != 0
        global f = 0
    end
end

println((f == 1) ? "APPROVED" : "DENIED")


Measured memory_bytes: 165896
A, B, C, D = map(int, input().split())


def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def lcm(a, b):
    return a * b // gcd(a, b)

M = lcm(C, D)
bc, bd, bm = B // C, B // D, B // M
ac, ad, am = (A - 1) // C, (A - 1) // D, (A - 1) // M

# print(M)
# print(bc, bd, bm)
# print(ac, ad, am)

c = bc - ac
d = bd - ad
m = bm - am
n = B - A + 1

ans = n - (c + d - m)

print(ans)



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

int main() {
    int A, B; cin >> A >> B;
    int rst = 0;
    if (A > B) {
        rst += A;
        A--;
    } else {
        rst += B;
        B--;
    }

    if (A > B) {
        rst += A;
        A--;
    } else {
        rst += B;
        B--;
    }

    cout << rst << endl;
}

Measured memory_bytes: 888
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
const int INFTY =2147483647;
int main(){
    int x,y,a,b,c,z,l;
    ll ans=0;
    priority_queue<int> A;
    priority_queue<int> B;
    priority_queue<int> C;
    C.push(-1);
    cin>>x>>y>>a>>b>>c;
    REP(i,a){
        cin>>z;
        A.push(z);
    }
    REP(i,b){
        cin>>z;
        B.push(z);
    }
    REP(i,c){
        cin>>z;
        C.push(z);
    }
    
    int m=0;
    int limi=x+y;
    while(limi-m>0){
        if(x==0||A.size()==0){
            A=decltype(A)();
            A.push(-1);
            x=-1;
        }
        if(y==0||B.size()==0){
            B=decltype(B)();
            B.push(-1);
            y=-1;
        }
        l=max(A.top(),max(B.top(),C.top()));
        if(l==A.top()&&x>0){
            A.pop();
            x--;
        }
        else if(l==B.top()&&y>0){
            B.pop();
            y--;
        }
        else{
            C.pop();
        }
        ans+=l;
        m++;
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 1912
#include <cstdio>

int main(){
    int x,y;
    for(int i = 0;;i++){
        scanf("%d%d",&x,&y);
        if(x  == 0 && y == 0){
            break;
        }
        if(x >= y){
            printf("%d %d\n",y,x);
            continue;
        }
        printf("%d %d\n",x,y);
    }
    return 0;
}


Measured memory_bytes: 2704
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {

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

		int N = sc.nextInt();

		Deque<Integer> deque = new ArrayDeque<Integer>();

		int[] b = new int[N];
		for(int i = 0 ; i < N ; i++){
			int p = sc.nextInt();

			if(i%2 ==0){
				deque.addLast(p);
			}
			else{
				deque.addFirst(p);
			}
		}
		if(N%2 ==0){
			for(int i = 0 ; i < N; i++){
				System.out.print(deque.getFirst() + " ");
				deque.removeFirst();
			}
		}
		else{
			for(int i = 0 ; i < N; i++){
				System.out.print(deque.getLast()+" ");
				deque.removeLast();
			}
		}
		sc.close();
	}

}


Measured memory_bytes: 96604
a,b,c,d = map(int,input().split())
total_num = b-a+1
# # of x%c==0
c_num = b//c - (a-1)//c
d_num = b//d - (a-1)//d
from fractions import gcd
lcm = c*d//gcd(c,d)
cd_num = b//lcm - (a-1)//lcm
print(total_num-c_num-d_num+cd_num)



Measured memory_bytes: 5104
def dfs(v, n, visited, graph):
    all_visited = True

    for i in range(n):
        if not visited[i]:
            all_visited = False

    if all_visited:
        return 1

    ret = 0

    for i in range(n):
        if not graph[v][i]:
            continue
        if visited[i]:
            continue
        visited[i] = True
        ret += dfs(i, n, visited, graph)
        visited[i] = False
    return ret


n, m = map(int, input().split())
graph = [[False for _ in range(n)] for _ in range(n)]

for i in range(m):
    a, b = map(int, input().split())
    graph[a - 1][b - 1] = True
    graph[b - 1][a - 1] = True

visited = [False for _ in range(n)]
visited[0] = True
print(dfs(0, n, visited, graph))


Measured memory_bytes: 3064
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

#define X first
#define Y second

typedef long long cc;

int main () {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int n;
    cin >> n;
    vector <cc> a(n);
    cc sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum += a[i];
    }
    cc cur = 0;
    cc res = sum;
    for (int i = 0; i < n; i++) {
        cur += a[i];
        if (2ll * cur <= sum) {
            res = min(res, sum - 2 * cur);
        } else {
            res = min(res, 2 * cur - sum);
        }
    }
    cout << res;
    return 0;
}
/*
2 * (x + k) = s + k
2 * x + k = s
2 * x - k = s
*/


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define INF 100000000
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 10000
using namespace std;
typedef long long ll;
typedef pair<int,bool> P;
typedef pair<P,P> PP;

struct UF{
	int par[MAX_N];
	int rank[MAX_N];
    bool piv[MAX_N];

	void init(int n){
		rep(i,n){
			par[i] = i;
			rank[i] = 0;
            piv[i] = false;
		}
	}

	int find(int x){
		if(par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
    
    bool canunite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return true;
        if(piv[x] & piv[y]) return false;
        return true;
    }

	void unite(int x, int y, int &z){
		x = find(x);
		y = find(y);
		if(x == y) return;
        
        if(piv[x] & piv[y]) z--;
		
		if(rank[x] < rank[y]){
			par[x] = y;
            piv[y] |= piv[x];
		} else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
            piv[x] |= piv[y];
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
} uf;

int n, t, k;
int ans;
struct edge{
    int w, a, b;
    bool operator>( const edge& right) const {
        return w > right.w;
    }
} e[10000];

void solve(){
    uf.init(n);
    ans = 0;
    rep(i,n-1){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        e[i].w = c; e[i].a = a; e[i].b = b;
    }
    sort(e,e+n-1,greater<edge>());
    rep(i,t){
        int a; cin >> a;
        a--;
        uf.piv[a] = true;
    }
    rep(i,n-1){
        if(t-1 == k){
            if(!uf.canunite(e[i].a,e[i].b)){
                ans += e[i].w;
            } else{
                uf.unite(e[i].a,e[i].b,t);
            }
            continue;
        }
        uf.unite(e[i].a,e[i].b,t);
    }
    cout << ans << endl;
}

int main(){
    int i = 1;
    while(cin >> n >> t >> k){
        if(n == 0 && t == 0 && k == 0) break;
        cout << "Case " << i << ": ";
        solve();
        i++;
    }
}

Measured memory_bytes: 3276
package main

import (
	"fmt"
	"os"
	"bufio"

)

func main(){
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n int
	fmt.Fscanf(in, "%d\n", &n)

	output := 0

	output += ( n / 500)* 1000

	n %= 500
	 output += (n / 5) * 5
	 fmt.Println(output)
}


Measured memory_bytes: 640
#include <iostream>
using namespace std;
int main(){
	int mountain[11] ;
	for( int i=1 ; i<=10 ; i++ ){
		cin >> mountain[i] ;
	}
	for( int i=1 ; i<=10 ; i++ ){
		for( int k=1 ; k<=10 ; k++ ){
			if( mountain[i] > mountain[k] )
				swap( mountain[i] , mountain[k] );
		}
	}
	cout << mountain[1] << endl << mountain[2] << endl << mountain[3] << endl;
}

Measured memory_bytes: 1160
# A - 高橋君とホテルイージー
N=int(input())
K=int(input())
X=int(input())
Y=int(input())
sum=0
N_count=N
K_count=K

while( (N_count>0) & (K_count>0) ):
    N_count-=1
    K_count-=1
    sum+=X
while( (N_count>0) & (K_count==0) ):
    N_count-=1
    sum+=Y
print(sum)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, a, b, c, d;
char s[MAXN];
int main() {
	read(n), read(a), read(b), read(c), read(d);
	scanf("%s", s + 1);
	for (int i = a; i <= c - 1; i++)
		if (s[i] == '#' && s[i + 1] == '#') {
			puts("No");
			return 0;
		}
	for (int i = b; i <= d - 1; i++)
		if (s[i] == '#' && s[i + 1] == '#') {
			puts("No");
			return 0;
		}
	if (d < c) {
		bool found = false;
		for (int i = b; i <= d; i++)
			if (s[i - 1] == '.' && s[i] == '.' && s[i + 1] == '.') found = true;
		if (!found) {
			puts("No");
			return 0;
		}
	}
	puts("Yes");
	return 0;
}

Measured memory_bytes: 384
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[]t = new int[N + 1];
		int[]x = new int[N + 1];
		int[]y = new int[N + 1];
		for(int i = 0; i < N; i++) {
			t[i + 1] = sc.nextInt();
			x[i + 1] = sc.nextInt();
			y[i + 1] = sc.nextInt();
		}
		sc.close();
		for(int i = 1; i <= N; i++) {
			int dt = t[i] - t[i - 1];
			int dl = Math.abs(x[i] - x[i - 1]) + Math.abs(y[i] - y[i - 1]);
			if(dt >= dl) {
				if((dt - dl) % 2 != 0) {
					System.out.println("No");
					System.exit(0);
				}
			}else {
				System.out.println("No");
				System.exit(0);
			}
		}
		System.out.println("Yes");
	}
}

Measured memory_bytes: 93480
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define int long long
using namespace std;

template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

template<typename T>
class CumulativeSum {
	vector<T> a;
	public:
	CumulativeSum(vector<T>& x) : a(x.size() + 1,0) {
		rep(i,x.size()){
			a[i + 1] = a[i] + x[i];
		}
	}
	T get(int l, int r){ // [l, r]の区間の合計を求める
		if(l > r) return 0;
		return a[r + 1] - a[l];
	}
	T operator [] (const int i) const {
		return a[i];
	}
};

signed main(){
	int n;
	string s;
	cin >> n >> s;

	vector<int> d;
	vector<int> m(n,0), c(n,0);
	int c_num = 0;
	for(int i = n - 1; i >= 0; i--){
		if(s[i] == 'D') d.emplace_back(i);
		if(s[i] == 'M') m[i] = c_num;
		if(s[i] == 'C') c_num++;
	}

	int m_num = 0;
	rep(i,n){
		if(s[i] == 'M') m_num++;
		if(s[i] == 'C') c[i] = m_num;
	}

	vector<int> fm(n,0), bc(n, 0);
	rep(i,n){ if(s[i] == 'M') fm[i]++; }
	rep(i,n){ if(s[i] == 'C') bc[i]++; }


	CumulativeSum<int> msum(m), csum(c), fmsum(fm), bcsum(bc);
	//rep(i,n + 1){ cout << msum[i] << ' '; } cout << endl;
	//rep(i,n + 1){ cout << csum[i] << ' '; } cout << endl;
	//rep(i,n + 1){ cout << bcsum[i]  ; } cout << endl;

	int q;
	cin >> q;
	rep(i,q){
		int k;
		cin >> k;

		int sum = 0;
		rep(i,d.size()){
			//cout << d[i] << ' ' << min(d[i] + k - 1,n - 1) << endl;
			//show(msum.get(d[i], min(d[i] + k - 1, n - 1)))
			//show(msum.get(min(d[i] + k - 1, n - 1) + 1, n - 1));
			//show(csum.get(min(d[i] + k - 1, n - 1) + 1, n - 1));
			sum += msum.get(d[i], min(d[i] + k - 1, n - 1));
			//sum += msum.get(min(d[i] + k - 1, n - 1) + 1, n - 1);
			//sum -= csum.get(min(d[i] + k - 1, n - 1) + 1, n - 1);
			//show(fmsum.get(d[i], min(d[i] + k - 1, n - 1)))
			//	show(bcsum.get(min(d[i] + k - 1, n - 1) + 1, n - 1))
			//	show(bcsum[min(d[i] + k - 1, n - 1) + 1])
			//	show(bcsum[n - 1])
			sum -= fmsum.get(d[i], min(d[i] + k - 1, n - 1)) *
				bcsum.get(min(d[i] + k - 1, n - 1) + 1, n - 1);
		}
		cout << sum << endl;
	}
}


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

typedef struct matrix{
    long long int m[55][55];
}M;
const int MOD = 1000000007;
long long int N, K;
M g;

M mul(M a, M b){
    M c;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            c.m[i][j] = 0;
            for(int k=0;k<N;k++){
                c.m[i][j] += (a.m[i][k] * b.m[k][j]) % MOD;
                c.m[i][j] %= MOD;
            }
        }
    }
    return c;
}

M exp(M a, long long int p){
    if(p == 1){
        return a;
    }
    M c = exp(a, p/2);
    c = mul(c, c);
    if(p % 2 == 1)
        c = mul(a, c);
    return c;
}
int main(){
    //freopen("test.in", "r+", stdin);
    cin>>N>>K;
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
            cin>>g.m[i][j];

    g = exp(g, K);
    long long int result = 0;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            result += g.m[i][j];
            result %= MOD;
        }
    }
    cout<<result<<endl;
}


Measured memory_bytes: 11264
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Attoparsec.ByteString.Char8
import Data.List

main :: IO ()
main = do
  contents <- B.getContents
  case parseOnly inputParser contents of
    Right r -> print $ solve r
    Left  e -> error e

inputParser = decimal

solve :: Int -> Int
solve n = subtract 2 $ foldl' f maxBound [1 .. n_max]
  where
    n_max = floor $ sqrt $ fromIntegral n

    f m a
      | r > 0 = m
      | otherwise = m `min` (a + b)
      where (b, r) = n `divMod` a

Measured memory_bytes: 508
#include "bits/stdc++.h"

typedef long long ll;
#define all(x) (x).begin(), (x).end()  // sortなどの引数を省略
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define max3(x, y, z) max(x, max(y, z))
#define min3(x, y, z) min(x, min(y, z))

#ifdef _MSC_FULL_VER  //デバッグ出力
#define dout cout
#define debug() if (true)
#define check(x) std::cout << "★" << #x << "の値:" << (x) << endl
#define pass(x) std::cout << "☆" << x << endl
#else
#define dout \
  if (false) cout
#define debug() if (false)
#define check(x) \
  if (false) cout << "★" << #x << "の値:" << (x) << endl
#define pass(x) \
  if (false) cout << "☆" << x << endl
#endif

using namespace std;
int main() {
  int N,x;
  cin >> N >> x;
  vector<int> list(N);
  rep(i, N) { cin >> list[i]; }
  sort(all(list));
  int cnt = 0;
  while (x>0){
    if (cnt == N) {
      cnt--;
      break;
    }
    x-=list[cnt];
    if (x<0)break;
    cnt++;
    //cout << x << endl;
  }
  cout << cnt;
}
/*
//よく使うやつ
int N;
cin >> N;
vector<int> list(N);
rep(i,N) {
  cin >> list[i];
}
//配列(スタックとしても使える)
vector<int> list;
list.at(i)
vector<vector<int>> list(10, vector<int>(10));
//ソート クイックだからO(NlogN)
sort(all(list));
sort(all(list), greater<int>());

//配列の内容表示
dout << "====listの内容====\n";
for (int edp = 0; edp < N; edp++) dout << list[edp] << endl;
dout << "====ここまで====\n";


//配列の最小最大(Nは要素数)
*min_element(all(c))
*max_element(all(c))

//a[0]からa[N]を逆順にする(計算量はO(N))
reverse(all(a));
//listの末尾に要素を追加
list.push_back(10);
//listの末尾から要素を削除
list.pop_back();
//空ですか？
list.empty()



//キュー(待ち行列)
//定義
queue<int> a;
//追加
a.push(x);
//取る
a.pop();
//チェック
a.front()
//サイズ
a.size()
//空ですか
a.empty()



//優先度付きキュー※全部計算量がO(NlogN)あるので注意
// int 型の要素を持ち、最も小さい値を取り出す形の priority_queue を定義
priority_queue<int, vector<int>, greater<int>> Q1;
// double 型の要素を持ち、最も大きい値を取り出す形の priority_queueを定義
priority_queue<double, vector<double>, less<double>> Q2;
//追加
a.push(x);
//取る
a.pop();
//一番小さいの教えて下さい
a.top()
//サイズ
a.size()
//空ですか
a.empty()



//部分文字列
//添字3から5文字
s.substr(3,5)
//添字3から全部
s.substr(3)

//入れ替え
swap(a,b);



// int 型の番地に string 型の変数を記録する場合（2^31 要素の string型配列と同じような感じ）
map<int, string> mp;
for (auto itr =mp.begin(); itr != mp.end(); itr++) {
    //順番にマップを眺める。itr->firstでキー、itr->secondがバリュー
}

//ソート済みのaの中で初めてx以上になるインデックスを返す
lower_bound(all(a), x) - a

//next_permutation
do {
    //ここにパーテーションがくる
  } while (next_permutation(s.begin(), s.end()));

*/


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;

int main(int argc, char *argv[])
{
  int a[3][3];
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
      scanf("%d",&a[i][j]);
    }
  }

  int tmp1 = a[0][1]-a[0][0];
  int tmp2 = a[0][2]-a[0][1];
  for (int i = 0; i < 3; i++) {
    if(tmp1 == a[i][1]-a[i][0] && tmp2 == a[i][2]-a[i][1]){
      continue;
    }else{
      printf("No\n");
      return 0;
    }
  }

  printf("Yes\n");
  
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline T chmax(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T chmin(T& a, const T b) { return a = (a > b) ? b : a; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;


signed main() {
  string s; 
  cin >> s;

  string t = "CODEFESTIVAL2016";

  int ans = 0;
  REP(i, s.size()){
    if(s[i] != t[i]) ans++;
  }

  cout << ans << endl;

  return 0;
}

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

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

const long long INF = 1LL << 60;

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

  int sum = 0;
  REP(i, 1, n){
    string S = to_string(i);
    int s = 0;
    rep(j, S.size()) s += S[j] - '0';
    if(a <= s && s <= b) sum += i;
  }
  
  cout << sum << endl;

  return 0;
}


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

using lint = long long;

vector<lint> A, B, C;

struct Position {
    int i, j, k;

    lint sum() {
        return A[i] + B[j] + C[k];
    }
    bool operator <(Position that) const {
        return A[i] + B[j] + C[k] < that.sum();
    }
};

int main() {
    int X, Y, Z, K;
    cin >> X >> Y >> Z >> K;
    A.resize(X);
    B.resize(Y);
    C.resize(Z);
    for (int i = 0; i < X; i++) {
        cin >> A[i];
    }
    for (int i = 0; i < Y; i++) {
        cin >> B[i];
    }
    for (int i = 0; i < Z; i++) {
        cin >> C[i];
    }

    sort(A.begin(), A.end(), greater<lint>());
    sort(B.begin(), B.end(), greater<lint>());
    sort(C.begin(), C.end(), greater<lint>());

    priority_queue<Position> que;
    que.push({0, 0, 0});
    vector<vector<vector<bool>>> done(X, vector<vector<bool>>(Y, vector<bool>(Z, false)));

    int count = 0;
    while (count < K) {
        Position pos = que.top();
        que.pop();

        int i = pos.i, j = pos.j, k = pos.k;
        if (done[i][j][k] == true) {
            continue;
        } else {
            done[i][j][k] = true;
        }

        cout << pos.sum() << endl;

        
        if (i + 1 < X and done[i + 1][j][k] == false) {
            que.push({i + 1, j, k});
        }
        if (j + 1 < Y and done[i][j + 1][k] == false) {
            que.push({i, j + 1, k});
        }
        if (k + 1 < Z and done[i][j][k + 1] == false) {
            que.push({i, j, k + 1});
        }

        count++;
    }

    return 0;
}

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

dp = [0]*n
dp[0] = 1000

for i in range(1, n):
    dp[i] = dp[i-1]
    for j in range(i):
        s = dp[j] // a[j]
        m = dp[j] + (a[i] - a[j]) * s
        dp[i] = max(dp[i], m)

print(dp[n-1])

Measured memory_bytes: 9080
#include <iostream>
using namespace std;
int main(){
    int n;
    cin >> n;
    int a[n],b[n+1];
    b[n]=-1;
    for (int i=0; i<n; i++){
        cin >> a[i];
        b[a[i]-1]=i;
    }
    int i=0;
    int length=0;
    for (int j=1; j<n+1; j++){
        if (b[j]>b[j-1]){
            continue;
        }else {
            length=max(length,j-i);
            i=j;
        }
    }
    cout << n-length << endl;
}



Measured memory_bytes: 1792
n = int(input())
a = [[] for i in range(n)]
for i in range(n):
    a[i] = list(map(int,input().split()))
    for j in range(n-1):
        a[i][j] -= 1
    a[i].reverse()
q = []

def check(i):
    if len(a[i]) == 0:
        return 
    j = a[i][-1]
    if len(a[j]) == 0:
        return
    if a[j][-1] == i:
        q.append([min(i,j), max(i,j)])
for i in range(n):
    check(i)
day = 0
while (len(q) > 0):
    day += 1
    prevQ = list(map(list, set(map(tuple, q))))
    q = []
    for i,j in prevQ:
        a[i].pop()
        a[j].pop()
        check(i)
        check(j)
for i in range(n):
    if len(a[i]) > 0:
        print(-1)
        exit()
print(day)

Measured memory_bytes: 76380
use std::io::stdin;
use std::cmp::min;
use std::str::FromStr;

fn main(){
    let (a, b, c): (i64, i64, i64) = get_triple();
    let d = min(a+b+1, c);
    println!("{}", d+b);
}

#[allow(dead_code)]
fn get_line() -> String {
    let mut s = String::new();
    match stdin().read_line(&mut s){
        Ok(_) => {s.trim().to_string()}
        Err(_) => String::new()
    }
}

#[allow(dead_code)]
fn get_vec<T: std::str::FromStr>() -> Vec<T> {
    let line = get_line();
    line.split_whitespace().filter_map(|x| x.parse().ok()).collect()
}

#[allow(dead_code)]
fn get_one<T: FromStr + Copy>() -> T {
    let v = get_vec();
    v[0]
}

#[allow(dead_code)]
fn get_pair<T: FromStr + Copy>() -> (T, T) {
    let v = get_vec();
    (v[0], v[1])
}

#[allow(dead_code)]
fn get_triple<T: FromStr + Copy>() -> (T, T, T) {
    let v = get_vec();
    (v[0], v[1], v[2])
}

#[allow(dead_code)]
fn get_chars() -> Vec<char> {
    get_line().chars().collect()
}


Measured memory_bytes: 4352
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
typedef long long int ll;
#define mod 1000000007
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define m0(x) memset(x,0,sizeof(x))


//組み合わせ（nとrが少ないとき）
int combination_low(ll n,ll r){
	const ll N = 500;
	ll C[N][N];
	memset(C, 0, sizeof(C));
	C[0][0]=1;
	for(ll n=1; n<N; n++){
		C[n][0]=C[n-1][0];
		for(ll r=1; r<N; r++){
			C[n][r] = C[n-1][r-1] + C[n-1][r];
		}
	}
	return C[n][r];
}

//二分累乗法
int powa(ll a, ll n){
	ll res = 1;
	while(n>0) {
		if(n&1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1; 
	}
	return res;
}

//逆元
int inverse(ll a){
	ll n = mod-2;
	return powa(a, n);
}

/*組み合わせ（いちばんうまくいったやつ）*/
int combination(ll n,ll r){
	ll x=1,y=1;
	for(int i=1;i<=r;i++){
		x = x * (n-i+1) % mod;
		y = y * i % mod;
	}
	return x * inverse(y) % mod;
}

int main(void){
	int a,b;
	scanf("%d %d",&a,&b);
	if(a>8 || b>8) printf(":(");
	else printf("Yay!");

	return 0;
}

Measured memory_bytes: 128
#include <stdio.h>
int main(void)
{
	int box;
	int a, b;
	while(scanf("%d %d", &a, &b)) 
	{
		if(a > b)
		{	
			box = b;
			b = a;
			a = box;
		}
		else if(a == 0 && b == 0)
			{
				break;	
			}
		printf("%d %d\n", a, b);
		
	} 
	return 0;
}


Measured memory_bytes: 2708
K = int(input())
 
from itertools import product
kumi = product(range(1, K+1), repeat=3)
 
from math import gcd
 
memo = [[None for i in range(K+1)] for i in range(K+1)]
# memo2 = {}
 
ans = 0
for a, b, c in kumi:
    a, b, c = sorted([a, b, c])
    if memo[a][b] is not None:
        v = memo[a][b]
    else:
        v = gcd(a, b)
        memo[a][b] = v
        memo[b][a] = v
    d = gcd(v, c)
    ans += d
 
print(ans)

Measured memory_bytes: 79644
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Sa;
		var a=sc.Ia;
		var t=sc.S;
		if(s[0]==t){a[0]--;}
		else {a[1]--;}
		Console.WriteLine("{0} {1}",a[0],a[1]);
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}

Measured memory_bytes: 13140
#!usr/bin/env python3

# A
"""
a,b,c,k = map(int ,input().split())
s = b-a
if (k%2 == 0):
    s *= -1
print(s)
"""
# B
# C
n = int(input())
a = [int(input()) for i in range(n)]
if a[0] != 0:
    print(-1)
    quit()
for i in range(1,n):
    if a[i] > i:
        print(-1)
        quit()
    if a[i]-a[i-1] > 1:
        print(-1)
        quit()
ans = a[-1]
for i in range(n-1)[::-1]:
    if a[i]+1 == a[i+1]:continue
    ans += a[i]
print(ans)


# D

# E

# F


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

typedef long long ll;

int main(){
    int N;
    cin >> N;
    vector<int> Vi(N);
    vector<int> Ci(N);
    for (int i=0;i<N;i++){
        cin >> Vi[i];
    }
    for (int i=0;i<N;i++){
        cin >> Ci[i];
    }
    int ans = 0;
    for (int i=0;i<N;i++){
        int v = Vi[i] - Ci[i];
        if (v>0) ans += v;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256