akhauriyash/Code-Regression huggingface dataset:
// AOJ 0275 Railroad
// 2018.2.21 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define QMAX 10000
typedef struct { int t, s; } QUE;
QUE que[QMAX+2]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i)
{
	int l, r, min;
	QUE qt;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
	if (r < qsize && que[r].t < que[min].t) min = r;
	if (min != i) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		min_heapify(min);
	}
}

void deq()
{
	que[0] = que[--qsize];
	min_heapify(0);
}

void enq(int s, int t)
{
	int i, min;
	QUE qt;

	i = qsize++;
	que[i].s = s, que[i].t = t;
	while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		i = min;
	}
}

#define INF 0x10101010
#define MAX 100003

int S;
int   memo[600002];
int   hi[MAX], *to[MAX]; short *w[MAX];
int dist[MAX];
char  mk[MAX];
int  hi2[MAX], *to2[MAX];

int   q[MAX], top, tail;
typedef struct { int s, e; } T;
T ord[200002], _ord[200002]; int sz;
int f[200002];

int c[40002], d[40002];
unsigned long long dp[MAX];

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0;
	int c = getchar_unlocked();
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

void dijkstra(int start)
{
	int i, s, e, d;

	memset(dist, INF, S << 2);
	qsize = 0;
	enq(start, 0), dist[start] = 0;
	while (qsize) {
		s = que[0].s, deq();
		for (i = 0; i < hi[s]; i++) {
			e = to[s][i], d = dist[s] + w[s][i];
			if (dist[e] < d) continue;
			if (dist[e] > d) hi2[e] = 0, dist[e] = d, enq(e, d);
			to2[e][hi2[e]++] = s;
		}
	}
}

void order(int goal)
{
    int i, k, s, e, max;

	q[0] = goal, top = 0, tail = 1;
	mk[goal] = 1;
	sz = 0, max = 0;
	while (top < tail) {
		s = q[top++];
		for (i = 0; i < hi2[s]; i++) {
			e = to2[s][i];
			if (!mk[e]) mk[e] = 1, q[tail++] = e;

			_ord[sz].s = e, _ord[sz++].e = s;
			k = dist[s];
			if (k > max) max = k;
			f[k]++;
		}
	}

	for (i = 1; i <= max; i++) f[i] += f[i-1];
	for (i = sz-1; i >= 0; i--) {
		k = dist[_ord[i].e], ord[--f[k]] = _ord[i];
	}
}

int main()
{
	int R, Q, u, v, _w, a, b, i, j, k;
	unsigned long long mask;
	
	S = in()+1, R = in();
	j = 0, i = R; while (i--) {
		memo[j++] = u = in(), hi[u]++;
		memo[j++] = v = in(), hi[v]++;
		memo[j++] = in();
	}
	for (i = 0; i < S; i++) if (hi[i]) {
		to [i] = malloc(hi[i] << 2), w[i] = malloc(hi[i] << 1);
		to2[i] = malloc(hi[i] << 2);
	}

	memset(hi, 0, S << 2);
	j = 0; while (R--) {
		u = memo[j++], v = memo[j++], _w = memo[j++];
		k = hi[u]++, to[u][k] = v, w[u][k] = _w;
		k = hi[v]++, to[v][k] = u, w[v][k] = _w;
	}
	
	a = in(), b = in();
	dijkstra(a);
	order(b);

	Q = in();
	for (i = 0; i < Q; i++) c[i] = in(), d[i] = in();
	k = 1+((Q-1)>>6); for (i = 0; k--; i += 64) {
        memset(dp, 0, sizeof(dp));
		mask = 1; for (j = 0; j < 64; j++) dp[c[i+j]] |= mask, mask <<= 1;
		for (j = 0; j < sz; j++) dp[ord[j].e] |= dp[ord[j].s];
        mask = 1; for (j = 0; j < 64; j++) {
			if (i+j >= Q) return 0;
            puts(dp[d[i+j]] & mask? "Yes": "No");
			mask <<= 1;
		}
    }
	return 0;
}



Measured memory_bytes: 23876
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <list>
#include <string>
#include <numeric>
#include <math.h>
#include <set>
#include <map>
#include <fstream>
#include <sstream>
#include <iterator>
#include <complex>
using namespace std;

int main()
{
	vector<vector<int>> A(3);
	vector<vector<bool>> p(3);
	for(int i=0; i<3; i++)
	{
		A[i].resize(3);
		p[i].resize(3);
		for(int j=0; j<3; j++)
		{
			p[i][j] = false;
			cin>>A[i][j];
		}
	}

	int N;
	cin>>N;
	vector<int> b(N);
	for(int i=0; i<N; i++)
	{
		cin>>b[i];
		for(int j=0; j<3; j++)
		{
			for(int k=0; k<3; k++)
			{
				if(b[i] == A[j][k])
				{
					p[j][k] = true;
				}
			}
		}
	}

	bool flag = false;

	for(int i=0; i<3; i++)
	{
		if((p[i][0] && p[i][1] && p[i][2]) || (p[0][i] && p[1][i] && p[2][i]))
		{
			flag = true;
		}
	}
	if((p[0][0] && p[1][1] && p[2][2]) || (p[0][2] && p[1][1] && p[2][0]))
	{
		flag = true;
	}
	
	if(flag)
	{
		cout<<"Yes"<<endl;
	}
	else
	{
		cout<<"No"<<endl;
	}

}

Measured memory_bytes: 256
import java.util.*;class Main{public static void main(String[]z){Scanner s=new Scanner(System.in);for(int t=s.nextInt(),i;t-->0;){char[]c=s.next().toCharArray(),d=c.clone();Arrays.sort(c);for(i=0;i<8;i++)d[i]=c[7-i];System.out.println(new Integer(new String(d))-new Integer(new String(c)));}}}

Measured memory_bytes: 12000
#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
//#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <string>

const int MAX_N = 100050;
const int MAX_Q = 100050;
int N, Q;
std::string S;
int l[MAX_Q], r[MAX_Q];
int ac[MAX_N];

int main(int argc, char **argv) {
    std::cin >> N >> Q;
    std::cin >> S;

    for (int i = 1; i <= Q; i++) {
        std::cin >> l[i] >> r[i];
    }

    for (int i = 2; i <= N; i++) {
        ac[i] = ac[i-1];
        if (S[i-1] == 'C' && S[i-2] == 'A') {
            ac[i]++;
        }
    }

    for (int i = 1; i <= Q; i++) {
        std::cout << ac[r[i]] - ac[l[i]] << std::endl;
    }

    return 0;
}


Measured memory_bytes: 2176
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>  
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <list>
#include <cmath>
#include <bitset>
#include <queue>
#include <stack>
#include <sstream>
#include <functional>
#include <cassert>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<char, char> pcc;
const int inf = 0x3f3f3f3f;
const LL lnf = 0x3f3f3f3f3f3f3f3fLL;
const double pi = 3.14159265358979323846;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;

int n, ml, mr;
char str[maxn];

int solve() {
	n = strlen(str);
	int ans = 0;
	for (int i = 0, j = n - 1; i < j;) {
		if (str[i] == str[j]) {
			++i; --j;
		}
		else if (str[i] == 'x') {
			++ans; ++i;
		}
		else if (str[j] == 'x') {
			++ans; --j;
		} 
		else {
			return  -1;
		}
	}
	return ans;
}

int main() {
	int CAS = 0;
	//std::ios::sync_with_stdio(0);  
	//std::cin.tie(0);  
#ifdef NIGHT_13  
	freopen("in.txt", "r", stdin);
	//freopen("out2.txt", "w", stdout);
	int time_night_13 = clock();
#endif // NIGHT_13  

	while (scanf("%s", str) == 1) {
		printf("%d\n", solve());
	}

#ifdef NIGHT_13  
	fprintf(stderr, "\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
	fprintf(stderr, "\t   Time: %d ms", (int)clock() - time_night_13);
	fprintf(stderr, "\n...........................................\n\n");
#endif // NIGHT_13  
	return 0;
}

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

class Way {
public:
  Way();
  void push(int c,int t);
private:
  string mc;
  string mt;
  int mnum;
};

Way::Way():
mnum(0){}

void Way::push(int c,int t)
{
	mc[mnum] = c;
  mt[mnum] = t;
  mnum++;
}

int main()
{
  int n,t,i;
  int min_c = 1001;
  cin>>n>>t;
  int c_in,t_in;
  Way way;

  for(i=0;i<n;i++){
    cin>>c_in>>t_in;
  	way.push(c_in,t_in);
    if(t_in<=t && min_c > c_in){
    	min_c = c_in;
    }
  }

  if(min_c==1001){
    cout<<"TLE"<<endl;
  }else{
  	cout<<min_c<<endl;
  }

}


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

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

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

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

    set<ll> key;
    multiset<ll> roop;
    rep(i,M)
    {
        int tmp, tmp2;
        cin >> tmp >> tmp2;
        key.insert(tmp);
        key.insert(tmp2);
        roop.insert(tmp);
        roop.insert(tmp2);
    }
    
    ll i = 1;
    for( ll i = 1;i <= N; i++ )
    {
        if( !roop.count(i) )  cout << 0 << endl;
        else cout << roop.count(i) << endl;
    }
}


Measured memory_bytes: 256
S = input().strip()
C = {"N":0,"S":0,"E":0,"W":0}
for i in range(len(S)):
    C[S[i]] += 1
if C["N"]>0 and C["S"]>0 and C["E"]>0 and C["W"]>0 or C["N"]==0 and C["S"]==0 and C["E"]>0 and C["W"]>0\
or C["N"]>0 and C["S"]>0 and C["E"]==0 and C["W"]==0 or C["N"]==0 and C["S"]==0 and C["E"]==0 and C["W"]==0:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 62024
<?php

 $n = trim(fgets(STDIN));
 $P = explode(" ", trim(fgets(STDIN)));
 
 $min = $P[0];
 $ans = 1;
 for($i=1;$i<$n;$i++){
   if($P[$i] < $min){
     $ans++;
     $min=$P[$i];
   }
 }
 echo $ans;

Measured memory_bytes: 19060
/* Name        : e
 * Creat       : Sat Jul 13 09:34:01 CST 2019
 * Accept      : Sat Jul 13 10:19:15 CST 2019
 * Author      : landcold7
 * Motto       : Action speaks louder more than words
 */
#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second
#define sz(x) ((int)(x).size())
#ifndef _has_trace
#define trace(...)
#endif
typedef long long ll;

void solve() {
  int n, k;
  cin >> n >> k;
  int mx = (n - 1) * (n - 2) / 2;
  if (k > mx) {
    cout << -1 << "\n";
    return;
  }
  vector<pair<int, int>> ans;
  for (int i = 2; i <= n; ++i) {
    ans.emplace_back(i, 1);
  }
  int extra = mx - k;
  for (int i = 2; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (extra-- > 0) {
        ans.emplace_back(i, j);
      }
    }
  }
  cout << sz(ans) << "\n";
  for (int i = 0; i < sz(ans); ++i) {
    cout << ans[i].x << " " << ans[i].y << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}


Measured memory_bytes: 384
n,k,q=map(int,input().split())
p=list(range(n))
for c in range(q):
    d=int(input())-1
    p[d]=p[d]+1
for c in range(n):
    p[c]=p[c]+k-q-c
    if p[c]>0:
        print('Yes')
    else:
        print('No')

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

using namespace std;
typedef long long ll;

const int N = 100 + 5;
const int M = 1e5 + 5;
const ll mod = 1e9 + 7;
int arr[N], dp[M];

void add(int &a, int b) {
    a += b;
    if (a >= mod)
        a -= mod;
}

void sub(int &a, int b) {
    a -= b;
    if (a < 0)
        a += mod;
}


int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n, k;
    cin >> n >> k;
    dp[0] = 1;
    for (int i = 0; i < n; i++) {
        int curr;
        cin >> curr;
        int segm[k + 1];
        memset(segm, 0, sizeof segm);
        for (int j = 0; j <= k; j++) {
            int to_add = dp[j];
            int l = j + 1;
            int r = j + min(curr, k - j);
            if (l <= r) {
                add(segm[l], to_add);
                if (r + 1 <= k)
                    sub(segm[r + 1], to_add);
            }
        }
        int sum = 0;
        for (int j = 0; j <= k; j++) {
            add(sum, segm[j]);
            add(dp[j], sum);
        }
    }
    cout << dp[k];
    return 0;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
signed main(){
  const Int MAX = 1e6+10;
  vector<Int> p(MAX,0);
  for(Int i=2;i<MAX;i++){
    if(p[i]) continue;
    for(Int j=i+i;j<MAX;j+=i) p[j]=1;
  }
  Int n;
  cin>>n;
  vector<Int> q(n);
  for(Int i=0;i<n;i++) cin>>q[i];

  vector<Int> dp(n+1,0);
  const Int MOD = 1e9+7;
  dp[0]=!p[q[0]];
  for(Int i=0;i<n;i++){
    if(i+1==n) (dp[i+1]+=dp[i])%=MOD;
    if(i+2==n) (dp[i+2]+=dp[i])%=MOD;

    if(i+1<n&&!p[q[i+1]]&&q[i]<q[i+1]) (dp[i+1]+=dp[i])%=MOD;
    if(i+2<n&&!p[q[i+2]]&&q[i]<q[i+2]) (dp[i+2]+=dp[i])%=MOD;
  }

  cout<<dp[n]<<endl;
  return 0;
}



Measured memory_bytes: 12248
a=list(map(int,input().split()))
b,c=a[2]-a[0],a[3]-a[1]
d,e=b+1,c+1
print('R'*b+'U'*c+'L'*b+'D'*c+'D'+'R'*d+'U'*e+'L'+'U'+'L'*d+'D'*e+'R',end='')

Measured memory_bytes: 3060
A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = min(a)+min(b)

for _ in range(M):
  x,y,c = map(int,input().split())
  ans = min(ans, a[x-1]+b[y-1]-c)
  
print(ans)

Measured memory_bytes: 24816
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;

int main() {
  string s;
  cin>>s;
  string ans={};
  rep(i,s.size()){
     if(s[i]=='0') ans+="0";
else if(s[i]=='1') ans+="1";
else if(s[i]=='B'&&ans.size()!=0) ans.erase(ans.size()-1);
  }
  cout<<ans<<endl;    
return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#define eps 1e-7
#define N 1200000
using namespace std;
int a[N],n;
int q[N],b[N];
bool check(int x){
	int tail=0;
	for(int i=1;i<=n;i++){
		int o=a[i];
		if(o>q[tail]){
			q[++tail]=o;
			b[tail]=1;
			continue;
		}
		if(x==1)return 0;
		while(tail>0&&q[tail]>o)tail--;
		while(tail>0&&q[tail]==o&&b[tail]==x)tail--,o--; 
		if(tail==0&&o==0)return 0;
		if(q[tail]==o)b[tail]++;
		else q[++tail]=o,b[tail]=2;
		
	}
	return 1;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	int l=1,r=n;
	check(2);
	while(r-l>3){
		int mid=(l+r)/2;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	check(2);
	for(int i=l;i<=r;i++){
		if(check(i)){
			printf("%d\n",i);
			return 0;
		}
	}
}

Measured memory_bytes: 5120
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int A = sc.nextInt();
		int B = sc.nextInt();

		int max = Math.max(A + B, A - B);
		max = Math.max(max, A * B);

		System.out.println(max);

	}

}

Measured memory_bytes: 23636
n,m=gets.split.map(&:to_i)
root=Array.new(n+1).map{Array.new}
m.times do
  ai,bi=gets.split.map(&:to_i)
  root[ai].push(bi)
  root[bi].push(ai)
end

queue=[1]
ans=[0]*(n+1)
pred=1
count=1
while !queue.empty?
  q=queue.shift
  trans=root[q]
  trans.each do |j|
    if ans[j]==0
      ans[j]=q
      count+=1
      queue.push(j)
    end
  end
end

if count<n
  puts "No"
else
  puts "Yes"
  puts ans[2..-1]
end


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

const long mod = 1e9+7;

long modpow(long a, long b) {
	long res = 1;
	while (b > 0) {
		if (b & 1) res = res * a % mod;
		a = a * a % mod;
		b /= 2;
	}
	return res;
}

long comb(long a, long b) {
	long res = 1;
	for (long i = a; i > a - b; i--) {
		res = res * i % mod;
	}
	for (long i = 1; i <= b; i++) {
		res = (res * modpow(i, mod - 2)) % mod;
	}
	return res;
}

int main() {
	long N, A, B;
	cin >> N >> A >> B;
	long ans = modpow(2, N);
	ans--;
	ans -= comb(N, A);
	ans -= comb(N, B);
	while (ans < 0) ans += mod;
	cout << ans << "\n";
}

Measured memory_bytes: 256
N = int(input())
H = list(map(int, input().split()))
ans = 0
H.append(0)
for i in range(max(H)):
    cnt = 0
    for k in range(N):
        if H[k] != 0 and H[k+1] == 0:
            cnt += 1
    ans += cnt
    H = list(map(lambda x:(x-1, 0)[x == 0], H))
print(ans)

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

static const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  for (int i = 1; i <= n; i++) m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; k++) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;
}


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

int main(){
  int N,Q; cin>>N>>Q;
  string S; cin>>S;
  int C=0;
  vector<int> start(100010,0);
  for(int i=1;i<N;i++){
    if(S.at(i-1)=='A' && S.at(i)=='C'){
      C--;
    }
    start[i]=C;
  }
  C=0;
  vector<int> goal(100010,0);
  for(int i=N-1;i>0;i--){
    if(S.at(i-1)=='A' && S.at(i)=='C'){
      C--;
    }
    goal[i-1]=C;
  }
  
  int l,r;
  for(int i=0;i<Q;i++){
    cin>>l>>r; l--;r--;
    cout<<start[l]+goal[r]-goal[0]<<endl;
  }
}

Measured memory_bytes: 1792
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr ll INF = 1ll << 55;

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

// multi-dimensional vector (require c++14)
template <typename T, typename U>
static inline vector<U> make_nd_vector(T &&n, U &&val) noexcept {
  return vector<U>(forward<T>(n), forward<U>(val));
}
template <typename T, typename... Args>
static inline decltype(auto) make_nd_vector(T &&n, Args &&... args) noexcept {
  return vector<decltype(make_nd_vector(forward<Args>(args)...))>(
      forward<T>(n), make_nd_vector(forward<Args>(args)...));
}
//
// Print from Iterator
template <typename ite>
void print_ite(ite start, ite end, char sp = ' ', char en = '\n') {
  for (auto i = start; i != end; i++)
    cout << *i << sp;
  cout << en;
}
//
inline void Yes(bool condition) { cout << (condition ? "Yes\n" : "No\n"); }

int main() {
  int n;
  cin >> n;
  auto a = vector<vector<ll>>(n, vector<ll>(n));
  auto dist = vector<vector<ll>>(n, vector<ll>(n, INF));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
      dist[i][j] = a[i][j];
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        chmin(dist[j][k], dist[j][i] + dist[i][k]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (a[i][j] > dist[i][j]) {
        cout << -1 << endl;
        return 0;
      }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i][j] < dist[i][j]) {
        ans += a[i][j];
      } else {
        ll sub_d = INF;
        for (int k = 0; k < n; k++) {
          if (k == i || k == j)
            continue;
          chmin(sub_d, dist[i][k] + dist[j][k]);
        }
        if (sub_d != a[i][j])
          ans += a[i][j];
      }
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 1664
// {{{
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// }}}

using namespace std;

using ll [[maybe_unused]] = long long;
using ull [[maybe_unused]] = unsigned long long;
using ld [[maybe_unused]] = long double;

[[maybe_unused]] static constexpr int mod = (int)1e9 + 7;
[[maybe_unused]] static constexpr int inf = 100100100;
[[maybe_unused]] static constexpr ll linf = 1e18;
[[maybe_unused]] static constexpr double eps = 1e-9;
[[maybe_unused]] static constexpr double pi = 3.14159265359;

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, a, b) for (ll i = a; i < b; ++i)
#define rrrep(i, a, b) for (ll i = a; i >= b; --i)
#define all(obj) begin(obj), end(obj)
#define rall(obj) rbegin(obj), rend(obj)
#define pb push_back
#define fst first
#define snd second


int main() {
    // cin.tie(0);
    // ios_base::sync_with_stdio(false);
    ll N, i;
    cin >> N >> i;
    cout << N - i + 1 << endl;
}


Measured memory_bytes: 256
X,K,D = map(int, input().split())
X = abs(X)
if K*D <= X:
    print(X-K*D)
else:
    tempK = X//D
    if (K-tempK)%2==0:
        print(X-D*tempK)
    else:
        print(abs(X-D*tempK-D))


Measured memory_bytes: 9120
#include <iostream>
#include <array>
#include <map>
#include <algorithm>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <random>
#include <queue>
#include <iomanip>
#include <functional>
#include <climits>
#include <tuple>
#include <unordered_map>
#include <limits>
#include <functional>
#include <cassert>
#include <numeric>
#include <bitset>
#include <set>

#define FOR(x,n) for(int x = 0; x < n; x++)
#define INPUT(x) scanf("%d",&x)
#define PUT(x) printf("%d\n",x)
#define DPUT(x) printf("%.15lf\n",x)
#define GET(x,y) std::get<x>(y)
#define CHMAX(x,y) x = std::max(x,y)
#define CHMIN(x,y) x = std::min(x,y)
#define SP_END (i != n - 1 ? ' ' : '\n') ;

using namespace std;

using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vll = std::vector<ll>;
using vpll = std::vector<pll>;
using vpii = std::vector<pii>;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vvpii = std::vector<vpii>;
using vch = std::vector<char>;
using vvch = std::vector<vch>;
using tiii = std::tuple<int, int, int>;

const ll mod197 = 1000000007LL;
const ll INF = 10000000000LL;

const double PI11 = 3.14159265359;

const int dis[5] = { 0,1,0,-1,0 };

const int disD[4] = { 0,1,1,0 };
const int disLD[3] = { 0,1,0 };

const char disC[4] = { 'U','D','R','L' };
const char disY[4] = { -1,1,0,0 };
const char disX[4] = { 0,0,1,-1 };

int main(void) {

	ll n, s = 0,N;
	cin >> n;

	N = n;
	while (n != 0) s += n % 10, n /= 10;

	cout << (N % s == 0 ? "Yes" : "No") << endl;

	return 0;
}


Measured memory_bytes: 256
#include <math.h>
#include <stdio.h>
int main()
{
  int n;
  scanf("%d", &n);
  int x[n], y[n];
  int i, j, lenx, leny;
  double sum = 0;
  for (i = 0; i < n; i++)
    scanf("%d %d", &x[i], &y[i]);
  for (i = 0; i < n; i++)
  {
    for (j = 0; j < n; j++)
    {
      lenx = x[i] - x[j];
      leny = y[i] - y[j];
      sum += sqrt(lenx * lenx + leny * leny);
    }
  }
  printf("%lf", sum / n);
}


Measured memory_bytes: 128
#![allow(unused_imports)]
#![allow(unused_macros)]
use std::cmp::{ min, max };

macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

#[allow(dead_code)]
const M: i64 = 1_000_000_007;
use std::collections::BinaryHeap;

fn add(x: i64,
       head: &mut BinaryHeap<i64>,
       tail: &mut BinaryHeap<i64>,
       ) -> (i64, i64) {
    let mut head_diff = 0;
    let mut tail_diff = 0;
    match (head.peek(), tail.peek()) {
        (None, None) => {
            head.push(x);
            tail.push(-x);
            head_diff += x;
            tail_diff += x;
        },
        (Some(&a), Some(&b)) => {
            if x < a {
                head.push(x);
                head.push(x);
                head_diff += x;
                head_diff += x;
            } else if -b < x {
                tail.push(-x);
                tail.push(-x);
                tail_diff += x;
                tail_diff += x;
            } else {
                head.push(x);
                tail.push(-x);
                head_diff += x;
                tail_diff += x;
            }
        },
        _ => {}
    }
    while head.len() > tail.len() {
        let x = -head.pop().unwrap();
        tail.push(x);
        head_diff += x;
        tail_diff -= x;
    }
    while tail.len() > head.len() {
        let x = -tail.pop().unwrap();
        head.push(x);
        head_diff += x;
        tail_diff -= x;
    }
    (head_diff, tail_diff)
}

fn main() {

    let mut sc = Scanner::new();
    let n: usize = sc.cin();

    let mut head = BinaryHeap::new();
    let mut tail = BinaryHeap::new();
    let mut head_sum = 0;
    let mut tail_sum = 0;
    let mut offset = 0;

    for _ in 0..n {
        let t: usize = sc.cin();
        if t == 1 {
            let a: i64 = sc.cin();
            let b: i64 = sc.cin();
            let (hd, td) = add(a, &mut head, &mut tail);
            head_sum += hd;
            tail_sum += td;
            // trace!(head_sum, &head);
            // trace!(tail_sum, &tail);
            offset += b;
        } else {
            let x = head.peek().unwrap();
            let y = (tail_sum - head_sum) / 2 + offset;
            println!("{} {}", x, y);
        }
    }

}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}


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

const long long int INF = 1e17;

long long int dis[305][305], g[305][305];

int main(void) {
    long long int n, m, l;
    cin >> n >> m >> l;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dis[i][j] = INF;
        }
        dis[i][i] = 0;
    }
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        dis[a][b] = dis[b][a] = c;
    }
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (dis[i][k] + dis[k][j] < dis[i][j]) {
                    dis[i][j] = dis[i][k] + dis[k][j];
                }
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(dis[i][j] <= l) {
                g[i][j] = 1;
            } else {
                g[i][j] = INF;
            }
        }
        g[i][i] = 0;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            for(int k = 1; k <= n; k++) {
                if (g[j][i] + g[i][k] < g[j][k]) {
                    g[j][k] = g[j][i] + g[i][k];
                }
            }
        }
    }
    int q;
    cin >> q;
    for(int i = 1; i <= q; i++) {
        int s, t;
        cin >> s >> t;
        if(g[t][s] < INF) cout << g[t][s] - 1 << "\n";
        else cout << -1 << "\n";
    }
    return 0;
}

Measured memory_bytes: 1920
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(true){
        String s=sc.nextLine();
        if (s.equals("0")) {
            break;
        }
        int Sum=0;

        for(int i=s.length()-1;i>=0;i--){
            char k=s.charAt(i);
            int t=k-'0';
            Sum+=t;

        }
        System.out.println(Sum);

    }
    sc.close();
    }
    
}


Measured memory_bytes: 26908
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl

const ll V = 100000;

void solve(long long N, long long W, std::vector<long long> w,
           std::vector<long long> v) {
  vector<vector<ll>> min_weight(N, vector<ll>(V + 1, LLONG_MAX));
  for (int i = 0; i < N; ++i) {
    min_weight[i][0] = 0;
  }
  min_weight[0][v[0]] = w[0];
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j <= V; j++) {
      if (j - v[i] >= 0 && min_weight[i - 1][j - v[i]] != LLONG_MAX) {
        min_weight[i][j] =
            min(min_weight[i - 1][j], min_weight[i - 1][j - v[i]] + w[i]);
      } else {
        min_weight[i][j] = min_weight[i - 1][j];
      }
    }
  }
  int max_ = 0;
  for (int j = 0; j <= V; j++) {
    if (min_weight[N - 1][j] <= W) {
      max_ = max(max_, j);
    }
  }
  std::cout << max_ << std::endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long W;
  scanf("%lld", &W);
  std::vector<long long> w(N);
  std::vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &w[i]);
    scanf("%lld", &v[i]);
  }
  solve(N, W, std::move(w), std::move(v));
  return 0;
}


Measured memory_bytes: 79488
#pragma warning disable

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Numerics;
using System.Collections;

static class MainClass
{
    public static void Main(string[] args)
    {

        var s = Console.ReadLine();
        var str = "";
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == 'B')
            {
                if (str.Length != 0)
                {
                    str = str.Remove(str.Length - 1, 1);
                }
            }
            else
            {
                str += s[i];
            }
        }
        Console.WriteLine(str);

        Console.ReadLine();
    }




    #region ライブラリ
    public static long ToInt64(this string str) => long.Parse(str);
    public static int ToInt32(this string str) => int.Parse(str);
    public static BigInteger ToBigInteger(this string str) => BigInteger.Parse(str);
    public static List<string> SplittedStringToList(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
    public static List<int> SplittedStringToInt32List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
    public static List<long> SplittedStringToInt64List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => long.Parse(x)).ToList();
    public static List<BigInteger> SplittedStringToBigInteger(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => BigInteger.Parse(x)).ToList();
    public const int INF = int.MaxValue / 2;
    public const long LONGINF = long.MaxValue / 2;
    public const int Mod1e9 = 1000000007;
    

    public static void PrintArray(int[,] array)
    {
        for (int i = 0; i < array.GetLength(0); i++)
        {
            var sb = new StringBuilder();
            for (int j = 0; j < array.GetLength(1); j++)
            {
                sb.Append(array[i, j]).Append(" ");
            }
            Console.WriteLine(sb.ToString());
        }
    }

    #endregion
}



Measured memory_bytes: 13140
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n;
  cin >> n;
  vector<vector<vs>> a(n + 1, vector<vs>(n + 1));
  a[1][1].push_back("a");
  rep2(i, 1, n) {
    rep2(j, 1, i + 1) {
      for(auto v : a[i][j]) {
        rep2(k, 1, j + 2) {
          a[i + 1][max(k, j)].push_back(v + (char)('a' + k - 1));
        }
      }
    }
  }
  vs ans;
  rep2(j, 1, n + 1) {
    for(auto v : a[n][j]) ans.push_back(v);
  }
  sort(all(ans));
  for(auto v : ans) cout << v << endl;
}


Measured memory_bytes: 10740
def main():
    N,M=map(int,input().split())
    if N%2!=0:
        for i in range(1,M+1):
            print(i,N-i+1)
    else:
        for i in range(1,(M+1)//2+1):
            print(i,N-i+1)
        for i in range((M+1)//2+1,M+1):
            print(i,N-i)
if __name__=='__main__':
    main()

Measured memory_bytes: 69324
/*author : Yashvardhan
Saturday, January 12, 2019
*/
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define vpi vector<pi>
#define ff first
#define ss second

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << "  ";
	__f(comma + 1, args...);}
#else
#define debug(...)
#endif

template <typename T> ostream& operator << (ostream& os,const vector<T>& v){for(int i=0;i<v.size();++i) os<<v[i]<<" ";return os;} 
template <typename T> ostream& operator << (ostream& os,const set<T>& v){for(auto it:v) os<<it<<" ";return os;} 
template <typename T, typename S> ostream& operator << (ostream& os, const pair<T,S>& v) {os<<v.ff<<" "<<v.ss;return os;}

const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;

int dp[100005];

int pow(int a, int b, int m)
{
	int ans = 1;
	while (b)
	{
		if (b & 1)
			ans = (ans * a) % m;
		b /= 2;
		a = (a * a) % m;
	}
	return ans;
}

signed main()
{

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef __APPLE__
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
		freopen("error.txt","w",stderr);
	#endif

	time_t t1, t2;
	t1 = clock();
	
	int n;
	cin>>n;

	int hs[n+1];
	for(int i = 1; i <= n ; i++){
		cin>>hs[i];
	}
	dp[1] = 0;
	dp[2] = abs(hs[2]-hs[1]);
	for(int i = 3; i <= n ; i++){
		dp[i] = min((dp[i-1]+abs(hs[i-1]-hs[i])),(dp[i-2]+abs(hs[i-2]-hs[i])));
	}
	cout<<dp[n]<<endl;
	

	t2 = clock();
	cerr<<endl<<t2-t1<<endl;
	
	return 0;
}



Measured memory_bytes: 1792
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import itertools
import bisect
mod = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
num_dict = {}
for i, a in enumerate(A):
    num_dict[i] = a
dic2 = sorted(num_dict.items(), key=lambda x: x[1])
for id_, num_people in dic2:
    id_ += 1
    print(id_, end=' ')


Measured memory_bytes: 35176
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int

struct edge
{
  int to, cost;
};
typedef std::vector<std::vector<edge>> graph;
const ll MOD = 1000000007;
using namespace std;

ll modpow(ll a, ll n, ll mod)
{
  ll res = 1;
  while (n > 0)
  {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod)
{
  return modpow(a, mod - 2, mod);
}

ll modnCr(ll n, ll r, ll mod)
{
  ll ans = 1;
  FOR(i, 1, n + 1)
  {
    ans = (ans * i) % mod;
  }
  ll inv = 1;
  FOR(i, 1, r + 1)
  {
    inv = (inv * i) % mod;
  }
  FOR(i, 1, n - r + 1)
  {
    inv = (inv * i) % mod;
  }
  ans = (ans * modinv(inv, mod)) % mod;
  return ans;
}

int main()
{
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  ll kumi = modnCr(n * m - 2, k - 2, MOD);
  FOR(d, 1, n)
  {
    ll tmp = (d * (n - d)) % MOD;
    tmp = (tmp * m) % MOD;
    tmp = (tmp * m) % MOD;
    ans = (ans + (tmp * kumi) % MOD) % MOD;
  }
  FOR(d, 1, m)
  {
    ll tmp = (d * (m - d)) % MOD;
    tmp = (tmp * n) % MOD;
    tmp = (tmp * n) % MOD;
    ans = (ans + (tmp * kumi) % MOD) % MOD;
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt() % 7;
        switch (k){
            case 0:System.out.println("thu");break;
            case 1:System.out.println("fri");break;
            case 2:System.out.println("sat");break;
            case 3:System.out.println("sun");break;
            case 4:System.out.println("mon");break;
            case 5:System.out.println("tue");break;
            case 6:System.out.println("wed");break;




        }




    }







}


Measured memory_bytes: 26380
#include <iostream>

using namespace std;

int main () {
   int a,b,c,k;
        cin >>a>>b>>c>>k;
        if(k>=a+b+c)
        {
            cout<<a-c;
        }
        else if(k<=a)
        {
            cout<<k;
        }
        else if(k>a and k<=a+b)
            {
                  cout<<a;
        }
        else if (k>a+b and k<a+b+c)
            {
                  cout<<a-(k-(a+b));
        }

    // enter your code here

    return 0;
}


Measured memory_bytes: 3616
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define all(x) x.begin(), x.end()
#define ll long long
#define INF 2e9
using namespace std;

void Main() {
    int n, m;
    cin >> n;
    int t[100], p[100], x[100];

    int t_sum = 0;
    rep(i, 0, n) {
        cin >> t[i];
        t_sum += t[i];
    }

    cin >> m;
    // rep(i,0,m){cin>>p[i]<<x[i];}
    // rep(i, 0, m) { cout << t_sum - t[p[i] - 1] + x[i] << endl; }
    string ans;
    rep(i, 0, m) {
        int p, x;
        cin >> p >> x;
        ans += to_string(t_sum - t[p - 1] + x) + " ";
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15) << endl;
    Main();
    return 0;
}

Measured memory_bytes: 256
"use strict";
function main(input) {
    const N = parseInt(input.replace("\n", ""));
    let flg = false;
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10; j++) {
            if (i * j === N) {
                flg = true;
                break;
            }
        }
        if (flg) break;
    }
    console.log(flg ? "Yes" : "No");
    return;
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 29520
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(b<=1) cout<<0<<endl;
else {
int cnt=1;
int x=a-1;
while(a<b){
    a+=x;
    cnt++;
}
cout<<cnt<<endl;}
return 0;
}

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

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

  int ni = n-1;
  while (ni > 0) {
    cout << a[ni] << " ";
    ni -= 2;
  }
  cout << a[0] << " ";
  if (n % 2 == 1) ni = 1;
  else ni = 2;
  while (ni < n) {
    cout << a[ni] << " ";
    ni += 2;
  }
  return 0;
}

Measured memory_bytes: 3988
# frozen_string_literal: true

# induce sort (internal routine)
def sa_is_induce(s, ls, sum_l, sum_s, lms)
  n = s.size
  sa = [-1]*n

  buf = *sum_s # deep copy
  lms.each{ |lms|
    if lms != n then
      sa[buf[s[lms]]] = lms
      buf[s[lms]] += 1
    end
  }

  buf = *sum_l # deep copy
  sa[buf[s[-1]]] = n-1
  buf[s[-1]] += 1
  sa.each{ |v|
    if v>=1 && !ls[v-1] then
      sa[buf[s[v-1]]] = v-1
      buf[s[v-1]] += 1
    end
  }

  buf = *sum_l # deep copy
  sa.reverse_each{ |v|
    if v>=1 && ls[v-1] then
      buf[s[v-1]+1] -= 1
      sa[buf[s[v-1]+1]] = v-1
    end
  }

  return sa
end

# SA-IS (internal routine)
def sa_is(s, upper)
  n = s.size

  return [] if n == 0
  return [0] if n == 1

  ls = [false]*n
  (n-2).downto(0){ |i|
    ls[i] = s[i]==s[i+1] ? ls[i+1] : s[i]<s[i+1]
  }

  sum_l = [0]*(upper+1)
  sum_s = [0]*(upper+1)
  n.times{ |i|
    if ls[i] then
      sum_l[s[i]+1] += 1
    else
      sum_s[s[i]] += 1
    end
  }
  0.upto(upper){ |i|
    sum_s[i] += sum_l[i]
    sum_l[i+1] += sum_s[i] if i<upper
  }

  lms = (1 ... n).select{ |i| !ls[i-1] && ls[i] }
  m=lms.size
  lms_map = [-1]*(n+1)
  lms.each_with_index{ |lms, id| lms_map[lms] = id }

  sa = sa_is_induce(s, ls, sum_l, sum_s, lms)

  return sa if m == 0

  sorted_lms = sa.select{ |sa| lms_map[sa] != -1 }
  rec_s = [0]*m
  rec_upper = 0
  rec_s[lms_map[sorted_lms[0]]] = 0
  1.upto(m-1){ |i|
    l, r = sorted_lms[i-1, 2]
    end_l = lms[lms_map[l]+1]||n
    end_r = lms[lms_map[r]+1]||n
    same = true
    if end_l-l != end_r-r then
      same = false
    else
      while l < end_l do
        break if s[l] != s[r]
        l+=1
        r+=1
      end
      same = false if l==n || s[l]!=s[r]
    end
    rec_upper += 1 if not same
    rec_s[lms_map[sorted_lms[i]]] = rec_upper
  }

  sa_is(rec_s, rec_upper).each_with_index{ |rec_sa, id|
    sorted_lms[id] = lms[rec_sa]
  }

  return sa_is_induce(s, ls, sum_l, sum_s, sorted_lms)
end

# suffix array for array of integers or string
def suffix_array(s, upper = nil)
  if not upper then
    case s
    when Array
      # compression
      n = s.size
      idx = (0 ... n).sort_by{ |i| s[i] }
      t = [0]*n
      upper = 0
      t[idx[0]] = 0
      1.upto(n-1){ |i|
        upper += 1 if s[idx[i-1]] != s[idx[i]]
        t[idx[i]] = upper
      }
      s = t
    when String
      upper = 255
      s = s.bytes
    end
  else
    s.each{ |s|
      raise ArgumentError if s<0 || upper<s
    }
  end
  return sa_is(s, upper)
end

# frozen_string_literal: true

# lcp array for array of integers or string
def lcp_array(s, sa)
  s = s.bytes if s === String

  n = s.size
  rnk = [0]*n
  sa.each_with_index{ |sa, id|
    rnk[sa] = id
  }

  lcp = [0]*(n-1)
  h = 0
  n.times{ |i|
    h -= 1 if h > 0
    next if rnk[i] == 0
    j = sa[rnk[i]-1]
    h += 1 while j+h<n && i+h<n && s[j+h]==s[i+h]
    lcp[rnk[i]-1] = h
  }
  return lcp
end

s = gets.chomp
lcp = lcp_array(s, suffix_array(s))
ans = s.size*(s.size+1)/2
lcp.each{ |lcp| ans -= lcp }
puts ans

Measured memory_bytes: 63436
#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define put(a) cout<<a<<"\n"
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){if(a>b){a=b;return 1;}return 0;}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){if(a<b){a=b;return 1;}return 0;}

template<typename T>
T upmin(const T& l, const T& r) {//SegmentTreeのupdate関数
    T res;
    res = min(l,r);
    return res;
}
template<typename T>
T upmax(const T& l, const T& r) {//SegmentTreeのupdate関数
    T res;
    res = max(l,r);
    return res;
}

template<typename T>
class SegmentTree {
public:
    using F = function<T(T&, T&)>;
    vector<T> seg;
    int sz = 1;
    T unit;
    F up;
    //T (*up)(const T& l,const T& r);
    SegmentTree(int n, F up, T unit = 0) {
        this->unit = unit;
        //int sz = 1;
        while (sz < n) sz <<= 1;
        seg.resize(sz * 2, unit);
        this->up = up;
    }
    void set(const int& k, const T& x) {//左からk番目の葉にxを代入する
        seg[sz - 1 + k] = x;
    }
    T get(const int& k) {//左からk番目の葉を得る
        return seg[sz - 1 + k];
    }
    void update(int k, const T& v) {// k 番目の値をvに変更
        k += sz - 1;
        seg[k] = v;
        while (k > 0) {
            k = (k - 1) / 2;
            seg[k] = up(seg[2 * k + 1], seg[2 * k + 2]);
        }
    }
    T query(int a, int b, int k, int l,int r) {//[a,b)のupの合成を求める(a,b,0,0,sz)のように使う
        if (r <= a || b <= l) {
            return unit;
        }
        if (a <= l && r <= b) {
            return seg[k];
        }
        else {
            T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return up(vl, vr);
        }
    }
};

int main(){
    int n;
    cin >> n;
    vector<P> p(n);
    REP(i,n){
        cin >> p[i].first;
        p[i].second = i;
    }
    sort(all(p));
    
    SegmentTree<int> sgmin(n,upmin<int>,INT_MAX);
    SegmentTree<int> sgmax(n,upmax<int>);
    ll res = 0;
    int sz = sgmin.sz;
    Rep(i,n){
        int t = p[i].second-sgmax.query(0,p[i].second,0,0,sz)+1;
        int t2 = sgmin.query(p[i].second+1,sz,0,0,sz);
        if(t2==INT_MAX){
            t2 = n-p[i].second;
        }else{
            t2 = t2-p[i].second-1;
        }
        res+=(double)t*t2*(i+1);
        sgmax.update(p[i].second,p[i].second+1);
        sgmin.update(p[i].second,p[i].second+1);
    }
    put(res);
    return 0;
}


Measured memory_bytes: 5888
N, M = map(int, input().split())
k = [0] * M
s = [0] * M
for i in range(M):
  l = [int(j) for j in input().split()]
  k[i], s[i] = l[0], l[1:]
p = [int(i) for i in input().split()]

res = 0
for bit in range(2**N):
  sw = [0] * N
  for i in range(N):
    sw[i] = (bit >> i) & 1
  are_shining = True
  for i in range(M):
    cnt_on = 0
    for j in range(k[i]):
      if sw[s[i][j]-1] == 1:
        cnt_on += 1
    if cnt_on % 2 != p[i]:
      are_shining = False
  if are_shining:
    res += 1

print(res)


Measured memory_bytes: 3064
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iostream>
#include <functional>
#include <bitset>
#include <algorithm>
#include <vector>
#include <forward_list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

ll gcd(ll a, ll b)
{
    ll tmp;
    ll c;
    if (a < b)
    {
        tmp = a;
        a = b;
        b = tmp;
    }

    do
    {
        c = a % b;
        a = b;
        b = c;
    } while (c != 0);

    return a;
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

int main(void)
{
    int n;

    cin >> n;

    vector<int> a(n), g(n), e;

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

    g[0] = a[0];

    e.push_back(0);

    ll ans = 0;

    for (int i = 1; i < n; i++)
    {
        g[i] = gcd(a[i], g[i - 1]);

        if (g[i] != g[i - 1])
        {
            e.push_back(i);
        }
    }

    for (auto x : e)
    {
        ll res;

        if (x == 0)
        {
            res = a[1];
        }
        else
        {
            res = a[0];
        }

        for (int i = 0; i < n; i++)
        {
            if (i == x)
            {
                continue;
            }

            res = gcd(res, a[i]);
        }

        ans = max(ans, res);
    }

    cout << ans << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (int i = 0; i < (int)c; i++)
int main()
{
	int n, k;
	scanf("%d%d", &n, &k);
	vector<int> x(n);
	rep(i, n) scanf("%d", &x[i]);
	int ans = 0;
	rep(i, n) {
		if(k - x[i] <= x[i]) ans += (k - x[i]) * 2;
		else ans += x[i] * 2;
	}
	printf("%d\n", ans);
	return 0;
}

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

using namespace std;
using ll = long long;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define INF 9223372036854775807
#define all(x) (x).begin(), (x).end()

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

  ll n, x = 0, ans = 0;
  string s;
  cin >> n >> s;

  REP(i, n) {
    if (s[i] == 'I') {
      x++;
    } else {
      x--;
    }

    ans = max(ans, x);
  }

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

Measured memory_bytes: 256