akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<iomanip>
#include<queue>
#include<stack>
#include<time.h>
#define rep(i,n)for(int i=0;i<n;i++)
#define int long long
#define ggr getchar();getchar();return 0;
#define prique priority_queue
constexpr auto mod = 1000000007;
#define inf 1e15
#define key 1e9
using namespace std;
typedef pair<int, int>P;
void yes() { cout << "Yay!" << endl; }
void no() { cout << ":(" << endl; }
template<class T> inline void chmax(T& a, T b) {
	a = std::max(a, b);
}
template<class T> inline void chmin(T& a, T b) {
	a = std::min(a, b);
}

//combination(Nが小さい時はこれを使う)
const int MAX = 330000;
int fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}
int COMB(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

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

//繰り返し二乗法（Nが大きい時の場合のcombination)
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= (i % mod); res %= mod;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

//debug処理
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//UnionFind
class UnionFind {
protected:
	int* par, * rank, * size;
public:
	UnionFind(unsigned int size) {
		par = new int[size];
		rank = new int[size];
		this->size = new int[size];
		rep(i, size) {
			par[i] = i;
			rank[i] = 0;
			this->size[i] = 1;
		}
	}
	int find(int n) {
		if (par[n] == n)return n;
		return par[n] = find(par[n]);
	}
	void unite(int n, int m) {
		n = find(n);
		m = find(m);
		if (n == m)return;
		if (rank[n] < rank[m]) {
			par[n] = m;
			size[m] += size[n];
		}
		else {
			par[m] = n;
			size[n] += size[m];
			if (rank[n] == rank[m])rank[n]++;
		}
	}
	bool same(int n, int m) {
		return find(n) == find(m);
	}
	int getsize(int n) {
		return size[find(n)];
	}
};
signed main() {
	string s;
	cin >> s;
	if (s[2] == s[3] && s[4] == s[5])puts("Yes");
	else puts("No");
	ggr
}


Measured memory_bytes: 256
def main():
    n, m = map(int, input().split())
    
    ans = []
    check = []
    check_once = []
    false_flg = 0
    for i in range(n):
        ans.append('0')
        check.append(0)
        check_once.append(0)

    for j in range(m):
        s, c = input().split()

        if int(s) == 1 and int(c) == 0 and n != 1:
            false_flg = 1

        if check[int(s)-1] == 0:
            ans[int(s)-1] = c
            check[int(s)-1] = 1

        elif check[int(s)-1] == 1 and ans[int(s)-1] == c:
            ans[int(s)-1] = c

        else:
            false_flg = 1
    
    tmp = int("".join(ans))
    
    if tmp // 10**(n-1) > 0 and false_flg != 1:
        print(tmp)

    elif false_flg != 1:
        if tmp == 0 and n == 1:
            print(0)
        else:
            tmp_ = 10**(n-1) + int(tmp)
            print(tmp_)
    else:
        print(-1)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)

int const MOD=1000000007;
int kaijo[100005];
int keisu[100005];
int rest=1000000007;
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
int dp[2010][2010];
signed main(){
  kaijo[0]=1;keisu[0]=1;keisu[1]=1;
  rep1(i,100000){
    kaijo[i]=kaijo[i-1]*i;
    keisu[i+1]=keisu[i]+modinv(i+1,rest);
    kaijo[i]%=rest;
    keisu[i+1]%=rest;
  }
  rep(i,2010){
    rep(j,2010){
      dp[i][j]=0;
    }
  }
  int n;cin>>n;
  vector<int> a(n);
  rep(i,n)cin>>a[i];
  int res=0;
  vector<int> t(n,0);
  vector<int> sum(n,0);
  rep(i,n-1){
    t[i]=a[i+1]-a[i];
  }
  for(int i=n-2;i>=0;i--){
    sum[i]=sum[i+1]+t[i];
  }

  
  rep(i,n-1){
    res+=t[i]*keisu[i+1];
    res%=rest;
  }
  res*=kaijo[n-1];
  res%=rest;
  cout<<res<<endl;
  return 0;
}


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

ll N;

int main(void){
    
    cin >> N;
    vector<ll> A(N, 0);
    for(ll i = 1; i <= N; ++i){
        cin >> A[i - 1];
        A[i - 1] -= i;
    }
    sort(A.begin(), A.end());
    
    ll ans = LONG_MAX, C = N / 2;
    for(ll c = C - 1; c < C + 1; ++c){
        ll sum = 0;
        for(ll i = 0; i < N; ++i) sum += abs(A[i] - A[c]);
        ans = min(ans, sum);
    }
    
    cout << ans << endl;
    
    return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define endl "\n"
#define speed() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(x) (int)(x).size()
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lli;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
    speed();
    int a, b; cin >> a >> b;
    string s; cin >> s;
    rep(i, 0, a) {
        if (!('0' <= s[i] && s[i] <= '9')) {
            cout << "No";
            exit(0);
        }
    }
    if (s[a] != '-') {
        cout << "No";
        exit(0);
    }
    rep(i, a+1, a+b+1) {
        if (!('0' <= s[i] && s[i] <= '9')) {
            cout << "No";
            exit(0);
        }
    }
    cout << "Yes";
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>

int main(void)
{
  int n, k, r, s, p;
  scanf("%d%d%d%d%d", &n, &k, &r, &s, &p);
  char t[100001];
  scanf("%s", t);
  int point[256];
  point['r'] = p;
  point['s'] = r;
  point['p'] = s;
  int ans = 0;

  for (int i = 0; i < strlen(t); i++) {
    if (i < k || t[i] != t[i - k])
      ans += point[t[i]];
    else
      t[i] = ' ';
  }
  printf("%d\n", ans);
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
    int i,j, n, d, x, a[100], s;

    scanf("%d%d%d", &n,&d,&x);
    for (i=0; i<n; i++) scanf("%d", &a[i]);

    s = 0;

    for (i=0; i<n; i++) {
        for (j=1; j<=d; j+=a[i]) s++;
    }

    printf("%d\n", s+x);

    return 0;
}

Measured memory_bytes: 128
lis=list(map(int,input().split()))
A=lis[0]
B=lis[1]
C=lis[2]
K=lis[3]
ans=0
if(A>=K):
    ans=K
else:
    if(A+B>=K):
        ans=A
    else:
        c=K-A-B
        ans=A-c
print(ans)


Measured memory_bytes: 9180
#include <iostream>
#include <string>

int main()
{
  int N;
  std::cin >> N;
  double ans = 0;
  while(N--)
  {
    std::string x, u;
    std::cin >> x >> u;
    if (u == "JPY")
      ans += std::stoll(x);
    else
      ans += 380000.0 * std::stod(x);
  }
  std::cout << ans << std::endl;
  return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <cmath>
#include <numeric>
using namespace std;

int A[200000];
int main(){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        cin >> A[i];
    }
    int max_A = -1;
    int max_A_ind = -1;
    int second_max_A = -1;
    for(int i = 0; i < N; i++){
        if(max_A < A[i]){
            second_max_A = max_A;
            max_A = A[i];
            max_A_ind = i;
        } else {
            second_max_A = max(second_max_A, A[i]);
        }
    }
    for(int i = 0; i < N; i++){
        if(A[i] != max_A){
            cout << max_A << endl;
        } else {
            cout << second_max_A << endl;
        }
    }
    return 0;
}


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

s={}
for i in range(n):
  if a[i] not in s:
    s[a[i]]=1
  else:
    s[a[i]]+=1
ans=0
for i in s:
  x=s.get(i-1, 0)+s[i]+s.get(i+1, 0)
  if x>ans:
    ans=x
print(ans)

Measured memory_bytes: 14180
import sys
readline = sys.stdin.readline
A,B = list(map(int, readline().split()))

def factorization(n):
    arr = [[1,1]]
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def gdc(x,y):
    if y == 0:
        return x
    return gdc(y, x % y)

g = gdc(B,A)
res = factorization(g)
# print(g,res)
print(len(res))

Measured memory_bytes: 3064
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int maxn=1e5+100;
int n;
set<int> s1,s2,s3;
int a[maxn],b[maxn],c[maxn];
long long b1[maxn],b2[maxn];
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        for (int i=1;i<=n;i++) scanf("%d",&b[i]);
        for (int i=1;i<=n;i++) scanf("%d",&c[i]);
 
        sort(a+1, a+n+1); sort(b+1, b+n+1); 
        sort(c+1, c+n+1);
        int i,j;
        i=j=1;
        while (i<=n && j<=n)
        {
            if (b[j]>a[i]) i++;
            else b1[j++]=i-1;
        }
        while (j<=n) b1[j++]=n;
 
        // for (int i=1;i<=n;i++)
        //     printf("%d ", b1[i]);
        // printf("\n");
 
        i=j=1;
        while (i<=n && j<=n)
        {
            if (b[j]>=c[i]) i++;
            else b2[j++]=i-1;
        }
        while (j<=n) b2[j++]=n;
 
        // for (int i=1;i<=n;i++)
        //     printf("%d ", b2[i]);
        // printf("\n");
 
        long long sum=0;
        for (int i=1;i<=n;i++)
            sum+=b1[i]*(n-b2[i]);
        printf("%lld\n", sum);
    }
    return 0;
}

Measured memory_bytes: 3200
// -*- mode:c++; coding:utf-8; indent-tabs-mode:nil; -*-
// ALDS1_4-D: Search - Allocation

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
//#include <numeric>
//#include <iostream>
using namespace std;

typedef unsigned int uint;

static pair<uint, uint> sum_max(const vector<uint>& ws) {
  auto ws_sum = 0u;
  auto max_w = 0u;
  for (auto w : ws) {
    ws_sum += w;
    max_w = max(max_w, w);
  }
  return make_pair(ws_sum, max_w);
}

static bool is_loaded(uint max_load, uint num, const vector<uint>& ws) {
  const auto last = ws.end();
  auto it = ws.begin();
  for (auto i = 0u; i < num; ++i) {
    auto capacity = max_load;
    while (it < last && *it <= capacity) {
      capacity -= *it++;
    }
    if (it == last) return true;
  }
  return false;
}

static uint min_load_capacity(uint num, const vector<uint>& ws) {
  const auto sum_max_ws = sum_max(ws);
  const auto ws_sum = sum_max_ws.first; // accumulate(ws.begin(), ws.end(), 0);
  const auto max_w = sum_max_ws.second; // *max_element(ws.begin(), ws.end());

  auto left = max(max_w, (ws_sum-1)/num+1);
  auto right = ws_sum;
  while (left < right) {
    const auto middle = (left+right)/2;
    if (is_loaded(middle, num, ws)) {
      right = middle;
    } else {
      left = middle+1;
    }
  }
  return right; // right == left;
}

int main(void) {
  uint n, k;
  //cin >> n >> k;
  scanf("%d %d", &n, &k);
  
  vector<uint> ws(n);
  for (auto i = 0u; i < n; ++i) {
    //cin >> ws[i];
    scanf("%d", &ws[i]);
  }

  auto c = min_load_capacity(k, ws);
  //cout << c << endl;
  printf("%d\n", c);

  return 0;
}
// end

Measured memory_bytes: 2788
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep1(i, n) for(int i = 1; i < n+1; i++)
#define sort(A) sort(A.begin(),A.end())
#define reverse(A) reverse(A.begin(),A.end());
#define vecmin(A) *min_element(A.begin(),A.end());
#define vecmax(A) *max_element(A.begin(),A.end());
typedef long long ll;

int main(){
  int X,A,B;
  cin >> X >> A >> B;
  if(B>A+X){
    cout << "dangerous" << endl;
  }else if(B<=A){
    cout << "delicious" << endl;
  }else{
    cout << "safe" << endl;
  }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
//NOT TO SUBMIT IN WRONG PLACE OR BY WRONG COMPILER
//ABC 166, task A, 2020/05/03
//ms,
/*
*/

int main(void){
  string S;
  cin >>S;

  if(S == "ABC") cout <<"ARC";
  else cout <<"ABC";
  return 0;
}
/*
*/


Measured memory_bytes: 3628
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1<<30;
const ll MOD = 1000000007;

int gcd(int a, int b) {
    return b ? gcd(b, a%b) : a;
}

int main(void) {

    int k; cin >> k;
    ll sum = 0;
    for(int i = 1; i <= k; i++) {
        for(int j = 1; j <= k; j++) {
            for(int l = 1; l <= k; l++) {
                sum += gcd(i, gcd(j, l));
            }
        }
    }

    cout << sum << endl;


    return 0;
}

Measured memory_bytes: 3636
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 26 12:13:54 2019

@author: hiros
"""

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

ans = B[0]
i = 1

while (i < N-1):
    if (B[i-1] <= B[i]):
        ans += B[i-1]
    else:
        ans += B[i]
    i += 1
    #print(ans,i)

ans += B[-1]

print(ans)

Measured memory_bytes: 38384
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		int[] X=new int[N];
		int[] Y=new int[N];
		for(int i=0;i<N;i++){
			int k=sc.nextInt();
			X[i]=k;
			Y[i]=k;
		}
		Arrays.sort(Y);
		int m=Y[N/2-1],k=Y[N/2];
		for(int i=0;i<N;i++){
			if(X[i]<=m){
				System.out.println(k);
			}else{
				System.out.println(m);
			}
		}
	}
}


Measured memory_bytes: 92440
#include<bits/stdc++.h>
using namespace std;
int nums[100002];
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>nums[i];
    vector<int> su(n);
    vector<int> back(n);
    su[0]=nums[0];
    back[n-1]=nums[n-1];
    for(int i = 1;i<n;i++){
        su[i] = gcd(su[i-1],nums[i]);
    }
    for(int i = n-2;i>=1;i--){
        back[i] = gcd(back[i+1],nums[i]);
    }
    int res = 1;
    for(int i=1;i<n-1;i++){
        res = max(res,gcd(su[i-1],back[i+1]));
    }
    res = max(res,su[n-2]);
    res = max(res,back[1]);
    cout<<res<<endl;
    return 0;
}

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

int main(){
	string s; cin >> s;
	bool aa = 0;
	for(int i = 0; i < s.size(); ++i){
		if(s[i] != 'x') aa = 1;
	}
	if(!aa){
		cout << 0 << endl;
		return 0;
	}
	ll po[26] = {};
	for(int i = 0; i < s.size(); ++i) ++po[s[i] - 'a'];
	int yo = 0;
	for(int i = 0; i < 26; ++i){
		if(po[i] % 2 && i != 'x' - 'a'){
			++yo;
		}
	}
	ll koko;
	if(yo >= 2){
		cout << -1 << endl;
		return 0;
	}else{
		ll sum = 0;
		for(int i = 0; i < 26; ++i) sum += po[i];
		sum -= po['x' - 'a'];
		ll yoyo = 0;
		sum += sum % 2;
		for(int i = 0; yoyo < sum / 2; ++i){
			if(s[i] != 'x'){
				koko = i;
				++yoyo;
			}
		}
	}
	string sub;
	string subr;
	int i;
	for(i = 0; sub.size() <= koko; ++i){
		sub += s[i];
	}
	--i;
	if(!yo){
		++i;
		while(s[i] == 'x') ++i;
	}
	for(; i < s.size(); ++i){
		subr += s[i];
	}
	reverse(subr.begin(), subr.end());
	ll ss = 0, ssr = 0;
	string sss, sssr;
	vector<int> vs, vr;
	for(int i = 0; i < sub.size(); ++i){	
		if(sub[i] != 'x'){
			vs.push_back(i - ss - 1);
			ss = i;
			sss += sub[i];
		}
	}
	for(int i = 0; i < subr.size(); ++i){	
		if(subr[i] != 'x'){
			vr.push_back(i - ssr - 1);
			ssr = i;
			sssr += subr[i];
		}
	}
	if(sss != sssr){
		cout << -1 << endl;
	}else{
		ll ans = 0;
		for(int i = 0; i < vs.size(); ++i) ans += abs(vs[i] - vr[i]);
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 1152
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int out=0,fh=1;
	char jp=getchar();
	while ((jp>'9'||jp<'0')&&jp!='-')
		jp=getchar();
	if (jp=='-')
		fh=-1,jp=getchar();
	while (jp>='0'&&jp<='9')
		out=out*10+jp-'0',jp=getchar();
	return out*fh;
}
const int MAXN=2e5+10;
int n,a[MAXN];
int t[MAXN],m=0;
int main()
{
	n=read();
	for(int i=1;i<=n;++i)
		a[i]=read();
	for(int i=n;i>=1;--i)
	{
		int x=a[i];
		for(int j=i*2;j<=n;j+=i)
			x^=t[j];
		t[i]=x;
		if(x)
			++m;
	}
	cout<<m<<endl;
	for(int i=1;i<=n;++i)
		if(t[i])
			printf("%d ",i);
	return 0;
}


Measured memory_bytes: 2432
from collections import Counter
def main():
    n,x,y=map(int,input().split())
    x,y=x-1,y-1
    ans=[]
    for i in range(n):
        dp = [n]*n
        dp[i] = 0
        calcstep(i, dp)
        dp[y] = min(dp[y], dp[x]+1)
        dp[x] = min(dp[x], dp[y]+1)
        calcstep(x, dp)
        calcstep(y, dp)
        #print(i, dp)
        ans += dp
    ans = Counter(ans)
    for i in range(1,n):
        print(ans[i]//2)

def calcstep(i, dp):
    for j in range(i, len(dp)-1):
        if dp[j+1] > dp[j]+1:
            dp[j+1] = dp[j]+1
        else:
            break
    for j in range(1,i+1)[::-1]:
        if dp[j-1] > dp[j]+1:
            dp[j-1] = dp[j]+1
        else:
            break

if __name__ == "__main__":
    main()

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

int dp[1000][1000];

void printmap(int size){
  for(int i = 0; i < size; i++){
    for(int j = 0; j < size; j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
}

int main(void){
  int size, max;
  char c;
  while(cin >> size, size != 0){
    max = 0;
    for(int i = 0; i < size; i++) for(int j = 0; j < size; j++) dp[i][j] = 1;
    
    for(int i = 0; i < size; i++) for(int j = 0; j < size; j++){
      cin >> c;
      if(c == '*') dp[i][j] = 0;
    }
    for(int i = 1; i < size; i++) for(int j = 1; j < size; j++){
      if(dp[i][j] == 0) continue;
      dp[i][j] = min(dp[i-1][j], min(dp[i-1][j-1], dp[i][j-1])) + 1;
      if(max < dp[i][j]) max = dp[i][j];
    }
    cout << max << endl;
  }
  return 0;
}

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

int main(){

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

Measured memory_bytes: 3592
#include<bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define SZ(x) ((int)(x).size())
#define debug(x) cerr << #x << ": " << x << '\n'
#define INF 999999999
typedef long long int Int;
typedef pair<int,int> P;
using ll = long long;
using VI = vector<int>;

int main(){
    int n,m;cin >> n >> m;
    vector<Int> x(m,0);
    for(auto &i:x) cin >> i;
    sort(x.begin(),x.end());
    vector<Int> a(m-1,0);
    rep(i,m-1){
        a[i] = abs(x[i]-x[i+1]);
    }
    
    sort(a.begin(),a.end());
    
    for(Int i=0;i<n-1;i++) if(a.size()>0){a.pop_back();}
    Int ans = 0;
    for(auto &i:a) ans+=i;
    cout << ans << endl;
}

Measured memory_bytes: 1792
//(・ω・)
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, atoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip> //setprecision
#include <limits>
#include <sstream> //istringstream
#include <stdlib.h>
#include <list>
#include <iterator>//std::advance()
using namespace std;
const double pi=3.141592653589793238;
int main(){
    int x;
    cin >> x;
    if(x==1){
        cout << 0 << endl;
    }
    else{
        cout << 1 << endl;
    }
}



Measured memory_bytes: 3632
import math
import numpy as np

H, W = map(int, input().split())

result = 10**10

def vertical_and_horizontal_cal(h, w):
    global result
    # - | このパターンを検証中
    for i in range(1, math.ceil(h / 2) + 1):
        area = np.array([i * w, (h-i) * (w//2), (h-i) * (w - w//2)])
        result = min(result, max(area) - min(area))

def vertical_and_vertical_cal(h, w):
    global result
    # - - このパターンを検証中
    for i in range(1, math.ceil(h / 2) + 1):
            area = np.array([i * w,  ((h-i)//2) * w, (h - i - (h-i)//2) * w])
            result = min(result, max(area) - min(area))

vertical_and_horizontal_cal(H, W)
vertical_and_horizontal_cal(W, H)
vertical_and_vertical_cal(H, W)
vertical_and_vertical_cal(W, H)

print(result)


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

int _=[](){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    return 0;
}();

int main()
{
    int n;
    cin>>n;
    vector<vector<int>> v;
    
    for(int i=0;i<n;i++)
    {
        int x,y,z;
        vector<int> dum;
        cin>>x>>y>>z;
        dum.push_back(x);
        dum.push_back(y);
        dum.push_back(z);
        v.push_back(dum);
    }
    
    vector<int> a(n,0),b(n,0),c(n,0);
    a[0]=v[0][0];
    b[0]=v[0][1];
    c[0]=v[0][2];
    
    for(int i=1;i<n;i++)
    {
        a[i] = v[i][0] + max(b[i-1],c[i-1]);
        b[i] = v[i][1] + max(a[i-1], c[i-1]);
        c[i] = v[i][2]+ max(a[i-1],b[i-1]);
    }
    
    cout<<max(a[n-1],max(b[n-1],c[n-1]))<<endl;
    
}

Measured memory_bytes: 7600
x1, y1, x2, y2 = map(int,input().split())
x3 = x2 - (y2-y1)
y3 = y2 + (x2-x1)
x4 = x3 - (y3-y2)
y4 = y3 + (x3-x2)
print(x3, y3, x4, y4)

Measured memory_bytes: 2940
#include<iostream>
using namespace std;
typedef pair<int,char> P;
bool isNum(char ch){return ('0'<=ch&&ch<='9');}
bool isAl(char ch){return ('A'<=ch&&ch<='Z');}
string s,t;
int n,p,len;

void solve(){
  while(1){
    if(p>=len||s[p]==')')break;
    int a=0;
    while(isNum(s[p]))a=a*10+s[p]-'0',p++;
    //cout<<a<<endl;
    a=max(a,1);
    if(s[p]=='('){
      int l=t.size();
      p++;solve();p++;
      int r=t.size();
      for(int i=0;i+1<a&&(int)t.size()<=n;i++)
	for(int j=l;j<r&&(int)t.size()<=n;j++)
	  t.push_back(t[j]);
    }else{
      string tmp="";
      while(isAl(s[p]))tmp.push_back(s[p]),p++;
      for(int i=0;i<a&&(int)t.size()<=n;i++)
	t+=tmp;
    }
  }
}

int main(){
  while(1){
    cin>>s>>n;
    if(s=="0"&&n==0)break;
    len=s.size();
    p=0;
    t="";
    solve();
    if(n<(int)t.size())cout<<t[n]<<endl;
    else cout<<'0'<<endl;
  }
  return 0;
}

Measured memory_bytes: 2112
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
typedef long long ll;

int main() {
	int x; cin >> x;
	int ans = 1;
	for (int i = 2; i <= x; i++) {
		int b = i;
		int a;
		for (int j = 2; pow(b,j) <= x; j++) {
			a = pow(b, j);
		}
		ans = max(a, ans);
	}
	cout << ans << endl;
}


Measured memory_bytes: 3960
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define mod 1000000007
#define endl '\n'
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);


int main()
{
   string s;
   cin>>s;
   ll flag=0;
   ll n=s.length(),i;
   for(i=0;i<n-1;i++)
   {
       if((s[i]=='A' && s[i+1]=='B') || (s[i]=='B' && s[i+1]=='A') )
       {
           flag=1;
       }
   }
   if(flag)
   {
       cout<<"Yes"<<endl;
   }
   else
   {
       cout<<"No"<<endl;
   }
}


Measured memory_bytes: 256
namespace ABC134Prac.C
{
    using System.Collections.Generic;
    using System.Linq;
    using static System.Console;

    public class Program
    {

        public static void Main(string[] args)
        {
            var n = ReadInputAsInt();
            var A = ReadLinesAsInt(n).ToList();


            foreach (var max_i in Solve(n, A))
            {
                WriteLine(max_i);
            }
        }

        public static IEnumerable<int> Solve(int n, List<int> a)
        {

            var max = a.Max();

            if (a.Count(x => x == max) > 1)
            {
                foreach (var x in Enumerable.Repeat(max, n))
                {
                    yield return x;
                }
                yield break;
            }

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

                if (a[i] != max)
                {
                    yield return max;
                    continue;
                }
                var _a = new List<int>(a);
                _a.RemoveAt(i);
                yield return _a.Max();
            }
        }

        static int ReadInputAsInt() => int.Parse(ReadLine());

        static IEnumerable<int> ReadLinesAsInt(int n)
        {
            for (int i = 0; i < n; i++)
            {
                yield return int.Parse(ReadLine());
            }
        }
    }
}

Measured memory_bytes: 20436
/* ex 7_1
   Tarara */

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

typedef struct point
{
    int x_elem;
    int y_elem;
} point;

int scan_point(point *po)
{
    return scanf("%d %d", &(po->x_elem), &(po->y_elem));
}

point shift_point(point po, point vec)
{
    po.x_elem += vec.x_elem;
    po.y_elem += vec.y_elem;
    return po;
}

int main(void)
{
    point p1, p2, p3, p4, vec;
    int temp;

    // 入力
    scan_point(&p1);
    scan_point(&p2);

    // P1P2ベクトルを計算し，vecへ代入する
    vec.x_elem = p2.x_elem - p1.x_elem;
    vec.y_elem = p2.y_elem - p1.y_elem;

    // 考察をすると，次の点へ移動するにはベクトルの要素を入れ替えて，
    // x方向の要素にだけマイナスを掛ければ良い．
    temp = vec.x_elem;
    vec.x_elem = -vec.y_elem;
    vec.y_elem = temp;
    p3 = shift_point(p2, vec);

    temp = vec.x_elem;
    vec.x_elem = -vec.y_elem;
    vec.y_elem = temp;
    p4 = shift_point(p3, vec);

    // 出力
    printf("%d %d %d %d\n", p3.x_elem, p3.y_elem, p4.x_elem, p4.y_elem);

    return 0;
}

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

int main(){
  int n,m,i,j;
  cin >> n;
  vector<int> d(n);
  for(i=0;i<n;i++){
    cin >> d.at(i);
  }
  cin >> m;
  vector<int> t(m);
  for(i=0;i<m;i++){
    cin >> t.at(i);
  }
  sort(d.begin(),d.end());
  sort(t.begin(),t.end());
  j=0;
  for(i=0;i<n;i++){
    if(j<m&&t.at(j)==d.at(i)){
      j++;
    }
  }
  
  if(j==m){
    cout << "YES" << endl;
  }else{
    cout << "NO" << endl;
  }
}

Measured memory_bytes: 4756
#include<bits/stdc++.h>
using namespace std;
int N;
char s[1010100];
int arr[1010100];
vector<int> res;
int main()
{
    scanf("%s", s);
    N = strlen(s);

    int cnt = 0;
    if(s[0] == '<') cnt = 1;
    else cnt = -1;
    for(int i=1; i<N; ++i)
    {
        if(s[i] != s[i-1])
        {
            res.push_back(cnt);
            cnt = 0;
        }
        if(s[i] == '<') ++cnt;
        else --cnt;
    }
    res.push_back(cnt);

    long long ans = 0;
    for(int i=0; i<(int)res.size(); ++i)
    {
        //printf("%d\n", res[i]);
        long long N = abs(res[i]);
        ans += N*(N-1)/2;
        if(i != 0 && res[i-1] > 0 && res[i] <0)
        {
            ans += max(res[i-1], abs(res[i]));
        }
    }
    if(res.front()<0) ans += abs(res.front());
    if(res.back()>0) ans += res.back();
    printf("%lld\n", ans);
}

Measured memory_bytes: 3832
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()

    ans = INF
    for i in range(len(S) - 3 + 1):
        ans = min(ans, abs(int(S[i : i + 3]) - 753))

    print(ans)
    return


if __name__ == '__main__':
    main()


Measured memory_bytes: 9200
s = input()
ll = len(s)
l = ll // 2

ans = True
t = l
if len(s) % 2 != 0:
    t = l - 1

for i in range(0, t):
    if s[i] != s[t-i]:
        ans = False

t = l
if len(s) % 2 != 0:
    t = l + 1

ll -= 1
c = 0
for i in range(t, ll):
    if s[i] != s[ll-c]:
        ans = False
    c += 1

if s[:t-1] != s[t:]:
    ans = False

if ans:
    x = "Yes"
else:
    x = "No"
print(x)



Measured memory_bytes: 3064
S = input()
S = S[::-1]

while 1:
    if len(S) == 0:
        res = 1
        break
    elif S[:5] == 'maerd':
        S = S[5:]
    elif S[:5] == 'esare':
        S = S[5:]
    elif S[0] == 'r':
        if S[:7] == 'remaerd':
            S = S[7:]
        elif S[:6] == 'resare':
            S = S[6:]
        else:
            res = 0
            break
    else:
        res = 0
        break

if res == 1:
    print('YES')
else:
    print('NO')



Measured memory_bytes: 3316
#include<bits/stdc++.h>
#define mx 307
using namespace std;

double dp[mx][mx][mx];
bool vis[mx][mx][mx];
double n;

double rec( int x, int y, int z )
{
    if( !(x+y+z) )
        return 0;

    if( vis[x][y][z] )
        return dp[x][y][z];

    double ret= 1;

    if(x)
        ret+= (x/n)*rec( x-1, y, z );
    if(y)
        ret+= (y/n)*rec( x+1, y-1, z );
    if(z)
        ret+= (z/n)*rec( x, y+1, z-1 );

    ret*= n/(x+y+z);

    vis[x][y][z]= 1;

    return dp[x][y][z]= ret;
}

int main()
{
    cin>>n;

    int x= 0, y= 0, z= 0;

    for( int i=0;i<n;i++ )
    {
        int p;
        cin>>p;

        if(p==1)
            x++;
        if(p==2)
            y++;
        if(p==3)
            z++;
    }

    cout<<setprecision(12)<<fixed<< rec( x, y, z ) <<endl;
}


Measured memory_bytes: 250112
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.NoSuchElementException;

public class Main {
	int N,A,B;
	long[] v;
	HashMap<Long,Integer> map;


	public long nCk(int n,int k)
	{
		long r = 1;
		for(long i = 1;i <= k;i++,n--){
			r *= n;
			r /= i;
		}
		return r;
	}

	public void solve() {
		N = nextInt();
		A = nextInt();
		B = nextInt();

		v = new long[N];
		for(int i = 0;i < N;i++){
			v[i] = nextLong();
		}

		Arrays.sort(v);
		for(int i = 0;i < N / 2;i++){
			long tmp = v[i];
			v[i] = v[N-i-1];
			v[N-i-1] = tmp;
		}

		ArrayList<Integer> list = new ArrayList<Integer>();
		long c = 0;
		long m = 1;
		for(int i = A;i <= B;i++){
			long sum = 0;
			for(int j = 0;j < i;j++){
				sum += v[j];
			}
			if(c * i < sum * m){
				c = sum;
				m = i;
			}
		}

		for(int i = A;i <= B;i++){
			long sum = 0;
			for(int j = 0;j < i;j++){
				sum += v[j];
			}
			if(c * i == sum * m){
				list.add(i);
			}
		}

		map = new HashMap<Long,Integer>();
		for(int j = 0;j < N;j++){
			map.put(v[j],map.containsKey(v[j]) ? map.get(v[j]) + 1 : 1);
		}


		long ans = 0;
		for(int i = 0;i < list.size();i++){
			HashMap<Long,Integer> use = new HashMap<Long,Integer>();
			for(int j = 0;j < list.get(i);j++){
				use.put(v[j],use.containsKey(v[j]) ? use.get(v[j]) + 1 : 1);
			}

			long sum = 1;
			for(long key : use.keySet()){
				sum *= nCk(map.get(key),use.get(key));
			}
			ans += sum;
		}
		out.println(String.format("%.09f",c * 1.0 / m));
		out.println(ans);
	}

	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

Measured memory_bytes: 23508
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

template<typename T> T mo(T x, T y) { x %= y; return x <= 0 ? x + y : x; }

int main() {
#ifdef FASTIO
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
    int a, b, c;
    cin >> a >> b >> c;
    if (a == b && b == c) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int

using namespace std;

int main(){
	ll N,W;
	cin>>N>>W;
	ll wrr[N+1],vrr[N+1];
	for(ll i=0;i<N;i++){
		cin>>wrr[i]>>vrr[i];
	}
	ll dp[W+1][N+1];
	
	for(ll i=0;i<=W;i++){
		if(i>=wrr[0]){
			dp[i][0]=vrr[0];
		}
		else{
			dp[i][0]=0;
		}
	}
	for(ll i=0;i<N;i++){
		dp[0][i]=0;
	}
	for(ll i=1;i<=W;i++){
		for(ll j=1;j<N;j++){
			dp[i][j]=dp[i][j-1];
			if(i>=wrr[j]){
				dp[i][j]=max(dp[i][j],dp[i-wrr[j]][j-1]+vrr[j]);
			}
		}
	}
	cout<<dp[W][N-1];
	
	//cout<<*max_element(dp[n-1],dp[n-1]+3);
}

Measured memory_bytes: 79104
#include<bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long
int main()
{
  int n,k;
  cin>>n>>k;
  int a[n+1]={0};
  for(int i=0;i<k;i++)
  {
      int d,e;
      cin>>d;
      for(int i=1;i<=d;i++)
      {
          cin>>e;
          a[e]++;
      }
  }
  int cnt=0;
  for(int i=1;i<=n;i++)
  {
      if(a[i]==0)
        cnt++;
  }
  cout<<cnt<<endl;
}



Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repb(i, n) for (int i = (n-1); i >= 0; i--)
#define rea(i, a, b) for (int i = (a); i < (b); i++)
#define reab(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ALL(a) (a).begin(),(a).end()
#define SZ(x) ((int)(x).size())
#define PRINT(V) for (auto v:(V)) {cerr << v << " ";} cerr << endl
#define dump(x) cerr << #x << " : " << (x) << endl
#define dump2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
inline ll GCD(ll a, ll b){return b ? GCD(b, a % b) : a;}
const int MOD = 1000000007;
/* END OF TEMPLATE */

int main(){
    ll N;
    cin >> N;
    vll h(N);
    rep(i, N){
        cin >> h[i];
    }
    ll ans = h[0];
    rea(i, 1, N){
        if(h[i-1] < h[i]){
            ans += h[i] - h[i-1];
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
/*
　　　  ∧＿∧　やあ
　　 （´・ω・｀)　　　　　/　　　　　ようこそ、バーボンハウスへ。
　　 ／∇y:::::＼　　　 [￣]　　　　　このテキーラはサービスだから、まず飲んで落ち着いて欲しい。
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|　うん、「また」なんだ。済まない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　  仏の顔もって言うしね、謝って許してもらおうとも思っていない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|
　　　　∇　∇　∇　∇　　　／.／|　　　でも、この提出を見たとき、君は、きっと言葉では言い表せない
　　　　┴　┴　┴　┴　／ ／　  |　　　「ときめき」みたいなものを感じてくれたと思う。
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　殺伐としたコンテストの中で、そういう気持ちを忘れないで欲しい
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　そう思って、この提出を投げたんだ。
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 じゃあ、判定を聞こうか。
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <ctype.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
// const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const lint MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};


int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if(a + b == c + d){
        cout << "Balanced" << endl;
    }
    else if(a + b > c + d){
        cout << "Left" << endl;
    }
    else {
        cout << "Right" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
using namespace std;
int main(){
	int i,j,k;
	long long int n,m;
	cin>>n;
	k=sqrt((double)(n*2));
	for(i=k;i>=0;i--){
		m=i*(i+1)/2;
		if(n>m)break;
	}
	cout<<i+1<<endl;
	return 0;
}


Measured memory_bytes: 256
while True:
    (n, x) = [int(i) for i in input().split()]
    if n == x == 0: break
    ans = 0
    for i in range(1,n-1):
        for j in range(i+1,n):
            c=x-i-j
            if c>j and c<=n:
                ans+=1
    print (ans)

Measured memory_bytes: 7708
import bisect

n = int(input())

a = [int(input()) for i in range(n)]

ary = []
ary.append(a[0])

for x in a[1:]:
  i = bisect.bisect_left(ary, x)
  if i == 0:
    ary[0:0] = [x]
  else:
    ary[i-1:i] = [x]
       
print(len(ary))

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

int main(){
  int N;
  cin>>N;
  vector<int> a(N+1);
  for(int i=1;i<=N;i++){
    cin>>a.at(i);
  }
  
  int next=a.at(1);
  int count=1;
  while(1){
    if(next==2){
      break;
    }
    if(count>=N){
      count=-1;
      break;
    }
    next=a.at(next);
    count++;
  }
  cout<<count<<endl;
  return 0;
}

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

struct Edge{
    long long from;
    long long to;
    long long cost;
};

using namespace std;
using Edges = vector<Edge>;
const long long INF = 1LL << 60;

/* bellman_ford(Es,s,t,dis)
    入力: 全ての辺Es, 頂点数N, 開始点 begin_idx, 最短経路を記録するdis
    出力: 負の閉路が存在するなら tureとbegin_idxからの最短距離
    計算量：O(|E||V|)
    副作用：dis が書き換えられる
*/

pair<bool, vector<long long>> bellman_ford(long long begin_idx, long long  N, const Edges& Es){
    //それぞれの頂点へ最短経路距離を保存する配列
    //始点0 ,その他はINF
    vector<long long>  dist(N, INF);
    dist[begin_idx] = 0;

    int cnt = 0;
    while(cnt < N){
        bool end = true;
        for(auto e: Es){
            if(dist[e.from] != INF && dist[e.from] + e.cost < dist[e.to] ){
                dist[e.to] = dist[e.from] + e.cost; 
                //一回でも更新したら,終わりではない
                end = false;
            }
        }
        //更新がなかったらループ抜ける
        if(end) break;
        cnt++;
    }
    return {cnt == N, dist};
}


// 問題例
//　http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B&lang=ja

int main(){
    long long V, E, r;
    cin >> V >> E >> r;

    Edges edges(E);
    for(int i=0; i<E; ++i){
        long long s, t, d;
        cin >> s >> t >> d;
        edges[i] = {s, t, d};
    }

    const auto& ans = bellman_ford(r, V, edges);
    if(ans.first == true){
        cout << "NEGATIVE CYCLE" << endl;
    }else{
        for(auto i : ans.second){
            if(i == INF) cout << "INF" << endl;
            else cout << i << endl;
        }
    }
}


Measured memory_bytes: 3160
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using Map = map<ll,ll>;
using T = tuple<ll,ll,ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define print(v) cout<<v<<endl;
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
template <typename T> bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1000000007;


int main(){
    ll N;
    cin >> N;
    vector<ll> A(N,0);
    for(ll i=0;i<N;i++)cin>>A[i];
    Map M;
    for(ll i=0;i<N;i++)M[A[i]]++;
    ll ans=0;
    for(ll i=1;i<100007;i++)chmax(ans,M[i-1]+M[i]+M[i+1]);
    print(ans)
    return 0;
}


Measured memory_bytes: 7296
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <iomanip>
using namespace std;
#define INF 1e9
#define PI acos(-1)
typedef long long ll;
typedef pair<int, int> p_ii;



int main() {
	int h, w; cin >> h >> w;
	vector<vector<int>> ans(h, vector<int>(w));
	int n; cin >> n;
	vector<int> color(n);
	for (int i = 0; i < n; i++) cin >> color[i];

	int x = 0, y = 0;
	
	for (int i = 0; i < n; i++) {
		int paint = color[i];
		while (paint) {
			ans[y][x] = i + 1;
			paint--;
			if (y % 2 == 0) {
				if (x + 1 >= w) {
					y++;
					continue;
				}
				else {
					x++;
				}
			}
			else {
				if (x - 1 < 0) {
					y++;
					continue;
				}
				else {
					x--;
				}
			}
		}
	}
	
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (j != 0)cout << " ";
			cout << ans[i][j];
		}
		cout << endl;
	}

	return 0;
}

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

using namespace std;

typedef long long ll;

double n,transfer[5],min_trans;
ll res;

int main()
{

    cin >> n ;
    for (int i = 0 ; i <= 4 ; i ++) cin >> transfer[i];
    min_trans = *min_element(transfer, transfer + 5);
    res = ceil((double)(n/min_trans)) + 4;
    cout << res << endl;
    return 0;

}


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

int main() {
	int n;
	cin>>n;
	int a[n], b[n-1];
	for(int i=0;i<n-1;i++)
		cin>>b[i];
	a[0] = b[0];
	a[n-1] = b[n-2];
	for(int i=1;i<n-1;i++)
		a[i] = min(b[i],b[i-1]);
	cout<<accumulate(a,a+n,0);
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include<iomanip>
using namespace std;

int N, M;

int main() {
    cin >> N >> M;
    for (int i = 0; i < max(N, M); i++) {
        cout << min(N, M);
    }
    cout << endl;
}


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

using namespace std;

int main(void)
{
	int w, h, n;
	cin >> w >> h >> n;

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

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
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(a > b){ a = b; return 1; } return 0; }
using ll = long long;
using pi = pair<int,int>;
const ll MOD=1e+9+7;
struct mint{
  ll x;
  mint(ll x=0):x(x%MOD){}
  mint& operator+=(const mint a){
    if((x+=a.x)>=MOD) x-=MOD;
    return *this;
  }
  mint& operator-=(const mint a){
    if((x += MOD-a.x)>=MOD) x-=MOD;
    return *this;
  }
  mint& operator*=(const mint a){
    (x*=a.x)%=MOD;
    return *this;
  }
  mint operator+(const mint a) const{
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const{
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const{
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const{
    if(!t) return 1;
    mint a = pow(t>>1);
    a*=a;
    if(t&1) a*=*this;
    return a;
  }
  
  //for prime mod
  mint inv() const{
    return pow(MOD-2);
  }
  mint& operator/=(const mint a){
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const{
    mint res(*this);
    return res/=a;
  }
};


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

  vector<vector<mint>> dp(n+1, vector<mint>(m+1));
  vector<vector<mint>> sum(n+1, vector<mint>(m+1));  
  rep(i,n) {
    rep(j,m) {
      if(s[i] == t[j]) {
	dp[i+1][j+1] = sum[i][j] + (mint)1;
      }
      sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + dp[i+1][j+1];
    }
  }
  mint res(1);
  rep(i,n+1) {
    rep(j,m+1) {
      res += dp[i][j];
    }
  }
  cout << res.x << "\n";
  
  return 0;
}


Measured memory_bytes: 65948
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;

#define int long long
#define double long double
#define mp make_pair
#define debug(x) cerr << #x << ": " << x << '\n';
#define fi first
#define se second
#define pb push_back
//typedef long long ll;
typedef pair<int, int> pii;
 
void fast() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
}

signed main() {
	fast();
	int a[2];
	int b[2];
	cin >> a[0] >> a[1];
	cin >> b[0] >> b[1];
	int ans = -1e18;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			ans = max(ans, a[i] * b[j]);
		}
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 3660
#include<iostream>
using namespace std;
int main() {
	int a, b, c, d,o=0;
	cin >> a >> b >> c >> d;
	if (a == 11 && b == 30 && c == 12 && d == 1) {
		o++;
	}
	if (a == 1 && b == 31 && c == 2 && d == 1) {
		o++;
	}
	if (a == 2 && b == 28 && c == 3 && d == 1) {
		o++;
	}
	if (a == 3 && b == 31 && c == 4 && d == 1) {
		o++;
	}
	if (a == 4 && b == 30 && c == 5 && d == 1) {
		o++;
	}
	if (a == 5 && b == 31 && c == 6 && d == 1) {
		o++;
	}
	if (a == 6 && b == 30 && c == 7 && d == 1) {
		o++;
	}
	if (a == 7 && b == 31 && c == 8 && d == 1) {
		o++;
	}
	if (a == 8 && b == 31 && c == 9 && d == 1) {
		o++;
	}
	if (a == 9 && b == 30 && c == 10 && d == 1) {
		o++;
	}
	if (a == 10 && b == 31 && c == 11 && d == 1) {
		o++;
	}
	cout << o << endl;

	return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int a = scanner.nextInt();
    String op = scanner.next();
    int b = scanner.nextInt();

    // aとbをopにしたがって計算する
    // opが"+"だったら足し算
    if(op.equals("+")) {
      System.out.println(a + b);
    // "-"だったら引き算
    } else if(op.equals("-")) {
      System.out.println(a - b);
    } else {
      System.out.println("+か-の演算子を使用してください。");
    }
  }
}

Measured memory_bytes: 35772