akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector <long long> A;
    long long a,b;
    cin>>a;
    for(int i=0;i<a;i++){
        cin>>b;
        A.push_back(b);
    }
    long long cnt=0;
    for(int i=0;i<A.size()-1;i++){
        if(A[i]>A[i+1]){
            cnt+=A[i]-A[i+1];
            A[i+1]=A[i];
        }
    }
    cout<<cnt;
}


Measured memory_bytes: 5368
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n, k, s;

    cin >> n >> k >> s;

    for (int i = 0; i < k; i++) {
        cout << s << " ";
    }

    for (int i = k; i < n; i++) {
        if (s != 1000000000) {
            cout << s+1 << " ";
        }
        else {
            cout << 1 << " ";
        }
    }

    cout << endl;
}


Measured memory_bytes: 1280
M = 998244353
a, b, c, d = gets.split.map(&:to_i)

dp = Array.new(c+1) {Array.new(d+1, 0)}
dp[a][b] = 1
a.upto(c) do |i|
    b.upto(d) do |j|
        next if i == a && j == b
        dp[i][j] = (dp[i-1][j] * j + dp[i][j-1] * i - dp[i-1][j-1] * (i-1) * (j-1)) % M
    end
end

p dp[c][d]

Measured memory_bytes: 84764
#include <iostream>

int main() {
  int n;
  int m;

  std::cin >> n >> m;

  int *a = new int[n * m];
  int *b = new int[m];

  for(int i = 0; i < n; ++i) {
    for(int j = 0; j < m; ++j) {
      std::cin >> a[i * m + j];
    }
  }
  for(int i = 0; i < m; ++i) {
    std::cin >> b[i];
  }

  int *c = new int[n];
  for(int i = 0; i < n; ++i) {
    c[i] = 0;
    for(int j = 0; j < m; ++j) {
      c[i] += a[i * m + j] * b[j];
    }
    std::cout << c[i] << std::endl;
  }

  delete[] a;
  delete[] b;
  delete[] c;

  return 0;
}

Measured memory_bytes: 3088
#include <cstdio>

using namespace std;

int main() {
    while (true) {
        size_t H, W;
        scanf("%zu %zu", &H, &W);

        if (!W && !H)
            return 0;

        for (size_t i=0; i<H; ++i) {
            for (size_t j=0; j<W; ++j)
                printf("%c", (i+j)&1? '.':'#');

            printf("\n");
        }

        printf("\n");
    }
}

Measured memory_bytes: 2552
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int d = sc.nextInt();
        int g = sc.nextInt();
        List<Integer> p = new ArrayList<>();
        List<Integer> c = new ArrayList<>();
        for (int i = 0; i < d; i++) {
            p.add(sc.nextInt());
            c.add(sc.nextInt());
        }
        int min = 1001;
        for (int i = 0; i < (1 << d); i++) {
            int sum = 0;
            int num = 0;
            for (int j = 0; j < d; j++) {
                if ((i >> j & 1) == 1) {
                    sum += p.get(j) * (j + 1) * 100 + c.get(j);
                    num += p.get(j);
                    if (sum >= g) {
                        min = Math.min(min, num);
                    }
                }
            }
        //    System.out.printf("i = %d,sum = %d%n", i, sum);
        }

        int max = 0;
        int cnt = 0;
        for (int i = 0; i < d; i++) {
            for (int j = 1; j <= p.get(d - i - 1); j++) {
                max += (d - i) * 100;
                //   System.out.println(max);
                if (max >= g) {
                    min = Math.min(min, cnt + j);
                    //     System.out.println("==" + max);
                    //     System.out.println(min);
                }
            }
            max += c.get(d - i - 1);
            cnt += p.get(d - i - 1);
            // System.out.printf("min= %d,max= %d%n", min, max);
        }
        System.out.println(min);
    }
}


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

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

    vector<int> a(n);
    for (int i : a)
    {
        cin >> i;
        if (i % 2 == 0)
        {
            if (i % 3 != 0 && i % 5 != 0)
            {
                cout << "DENIED\n";
                return 0;
            }
        }
    }
    cout << "APPROVED\n";
    return 0;
}


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

int main()
{
	ll n,W;
	cin >> n >> W ;		
																				
	ll w[n];									
	int v[n];
	int max_value = 0;
	ll INF = 1e18+5;

	for (int i=0;i<n;i++){
		cin >> w[i] >> v[i];
		max_value+=v[i];} 

	vector <ll> dp(max_value+1,INF);			// interesting example of change of dimension in a
												//dp here we use the fact for min weight for a certain value
	dp[0]=0;									// unlike the regular dp in which we consider max value for a 	
	for (int i=1; i<=n; i++)					//certain weight 
	{
		for(int j=max_value; j>=v[i-1]; j--) 
        {
        	dp[j] = min(dp[j] , w[i-1] + dp[j-v[i-1]]);
        	//cout << dp[j] <<" ";
        }    
      //  cout << "\n";
	} 
	ll answer = 0;
	for (ll i=0;i<=max_value;i++) if (dp[i]<=W) answer = max(answer,i);
	cout << answer;
	return 0;
}



Measured memory_bytes: 3956
#include <iostream>
#include <vector>

using namespace std;


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

    vector<int> A;
    vector<int> B;

    for (int i = 0; i < a; ++i) {
        A.push_back(b);
    }

    for (int i = 0; i < b; ++i) {
        B.push_back(a);
    }

    int minSize = min(A.size(), B.size());

    for (int i = 0; i < minSize; ++i) {
        int x = A[i];
        int y = B[i];
        if (x < y) {
            for (int j : A) {
                cout << j;
            }
            return 0;
        } else if (y < x) {
            for (int j : B) {
                cout << j;
            }
            return 0;
        }
    }

    if (A.size() > B.size()) {
        for (int j : B) {
            cout << j;
        }
        return 0;
    } else {
        for (int j : A) {
            cout << j;
        }
        return 0;
    }
}

Measured memory_bytes: 256
from collections import Counter

N,A,B = map(int, input().split())

V = list(map(int, input().split()))

V.sort(reverse=True)

c = Counter(V)
# V[:A] は 逆順にA個取り出す
#print("V[:A]", V[:A])
max_average = sum(V[:A]) / A
#print("max_average", max_average)
# それぞれの要素の個数
c_max = Counter(V[:A])
#print("c_max", c_max)

# 組み合わせ
def nCr(n,r):
    if n < 0 or r < 0 or n < r: return 0
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
  
    # 分子のn*(n-1)*...がr個分続くやつ
    numerator = [n-r+k+1 for k in range(r)]
    # 分母：r!=r*(r-1)*...*3*2の要素
    denominator = [k+1 for k in range(r)]
  
    # 分母の要素で割れる部分を割っていく部分
    for p in range(2, r+1):
        # 分母は1,2,3,...rのようになっており、1は意味がないのでスキップした形か
        pivot = denominator[p-1]
        if pivot > 1:
          # 分子のX番目と分母のX-offset番目が共通の約数を持つということだと思う。piv分ずれているのだから、pivの倍数というところか
            offset = (n-r) % p
            for k in range(p-1, r, p):
                # 約分できる要素について割る
                numerator[k - offset]  /= pivot
                denominator[k] /= pivot
        
    ret = 1
    for k in range(r):
        if numerator[k] > 1: ret *= int(numerator[k])
      
    return ret

# A個選んだ時の最大平均値が、全て同じ価値で構成されている時
# この場合は、全体の個数の取り方が、A〜min(B, V_maxの個数)と、複数ある
if len(c_max) == 1:
    # A個選ぶ、A+1個選ぶ、...、min(B, c[V[0]])個選ぶ　の和が答え
    ans = 0
    n = c[V[0]]
    for r in range(A, min(B, n)+1):
        ans += nCr(n,r)


# 最大平均値が複数の価値から作られる場合、価値が大きい順から全て使われる価値がいくつかと、中途半端に使われる価値が一つになる
# なので、中途半端に選ばれるものの選び方を数え上げる
# この時、全体の個数はA個固定（さらに何かを追加すると平均が減少するため）
else:
    
    # 最大平均値を構成する、最小の価値を探す
    tmp = list(c_max.keys())
    tmp.sort()
    # 最小の値
    min_num = tmp[0]
    # 最小以外の数字で何個選んでいるか
    cnt = 0
    for i in tmp[1:]:
        cnt += c_max[i]
    remaining = A - cnt

    ans = nCr(c[min_num], remaining)

print(max_average)
print(ans)


Measured memory_bytes: 3316
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll N = 1e6 +9;
const ll p = 31;
const ll m = 1e9 + 7;
const ll inf= 1e14;
ll mod = 1e9+7;
ll powm(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }

int main() {   
     ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    
     ll t = 1;
     // cin>>t;
 
     while(t--){
          ll n;cin>>n;
          ll a[n];
          ll ans = 0;
          ll search = 1;
          for(int i= 0;i<n;i++){
               cin>>a[i];
               if(a[i]==search){
                    search++;
               }
               else{
                    ans++;
               }
          }
          if(ans==n){
               cout<<-1;
               return 0;
          }
          cout<<ans;

     }
}         

Measured memory_bytes: 1792
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
    long long n;
    long long sum=0;
    long long same=1;
    string s[100005];
    cin >> n;
    for(long long i=1;i<=n;i++)
    {
        cin >> s[i];
        sort(s[i].begin(),s[i].end());
    }
    sort(s+1,s+n+1);
    for(long long i=1; i<=n;i++)
    {
        if(s[i]==s[i-1])
        {
            sum+=same;
            same++;
        }
        else
        {
            same=1;
        }
    }
    cout << sum;
    return 0;
}


Measured memory_bytes: 7296
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL ans=1e18+10;
void check(int a,int b,int c){
	if (a<2) return;
	if (a&1) ans=min(ans,1ll*b*c);
	else ans=0;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int a,b,c;
	cin>>a>>b>>c;
	check(a,b,c);check(b,c,a);check(c,a,b);
	cout<<ans<<'\n';
	return 0;
}

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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

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

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const int INF = 1e9;
const ll MOD = 1e9 + 7;

const int MAXN = 2e5 + 5;

char s[MAXN];
char t[MAXN];

void solve(){
	int n;
	scanf("%s", &s[0]);
	n = strlen(s);
	int m = 0;
	REP(i, 0, n) {
		if (s[i] == 'B' && s[i + 1] == 'C') {
			t[m++] = 'D';
			i++;
		} else {
			t[m++] = s[i];
		}
	}
	int cnt = 0;
	ll ans = 0ll;
	for(int i = m - 1; i >= 0; i--) {
		if (t[i] == 'A') {
			ans += cnt;
		} else if (t[i] == 'D') {
			cnt++;
		} else {
			cnt = 0;
		}
	}
	cout << ans << endl;
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}


Measured memory_bytes: 768
n = int(input())
a_lst = list(map(int, input().split()))
q = int(input())
query = [tuple(map(int, input().split())) for _ in range(q)]
comp = sorted(a_lst)
diff = sum([a_lst[i] != comp[i] for i in range(n)])
if diff == 0:
  print(0)
else:
  for i in range(q):
    x, y = query[i]
    x -= 1
    y -= 1
    diff -= (a_lst[x] == comp[y]) + (a_lst[y] == comp[x]) - (a_lst[x] == comp[x]) - (a_lst[y] == comp[y])
    if diff == 0:
      print(i + 1)
      break
    a_lst[y], a_lst[x] = a_lst[x], a_lst[y]
  else:
    print(-1)


Measured memory_bytes: 65192
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define REP(i,n) for(int i=0;i<n;++i)
#define REP1(i,n) for(int i=1;i<=n;++i)
#define SZ(i) int(i.size())
#define eb emplace_back
#define ALL(i) i.begin(),i.end()
#define X first
#define Y second
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"#%d: %s = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
template<typename It> ostream& _printRng(ostream &os,It bg,It ed)
{
    os<<"{";
    for(It it=bg;it!=ed;it++) {
        os<<(it==bg?"":",")<<*it;
    }
    os<<"}";
    return os;
}
template<typename T> ostream &operator << (ostream &os,vector<T> &v){return _printRng(os,v.begin(), v.end());}
template<typename T> void pary(T bg, T ed){_printRng(cerr,bg,ed);cerr<<endl;}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#define pary(...)
#endif

const int MAXN = 100005;
const ll MOD = 1000000007;

int n, q;
ll m;

vector<array<int,2> > dt[2];

int sz, djs[MAXN];

int fnd (int x) {
    return x == djs[x] ? x : djs[x] = fnd(djs[x]);
}

void uni (int x, int y) {
    x = fnd(x), y = fnd(y);
    if (x != y) {
        sz--;
        djs[x] = y;
    }
}
/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> n >> m >> q;
    REP (i, q) {
        int a, b, c;
        cin >> a >> b >> c;
        dt[c].push_back({a,b});
    }

    REP1 (i, n) {
        djs[i] = i;
        sz++;
    }

    for (auto v : dt[0]) {
        uni(v[0], v[1]);
    }

    for (auto v : dt[1]) {
        if (fnd(v[0]) == fnd(v[1])) {
            cout << "No" << endl;
            return 0;
        }
    }


    if (m == n - 1) {
        if (dt[1].empty()) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    } else if (m <= n + 1LL*sz*(sz-3)/2) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}


Measured memory_bytes: 1784
#include <bits/stdc++.h>
#include <cmath>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP1(i,n) for(int i = 1; i < n+1 ;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 INF 2e9
#define ALL(v) v.begin(), v.end()
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
using namespace std;
typedef long long ll;
int main(){
  long X;
  cin >> X;
  cout << ((X - (X % 500)) / 500) * 1000 + ((X % 500) - (X % 500) % 5) <<endl;
}


Measured memory_bytes: 256
s = input()
s = [int(s[:2]), int(s[2:])]

ans = 0
if s[0] < 1 or s[0] > 12:
    ans += 1
if s[1] < 1 or s[1] > 12:
    ans += 2

p = ['AMBIGUOUS', 'YYMM', 'MMYY', 'NA']
print(p[ans])

Measured memory_bytes: 38256
N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())

prv = 0
ans = 0
for i in range(N):
    s = min(prv, A[i])
    A[i] -= s
    ans += s
    s = min(A[i], B[i])
    A[i] -= s
    B[i] -= s
    ans += s
    prv = B[i]
s = min(prv, A[N])
A[N] -= s
ans += s
print(ans)

Measured memory_bytes: 18624
n = int(input())
a = n // 3
b = n // 5
c = n // 15
print(n * (n + 1) // 2 - a * (a + 1) // 2 * 3 - b * (b + 1) // 2 * 5 + c * (c + 1) // 2 * 15)

Measured memory_bytes: 8996
import numpy as np
n,d = map(int, input().split())
arr = np.empty((0,d), int)
for i in range(n):
  x = np.array(list(map(int, input().split()))).reshape(1,d)
  arr = np.append(arr, x, axis = 0)
lst = []
for i in range(1,n):
  for j in range(i):
    y = sum((arr[i]-arr[j])**2)**0.5
    if y%1 == 0:
      lst.append(y)
print(len(lst))

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

#define sai(j,n) for(sei j=0;j<sei(n);++j)
#define gsai(j,n) for(sei j=sei(n)-1;j>=0;--j)
#define kan(j,m,n) for(sei j=m;j<sei(n);++j)
#define gkan(j,m,n) for(sei j=sei(n)-1;j>=sei(m);--j)
#define zen(h) (h).begin(),(h).end()
#define yu(h) v.erase(unique(zen(h)), h.end());
#define mugen 10000000111
#define yo 10000000711
#define kyo 1e-9
#define datu break
#define saiki(a) while(a)
#define seiri(h) sort(zen(h))
#define xin() main()
#define hen return
#define nyu(a) cin >> a
#define syutu(a) cout << a << endl
#define ru(a) if(a)
#define ta else
#define ruguo(a) else if(a)
#define tyo() length()
#define tei const

using namespace std;
typedef int seisu;
typedef void mu;
typedef long long sei;
typedef long double xyo;
typedef string moji;
typedef pair<sei, sei> so;
typedef pair<xyo, xyo> sox;
typedef vector<sei> hs;
typedef vector<xyo> hx;
typedef vector<hs> hhs;
typedef vector<hx> hhx;
typedef vector<moji> hm;
typedef vector<so> hso;
typedef vector<sox> hsox;
typedef bool singi;

const seisu h1[8] = {1,1,0,-1,-1,-1,0,1};
const seisu h2[8] = {0,1,1,1,0,-1,-1,-1};

sei dai(sei a, sei b){return max(a,b);}
sei syo(sei a, sei b){return min(a,b);}

moji m;
singi s=1;

seisu xin(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    nyu(m);
    sei t = m.length();

    ru(m[0] != 'A'){
        s = 0;
    }

    sei b = m.find('C');
    ru(b < 2 || b > t-2){
        s = 0;
    }

    kan(j,1,b){
        ru(isupper(m[j])){
            s = 0;
        }
    }

    kan(j,b+1,t){
        ru(isupper(m[j])){
            s = 0;
        }
    }

    ru(s){
        syutu("AC");
    }
    ta{
        syutu("WA");
    }
    hen 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        String a = sc.next();
        if(a.contentEquals("ARC"))
        	System.out.println("ABC");
        else
        	System.out.println("ARC");
    	sc.close();
	}
}

Measured memory_bytes: 35640
def solve():
    n = int(input())
    p = list(map(int, input().split()))
    ans = 0
    for i in range(1, n-1):
        if p[i-1] < p[i] and p[i] <= p[i+1]:
            ans += 1
        elif p[i+1] < p[i] and p[i] <= p[i-1]:
            ans += 1
    print(ans)


solve()


Measured memory_bytes: 38384
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
for i in range(n):
    a[i] -= 1
ans = 0
for i in range(n-1):
    ans += b[a[i]]
    if a[i] + 1 == a[i+1]:
        ans += c[a[i]]
ans += b[a[-1]]
print(ans)

Measured memory_bytes: 3064
package main

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

func main() {
	resolve(parseProblem(os.Stdin))
}

func parseProblem(r io.Reader) (int, int) {
	const (
		initialBufSize = 100000
		maxBufSize     = 1000000
	)
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	n, m := scanInt(sc), scanInt(sc)

	return n, m
}

func resolve(n, m int) {
	offset := 1
	startOffset := m + 1
	for i := m; i > 0; i-- {
		base := offset
		if (m-i)%2 == 1 {
			base += startOffset
		}
		fmt.Printf("%d %d\n", base, base+i)
		if (m-i)%2 == 1 {
			offset++
		}
	}
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}


Measured memory_bytes: 3312
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()



int main() {
    ll n, dp[105][4][4][4]{};
    string t = "AGCT";
    cin >> n;
    dp[0][3][3][3] = 1;
    for(int m=0;m<n;m++){
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                for(int l=0;l<4;l++){
                    if(j==0){
                        if(i==1 && l==2)continue;
                        if(i==2 && l==1)continue;
                    }
                    if(j==1){
                        if(i==0 && l==2)continue;
                    }
                    if(k==0 && j==1 && l==2)continue;
                    if(k==0 && i==1 && l==2)continue;
                    dp[m+1][l][i][j] += dp[m][i][j][k];
                    dp[m+1][l][i][j] %= MOD;
                }
            }
        }
    }
    }
    ll ans = 0;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                ans += dp[n][i][j][k];
                ans %= MOD;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define repn(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) for(int i = 0; i < (a); i++)
#define all(v) v.begin(), v.end() 
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'

#ifdef LOCAL
clock_t timer = clock();
#endif

int dp[100005][4];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n;
	cin >> n;
	int a[n], b[n], c[n];
	for(int i = 0; i < n; i++){
		cin >> a[i] >> b[i] >> c[i];
	}
	dp[0][1] = a[0];
	dp[0][2] = b[0];
	dp[0][3] = c[0];
	for(int i = 1; i < n; i++){
		dp[i][1] = max(dp[i - 1][2], dp[i - 1][3]) + a[i];
		dp[i][2] = max(dp[i - 1][1], dp[i - 1][3]) + b[i];
		dp[i][3] = max(dp[i - 1][1], dp[i - 1][2]) + c[i];
	}
	cout << max(dp[n - 1][1], max(dp[n - 1][2], dp[n - 1][3])) << endl;
#ifdef LOCAL
        cout << "Total Time: " << (double)(clock() - timer) / CLOCKS_PER_SEC << endl;
#endif
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


Measured memory_bytes: 2944
i,a[1<<17],b[1<<17];c;
buf[1145141];char*p=buf;
main(s){
	for(read(0,buf,1000000);*p;p++)if(*p>10)a[i]=a[i]*10+*p-48;else ++i;
	for(b[i=s]=1;!b[s=a[s]]++&&s-2;i++);
	printf("%d",s==2?i:-1);
}

Measured memory_bytes: 1536
//  Created by conan1024hao in 2019.
//  Copyright © 2019 conan1024hao. All rights reserved.
//  専用ライブラリです、自由にコピーして構いません。
//  感谢看我的代码！Wechat:conan1024hao QQ:810396815
#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define INF 10e16
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll>P;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
ll cmp(pair<ll,string>a,pair<ll,string> b){
        if(a.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se<b.se;
}

//---------------------------------------------------------------------------
int combination(int n,int r)
{
    int a=1;
    int b=1;
    if(n-r>=r)
    {
        for(int i=2;i<=r;i++)
            a*=i;
        for(int i=n-r+1;i<=n;i++)
            b*=i;
        return b/a;
    }
    else
    {
        for(int i=2;i<=n-r;i++)
            a*=i;
        for(int i=r+1;i<=n;i++)
            b*=i;
        return b/a;
    }
}

//---------------------------------------------------------------------------
int main(){//問題をちゃんと見ろ!!!!!!!!!!!!!!!!!      llか??????????
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    
    int n;cin>>n;
    int m[102];
    memset(m, 0, sizeof(m));
    for(int i=2;i<=n;i++){
        int now=i;
        for(int j=2;j<=i;j++){
            while(now%j==0){
                m[j]+=1;
                now/=j;
            }
        }
    }
    int sum=0;
    //75 = 3 * 5 * 5 = (2+1) * (4+1) * (4+1) = (4+1) * (14+1) = (2+1) * (24+1)
    //Senくんのて提出を参考した
    int o2=0,o4=0,o14=0,o24=0,o74=0;//cout<<o2<<" "<<o4<<" "<<o14<<" "<<o24<<" "<<o74<<endl;
    for(int i=2;i<=n;i++){
      //  cout<<m[i]<<endl;
        if(m[i]>=2)
            o2++;
        if(m[i]>=4)
            o4++;
        if(m[i]>=14)
            o14++;
        if(m[i]>=24)
            o24++;
        if(m[i]>=74)
            o74++;
    }
    //cout<<o2<<" "<<o4<<" "<<o14<<" "<<o24<<" "<<o74<<endl;
    sum+=(o2-2)*o4*(o4-1)/2;
    sum+=(o4-1)*o14;//括号里的（-）是为了把自己去除
    sum+=(o2-1)*o24;
    sum+=o74;
    cout<<sum<<endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------


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


int main() {
    int N; cin >> N;
    vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i];
    vector<int> left(N + 1, 0);
    vector<int> right(N + 1, 0);

    for (int i = 0; i < N; i++) left[i + 1] = __gcd(left[i], A[i]);
    for (int i = N; i > 0; i--) right[i - 1] = __gcd(right[i], A[i - 1]);
    int ans = 1;
    for (int i = 0; i < N; i++) ans = max(ans, __gcd(left[i], right[i + 1]));

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


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

const int inf = 1000000000;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  x--; y--;

  vector<int> ans(n);

  for (int sv = 0; sv < n; sv++) {
    queue<int> q;
    vector<int> dist(n, inf);
    auto push = [&] (int v, int d) {
      if (dist[v] != inf) return;
      dist[v] = d;
      q.push(v);
    };
    push(sv, 0);

    while (!q.empty()) {
      int v = q.front(); q.pop();
      if (v+1 < n) push(v+1, dist[v]+1);
      if (v-1 >= 0) push(v-1, dist[v]+1);
      if (v == x) push(y, dist[v]+1);
      if (v == y) push(x, dist[v]+1);
    }
    for (int i = 0; i < n; i++) ans[dist[i]]++;
  }
  for (int i = 0; i < n; i++) ans[i] /= 2;
  for (int i = 1; i < n; i++) cout << ans[i] << endl;
}

Measured memory_bytes: 256
using System;
class Program {
    static void Main(string[] args){
        string[] input = Console.ReadLine().Split(' ');
        int n = int.Parse(input[0]);
        int k = int.Parse(input[1]);
        string s = Console.ReadLine();
        char[] c = new char[n];
        c = s.ToCharArray();
        c[k - 1] = Char.ToLower(c[k - 1]);
        s = new String(c);
        Console.WriteLine(s);
    }
}

Measured memory_bytes: 13140
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
using namespace std;

#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000000;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

///////////////////////////

bool comp(int x[10], int y[10]) {
	ll xsum=0, ysum=0;
	repn(i,9){
		xsum += x[i];
		ysum += y[i];
	}

	if (xsum > ysum) { return true; }
	if (ysum > xsum) { return false; }

	repn(i, 9) {
		if (x[10 - i] > y[10 - i]) { return true; }
		if (y[10 - i] > x[10 - i]) { return false; }

	}

}




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

	int bo[10];
	bo[1] = 2;
	bo[2] = 5;
	bo[3] = 5;
	bo[4] = 4;
	bo[5] = 5;
	bo[6] = 6;
	bo[7] = 3;
	bo[8] = 7;
	bo[9] = 6;

	int a[10];
	repn(i, M) cin >> a[i];

	int val[100000][10] = {};

	repn(i, N) {
		repn(j, 9) {
			val[i][j] = -1;
		}
	}

	repn(j, 9) {
		val[0][9] = 0;
	}

	repn(i, N) {
		repn(j, M) {
			int c = a[j];

			int nval[10];

			if (i >= bo[c]&&val[i-bo[c]][1]!=-1) {
				repn(k, 9) {
					if (k != c) { nval[k] = val[i - bo[c]][k]; }
					if (k == c) { nval[k] = val[i - bo[c]][k] + 1; }
				}

				

				if (comp(nval,val[i])) {
					repn(k, 9) {
						val[i][k] = nval[k];
					}
				}

				 
				

			}
		}

		
	}

	for(int k=9;k>0;k--) {
		while (val[N][k] > 0) {
			cout << k;
			val[N][k]--;
		}
	}


	system("PAUSE");
}

Measured memory_bytes: 4344
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define hell1 1e9+7
#define hell2 1e9+9
#define pi 3.1415926535
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define ll long long
#define ld long double 
#define no_of_digits(n) floor(log10(n)) + 1
#define see(x) cout << (#x) << " is " << (x) << endl
#define INF 1e9

using namespace std;

ll mult(ll a,ll b, ll p=hell1){   return ((a%p)*(b%p))%p; }
ll multbig(ll a,ll b,ll mod=hell1){   if(a==0 or b==0) return 0;if(a==1 or b==1) return (a*b)%mod;ll cur=multbig(a,b/2,mod);cur=(2*cur)%mod;if(b%2) cur=(cur+a)%mod;return cur;
}
ll add(ll a, ll b, ll p=hell1){ if(a>=p) a%=p; if(b>=p) b%=p; if(a+b>p) return (a + b)%p; else return a+b;}
ll sub(ll a, ll b, ll p=hell1){return (a%p - b%p + p)%p;}
ll fpow(ll n, ll k, ll p = hell1) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = mult(r,n,p); n = mult(n,n,p);} return r;}
ll inv(ll a, ll p = hell1) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = hell1){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}
int isPowerof2(ll x) { return (x && !(x & x-1)); } 
ll log2(ll x) { ll res = 0; while (x >>= 1) res++; return res; } 

// max({a, b, c, d})

vector<ll>adj[200005];
ll update[200005];
ll ans[200005];

int binaryToDecimal(string n) 
{ 
    string num = n; 
    int dec_value = 0; 
  
    // Initializing base value to 1, i.e 2^0 
    int base = 1; 
  
    int len = num.length(); 
    for (int i = len - 1; i >= 0; i--) { 
        if (num[i] == '1') 
            dec_value += base; 
        base = base * 2; 
    } 
  
    return dec_value; 
} 

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

    int ans = c-a+b;

    if(ans<0) cout << 0 << "\n";
    else cout << ans << "\n";
}




Measured memory_bytes: 6016
a = int(input())
import math

def is_prime(n):
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True

while 1:
    if is_prime(a) == False:
        a = a+1
    else:
        break

print(a)

Measured memory_bytes: 2940
#include<bits/stdc++.h>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;

//typedef int kz;
//typedef long kz;
typedef long long kz;

kz gcd(kz a,kz b) { if(b==0) return a; else gcd(b,a%b); }
kz lcm(kz a,kz b) { return a*b/gcd(a,b); }

/**
 * vector<int>ar(3);
 * for(auto&e:ar){
 *     scanf("%d",&e);
 * }
 * sort(ar.begin(),ar.end())
 * int sum=accumulate(ar.begin(),ar.end(),0);
 **/


int main(){
    int mod=1000000007;
    int n,m;
    string c;
    cin>>c;
    reverse(c.begin(),c.end());
    cout<<c<<endl;
    return 0;
}

Measured memory_bytes: 2976
import sys
sys.setrecursionlimit(10000)

def bfs(x, y, graph):
  dxy = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
  for dx, dy in dxy:
    nextx = x + dx
    nexty = y + dy
    if (graph[nextx][nexty] == 1):
      graph[nextx][nexty] = 0
      bfs(nextx, nexty, graph)

while(1):
  W, H = map(int, input().split())
  if(H == 0 and W == 0):
    break
    
  graph = [[0] * (W+2) for _ in range(H+2)]
  for i in range(H):
    x = list(map(int, input().split()))
    for j in range(W):
      graph[i+1][j+1] = x[j]
  
  cnt = 0
  for i in range(1,H+1):
    for j in range(1,W+1):
      if(graph[i][j] == 1):
        cnt += 1
        bfs(i, j, graph)
  print(cnt)
  


Measured memory_bytes: 8372
k=int(input())
k-=1
z=[]
zz=[]
for i in range(1,10):
    z.append(str(i))
zz+=z
for i in range(9):
    tmp=[]
    for j in z:
        e=int(j[-1])
        for p in range(3):
            if 0<=e+(p-1)<=9:
                tmp.append(j+str(e+p-1))      
    z=tmp
    zz+=z
for i in range(len(zz)):
    zz[i]=int(zz[i])
zz.sort()

print(zz[k])

Measured memory_bytes: 74460
#include <iostream>

using namespace std;

int main(){
    int n, d, x;
    cin >> n >> d >> x;
    --d;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        x += d / a + 1;
    }
    cout << x << endl;
    return 0;
}


Measured memory_bytes: 384
N=int(input())
alist=[]
for i in range(N):
    alist.append(int(input()))
#print(alist)
hh = [0]*101
for i in range(N):
    hh[alist[i]] = max(1,hh[alist[i]])
#print(hh)
ss = sum(hh)
print(ss)


Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

int main(){
  int n;
  int a[100010]={0};
  ll sum=0;
  cin>>n;
  a[0]=0;
  REP(i,1,n+1)cin>>a[i];
  a[n+1]=0;
  REP(i,1,n+2){
    sum+=abs(a[i-1]-a[i]);
    //cout<<abs(a[i-1]-a[i])<<endl;
  }
  REP(i,1,n+1){
    cout<<(sum-abs(a[i-1]-a[i])-abs(a[i]-a[i+1])+abs(a[i-1]-a[i+1]))<<endl;
  }
  return 0;
}


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

using namespace std;
typedef long long ll;
#define mem(dp,a)           memset(dp,a,sizeof dp)
#define rep(i,a,b)          for(ll i=a;i<b;i++)
#define repb(i,a,b)         for(ll i=a;i>=b;i--)
#define f(n)                for (ll i=0;i<n;i++)
#define pb(x)               push_back(x)
#define mp(x,y)             make_pair(x,y)
#define tr(c,it)            for(((c).begin())it=(c).begin();it!=(c).end();it++)
#define sz(c)               (c).size()
#define all(c)              (c).begin(),(c).end()
#define sl(a)               scanf("%lld",&a);
#define TH                  1000
#define F                   first
#define S                   second
#define N                   100005
ll MOD=pow(10,9)+7;
ll INF=1ll<<60;

ll dp[101][100001];

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

    vector <ll> v;
    v.pb(0);
    ll w[n+1];

    rep(i,1,n+1)
    {
        cin>>w[i];
        ll k;cin>>k;
        v.pb(k);
    }

    rep(i,0,n+1)
    {
        rep(j,0,100001)
            dp[i][j]=INF;
    }

    rep(i,0,n+1)
    dp[i][0]=0;

    rep(i,1,n+1)
    {
        rep(j,1,100001)
        {
            if(j>=v[i])
                dp[i][j]=min(w[i]+dp[i-1][j-v[i]],dp[i-1][j]);
            else
                dp[i][j]=dp[i-1][j];
        }
    }

    ll ans=-1;
    rep(i,1,n+1)
    {
        rep(j,1,100001)
        {
            if(dp[i][j]!=INF && dp[i][j]<=W)
                ans=j;
        }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 79104
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits>

using namespace std;


int main(int argc, char** argv){
    int A, B, C;
    cin >> A >> B >> C;
    if (A < C && C < B) cout << "Yes\n";
    else if (B < C && C < A) cout << "Yes\n";
    else cout << "No\n";

    return 0;
}


Measured memory_bytes: 256
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		if (n==1) {
			System.out.println("Hello World");
		} else {
			int a = sc.nextInt();
			int b = sc.nextInt();
			System.out.println(a+b);
		}
	}
}


Measured memory_bytes: 21204
#include <iostream>
#include <string>
int main(){
  int n,a,b,ans=0;
  std::cin>>n;
  a=n/100;b=n%100;
  if (a==0||a>12)ans+=2;
  if (b==0||b>12)ans++;
  if (ans==0){
    std::cout << "AMBIGUOUS" << '\n';
  }else if (ans==1){
    std::cout << "MMYY" << '\n';
  }else if (ans==2){
    std::cout << "YYMM" << '\n';
  }else{
    std::cout << "NA" << '\n';
  }
}


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

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define PI 3.14159265358979
#define MOD 1000000007 // = 10^9 + 7

using namespace std;
using ll = long long;


void solve()
{
	string s;
	cin >> s;
	for(int i = 0; i < s.length(); ++i)
	{
		if((i & 1) == 1 && s[i] == 'R')
		{
			cout << "No";
			return;
		}
		if((i & 1) == 0 && s[i] == 'L')
		{
			cout << "No";
			return;
		}
	}
	cout << "Yes";
}

int main()
{
	fastio;
	solve();

	return 0;
}

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

int main() {
  int N,i;
  cin >> N;
  int p[N];
  for(i=0;i<N;i++)
    cin >> p[i];
  sort(p,p+N);
  int ans=0;
  if(p[N/2]-p[N/2-1]>=0)
    ans=p[N/2]-p[N/2-1];
  cout << ans << endl;
}


Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){

  int n,w,w1[100],v1[100];

  cin>>n>>w;
  vector <int> now[n+1];

  for(int i=0;i<n;i++) cin>>v1[i]>>w1[i];
  for(int i=0;i<n+1;i++) now[i].assign(w+1,0);

  for(int i=0;i<n;i++) for(int j=0;j<w+1;j++){
      if(j<w1[i]) now[i+1][j]=now[i][j];
      else now[i+1][j]=max(now[i][j],now[i][j-w1[i]]+v1[i]);
    }

  sort(now[n].begin(),now[n].end());
  cout<<now[n][w]<<endl;

  return 0;
}

Measured memory_bytes: 6904
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <valarray>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <limits>
#include <numeric>
#include <utility>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <regex>
#include <array>
#include <bitset>
#include <complex>
#include <tuple>
#include <random>

using namespace std;
using ll = long long;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

int main()
{
  int n;
  cin >> n;
  if (n % 10 == 3)
    cout << "bon" << endl;
  else if (n % 10 == 0 || n % 10 == 1 || n % 10 == 6 || n % 10 == 8)
    cout << "pon" << endl;
  else
    cout << "hon" << endl;

  return 0;
}


Measured memory_bytes: 3132
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
class BIT {
  vector<T> tree_;

 public:
  BIT(int n) : tree_(n, 0) {}
  void Add(int i, T x) {
    for (i++; i <= tree_.size(); i += i & -i) {
      tree_[i - 1] += x;
    }
  }

  T Sum(int s) {
    T ans = 0;
    for (s++; s > 0; s -= s & -s) {
      ans += tree_[s - 1];
    }

    return ans;
  }

  T Sum(int l, int r) { return Sum(r) - Sum(l - 1); }
};

int main(int argc, const char *argv[]) {
  using ll = long long;
  int n;
  cin >> n;
  vector<vector<int>> vs(n);
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    vs[c - 1].push_back(i);
  }

  vector<vector<int>> tree(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }

  int k = 0;
  vector<int> in(n), out(n);
  function<void(int, int)> dfs;
  dfs = [&](int v, int p) {
    in[v] = k++;
    for (int u : tree[v]) {
      if (u != p) {
        dfs(u, v);
      }
    }

    out[v] = k - 1;
  };

  dfs(0, -1);

  auto c2 = [](ll n) -> ll { return n * (n + 1) / 2; };
  ll tot = c2(n);
  BIT<ll> d(n);
  for (int i = 0; i < n; i++) {
    d.Add(i, 1);
  }

  for (int i = 0; i < n; i++) {
    ll ans = tot;
    vector<int> &nvs = vs[i];
    sort(nvs.begin(), nvs.end(), [&](int a, int b) { return in[a] > in[b]; });
    vector<pair<int, int>> history;
    for (int v : nvs) {
      int cnt = 1;
      for (int u : tree[v]) {
        if (in[u] < in[v]) {
          continue;
        }

        int num = d.Sum(in[u], out[u]);
        ans -= c2(num);
        cnt += num;
      }

      d.Add(in[v], -cnt);
      history.emplace_back(in[v], cnt);
    }

    ans -= c2(d.Sum(n - 1));
    for (auto p : history) {
      d.Add(p.first, p.second);
    }

    cout << ans << '\n';
  }

  return 0;
}

Measured memory_bytes: 41212
use std::io::*;
use std::str::FromStr;
#[allow(unused_imports)]
use std::collections::*;

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

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

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

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

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

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

trait Joinable {
    fn join(self, sep: &str) -> String;
}

impl<U: ToString, T: Iterator<Item = U>> Joinable for T {
    fn join(self, sep: &str) -> String {
        self.map(|x| x.to_string()).collect::<Vec<_>>().join(sep)
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    std::thread::Builder::new()
        .stack_size(104_857_600)
        .spawn(solve)
        .unwrap()
        .join()
        .unwrap();
}

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

    loop {
        let n = sc.read();
        let x: i64 = sc.read();
        let mut ans = 0;
        let mut a = sc.vec::<i64>(n);
        for i in 0..n-1 {
            let mut over = a[i] + a[i+1] - x;
            if over > 0 {
                ans += over;
                let eat = min(over, a[i+1]);
                a[i+1] -= eat;
                over -= eat;
                if over > 0 {
                    a[i] -= over;
                }
            }
        }
        println!("{}", ans);
    }
}


Measured memory_bytes: 8572
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
    // ifstream in("data1.txt");
    // cin.rdbuf(in.rdbuf());
    int n,m;
    cin >> n >> m;
    int A[n][m];
    int b[m];
    for(int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++){
            cin >> A[i][j];
        }
    }

    rep(i, m) cin >> b[i];

    for(int i = 0; i < n; i++) {
        int tmp = 0;
        for(int j = 0; j < m; j++) {
            tmp += A[i][j] * b[j];
        }
        cout << tmp << endl;
    }
}



Measured memory_bytes: 3124
import sys


def main() -> None:
    n = int(next_str())
    s = next_str()
    t = next_str()

    ans = n
    for i in range(n):
        flg = False
        for j in range(n - i):
            if s[i + j] != t[j]:
                ans += 1
                flg = True
                break
        if not flg: break
    print(ans)


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

Measured memory_bytes: 3188
#include <cassert>
#include <algorithm>
#include <numeric>
#include <iostream>
using namespace std;
constexpr int M = 1000000007;

class ModComb {
    long long *fact, *facti;
    int mod;
public:
    explicit ModComb(int n, int m) : mod(m) {
        fact = new long long[n+1];
        facti = new long long[n+1];
        fact[0] = 1; facti[0] = 1;
        for (int i = 1; i <= n; i++) fact[i] = (fact[i-1] * i) % m;
        // calc 1/n!
        long long &inv = facti[n], pw = fact[n];
        inv = 1;
        for (int e = mod-2; e > 0; e /= 2) {
            if (e&1) inv = inv * pw % mod;
            pw = pw * pw % mod;
        }
        for (int i = n-1; i > 0; i--) facti[i] = (facti[i+1] * (i+1)) % m;
        for (int i = 1; i <= n; i++) assert(fact[i] * facti[i] % m == 1);
    }

    ~ModComb() {
        if (fact) delete[] fact;
        if (facti) delete[] facti;
    }

    long long get(int n, int k) {
        if (n < 0 || k < 0 || k > n) return 0;
        return fact[n] * facti[k] % M * facti[n-k] % M;
    }
};

int main() {
    int h, w, n; cin >> h >> w >> n;
    pair<int, int> P[n];
    for (int i = 0; i < n; i++) cin >> P[i].first >> P[i].second;
    sort(P, P+n);
    ModComb mc(h+w, M);
    long long dp[n] = {};
    for (int i = 0; i < n; i++) {
        int r = P[i].first, c = P[i].second;
        long long sum = 0;
        for (int j = 0; j < n; j++) {
            int r1 = P[j].first, c1 = P[j].second;
            if (r1 <= r && c1 <= c) sum += dp[j] * mc.get(r+c-r1-c1, r-r1) % M;
        }
        dp[i] = (mc.get(r+c-2, r-1) - sum) % M;
    }
    long long ans = mc.get(h+w-2, h-1);
    for (int i = 0; i < n; i++) {
        int r = P[i].first, c = P[i].second;
        ans -= dp[i] * mc.get(h+w-r-c, h-r) % M;
    }
    cout << (ans % M + M) % M << endl;
}


Measured memory_bytes: 3456
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef long long ll;

int main()
{
    int n;
    ll have=0;
    ll mon=1000;
    ll save=1000;
    scanf("%d",&n);
    int a[n];
    scanf("%d",&a[0]);

    for(int i=1;i<n;i++)
    {
        scanf("%d",&a[i]);

        if(a[i]>a[i-1])
        {
            have=have+mon/a[i-1];
            mon=mon-a[i-1]*(mon/a[i-1]);
        }
        else if(a[i]<a[i-1])
        {
            mon=mon+a[i-1]*have;
            have=0;
            save=mon;
        }
    }

    if(have!=0)
    {
        while(have>0)
        {
            mon=mon+a[n-1];
            have=have-1;
        }
        if(mon>save)
        {
            save=mon;
        }
    }

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

    
}

Measured memory_bytes: 1672
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> P;
  vector<int> Q;
  for (int i = 0; i < N; i++) {
    int p;
    cin >> p;
    P.push_back(p);
  }

  for (int i = 0; i < N; i++) {
    int q;
    cin >> q;
    Q.push_back(q);
  }

  vector<int> ans;
  for (int i = 0; i < N; i++) ans.push_back(i + 1);

  ll pcnt = 0, qcnt = 0;
  bool pflag = false, qflag = false;
  ll a = 1, b = 1;
  ll cnt = 0;
  do {
    cnt++;
    for (int i = 0; i < N; i++) {
      if (ans[i] == P[i]) pcnt++;
      if (ans[i] == Q[i]) qcnt++;
    }
    if (pcnt == N) {
      pflag = true;
      a = cnt;
    }

    if (qcnt == N) {
      qflag = true;
      b = cnt;
    }
    pcnt = 0, qcnt = 0;
    if (pflag && qflag) break;
  } while (next_permutation(ans.begin(), ans.end()));

  cout << abs(a - b) << endl;
  return 0;
}

Measured memory_bytes: 256