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

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

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

bool able[21][21][21];
signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true) {
    int p, q;
    cin >> p >> q;
    if(!p) break;
    vector<string> s(p), t(q);
    REP(i, p) cin >> s[i];
    REP(i, q) cin >> t[i];

    VI ans(q, -2);
    FOR(R, 1, 21) FOR(C, 1, 21) FOR(S, 1, 21) {
      int a = 0,  b = 0, c = 0;
      bool flag = true;
      REP(i, p) {
        int tmp = -1;
        REP(j, s[i].size()) if(s[i][j]!='.') {tmp = j; break;}
        if(R*a + C*b + S*c != tmp) {
          flag = false;
          break;
        }
        REP(j, s[i].size()) {
          if(s[i][j] == '(') a++;
          else if(s[i][j] == ')') a--;
          else if(s[i][j] == '{') b++;
          else if(s[i][j] == '}') b--;
          else if(s[i][j] == '[') c++;
          else if(s[i][j] == ']') c--;
        }
      }
      if(!flag) continue;
      // ここまで来てたら(R,C,S)の組を取ることが可能
      a = b = c = 0;
      REP(i, q) {
        // i行目のインデントで a*R + b*C + c*S となる可能性がある
        // i行目の答えを設定
        if(ans[i] == -2) ans[i] = a*R + b*C + c*S;
        // 2通り以上ある(答えが一意に決まらない)ときは-1
        else if(ans[i] != a*R + b*C + c*S) ans[i] = -1;
        REP(j, t[i].size()) {
          if(t[i][j] == '(') a++;
          else if(t[i][j] == ')') a--;
          else if(t[i][j] == '{') b++;
          else if(t[i][j] == '}') b--;
          else if(t[i][j] == '[') c++;
          else if(t[i][j] == ']') c--;
        }
      }
    }

    REP(i, q) cout << ans[i] << (i==q-1?"":" ");
    cout << endl;
  }

  return 0;
}



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

int main() {
  map<string,int> num;
  int N,M;
  cin >> N;
  string s;
  for (int i=0; i<N; i++) {
    cin >> s;
    num[s]++;
  }
  cin >> M;
  for (int i=0; i<M; i++) {
    cin >> s;
    num[s]--;
  }
  int ans = 0;
  for (auto i=num.begin(); i!=num.end(); i++) {
    ans = max(i->second,ans);
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
object Main extends App {
  val in = new java.util.Scanner(System.in)
  val n, m = in.nextInt
  val xs = Array.fill(m)(in.nextInt).sorted
  val spaces = xs.zip(xs.tail).map(s => s._2 - s._1)
  println(spaces.sorted.dropRight(n - 1).sum)
}

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

bool ok(char c) {return c=='A' || c=='C' || c=='G' || c=='T';}

signed main() {
  string s; cin >> s;
  int n = s.size();
  int ans = 0;
  int l = 0;
  for(int i=0; i<n; i++) {
    if(ok(s[i])) l++;
    else {
      ans = max(ans,l);
      l=0;
    }
  }
  ans = max(ans,l);
  cout << ans << endl;
}


Measured memory_bytes: 256
N=input()
A=input().split(' ')
dict={}
for i in A:
    if i not in dict.keys():
        dict[i]=1
    else:
        dict[i]+=1
count=0
for k,v in dict.items():
    k=int(k)
    v=int(v)
    if v<k:
        count+=v
    else:
        count+=v-k
print(count)

Measured memory_bytes: 20600
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    ll n,x,cnt=0; cin>>n>>x;
    vector<ll> a(n);
    rep(i,n) cin>>a[i];
    sort(all(a));
    rep(i,n){
        x-=a[i];
        if(x<0) break;
        if(i==n-1&&x>0) break;
        cnt++;
    }
    cout<<cnt;
}

Measured memory_bytes: 3616
import java.util.*;

public class Main {

  static int N;
  static boolean[][] graph;
  static boolean[] visited;

  public static void main (String[] args) {

    Scanner sc = new Scanner(System.in);

    N = sc.nextInt();
    int M = sc.nextInt();
    graph = new boolean[N][N];

    for (int i = 0; i < M; i++) {
      int a = sc.nextInt() - 1;
      int b = sc.nextInt() - 1;
      graph[a][b] = true;
      graph[b][a] = true;
    }

    sc.close();

    visited = new boolean[N];

    // 1は訪問済み
    visited[0] = true;

    int count = dfs(0);

    System.out.println(count);

  }

  static int dfs(int v) {
 
    boolean all = true;

    // すべての点を確認する
    for (int i = 0; i < N; i++) {
      if (visited[i] == false) {
        all = false;
        break;
      }
    }

    // すべての点を通っているときは1を返す
    // ( = 答えを1増やす)
    if (all) {
      return 1;
    }

    // 答えカウント用
    int count = 0;

    // すべての点で確認する
    for (int i = 0; i < N; i++) {
      // パスが繋がっていない場合は次の点へ
      if (graph[v][i] == false) {
        continue;
      }
      // すでに訪れた点だった場合は次の点へ
      if (visited[i]) {
        continue;
      }
      // 訪れた点とする
      visited[i] = true;
      // 再帰
      count += dfs(i);
      // 訪れていない点とする
      visited[i] = false;
    }

    return count;
 
  }

}

Measured memory_bytes: 23764
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
main() {
  int N,M; cin>>N>>M;
  if(N>=M) { cout<<0<<endl; exit(0); }
  vector<int> X(M); for(auto& x:X) cin>>x;
  sort(X.begin(), X.end());
  vector<int> dist;
  for(int i=1;i<M;++i) dist.push_back(X[i]-X[i-1]);
  sort(dist.begin(), dist.end());
  vector<int> sum=dist;
  for(int i=1;i<sum.size();++i) sum[i]+=sum[i-1];
  cout<< sum[sum.size()-N] <<endl;
}

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

using namespace std;

bool possible(long long longestLog, const vector<long long>& A, long long K) {
    long long N = A.size();
    long long numOfLongestLogs = 0;
    for (long long i = 0; i < N; ++i) {
        numOfLongestLogs += A[i] / longestLog;
        if (A[i] % longestLog == 0) {
            --numOfLongestLogs;
        }
    }
    return (numOfLongestLogs <= K);
}

void Main() {
    long long N, K;
    cin >> N >> K;
    vector<long long> A(N, 0LL);
    for (long long i = 0; i < N; ++i) {
        cin >> A[i];
    }
    sort(A.begin(), A.end(), greater<long long>());

    long long ub = A.front();
    long long lb = 1LL;
    while (ub - lb > 1LL) {
        long long mid = (ub + lb) / 2LL;
        if (possible(mid, A, K)) {
            ub = mid;
        }
        else {
            lb = mid;
        }
    }
    bool test = possible(lb, A, K);
    if (test) {
        cout << lb << endl;
    }
    else {
        cout << ub << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}


Measured memory_bytes: 4840
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#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 <limits>//setprecision
#include <iomanip>
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
//#define endl "\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;


void chmax(long long &a,long long b){if(a < b) a = b;}
void chmin(long long &a,long long b){if(a > b) a = b;}

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M,long long MOD) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % MOD, M / 2,MOD);
    //When end-of-a bit is 1, times simple N.
    if (M & 1) res = (res * N) % MOD;
    return res %= MOD;
}
long long gcd(long long a, long long b) {
    if (b == 0) return a; else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}



int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N;cin >> N;
    vector<pair<long long,long long>> point(N);
    for(int i = 0;i<N;i++) cin >> point.at(i).first >> point.at(i).second;
    sort(point.begin(),point.end());
    map<pair<long long,long long>,int> M;
    for(int i = 0;i<N;i++){
        for(int j = i+1;j<N;j++){
            M[make_pair(point.at(j).first-point.at(i).first,point.at(j).second-point.at(i).second)]++;
        }
    }
    int ans = max(N-1,1);
    for(auto P : M){
        ans = min(ans,N-P.second);
    }
    cout << ans << endl;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

const int MAX = 3000;

long long 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;
    }
}
long long COM(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;
}

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

int main() {
    ll n, k;
    cin >> n >> k;
    COMinit();
    ll ans;
    rep(i, 1, k+1) {
        if(i-1 > n-k) ans = 0;
        else ans = COM(k-1, i-1) % MOD * COM(n-k+1, n-k-i+1) % MOD;
        cout << ans << endl;
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
const int mod=1e9+7;
using LL=long long;
using namespace std;
namespace ndifix{

}//end of namespace ndifix

int main(){
  int n;cin>>n;
  vector<int> p(n);for(int i=0;i<n;i++)cin>>p[i];

  bool flag=1;
  for(int i=0;i<n-1;i++)if(p[i]>p[i+1])flag=0;
  if(flag){cout<<"YES"<<endl;return 0;}
  for(int i=0;i<n;i++){
  for(int j=i+1;j<n;j++){
    flag=1;
    vector<int> a=p;
    swap(a[i],a[j]);
    for(int i=0;i<n-1;i++)if(a[i]>a[i+1])flag=0;
    if(flag){cout<<"YES"<<endl;return 0;}
  }}

  cout<<"NO"<<endl;
  return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int t;
char s[10005];     
int main(){
	scanf("%s", s);
	printf("%s", s);
	if(s[strlen(s)-1]=='s') printf("es");
	else printf("s");
	return 0;
}
	

Measured memory_bytes: 1728
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
typedef long long ll;
namespace mp = boost::multiprecision;
using Mint = mp::cpp_int;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define sort_g(v) sort(all(v), greater<int>())
#define max_v(v) *max_element(all(v));
#define min_v(v) *min_element(all(v));
#define dump(x) cerr << #x << ' ' << '=' << ' ' << x << endl
template <typename T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}
template<typename T> ostream& operator << (ostream& os, vector<T>& vec) {
    for(int i = 0; i < vec.size(); i++) {
        os << vec.at(i) << (i + 1 == vec.size() ? "" : " ");
    }
    return os;
}

int main() {
    int X, N, A;
    cin >> X >> N;
    vector<int> P(1001);
    rep(i, N) {
        cin >> A;
        P.at(A)++;
    }
    
    int ans = 0;
    int min_D = 10000;
    rep(i, 1000) {
        if(P.at(i) == 1) continue;

        int D = abs(i - X);
        if(D < min_D) {
            min_D = D;
            ans = i;
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 3620
from collections import Counter
n=int(input())
a=list(map(int, input().split()))
dic=Counter(a)
ans=0
if n==1:
  print('1')
elif n==2:
  if any([a[0]==a[1],a[0]==a[1]-1,a[0]==a[1]+1]):
    print('2')
  else:
    print('1')
else:
  for i in range(n-2):
    ans=max(ans,dic[i]+dic[i+1]+dic[i+2])
  print(ans)

Measured memory_bytes: 14564
#include <stdio.h>
#include <string.h>
#include <math.h>

int a[410][410],b[410][410];
int xmax,ymax,xmin,ymin;

void init();
void serch(int x,int y);

int main(){
	int N;
	int n,d;
	int i,j,k,l,m,c;
	int x,y;
	while(1){
		scanf("%d",&N);
		if(N==0)break;
		x=200;
		y=200;
		init();
		a[x][y]=0;
		for(i=1; i<N; i++){
			scanf("%d %d",&n,&d);
			c=1;
			for(j=0; j<410; j++){
				for(k=0; k<410; k++){
					if(a[j][k]==n){
						if(d==0)a[j-1][k]=i;
						if(d==1)a[j][k-1]=i;
						if(d==2)a[j+1][k]=i;
						if(d==3)a[j][k+1]=i;
						c=0;
						break;
					}
				}
				if(!c)break;
			}
		}
		serch(x,y);
		printf("%d %d\n",xmax-xmin+1,ymax-ymin+1);
		//printf("xmax:%d xmin:%d ymax:%d ymin%d\n",xmax,xmin,ymax,ymin);
	}
	return 0;
}


void init(){
	int i,j;
	for(i=0; i<410; i++){
		for(j=0; j<410; j++){
			a[i][j]=-1;
			b[i][j]=0;
			xmax=200;
			xmin=200;
			ymax=200;
			ymin=200;
		}
	}
}

void serch(int x,int y){
	if(a[x][y]<0||b[x][y]==1)return;
	b[x][y]=1;
	if(x>xmax)xmax=x;
	if(x<xmin)xmin=x;
	if(ymax<y)ymax=y;
	if(ymin>y)ymin=y;

	//printf("x:%d y:%d xmax:%d ymax:%d\n",x,y,xmax,ymax);

	if(x-1>=0)serch(x-1,y);
	if(y-1>=0)serch(x,y-1);
	if(x+1<410)serch(x+1,y);
	if(y+1<410)serch(x,y+1);
}

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

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
  cin >> s;
  cout << 700 + 100 * count(s.begin(), s.end(), 'o') << '\n';
    return 0;
}


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

#K と K の間で K を作れれば、数列は存在する。  
#絶対に作れないパターン
if K >= 2 ** M:
  print("-1")
  quit()
if M == 0:
  print(0, 0)
  quit()
elif M == 1:
  if K == 0:
    print(0, 0, 1, 1)
  else:
    print(-1)
  quit()
#K と K の間で K を作れれば、数列は存在する。  
  
now = 0  
for i in range(2 ** M):
  if i != K:
    now = now^i
  #print(now)
#print(now)

print(K, end = " ")
for i in range(2 ** M):
  if i != K:
    print(i, end = " ")
print(K, end = " ")
for i in range(2 ** M):
  if (2 ** M - 1 - i) != K:
    print(2 ** M - 1 - i, end = " ")
  
  
  

  
  

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

int main2() {
  ll N = nextLong();
  ll ans = N*(N+1)*(N+2)/6;
  REP(i, N-1) {
    int a = nextLong() - 1;
    int b = nextLong() - 1;
    if (a > b) swap(a, b);
    ans -= (ll)(a + 1) * (N - b);
  }
  cout << ans << endl;
  return 0;
}
int main() {
  
#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}

Measured memory_bytes: 3760
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<numeric>
#include<queue>
#include<stack>
#include<cstring>
#include<limits>
#include<functional>
#include<unordered_set>
#include<iomanip>
#include<cassert>
#include<regex>
#include<bitset>
#include<chrono>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define pb push_back
#define eb emplace_back
using ll=long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 50;
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
 
using namespace std;
 
template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " <<to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
 
 ll gcd(ll n, ll m) {
	ll tmp;
	while (m!=0) {
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m) {
	return abs(n * m) / gcd(n, m);//gl=xy
}

void solve(){
    ll a,b;
    cin>>a>>b;
    cout<<lcm(a,b)<<"\n";
}
 
int main(){
	ios::sync_with_stdio(false);
    cin.tie(0);
	cout<<fixed<<setprecision(15);
	solve();
	return 0;
}

Measured memory_bytes: 256
def addbit(i):
    i += 1
    while i <= N:
        BIT[i] += 1
        i += i & (-i)
def getsum(i):
    ret = 0
    i += 1
    while i != 0:
        ret += BIT[i]
        i -= i&(-i)
    return ret
def bit(L):
    global BIT
    BIT=[0] * (N+1)
    re = 0
    for l in L:
        re += getsum(l)
        addbit(l)
    return N*(N-1)//2 - re
def get_rank(L):
    D = {x:i for i,x in enumerate(sorted(L))}
    return [D[x] for x in L]
def calc(Z):
    Z.sort()
    l, r, lc, rc = -1<<28, 1<<28, N*(N-1)//4+1, N*(N-1)//4+1
    while (r - l) / max(min(abs(l), abs(r)), 1) > 0.000000002:
        m = (l * rc + r * lc) / (lc + rc)
        A = get_rank([s*m+t for s, t in Z])
        c = bit(A)
        if c > N*(N-1)//4:
            l = m
            lc = ((c - N*(N-1)//4) * 2) ** 0.3
        else:
            r = m
            rc = ((N*(N-1)//4 - c) * 2 + 1) ** 0.3
    return (l+r) / 2

N = int(input())
X, Y = [], []
for _ in range(N):
    a, b, c = map(int, input().split())
    X.append((-a/b, c/b))
    Y.append((-b/a, c/a))
print(calc(X), calc(Y))

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

int main() {
    int N;
    scanf("%d", &N);

    int *H = malloc((N + 1) * sizeof(int));
    int *DP = malloc((N + 1) * sizeof(int));
    for (int i = 1; i <= N; i++) {
        scanf("%d", &H[i]);
    }

    DP[1] = H[1] - 1; // 減らしておいた方が可能性がある

    for (int i = 2; i <= N; i++) {
        // 単調減少になるかどうか
        if (DP[i - 1] > H[i]) {
            printf("No\n");
            return 0;
        } else if (DP[i - 1] == H[i]) {
            DP[i] = H[i];
        } else {
            DP[i] = H[i] - 1;
        }
    }

    printf("Yes\n");
    return 0;
}


Measured memory_bytes: 896
#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;

string S;

int main(void) {
  cin >> S;

  bool ans = false;
  REP(i, 0, 10) {
    ll cnt1 = 0;
    REP(j, 0, 3) if(S[j] == ('0' + i)) cnt1++;
    if(cnt1 == 3) ans = true;
    ll cnt2 = 0;
    REP(j, 1, 4) if(S[j] == ('0' + i)) cnt2++;
    if(cnt2 == 3) ans = true;
  }

  cout << (ans ? "Yes" : "No") << endl;
}


Measured memory_bytes: 256
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ld long double
#define al(a) (a).begin(),(a).end()
#define mk make_pair
#define check cout<<"?"<<endl;

ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;

int main(){
    int n; cin>>n;
    vector<pp> x(n);
    vector<int> c(n,0),ans(n*n,0);
    rep(i,n){
        c[i]=i;
        int a; cin>>a;
        x[i]=mk(a,i);
    }
    sort(al(x));
    int idx=0,cur=0;
    queue<int> que;
    rep(i,n*n){
        if(cur<n && x[cur].first-1==i){
            if(c[x[cur].second]>0){
                cout<<"No"<<endl;
                return 0;
            }
            ans[i]=x[cur].second+1;
            rep(j,n-(x[cur].second+1)) que.push(x[cur].second+1);
            cur++;
        }
        else{
            while(idx<n && c[x[idx].second]==0) idx++;
            if(idx==n){
                if(que.size()==0){
                    cout<<"No"<<endl;
                    return 0;
                }
                ans[i]=que.front();
                que.pop();
            }
            else{
                ans[i]=x[idx].second+1;
                c[x[idx].second]--;
            }
        }
    }
    cout<<"Yes"<<endl;
    rep(i,n*n) cout<<ans[i]<<" ";
    cout<<endl;
}


Measured memory_bytes: 4816
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main()
{
    int s;
    cin >> s;
    set<int> st;
    int x = s;
    while(st.count(x) == 0){
        st.insert(x);
        if(x % 2 == 0){
            x /= 2;
        }
        else{
            x = x * 3 + 1;
        }
    }
    cout << (st.size() + 1) << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#define WIDTH 600
using namespace std;

int func(int x,int d){
	return x*x*d;
}

int main(){
	int d;
	while(cin>>d){
		unsigned int S=0;
		for(int i=0;i<WIDTH;i+=d){
			S+=func(i,d);
		}
		cout<<S<<endl;
	}

}

Measured memory_bytes: 1156
#include "bits/stdc++.h"
using namespace std;
 
int main() {
    int b1, b2, b3;
    cin >> b1 >> b2 >> b3;
 
    int sum = b1;
    sum += b2;
    sum += b3 * 2;
    if (sum >= 2) {
        cout << "Open" << endl;
    } else cout << "Close" << endl;
}


Measured memory_bytes: 3100
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int main() {
	long long N, X;
	cin >> N >> X;
	vector<long long> x(N);
	vector<long long> val(N);
	for (long long i = 0; i < N; i++)cin >> x[i];
	sort(x.begin(), x.end());
	val[0] = abs(X - x[0]);
	if (N > 1) for (long long i = 1; i < N; i++)val[i] = x[i] - x[i - 1];
	sort(val.begin(), val.end());
	bool flag1,flag2;
	long long ans = 1;
	for (long long i = 1; i <= sqrt(val[0]); i++) {
		flag1 = true;
		flag2 = true;
		for (long long j = 0; j < N; j++) {
			if (val[j] % i != 0) flag1 = false;
			if (val[j] % (val[0] / i) != 0) flag2 = false;
		}
		if (flag1 == true)ans = max(ans,i);
		if (flag2 == true)ans = max(ans, val[0] / i);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1792
use std::io::Read;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    let answer = solve(&buf);

    println!("{}", answer);
}

fn solve(input: &str) -> String {
    let mut iterator = input.split_whitespace();

    const MAX: usize = 100_000;
    let primes = enumerate_primes(MAX);
    let mut s: Vec<usize> = vec![0; MAX + 1];
    let mut i = 3;
    while i <= MAX {
        if primes.contains(&i) && primes.contains(&((i + 1) / 2)) {
            s[i] = s[i - 2] + 1;
        } else {
            s[i] = s[i - 2];
        }
        i += 2;
    }

    let q: usize = iterator.next().unwrap().parse().unwrap();
    let mut result = String::new();
    for _ in 0..q {
        let l: usize = iterator.next().unwrap().parse().unwrap();
        let r: usize = iterator.next().unwrap().parse().unwrap();

        let sl = if l == 1 {
            0
        } else {
            s[l - 2]
        };
        let sr = s[r];
        result.push_str(&format!("{}\n", sr - sl));
    }
    return result.trim().to_string();
}

fn enumerate_primes(n: usize) -> std::collections::HashSet<usize> {
    let mut primes = std::collections::HashSet::new();
    for i in 2..(n + 1) {
        primes.insert(i);
    }
    let mut i = 2;
    while i * i <= n {
        if primes.contains(&i) {
            let mut j = i * 2;
            while j <= n {
                primes.remove(&j);
                j += i;
            }
        }
        i += 1;
    }
    return primes;
}


Measured memory_bytes: 10492
import math
n,m = map(int, input().split())
a = math.factorial(m)
b = math.factorial(n)
if 1 < abs(n-m):
    print(0)
elif abs(n-m) == 0:
    print((a*b*2)%1000000007)
elif abs(m-n) == 1:
    print((a*b)%1000000007)

Measured memory_bytes: 11040
// 去吧！皮卡丘! 把AC带回来！
//			へ　　　　　／|
// 　　/＼7　　　 ∠＿/
// 　 /　│　　 ／　／								AC		AC		AC		AC
//		│　Z ＿,＜　／　　 /`ヽ						AC		AC		AC		AC
//		│　　　　　ヽ　　 /　　〉					AC		AC		AC		AC
// 　Y　　　　　`　 /　　/	/						AC		AC		AC		AC
// 　亻●　.　●　　〈　/　/						AC		AC		AC		AC
// 　()　 へ　　　　|　＼〈							AC		AC		AC		AC
// 　　>- ._　 ィ　 │ ／／							AC		AC		AC		AC
// 　 / へ　　 /　/＜| ＼＼							AC		AC		AC		AC
// 　 ヽ_/　　(_／　 │／／							AC		AC		AC		AC
// 　　 7　　　　　　　|／							AC		AC		AC		AC
// 　　 ＞―r￣￣`-―＿/							AC		AC		AC		AC
//**************************************Accepted*****************************************//

#include<bits/stdc++.h>

#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define Unique(x) x.erase(unique(all(x)),x.end());

using namespace std;

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

const int Days[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};

inline int read()
{
	#define gc getchar
	
	int ans=0;
	bool f=1;
	char ch=gc();
	
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	
	while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
	
	return f?ans:-ans;
	
	#undef gc
}

typedef long long ll;

int n,k,c;
char s[200010];
int lwork[200010],rwork[200010];

int main(int argc,char const *argv[])
{
	ios::sync_with_stdio(false);
	
	scanf("%d%d%d",&n,&k,&c);
	scanf("%s",s);
	
	for(int i=0;i<n;i++)
	{
		int add=(s[i]=='o');
		
		if(i==0)lwork[i]=add;
		else if(i<c+1)lwork[i]=max(lwork[i-1],add);
		else lwork[i]=max(lwork[i-1],lwork[i-c-1]+add);
	}
	for(int _i=0;_i<n;_i++)
	{
		int i=n-1-_i;
		int add=(s[i]=='o');
		
		if(_i==0)rwork[i]=add;
		else if(_i<c+1)rwork[i]=max(rwork[i+1],add);
		else rwork[i]=max(rwork[i+1],rwork[i+c+1]+add);
	}
	for(int i=0;i<n;i++)
	{
		int works=0;
		
		if(i>0)works+=lwork[i-1];
		if(i<n-1)works+=rwork[i+1];
		if(works<k)printf("%d\n",i+1);
	}
	
	//printf("Time used=%.12f",(double)(clock())/CLOCKS_PER_SEC);
	return 0;
}

/*	GOD BLESS	*/


Measured memory_bytes: 3328
#define _GLIBCXX_DEBUG
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

void solve() {
    int cnt[26] = {0};
    char s;
    while (cin >> s) {
        if ('A' <= s && s <= 'Z') s = tolower(s);
        if ('a' <= s && s <= 'z') cnt[s - 'a']++;
    }
    for (int i = 0; i < 26; i++) {
        cout << char('a' + i) << " : " << cnt[i] << "\n";
    }
}
int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}


Measured memory_bytes: 3212
def resolve():
    '''
    code here
    '''
    N = int(input())

    memo = [0 for _ in range(N+1)]
    memo[:2] = [2, 1]

    for i in range(2, N+1):
        memo[i] = memo[i-1] + memo[i-2]

    print(memo[N])

if __name__ == "__main__":
    resolve()


Measured memory_bytes: 9120
#include "bits/stdc++.h"
using namespace std;
const int maxn = 100100;
long long a[maxn], b[maxn], c[maxn];
long long bestX[maxn], bestY[maxn];
int M(){
    int x, y, z;
    if(!~scanf("%d %d %d", &x, &y, &z))
        return 0;
    for(int e = 0; e < x + y + z; e++)
        scanf("%lld %lld %lld", a + e, b + e, c + e);
    vector<int> order;
    for(int e = 0; e < x + y + z; e++)
        order.push_back(e);
    sort(order.begin(), order.end(), [&](int u, int v){
        return a[u] - b[u] < a[v] - b[v];
    });
    multiset<long long> forY;
    long long sumY = 0;
    for(int e = 0; e < y; e++)
        sumY += b[order[e]] - c[order[e]], forY.insert(b[order[e]] - c[order[e]]);
    bestY[y-1] = sumY;
    for(int e = y; e < (x + y + z); e++){
        long long cur = b[order[e]] - c[order[e]];
        long long mi = * forY.begin();
        if(cur > mi){
            forY.erase(forY.begin());
            sumY -= mi;
            sumY += cur;
            forY.insert(cur);
        }
        bestY[e] = sumY;
    }
    multiset<long long> forX;
    long long sumX = 0;
    for(int e = (x + y + z) - 1; e >= y + z; e--)
        sumX += a[order[e]] - c[order[e]], forX.insert(a[order[e]] - c[order[e]]);
    bestX[y + z] = sumX;
    for(int e = y + z - 1; e >= 0; e--){
        long long cur = a[order[e]] - c[order[e]];
        long long mi = * forX.begin();
        if(cur > mi){
            forX.erase(forX.begin());
            sumX -= mi;
            sumX += cur;
            forX.insert(cur);
        }
        bestX[e] = sumX;
    }
    long long ans = -(1ll<<60);
    for(int e = y-1; e < y + z; e++)
        ans = max(ans, bestY[e] + bestX[e + 1]);
    for(int e = 0; e < (x + y + z); e++)
        ans += c[e];
    cout << ans << endl;
    return 1;
}
int main(){
    while(M());
    return 0;
}


Measured memory_bytes: 8312
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String s = scanner.next();
		int cnt = 0;
		int a = 0;
		int b = 0;
		int c = 0;
		
		for (int i = 0; i < 3; i++) {
			if (s.charAt(i) == 'a') {
				a++;
			}
			if (s.charAt(i) == 'b') {
				b++;
			}
			if (s.charAt(i) == 'c') {
				c++;
			}
		}
		
		if (a == 1 && b == 1 && c == 1) {
			System.out.println("Yes");
		}else {
			System.out.println("No");
		}
		scanner.close();

	}

}


Measured memory_bytes: 21844
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<iomanip>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1e9+7
#define LINF (long long)4e18


int main(){
    int N,K;cin >> N >> K;
    vector<int> h(N);
    rep(i,N) cin >> h[i];
    vector<int> dp(N+100,INF);
    dp[0] = 0;
    int ans = 0;
    rep(i,N){
        repr(j,1,K+1){
            dp[i+j] = min(dp[i+j],abs(h[i+j] - h[i]) + dp[i]);
        }
    }
        ans = dp[N-1];
    cout << ans << endl;
}

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

int main (int argc,char **argv)
{
	char n[5];
	scanf("%s",n);

	

	if((n[0] == n[1]) && (n[1] == n[2]))
	{
		printf("Yes\n");
	}

	else if((n[1] == n[2]) && (n[2] == n[3]))
	{
		printf("Yes\n");
	}

	else if((n[0] == n[1]) && (n[1] == n[2]) && (n[2] == n[3]))
	{
		printf("Yes\n");
	}

	else 
	{
		printf("No\n");
	}

	return 0;
}

Measured memory_bytes: 128
<?php

// 読み取り
// echo "Please enter N X Y:";
fscanf(STDIN, "%d %d %d", $N, $X, $Y);

// 各kの時の組み合わせ数を格納する配列を作成する
// key: k
// value: k=keyの時の組み合わせ数
$combination_at_k = array_fill(1, $N-1, 0);

// 各kについて組み合わせ数を算出する
for($k=1;$k<$N;$k++){
  // ホップ元を掃引
  for($start=1;$start<$N-$k+1;$start++){
    // ホップ先
    $end = $start + $k;
    $true_k = abs($X-$start) + 1 + abs($end-$Y);
    if($true_k < $k){
      $combination_at_k[$true_k]++;
    }
    else {
      $combination_at_k[$k]++;
    }
  }
}

foreach ($combination_at_k as $k => $c_num) {
  // code...
  echo $c_num . "\n";
}

?>

Measured memory_bytes: 3828
#include <iostream>
#include <iomanip>
#include <ios> 
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <bitset>
#include <map>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define sz(c) ((int)(c).size())

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
//const int MAX=;

bool mo(int a){
	if(1<=a && a<=12)return true;
	else return false;
}

int main(){
	int n;
	cin>>n;
	int a=n/100;
	int b=n%100;
	
	if(mo(a)  && mo(b) )cout<<"AMBIGUOUS";
	if(!mo(a) && mo(b) )cout<<"YYMM";
	if(mo(a)  && !mo(b))cout<<"MMYY";
	if(!mo(a) && !mo(b))cout<<"NA";
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<functional>
#include<math.h>

#define rep(i,n) for(ll i=0;i<(n);i++)
typedef long long ll;
using namespace std;

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

	for (int i = a; i <= b; i++) {
		if (i%k == 0) {
			cout << "OK\n";
			return 0;
		}
	}
	cout << "NG\n";

	return 0;
}

Measured memory_bytes: 3624
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;
typedef long long ll;

#define PI 3.141592653

int main(){
    int n;
    double x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4,slope_1,slope_2;
    scanf("%d",&n);
    
    for(int i=0;i<n;i++){
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x_1,&y_1,&x_2,&y_2,&x_3,&y_3,&x_4,&y_4);
        
        if(x_1==x_2){
            
            if(x_3==x_4)
                puts("YES");
            else
                puts("NO");
            
            continue;
        }
        
        slope_1=(y_1-y_2)/(x_1-x_2);
        slope_2=(y_3-y_4)/(x_3-x_4);
        
        if(abs(slope_1-slope_2)<1e-7)
            puts("YES");
        else
            puts("NO");
    }
    
    return 0;
}

Measured memory_bytes: 1204
#include <bits/stdc++.h>
typedef long long ll;
const int INF=1e9,MOD=1e9+7,ohara=1e6;
const ll LINF=1e18;
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(int i=(a);i<(b);i++)
#define rrrep(i,a,b) for(int i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define Size(n) (n).size()
#define Cout(x) cout<<(x)<<endl

ll n,cnt,ans,a[ohara],b,c,d,tmp,tmpp,m,h,w,x,y,sum,pos,al[ohara];
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"),s;
bool fl;
struct edge{int to,cost;};
unordered_map<ll,ll> mp;

//-------------------------↓↓↓↓↓↓------------------------

int main(void){
       cin.tie(0);
    ios::sync_with_stdio(false);
      
    cin>>n;
    set<ll> tmp;
    rep(i,n){
        cin>>a[i];
        mp[a[i]]=i+1;
    }
    tmp.insert(n+1);
    tmp.insert(0);
    rrep(i,1,n+1){ 
       auto iti=tmp.upper_bound(mp[i]);
       ll ri=*iti;
       iti--;
       ll le=*iti;
       ri-=mp[i]+1;
       le=mp[i]-le;
       le--;
       ans+=((le+1)*(ri+1))*i;
       tmp.insert(mp[i]);
    }
    Cout(ans);
    return 0;
}


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

#define MP make_pair
#define FF first
#define SS second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pie;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll LINF = 4e18;
const ll delta = 1634569;

ll n, ans;

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	if (n == 2)
		return cout << 0 << endl, 0;
	for (ll i = 2; i * i <= n; i++)
		if (n % i == 0) {
			set<ll> t;
			t.insert(i);
			t.insert(n / i);
			for (ll m : t)
				if (n / m == n % (m - 1))
					ans += m - 1;
		}
	cout << ans + n - 1 << endl;
}



Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;
using int64 = long long;

int main() {
    int k, sum = 0;
    cin >> k;

    for (int i=1; i<=k; i++)
        for (int j=1; j<=k; j++)
            for (int h=1; h<=k; h++)
                sum += gcd(gcd(i, j), h);

    cout << sum << endl;
}


Measured memory_bytes: 3160
import qualified Data.ByteString.Char8 as B
import qualified Data.IntSet as S
import qualified Data.Array.IArray as A
import Data.Maybe
import Data.Bool

main = do
  [n,_] <- map read . words <$> getLine
  es <- readUndirectedEdge <$> B.getContents
  let g = buildG n es
  putStrLn $ bool "IMPOSSIBLE" "POSSIBLE" (voyage n g)

voyage n g = S.foldl' (\acc x -> S.member n (from g x) || acc) False (from g 1)

type Vertex = Int
type Vertexes = S.IntSet
type Edge = (Vertex, Vertex)
type Graph = A.Array Vertex Vertexes

buildG :: Int -> [Edge] -> Graph
buildG n = A.accumArray (flip S.insert) S.empty (1, n)

from :: Graph -> Vertex -> Vertexes
from g = (g A.!)

readUndirectedEdge :: B.ByteString -> [Edge]
readUndirectedEdge = concatMap (constructTwoWay . map readInt . B.words) . B.lines

constructTwoWay :: [Int] -> [Edge]
constructTwoWay [s,t] = [(s, t), (t, s)]
constructTwoWay _ = undefined

readInt :: B.ByteString -> Int
readInt = fst . fromJust . B.readInt


Measured memory_bytes: 57212
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<utility>
#include<queue>
#include <map>

using namespace std;



int main(){

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

	map<int, int> map;
	int flag=0;
	for(int i=0;i<n;i++){
		if(map[a[i]]==0)map[a[i]]=1;
		else flag=1;
	}
	if(flag==1)cout<<"NO";
	else cout<<"YES";


	cout<<endl;

	return 0;
}


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

using namespace std;

int main(int argc, char *argv[]) {
	int N;
	cin >> N;

	vector<int> buttons;
	int button;
	for (int i = 0; i < N; i++) {
		cin >> button;
		buttons.push_back(button);
	}

	std::set<int> already;
	int next = 0;
	int answer = 0;
	for (int j = 0; j < N; j++) {
		if (j == 0) {
			next = buttons[0];
			already.insert(1);
		} else {
			next = buttons[next - 1];
			if (already.find(next) != already.end()) {
				answer = -1;
				break;
			}
			already.insert(next);
		}
		if (next == 2) {
			answer = j + 1;
			break;
		}
	}

	cout << answer << endl;
	return 0;
}


Measured memory_bytes: 5368
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#define inf 1<<30
#define ll long long
#define db double
#define c233 cout<<"233"<<endl
#define mem(s) memset(s,0,sizeof(s))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
const int N=10050;
const int M=20050;
using namespace std;

bool v[N];
int nxt[M*2],to[M*2],w[M*2],co[M*2],head[N],dis[N],lastco[N];
int n,m,ans,maxe;

void fre(){
  freopen("in","r",stdin);
  freopen("out","w",stdout);
}

inline int gi(){
  int x=0,res=1;char ch=getchar();
  while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}
  while(ch<='9'&&ch>='0')x=(x<<1)+(x<<3)+ch-48,ch=getchar();
  return x*res;
}

void clear(){
  ans=maxe=0;
  mem(head);
  mem(lastco);
}

void build(int a,int b,int c,int d){
  nxt[++maxe]=head[a];to[maxe]=b;
  co[maxe]=d;w[maxe]=c;head[a]=maxe;
}

void init(){
  for(int i=1;i<=m;i++){
    int a=gi(),b=gi(),c=gi(),d=gi();
    build(a,b,c,d);
    build(b,a,c,d);
  }
}

void spfa(){
  queue<int> q;
  while(!q.empty())q.pop();
  for(int i=1;i<=n;i++)dis[i]=inf,v[i]=0;
  q.push(1);dis[1]=0;v[1]=1;
  while(!q.empty()){
    int x=q.front();q.pop();v[x]=0;
    for(int i=head[x];i;i=nxt[i]){
      int u=to[i];
      if(dis[u]==dis[x]+w[i])
	lastco[u]=min(lastco[u],co[i]);
      if(dis[u]>dis[x]+w[i]){
	dis[u]=dis[x]+w[i];
	lastco[u]=co[i];
	if(!v[u])q.push(u),v[u]=1;
      }
    }
  }
}

void work(){
  spfa();
  for(int i=1;i<=n;i++)
    ans+=lastco[i];
  printf("%d\n",ans);
}

int main(){
  //fre();
  while(1){
    clear();
    n=gi();m=gi();
    if(m==0&&n==0)break;
    init();
    work();
  }
  return 0;
}

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

#define A first
#define B second

const int M = 3e5+5;

ll x[M], y[M], n;
vector<pii> v;
ll ans = 0;

void comp() {
	ll sum = 0;
	int l = 0, r = (int)v.size()-1;

	for (int i = 0; i <= n; ++i) {
		//advance left counter
		while (l < v.size() && v[l].B != 1)
			l++;
		while (r >= 0 && v[r].B != -1)
			r--;
		if (l == v.size() || r == -1)
			break;
		if (l > r)
			break;
		sum -= 2*v[l].A;
		sum += 2*v[r].A;
		l++;
		r--;
		ans = max(ans, sum);
	}
}

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

	for (int i = 0; i < n; ++i) {
		v.push_back(pii(x[i], -1));
		v.push_back(pii(y[i], 1));
	}

	v.push_back(pii(0, -1));
	sort(v.begin(), v.end());

	comp();

	v.clear();
	for (int i = 0; i < n; ++i) {
		v.push_back(pii(x[i], -1));
		v.push_back(pii(y[i], 1));
	}

	v.push_back(pii(0, 1));
	sort(v.begin(), v.end());
	comp();

	cout << ans << endl;
}

Measured memory_bytes: 9328
#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n];
    for(int i = 0;i<n;i++)
    {
        cin>>a[i];
    }
    int L[n];
    int R[n];
    L[0] = a[0];
    for(int i=1;i<n;i++)
    {
        L[i] = a[i]^L[i-1];
    }
    R[n-1] = a[n-1];

    for(int i = n-2;i>=0;i--)
    {
        R[i] = a[i]^R[i+1];
    }

    for(int i = 0;i<n;i++)
    {
        a[i] = L[i]^R[i];
    }
    for(int i = 0;i<n;i++)
    {
        cout<<a[i]<<" ";
    }


}


Measured memory_bytes: 5908
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    static class BTAKOYAKIFESTIVAL2019
    {
        static void Main(string[] args)
        {
            // 2 ≤ n ≤ 50
            var n = int.Parse(Console.ReadLine());
            // 0 ≤ d ≤ 100
            var dList = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();

            int answer = 0;

            for (int i = 0; i < dList.Count; i++)
            {
                for (int j = 0; j < dList.Count; j++)
                {
                    if (j <= i) { continue; }
                    answer += dList[i] * dList[j];
                }
            }

            Console.WriteLine(answer);
            Console.ReadLine();
        }
    }
}

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

#define ll long long int 
#define MOD 1000000007
#define sc(a) scanf("%lld",&a)
#define f0(n) for(ll i=0;i<n;i++)
#define f1(n) for(ll i=1;i<=n;i++)
#define pb(v,a) v.push_back(a)
#define pbp(v,a,i) v.push_back(make_pair(a,i)) 
#define srt(v) sort(v.begin(),v.end());

int arr[200];

int main()
{
	int n,k,a,b,cn=0;
	cin>>n>>k;
	for(int i=1;i<=k;i++)
	{
		cin>>a;
		while(a--)
		{
			cin>>b;
			arr[b]=arr[b]+1;
		}
	}
	for(int i=1;i<=n;i++)
		if(arr[i]==0)
			cn=cn+1;

	cout<<cn<<endl;
}

Measured memory_bytes: 3640
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;

const int U = 0;
const int D = 1;
const int L = 2;
const int R = 3;

vector<int> x_0[4];
vector<int> y_0[4];

vector<int> x_1[4];
vector<int> y_1[4];

vector<int> x_2[4];
vector<int> y_2[4];

int mi = LLONG_MAX;

set<int> s[600001];

void func(vector<int>& fst_base, vector<int>& fst_n, vector<int> & snd_base, vector<int>& snd_n, bool is_change)
{
	for (int i = 0; i < 600001; i++)
	{
		s[i].clear();
	}

	for (int i = 0; i < fst_n.size(); i++)
	{
		s[fst_base[i]].insert(fst_n[i]);
	}

	for (int i = 0; i < snd_base.size(); i++)
	{
		auto it = s[snd_base[i]].upper_bound(snd_n[i]);
		if (it != s[snd_base[i]].end())
		{
			int sub = *it - snd_n[i];
			//if (is_change)
			//{
			//	sub = sub * 10 / 4;
			//}
			//else
			//{
			//	sub = sub * 10 / 2;
			//}
			sub = sub * 10 / 2;
			mi = min(mi, sub);
		}
	}
}

signed main()
{
	cin >> N;
	REP(i, N)
	{
		int x, y;
		char c;
		cin >> x >> y >> c;
		if (c == 'U') { x_0[U].push_back(x); y_0[U].push_back(y); }
		if (c == 'D') { x_0[D].push_back(x); y_0[D].push_back(y); }
		if (c == 'L') { x_0[L].push_back(x); y_0[L].push_back(y); }
		if (c == 'R') { x_0[R].push_back(x); y_0[R].push_back(y); }
	}

	for (int d = 0; d < 4; d++)
	{
		for (int i = 0; i < x_0[d].size(); i++)
		{
			x_1[d].push_back(x_0[d][i] + y_0[d][i] + 200000);
			y_1[d].push_back(- x_0[d][i] + y_0[d][i] + 200000);
		}
		for (int i = 0; i < x_0[d].size(); i++)
		{
			x_2[d].push_back(x_0[d][i] - y_0[d][i] + 200000);
			y_2[d].push_back(x_0[d][i] + y_0[d][i] + 200000);
		}
	}

	func(x_0[D], y_0[D], x_0[U], y_0[U], false);
	func(y_0[L], x_0[L], y_0[R], x_0[R], false);

	func(x_1[R], y_1[R], x_1[U], y_1[U], true);
	func(x_1[D], y_1[D], x_1[L], y_1[L], true);

	func(x_2[L], y_2[L], x_2[U], y_2[U], true);
	func(x_2[D], y_2[D], x_2[R], y_2[R], true);
	
	if (mi < LLONG_MAX)
	{
		cout << mi << endl;
	}
	else
	{
		cout << "SAFE" << endl;
	}
}



Measured memory_bytes: 36316
import itertools as z;n=int(input());p=list(z.combinations([list(map(int, input().split())) for i in range(n)],2));a=0
for c,d in p:a+=((c[0]-d[0])**2+(c[1]-d[1])**2)**0.5
print(a*(n-1)/len(p))

Measured memory_bytes: 38256