akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int) (u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status()
{
    ifstream t("/proc/self/status");
    cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline T read()
{
	register T sum(0), fg(1);
	register char ch(getchar());
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') fg = -1;
	for(;  isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) - '0' + ch;
	return sum * fg;
}

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

const int MAXN = (int) 2e5;

struct edge
{
	int adj, nxt, w;

	edge() { }
	edge(int _adj, int _nxt, int _w): adj(_adj), nxt(_nxt), w(_w) { }
};

edge e[MAXN * 2 + 5];
int st[MAXN + 5], edge_cnt;

inline void add_edge(int u, int v, int w) { e[edge_cnt] = edge(v, st[u], w), st[u] = edge_cnt++; }

int n, m;

char s[MAXN + 5];

inline void input()
{
	n = read<int>(), m = read<int>();
	scanf("%s", s + 1);

	memset(st, -1, sizeof st), edge_cnt = 0;
	for(int i = 0; i < m; ++i)
	{
		int u = read<int>(), v = read<int>(), w = s[u] == s[v];
		add_edge(u, v, w);
		add_edge(v, u, w);
	}
}

bool vis[MAXN * 2 + 5], cut[MAXN * 2 + 5];

inline void dfs(int u, int id)
{
	for(int i = st[u]; ~i; i = e[i].nxt)
	{
		int v = e[i].adj;
		if(!vis[i] && e[i].w != e[id].w)
		{
			vis[i] = cut[i] = 1;
			dfs(v, i);
			vis[i] = 0;
		}
		else if(e[i].w != e[id].w) { puts("Yes"); exit(0); }
	}
}

inline void solve()
{
	for(int i = 0; i < edge_cnt; ++i) if(!cut[i]) dfs(e[i].adj, i);
	puts("No");
}

int main()
{
//	freopen("C.in", "r", stdin);
//	freopen("C.out", "w", stdout);

	input();
	solve();

	return 0;
}



Measured memory_bytes: 10368
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-9;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

int h,w,d;

int main(){
	cin>>h>>w>>d;
	int D=2*d;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int x=(i+j)%D,y=(i-j+1000*D)%D;
			if(x>=d&&y>=d) cout<<'R';
			if(x>=d&&y<d) cout<<'Y';
			if(x<d&&y>=d) cout<<'G';
			if(x<d&&y<d) cout<<'B';
		}
		cout<<endl;
	}
}

Measured memory_bytes: 512
#include <stdio.h>

void mms();
 
int main(void) {
    
    mms();
    return 0;
}

void mms(){
	int i,n,x[10000];
    long min, max, sum;
     
    scanf("%d",&n);
    for(i = 0;i < n;i++){
        scanf("%d",&x[i]);
    }
    
    min = x[0]; max = x[0]; sum = 0;
     
    for(i = 0;i < n;i++){
        if(max < x[i]){
            max = x[i];
        }
        else{}
    }
     
    for(i = 0;i < n;i++){
        if(min > x[i]){
            min = x[i];
        }
        else{}
    }
     
    for(i = 0;i < n;i++){
        sum = sum + x[i];
    }

     
    printf("%ld %ld %ld\n",min,max,sum);
}

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

#define NREP() for(ll i = 0; i < n; i++)
#define MREP() for(ll j = 0; j < m; j++)
#define REP(i, x, y) for(ll i = x; i < y; i++)
#define ALL(x) (x).begin(), (x).end()
#define MSG(x) cout << x << endl;
#define END(x) cout << x << endl; exit(0);
#define IPT(v, n) NREP(){ cin >> v[i]; }
#define YN(x) x ? cout << "YES" << endl : cout << "NO" << endl;
#define Yn(x) x ? cout << "Yes" << endl : cout << "No" << endl;
#define yn(x) x ? cout << "yes" << endl : cout << "no" << endl;

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;

const static ll MOD = 1e9 + 7;
ll n, cnt, ans, tmp, sum, minimum, maximum, a, b, c, d, k, m, h, w, q, x, y;
string s, t;

int main() {
    cin >> a >> b;
    MSG(max(a - 2 * b, 0ll))
}


Measured memory_bytes: 256
n = int(input())
t, a = map(int,input().split())
h = list(map(int,input().split()))
tmp = int(1e9)
ans = 0
for i in range(n):
    av_tmp = abs(a - (t-h[i]*0.006))
    if av_tmp < tmp:
        ans = i+1
        tmp = av_tmp
print(ans)

Measured memory_bytes: 3064
#!/usr/bin/python

import sys

kusi = int(input())
kusi_len = input().rstrip().split(" ")
cnt = 0
ans = 0
for line in sorted(kusi_len, key=int):
  cnt += 1
  if cnt == 1:
    a = int(line)
  elif cnt == 2:
    b = int(line)
    if a <= b:
      ans = ans + a
    else:
      ans = ans + b
    cnt = 0
print(ans)


Measured memory_bytes: 3060
#include <iostream>
#include<list>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    priority_queue<int> que[n];
    int tp1,tp2,a;
    while(q--){
        cin>>tp1;
        switch(tp1){
        case 0:
            cin>>tp2>>a;
            que[tp2].push(a);
            break;
        case 1:
            cin>>tp2;
            if(!que[tp2].empty())cout<<que[tp2].top()<<"\n";
            break;
        case 2:
            cin>>tp2;
            if(!que[tp2].empty())que[tp2].pop();
            break;
        }
    }
}

//list<int> lit;
//list<int>::iterator it;
//int main()
//{
//    lit.clear();
//    int t,tp,a;
//    it=lit.begin();
//    cin>>t;
//    while(t--){
//        cin>>tp;
//        switch(tp){
//        case 0:
//            cin>>a;
//            lit.insert(it,a);
//            break;
//    case 1:
//            cin>>a;
//            if(a<0){
//                while(a++){
//                    if(it!=lit.end())it++;
//                }
//            }
//            else if(a>0){
//                while(a--){
//                    if(it!=lit.begin())it--;
//                }
//            }
//        break;
//    case 2:
//        lit.erase(--it);
//        break;
//        }
//    }
//    lit.reverse();
//    for(it=lit.begin();it!=lit.end();it++){
//
//        cout<<*it<<"\n";
//    }
//}



Measured memory_bytes: 4092
#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()
{
     ll n,i;
     cin>>n;
     ll arr[n];
     ll flag=1;
     for(i=0;i<n;i++)
     {
         cin>>arr[i];
         if(arr[i]%2==0)
         {
             if(arr[i]%3!=0 && arr[i]%5!=0)
             {
                 flag=0;
                 break;
             }
         }
     }
     if(flag)
     {
         cout<<"APPROVED";
     }
     else
     {
         cout<<"DENIED";
     }
     cout<<endl;
}


Measured memory_bytes: 256
/*
*@author:mel0n
*
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int,int>pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

//typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>

const int MOD=1000000007;
const ll INF=1e18;
const int MX=100001;
const ld PI=4*atan((ld)1);

//void setIn(string s){freeopen(s.c_str(),"r",stdin);}
//void setOut(string s){freeopen(s.c_str(),"w",stdout); }
//void setIO(){ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//}




int main()
{

	string s;
	cin>>s;
	ll ans=0,res=0;
	for(auto x:s)
	{
		if(x=='R')
			ans++;
		else
			ans=0;
		res=max(res,ans);
	}	
	cout<<res<<endl;	
}







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

int main(){
    ll K,A,B;
    cin >> K >>A >> B;
    if(A>=K || B-A<=2){
        cout << K+1 << endl;
        return 0;
    }

    ll ans =1;
    ans += A-1;

    if((K-A+1)%2==0){
        ans += (B-A)*(K-A+1)/2;
    }else{
        ans++;
        ans += (B-A)*(K-A)/2;
    }
    cout << ans << endl;


    
    return 0;
}

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

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

dp=[10**10 for i in range(N)]
dp[0]=0

for i in range(1,N):
    MIN=10**10
    for j in range(1,K+1):
        if i-j<0:
            break
        else:
            if dp[i-j]+abs(H[i]-H[i-j])<MIN:
                MIN=dp[i-j]+abs(H[i]-H[i-j])
    dp[i]=MIN
#print(dp)
print(dp[N-1])

Measured memory_bytes: 56544
#include <iostream>
#include <string.h>

int main(){
  std::string a;
  std::cin >> a;

  if(a == "Sunny")
    std::cout << "Cloudy\n";
  else if(a == "Cloudy")
    std::cout << "Rainy\n";
  else 
    std::cout << "Sunny\n";

  return 0;
}


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

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

template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) { REP(i,n) cout<<v[i]<< ' '; cout << endl; }

typedef long long ll;

const int INF = INT_MAX / 2;

const int WMAX = 100000;
const int NMAX = 100;
ll dp[NMAX+10][WMAX+10];

int main() {
  int N,W; cin>>N>>W;
  vector<int> w(N);
  vector<ll> v(N);
  REP(i,N) cin>>w[i]>>v[i];
  for(int i = 0; i < N; i++) {
    for(int j = 0; j <= W; j++) {
      int nw = j + w[i];
      chmax(dp[i+1][j], dp[i][j]);
      if(nw <= W) 
	chmax(dp[i+1][nw], dp[i][j] + v[i]);
    }
  }
  ll ans = 0;
  for(int i = 0; i <= W; i++) {
      chmax(ans, dp[N][i]);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 79360
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll mod=1e9+7;
int dy[]={1,-1,0,0};
int dx[]={0,0,1,-1};

int main(){
    int N;
    cin>>N;
    vector<pair<int,int>> a(N);
    for(int i=0;i<N;i++){
        int tmp;
        cin>>tmp;
        a[i]={tmp,i};
    }
    sort(a.rbegin(),a.rend());
    int max_v=a[0].first;
    int max_2nd=a[1].first;
    int max_locate=a[0].second;
    for(int i=0;i<N;i++){
        if(i==max_locate){
            cout<<max_2nd<<endl;
        }else{
            cout<<max_v<<endl;
        }
    }
    return 0;
}


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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  int n ;
  cin >> n ;
  int ans = 0;
  
  rep(i,n){
    int c ;
    cin >> c ;
    ans += c-1 ;
  }
  
  cout <<ans ;
}
 

Measured memory_bytes: 256
#include<stdio.h>
#define INF 100000000
int m[101][101];
int p[101];
int n;
void matrixChainOrder(void){
  int i,j,k,q,l;
  for(i=1;i<=n;i++) m[i][i] = 0;
  for(l=2;l<=n;l++){
    for(i=1;i<=n-l+1;i++){
      j = i+l-1;
      //printf("a%d %d\n",i,j);
      m[i][j] = INF;
      for(k=i;k<=j-1;k++){
	q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
	m[i][j] = m[i][j] > q ? q : m[i][j];
      }
    }
  }
}
int main(){
  int i,j;
  for(i=0;i<101;i++)
    for(j=0;j<101;j++) m[i][j] = INF;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d%d",&p[i-1],&p[i]);
  }
  matrixChainOrder();
  printf("%d\n",m[1][n]);
  return 0;
}



Measured memory_bytes: 1992
#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
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; }
int main(){
    int n,m;
    cin>>n>>m;
    vector<int> ans;
    string s;
    cin>>s;
    reverse(all(s));
   // cout<<s<<endl;
    //int times=0;
    for(int i=0;i<=n;i++){
        if(m>=n-i){
            ans.push_back(n-i);
            break;
        }
        int j=m;
       while(j>0){
           if(i+j<=n&&s[i+j]=='0'){
               ans.push_back(j);
               break;
           }
           j--;
       }
      if(j==0){
          cout<<-1<<endl;
          return 0;
      }
      i+=j-1;
    }
    reverse(all(ans));
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}


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

int main(){
  int n;
  cin >> n;
  vector<int64_t>a(n);
    int64_t ans=0,sum=0;
  for(int i=0;i<n;i++){
    cin >> a.at(i);
    sum+=a.at(i);
  }
  for(int i=0;i<n-1;i++){
    sum-=a.at(i);
    ans=(ans+sum%1000000007*a.at(i))%1000000007;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 4772
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,tmp,minor;

int main(){
	string s="";
	cin>>n;
	while(n>0){
		tmp=n%26;
		n/=26;
		if(!tmp){
			tmp=26;
			n--; 
		}
		s=char(tmp-1+'a')+s;
	}
	cout<<s; 
	return 0;
}

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

int main() {
     long long i,j,l,q,t,m,n,h,p;
     long long k,z,p1,h1,x,y;
     string st="",st1="";
     q=1;
     for(l=0;l<q;l++)
     {
          std::cin >> st;
          n=st.length();
          if(st[n-1]=='s')
               st+="es";
          else st+="s";
          std::cout << st << std::endl;
     }
    return 0;
}

Measured memory_bytes: 3628
n,h=map(int,input().split())
aa=[]
bb=[]
for _ in range(n):
  a,b=map(int,input().split())
  aa.append(a)
  bb.append(b)
  
maxa=max(aa)
bb.sort(reverse=True)
nage=0
ch=0
for i in range(n):
  if bb[i]>=maxa:
    nage+=bb[i]
    ch+=1
  else:
    break
    
if h>nage:
  rest=h-nage
  ch1=0
  if rest%maxa==0:
    ch1+=rest//maxa
  else:
    ch1+=(rest//maxa+1)
  print(ch1+ch)
  
else:
  ans=0
  hp=0
  for j in range(n):
    if hp>=h:
      break
    else:
      ans+=1
      hp+=bb[j]
  print(ans)
    
  
  

Measured memory_bytes: 17156
#include<cstdio>
char isl[20][20];
int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
void dfs(int y,int x){
    if(y<0||x<0||y>=12||x>=12)return;
    if(isl[y][x]=='1'){
        isl[y][x]='0';
        for(int i=0;i<4;i++)dfs(y+dy[i],x+dx[i]);
    }
}
int main(){
    while(scanf("%s",isl[0])!=EOF){
        for(int i=1;i<12;i++)scanf("%s",isl[i]);
        int cnt=0;

        for(int i=0;i<12;i++){
            for(int j=0;j<12;j++){
                if(isl[i][j]=='1'){
                    cnt++;
                    dfs(i,j);
                }
            }
        }

        printf("%d\n",cnt);
    }
}

Measured memory_bytes: 968
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい
//for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007
int main(){
    int n,k;
    cin >> n>>k;
    vector<int> a(n);
    rep(i,n){
        cin >> a[i];
    }
    cout <<1+ceil(double(n-k)/(k-1))<<endl;
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(void) {
  double distance;
  int N, D, count, i, j, k, tmp;
  vector<int> v_tmp;
  vector<vector<int>> X;
  cin >> N;
  cin >> D;
  for(i = 0; i < N; i++) {
    v_tmp.clear();
    for(j = 0; j < D; j++) {
    	cin >> tmp;
    	v_tmp.push_back(tmp);
    }
    X.push_back(v_tmp);
  }
  
  count = 0;
  for(i = 0; i < N - 1; i++) {
    for(j = i + 1; j < N; j++) {
      distance = 0.0;
      for(k = 0; k < D; k++) {
        distance += pow(X[i][k] - X[j][k], 2.0);
      }
      distance = sqrt(distance);
      if(ceil(distance) == floor(distance)) {
        count++;
      }
    }
  }
  
  cout << count << endl;
  
  return 0;
}

Measured memory_bytes: 256
void main() {
    int[] tmp = readln.split.to!(int[]);
    int d = tmp[0], n = tmp[1];
    writeln(n != 100 ? 100 ^^ d * n : 100 ^^ d * (n + 1));
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.uni;

Measured memory_bytes: 256
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    }

    static class B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 整数の入力
            int N = Integer.parseInt(in.next());
            String S = in.next();
            int ans = 0;

            for (int i = 0; i < N - 2; i++) {
                if ('A' == S.charAt(i) && 'B' == S.charAt(i + 1) && 'C' == S.charAt(i + 2)) {
                    ans++;
                }
            }

            // 出力
            out.println(ans);

        }

    }
}



Measured memory_bytes: 25044
// 失敗するからこそ そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
// ぜったいあきらめない
#if defined(DAIJOBU)
#include "/home/ichigo/includes.hpp"
#include "/home/ichigo/debug.hpp"
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#include <bits/stdc++.h>
#define deb(x...)
#endif
#define FIO {ios::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);}

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


int main(){
	FIO
	
	int a[3];
	cin >> a[0] >> a[1] >> a[2];
	sort(a, a + 3);
	cout << a[0] + a[1] << '\n';
 	return 0; 
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
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; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

signed main()
{
    int n,a,b;
    cin>>n>>a>>b;
    if(a>b) swap(a,b);
    int c=b-a;
    if(c%2==0){
        //cout<<min({c/2,n-a,b})<<endl;
        cout<<c/2<<endl;
    }
    else
    {
       cout << min({n-b + 1+ (c-1) /2 ,a  + (c-1)/2}) << endl;
    }
    
    
    return 0;
}

Measured memory_bytes: 256
import java.util.*

fun main(args:Array<String>) {

    val mod = 1000000007L
    val n = readLine()!!.toInt()
    val dp = LongArray(n)
    val factorial = LongArray(n+1)
    val edges = Array(n){HashSet<Int>()}
    val s = Array(n){0}
    val visited = BooleanArray(n)

    factorial[0] = 1
    factorial[1] = 1

    for(i in 2 ..n) {
        factorial[i] = (factorial[i-1] * i) % mod
    }

    for(i in 0 until n - 1) {
        var (a,b) = readLine()!!.split(" ").map { it.toInt() }
        a--
        b--
        edges[a].add(b)
        edges[b].add(a)
    }

    fun modInv(a:Long, m:Long = mod): Long {
        // a^m-2
        fun mInv(a:Long, m:Long): Long {
            if(m == 0L) return 1L
            if(m == 1L) return a

            var result = mInv(a, m / 2) % mod
            if(result < 0) {
                println(".")
            }
            result = (result * result) % mod
            if(m % 2 != 0L) {
                result = (result * a) % mod
            }
            if(result > mod) {
                println(".")
            }
            return result
        }
        val ret = mInv(a, m - 2)
        return ret
    }

    fun dfs(cur:Int): Int {
        var cnt = 0
        visited[cur] = true
        for(adj in edges[cur]) {
            if(visited[adj]) continue
            cnt += dfs(adj)
        }
        visited[cur] = false
        s[cur] = cnt + 1
        dp[cur] = (factorial[s[cur] - 1])
        for(adj in edges[cur]) {
            if(visited[adj])  continue
            dp[cur] = (dp[cur] * dp[adj]) % mod
            dp[cur] = dp[cur] * modInv(factorial[s[adj]]) % mod
//            dp[cur] = (dp[cur] * dp[adj] / factorial[s[adj]]) % mod
//            dp[cur] %= mod
        }
        return s[cur]
    }

    fun dfs2(root:Int) {
        visited[root] = true
        for(child in edges[root]) {
            if(visited[child]) continue
            //dp[child] = dp[cur] * s[child] / factorial[ n - s[child] ]
//            dp[child] = dp[root] * s[child] / factorial [ n - s[child]]
            val v = (n - s[child]).toLong()
            dp[child] = (dp[root] * s[child]) % mod
            dp[child] = (dp[child] * modInv(v)) % mod
            /*
            if(dp[child] / v == 0L) {
                dp[child] = (dp[child] + mod) / v
                if(dp[child]==0L) {
                    while (true){
                    }
                }
            } else {
                dp[child] = dp[child] / v
            }
             */
            dp[child] %= mod
            dfs2(child)
        }
        visited[root] = false
    }
    dfs(0)
    dfs2(0)

    for(i in 0 until n) {
        println(dp[i])
    }
}

Measured memory_bytes: 170124
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define debug(s, param) std::cerr << s << param << std::endl;

using namespace std;
using ll = long long;

int main() {
  string s, t;
  cin >> s >> t;
  int n = (int)s.size() - (int)t.size();
  int ans = (int)t.size();
  rep (i, n + 1) {
    string s1 = s.substr(i, t.size());
    int num = (int)t.size();
    rep (i, (int)t.size()) {
      if (s1[i] == t[i]) --num;
    }
    ans = min(ans, num);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3584
#include <stdio.h>
#include <stdlib.h>

int yama[51];

void init_yama(int n){
	int i,j;
	for(i=n,j=1;j<=n;i--,j++){
		yama[j] = i;
	}
	return;
}

void cut(int p, int c){
	int temp[50];
	int i,j;
	for(i=1,j=0;i<=p-1;i++,j++){
		temp[j]=yama[i];
	}

	for(i=p,j=1;j<=c;i++,j++){
		yama[j]=yama[i];
	}

	for(i=c+1,j=0;j<p-1;i++,j++){
		yama[i]=temp[j];
	}

	return;
}

int main()
{
    int n,r,p,c,i,j;

    while(1){
		scanf("%d %d",&n,&r);
		if(n==0 && r==0)
			break;

		init_yama(n);

		for(i=0;i<r;i++){
			scanf("%d %d",&p,&c);
			cut(p,c);
		}


		printf("%d\n",yama[1]);
    }

    return 0;
}

Measured memory_bytes: 596
#include<iostream>
using namespace std;
int t[300];

int change(string &s){
  int res=0,num=0;
  for(int i=0;i<(int)s.size();i++){
    if('0'<=s[i]&&s[i]<='9')num=num*10+(s[i]-'0');
    else{
      if(num==0)num=1;
      res+=num*t[(int)s[i]];
      num=0;
    }
  }
  return res;
}

void change2(int num){
  int a=num/1000;
  if(a>1)cout<<a;
  if(a>0)cout<<'m';
  num%=1000;

  a=num/100;
  if(a>1)cout<<a;
  if(a>0)cout<<'c';
  num%=100;

  a=num/10;
  if(a>1)cout<<a;
  if(a>0)cout<<'x';
  num%=10;

  a=num;
  if(a>1)cout<<a;
  if(a>0)cout<<'i';

  cout<<endl;
}

int main(){
  t[(int)'m']=1000;
  t[(int)'c']=100;
  t[(int)'x']=10;
  t[(int)'i']=1;
  string A,B;
  int Tc;
  cin>>Tc;
  for(int tc=0;tc<Tc;tc++){
    cin>>A>>B;
    int a=change(A);
    int b=change(B);
    change2(a+b);
  }
  
  return 0;
}

Measured memory_bytes: 1208
#include <bits/stdc++.h>
using namespace std;
struct point{int x;int y;};
int i,j,k,count1=0,count2=0;

int main(){
    int h,w;
    cin>>h>>w;
    char c[h][w];
    for(i=0;i<h;i++){
        for(j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    for(i=0;i<h;i++){
        for(j=0;j<w;j++){
            if(c[i][j]=='#'){
                count2++;
                if(c[i][j-1]=='#'||c[i][j+1]=='#'||c[i-1][j]=='#'||c[i+1][j]=='#'){
                    count1++;
                }
            }
        }
    }
    cout<<((count1==count2)?"Yes":"No")<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
// const int N = 1e5 + 7;
// typedef long long int lli;
// int a[N];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> ve(n, 0);
    for (int i = 0; i < n; i++) cin >> ve[i];
    vector<int> v(n, INT_MAX);
    v[0] = 0;
    for (int i = 1; i < n && i <= k; i++) {
        for (int j = 0; j < i; j++) {
            v[i] = min(v[j] + abs(ve[j] - ve[i]), v[i]);
        }
    }
    for (int i = k + 1; i < n; i++) {
        for (int j = i - k; j < i; j++) {
            v[i] = min(v[j] + abs(ve[j] - ve[i]), v[i]);
        }
    }
    cout << v[n - 1] << endl;
    return 0;
}

Measured memory_bytes: 1152
/*************************************************************************
    > File Name: 1.cpp
    > Author: PumpkinYing
    > Created Time: 2018年01月20日 星期六 12时12分00秒
 ************************************************************************/

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define INF 1e9
#define pii pair<int,int>
typedef long long ll;

const int maxn = 210;
int d[maxn][maxn];

int dep[maxn];
int n;
int cnt = 0;

int bfs(int s,int t)
{
	mem(dep,-1);
	dep[s] = 0;
	queue<int> q;
	q.push(s);
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		for(int v = 0;v < cnt;v++) 
		{
			if(d[u][v] > 0 && dep[v] == -1) 
			{
				dep[v] = dep[u]+1;
				q.push(v);
			}
		}
	}
	return dep[t] != -1;
}

int dfs(int u,int t,int mn)
{
	if(u == t) return mn;
	int tmp;
	for(int v = 0;v < cnt;v++)
	{
		if(d[u][v] > 0 && dep[v] == dep[u]+1 
				&& (tmp = dfs(v,t,min(mn,d[u][v]))))
		{
			d[u][v] -= tmp;
			d[v][u] += tmp;
			return tmp;
		}
	}
	return 0;
}

void addedge(int u,int v,int c)
{
	d[u][v] += c;
}


int max_flow(int s,int t)
{
	int ans = 0;
	while(bfs(s,t))
	{
		while(1)
		{
			int tmp = dfs(s,t,INF);
			if(tmp == 0) break;
			ans += tmp;
		}
	}
	return ans;
}

pii red[maxn],blue[maxn];

int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) scanf("%d%d",&red[i].first,&red[i].second);
	for(int i = n+1;i <= 2*n;i++) scanf("%d%d",&blue[i].first,&blue[i].second);

	int s = 2*n+1,t = 2*n+2;
	cnt = t+1;

	for(int i = 1;i <= n;i++)
		for(int j = n+1;j <= 2*n;j++)
		{
			if(red[i].first < blue[j].first && red[i].second < blue[j].second) addedge(i,j,1);
		}

	for(int i = 1;i <= n;i++) addedge(s,i,1);
	for(int i = n+1;i <= 2*n;i++) addedge(i,t,1);

	int ans = max_flow(s,t);
	printf("%d\n",ans);

	return 0;
}


Measured memory_bytes: 384
#include <cstring>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <set>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int a[300001];
int d[300001];
int m[300001];

int main(){
  const int n = getInt();
  const int q = getInt();

  REP(i,n) a[getInt()] = 1;
  int c = -1;
  REP(i,300001){
    d[i] = c;
    if(a[i]) c = i;
  }

  // REP(i,20) printf("%d: %d\n", i, d[i]);

  memset(m, -1, sizeof(m));
  REP(i,q){
    const int qq = getInt();
    if(m[qq] != -1){
      printf("%d\n", m[qq]);
    }else{
      int ret = 0;
      for(int i = 0; i < 300001; i += qq){
        if(d[i] != -1 && d[i] > i - qq)
          ret = max(ret, d[i] - (i - qq));
      }
      printf("%d\n", m[qq] = ret);
    }
  }
  return 0;
}

Measured memory_bytes: 4548
import java.util.Scanner;

class Main {

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

		int N = sc.nextInt();
		
		int[] a = new int[N];
		for(int i=0; i<N; i++){
			a[i] = sc.nextInt();
		}

		int max = a[0];
		int min = a[0];
		for(int i=1; i<N; i++){
			if(a[i]>max){
				max = a[i];
			}
			if(a[i]<min){
				min = a[i];
			}
		}

		System.out.println(max-min);

	}

}


Measured memory_bytes: 21844
def next(n):
    # nの次に小さい、3と7と5しか出てこない数
    s = str(n)
    if s[-1] == "3":
        return int(s[:-1] + "5")
    if s[-1] == "5":
        return int(s[:-1] + "7")
    # s[-1] == "7"
    if len(s) == 1:
        return 33
    else:
        s = str(next(s[:-1]))
        return int(s + "3")

def is_753(n):
    # 3, 7, 5 がすべて表れているかを判定
    s = str(n)
    if "3" in s and "5" in s and "7" in s:
        return True
    return False

def next_753(n):
    # 次の753数を返す
    while True:
        n = next(n)
        if is_753(n):
            return n

N = int(input())
ans = 0
n = 357
while n <= N:
    ans += 1
    n = next_753(n)
print(ans)

Measured memory_bytes: 3064
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        hi = h[i]
        a[i] = min(a[j] + abs(hi - h[j]) for j in range(max(0, i - k), i))
    print(a[-1])

if __name__ == '__main__':
    main()


Measured memory_bytes: 13980
#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));}
void printvi(vector<int> v){rep(i,0,v.size()){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
void printvil(vector<int> v){rep(i,0,v.size()){cout<<v[i]<<endl;}}
void printvvi(vector<vector<int>> v){
    rep(i,0,v.size()){
        rep(j,0,v[i].size())cout<<v[i][j];
        cout<<endl;
    }
}
void printvstr(vector<string> v){
    rep(i,0,v.size()){
        cout<<v[i]<<endl;
    }
}

signed main (){
    string s[3];
    rep(i,0,3)cin>>s[i];
    cout<<s[0][0]<<s[1][1]<<s[2][2]<<endl;
    
}
/*

*/


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define gcd __gcd
#define pcount __builtin_popcount
#define pcountll __builtin_popcountll
#define nxtperm next_permutation
//primitive roots: 998244353 - 3; 7340033 - 5
//#pragma 03
//#pragma GCC target ("sse4.2")

const ll mod = 1000000007;		const double PI = acos(-1.);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef complex<double> cd;

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
	{
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
	{
        static const uint64_t kawaii = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + kawaii);
    }
};

int dp[3005][3005]; 
int n, m; 
string s, t, ans;
 
void traverse(int i, int j)
{
	if(i == 0 or j == 0)
	{
		reverse(ans.begin(), ans.end()); cout << ans ;
		exit(0);
	 } 
	if(s[i-1] == t[j-1])
	{
		ans.pb(s[i-1]); 
		traverse(i-1, j-1); 
	}
	else
	{
		if(dp[i][j] == dp[i-1][j]) traverse(i-1, j); 
		else if(dp[i][j] == dp[i][j-1]) traverse(i, j-1); 
		else traverse(i-1, j-1); 
	}
}

signed main()
{
//	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
// 	fast_cin();
	cin >> s >> t; 
	n = s.length(); m = t.length(); 
	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], dp[i-1][j-1]}); 
			}
		}
	}
	traverse(n, m); 
}

/*
	Test input:
*/


Measured memory_bytes: 35456
using System;
using System.Collections.Generic;
using System.Text;

class TEST{
	static void Main(){
		while(true){
			var s=Console.ReadLine().Split(' ');
			var H=int.Parse(s[0]);
			var W=int.Parse(s[1]);
			if(H==0&&W==0)break;
			List<String> Tiles= new List<String>();
			for(int i=0;i<H;i++)Tiles.Add(Console.ReadLine());
			
			Dictionary<RCPair,int> FootSteps=new Dictionary<RCPair,int>();
			
			
			//walk around
			RCPair FootStep;
			FootStep.R=0;
			FootStep.C=0;
			
			while(true){
				if(Tiles[FootStep.R][FootStep.C]=='.'){
					Console.WriteLine(FootStep.C+" "+FootStep.R);
					break;
				}
				if(FootSteps.ContainsKey(FootStep)){
					Console.WriteLine("LOOP");
					break;
				}
				FootSteps.Add(FootStep,1);
				if(Tiles[FootStep.R][FootStep.C]=='<')FootStep.C--;
				if(Tiles[FootStep.R][FootStep.C]=='>')FootStep.C++;
				if(Tiles[FootStep.R][FootStep.C]=='^')FootStep.R--;
				if(Tiles[FootStep.R][FootStep.C]=='v')FootStep.R++;
			}
			
		}
	}
}

struct RCPair{
	public int R;
	public int C;
}

Measured memory_bytes: 6052
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>

#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class CIntervalGame {
public:
    static constexpr int kStressIterations = 0;

    static void generateTest(std::ostream& test) {
    }

    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<int> L, R;
        REP(i, N) {
            int l, r;
            cin >> l >> r;
            L.push_back(l);
            R.push_back(r);
        }
        sort(L.rbegin(), L.rend());
        sort(R.begin(), R.end());

        int res = 0;
        int tmp = 0;
        REP(i, N) {
            tmp += L[i];
            res = max(tmp, res);
            tmp -= R[i];
            res = max(tmp, res);
        }

        tmp = 0;
        REP(i, N) {
            tmp -= R[i];
            res = max(tmp, res);
            tmp += L[i];
            res = max(tmp, res);
        }

        cout << res * 2 << endl;
    }
};


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    CIntervalGame solver;
    solver.solve(in, out);
    return 0;
}


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

using namespace std;

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

int main() {
  
  int N, Q; cin >> N >> Q;
  vector<int> A(600001);
  rep(i, N) {
    int x; cin >> x; A[x] = x;
  }
  
  int now = 0;
  rep(i, 600001) {
    if(now < A[i]) { now = A[i]; }
    else { A[i] = now; }
  }
  
  rep(_, Q) {
    int ans = 0;
    int p; cin >> p;
    for(int i=p-1; i<600001; i+=p) {
      ans = max(ans, A[i]%p);
    }
    cout << ans << endl;
  }
  
  return 0;
}

Measured memory_bytes: 3380
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>; // グラフ型
typedef long long ll;
#define _GLIBCXX_DEBUG

 // 深さ優先探索
vector<bool> seen;
int dfs(const Graph &G, ll v) {
    seen[v] = true; // v を訪問済にする
    int score = 0; 

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue; // next_v が探索済だったらスルー

        // 負荷さ優先探索中の処理



        dfs(G, next_v); // 再帰的に探索, 毎回mat渡してええの？
    }
    return score;
} 

/*
//桁数の計算
ll keta(ll num){
  ll ans= 0 ;
  ll rem;

  for (ll i = 4; i >= 0 ; i--){
    rem = pow(10,i);
    ans += (num / rem); 
    num = num % rem;
  }
  return ans;
}
*/


int main() {
  ll N, ans, tmp;
  cin >> N;
  vector<ll> a(N);

  for (ll i = 0; i < N; i++){
    ll input;
    cin >> input;
    a.at(i) = input - 1;
  }

  ans = 1;// 回数をカウント
  tmp = 0;//今光っているところ
  ll flag = false;

  while (a.at(tmp) != 1){
    tmp = a.at(tmp);
    ans++;
    
    if(ans >= N){
      flag = true;
      break;
    }
  }
  
  if (!flag) cout << ans << endl; 
  else cout << "-1" << endl;

  return 0;
}

Measured memory_bytes: 1024
import java.util.*;

public class Main {

  static int ans;
  static int[][] dp;
static int R;
static int[] r;
static int[] mach;
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    R = sc.nextInt();
    r = new int[R];
    mach = new int[R];
    for(int i = 0; i < R; i++) {
      r[i] = sc.nextInt() - 1;
    }

    dp = new int[n][n];
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        if(i != j) dp[i][j] = 100000000; 
      }
    }
    for(int i = 0; i < m; i++) {
      int a = sc.nextInt() - 1;
      int b = sc.nextInt() - 1;
      int c = sc.nextInt();
      dp[a][b] = c;
      dp[b][a] = c;
    }
    
    for(int k = 0; k < n; k++) {
      for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
          dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j]); 
        }
      }
    }

    ans = Integer.MAX_VALUE;
    
    dfs(0, 0);

    System.out.println(ans);
  }

  public static void dfs(int n, int mask) {
    if(n == R) {
      int s = 0;
      for(int i = 0; i < R - 1; i++) {
        s += (dp[mach[i]][mach[i + 1]]);
      }
      ans = Math.min(ans, s);
    } else {
      for(int j = 0; j < R; j++) {
        if((mask & (1 << j)) == 0) {
          mach[n] = r[j]; 
          dfs(n + 1, (mask + (1 << j)));
        }
      }
    }
  }
}

Measured memory_bytes: 45372
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;


void main()
{
  auto n = readln.chomp.to!int;
  int sum;
  foreach (i; 1..n+1) {
    sum += i;
  }
  sum.writeln;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int C[26];

int main()
{
	int N, K, res = 0;
	string P;
	cin >> N >> K;
	for (int i = 0; i < N; i++) {
		cin >> P;
		C[P[0] - 'A']++;
	}
	for (int i = 0; i < N; i++) {
		int var = 0;
		sort(C, C + 26, greater<int>());
		for (int j = 0; j < 26; j++) {
			if (C[j]) {
				C[j]--;
				var++;
				if (var == K) {
					res++;
					break;
				}
			}
		}
		if (var != K) {
			break;
		}
	}
	cout << res << endl;
	return 0;
}


Measured memory_bytes: 384
package main

// https://arc061.contest.atcoder.jp/tasks/arc061_a

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

var n int
var input []rune

/*
- 与えられた文字列長を計算する
- 文字列長-1の値を保存
- 深さ優先探索でbit全探索を行う
- bit列を引数として、そこから数式に変換し和を計算する関数を個別実装
*/
func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	str := sc.Text()

	input = []rune(str)

	n = len(str) - 1

	answer := recursion(0, 0)

	fmt.Println(answer)
}

func recursion(i, bits int) int {
	if i == n {
		return bitsToSum(bits)
	}

	on := bits | (1 << byte(i))
	off := bits
	return recursion(i+1, on) + recursion(i+1, off)
}

func bitsToSum(bits int) int {
	sum := 0

	equationStr := ""
	for i := 0; i < n; i++ {
		equationStr += string(input[i])
		bit := 1 & (bits >> byte(i))
		if bit == 1 {
			equationStr += "+"
		}
	}
	equationStr += string(input[n])

	literalStrSlice := strings.Split(equationStr, "+")
	for _, str := range literalStrSlice {
		val, _ := strconv.Atoi(str)
		sum += val
	}

	return sum
}


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

int main() {
    string s;
    cin >> s;

    reverse(s.begin(), s.end());

    int N = s.size();
    ll ans = 0;
    for (int bit = 0; bit < (1 << (N - 1)); ++bit) {
        ll now = s[0] - '0';
        ll mul = 1;
        rep(i, N - 1) {
            if (bit & (1 << i)) {
                mul = 1;
            } else {
                mul *= 10;
            }
            now += (s[i + 1] - '0') * mul;
        }
        ans += now;
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef queue<ll> ql;
typedef set<ll> sl;
typedef vector<ll> vl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, n) for(ll i = 1; i <= ll(n); i++)
//#define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
//#define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll &b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll &b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 500010;
ll num, M, N, ans=0, count=0, sum=0;
int main() {
    string S;
    cin >> S;

    if(S[2]==S[3] && S[4]==S[5]) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
using namespace std;
typedef pair<int ,int > P;
typedef long long ll;

int n;
bool ok(int a,int b,int c) {
    int s1=a*60+b;
    int s2;
    if(b<c) {
        s2=a*60+c;
    }
    else {
        s2=a*60+c;
        s2+=60;
    }
    return s2-s1<=8;
}
int f(int a,int b,int c) {
    int s1=a*60+b;
    if(11*60<=s1&&s1<=14*60+59) return 0;
    if(18*60<=s1&&s1<=20*60+59) return 1;
    if(21*60<=s1&&s1<=25*60+59) return 2;
    return 3;
}
void solve() {
    int cnt1[3]={},cnt2[3]={};
    int a,b,c;
    rep(i,n) {
        scanf("%d:%d %d",&a, &b, &c);
        if(a<3) a+=24;
        int t=f(a,b,c);
        cnt1[t]++;
        if(ok(a,b,c)&&t!=3) cnt2[t]++;
    }
    string times[3]={"lunch","dinner","midnight"};
    rep(i,3) {
        if(cnt1[i]==0) {
            printf("%s no guest\n",times[i].c_str());
        }
        else {
            printf("%s %d\n",times[i].c_str(),100*cnt2[i]/cnt1[i]);
        }
    }

}
int main() {
    while(cin>>n) {
        if(n==0) break;
        solve();
    }

    return 0;
}

Measured memory_bytes: 1248
n=int(input())
if n<=2 or n==4:
  print("No")
elif n==3:
  print("Yes")
  for i in range(5):
    print(i+1,i+2)
elif n%2==0:
  nn=n.bit_length()
  if n==2**(nn-1):
    print("No")
  else:
    print("Yes")
    print(1,2)
    print(2,3)
    print(3,n+1)
    print(n+1,n+2)
    print(n+2,n+3)
    for i in range(4,n):
      print(n+1,i)
      if i%2==0:
        print(i,i+n+1)
      else:
        print(i,i+n-1)
    n1,n2=2**(nn-1),(n+1)-2**(nn-1)
    print(n1,n)
    print(n2,2*n)
else:
  print("Yes")
  print(1,2)
  print(2,3)
  print(3,n+1)
  print(n+1,n+2)
  print(n+2,n+3)
  for i in range(4,n+1):
    print(1,i)
    if i%2==0:
      print(i,i+n+1)
    else:
      print(i,i+n-1)

Measured memory_bytes: 5892