akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する


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

    ll a, b;

    cin >> a >> b;

    if (a * b == 15) {
        cout << "*" << endl;
    } else if (a + b == 15) {
        cout << "+" << endl;
    } else {
        cout << "x" << endl;
    }


    return 0;
}


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

int main() {
  int w,h,n;
  cin>>w>>h>>n;
  int x1=0,y1=0,x2=w,y2=h;
  for(int i=0;i<n;i++){
    int x,y,a;
    cin>>x>>y>>a;
    if(a==1)x1=max(x1,x);
    else if(a==2)x2=min(x2,x);
    else if(a==3)y1=max(y1,y);
    else y2=min(y2,y);
  }
  if(x2-x1<=0||y2-y1<=0)cout<<0<<endl;
  else cout<<(x2-x1)*(y2-y1)<<endl;
  return 0;
}
  

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ll> sl;
typedef pair<int,int> pii;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define per(i,a,b) for(int i=(b)-1; i>=(a); --i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

const ll MOD = 1e9+7;
int n;

bool isValid(string s) {
	if(s.size() < 3) {
		return true;
	} else if(s.size() == 3) {
		return s != "AGC" && s != "ACG" && s != "GAC";
	} else {
		bool ret = isValid(s.substr(0,3));
		ret &= isValid(s.substr(1,3));
		ret &= ((s.substr(0,1) + s.substr(2,2)) != "AGC");
		ret &= ((s.substr(0,2) + s.substr(3,1)) != "AGC");
		return ret;
	}
}

map<string, ll> dp[101];
ll solve(int pos, string s) {
	if(pos == n) return 1;

	map<string, ll>& p = dp[pos];
	if(p.find(s) != p.end()) {
		return p[s];
	}
	p[s] = 0;
	for(char c : "ACGT") {
		if(c == '\0') break;
		string ns = s + string(1,c);
		if(isValid(ns)) { 
			p[s] += solve(pos+1, ns.substr(1));
			p[s] %= MOD;
		}
	}
	return p[s];
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);
	cin >> n;
	string s = "DDD";
	cout << solve(0, s) << endl;
	return 0;
}


Measured memory_bytes: 896
#!/usr/bin/env python
# -*- coding: utf-8 -*

N = int(raw_input())
D, X = map(int, raw_input().split())
A = []
for i in range(N):
    A.append(int(raw_input()))

k = []
ans = X + N

for i in range(N):
    k.append(0)
    while 1:
        if 1 + k[i] * A[i] > D:
            k[i] -= 1
            break
        else:
            k[i] += 1
    ans += k[i]

print str(ans)


Measured memory_bytes: 2568
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<time.h>
#include<queue>
#include<bitset>
#include<vector>
using namespace std;
const int maxn=1e5+100;
char a[maxn];
int main()
{
    scanf("%s",a);
    int len=strlen(a),ans=len;
    for(int i=1;i<len;i++)
        if(a[i-1]!=a[i])
            ans=min(ans,max(i,len-i));
    printf("%d\n",ans);
    return 0;
}

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

int main() {
    int N,D,X;
    cin >> N >> D >> X;
    int A[N];
    rep(i,N){
        cin >> A[i];
    }
    int sum=0;
    rep(i,N){
        rep(j,D){
            if(1+j*A[i]<=D){
                sum++;
            }
        }
    }
    
    cout << sum+X << endl;
}


Measured memory_bytes: 256
import numpy as np
S = input()
spl = list(S)
alpha_set = ['A', 'T', 'G', 'C']
x = np.zeros(len(S))
for i in range(len(S)):
  if len(set(alpha_set)-set(spl[i]))==4:
    x[i] = 0
  else:
    x[i] = 1
count = 0
answer = 0
for i in range(len(x)):
  if x[i]==1:
    count += 1
    if answer<count:
      answer = count
  else:
    count = 0
print(answer)

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

typedef long long LL;
using namespace std;

const LL MOD = 1e9+7;
LL n, a, b;

LL powmod(LL x, LL y){
    LL res=1;
    while(y){
        if(y&1)
            res=res*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return res%MOD;
}

LL inv[200005];

int main(){
    cin>>n>>a>>b;
    LL res=0;//thh操作，想去套公式

    res=powmod(2,n);
    LL t1=1, t2=1;
    for(int i=1;i<=a;++i)t1=t1*(n-i+1)%MOD;
    for(int i=1;i<=b;++i)t2=t2*(n-i+1)%MOD;

    inv[0]=inv[1]=1;
    for(int i=2;i<=200000;++i)inv[i]=MOD-MOD/i*inv[MOD%i]%MOD;
    for(int i=1;i<=a;++i)t1=t1*inv[i]%MOD;
    for(int i=1;i<=b;++i)t2=t2*inv[i]%MOD;
    //cout<<t1<<" "<<t2<<endl;
    cout<<((res-t1-t2-1)%MOD+MOD)%MOD;



    return 0;
}


Measured memory_bytes: 1792
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
ans = 0
for i in range(len(A)):
    ans += B[A[i]-1]
    if i != 0 and A[i] == A[i-1] + 1:
        ans += C[A[i]-2]
print(ans)

Measured memory_bytes: 38256
N = int(input())
*A, = map(int, input().split())
right = 0; ans = 0; S = 0; S_xor = 0;
for left in range(N):
    while right < N and S_xor ^ A[right] == S + A[right]:
        S_xor ^= A[right]
        S += A[right]
        right += 1
    ans += right - left
    if right == left:
        right += 1
    else:
        S_xor ^= A[left]
        S -= A[left]
print(ans)

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

int main() {
    int n,k;
    cin>>n>>k;
    vector<int>vec(n,k);
    vector<int>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a.begin(),a.end(),greater<int>());
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i==j)continue;
            if(vec[i]-a[j]>0)vec[i]-=a[j];
        }
    }
    int c=0;
    for(int i=0;i<n;i++){
        if(vec[i]-a[i]>0){
            c++;
        }
    }
    cout<<c<<endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.*;
public class Main{
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    if(s.equals("a") || s.equals("i") || s.equals("u") || s.equals("e") || s.equals("o")){
      System.out.println("vowel");
    }else{
      System.out.println("consonant");
    }
  }
}


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

#define int long long
#define pb push_back 
#define pf push_front 
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

int H, W;
int ey, ex;
char maps[201][201];

int bfs1() {
  bool used[201][201] = {{0}};
  queue<Pip> q;
  q.push(Pip(0, Pii(ey, ex)));
  while ( !q.empty() ) {
    Pip p = q.front(); q.pop();
    int c = p.fr,  y = p.sc.fr, x = p.sc.sc;    

    if ( maps[y][x] == '@' ) {
      return c;
    }

    if ( used[y][x] ) continue;
    used[y][x] = true;

    Rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if ( ny < 0 || nx < 0 || ny >= H || nx >= W || maps[ny][nx] == '#' || maps[ny][nx] == '$' ) continue;
      q.push(Pip(c+1, Pii(ny, nx)));
    }
  }

  return INF;
}

int bfs2() {
  bool used[201][201] = {{0}};
  queue<Pip> q;
  q.push(Pip(0, Pii(ey, ex)));
  while ( !q.empty() ) {
    Pip p = q.front(); q.pop();
    int c = p.fr,  y = p.sc.fr, x = p.sc.sc;    

    if ( maps[y][x] == '$' ) {
      return c;
    }

    if ( used[y][x] ) continue;
    used[y][x] = true;

    Rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if ( ny < 0 || nx < 0 || ny >= H || nx >= W || maps[ny][nx] == '#' ) continue;
      q.push(Pip(c+1, Pii(ny, nx)));
    }
  }

  return INF;
}

signed main() {
  cin >> H >> W;
  Rep(i, H) Rep(j, W) {
    cin >> maps[i][j];
    if ( maps[i][j] == '%' ) ey = i, ex = j;
  }

  if ( bfs1() < bfs2() ) cout << "Yes" << endl;
  else cout << "No" << endl;
}

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

int main() {
  int N;
  string S;
  cin >> N >> S;
  if (N%2 != 0) {
      cout << "No" << endl;
      return 0;
  }
  for (int i=0; i<N/2; i++) {
    if(S.at(i) != S.at(i+N/2)){
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,k;
int main(){
  cin >> k >> a >> b;
  ll bis = 1;
  if(b-a<=2 || k<=a-1){
    bis = k+1;
  }else{
    bis += a-1;
    k -= (a-1);

    bis += (b-a)*(k/2) + k%2;
  }
  cout << bis << endl;
  return 0;
}


Measured memory_bytes: 256
N,Q = map(int, input().split())

A = [2**31 - 1] * N

q = [tuple(map(int, input().split())) for _ in range(Q)]

seg_num = 2**(N-1).bit_length()
ide_ele = float("inf")
func = min
seg_lst = [ide_ele] * 2 * seg_num

def init(lst):
    for i in range(N):
        # seg_lst1内のindexに変換
        seg_lst[i + seg_num - 1] = lst[i]
    
    # それぞれのノードについて、子の部分を引き上げてくる
    for i in range(seg_num - 2, -1, -1):
        seg_lst[i] = func(seg_lst[2*i + 1], seg_lst[2*i + 2])

def update(i, x):
    # seg_lst1内のindexに変換
    i += seg_num - 1
    seg_lst[i] = x
    while i:
        # 親に移って子の部分をくみ上げる
        i = (i - 1) // 2
        seg_lst[i] = func(seg_lst[2*i + 1], seg_lst[2*i + 2])

def query(l,r):
    if r <= l:
        return ide_ele

    l += seg_num - 1
    r += seg_num - 2

    ret = ide_ele

    while r - l > 1:

        if l & 1 == 0:
            ret = func(ret, seg_lst[l])
        if r & 1 == 1:
            ret = func(ret, seg_lst[r])
            r -= 1
        l  = l // 2
        r = (r - 1) // 2

    if l == r:
        ret = func(ret, seg_lst[l])
    else:
        ret = func(ret, seg_lst[l])
        ret = func(ret, seg_lst[r])
    return ret


init(A)

for c,x,y in q:
    if c == 0:
        update(x, y)
    else:
        print(query(x, y+1))


Measured memory_bytes: 23876
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define rep(i, n) for(int i=0;i<(int)(n);++i)
template <typename T> lint gcd(T a, T b) {return b?gcd(b, a%b):a;}
template <typename T> lint lcm(T a, T b) {return a * b / gcd(a, b);};
template <typename T> inline void chmin(T &a, const T& b){if(a>b)a=b;}
template <typename T> inline void chmax(T &a, const T& b){if(a<b)a=b;}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lint h, n, sum = 0;
  cin >> h >> n;
  vector<int> vec(n);
  rep(i, n) {
    cin >> vec[i];
    sum += vec[i];
  }
  if (sum >= h) {
    cout << "Yes" << '\n';
  } else{
    cout << "No" << '\n';
  }
  
}


Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

int powmod(int a, int n, int mod) {
	if (n == 0) return 1;
	if (n % 2 == 0) return powmod((a * a) % mod, n / 2, mod);
	return (a * powmod(a, n - 1, mod)) % mod;
}

int mod = 1000000007;
int n, m;
int fact[500001];
int finv[500001];

int comb(int n, int k) {
	return fact[n] * finv[k] % mod * finv[n - k] % mod;
}

signed main() {
	int i;
	
	cin >> n >> m;
	
	fact[0] = finv[0] = 1;
	for (i = 1; i <= m; i++) {
		fact[i] = i * fact[i - 1] % mod;
		finv[i] = powmod(fact[i], mod - 2, mod);
	}
	
	//i箇所一致する場合
	int ans = 0;
	for (i = 0; i <= n; i++) {
		int res1 = comb(m, i) * fact[i] % mod;
		int res2 = comb(m - i, n - i) * fact[n - i] % mod;
		int res3 = res1 * res2 % mod * res2 % mod;
		int res = comb(n, i) * res3 % mod;
		if (i % 2 == 0) ans = (ans + res) % mod;
		else ans = (ans - res + mod) % mod;
	}
	cout << ans << endl;
	
	return 0;
}

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

using namespace std;

string s[10010];

int a[1010],b[1010];

vector <string> v;

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

    memset(a,0,1010*sizeof(a[0]));

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

    for(int i=0;i<n;i++)
    {
        int count = 0;

        for(int j=0;j<m;j++)
        {
            if(s[i][j] == '#')
                count++;
        }

        if(count > 0)
            v.push_back(s[i]);
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<v.size();j++)
        {
            if(v[j][i] == '#')
                a[i] = 1;
        }
    }

    for(int i=0;i<v.size();i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[j] == 1)
                cout << v[i][j];
        }
        cout << endl;
    }
}

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define pb push_back
#define mp make_pair

int main()
{
    int n;
    int a[123456] = {};
    cin >> n;
    ll sum = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        sum += (ll)a[i];
    }
    ll div = (ll)(1 + n) * (ll)(n) / 2ll;
    ll cnt = sum / div;
    if(sum % div != 0) puts("NO");
    else{
        for(int i = 0; i < n; i++){
            ll cur;
            if(i == 0) cur = a[i] - a[n - 1] - cnt;
            else cur = a[i] - a[i - 1] - cnt;
            if(cur > 0 || cur % n != 0){
                puts("NO");
                exit(0);
            }
        }
        puts("YES");
    }

    return 0;
}


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

using namespace std;

int n, x, y, z;
long long ans = LLONG_MIN, k[100005], ta, tb, tc, add;
pair< long long, pair< long long, long long > > p[100005];
priority_queue< long long, vector< long long >, greater< long long > > q;

int main() {
	scanf("%d%d%d", &x, &y, &z);
	n = x + y + z;
	for (int i = 0; i < n; i++) {
		scanf("%lld%lld%lld", &ta, &tb, &tc);
		ta -= tc, tb -= tc, add += tc;
		p[i] = make_pair(ta - tb, make_pair(ta, tb));
	}
	sort(p, p + n);
	ta = 0;
	for (int i = 0; i <= n; i++) {
		if (i) {
			ta += p[i - 1].second.second;
			q.push(p[i - 1].second.second);
		}
		if (q.size() > y) {
			ta -= q.top();
			q.pop();
		}
		if (q.size() == y)
			k[i] += ta;
	}
	q = priority_queue< long long, vector< long long >, greater< long long > >();
	ta = 0;
	for (int i = n; i >= 0; i--) {
		if (i < n) {
			ta += p[i].second.first;
			q.push(p[i].second.first);
		}
		if (q.size() > x) {
			ta -= q.top();
			q.pop();
		}
		if (q.size() == x)
			k[i] += ta;
	}
	for (int i = y; i <= n - x; i++)
		ans = max(ans, k[i]);
	printf("%lld\n", ans + add);
	return 0;
}


Measured memory_bytes: 4472
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>

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

// 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;
struct INIT { INIT(){
  cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;


signed main() {
  string s, t, u; 
  cin >> s >> t >> u;

  cout << s[0] << t[1] << u[2] << "\n";

  return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include<queue>
#include<cmath>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)

struct stick{
    int red;
    int blue;
    int rnext;
    int bnext;
    int rm;
    int bm;
    int bef;
};

int labs(int a, int b){
	if(a>b)return a-b;
	else return b-a;
}

stick sticks[102];
int GCM(int a,int b){
    if(a==b||a%b==0){return b;}
    return GCM(min(a,b),labs(a,b) );
}

signed main(void){
    while(1){
        int N;
        scanf("%lld",&N);
        if(N==0)break;
        queue<int> que;
        rep(i,N){//initialize
            sticks[i].bnext=-1;
            sticks[i].rnext=-1;
            sticks[i].bef=-1;
            sticks[i].bm=1;sticks[i].rm=1;
        }
        rep(i,N){
            int p,q,r,b;
            scanf("%lld %lld %lld %lld",&p,&q,&r,&b);
            r--;b--;
            sticks[i].red=p/GCM(p,q);
            sticks[i].blue=q/GCM(p,q);
            if(r>-1){sticks[i].rnext=r;sticks[r].bef=i;}
            if(b>-1){sticks[i].bnext=b;sticks[b].bef=i;}
            if(r==-1&&b==-1){que.push(i);}
        }
        //rep(i,N){printf("%lld %lld %lld %lld\n",i,sticks[i].bef,sticks[i].rnext,sticks[i].bnext);}
        int ans=0;
        while(que.size()){
            int n=que.front();que.pop();
            int be=sticks[n].bef;
            int A,B;
            A=sticks[n].bm*sticks[n].red/GCM(sticks[n].bm,sticks[n].red);
            B=sticks[n].rm*sticks[n].blue/GCM(sticks[n].rm,sticks[n].blue);
            int alpha=(A/sticks[n].red)*(B/sticks[n].blue)/GCM(A/sticks[n].red, B/sticks[n].blue);
            if(be==-1){
                ans=alpha*(sticks[n].blue+sticks[n].red);
               // printf("[[%lld]] %lld %lld %lld %lld %lld/",n,A,B,sticks[n].blue,sticks[n].red,alpha);
                //break;
            }else{
                if(sticks[be].rnext==n){
                    sticks[be].rnext=-1;sticks[be].rm=alpha*(sticks[n].blue+sticks[n].red);
                }else{
                    sticks[be].bnext=-1;sticks[be].bm=alpha*(sticks[n].blue+sticks[n].red);
                }
            }
                //printf("[%lld] %lld %lld %lld %lld %lld/",n,A,B,sticks[n].blue,sticks[n].red,alpha);
            
            if(be>-1&&sticks[be].bnext==-1&&sticks[be].rnext==-1){
                que.push(be);
                //printf("(%lld)",be);
                //break;
            }
        }
        printf("%lld\n",ans);
    }
}

Measured memory_bytes: 2656
import java.util.Scanner;

public class Main {

public static void main(String args[]) {
	Scanner s = new Scanner(System.in);
	int a =s.nextInt();
	int b= s.nextInt();
	int c = s.nextInt();
	int min;
	if(a>b) {
		min = a;
		a = b;
		b = min;
	}
	if(a>c) {
		min = a;
		a = c;
		c = min;
	}
	if(b>c) {
		min = b;
		b = c;
		c = min;
	}
	System.out.println(a+" "+b+" "+c);
	s.close();
}
}



Measured memory_bytes: 26112
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> llP;

int main() {
  int n;
  cin>>n;
  int b[n];
  for(int i=0;i<n-1;i++) cin>>b[i];
  int ans=0;
  for(int i=0;i<n;i++){
    if(i==0){
      ans+=b[i];
      continue;
    }
    if(i==n-1){
      ans+=b[i-1];
      continue;
    }
    ans+=min(b[i-1],b[i]);
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
m[10000],c[10000];
int D(n){if(c[n]>=0)return c[n];c[n]=1;int r=D(m[n]);c[n]=0;return r;}
main(n,x,y,r,s){for(;scanf("%d",&n),n;printf("%d\n",r)){
	memset(c,255,sizeof(m));
	for(y=0;y<n;y++)for(x=0;x<n;x++)scanf("%d%d",&r,&s),m[y*n+x]=s*n+r;
	for(r=x=0;x<n*n;x++)r+=D(x);
}exit(0);}

Measured memory_bytes: 684
def merge(a, left, mid, right):
    INF = int(1e+11)
    l = a[left:mid]
    r = a[mid:right]
    l.append(INF)
    r.append(INF)
    i = 0
    j = 0

    ans = 0

    for k in range(left, right):
        if l[i] <= r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1
            if l[i] != INF:
                ans += mid - left - i

    return ans


def merge_sort(a, left, right):
    ans = 0
    if left + 1 < right:
        mid = (left + right) // 2
        ans += merge_sort(a, left, mid)
        ans += merge_sort(a, mid, right)
        ans += merge(a, left, mid, right)
        # print(left, right, ans)
    return ans

n = int(input())
a = [int(x) for x in input().split()]

print(merge_sort(a, 0, len(a)))


Measured memory_bytes: 27488
# 3 3   n: node, m: link
# 1 2   a_1   b_1
# 1 3
# 2 3   a_m   b_m

$n, $m = gets.split.map(&:to_i)
$array_a, $array_b = $m.times.map{ gets.split.map(&:to_i) }.transpose
$array_a.freeze
$array_b.freeze

# count_1_a = $array_a.count(1)
# count_1_b = $array_b.count(1)

indexes_1_a = []
indexes_1_b = []

$array_a.each_with_index do |a, i|
  indexes_1_a << i if a == 1
end
$array_b.each_with_index do |b, i|
  indexes_1_b << i if b == 1
end

reached_node = Array.new($n, false)
used_link = []
$ans = 0

# $binding = 0

def move(to, reached_node, used_link)
  # $binding += 1
  # puts "binding: #{$binding}"
  # p reached_node
  # p used_link
  if reached_node.count(true) == $n
    $ans += 1
    return true
  end
  if used_link.length == $m
    return false
  end

  indexes_to_a = []
  indexes_to_b = []

  $array_a.each_with_index do |a, i|
    indexes_to_a << i if a == to && !(used_link.include?(i))
  end
  $array_b.each_with_index do |b, i|
    indexes_to_b << i if b == to && !(used_link.include?(i))
  end

  indexes_to_a.each do |i|
    reached_node_temp = []
    reached_node.each do |j|
      reached_node_temp << j
    end
    used_link_temp = []
    used_link.each do |j|
      used_link_temp << j
    end
    to = $array_b[i]
    next if reached_node[to-1]
    reached_node_temp[to-1] = true
    used_link_temp << i
    move(to, reached_node_temp, used_link_temp)
  end
  indexes_to_b.each do |i|
    reached_node_temp = []
    reached_node.each do |j|
      reached_node_temp << j
    end
    used_link_temp = []
    used_link.each do |j|
      used_link_temp << j
    end
    to = $array_a[i]
    next if reached_node[to-1]
    reached_node_temp[to-1] = true
    used_link_temp << i
    move(to, reached_node_temp, used_link_temp)
  end
end

indexes_1_a.each do |i|
  reached_node = Array.new($n, false)
  from = $array_a[i]
  to = $array_b[i]
  reached_node[from-1] = true
  reached_node[to-1] = true
  move(to, reached_node, [i])
end
indexes_1_b.each do |i|
  reached_node = Array.new($n, false)
  from = $array_b[i]
  to = $array_a[i]
  reached_node[from-1] = true
  reached_node[to-1] = true
  move(to, reached_node, [i])
end

puts $ans



Measured memory_bytes: 14708
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct data{
	int t;
	int s;
	int d;
	bool operator<( const data& right ) const {
        return t < right.t;
    }
};

int main(void){
//	FILE* fp_in = freopen("data.txt", "r", stdin);
	while(1) {
		int N, M;
		vector< bool > c;
		vector< data > d;
	
		cin >> N >> M;
		if( N == 0 && M == 0 ) break;
	
		for(int i = 0; i < N; i++)
			c.push_back( false );
		c[0] = true;
	
		for(int i = 0; i < M; i++) {
			data newdata;
			cin >> newdata.t >> newdata.s >> newdata.d;
			newdata.s --;
			newdata.d --;
			d.push_back( newdata );
		}
			
		sort( d.begin(), d.end() );

		for(int i = 0; i < d.size(); i++) {
			if ( c[d[i].s] == true ) c[d[i].d] = true;
		}

		int ans = 0;
		for(int i = 0; i < c.size(); i++) {
			if( c[i] == true ) ans ++;
		}
		cout << ans << endl;
	}
//	while(1){}
    return 0;
}

Measured memory_bytes: 1708
#include <cstdio>
#include <cstdlib>


int parent(int i){
	return i/2;
}

int left(int i){
	return i*2;
}

int right(int i){
	return i*2+1;
}

int main(void){
	int i,n;
	int *A;
	scanf("%d", &n);
	A = new int[n];
	for(i=0;i<n;i++){
		scanf("%d", &A[i]);
	}
	
	for(i=0;i<n;i++){
		printf("node %d: key = %d, ", i+1, A[i]);
		if(i!=0){
			printf("parent key = %d, ", A[parent(i+1)-1]);
		}
		if(left(i+1) <= n){
			printf("left key = %d, ", A[left(i+1)-1]);
		}
		if(right(i+1) <= n){
			printf("right key = %d, ", A[right(i+1)-1]);
		}
		printf("\n");
	}
	return 0;
}

Measured memory_bytes: 2676
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

string s;
ll cur = 0;

ll hashf(ll a, ll b, ll c, ll d) {
    if (s[cur] == 'a') {
        cur++;
        return a;
    } else if (s[cur] == 'b') {
        cur++;
        return b;
    } else if (s[cur] == 'c') {
        cur++;
        return c;
    } else if (s[cur] == 'd') {
        cur++;
        return d;
    }
    assert(s[cur] == '[');
    cur++;
    char op = s[cur];
    cur++;
    ll p = hashf(a, b, c, d);
    ll q = hashf(a, b, c, d);
    assert(s[cur] == ']');
    cur++;
    if (op == '+') {
        return p | q;
    } else if (op == '*') {
        return p & q;
    } else {
        return p ^ q;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (1) {
        cin >> s;
        if (s == ".") {
            break;
        }
        ll num;
        cin >> num;
        ll numd = num % 10;
        num /= 10;
        ll numc = num % 10;
        num /= 10;
        ll numb = num % 10;
        num /= 10;
        ll numa = num;
        cur = 0;
        ll hs = hashf(numa, numb, numc, numd);
        ll ans = 0;
        REP(i, 0, 10) {
            REP(j, 0, 10) {
                REP(k, 0, 10) {
                    REP(l, 0, 10) {
                        cur = 0;
                        ll tmp = hashf(i, j, k, l);
                        if (hs == tmp) {
                            ans++;
                        }
                    }
                }
            }
        }
        cout << hs << " " << ans << endl;
    }
}



Measured memory_bytes: 3220
#include <iostream>

using namespace std;

int n;
int a[105];

int main(void)
{
	cin >> n;
	for(int i = 0; i < n; i++) cin >> a[i];
	
	int ans = 0;
	bool flag = false;
	for(int i = 1; i < n; i++){
		if(!flag){
			if(a[i] == a[i-1]){
				ans++;
				flag = true;
			}
		}else flag = false;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
LL n;
LL dp[maxn];
LL a[maxn];
LL k;
inline LL max(LL x, LL y) {
	return x > y ? x : y;
}
inline LL min(LL x, LL y) {
	return x < y ? x : y;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	dp[0]=dp[1] = 0;
	for (int i = 2; i <= n; i++)
		dp[i] = inf;
	for (int i = 2; i <= n; i++)
		for (int j = i - 1; j >= max(1LL, i - k); j--)
			dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
	cout << dp[n];
	return 0;
}

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

int main() {
  int a, b, ans;
  char sum;
  cin >> a >> b;
  for(int i = 0; i < a * b; i++) {
    cin >> sum;
    if(sum == '#') ans++;
  }
  cout << (a + b - 1 == ans ? "Possible" : "Impossible") << endl;
}


Measured memory_bytes: 256
// Problem : A - Coffee
// Contest : AtCoder Beginner Contest 160
// URL : https://atcoder.jp/contests/abc160/tasks/abc160_a
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define fio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ull unsigned long long
#define ll long long
#define inf INT_MAX
#define mninf INT_MIN
#define infl 1e18+5
#define mninfl -1e18-5
#define Mod 1000000007
#define newmod  998244353
int ah=911382323;
int bh=972663749;
#define endl "\n"
#define cp(x) cout<<(x)<<" "
#define cl(x) cout<<(x)<<endl
#define ct(x,y) cout<<(x)<<" "<<(y)<<endl;
#define nl   cout<<endl
//const long double pi=acos(-1);
//looping
#define int long long
#define For(i,a,b) for(int i=a;i<b;i++)
#define test(t)  int (t); cin>>(t); while((t)--)
#define array(a,n) int (n); cin>>(n); int a[n]; for(int i=0;i<(n);i++) cin>>a[i];
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define pa(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' ';  cout<<endl
#define pv(v)  for(int i=0;i<(v).size();i++) cout<<v[i]<<" ";  cout<<endl
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}
template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}
template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}
template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << " = " << it -> second ;
    }
    return os << "]";
}
#define deb(x) cerr << #x << " = " << x << endl;
#define deb2(x,y)  cerr << #x << " = " << x <<"   "<< #y << " = " << y << endl;
//containers
#define F first
#define S second
#define pb emplace_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mkp make_pair
#define all(v) (v).begin(),(v).end()
int dx[8]={1,-1,0,0,1,1,-1,-1};
int dy[8]={0,0,1,-1,1,-1,1,-1};
typedef vector<int> vi;
typedef vector< vector< int > > vvi;
typedef vector < pair <int, int> > vpii;
/*/----------------------------Code begins----------------------------------/*/
int32_t main(){
fio;
string s;
cin>>s;
if(s[2]==s[3]&&s[4]==s[5]){
	cout<<"Yes";
}
else cout<<"No";
}




























Measured memory_bytes: 256
/*
 _     _                             
| |__ | |__   __ _  __ _ _   _  __ _ 
| '_ \| '_ \ / _` |/ _` | | | |/ _` |
| |_) | | | | (_| | (_| | |_| | (_| |
|_.__/|_| |_|\__,_|\__, |\__, |\__,_|
                   |___/ |___/       
*/
#include<bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;

#define N  100005

ll int func(ll int n)
{
	if(n==1) return 1;
	if(n==0) return 0;
	return 2*func(n/2)+1;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		ll int n;
		cin>>n;
		cout<<func(n)<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
//set many funcs template
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007

int max(int a,int b){if(a>b){return a;}return b;}
int min(int a,int b){if(a<b){return a;}return b;}
int zt(int a,int b){return max(a,b)-min(a,b);}
int round(int a,int b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
int ceil(int a,int b){if(a%b==0){return a/b;}return (a/b)+1;}
int gcd(int a,int b){int c;while(b!=0){c=a%b;a=b;b=c;}return a;}
int lcm(int a,int b){int c=gcd(a,b);a/=c;return a*b;}
int nCr(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
int fact(int a){int i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
int pow(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
long long llmax(long long a,long long b){if(a>b){return a;}return b;}
long long llmin(long long a,long long b){if(a<b){return a;}return b;}
long long llzt(long long a,long long b){return llmax(a,b)-llmin(a,b);}
long long llround(long long a,long long b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
long long llceil(long long a,long long b){if(a%b==0){return a/b;}return (a/b)+1;}
long long llgcd(long long a,long long b){long long c;while(b!=0){c=a%b;a=b;b=c;}return a;}
long long lllcm(long long a,long long b){long long c=llgcd(a,b);a/=c;return a*b;}
long long llnCr(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
long long llfact(long long a){long long i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
long long llpow(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
double dbmax(double a,double b){if(a>b){return a;}return b;}
double dbmin(double a,double b){if(a<b){return a;}return b;}
double dbzt(double a,double b){return dbmax(a,b)-dbmin(a,b);}
int sortfncsj(const void *a,const void *b){if(*(int *)a>*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int sortfnckj(const void *a,const void *b){if(*(int *)a<*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int llsortfnckj(const void *a,const void *b){if(*(long long *)a<*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int dbsortfncsj(const void *a,const void *b){if(*(double *)a>*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int dbsortfnckj(const void *a,const void *b){if(*(double *)a<*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}

int main(void){
    char s[8]={0};
    int i;
    scanf("%s",s);
    for(i=0;i<strlen(s)-1;i++){
        if(s[i]=='A'&&s[i+1]=='C'){printf("Yes\n");return 0;}
    }
    printf("No\n");
    return 0;
}

Measured memory_bytes: 128
#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;


#define EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS)

class Point {
 public:
  double x, y;

  Point(double x = 0, double y = 0): x(x), y(y) {}

  Point operator + (Point p) { return Point(x+p.x, y+p.y); }
  Point operator - (Point p) { return Point(x-p.x, y-p.y); }
  Point operator * (double a) { return Point(a*x, a*y); }
  Point operator / (double a) { return Point(x/a, y/a); }

  double norm() { return x*x+y*y; }
  double abs() { return sqrt(norm()); }

  bool operator < (const Point &p) const {
    return x != p.x ? x < p.x : y < p.y;
  }

  bool operator == (const Point &p) const {
    return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;
  }
};

typedef Point Vector;
typedef vector <Vector> Polygon;

class Segment {
 public:
  Point p1, p2;

  Segment(Point p1 = Point(), Point p2 = Point()): p1(p1), p2(p2) {}
};

double norm(Vector a) {
  return a.x*a.x+a.y*a.y;
}

double abs(Vector a) {
  return sqrt(norm(a));
}

// ?????????|a||b|cos
double cross(Vector a, Vector b) {
  return a.x*b.y-a.y*b.x;
}
// ?????????|a||b|sin
double dot(Vector a, Vector b) {
  return a.x*b.x+a.y*b.y;
}

Point project(Segment s, Point p) {
  Vector base = s.p2-s.p1;
  double r = dot(p-s.p1, base)/norm(base);
  return s.p1+base*r;
}

int main() {
  Segment l;
  cin >> l.p1.x >> l.p1.y >> l.p2.x >> l.p2.y;
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    Vector p;
    cin >> p.x >> p.y;
    Vector ans = project(l, p);
    cout << fixed << setprecision(20) << ans.x << " "<< ans.y << endl;
  }
}

Measured memory_bytes: 1292
require("fs").readFileSync("/dev/stdin","utf8").split('\n').map(function(i){for(s=26;s--;)r=i.replace(/[a-z]/g,function(j){return String.fromCharCode((j.charCodeAt(0)+s)%26+97)}),r.match(/th(e|is|at)/)?console.log(r):0})

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

char *string[101];

void choice_sort(int num, int length){
  int i=0,j=0;
  int min_index=0;
  char *tmp=NULL;

  for(i=0;i < num-1;i++){
    min_index = i;
    for(j= i + 1 ;j < num;j++){
      if(strcmp(string[ min_index ] , string[ j ]) > 0){//s1 > s2
        min_index = j;
      }
    }
    //先頭に最大値をスワップ
      tmp = string[i];
      string[i] = string[min_index];
      string[min_index] = tmp;

  }
}

int main(int argc, char const *argv[]) {
  int i=0;
  char *ans=NULL;
  int num=0;
  int length=0;

  scanf("%d %d", &num, &length);
  ans = (char*)malloc(sizeof(char) * 101 * 101);
  memset(ans, '\0',101 * 101);

  for(i = 0; i < 101; i++) {
    string[i] = (char *)malloc(sizeof(char) * 101);
    memset( string[i], '\0', 101);
  }

  for(i=0;i<num;i++){
    scanf("%s", string[i]);
  }

  choice_sort(num, length);

  for(i=0;i<num;i++){
    strcat(ans, string[i]);
    //printf("%s\n",string[i] );
  }

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


  return 0;
}


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

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

void dfs(int y, int x, vector< vector<int> > &v){
    v[y][x] = 0;

    for(int i = 0 ; i < 8 ; i++){
        int ny = y + dy[i], nx = x + dx[i];
        if(v[ny][nx] == 1){
            dfs(ny, nx, v);
        }
    }

    return;
}

int main(){
    int w, h;
    while(cin >> w >> h, w){
        vector< vector<int> > c(h + 2, vector<int>(w + 2, 0));
        for(int i = 1 ; i <= h ; i++){
            for(int j = 1 ; j <= w ; j++){
                cin >> c[i][j];
            }
        }

        int ans = 0;
        for(int i = 1 ; i <= h ; i++){
            for(int j = 1 ; j <= w ; j++){
                if(c[i][j] == 1){
                    ans++;
                    dfs(i, j, c);
                }
            }
        }

        cout << ans << endl;
    }
}


Measured memory_bytes: 3140
def func(n, x):
    ans = 0
    for i in range(1, x // 3):
        for j in range(i+1, x // 2):
            k = x - i - j
            if j < k <= n:
                ans += 1
    return ans


while True:
    n, x = map(int, input().split())
    if n == 0 and x == 0:
        break
    print(func(n,x))



Measured memory_bytes: 5600
n, m, x = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(n)]
ans = 193934993409409
for mask in range(2 ** n):
    C = [0] * m
    cst = 0
    for i in range(n):
        if (mask >> i) & 1:
            cst += X[i][0]
            for j in range(1, m + 1):
                C[j - 1] += X[i][j]
    if min(C) >= x:
        ans = min(ans, cst)
if ans < 91912922191:
    print(ans)
else:
    print(-1)


Measured memory_bytes: 9092
N = int(input())
nums = []
for i in range(N):
    nums.append(list(map(int, input().split())))

sum = 0
for i in range(N):
    j = N - 1 - i
    if((nums[j][0] + sum) % nums[j][1] != 0):
        sum += nums[j][1] - ((nums[j][0] + sum) % nums[j][1])

print(sum)

Measured memory_bytes: 29428
n = int(input())
A = list(map(int,input().split()))

import math
from functools import reduce

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

def lcm_list(numbers):
    return reduce(lcm, numbers)

x = lcm_list(A)
ans = 0
for a in A:
    ans += (x-1)%a

print (ans)


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

const int N = 301;

int n, k, dp[N][N][N];
string s;

int solve(int l, int r, int k){
    if(r < l) return 0;
    if(l == r) return 1;
    int &ret = dp[l][r][k];
    if(ret != -1) return ret;
    ret = max(solve(l + 1, r, k), solve(l, r - 1, k));
    if(s[l] == s[r]) ret = max(ret, solve(l + 1, r - 1, k) + 2);
    else if(k > 0) ret = max(ret, solve(l + 1, r - 1, k - 1) + 2);
    return ret;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    memset(dp, -1, sizeof dp);
    cin >> s >> k;
    n = s.size();
    cout << solve(0, n - 1, k) << endl;
}


Measured memory_bytes: 106752
#include<stdio.h>
#define size 201
int main()
{
    int N,j=0;
    scanf("%d",&N);
    char S[N],T[N];
    int Array[size];
    scanf("%s",&S);
    scanf("%s",&T);
    for(int i=0;i<N;i++){
        Array[j]=S[i];
        Array[j+1]=T[i];
        j+=2;
    }
    for(int k=0;k<2*N;k++){
        printf("%c",Array[k]);
        if(k==2*N-1){
            printf("\n");
        }
    }
    return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/tag_and_trait.hpp>
#define BEGIN ios_base::sync_with_stdio(0);cin.tie(0)
#define END return EXIT_SUCCESS
#define FOR(I,A,B) for((I)=(A);(I)<(B);(I)++)
#define REP(I,N) FOR(I,0,N)
#define UP(I,A,B) for((I)=(A);(I)<=(B);(I)++)
#define DW(I,A,B) for((I)=(A);(I)>=(B);(I)--)
#define IN(P) std::cin>>(P)
#define IN2(P1,P2) std::cin>>(P1)>>(P2)
#define IN3(P1,P2,P3) std::cin>>(P1)>>(P2)>>(P3)
#define INS(I,N,V) REP(I,N) std::cin>>V[I]
#define OUT(P) std::cout<<P<<std::endl
#define OUT2(P1,P2) std::cout<<P1<<" "<<P2<<std::endl
#define OUT3(P1,P2,P3) std::cout<<P1<<" "<<P2<<" "<<P3<<std::endl
#define OUTS(I,N,V) REP(I,N) std::cout<<V[i]<<std::endl
#define ALL(C) (C).begin(),(C).end()
using namespace std;
using namespace __gnu_pbds;
typedef unsigned long ul;
ul N,W,w[101],v[101],dp[101][100001],i,j;
inline void solve(){
  IN2(N,W);
  REP(i,N){
    IN2(w[i],v[i]);
  }
  REP(i,N){
    REP(j,W+1){
      if(j+w[i]<=W)dp[i+1][j+w[i]]=max(dp[i+1][j+w[i]],dp[i][j]+v[i]);
      dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
    }
  }
  OUT(dp[N][W]);
}

int main(int argc,char**argv){
  BEGIN;
  solve();
  END;
}


Measured memory_bytes: 78336
using System;
using System.Linq;
using System.Collections.Generic;

class Amondai
{
    static void Main()
    {
        var abc = Console.ReadLine().Split(' ').Select(x => Int32.Parse(x)).ToArray();
        var max = abc[1] / abc[0];
        Console.WriteLine(abc[2]<max?abc[2]:max);
    }
}

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

int main() {

  vector<int64_t> lucas(87);

  lucas.at(0) = 2;
  lucas.at(1) = 1;

  for (int i =2; i <= 86; i++) {
    lucas.at(i) = lucas.at(i-2) + lucas.at(i-1);
  }

  int x;
  cin >> x;

  cout << lucas.at(x) << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
 int K;
 cin >> K;
 queue<long long int> p;
 for(int i = 1; i < 10; i++) {
     p.push(i);
 }
 long long int sum = 0;
 long long int a;
 while(sum < K - 1) {
    long long int num = p.front();
    
    p.pop();
    sum++;
    if(num % 10 == 0) {
    a = num * 10 + num % 10;
    p.push(a);
    a = num * 10 + num % 10 + 1;
    p.push(a);
    }else if(num % 10 == 9) {
    a = num * 10 + num % 10 - 1;
    p.push(a);
    a = num * 10 + num % 10;
    p.push(a);
    } else {
    a = num * 10 + num % 10 - 1;
    p.push(a);
    a = num * 10 + num % 10;
    p.push(a); 
    a = num * 10 + num % 10 + 1;
    p.push(a);
    }
 }

 cout << p.front() << endl; 
 return 0;
}

Measured memory_bytes: 1792
use std::io::{BufRead, Read};

pub struct Scanner<R>
where
    R: BufRead,
{
    reader: R,
}

impl<R> Scanner<R>
where
    R: BufRead,
{
    pub fn new(reader: R) -> Self {
        Self { reader }
    }

    pub fn next<T>(&mut self) -> T
    where
        T: ::std::str::FromStr,
        <T as ::std::str::FromStr>::Err: ::std::fmt::Debug,
    {
        self.reader
            .by_ref()
            .bytes()
            .map(|b| b.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>()
            .parse::<T>()
            .unwrap()
    }
}

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

    let s: String = sc.next();
    let p: String = sc.next();
    let mut contain: bool = false;

    'outer: for i in 0..s.len() {
        for j in 0..p.len() + 1 {
            if j == p.len() {
                contain = true;
                break 'outer;
            }
            // let x = s.chars().nth((i + j) % s.len()).unwrap();
            // let y = p.chars().nth(j).unwrap();

            // println!("x: {}, y:{}, i: {}, j: {}", x, y, i, j);
            if s.chars().nth((i + j) % s.len()).unwrap() != p.chars().nth(j).unwrap() {
                continue 'outer;
            }
        }
    }

    if contain {
        println!("Yes");
    } else {
        println!("No");
    }
}



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

using namespace std;

#define io freopen("test.txt", "r", stdin);
#define fast ios_base::sync_with_stdio(0);cin.tie(0);


#define ull unsigned long long int
#define lli long long int
#define li long int
#define ld long double

#define cout(x) cout<<x<<" "
#define coutn(x) cout<<x<<endl
#define newline cout<<endl

#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define ar array

#define pii pair<int,int>
#define pll pair<lli,lli>
#define pps pair<pair<int,int>, string>

const int MOD=1000000007;
const lli INF=1e18;
const int MX=100001;
const int N=1000000;


void solve(){

    int n,k;
    cin>>n>>k;

    vector<int>w(n+1),v(n+1);

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

    vector<vector<lli>>a(n+1, vector<lli>(k+1,0));


    for(int i=1;i<=n;i++){
        for(int j=1;j<=k;j++){
            if(w[i]<=j) a[i][j]=max(a[i-1][j], v[i]+a[i-1][j-w[i]]);
            else a[i][j]=a[i-1][j];
        }
    }


    // for(int j=1;j<=n;j++){
    //     for(int i=1;i<=k;i++){
    //         if(w[j-1]>=i) a[j%2][i]=max(a[(j+1)%2][i], v[j-1]+a[(j+1)%2][i-w[j-1]]);
    //         else a[j%2][i]=a[(j+1)%2][i];
    //     }
    // }

    // for(auto x:a){
    //     for(auto y:x) cout<<y<<" ";
    //     cout<<"\n";
    // }

    cout<<a[n][k]<<"\n";

}


int main(){
    fast
    // #ifndef godzqa
    //     io
    // #endif

    lli t;
    // cin>>t;
    t=1;

    while(t--){
        solve();
    }
    
    return 0;
}

Measured memory_bytes: 83076