akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <sstream>
#include <queue>

using namespace std;
using ll = unsigned long long;
using triplet = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vi>;

int main()
{
#ifdef LOCAL_TEST
    freopen("data.txt", "r", stdin);
#endif

    int X;
    scanf("%d\n", &X);
    
    ll l = 1;
    ll r = X;
    
    while (l < r)
    {
        ll m = l + ((r - l)>>1);
        ll sum_m = (m*(m+1))>>1;
        if (sum_m == X)
        {
            l = m; break;
        }
        else if (sum_m < X)
        {
            l = m+1;
        }
        else //sum_m > X
        {
            r = m;
        }
    }
    printf("%d\n", (int)l);
    return 0;
}

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

#define llong long long 

int N, K;
int det[101];
int score[101][4][3];

int solve(int day, int kind, int d){
	if(score[day][kind][d] != -1) return score[day][kind][d];
	if(d == 3) return 0;

	if(day == N) return score[day][kind][d] = 1;

	int ret = 0;
	if(det[day + 1] != 0){
		if(det[day + 1] == kind) ret = solve(day + 1, det[day + 1], d + 1) % 10000;
		else ret = solve(day + 1, det[day + 1], 1) %  10000;
	}else{
		for(int i = 1; i <= 3; i++){
			if(kind == i) ret += solve(day + 1, i, d + 1) % 10000;
			else ret += solve(day + 1, i, 1) % 10000;
		}
	}

	return score[day][kind][d] = ret % 10000;
}

int main(){
	fill_n(det, 101, 0);
	fill_n(**score, 101 * 4 * 3, -1);
	cin >> N >> K;
	int day;
	for(int i = 0; i < K; i++){
		cin >> day;
		cin >> det[day];
	}

	cout << solve(0, 0, 0) << endl;
	return 0;
}

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

int main() {
	int n;
	cin>>n;
	long long int a[n+1];
	for(int i=0;i<n+1;i++)
		a[i]=1;
		
	a[0] = 0;
	
	for(int i=2;i<n+1;i++) {
		for(int j=i;j<n+1;j+=i) {
			a[j]+=1;
		}
	}
	
	long long int res=0;
	
	for(int i=1;i<n+1;i++) {
		res+= i*a[i];
	}
	
	cout<<res<<endl;
	return 0;
}

Measured memory_bytes: 81720
MOD = 10**9 + 7

N = int(input())
S = input()
ok = (S[0] == S[-1] == 'B')
if ok:
    ans = 1
    cur = 0
    prv = 'W'
    p = 1
    for c in S:
        if prv == c:
            if p & 1:
                ans = ans * cur % MOD
                cur -= 1
            else:
                cur += 1
            p ^= 1
        else:
            if p & 1:
                cur += 1
            else:
                ans = ans * cur % MOD
                cur -= 1
            prv = c
    if cur != 0:
        ok = 0
    for i in range(1, N+1):
        ans = ans * i % MOD
if ok:
    print(ans)
else:
    print(0)

Measured memory_bytes: 3516
import itertools
n = int(input())
lst = [[0,0,0]]

for _ in range(n):
  l = list(map(int, input().split()))
  lst.append(l)
  
hp = [[0] * 3 for _ in range(n+1)]

plst = list(itertools.permutations([_ for _ in range(3)], 2))

for _ in range(0, n):
    for i in plst:
      hp[_+1][i[1]] = max(hp[_][i[0]] + lst[_+1][i[1]], hp[_+1][i[1]])

print(max(hp[n]))
  

Measured memory_bytes: 74712
#include<stdio.h>
int main()
{
    int i,min,max,a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
    {
        max=a;
        min=b;
    }
    else
    {
        max=b;
        min=a;
    }
    for(i=1; i<=min; i++)
        if(i*max>=c)
            break;
    printf("%d",i);
    return 0;

}


Measured memory_bytes: 128
a,b,c = map(int, input().split())
lst = [a, b, c]
s_lst = sorted(lst)

dist = (s_lst[2]-s_lst[1]) + (s_lst[2]-s_lst[0])

if(dist%2==0):
  print(int(dist/2))
else:
  print(int((dist + 3)/2))

Measured memory_bytes: 3060
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,b[100010],ans[100010];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>b[i];
		ans[b[i]]=i;
	}
	for(int i=1;i<=n;i++)
		cout<<ans[i]<<' ';
	return 0;
}

Measured memory_bytes: 1536
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <math.h>
using namespace std;

int main(){
	int N = 0, K = 0, pos = 0, L = 0, R = 0, min = 10000000;
	
	vector<int> A;

	cin >> N >> K;
	A.resize(N);

	for (int i = 0; i < N; ++i){
		cin >> A[i];
		if (A[i] == 1)
			pos = i;
	}
	for (int i = 0; i < K; ++i){
		int c = 0;
		L = pos + i - (K - 1);
		if (L < 0)
			continue;
		R = (N - 1) - (pos + i);
		if (R < 0)
			continue;
		c = (L / (K - 1)) + (R / (K - 1)) + 1;
		if (L % (K - 1) != 0)
			++c;
		if (R % (K - 1) != 0)
			++c;

		if (min > c)
			min = c;
	}	

	cout << min << endl;
	

	return 0;
}

Measured memory_bytes: 640
#include <iostream>

using namespace std;

int main() {
    int room[4][3][10] = {0};
    int n, i, j, k;
    
    cin >> n;
    
    for (i = 0; i < n; i++) {
        int b, f, r, v;
        cin >> b >> f >> r >> v;
        room[b - 1][f - 1][r - 1] += v;
    }
    
    for(i = 0; i < 4; i++) {
        if(i > 0) cout << "####################" << endl;
        for (j = 0; j < 3; j++) {
            for (k = 0; k < 10; k++) {
                cout << " " << room[i][j][k];
            }
            cout << endl;
        }
    }
}

Measured memory_bytes: 1160
#include <bits/stdc++.h> 
using namespace std;
#define rep(i, N) for(int i=0; i<N; i++)
typedef long long ll;
#define dump(x) cerr<<#x<<"="<<x<<endl
using P = pair<int, int>;

int main() {
    int l,r,d; cin >> l >> r>> d;
    int cnt =0;
    for(int i=l; i<=r; i++){
        if(i%d==0) cnt++;
    } 
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 3604
import sys

l, n = map(int, input().split())
x = [int(input()) for _ in range(n)]

x_sum = [0]
for i in range(n):
	x_sum.append(x_sum[-1] + x[i])
ans = x[-1]
for i in range(n-1):
	num = x[i]
	e = (i+n) // 2
	if (n-1-i) % 2 == 1:
		num += l * (n-1-i)
		num += 2 * (x_sum[e] - x_sum[i]) - x[i]
		num -= 2 * (x_sum[n] - x_sum[e]) - x[e]
	else:
		num += l * (n-1-i)
		num += 2 * (x_sum[e] - x_sum[i]) - x[i] + x[e]
		num -= 2 * (x_sum[n] - x_sum[e+1])

	ans = max(ans, num)

x = [l-x[n-i] for i in range(1, n+1)]

x_sum = [0]
for i in range(n):
	x_sum.append(x_sum[-1] + x[i])
for i in range(n-1):
	num = x[i]
	e = (i+n) // 2
	if (n-1-i) % 2 == 1:
		num += l * (n-1-i)
		num += 2 * (x_sum[e] - x_sum[i]) - x[i]
		num -= 2 * (x_sum[n] - x_sum[e]) - x[e]
	else:
		num += l * (n-1-i)
		num += 2 * (x_sum[e] - x_sum[i]) - x[i] + x[e]
		num -= 2 * (x_sum[n] - x_sum[e+1])

	ans = max(ans, num)

print(ans)

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

int main()
{
ll n ;
cin >> n ;
vector<ll> a(n);
rep(i, n){
  cin >> a[i];
}
rep(i,n){
  if(a[i]%2==0&&(a[i]%3!=0&&a[i]%5!=0))ans = "DENIED";
}
cout << ans << endl;
return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <math.h>
#include <cmath>
#include <set>   
#include <bitset>
//#include <boost/math/common_factor_rt.hpp>

#define ll long long
int L = 2000000007;
using namespace std;
ll a[200050] = {};
ll sum[200050] = {};
int main()
{
	ll n;
	ll ans = L;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sum[0] = 0;

	for (int i = 0; i < n; i++)
	{
		sum[i + 1] = a[i] + sum[i];
	}
	ll Max = sum[n];

	for (int i = 1; i < n; i++)
	{
		ll acm = abs(sum[i] - (Max - sum[i]));

		ans = min(ans, acm);
	}
	
	cout << ans << endl;
}

int dsum(ll a) {
	int ans = 0;
	while (a != 0) {
		ans += a % 10;
		a /= 10;
	}
	return ans;
}


Measured memory_bytes: 3328
def main():
    import sys
    input=sys.stdin.buffer.readline
    n=int(input())
    b,M=37,998244353
    x,y=1,0
    f,h=[1],[0]
    f_add,h_add=f.append,h.append
    for c in input():
        x=x*b%M
        f_add(x)
        y=(y*b+c)%M
        h_add(y)
    ok,ng=0,n//2+1
    while ng-ok>1:
        mid=ok+ng>>1
        d={}
        for i in range(n-mid+1):
            k=(h[i+mid]-h[i]*f[mid])%M
            if k in d:
                if d[k]+mid<=i:
                    ok=mid
                    break
            else:
                d[k]=i
        else:
            ng=mid
    print(ok)
main()

Measured memory_bytes: 4156
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<utility>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const ll mod = 1000000007;
const ll INF = mod * mod;

#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define stop char nyaa;cin>>nyaa;

struct edge {
	int to; ll cost;
};
vector<edge> G[300];

ll dist[300];
ll ans[300][300];
void solve() {
	int n, m;ll l; cin >> n >> m >> l;
	rep(i, m) {
		int a, b, c; cin >> a >> b >> c; a--; b--;
		G[a].push_back({ b,c });
		G[b].push_back({ a,c });
	}
	rep(i, n) {
		fill(dist, dist + n, INF);
		priority_queue<LP, vector<LP>, greater<LP>> q;
		q.push({ 0,i }); dist[i] = 0;
		while (!q.empty()) {
			LP p = q.top(); q.pop();
			int id = p.second;
			if (p.first > dist[id])continue;
			rep(j, G[id].size()) {
				if (G[id][j].cost > l)continue;
				ll d = p.first / l;
				ll nd = p.first + G[id][j].cost;
				if (nd > (d + 1)*l) {
					nd = (d + 1)*l + G[id][j].cost;
				}
				int to = G[id][j].to;
				if (nd < dist[to]) {
					dist[to] = nd;
					q.push({ nd,to });
				}
			}
		}
		rep(j, n) {
			if (dist[j] == INF)ans[i][j] = -1;
			else ans[i][j] = (dist[j]-1) / l;
		}
	}
	int q; cin >> q;
	rep(i, q) {
		int s, t; cin >> s >> t; s--; t--;
		cout << ans[s][t] << endl;
	}
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();

	//stop
	return 0;
}


Measured memory_bytes: 3584
/*
氏名 : 杉谷　裕貴,学籍番号 : 190441066
概要 : 足りないカードを出力するプログラム
*/
#include<stdio.h>

int main(void)
{
	int n;
	char suits[52];
	int ranks[52];
	int i, j;

	int cards[4][14] = { 0 };

	scanf("%d ", &n);
	for (i = 1; i <= n; ++i) {
		scanf("%c %d ", &suits[i], &ranks[i]);

		if (suits[i] == 'S') {
			cards[0][ranks[i]] = 1;
		}
		else if (suits[i] == 'H') {
			cards[1][ranks[i]] = 1;
		}
		else if (suits[i] == 'C') {
			cards[2][ranks[i]] = 1;
		}
		else if (suits[i] == 'D') {
			cards[3][ranks[i]] = 1;
		}
	}

	for (i = 0; i < 4; ++i) {
		for (j = 1; j <= 13; ++j) {
			if (cards[i][j] == 0) {
				if (i == 0) printf("S ");
				else if (i == 1) printf("H ");
				else if (i == 2) printf("C ");
				else if (i == 3) printf("D ");
				printf("%d\n", j);
			}
		}
	}

	return 0;
}


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

using namespace std;

int main(){

    int n,a;

    cin>>n>>a;
    string s[n];
    for(int i=0;i<n;i++)cin>>s[i];
    sort(s,s+n);
    for(int i=0;i<n;i++)cout<<s[i];
    cout<<endl;

    return 0;

}

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

#define REP(i, n) for(int i = 0; i < (int) (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9

using namespace std;

int stones[15][5];
int n, score;

void del(){
  REP(i, n){
    int tmp = 0; int count = 0;
    REP(j, 5){
      if(stones[i][j] == tmp) {
        count++;
        if(j == 4 and count >= 3 and tmp > 0) { REP(k, count) stones[i][j-k] = 0; score+= tmp * count; }
      }
      else{
        if(count >= 3 and tmp > 0) { REP(k, count) stones[i][j-1-k] = 0; score += tmp * count; }
        count = 1;
      }
      tmp = stones[i][j];
    }
  }
  REP(k, n) REP(j, 5) REP(i, n){
    if(stones[n-1-i][j] == 0 and n-2-i >= 0) swap(stones[n-1-i][j], stones[n-2-i][j]);
  }
}

int main(){
  while(cin >> n and n){
    memset(stones, -1, sizeof(stones));
    score = 0;
    REP(i, n) REP(j, 5) cin >> stones[i][j];
    REP(k, n) del();
    cout << score << endl;
  }
  return 0;
}

Measured memory_bytes: 3092
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,d1,x;
long double X,D,XX,DD,ans,bns,k; 
long double A=4,B=2;
int main(){
	scanf("%d%d%d",&n,&d1,&x);
	D=2*d1+1*x;
	X=x*4;
	DD=D+X*(n-1);
	XX=-X;
	for(int i=1;i<=n;i++){
		long double o=i;
		if(o!=1){
			bns+=(long double)1.0/o;
			k+=bns;
			ans+=((k-bns)/A*X+bns/A*D);
			ans+=((k-bns)/A*XX+bns/A*DD);
		}
		ans+=(d1+(2*i-2)*x)/2.0;
		ans+=(d1+(2*i-1)*x)/2.0;
	}
	printf("%.12Lf",ans);
} 

Measured memory_bytes: 256
def resolve():
    s=str(input())
    val=len(s)
    cntg=0
    cntp=0
    w=0
    l=0
    for i in range(len(s)):
        if cntg-cntp<=0:
            cntg+=1
            if s[i]=='p':
                l+=1
        else:
            if s[i]=='g':
                cntp+=1
                w+=1
            else:
                cntp+=1
    print(w-l)



resolve()

Measured memory_bytes: 40560
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL);
inline void IO() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
}

int main(int argc, char const *argv[])
{
	//IO();
	FAST_IO
	
	ll n; cin >> n;
	string t;
	while(n > 0LL) {
		n -= 1;
		t += (char)('a' + (n%26LL));
		n /= 26LL;
	}
	reverse(t.begin(),t.end());
	cout << t;
	return 0;
}


Measured memory_bytes: 3632
w, h, n = map(int, input().split())
x_reg = 0
y_reg = 0

for i in range(n):
    x, y, a = map(int, input().split())
    if a == 1 and x_reg < x:
        x_reg = x
    if a == 2 and w > x:
        w = x
    if a == 3 and y_reg < y:
        y_reg = y
    if a == 4 and h > y:
        h = y

x_edge = max(0, w-x_reg)
y_edge = max(0, h-y_reg)

print(x_edge*y_edge)

Measured memory_bytes: 3064
#if 0
2020.03.21

考虑一个暴力的贪心做法，从大到小枚举 S = x + y + z ，
然后对于所有 (x, y, z) ，如果之前没有与之连边的点被选，那么这个点一定要出现在最优解中。
可以给边定向，边从小连到大，那么整个图就是 DAG ，在 DAG 中抽象这个贪心：
每次选择一个点满足其到的点都没有被选，然后删掉选择它加入最优解集合。
那么一个点被选当且仅当其到的点都没被选，一个点没被选当且仅当其到的点存在被选的点（或没有出度）。
这可以对应到博弈中的必胜必败态，这个 DAG 可以看做博弈转移图，答案就是求所有必败态的权值和。

然而点数很大。
考虑对于一个 (x, y, z) ，如何快速求出其必胜必败态，
每次移动只能移动一维，这个博弈三维独立，就相当于每个维度有一个子游戏。
对于每一维求出 SG 值 f[x], g[y], h[z] ，三者异或和就是这个的 SG 值。
考虑计数，FWT 做异或卷积即可（事实上也可以三维暴力枚举 SG 值，SG 值的上界是 O(sqrt(M)) 的）。
#endif
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
} read;

const int maxn = 200005, mod = 998244353;
std::vector<int> G[maxn];
int sg[3][maxn];
ll f[3][maxn], g[maxn];
ll po[maxn * 3];

void FWT (ll *a, int n) {
	for (int m = 1; m < n; m <<= 1)
		for (int i = 0; i < n; i += m << 1)
			for (int k = i; k < i + m; k ++) {
			ll x = a[k], y = a[k + m];
			(a[k] = x + y) %= mod;
			(a[k + m] = x + mod - y) %= mod;
		}
}

void iFWT (ll *a, int n) {
	FWT(a, n);
	ll inv = mod - (mod - 1) / n;
	for (int i = 0; i < n; i ++)
		(a[i] *= inv) %= mod;
}

int main () {
	int n = read, len = 1;
	while (len < n)
		len <<= 1;

	po[0] = 1;
	ll bs = 1000000000000000000ll % mod;
	for (int i = 1; i <= n * 3; i ++)
		po[i] = po[i - 1] * bs % mod;

	for (int o = 0; o < 3; o ++) {
		int m = read;
		for (int i = 1; i <= n; i ++)
			G[i].clear();
		for (int i = 1; i <= m; i ++) {
			int u = read, v = read;
			if (u < v) G[u].push_back(v);
			if (v < u) G[v].push_back(u);
		}
		for (int i = n; i; i --) {
			std::set<int> mex;
			for (int j : G[i])
				mex.insert(sg[o][j]);
			while (mex.count(sg[o][i]))
				++ sg[o][i];
		}
		for (int i = 1; i <= n; i ++)
			(f[o][sg[o][i]] += po[i]) %= mod;
		FWT(f[o], len);
	}

	for (int i = 0; i < len; i ++)
		g[i] = f[0][i] * f[1][i] % mod * f[2][i] % mod;
	iFWT(g, len);

	printf("%lld\n", g[0]);
}


Measured memory_bytes: 20864
#include<iostream>
#include<algorithm>
using namespace std;
int a[5];
int main() {
	cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4];
	sort(a, a + 5);
	cout << a[4] << ' ' << a[3] << ' ' << a[2] << ' ' << a[1] << ' ' << a[0] << endl;
}

Measured memory_bytes: 1176
package main

import (
	"fmt"
	"sort"
)

func main() {
	var N int
	fmt.Scan(&N)
	A := make([]int, N)
	total := int(0)

	for i := range A {
		var a int
		fmt.Scan(&a)
		A[i] = a - int(i) - int(1)
		total += A[i]
	}
	sort.Ints(A)

	abe1 := A[N/2]
	abe2 := abe1 + 1
	abe3 := abe1 - 1

	r1 := int(0)
	r2 := r1
	r3 := r1
	for _, i := range A {
		r1 += abs(i - abe1)
		r2 += abs(i - abe2)
		r3 += abs(i - abe3)
	}

	fmt.Println(min(r3, min(r1, r2)))

}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}


Measured memory_bytes: 5504
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

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

const ll INF=1e18;

int main(){
    ll coins[12]={1,6,6*6,6*6*6,6*6*6*6,6*6*6*6*6,6*6*6*6*6*6,9,9*9,9*9*9,9*9*9*9,9*9*9*9*9};
    vector<ll> dp(100001,INF);
    dp[0]=0;
    for(ll i=1;i<=100000;i++){
        rep(j,12){
            if(i-coins[j]>=0){
                dp[i]=min(dp[i],dp[i-coins[j]]+1);
            }
        }
    }
    ll n;
    cin >> n;
    cout << dp[n] << endl;
} 


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define ppb pop_back
using namespace std;
int dp[3005][3005];
string ans;
int main()
{
	ios::sync_with_stdio(false);
	string s,t;
	cin>>s>>t;
	int n=s.size(),m=t.size();
	//for (int i=0;i<3005;i++) dp[i][0]=dp[0][i]=0;
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++)
		{
			if(s[i-1]==t[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
		}
	}
	int i=n,j=m;
	while(i>0 && j>0)
	{
		if (s[i-1]==t[j-1])
		{
			ans+=s[i-1];
			i--;
			j--;
		} else
		if (dp[i][j]==dp[i-1][j]) i--; else j--;
	}
	reverse(ans.begin(),ans.end());
	cout<<ans;
	return 0;
}

Measured memory_bytes: 35456
#include <bits/stdc++.h>
//#include <iostream>
//#include <string>
//#include <algorithm>
//#include <stdio>
using namespace std;
#define fr(i, r, n) for(int i = r; i < n; ++i)
const int maxn = (int)1e3 +10;

int main()
{
   int n;
   cin>>n;
   cout<<3*n*n<<endl;


    return 0;
}



Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
#include<stdio.h>
#include <cstdint>
#include <stdlib.h> 
#include <time.h>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) REP(i,1,n+1)
#define all(a) a.begin(), a.end()
#define fi first
#define se second
#define RNG(x, a, n) &((x)[a]), &((x)[n])
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> PII;
typedef vector<ll> Vl;
typedef vector<int> VI;
typedef tuple<int, int, int> TT;
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define dmt(x, y, z) make_tuple(x, y, z)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
const int MAX = 2000000;
const int inf = 1000000007;
const ll mod = 1000000007;
const ll longinf = 1LL << 60;
const long double PI = (acos(-1));
const long double EPS = 0.0000000001;

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



int n, ans;
int a[2][100005];

int main() {

	cin >> n;

	rep(i, 2) {
		rrep(j, n) {
			int tmp;
			cin >> tmp;
			a[i][j] = a[i][j - 1] + tmp;
		}
	}

	rrep(i, n) {
		ans = max(ans, a[0][i] + (a[1][n] - a[1][i - 1]));
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
#define int long
#define ll long long
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) x.size()
#define fi first
#define se second
const int INF = 1e9;
using namespace std;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

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

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision(20);

    while(true){
        int N; cin >> N;
        if(N==0) break;
        int sum=0;
        int A[N];
        rep(i,N){
            cin >> A[i];
            sum+=A[i];
        }
        sort(A,A+N);
        sum-=A[0];
        sum-=A[N-1];
        cout << sum/(N-2) << endl;
    }
}


Measured memory_bytes: 3140
#include <cassert>

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#include <boost/foreach.hpp>
//#include <boost/range/algorithm.hpp>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1e9
#define LINF 1e18
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define MP make_pair
#define MOD 1000000007LL
#define int long long
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
struct edge{int to,cost;};
typedef pair<int,int> P;

bool isupper(char c){if('A'<=c&&c<='Z')return 1;return 0;}
bool islower(char c){if('a'<=c&&c<='z')return 1;return 0;}
bool isPrime(int x){if(x==1)return 0;if(x==2)return 1;if(x%2==0)return 0;for(int i=3;i*i<=x;i++)if(x%i==0)return 0;return 1;}
bool iskaibun(string s){for(int i=0;i<s.size()/2;i++)if(s[i]!=s[s.size()-i-1])return 0;return 1;}
bool isnumber(char c){return ('0'<=c&&c<='9');}
bool isalpha(char c){return (isupper(c)&&islower(c));}

signed main (){
    int n;cin>>n;
    vector<int> v(n);
    rep(i,0,n)cin>>v[i];
    Sort(v);
    if(v[n-1]<accumulate(v.begin(),v.end()-1,0))cout<<"Yes"<<endl;
    else cout<<"No"<<endl;

}
/*
1 2 0 6 7
*/


Measured memory_bytes: 256
import bisect
s = input()
t = input()
n = len(s)
m = len(t)
indices = [[] for _ in range(26)]
for i in range(n):
    indices[ord(s[i]) - ord('a')].append(i)
for i in range(n):
    indices[ord(s[i]) - ord('a')].append(i + n)

ans = 0
p = 0
for i in range(m):
    c = ord(t[i]) - ord('a')
    if len(indices[c]) == 0:
        print(-1)
        exit()
    p = indices[c][bisect.bisect_left(indices[c], p)] + 1
    if p >= n:
        p -= n
        ans += n
ans += p
print(ans)

Measured memory_bytes: 11892
#include<iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
#define rep(i, n) for (int i=0; i<(n); i++)
using namespace std;

int main(){
    int K, N; cin >> N >> K;
    double p[N], tot=0, ans;
    rep(i, N) cin >> p[i];
    rep(i, K){
        tot += (p[i]+1)/2;
    }
    ans = tot;;
    for(int i=1; i<N-K+1; i++){
        tot += (p[i+K-1]+1)/2 - (p[i-1]+1)/2;
        if(ans < tot) ans = tot;
    }
    //rep(i, N-K+1) cout << sum[i] << " ";
    //cout << endl;
    cout << setprecision(10) << ans << endl;
}

Measured memory_bytes: 1792
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long n = N;
        int total = 0;
        while (n > 0) {
            total += n % 10;
            n = n /10;
        }
        int k = 0;
        long a = 0;
        while (N  > 0) {
            a = N;
            N = N /10;
            k++;
        }
        System.out.println(Math.max((a-1 + 9*(k - 1)), total));
    }

}


Measured memory_bytes: 20948
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};


int main() {
    ll W,H,x,y;
    cin >>W>>H>>x>>y;
    
    cout << fixed << setprecision(15) << double(W*H)/2.0 << ' ';
    if(x*2==W && y*2==H){
        cout<<1<<endl;
    }else{
        cout<<0<<endl;
    }


    return 0;
    

}

Measured memory_bytes: 3744
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)
#define REV(X, Y, Z) for (ll(X) = (Y); (X) > (Z); (X)--)

using namespace std;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMax = 1e12 + 1;
constexpr ll kMod = 1e9 + 7;
constexpr long double kPI = 3.14159265358979323846;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

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

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  ll A, B;
  cin >> A >> B;
  cout << A * B << endl;

  return 0;
}


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

using namespace std;

int main() {
	int a, b;
	scanf("%d%d", &a, &b);
	
	if (a <= 5) printf("0\n");
	else if (a <= 12) printf("%d\n", b / 2);
	else printf("%d\n", b);
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
int main(){
    int x,i,z,max;
    cin >> x;
    max = 1;
    for(i=2;i<32;i++){
        z = i*i;
        while(z<=x){
            if(max < z) max = z;
            z *= i;
        }
    }
    cout << max << endl;
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>

int main()
{
  long long l, r;
  scanf("%lld %lld", &l, &r);
  
  int ans = 2018;
  for (long long i = l; i < r && ans; ++i)
  {
    for (long long j = i + 1; j <= r && ans; ++j)
    {
      if (ans > ((i * j) % 2019)) ans = (i * j) % 2019;
    }
  }
  printf("%d\n", ans);
  return 0;
}

Measured memory_bytes: 128
import java.util.*;

public class Main {
    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int K = sc.nextInt();
    int[]A = new int[N];
    
        int cou = 0;
        for(int i=0; i<N; i++){
            A[i] = sc.nextInt();
        if(A[i]>=K){
            cou++;
         }
}
        System.out.print(cou);
}
}

Measured memory_bytes: 49428
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;

int main() {
    int n, p;
    cin >> n >> p;
    vi a(n);
    int cou = 0;
    ll ans = 1;
    rep(i, n) {
        cin >> a[i];
        ans <<= 1;
        if(a[i] & 1) {
            cou++;
        }
    }
    if(cou) ans >>= 1;
    

    if(1 == p and cou == 0) {
        ans = 0;
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
use std::collections::*;
use std::io::*;
use std::*;
macro_rules! get {
    ($($t:tt),*; $n:expr) => {
        {
            let stdin = std::io::stdin();
            let ret = std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
                let line = line.unwrap();
                let mut it = line.split_whitespace();
                _get!(it; $($t),*)
            }).collect::<Vec<_>>();
            ret
        }
    };
    ($($t:tt),*) => {
        {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut it = line.split_whitespace();
            _get!(it; $($t),*)
        }
    };
}
macro_rules! _get {
    // iterator(splitted input-string) => iterator(output-type)
    ($it:ident; [char]) => {
        _get!($it; String).chars().collect::<Vec<_>>()
    };
    ($it:ident; usize1) => {
        $it.next().unwrap().parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
    };
    ($it:ident; [usize1]) => {
        $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
    };
    ($it:ident; [$t:ty]) => {
        $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
    };
    ($it:ident; $t:ty) => {
        $it.next().unwrap().parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
    };
    ($it:ident; $($t:ty),+) => {
        ($(_get!($it; $t)),*)
    };
}
fn modpow(a: i64, p: i64, m: i64) -> i64 {
    if p == 0 {
        return 1;
    }
    if p % 2 == 0 {
        let d = modpow(a, p / 2, m);
        return d * d % m;
    } else {
        return a * modpow(a, p - 1, m) % m;
    }
}

fn main() {
    let n = get!(usize);
    if n == 1 {
        let x = get!(i32);
        if x == 1 {
            println!("{}", 0);
        } else {
            println!("{}", 1);
        }
        return;
    }
    let mut x = get!([char]);
    let mut dp = vec![[0; 2]; n as usize];
    dp[0][0] = if x[0] == '1' { 1 } else { 0 };
    dp[0][1] = if x[0] == '1' { 1 } else { 0 };
    let pops = x.iter().filter(|c| c == &&'1').count() as i64;
    for i in 1..n {
        if pops - 1 >= 1 {
            dp[i][0] = dp[i - 1][0] * 2 + if x[i] == '1' { 1 } else { 0 };
            dp[i][0] %= pops - 1;
        }
        dp[i][1] = dp[i - 1][1] * 2 + if x[i] == '1' { 1 } else { 0 };
        dp[i][1] %= pops + 1;
    }
    for i in 0..n {
        let pop = if x[i] == '1' { 0 } else { 1 };
        let popcount: i64 = pops + if x[i] == '1' { -1 } else { 1 };
        if popcount == 0 {
            println!("{}", 0);
            continue;
        }
        let mut left = dp[n - 1][pop] as i64
            + (if pop == 0 { -1 } else { 1 }) * modpow(2, (n - i - 1) as i64, popcount as i64);
        left = (left + popcount) % popcount;
        let mut count = 1;
        while left != 0 {
            left = left % left.count_ones() as i64;
            count += 1;
        }
        println!("{}", count);
    }
}


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

int main(){
    int n;
    cin>>n;
    vector<int>a(n);
    rep(i,n){
        cin>>a[i];
        a[i]--;
    }
    int ans=0;
    rep(i,n){
        if(a[a[i]]==i)ans++;
    }
    cout<<ans/2<<endl;
}

Measured memory_bytes: 640
using System;
using System.Linq;

class Procon {
  public Procon () { }
  static void Main (string[] agrs) {
    new Procon ().Do ();
  }

  Scanner cin;

  public void Do () {
    cin = new Scanner ();
    int A = cin.nextInt ();
    int B = cin.nextInt ();

    if ((B % A) == 0) {
      Console.WriteLine (A + B);
      return;
    }

    Console.WriteLine (B - A);
    return;
  }
}

class Scanner {
  string[] s;
  int i;
  int max_i;

  public Scanner () {
    s = new string[0];
    max_i = 0;
    i = 0;
  }

  public string nextString () {
    if (i >= s.Length) {
      s = Console.ReadLine ().Split (' ');
      max_i = s.Length;
      i = 0;
      if (max_i == 0) {
        return "";
      }
      return s[i++];
    } else {
      return s[i++];
    }
  }

  public int nextInt () {
    return int.Parse (nextString ());
  }

  public long nextLong () {
    return long.Parse (nextString ());
  }

  public double nextDouble () {
    return double.Parse (nextString ());
  }

  public string[] arrayString () {
    return Console.ReadLine ().Split (' ');
  }

  public int[] arrayInt () {
    string[] str = Console.ReadLine ().Split (' ');
    int[] array = new int[str.Length];
    for (int i = 0; i < str.Length; i++) {
      array[i] = int.Parse (str[i]);
    }
    return array;
  }

  public long[] arrayLong () {
    string[] str = Console.ReadLine ().Split (' ');
    long[] array = new long[str.Length];
    for (int i = 0; i < str.Length; i++) {
      array[i] = long.Parse (str[i]);
    }
    return array;
  }

  public double[] arrayDouble () {
    string[] str = Console.ReadLine ().Split (' ');
    double[] array = new double[str.Length];
    for (int i = 0; i < str.Length; i++) {
      array[i] = double.Parse (str[i]);
    }
    return array;
  }
}

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

void fnInput(map<char, int>& rmpChars)
{
  string sStr;
  cin >> sStr;

  for (char ch = 'a'; ch <= 'z'; ch++)
    rmpChars[ch] = 0;

  for (int i = 0; i < sStr.size(); i++)
    rmpChars[ sStr[i] ]++;
}

void fnCharCheck(const map<char, int>& cnrmpChars)
{
  for (pair<const char, int> PChars : cnrmpChars)
    if (!PChars.second)
    {
      cout << PChars.first << endl;
      return;
    }

    cout << "None" << endl;
}

int main()
{
  map<char, int> mpChars;

  fnInput(mpChars);
  fnCharCheck(mpChars);

  return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;typedef long long ll;typedef string str;
typedef vector<str> vst;
typedef vector<ll> vll;
int main(){
  ll n;
  cin>>n;
  vst s(n);
  vll march(5);
  for (ll i=0;i<n;i++) {
    cin>>s[i];
    if (s[i][0]=='M') march[0]++;
    if (s[i][0]=='A') march[1]++;
    if (s[i][0]=='R') march[2]++;
    if (s[i][0]=='C') march[3]++;
    if (s[i][0]=='H') march[4]++;
  }
  
  ll ans=0;
  for (ll i=0;i<3;i++) {
    for (ll j=i+1;j<4;j++) {
      for (ll k=j+1;k<5;k++) {
        ans+=march[i]*march[j]*march[k];
      }
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 6144
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

void solve()
{
    ll n, m;
    cin >> n >> m;

    int x, ans = n;

    for (ll i = 0; i < m; i++)
    {
        cin >> x;
        ans -= x;
    }

    if (ans < 0)
        cout << -1 << "\n";

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

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);

    //int t;
    //cin >> t;

    // while (t--)
    solve();

    return 0;
}

Measured memory_bytes: 3616
#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>
#include <numeric>
#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);

int main(void)
{
    int n, t;

    cin >> n >> t;

    set<ll> s;

    for (int i = 0; i < n; i++)
    {
        int c, time;
        cin >> c >> time;

        if (t >= time)
        {
            s.insert(c);
        }
    }

    if (s.size())
    {
        cout << *s.begin() << endl;
    }
    else
    {
        cout << "TLE" << endl;
    }
}


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

int main() {
  int n,m,x,y;cin>>n>>m>>x>>y;
  vector<int>a(n),b(m);
  rep(i,n)cin>>a[i];
  rep(i,m)cin>>b[i];
  sort(all(a));
  sort(all(b));
  if(x<b[0]&&a[n-1]<y&&a[n-1]<b[0])cout<<"No War"<<endl;
  else cout<<"War"<<endl;
}

Measured memory_bytes: 3624
#include<stdio.h>
int main(void)
{
  int a,b,c,x,y;
  
  scanf("%d",&a);
  scanf("%d",&b);
  scanf("%d",&c);
  
  x=a-b;
  y=c-x;
  
  if(y>0)
  {
    printf("%d",y);
  }
  else{
    printf("0\n");
  }
  
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  
  vector<int> a(N, 0);
  for(int i=0; i<N; i++)
    cin >> a.at(i);
  
  int now = 1;
  int min;
  
  for(int i=0; i<N; i++) {
    now = a.at(now - 1);
    if (now == 2) {
      min = i+1;
      break;
    }
  }
  
  if (now == 2)
    cout << min << endl;
  else
    cout << -1 << endl;
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <string>
using namespace std;

int main(){
    int n,a,b;
    cin >> n >> a >> b;
	long long sum = 0;
    for ( int i = 0 ; i <= n ; i++){
        int temp = i , dos = 0;
        while(temp>0){
            dos += temp%10;
            temp /= 10;
        }
        if ( dos >= a && dos <= b) sum += i;
    }
    cout << sum << endl;
}

Measured memory_bytes: 256
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.TreeMap;
import java.math.BigDecimal;
import java.math.BigInteger;
    
public class Main{
 
static final InputStream in=System.in;
static final PrintWriter out=new PrintWriter(System.out);
static final int INF=Integer.MAX_VALUE;
static final long LINF=Long.MAX_VALUE;
 
public static void main(String[] args) throws IOException{
InputReader ir=new InputReader(in);
out.println(compare(ir.nextInt(),ir.nextInt()));
out.flush();
}

public static String compare(int a,int b){
 return a>b?"a > b":a<b?"a < b":"a == b";
}

static class InputReader {
 private InputStream in;
 private byte[] buffer=new byte[1024];
 private int curbuf;
 private int lenbuf;
 
 public InputReader(InputStream in) {this.in=in; this.curbuf=this.lenbuf=0;}
  
 public boolean hasNextByte() {
  if(curbuf>=lenbuf){
   curbuf= 0;
   try{
    lenbuf=in.read(buffer);
   }catch(IOException e) {
    throw new InputMismatchException();
   }
   if(lenbuf<=0) return false;
  }
  return true;
 }
 
 private int readByte(){if(hasNextByte()) return buffer[curbuf++]; else return -1;}

 private boolean isSpaceChar(int c){return !(c>=33&&c<=126);}
 
 private void skip(){while(hasNextByte()&&isSpaceChar(buffer[curbuf])) curbuf++;}

 public boolean hasNext(){skip(); return hasNextByte();}
 
 public String next(){
  if(!hasNext()) throw new NoSuchElementException();
  StringBuilder sb=new StringBuilder();
  int b=readByte();
  while(!isSpaceChar(b)){
   sb.appendCodePoint(b);
   b=readByte();
  }
  return sb.toString();
 }
 
 public int nextInt() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  int res=0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }
 
 public long nextLong() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  long res = 0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }
 
 public double nextDouble(){return Double.parseDouble(next());}
 
 public BigInteger nextBigInteger(){return new BigInteger(next());}
 
 public int[] nextIntArray(int n){
  int[] a=new int[n];
  for(int i=0;i<n;i++) a[i]=nextInt();
  return a;
 }
 
 public char[][] nextCharMap(int n,int m){
  char[][] map=new char[n][m];
  for(int i=0;i<n;i++) map[i]=next().toCharArray();
  return map;
 }
}
}

Measured memory_bytes: 24420
t=[int(input()) for _ in range(5)]
ans=0
b=[x%10 for x in t]
c=[x%10 for x in t if x%10>0]
if c==[]:
    print(sum(t))
else:
    for i in range(5):
        if b[i]>0:
            ans+=(t[i]//10+1)*10
        else:
            ans+=t[i]
    print(ans-10+min(c))

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int n,k;cin >> n >> k;
vector<int>a(n);
for(int i = 0;i<n;i++) cin >> a[i];
sort(a.begin(),a.end());
int sum = 0;
for(int i = 0;i<k;i++) sum += a[i];
cout << sum;
return 0;
}

Measured memory_bytes: 3632
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<functional>
#include<math.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pf(n) printf("%d\n",n)
#define pff(a,b) printf("%d  %d\n",a,b);
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1001001001;
const double PI = acos(-1);

int main() {
	int n;
	cin >> n;
	map<string, int> mp;
	ll ans = 0;
	rep(i, n) {
		string s;
		cin >> s;
		sort(s.begin(), s.end());
		mp[s]++;
	}
	for (auto& p : mp) {
		int a = p.second;
		ans += (ll)a*(a - 1) / 2;
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 11404
n,x = map(int, input().split())

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

d = [0]*(n+1)
c = 1
for i in range(1,n+1):
    d[i] = d[i-1] + l[i-1]
    if d[i] <= x:
      c += 1
    else:
      break
      
    
print(c)

Measured memory_bytes: 2940
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<sstream>
#include<functional>
#include<map>
#include<set>
#include<cmath>

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

using namespace std;

int a[1000010];

bool p(int n){
int m;
for(int i=2;i<=n;i++){
if(n%i==0){ m=i; break;
	}
}
if(m==n) return true;
else return false;
}

int main(){

int n;
while(1){

cin >> n;

if(cin.fail()) break;

int f=floor(sqrt(1.0*n));

for(int i=2;i<=n;i++) a[i]=1;


for(int i=2;i<=f;i++){
if(a[i]!=0){
if(p(i)){
	for(int j=2*i;j<=n;j+=i)
	a[j]=0;
}
}
}

int x=0;
for(int i=2;i<=n;i++){
	if(a[i]!=0) x++;
}

	cout << x << endl;

}

	return 0;
}

Measured memory_bytes: 5064
#include<stdio.h>
int main()
{	
	char s[4];
	scanf("%s",s);
	if(s[0]=='S'&&s[2]=='N') printf("7\n");
	if(s[0]=='M') printf("6\n");
	if(s[0]=='T'&&s[2]=='E') printf("5\n");
	if(s[0]=='W') printf("4\n");
	if(s[0]=='T'&&s[2]=='U') printf("3\n");
	if(s[0]=='F') printf("2\n");
	if(s[0]=='S'&&s[2]=='T') printf("1\n");
	return 0;
}


Measured memory_bytes: 1596
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
#define MN 200005
int N,a[MN];
long long ans,sum,XOR;
int main(){
	N=read();register int i,l,r;
	for(i=1;i<=N;i++) a[i]=read();
	for(l=1,r=0;l<=N;l++){
		while(r<N&&(sum+a[r+1])==(XOR^a[r+1])) sum+=a[r+1],XOR^=a[r+1],r++;
		ans+=r-l+1;sum-=a[l],XOR^=a[l];
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 1024
import Foundation

extension String {
    
    func toStrList() -> [String] {
        return self.map{String($0)}
    }
    
    func toIntList() -> [Int] {
        return self.map{Int(String($0))!}
    }
    
    func toSepDoubleList() -> [Double]{
        return self.split(separator: " ").map{Double($0)!}
    }

    func toSepIntList() -> [Int] {
        return self.split(separator: " ").map{Int($0)!}
    }

    func toSepStrList() -> [String] {
        return self.split(separator: " ").map{String($0)}
    }
    
    func zFill(n:Int) -> String {
        return String(format: "%0\(n)d", Int(self)!)
    }
    
    func count(c:Character) -> Int{
        var result = 0

        for e in self{
            if e == c{
                result += 1
            }
        }
        return result
    }

    func counter() -> Dictionary<Character, Int>{
        var result:[Character:Int] = [:]
        
        for c in self{
            guard let _ = result.index(forKey: c) else {
                result[c] = 1
                continue
            }
            result[c]! += 1
        }
        return result
    }
}

extension Int {
    func toBin() -> String {
        return String(self, radix: 2)
    }
    
    func toNBin(_ radix:Int) -> String{
        return String(self, radix: radix)
    }
    
    func toD() -> Double {
        return Double(self)
    }
}

extension Double {
    func toI() -> Int {
        return Int(self)
    }
}

extension Array where Element: Equatable{
    func count(n:Self.Element) -> Int{
        var result = 0
        for e in self{
            if e == n{
                result += 1
            }
        }
        return result
    }
}

func pow(_ base:Int,_ exp:Int) -> Int{
    let d = pow(base.toD(), exp.toD())
    return Int(d)
}

func lotateMatrix(theta:Double) -> [[Double]] {
    return [
        [cos(theta), -sin(theta)],
        [sin(theta), cos(theta)]
    ]
}

func lotate(matrix:[[Double]], point:[Double]) -> [Double] {
    var x = point[0]
    var y = point[1]
    
    let rx = matrix[0][0]*x + matrix[0][1]*y
    let ry = matrix[1][0]*x + matrix[1][1]*y
    
    return [rx, ry]
}


func abc168c() -> Double {
    let ABHM = readLine()!.toSepDoubleList()
    let Mprob = ABHM[3] / 60
    let Hprob = (ABHM[2] / 12.0) + ((1/12) * Mprob)
    
    let Hlot = lotateMatrix(theta: 2.0*Double.pi*Hprob)
    let Mlot = lotateMatrix(theta: 2.0*Double.pi*Mprob)
    
    let Apoint = lotate(matrix: Hlot, point: [0, ABHM[0]])
    let Bpoint = lotate(matrix: Mlot, point: [0, ABHM[1]])
    
    let p = CGPoint(x: Apoint[0]-Bpoint[0], y: Apoint[1] - Bpoint[1])
    
    return Double(sqrt(p.x * p.x + p.y * p.y))
}


print(abc168c())



Measured memory_bytes: 13840