akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) { return std::vector<T>(n, std::forward<T>(val)); }
template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}
template<class Cond> struct chain {
  Cond cond; chain(Cond cond) : cond(cond) {}
  template<class T> bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; }
};
template<class Cond> chain<Cond> make_chain(Cond cond) { return chain<Cond>(cond); }

#include <bitset>
#include <array>
#include <vector>

template<const int H, const int W>
struct bit_matrix {
  std::array<std::bitset<W>, H> m;

  bit_matrix(){} 

  std::bitset<W>& operator[](int i) { return m[i]; }
  const std::bitset<W>& operator[](int i) const { return m[i]; }

  template<const int X>
  bit_matrix<H, X> operator*(const bit_matrix<W, X>& r) const {
    bit_matrix<H, X> ans;
    bit_matrix<X, W> tr;
    for(int i = 0;i < W;i++) for(int j = 0;j < X;j++) {
      if(r[i][j]) tr[j].set(i);
    }
    for(int i = 0;i < H;i++) for(int j = 0;j < X;j++) {
      ans[i][j] = (m[i] & tr[j]).count() & 1;
    }
    return ans;
  }
};

template<const int H, const int W>
std::vector<int> gauss_jordan(bit_matrix<H, W>& A, bool is_extended = false) {
  int rank = 0;
  std::vector<int> fst;
  for(int j = 0; j < W; j++) {
    if(is_extended && j + 1 == W) break;
    int pivot = -1;
    for(int i = rank; i < H; i++) {
      if(A[i].test(j)) {
        pivot = i;
        break;
      }
    }
    if(pivot == -1) continue;
    std::swap(A[rank], A[pivot]);
    for(int i = 0;i < H; i++) {
      if(i != rank && A[i][j]) {
        A[i] ^= A[rank];
      }
    }
    fst.push_back(j);
    rank++;
  }
  return fst;
}

const int H_MAX = (int)(101010);
const int W_MAX = 62;

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);
  i64 sum = 0;
  rep(i,0,N) {
    cin >> A[i];
    sum ^= A[i];
  }
  i64 ans = 0;
  vector<i64> bit;
  rep(i,0,61) {
    if(sum & (1ll << i)) {
      ans += (1ll << i);
    }
    else {
      bit.push_back(i);
    }
  }
  reverse(all(bit));

  bit_matrix<H_MAX, W_MAX> mat;
  rep(i,0,N) {
    rep(j,0,bit.size()) {
      mat[i][j] = !!(A[i] & (1ll << bit[j]));
    }
  }
  auto ranks = gauss_jordan(mat);
  i64 res = 0;
  for(int i = 0;i < ranks.size();i++) {
    i64 x = 0;
    rep(j,0,bit.size()) {
      if(mat[i][j]) {
        x ^= (1ll << bit[j]);
      }
    }
    res = std::max(res, res ^ x);
  }
  cout << (ans + (res * 2)) << endl;
}


Measured memory_bytes: 1792
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = 10**9

# 個数制限なしナップザック問題
h,n = map(int, input().split())
dp = [INF]*(h+1)
dp[0] = 0
for i in range(n):
    a,b = map(int, input().split())
    for j in range(h):
        nj = min(a+j, h)
        dp[nj] = min(dp[nj], dp[j]+b)

print(dp[-1])


Measured memory_bytes: 71940
/*
___ is a raging sadist.
*/

//Destroy all WA at test 2

#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define endl "\n"
#define PI 3.14159265
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7; 
using namespace std;

ll power(ll x, ll y) 
{ 
   ll res = 1;      
  
   
    while (y > 0) 
    { 
       
        if (y & 1) 
            res = (long long)(res*x); 
  

        y = y>>1; 

        if (x<=100000000)
        x = (long long)(x*x);

        //cout<<x<<'\n'; 
    } 
    return res; 
} 






int main()
{

#ifndef ONLINE_JUDGE    
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

char x;
cin>>x;
if (x>='A'&&x<='Z')
cout<<'A';
else
cout<<'a';

return 0;
}

Measured memory_bytes: 3608
#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rrep(i,n) for(ll i = 1; i <= (n); ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (ll)(x).size()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
ll gcd(ll a,ll b){if(a%b==0){return b;}else{return(gcd(b,a%b));}}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int INF=1e9;
const ll MX = 1e18;
const ll MOD=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
const double PI=acos(-1);
const string tr="abcdefghijklmnopqrstuvwxyz";
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define YN {puts("YES");}else{puts("NO");}
const ll MAX = 1000001;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {fac[0] = fac[1] = 1;finv[0] = finv[1] = 1;inv[1] = 1;for (ll 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 < k) return 0;if (n < 0 || k < 0) return 0;return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;}
ll llpow(ll n,ll i){ll cnt=n;  for(ll j=0;j<i-1;j++){n*=cnt;}return n;}
bool ip/*is_prime*/(long long N) {if (N == 1) return false;for (long long i = 2; i * i <= N; ++i) {if (N % i == 0) return false;}return true;}
int digit(ll N) {int ans = 0;while (N) {++ans;N /= 10;}return ans;}
vector<pair<ll,ll>> pf/*prime_factorize*/(ll n){vector<pair<ll,ll>> res;for(ll a=2;a*a<=n;a++){if(n%a!=0) continue;ll ex=0;while(n%a==0){ex++;n/=a;}res.pb({a,ex});}if(n!=1) res.pb({n,1});return res;}
vector<ll> div/*divisor*/(ll n){  vector<ll> res={1};for(ll a=2;a*a<=n;a++){if(n%a!=0) continue;ll b=n/a;res.pb(b);if(b!=a) res.pb(a);}sort(all(res));return res;}

int main() {
  ll n;
  cin>>n;
  if(n%2!=0){
    cout<<0<<endl;
    return 0;
  }
  ll ja=10;
  ll ans=0;
  while(ja<=n){
    ans+=n/ja;
    ja*=5;
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 3588
import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long x = sc.nextLong();
		long mod = x % 11;
		
		long ans = (x / 11) * 2 + (mod == 0 ? 0 : (mod < 7 ? 1 : 2));
		System.out.println(ans);
		
		sc.close();
	}
	
}



Measured memory_bytes: 26988
#include <bits/stdc++.h>
#include <vector>
#include <string>

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

using namespace std;

int main(void)
{
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    cin >> s.at(i);
  }

  int ac, wa, tle, re;
  ac = 0, wa = 0, tle = 0, re = 0;
  for (string val : s) {
    if (val.compare("AC") == 0) ++ac;
    else if (val.compare("WA") == 0) ++wa;
    else if (val.compare("TLE") == 0) ++tle;
    else if (val.compare("RE") == 0) ++re;
  }

  cout << "AC x " << ac << endl;
  cout << "WA x " << wa << endl;
  cout << "TLE x " << tle << endl;
  cout << "RE x " << re << endl;
}

Measured memory_bytes: 6348
#include<iostream>
#include<vector>
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
const int M = (1e6);
bool p[M];
vector<long long int>sum;
void isPrime(){
  rep(i,M)p[i] = true;
  p[0] = p[1] = false;
  
  sum.push_back(0);
  loop(i,2,M){
    if(p[i]){
      sum.push_back(sum[sum.size()-1]+i);
      
      for(int j = i + i ; j < M ; j += i)
	p[j] = false;
    }
  }
}
int main(void){
  isPrime();
  int n;
  while(cin>>n,n)cout<<sum[n]<<endl;
}

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout.precision(10);
    cout<<fixed;

    #ifdef _DEBUG
    //freopen("input.txt", "r", stdin);
    #endif // _DEBUG

    int n;
    cin>>n;
    unordered_set<int> s;
   for (int i = 0; i < n; ++i) {
        int x;
        cin>>x;
        s.insert(x);
    }
    if (s.size() == n) cout<<"YES\n";
    else cout<<"NO\n";

    return 0;
}

Measured memory_bytes: 8520
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int M = sc.nextInt();
		int[] a = new int[A];
		int[] b = new int[B];
		int minA = Integer.MAX_VALUE;
		for(int i = 0; i < A; i++) {
			a[i] = sc.nextInt();
			if(minA > a[i]) minA = a[i];
		}
		int minB = Integer.MAX_VALUE;
		for(int i = 0; i < B; i++) {
			b[i] = sc.nextInt();
			if(minB > b[i]) minB = b[i];
		}
		int min = minA + minB;
		for(int i = 0; i < M; i++) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			int c = sc.nextInt();
			int m = a[x -1] + b[y - 1] - c;
			if(min > m) min = m;
		}
		sc.close();
		System.out.println(min);
	}
}


Measured memory_bytes: 149220
#include<stdio.h>
double abs(double a){return a<0?-a:a;}
int main(){
	double PI=3.14159265359;
	double EPS=0.0000000001;
	double a;
	while(scanf("%lf",&a),a>0.000000001){
		for(int i=1;;i++){
			int t=PI*i;
			if(abs((double)t/i-PI)<a+EPS){
				printf("%d/%d\n",t,i);
				break;
			}
			t++;
			if(abs((double)t/i-PI)<a+EPS){
				printf("%d/%d\n",t,i);
				break;
			}
		}
	}
}

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

#define MAXN 10000000

bool is_prime[MAXN];

void sieve()
{
	for(int i=1;i<=MAXN;i++)
	  is_prime[i] = true;
	for(int i=2;i<=MAXN;i++)
	  if(is_prime[i])
	  {
	  	for(int j=2*i;j<=MAXN;j+=i)
	  	  is_prime[j] = false;
	  }
	
}
int main()
{
	sieve();
	int n;
	while(cin>>n)
	{
		int res = 0;
		for(int i=2;i<=n;i++)
		  if(is_prime[i])
		    res++;
		
		cout << res <<endl;
	}
	
}


Measured memory_bytes: 12868
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
            　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
                  　　　　 （ ´_ゝ`）　/　 ⌒i
                       　　　　／　　　＼　 　  |　|
                           　　　 /　　 /￣￣￣￣/　　|
                               　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
                                 　 　　　＼/＿＿＿＿/　（u　⊃
                                      ---------------------------------------------------------------------------------------------------*/




typedef long long ll;
#define INF INT_MAX/2
int K;
//---------------------------------------------------------------------------------------------------
int memo[45][100000];
int vis[45][100000];
int f(int rest, int mo) {
    if (rest == 0 and mo == 0) return 1;

    if (0 < memo[rest][mo]) return memo[rest][mo];
    if (vis[rest][mo]) return memo[rest][mo] = 2;
    vis[rest][mo] = 1;

    int res = 2;
    rep(i, 0, 10) if (0 <= rest - i) {
        if (f(rest - i, (mo * 10 + i) % K) == 1) {
            res = 1;
            break;
        }
    }

    return memo[rest][mo] = res;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> K;

    int ma = 0, k = K;
    while (0 < k) {
        ma += k % 10;
        k /= 10;
    }

    rep(ans, 1, ma) if (f(ans, 0) == 1) {
        printf("%d\n", ans);
        return;
    }
    printf("%d\n", ma);
}

Measured memory_bytes: 35072
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define inf 1000000000000000
#define mod 1000000007
typedef pair<int, int> P;

bool prime(int n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)return false;
	}
	return n != 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}

int d[3000][3000];

signed main() {
	int h, w;
	cin >> h >> w;
	char c[3000][3000];
	rep(i, h) {
		rep(j, w) {
			cin >> c[j][i];
		}
	}
	rep(i, h) {
		int cnt = 0;
		rep(j, w) {
			if (c[j][i] == '.') cnt++;
			else {
				for (int k = j - cnt; k < j; k++) {
					d[k][i] += cnt;
				}
				cnt = 0;
			}
		}
		for (int k = w - cnt; k < w; k++) {
			d[k][i] += cnt;
		}
	}
	rep(i, w) {
		int cnt = 0;
		rep(j, h) {
			if (c[i][j] == '.') cnt++;
			else {
				for (int k = j - cnt; k < j; k++) {
					d[i][k] += cnt;
				}
				cnt = 0;
			}
		}
		for (int k = h - cnt; k < h; k++) {
			d[i][k] += cnt;
		}
	}
	int ans = 0;
	rep(i, h) {
		rep(j, w) {
			ans = max(ans, d[j][i] - 1);
		}
	}
	cout << ans << endl;


	return 0;
}


Measured memory_bytes: 54784
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
    WRITER:kakitamasziru/OxOmisosiru
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#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 <iomanip>
#include <limits>//setprecision
//#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
//#include <unordered_map> // unordered_map
//#include <unordered_set> // unordered_set
#include <bitset> // bitset
//#include <cctype> // isupper, islower, isdigit, toupper, tolower
//It is so troublesome that I include bits/stdc++.h !
using namespace std;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const long long INF = 1001001001001001;
const int MOD = 1000000007;
typedef pair<long long,long long> P;
//NのM乗を求める(繰り返し二乗法)
long long mod_pow(long long N, long long M) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % MOD, M / 2);
    //最下位ビット(*N)が1の時は単独でNをかける
    if (M & 1) res = (res * N) % MOD;

    return res %= MOD;
}

queue<long long> Q;

long long dp[114][114514];

int main() {
    int N,W;cin >> N >> W;
    //first:weight,second:value
    vector<P> goods(N);
    for(int i = 0;i<N;i++){
        cin >> goods.at(i).first >> goods.at(i).second;
    }
    for(int i = 0;i<114;i++){
        for(int j = 0;j<114514;j++) dp[i][j] = 0;
    }

    for(int i = 0;i<N;i++){
        for(int j = 0;j<=W;j++){
            if(goods.at(i).first+j <= W) {
                chmax(dp[i+1][j+goods.at(i).first],dp[i][j]+goods.at(i).second);

            }
            chmax(dp[i+1][j],dp[i][j]);
        }
    }

    cout << dp[N][W] << endl;

    // PLEASE GIVE ME THE "ACCEPTED" !
}

Measured memory_bytes: 102272
#include <iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	int n, k,p[1000];
	cin >> n >> k;
	for (size_t i = 0; i < n; i++)
	{ 
		int x;
		cin >> x;
		p[i] = x;
	}
	sort(p, p + n);
	int sum = 0;
	for (size_t i = 0; i < k; i++)
	{
		sum += p[i];
	}
	cout << sum<<endl;
	return 0;
}

Measured memory_bytes: 3568
#include<stdio.h>

int main()
{
    double r;
    double area,cicle;
    scanf("%lf",&r);
    area=r*r*3.141592653589;
    cicle=2*r*3.141592653589;
    printf("%lf %lf\n",area,cicle);
    return 0;
}


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


int main() {
    double A,B,H,M;
    cin >> A >> B >> H >> M;

    double LongR = M*6;
    double ShortR = 360*((H*60 + M)/720);
    double pi = 3.141592653589793;

    cout << setprecision(12);

    cout << sqrt(A*A + B*B - 2*A*B*cos((LongR - ShortR)*pi/180)) << endl;;

}

Measured memory_bytes: 4228
#include <iostream>

using namespace std;

int main() {
    string S;
    cin >> S;
    for (int i=0; i<S.length(); ++i) {
        if (i%2==0) {
            if (S[i]!='R' && S[i]!='U' && S[i]!='D') {
                cout << "No" << endl;
                return 0;
            }
        } else if (i%2==1) {
            if (S[i]!='L' && S[i]!='U' && S[i]!='D') {
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}

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

int main(){
    int n, x, mini = INT_MAX;
    cin >> n >> x;
    vector<int> m(n);
    rep(i,n){
        cin >> m[i];
        x -= m[i];
        mini = min(mini, m[i]);
    }
    cout << int(n + x/mini) << endl;

}



Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 1e5+10 ;

vector<int> s[MAXN];
int color[MAXN],n,m;

void dfs(int x,int id)
{
    stack<int> st;
    color[x] = id;
    st.push(x);
    while(!st.empty())
    {
        int u = st.top();   st.pop();
        for(int i = 0; i < s[u].size(); ++i)
        {
            int v = s[u][i];
            if(color[v] == -1)
            {
                color[v] = id;
                st.push(v);
            }
        }
    }
}
void init()
{
    int id = 0;
    memset(color,-1,sizeof color);
    for(int i = 0; i < n; ++i)
    {
        if(color[i] == -1)
            dfs(i,++id);
    }
}

int main(void)
{
    cin>>n>>m;
    int u,v;
    for(int i = 1; i <= m; ++i)
    {
        cin>>u>>v;
        s[u].push_back(v);
        s[v].push_back(u);
    }
    init();
    int q;
    cin>>q;
    while(q--)
    {
        cin>>u>>v;
        if(color[u] == color[v])
            cout<<"yes\n";
        else
            cout<<"no\n";
    }
}



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

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< pii >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; cin >> x;
#define int2(x, y) Int(x); Int(y);
#define int3(x, y, z) Int(x); int2(y, z);
#define in(x, a, b) ((a) <= (x) && (x) < (b))
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)

//int dxy[5] = {0, 1, 0, -1, 0};
// cmd

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

    Int(n);

    int ans = 0;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    vector<int> c(n-1);
    for (int i = 0; i < n-1; i++) {
        cin >> c[i];
    }
    
    rep(i, n) {
        ans += b[a[i]-1];
    }

    Rep(i, n) {
        if (a[i-1] + 1 == a[i]) ans += c[a[i-1]-1];
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<n; ++i)
#define rep1(i, n) for (int i=1; i<=n; ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr ll  MOD = (1e9+7);
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a / gcd(a, b) * b; }

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
ll factorial(ll n, ll m=2) {
    // calculate n!
    m = max(2LL, m);
    ll rtn = 1;
    for (ll i=m; i<=n; i++) {
        rtn = (rtn * i) % MOD;
    }
    return rtn;
}
ll modinv(ll a, ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
ll modpow(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 main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    ll n; cin >> n;
    vector<ll> a(n); 
    rep(i, n) cin >> a[i];
    ll sum = a[0];
    ll cost1 = 0;
    // even index == positive
    if (sum <= 0) {
        cost1 += 1+abs(sum);
        sum = 1;
    }
    rep(i, n-1) {
        if (sum < 0) {
            if (a[i+1]+sum > 0) sum += a[i+1];
            else { cost1 += abs(1-(a[i+1]+sum)); sum = 1; }
        } else {
            if (a[i+1]+sum < 0) sum += a[i+1];
            else { cost1 += abs(-1-(a[i+1]+sum)); sum = -1; }
        }
    }
    // even index == negative
    sum = a[0];
    ll cost2 = 0;
    if (sum >= 0) {
        cost2 += abs(sum)+1;
        sum = -1;
    }
    rep(i, n-1) {
        if (sum < 0) {
            if (a[i+1]+sum > 0) sum += a[i+1];
            else { cost2 += abs(1-(a[i+1]+sum)); sum = 1; }
        } else {
            if (a[i+1]+sum < 0) sum += a[i+1];
            else { cost2 += 1+(a[i+1]+sum); sum = -1; }
        }
    }
    cout << min(cost1, cost2) << endl;
    return 0;
}

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

int P[10010];
int n,q,com,x,y;
void init(int a) {
	for(int i=0; i<a; ++i) P[i] = i;
}

int root(int a) {
	if(P[a] == a) return a;
	else return(P[a] = root(P[a]));
}

bool is_same_set(int a,int b) {
	return root(a) == root(b);
}

void unite(int a,int b) {
	P[root(a)] = root(b);
}

int main() {
	cin >> n >> q;
	init(n);
	for(int i=0; i<q; ++i) {
		cin >> com >> x >> y;
		if(com == 0) unite(x,y);
		else cout << is_same_set(x,y) << endl;
	}
}

Measured memory_bytes: 1320
#include <bits/stdc++.h>
#include <algorithm>
#include <string>

using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
signed main(){
	int size;cin>>size;int K;cin>>K;
  	string S;cin>>S;
  	int ans;ans=0;
  	int	chunk[size+1]={};
  	char bit;bit='1';
  	int k;k=0;
	rep(i,size){
    	if(S[i]==bit){
        	chunk[k]=chunk[k]+1;
        }else{
        	k=k+1;
          	chunk[k]=chunk[k]+1;
        	if(bit=='0'){
          		bit='1';
        	}else{
        		bit='0';
        	}  
        }  
    }
  	k=k+1;
  	int sum;sum=0;
  	for(int i=0;i<k+1;i++){
      	sum=0;
    	for(int j=i;j<min(k+1,i+2*K+1);j++){
        	sum=sum+chunk[j];
        }
      if(sum>ans){
      	ans=sum;
      }
      
      	i=i+1;
    }
  	
  	cout<<ans<<endl;
	return 0;
}


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

int main()
{
    int X; cin>>X;
    for(int i=0;i<1001;i++){
        if(100*i<=X&&105*i>=X){
            cout<<1<<endl;
            return 0;
        }
    }
    cout<<0<<endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long numberH = sc.nextInt();
		long numberW = sc.nextInt();
		sc.close();

		long answer = 0;
		if(numberW == 1 || numberH == 1) {
			answer = 1;
		}else {
				if(numberH % 2 ==1) {
					answer = (numberH * numberW +1) /2 ;
				}else {
					answer = (numberH * numberW)/2;
				}
			}
		System.out.println(answer);
	}

}


Measured memory_bytes: 22868
#include <bits/stdc++.h>
#include <math.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;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

int main()
{
    string s;
    cin >> s;
    if (s == "AAA" or s == "BBB")
        cout << "No" << endl;
    else {
        cout << "Yes" << endl;
    }
}

Measured memory_bytes: 256
#include <cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
int main() {
    ll n;
    cin >> n;
    ll a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;
    ll check = min({ a,b,c,d,e });
   
    ll ans = ceil((double)n/check) + 4;
    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##_max=(N);i<i##_max;++i)
#define repp(i,l,r) for(int i=(l),i##_max=(r);i<i##_max;++i)
#define per(i,N) for(int i=(N)-1;i>=0;--i)
#define perr(i,l,r) for(int i=r-1,i##_min(l);i>=i##_min;--i)
#define all(arr) (arr).begin(), (arr).end()
#define SP << " " <<
#define SPF << " "
#define SPEEDUP cin.tie(0);ios::sync_with_stdio(false);
#define MAX_I INT_MAX //1e9
#define MIN_I INT_MIN //-1e9
#define MAX_UI UINT_MAX //1e9
#define MAX_LL LLONG_MAX //1e18
#define MIN_LL LLONG_MIN //-1e18
#define MAX_ULL ULLONG_MAX //1e19
  typedef long long ll;
  typedef pair<int,int> PII;
  typedef pair<char,char> PCC;
  typedef pair<ll,ll> PLL;
  typedef pair<char,int> PCI;
  typedef pair<int,char> PIC;
  typedef pair<ll,int> PLI;
  typedef pair<int,ll> PIL; 
  typedef pair<ll,char> PLC; 
  typedef pair<char,ll> PCL; 

inline void YesNo(bool b){ cout << (b?"Yes" : "No") << endl;}
inline void YESNO(bool b){ cout << (b?"YES" : "NO") << endl;}
inline void Yay(bool b){ cout << (b?"Yay!" : ":(") << endl;}

int main(void){
  SPEEDUP
  cout << setprecision(15);
  string s;cin >> s;
  ll count = 0;
  ll sum = 0;
  rep(i,s.length()){
    if(s[i] == 'B') ++count;
    else sum += count;
  }
  cout << sum << endl;
  return 0;
}


Measured memory_bytes: 720
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        if ((a * b) % 2 == 0)
            System.out.println("Even");
        else
            System.out.println("Odd");
    }
}

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

#define ll long long

bool cmp(ll a, ll b)

{
    return a > b;
}

int main()

{
    ll n, h;
    cin >> n >> h;
    ll thrown[n + 5];

    ll mx = INT_MIN;
    for(int i = 0; i < n; i++) {
        ll tmp;
        cin >> tmp >> thrown[i];
        mx = max(tmp, mx);
    }

    sort(thrown, thrown + n, cmp);

    ll cnt = 0, sum = 0;
    for(int i = 0; i < n; i++) {
        if(thrown[i] < mx)
            break;
        sum += thrown[i];
        cnt++;
        if(sum >= h)
            break;
    }

    if(sum < h) {
        cnt = cnt + ( (h - sum) / mx);
        if((h - sum) % mx != 0)
            cnt++;
    }

    cout << cnt << endl;

    return 0;
}



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

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

int gcd(ll a, ll b){
    if(a<b) return gcd(b,a);
    ll r;
    while(r=a%b){
        a=b;
        b=r;
    }
    return b;
}

int main(){
    ll a,b,count=1;
    cin >> a >> b;
   cout << (a*b)/gcd(a,b) << endl;
   
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lb;
#define REP(i,j,n) for (int i=j;i<(n);i++)
#define RREP(i,n) for (int i = n; 0<i;i--)
#define print(out) cout<< out  << "\n";
#define mod 1000000007
const int INF = 1e9;

int main(){
  int N;cin>>N;
  double d=0,tmp;
  REP(i,0,N){
    cin>>tmp;
    d+=1/tmp;
  }
  print(setprecision(16)<<1/d)

}


Measured memory_bytes: 256
$<.map{|l|(n=l.to_i)>0&&(s=(0..n/2+1).flat_map{|i|[(n*n+1)/2-n/2-(n-1)*i,n*n-n/2+1-(n-1)*i]}[0,n]
n.times{puts"%4d"*n%s;s=s.rotate(-1).map{|i|i%n>0?i+1:i-n+1}})}

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

int main()
{
    int N, T;
    cin >> N >> T;
    int P[1010][1010] = {0};
    bool flag = false;
    int ans = 10010;
    for (int i = 0; i < N; i++)
    {
        int c, t;
        cin >> c >> t;
        if (t <= T)
        {
            ans = min(ans, c);
            flag = true;
        }
    }

    if (flag)
    {
        cout << ans;
    }
    else
    {
        cout << "TLE";
    }
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<iomanip>
#include<functional>
#include<cctype>
#include<map>
using namespace std;
typedef long long llong;
int main() {
	string s;
	int cnt = 0;
	cin >> s;
	string b = "CODEFESTIVAL2016";
	for (int i = 0; i < s.size(); i++) {
		if (s[i] != b[i])cnt++;
	}
	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize "-O3"
#pragma GCC target ("avx")
#define _USE_MATH_DEFINES
#include<bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define inf (1ll<<60)
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v),greater<int>())
#define SPEED ios::sync_with_stdio(false);cin.tie(0);cout<<fixed<<setprecision(12)
signed gcd(int a, int b){return b?gcd(b,a%b):a;}
signed lcm(int a, int b){return a*b/gcd(a,b);}
//priority_queue<int, vi, greater<int> > que;//昇順

using namespace std;

const int mod=1e9+7;

signed main(){
    SPEED;
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if(abs(a-c)<=d||((abs(a-b)<=d)&&(abs(b-c)<=d)))
    {
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;
}


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

m=10**9+7

dp=[ [0]*(k+1) for _  in range(n+1) ]
dp[0][0]=1

dp_sum=[ [0]*(k+1) for _  in range(n+1) ]

for i in range(n):
    for j in range(k+1):
        if j==0:
            dp_sum[i][j]=1
        else:
            dp_sum[i][j]= (dp_sum[i][j-1]+dp[i][j])%m
        if j-a[i]>0:
            dp[i+1][j]= (dp_sum[i][j] - dp_sum[i][j-a[i]-1])%m
        else:
            dp[i+1][j]= (dp_sum[i][j])%m

print(dp[n][k]%m)

Measured memory_bytes: 192792
n = int(input())
a = [int(input()) for _ in range(n)]

sum_a = sum(a)

if sum_a % 10 == 0:
	can_nums = list(filter(lambda x: x%10 != 0, set(a)))
	if len(can_nums) == 0:
		print(0)
	else:
		print(sum_a - min(can_nums))
else:
	print(sum_a)

Measured memory_bytes: 3060
from collections import defaultdict
N,*A = map(int, open(0).read().split())
dp_not = defaultdict(lambda :-10**18)
dp_take = defaultdict(lambda :-10**18)
dp_not[(0,0)] = 0
for i,x in enumerate(A,1):
  for j in range(N//2-(N-i+1)//2,(i+1)//2+1):
    dp_not[(i,j)] = max(dp_not[(i-1,j)],dp_take[(i-1,j)])
    dp_take[(i,j)] = dp_not[(i-1,j-1)] + x
print(max(dp_not[(N,N//2)], dp_take[(N,N//2)]))


Measured memory_bytes: 232160
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#define LL long long
#define N 105
using namespace std;
char s[N];
int main(){
	ios::sync_with_stdio(false);
	LL k;
	int i;
	while(cin>>s>>k){
		for(i=0;i<k;i++){
			if(s[i]!='1')break;
		} 
		if(i==k)cout<<1<<endl;
		else cout<<s[i]<<endl;
		
	}
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    if(a+b==0){
        cout<<0<<endl;
    }else if(a+b>0){
        cout<<1<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}

Measured memory_bytes: 3088
#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include <numeric>
 
 
#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 SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
 
typedef long long int ll;
#define ARRAY_MAX 100005


int main(){

    int n;
    cin >> n;
    vector<int> a(2*n);
    REP(i,2*n){
        cin >> a[i];
    }
    

    VSORT(a);
    int ans = 0;
    for(int i = 0;i < 2*n;i+=2){
        ans += min(a[i],a[i+1]);
    }
    cout << ans << endl;


    return 0;
}

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


int find(string& str, const string& word) {
  int count = 0;
  for (string::size_type pos = 0; (pos = str.find(word, pos)) != string::npos; pos += word.size()) {
    ++count;
  }
  return count;
}

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

    string str;
    cin >> str;

    const string abcstr = "ABC";


    int ans = find(str, abcstr);

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


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

public class Main {
    public static void main (String[] args) throws IOException {
        final String s;
        try (BufferedReader reader =
                 new BufferedReader (new InputStreamReader (System.in))) {
            s = reader.readLine();
        }
        int           N    = Integer.parseInt (s);
        StringBuilder sb   = new StringBuilder();
        int           base = 2;
        while (0 != N) {
            // base = 3 でうまくいかない
            // 例えば 7
            // base = 2 は OK
            // sb.insert (0, String.valueOf (Math.abs (N % base)));
            // N -= Math.abs (N % base);
            // N /= -base;

            // base = 3 でもうまくいく
            // 最小非負剰余
            sb.insert (0, String.valueOf ((N % base + base) % base));
            N -= (N % base + base) % base;
            // 余りゼロにして -base 割りの商
            N = (N - (N % base + base) % base) / -base;
        }
        if (sb.length() == 0) sb.append ("0");
        System.out.println (sb);
    }
}


Measured memory_bytes: 21076
#include<stdio.h>

long long int lgc(long long int a, long long int b){
 
  long long int r, tmp;

  r = a % b;
  while(r!=0){
    a = b;
    b = r;
    r = a % b;
  }
 
  return b;
}


int main(void){

  long long int n, x;
  scanf("%lld %lld",&n, &x);

  long long int p, q;
  p = lgc(n,x);
  q = n / p;

  long long int ans;
  ans = p * (q - 1) * 3;

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

  return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
#define next Next
#define int long long
/*char buf[1<<21],*p1=buf,*p2=buf;
inline int gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}*/
#define gc getchar
inline int read()
{
	int ret=0,f=0;char c=gc();
	while(!isdigit(c)){if(c=='-')f=1;c=gc();}
	while(isdigit(c)){ret=ret*10+c-48;c=gc();}
	if(f)return -ret;return ret;
}
signed main()
{
	int n=read(),m=read(),ans=0;
	if(n>m)swap(n,m);
	if(n==1&&m==1){cout<<1;return 0;}
	if(n==1){cout<<m-2;return 0;}
	ans=(n-2)*(m-2);
	cout<<ans;
}

Measured memory_bytes: 256
import java.io.*;
import java.util.*;


public class Main{
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));

        // Start writing your solution here. -------------------------------------
        int n = sc.nextInt(), K = sc.nextInt();
        int a[] = new int[n + 1];
        int pre[] = new int[n + 1];
        long MOD = 1000000007;
        long res = 0;
        for(int i = 1; i <= n ; i ++)
        {
            a[i] = sc.nextInt();
            pre[i] = pre[i - 1] +  a[i];
        }
        long dp[][] = new long[n + 1][K + 1];
        for(int i = 0; i <= n; i++) dp[i][0] = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= K; j++)
            {
                dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
                if(j - a[i] > 0) dp[i][j] -= dp[i - 1][j - a[i] - 1];
                dp[i][j] = (dp[i][j] + MOD) % MOD;
            }
        }
        //for(int i = 1; i <= n; i++) System.out.println(Arrays.toString(dp[i]));
        out.println(dp[n][K]);
        out.close();

        /*
        int n      = sc.nextInt();        // read input as integer
        long k     = sc.nextLong();       // read input as long
        double d   = sc.nextDouble();     // read input as double
        String str = sc.next();           // read input as String
        String s   = sc.nextLine();       // read whole line as String

        int result = 3*n;
        out.println(result);                    // print via PrintWriter
        */

        // Stop writing your solution here. -------------------------------------
        out.close();
    }
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    //--------------------------------------------------------
}

Measured memory_bytes: 127188
#include <iostream>
#include <cstring>

using namespace std;


int map[50][50];
int N;
int x,y;


int main(){

  while(1){

    cin >> N;
    if(N == 0) break;

    memset(map,0,sizeof(map));

    for(int i = 0; i < N ; i++){
      cin >> x >> y;
      map[x][y] = 1;
    }

    int M;
    int l;
    char d;
    int tr = 0;

    cin >> M;
    int nx = 10, ny =10;
    for(int i = 0; i < M ; i++){
      cin >> d >> l;
      if(d == 'N'){
	for(int i = 0 ; i < l; i++){
	  ny++;
	  if(map[nx][ny] == 1){
	    tr++;
	    map[nx][ny] = 0;
	  }
	}
      }else if(d == 'E'){
	for(int i = 0 ; i < l; i++){
	  nx++;
	  if(map[nx][ny] == 1){
	    tr++;
	    map[nx][ny] = 0;
	  }
	}
	
      }else if(d == 'S'){
	for(int i = 0 ; i < l; i++){
	  ny--;
	  if(map[nx][ny] == 1){
	    tr++;
    map[nx][ny] = 0;
	  }
	}
       }else if(d == 'W'){
	for(int i = 0 ; i < l; i++){
	  nx--;
	  if(map[nx][ny] == 1){
	    tr++;
	    map[nx][ny] = 0;
	  }
	}
      }
    }
    if( tr == N ) cout << "Yes" << endl;
    else cout << "No" << endl;
  }

  return 0;
}

Measured memory_bytes: 1168
def f(s):
	cnt = 0
	st = set()
	cnt += 1
	st.add(s)
  
	while True:
		cnt += 1
		if s % 2 == 0:
			tmp = s / 2
		else:
			tmp = 3 * s + 1
		if tmp in st:
			break
    
		st.add(tmp)
		s = tmp
	return cnt
  
s = int(input())
ans = f(s)
print(ans)

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

int main() {
    
    int k, n;
    cin >> k >> n;
    
    vector<int> v;
    int result = k;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        v.push_back(x);
        if (i > 1) {
            int d = (k - x) + v[i-1];
            if (d < result) {
                result = d;
            }
        }
    }
    
    int d = v[n-1] - v[0];
    if (d < result) {
        result = d;
    }

    cout << result << endl;


    return 0;
}

Measured memory_bytes: 4320
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>

//LLONG_MAX
#define int long long
#define CONTAINS(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v,a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v,a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int N, K;

vector<int> M;
vector<int> P;

signed main()
{
	cin >> N >> K;
	REP(i, N)
	{
		int x;
		cin >> x;
		if (x < 0)
		{
			M.push_back(x);
		}
		else
		{
			P.push_back(x);
		}
	}
	REVERSE(M);

	int min = INT_MAX;

	int len = 0;
	int p = MIN(K, P.size());
	if (p > 0)
	{
		len += P[p - 1];
	}
	int m = K - p;
	if (m > 0)
	{
		len += abs(M[m - 1] * 2);
	}
	min = len;

	for (int i = p - 1; i >= 0; i--)
	{
		if (m >= M.size())
		{
			break;
		}

		int sub_p = i == 0 ? P[0] : P[i] - P[i - 1];
		len -= sub_p;

		int sub_m = m == 0 ? M[0] : M[m] - M[m- 1];
		len += abs(sub_m) * 2;
		min = MIN(len, min);

		m++;
	}

	len = 0;
	m = MIN(K, M.size());
	if (m > 0)
	{
		len += abs(M[m - 1]);
	}
	p = K - m;
	if (p > 0)
	{
		len += P[p - 1] * 2;
	}
	min = MIN(min, len);

	for (int i = m - 1; i >= 0; i--)
	{
		if (p >= P.size())
		{
			break;
		}

		int sub_m = i == 0 ? M[0] : M[i] - M[i - 1];
		len -= abs(sub_m);
		int sub_p = p == 0 ? P[0] : P[p] - P[p - 1];
		len += sub_p * 2;
		min = MIN(len, min);

		p++;
	}

	cout << min << endl;
}







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

int main() {
  int N,A,B;
  cin>>N>>A>>B;
  string S;
  cin>>S;
  int X=0;
  int Y=0;
  for(int i=0;i<N;i++){
    if(S.at(i)=='c'){
      cout<<"No"<< endl;
    }
    if(S.at(i)=='a'){
      if(X+Y<A+B){
        cout<<"Yes"<< endl;
        X++;
      }
      else{
        cout<<"No"<< endl;
      }
    }
    if(S.at(i)=='b'){
      if(X+Y<A+B&&Y<B){
        cout<<"Yes"<< endl;
        Y++;
      }
      else{
        cout<<"No"<< endl;
      }
    }
  }
}
    

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

int main() {
  string s;
  cin >> s;
  if(s[0] == 'S') {
    cout << "Cloudy" << endl;
  }
  if(s[0] == 'C') {
    cout << "Rainy" << endl;
  }
  if(s[0] == 'R') {
    cout << "Sunny" << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <string>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <iostream>
#include <functional>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <cmath>
#include <limits>
#include <tuple>
#include <queue>
#include <stack>
#include <bitset>

using namespace std;
typedef long long ll;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()

int main()
{
	int N;
	cin >> N;
	string c;
	cin >> c;
	set<int> W, R;
	rep(i, N) {
		if (c[i] == 'W')
			W.insert(i);
		else
			R.insert(i);
	}
	int ans = 0;
	for (;;) {
		if (W.empty() || R.empty())
			break;
		auto wbeg = W.begin();
		auto rlast = R.end();
		rlast--;
		if (*wbeg > *rlast)
			break;
		W.erase(wbeg);
		R.erase(rlast);
		ans++;
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 12972
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.range;

ulong[51][51] CMB;

ulong comb(ulong n, ulong r)
{
    if (CMB[n][r]) {
        return CMB[n][r];
    } else if(r > n) {
        return CMB[n][r] = 0;
    } else if (n == 0 || r == 0) {
        return CMB[n][r] = 1;
    } else {
        return CMB[n][r] = comb(n-1, r-1) + comb(n-1, r);
    }
}

void main()
{
    auto nab = readln.split.to!(int[]);
    auto a = nab[1];
    auto b = nab[2];

    auto vs = readln.split.to!(ulong[]);
    vs.sort();
    vs.reverse();

    writefln("%0.6f", reduce!"a + b"(0L, vs[0..a]) / a.to!double);

    if (vs.length > a && vs[0] == vs[a]) {
        auto cnt = vs.count(vs[a]);
        auto v_max = b > cnt ? cnt : b;
        ulong ret = 0;
        foreach (i; a..v_max+1)
            ret += comb(cnt, i);
        writeln(ret);
    } else if (vs.length > a && vs[a-1] == vs[a]) {
        auto cnt = vs.count(vs[a]);
        auto space = vs[0..a].count(vs[a]);
        writeln(comb(cnt, space));
    } else {
        writeln(1);
    }
}

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,D,X;
ll T;
double F[402020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>D>>X;
	FOR(i,2*N) T += D, D += X;
	for(x=1;x<=N;x++) F[x] = 1.0/(2*x) + ((x-1.0)/x)*(1+1.0/x)*F[x-1];
	_P("%.12lf\n",T*F[N]);
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


Measured memory_bytes: 1792
#include <iostream>

using namespace std;

typedef long long ll;

int main () {
  ios::sync_with_stdio(false);

  ll x, y;
  cin >> x >> y;

  int ans = 1;
  while (2 * x <= y) {
    x *= 2;
    ans++;
  }

  cout << ans << endl;
}


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

int main() {
    int N , sum, l , r;
    sum = 0;
    cin >> N;
    for(int i = 0; i < N; i++){
        cin >> l >> r;
        sum = sum + (r - l + 1);
    }

    cout << sum << endl;




}

Measured memory_bytes: 3592