akhauriyash/Code-Regression huggingface dataset:
Start = 0
Goal = 1

def solve degree, max
  if degree[Start] % 2 == 1 && degree[Goal] % 2 == 1
    2.upto(max) do |i|
      return false if degree[i] % 2 == 1
    end
    true
  else
    false
  end
end

degree = Array.new(101,0)
max = -1
while input = $stdin.gets
  a,b = input.chomp.split(" ").map(&:to_i)
  unless (a|b) == 0
    degree[a - 1] += 1
    degree[b - 1] += 1
    max = a - 1 if a > max
    max = b - 1 if b > max
  else
    puts(solve(degree, max)? "OK" : "NG")
    degree = Array.new(101,0)
    max = -1
  end
end

Measured memory_bytes: 6104
#include <bits/stdc++.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long ll;
ll mod=1000000007;
ll BIG=1e9;

ll d[10][10];
vector<int> num(10);
int main(){
    int H,W; cin>>H>>W;
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++) cin>>d[i][j];
    }
    

    for(int k=0;k<10;k++){
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
        }
    }

    for(int i=0;i<H*W;i++){
        int x; cin>>x;
        if(x!=-1) num.at(x)++;
    }

    ll ans=0;
    for(int i=0;i<10;i++){
        ans+=num.at(i)*d[i][1];
    }

    cout<<ans<<endl;



}

Measured memory_bytes: 256
def main():
    from fractions import gcd
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a = [i//2 for i in a]
    g = a[0]

    s = set()
    for i in a:
        j = i
        cnt = 0
        while j % 2 == 0:
            j //= 2
            cnt += 1
        s.add(cnt)
    if len(s) >= 1:
        if len(s) == 1:
            for i in range(n-1):
                g = (g*a[i+1])//gcd(g, a[i+1])
            print((m//g+1)//2)
            return
        else:
            print(0)
            return
    for i in range(n-1):
        g = (g*a[i+1])//gcd(g, a[i+1])

    print((m//g+1)//2)


main()

Measured memory_bytes: 89580
n, y = map(int, input().split())

for a in range(n + 1):
    for b in range(n + 1 - a):
        if a * 10000 + b * 5000 + (n - a - b) * 1000 == y :
            print(a, b, (n - a - b))
            exit()
print(-1, -1, -1)

Measured memory_bytes: 3060
#pragma warning(disable:4786)
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <functional>
#include <utility>
#include <map>

using namespace std;

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

typedef long long ll;
typedef pair<int, int> P;
#define mp make_pair


const int MOD = 1000000007;

int main() {
	int N;
	string s, t;
	cin >> N >> s >> t;
	int i;
	for (i = N; i > 0; i--) {
		bool flag = true;
		rep(j, i) {
			if (s[N - i + j] != t[j]) {
				flag = false;
				break;
			}
		}
		if (flag) {
			break;
		}
	}
	cout << 2 * N - i << endl;
	return 0;
}

Measured memory_bytes: 256
main = getLine >>= print . yamanote . map read . words

yamanote [a,b,c] = circular 0 True
 where
  circular m False
   | mod (m+b) 60 == 0 = -1
   | otherwise = circular (m+b) True
  circular m True
   | (mod m 60) <= c && c <= (mod m 60) + a = 60 * (div m 60) + c
   | otherwise = circular (m+a) False

Measured memory_bytes: 3616
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
#define int long long
bool used[2000000];

int f(int n){

   fill((bool*)used, (bool*)used+2000000, 0);
   used[n]=1;
   int cnt=0;
   while(n>=10){
      string s=to_string(n);
      int nex=0;
      for(int i=1; i<s.size(); i++){

         nex=max<int>(nex, stoll(s.substr(0, i))*stoll(s.substr(i)));
      }
      if(used[nex]) return -1;
      used[nex]=1;
      n=nex; cnt++;
   }

   return cnt;
}

signed main(){

   int Q;
   cin>> Q;
   for(int i=0; i<Q; i++){
      int N;
      cin>> N;
      cout<< f(N)<< endl;
   }
   
   return 0;
}

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

int main() {
    string firstStr, secondStr;
    cin >> firstStr >> secondStr;

    int ans = 0;
    for(int i =0; i < firstStr.size(); i++) {
        if(firstStr.at(i) != secondStr.at(i)) {
            ans++;
        }

    }

    cout << ans << endl;

    return 0;

}


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

int main(){
	ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
// #ifndef ONLINE_JUDGE 
//     freopen("input.txt", "r", stdin); 
//     freopen("output.txt", "w", stdout); 
// #endif
   int n,k;
   cin>> n>> k;
   //int a[n][2];
   vector <long long> dp(k+1);
   //dp[i] = the maximum total value of items with total value exactly i
   for (int i=0;i<n;i++){
   		int weight, value;
   		cin>> weight>>  value;
   			for (int weight_cu=k-weight;weight_cu>=0;weight_cu--){
   			dp[weight_cu+weight]=max(dp[weight_cu+weight],dp[weight_cu]+value);
   		}
   		// for(auto x:dp){
   		// 	cout<<x<<"\n";
   		// }
   }
   long long ans=0;

   for (int i=0;i<=k;i++){
   		ans=max(ans,dp[i]);
   	}
   	cout<<ans;
	return 0;

}


Measured memory_bytes: 1024
import java.util.Scanner;

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

        if (x == 1) {
            System.out.println("Hello World");
            sc.close();
        } else {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
            sc.close();
        }
    }
}

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

using namespace std;

void Solve(){
    int n;
    double m, k = 0;
    cin>>n>>m;
    vector<int> v(n);
    for(int i = 0; i < n; i++){
        cin>>v[i];
        k += v[i];
    }
    sort(v.rbegin(), v.rend());
    double res = v[m - 1];
    double ans = k / (4 * m);
    if(res >= ans) cout<<"Yes\n";
    else cout<<"No\n";

}
int main(){
    int q = 1;
    while(q--){
        Solve();
    }
}

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

int main(void){
  int N,X,m[102],M,i,j,buff,count=0;
  char str[100],*p;
  
  fgets(str,sizeof(str),stdin);
  p=strtok(str," \n");
  N=atoi(p);
  p=strtok(NULL," \n");
  X=atoi(p);
  
  for(i=0,M=0;i<N;i++){
    fgets(str,sizeof(str),stdin);
    m[i]=atoi(str);
    M+=m[i];
  }
  for(i=0;i<N-1;i++){
    for(j=N-1;j>i;j--){
     if(m[j-1]>m[j]){
      buff=m[j-1],m[j-1]=m[j],m[j]=buff; 
     }
    }
  }
  X-=M,count=N;
  count+=X/m[0];
  
  printf("%d",count);
  
 return 0; 
}

Measured memory_bytes: 128
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
    x=0; char c=gc(); int flag=0;
    while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
    while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
    if (flag) x=-x;
}
template <class T> T _max(T a,T b){return a>b ? a : b;}
template <class T> T _min(T a,T b){return a<b ? a : b;}
template <class T> bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
const int mod=1000000000+7,N=305,inv2=(mod+1)/2;
int Add(int a,int b){a+=b; return a>=mod ? a-mod : a;}
int Sub(int a,int b){a-=b; return a>=0 ? a : a+mod;}
int Mul(int a,int b){return 1LL*a*b%mod;}
void add(int &a,int b){a=Add(a,b);}
void sub(int &a,int b){a=Sub(a,b);}
void mul(int &a,int b){a=Mul(a,b);}
int qpow(int x,int y)
{
	int ans=1;
	while (y)
	{
		if (y&1) mul(ans,x);
		mul(x,x);
		y>>=1;
	}
	return ans;
}

int n,deg[N];
int dp[N][N][N<<1];
int fac[N],inv_fac[N];
void init()
{
	read(n);
	for (int i=1;i<=n;++i)
	{
		read(deg[i]);
	}
	fac[0]=1;
	for (int i=1;i<=n;++i)
	{
		fac[i]=Mul(fac[i-1],i);
	}
	inv_fac[n]=qpow(fac[n],mod-2);
	for (int i=n-1;i>=0;--i)
	{
		inv_fac[i]=Mul(inv_fac[i+1],i+1);
	}
	sort(deg+1,deg+n+1,greater<int>());
	//outarr(deg,1,n);
	dp[1][0][0]=1;
	dp[1][1][deg[1]]=deg[1]-1;
	for (int i=2;i<=n;++i)
	{
		for (int j=0;j<=i;++j)
		{
			for (int q=0;q<=n+n;++q)
			{
				dp[i][j][q]=dp[i-1][j][q];
				if (deg[i]>=2&&j>=1&&q>=deg[i]) 
				{
					add(dp[i][j][q],Mul(dp[i-1][j-1][q-deg[i]],deg[i]-1));
				}
				//cout<<i<<" "<<j<<" "<<q<<" "<<dp[i][j][q]<<endl;
			}
		}
	}
}

void solve()
{
	int multi=1;
	for (int i=1;i<=n;++i)
	{
		mul(multi,inv_fac[deg[i]-1]);
	}
	//outln(multi);
	int ans=Mul(dp[n][n][n+n],Mul(fac[n-1],inv2));
	for (int loop=3;loop<n;++loop)
	{
		for (int sum_deg=loop*2;sum_deg<=n+n;++sum_deg)
		{
			int tmp=Mul(fac[loop-1],Mul(inv2,fac[n-loop-1]));
			mul(tmp,multi);
			//cout<<loop<<" "<<sum_deg<<" "<<tmp<<" "<<dp[n][loop][sum_deg]<<endl;
			mul(tmp,dp[n][loop][sum_deg]);
			mul(tmp,sum_deg-loop*2);
			add(ans,tmp);
		}
	}
	printf("%d\n",ans);
} 
       
#undef int
int main()
{
	init();
	solve();
    return 0;
}


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

#define rep(i, n) for(int i=0; i<n; ++i)
#define repit(it, li) for(auto it=li.begin(); it!=li.end(); it++)
#define ll long long int

int main(){
    int x, n;
    cin>>x>>n;
    bool pm[101]={0};
    rep(i, n){
        int p;
        cin>>p;
        pm[p]=true;
    }

    int ans;
    for(int i=0; ; i++){
        if(x-i<0 || !pm[x-i]){
            ans=x-i;
            break;
        }
        if(x+i>=101 || !pm[x+i]){
            ans=x+i;
            break;
        }
    }


    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3628
import java.util.*;

public 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 ans=0;
        
        for(int i=0; i<n; i++){
            ans += a[i]-1;
        }
        
        System.out.println(ans);
    }
}

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

 
int main() {


    int n;
    cin>>n;

    vector<int> A(n),B(n);
    for(int i=0;i<n;i++){
        cin>>A.at(i)>>B.at(i);
    }

    int64_t sum=0;
    for(int i=n-1;i>=0;i--){
        if( ((A.at(i)+sum)%B.at(i)) != 0 )
            sum+=B.at(i)-(A.at(i)+sum)%B.at(i);
    }

    cout<<sum<<endl;

 	return 0;
}


Measured memory_bytes: 4036
import sys
A,B,C = map(int,input().split())
K = int(input())

for i in range(K):
    if A >= B:
        B *= 2
    elif B >= C:
        C *= 2
    #print(A,B,C)
if A < B < C:
    print('Yes')
else:
    print('No')
        
    

Measured memory_bytes: 9156
n = int(input())
money = 1000
beet = 0
a = [int(i) for i in input().split()]

def buy(price):
    global money
    global beet
    beet = money // price
    money = money % price

def sell(price):
    global money
    global beet
    money += beet * price
    beet = 0

flag = True
for i in range(n-1):
    if flag:
        buy(a[i])
        flag = False
    if a[i] > a[i+1]:
        sell(a[i])
        flag = True
sell(a[-1])
print(money)

Measured memory_bytes: 9160
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		if(i%2==1)
			if(s[i]=='R')
			{
				cout<<"No";
				return 0;
			}
		if(i%2==0)
			if(s[i]=='L')
			{
				cout<<"No";
				return 0;
			}
	}
	cout<<"Yes";
	return 0;
}


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

using namespace std;

struct problems {
    int num;
    int bonus;
};

/// bit配列のインクリメント
/// 全てが1になったらfalseを返す
bool abc(vector<int> &x, int index, int limit) {
    if(x[index] == 0) {
        x[index] = 1;
        return true;
    }
    else {
        if(index == limit) {
            return false;
        }
    }
    
    x[index] = 0;
    return abc(x, index + 1, limit);
}

void showBit(vector<int> &bit) {
    cout << "[ ";
    for(int i = 0; i < bit.size(); i++) {
        cout << bit[i] << " ";
    }
    cout << "]" << endl;
}

int main() {
    int d, g;
    cin >> d >> g;
    
    vector<problems> v(d);
    for(int i = 0; i < d; i++) {
        cin >> v[i].num >> v[i].bonus;
    }
    
    vector<int> bit(d, 0);
    int ans = 1010;
    do {
        int max_point = 0;
        int max_num = 0;
        int p_num = 0;
        int p_point = 0;
        for(int i = 0; i < d; i++) {
            if(bit[i] == 1) {
                p_num += v[i].num;
                p_point += v[i].bonus + (v[i].num * (i + 1) * 100);
            }
            else {
                max_point = max(max_point, (i + 1) * 100);
                max_num = v[i].num;
            }
        }
        
        if(p_point < g) {
            int cnt = (g - p_point) / max_point;
            cnt = (g - p_point) - cnt * max_point != 0 ? cnt + 1 : cnt;
            
            if(cnt < max_num) {
                p_num += cnt;
            }
            else {
                continue;
            }
        }
        
        ans = min(ans, p_num);

    } while(abc(bit, 0, d - 1));
    
    cout << ans << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<cmath>
#define INF 0x7f7f7f7f

using namespace std;

int map[26][26];

int main()
{
	int h,w;
	scanf("%d%d",&h,&w);
	
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&map[i][j]);
		}
	}
	int ans=INF;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int sum=0;
			for(int t=0;t<h;t++){
				for(int l=0;l<w;l++){
					int cos=min(abs(i-t),abs(j-l));
					sum+=cos*map[t][l];
				}
			}
			ans=min(ans,sum);
		}
	}
	printf("%d\n",ans);
	return 0;
}


Measured memory_bytes: 2736
#include <iostream>
#include <vector>
#include <algorithm>
#include<string>

using namespace std;

int main(){
    int n, m;
    cin >> n;
    cin >> m;

    int a[m];
    int y[n-1];


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

    for(int i = 0; i < n-1; i++){
        y[i] = 0;
    }

    for(int i = 0; i < m-1; i ++){
        y[i] = abs(a[i+1] - a[i]);
    }

    sort(y, y+m-1);
    int all = 0;

    for(int i = 0; i < m - 1 - (n - 1); i++){
        all += y[i];
    }
    cout << all << endl;
}

Measured memory_bytes: 1024
// AtCoder-Template.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second
#define FOR(i,N) for(auto i=0; i<N; ++i)
#define FORREV(i,N,_cnt) for(auto i=N-1,cnt=_cnt; cnt > 0; --i, --cnt)
#define ALL(x) x.begin(), x.end()

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T& a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T& a, const T& b) { if (b > a) a = b; }
/* clang-format on */

int main() {
#ifdef DEBUG
	ifstream ifs("in.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	ll N, K; cin >> N >> K;
	ll ans = 0;
	if (K == 1)
		ans = 0;
	
	else ans = N - K;

	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;
class Main{
 public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int N= sc.nextInt();
int M= sc.nextInt();
int gcd=1;
for(int n=1;n<=M/N;n++) {
   if(N==1) {
	   gcd=M;
   break;
   }
   else if(M%n==0) {
	if(n*N<=M) {
	gcd=n;
	 }
    }	
 }
System.out.println(gcd);
 }
  }

Measured memory_bytes: 22608
#ifdef LOCAL111
	#define _GLIBCXX_DEBUG
#else
	#define NDEBUG
#endif
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; }

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#ifdef LOCAL111
	#define DEBUG(x) cout<<#x<<": "<<(x)<<endl
	template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
#else
	#define DEBUG(x) true
	template<typename T> void dpite(T a, T b){ return; }
#endif
#define F first
#define S second
#define SNP string::npos
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}

typedef long long int LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;

void ios_init(){
	//cout.setf(ios::fixed);
	//cout.precision(12);
#ifdef LOCAL111
	return;
#endif
	ios::sync_with_stdio(false); cin.tie(0);	
}

int main()
{
	ios_init();
	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n));
	vector<vector<bool>> fv(n, vector<bool>(n,true));
	REP(i, n) REP(j, n) cin >> a[i][j];
	bool ex = true;
	LL ans = 0;
	REP(k,n){
		REP(i, n) REP(j, n) {
			if(a[i][j] > a[i][k] + a[k][j]) {
				ex = false;
			} else if(a[i][j] == a[i][k] + a[k][j]) {
				if(i == k or j == k) {
					continue;
				}
				fv[i][j] = false;
			}
		}
	}
	REP(i,n) REP(j,n) if(fv[i][j]){
		ans += a[i][j];
	}
	if(ex) cout << ans/2 << endl;
	else cout << -1 << endl;
	return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
int a[11][11];
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>a[i][j];
		}
	}
	int ans=0;
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			int num=0;
			for(int k=0;k<m;k++)
			{
				num+=abs(a[i][k]-a[j][k])*abs(a[i][k]-a[j][k]);
			}
			if(sqrt(num)==floor(sqrt(num)))
			  ans++;
		}
	}
	cout<<ans;
	return 0;
}

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

int e[100000],f[99999];
int main(){
    int a;cin>>a;
    for(int b=0;b<a;b++){
	int c,d;cin>>c>>d;
	for(int g=0;g<c;g++){scanf("%d",&e[g]);}
	int sum=0;
	for(int g=1;g<c;g++){sum+=e[g]-e[g-1];f[g-1]=e[g]-e[g-1];}
	sort(f,f+c-1,greater<int>());
	for(int i=0;i<d-1&&i<c-1;i++){
	    sum-=f[i];
	    }
	    cout<<sum<<endl;
	}
    }

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

int main(){
	long long int n,k,p,count=0,x,c=0;
	std::cin>>n>>k;

	std::vector<long long int> v(n);

	std::vector<long long int> u(n);

	std::vector<long long int> g(n);
	
	for (int i = 0; i < n; i++) {
		long long int a;
		std::cin >> a;

		v[i] = a-1;
	}

	p = 0;
	u[0] = 1;

	while (1) {
		p = v[p];

		c++;
		if (u[p] == 1) {
			x = p;
			break;
		}

		u[p] = 1;
	}


	while (1) {
		p = v[p];

		
		if (g[p] == 1) {
			break;
		}
		count++;

		g[p] = 1;
	}

	if (k < c - count) {
		x = 0;
		for (int i = 0; i < k; i++) {
			x = v[x];
		}
		std::cout << x + 1 << std::endl;

		exit(0);
	}

	k = (k-c+count) % count;

	//std::cout << "C="<<c << std::endl;
	//std::cout << "count=" << count << std::endl;
	//std::cout << "k="<<k << std::endl;
	//std::cout << "x=" << x << std::endl;


	
	for (int i = 0; i < k; i++) {
		x = v[x];
	}





	std::cout<<x+1<<std::endl;
	return 0;
}

Measured memory_bytes: 8256
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef priority_queue<ll> PQI;
typedef priority_queue<P> PQP;
typedef vector<vector<P>> pvec;
typedef vector<vector<ll>> vvec;
typedef vector<ll> vec;
const vector<int> dx = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> dy = {1, 0, -1, 0, 1, -1, 1, -1};
const int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30);
const int INF = (1LL << 60);
const double EPS = (1 >> 30);
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}



signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    //---------------------------------------------
    
    int a,b,q;
    cin>>a>>b>>q;
    vector<int> s(a);
    REP(i,a) cin >> s[i];
    vector<int> t(b);
    REP(i,b) cin >> t[i];
    vector<int> y(q);
    REP(i,q) cin >> y[i];
    
    for(int i=0;i<q;i++){
        int x;
        //cin>>x;
        x=y[i];
        ll ans=INF;
        int it=lower_bound(all(s),x)-s.begin();
        for(int j=max(0ll,it-2);j<min(a,it+2);j++){
            int it2=lower_bound(all(t),x)-t.begin();
            for(int k=max(0ll,it2-2);k<min(b,it2+2);k++){
                chmin(ans,abs(x-s[j])+abs(s[j]-t[k]));
            }
        }

        it=lower_bound(all(t),x)-t.begin();
        for(int j=max(0ll,it-2);j<min(b,it+2);j++){
            int it2=lower_bound(all(s),x)-s.begin();
            for(int k=max(0ll,it2-2);k<min(a,it2+2);k++){
                chmin(ans,abs(x-t[j])+abs(t[j]-s[k]));
            }
        }
        cout<<ans<<endl;
    }
}

Measured memory_bytes: 3712
from fractions import gcd
a,b,c,d=map(int,input().split())
def lcm(x,y):
  return (x*y)//gcd(x,y)
B=b-(b//c + b//d - b//lcm(c,d))
A=a-1-((a-1)//c + (a-1)//d - (a-1)//lcm(c,d))
print(B-A)

Measured memory_bytes: 5048
#include<iostream>
using namespace std;
const int mod=1e9+7;
const int N=5005;
long long dp[N][N];
int main(){
	int n,i,j,k,l;
	cin>>n;
	string s;
	cin>>s;
	l=s.size();
	dp[0][0]=1;
	for(i=1;i<=n;i++){
		dp[i][0]=(dp[i-1][1]+dp[i-1][0])%mod;
		for(j=1;j<=n;j++){
			dp[i][j]=(dp[i-1][j-1]*2+dp[i-1][j+1])%mod;
		}
	}
	for(i=1;i<=l;i++){
		dp[n][l]=(dp[n][l]*(mod+1)/2)%mod;
	}
	cout<<dp[n][l];
}

Measured memory_bytes: 195712
#include <iostream>
#define MAX 5000
using namespace std ;

int a[40],b[40],c[40],n,ma,mb,opt;
int search(int sumA, int sumB, int sumC, int st){
    if(sumC>=opt) return opt;  
    if((sumA+a[st])*mb==(sumB+b[st])*ma){
        opt=min(opt,sumC+c[st]);
        if(st==n-1) return opt;
        return min(opt, search(sumA, sumB, sumC, st+1));
    }
    if(st==n-1) return opt;
    return min(min(opt, search(sumA, sumB, sumC, st+1)), search(sumA+a[st], sumB+b[st], sumC+c[st], st+1));
}
int main(void){
    opt=MAX;
    cin>>n>>ma>>mb;
    for(int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i];
    opt=search(0,0,0,0);
    if(opt>=MAX) opt=-1;
    cout<<opt<<endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#define MAX 2000001
#define VMAX 10000

int main(){
  unsigned short *A,*B;

  int C[VMAX+1];
  int n,i,j;
  scanf("%d",&n);

  A = malloc(sizeof(short)*n+1);
  B = malloc(sizeof(short)*n+1);

  for(i=0;i<=VMAX;i++)C[i]=0;

  for(i=0;i<n;i++){
    scanf("%hu",&A[i+1]);
    C[A[i+1]]++;
  }

  for(i=1;i<=VMAX;i++)C[i]=C[i]+C[i-1];

  for(j=1;j<=n;j++){
    B[C[A[j]]]=A[j];
    C[A[j]]--;
  }

  for(i=1;i<=n;i++){
    if(i>1)printf(" ");
    printf("%d",B[i]);
  }
  printf("\n");

  return 0;
}



Measured memory_bytes: 9636
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <regex>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1000000007;

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//myfuncs
//素数判定
template<class T> inline T isPrime(T x) {
	bool ret = true;
	if (x < 2) {
		ret = false;
	}
	for (LL i = 2; i <= sqrt(x); i++) {
		if (x%i == 0) {
			ret = false;
		}
	}
	return ret;
}

//Union FInd
struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

//main code

int main(int argc, char const* argv[])
{
	int n;
	cin >> n;
	if(n%2==0) cout << n << endl;
	if(n%2==1) cout << n*2 << endl;
	
	return 0;
}


Measured memory_bytes: 256
while True:
    H,W = map(int, input().split())
    if H == 0 and W == 0:
        break
    for i in range(H):
        for j in range(W):
            if (i+j) % 2 == 0:
                print("#",end = '')
            else:
                print('.',end = '')
        print()
    print()


Measured memory_bytes: 7008
N = int(input()) + 2
X = [0, 0] + list(map(int, input().split()))
dp = [[0, 0] for _ in range(N)]
for i in range(2, N):
    if i%2:
        dp[i][1] = max(dp[i-2][1] + X[i], dp[i-1][1])
        if i!=3:
            dp[i][0] = max(dp[i-2][0] + X[i], dp[i-1][0])
    else:
        dp[i][1] = dp[i-2][1] + X[i]
        if i!=2:
            dp[i][0] = max(dp[i-2][0] + X[i], dp[i-1][1])
if N%2:
    print(dp[-1][0])
else:
    print(dp[-1][1])


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

#define ll long long int
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define debug(a) cout << #a << " = " << a << " ";
#define endl "\n"
#define MOD (int) 1e9+7
#define inf (int) (1e18)

void solve(){
    long int ans=0;
    long int n;
    cin>>n;
    long int a[n];
    for(long int i=0;i<n;i++)
        cin>>a[i];

    sort(a,a+n);
    for(long int i=0;i<n;i++){
        for(long int j=i+1;j<n;j++){
            for(long int k=j+1;k<n;k++){
                if(a[i]+a[j]>a[k] && a[i]!=a[j] && a[j]!=a[k] && a[i]!=a[k]){
                    // debug(i); debug(j); debug(k); cout<<endl;
                    ans++;
                }
            }
        }
    }
    cout<<ans<<endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long int t=1;
    // cin >> t;
    for(long int tt=1;tt<=t;tt++){
        solve();
    }
    return 0;
}

Measured memory_bytes: 3668
#include <iostream>
#define MAX_COUNT 1005
using namespace std;

int main(int argc, char const *argv[])
{
	int required, count;
	int sum = 0;
	int tmp;
	int prices[MAX_COUNT] = {0};
	cin >> count >> required;
	for (int i = 0; i < count; ++i) cin >> prices[i];
	for (int i = 0; i < count; ++i)
		for (int j = i + 1; j < count; ++j) 
			if (prices[j] < prices[i])
			{
				tmp = prices[i];
				prices[i] = prices[j];
				prices[j] = tmp;
			}
	for (int i = 0; i < required; ++i) sum += prices[i];
	cout << sum;
	return 0;
}

Measured memory_bytes: 3616
import collections
n = int(input())
v = list(map(int, input().split()))

odd = [v[i] for i in range(n) if i % 2 == 0]
even = [v[i] for i in range(n) if i % 2 == 1]

t = collections.defaultdict(int)
t2 = collections.defaultdict(int)
for x in odd:
    t[x] += 1
for x in even:
    t2[x] += 1

t = list(t.items())
t.sort(key = lambda x:x[1], reverse=True)

t2 = list(t2.items())
t2.sort(key = lambda x:x[1],reverse=True)


t.append([0, 0])
t2.append([0, 0])
ans = t[0][1] + t2[0][1]
if t[0][0] == t2[0][0]:
    ans = max(t[0][1] + t2[1][1], t[1][1]+t2[0][1])
print(n - ans)


Measured memory_bytes: 19040
#include <stdio.h>

int main(){
	int i;
	int l[10];
	int v1,v2;
	int sum;
	int len;
	double d;
	
	while(scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",&l[0],&l[1],&l[2],&l[3],&l[4],&l[5],&l[6],&l[7],&l[8],&l[9],&v1,&v2)!=EOF){
		sum=0;
		for(i=0;i<10;i++) sum += l[i];
		d = (double)sum*v1/(v1+v2);
		for(i=0,len=0;len<d;i++){
			len += l[i];
		}
		printf("%d\n",i);
	}
	
	return 0;
}

Measured memory_bytes: 596
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 100010;
const int MAXM = 100010;
const int MAXK = 510;
const int MAXQ = 200010;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
  int a, b;
  cin >> a >> b;
  cout << b*2 - a;
    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring> 
#include<iostream>
using namespace std;
#define INF 1000000000
long long int a,b,c,d,s;
int main(){
scanf("%lld",&s);
if(s%INF)
printf("0 0 %lld %lld %lld %lld\n",1,s/INF+1,INF,INF-s%INF);
else 
printf("0 0 %lld %lld %lld %lld\n",1,s/INF,INF,0);
}

Measured memory_bytes: 3804
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int main()
{
  int a, b, k;
  cin >> a >> b >> k;
  for (int i = 0; i < k; ++i)
  {
    if (i % 2 == 0)
    {
      if (a % 2 == 1)
        a--;
      a /= 2;
      b += a;
    }
    else
    {
      if (b % 2 == 1)
        b--;
      b /= 2;
      a += b;
    }
  }
  cout << a << ' ' << b << endl;

  return 0;
}


Measured memory_bytes: 256
arr = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
K = int(input())
print(arr[K-1])

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

using namespace std;

int main(){

   int N;
   cin>> N;
   string s, t;
   cin>> s>> t;

   if(s.size()+t.size()<=N){
      cout<< N<< endl;
      return 0;
   }
   string u=s+t;
   int n=u.size();
   string rt=t; reverse(rt.begin(), rt.end());
   int ans=n;
   while(n>=N){
      int d=u.size()-n;
      for(int i=0; i<u.size()-d; i++){
         string S=u.substr(0, i)+u.substr(i+d);
         string rS=S; reverse(rS.begin(), rS.end());
         if(S.substr(0, N)==s&&rS.substr(0, N)==rt){
            ans=n;
         }
      }
      n--;
   }

   cout<< ans<< endl;

   return 0;
}


Measured memory_bytes: 256
import java.util.*;
public class Main {
    public static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
    	int i, j, k;
    	String moji = "";
    	int count[] = new int[26];
	    while(sc.hasNextLine()) {
	    	moji += sc.nextLine();
	    }
	    moji = moji.toLowerCase();
	   	for(i = 0; i < moji.length(); i++) {
	   		char c = moji.charAt(i);
	   		if('a' <= c && 'z' >= c) {
	   			count[c - 'a']++;
	   		}
	   	}
    	for(i = 0; i < 26; i++) {
    		System.out.println((char)(i + 'a') + " : " + count[i]);
    	}
    }
}


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

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

#define inf long long_MAX/3
#define INF long long_MAX/3
#define PB push_back
#define pb push_back
#define MP make_pair
#define mp make_pair
#define ALL(a) (a).begin(),(a).end()
#define all(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define PII pair<long long,long long>
#define pii pair<long long,long long>
#define pcc pair<char,char>
#define pic pair<long long,char>
#define pci pair<char,long long>
#define VS vector<string>
#define VI vector<long long>
#define debug(x) cout<<#x<<": "<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define in scanf
#define out prlong longf
#define LL long long
#define ll long long
#define ULL unsigned long long
#define ull unsigned long long
#define eps 1e-14
#define FST first
#define SEC second

long long l,r;

int main(){

	cin >> l >> r;
	if(r-l >= 2019){
		cout << 0;
	}else{
		int m = 2018;
		l = l %2019;
		r = r %2019;

		FOR(i,min(l,r),max(l,r)){
			FOR(j,i+1,max(l,r)+1){
				m = min(m,(i*j)%2019);
			}
		}
		cout << m;
	}


}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#define int long long
#define mod 1000000007
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int n, ans = 0, cnt = 0;
vector<pair<int, int>> v[20];
bool b[20];
void f(int k) {
	if (k > n) {
		if (ans < cnt) {
			bool b00l = 1;
			for1(i, n) {
				if (b[i])for (pair<int, int>p : v[i])if (b[p.first] != p.second)b00l = 0;
			}
			if (b00l)ans = cnt;
		}
		return;
	}
	f(k + 1);
	b[k] = 1; cnt++;
	f(k + 1);
	b[k] = 0; cnt--;
	return;
}
signed main() {
	cin >> n;
	for1(i, n) {
		int k; cin >> k;
		for0(j, k) { int a, b; cin >> a >> b; v[i].push_back(make_pair(a, b)); }
	}
	f(1);
	cout << ans << endl;
}

Measured memory_bytes: 256
a=int(input())
b=int(input())
c=int(input())
x=int(input())
ans=0
for i in range(a+1):
  for j in range(b+1):
    for k in range(c+1):
      if 500*i+100*j+50*k==x:
        ans+=1
print(ans)

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

#ifdef LOCAL
#define debug(_) cerr << #_ << ": " << (_) << '\n'
#define _GLIBCXX_DEBUG
#else
#define debug(_) (void(0))
#endif  // LOCAL

const long long MOD = 1000000007;

int main(){
    int N;
    vector<long long> As;
    long long sumA = 0, sumAsqrd = 0;

    cin >> N;
    As.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> As[i];
        sumA += As[i];
        sumAsqrd += As[i] * As[i] % MOD;
        sumA %= MOD;
        sumAsqrd %= MOD;
    }

    cout << (sumA * sumA + MOD * 10 - sumAsqrd) % MOD * 500000004 % MOD << '\n';

    return 0;
}

Measured memory_bytes: 4760
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> v(n);
    for(int i = 0; i < n; i++) cin >> v[i];
    int ans = 101;
    while(ans--)
    {
        int tmp = 0;
        for(int i = 0; i < n; i++)
        {
            if(v[i] + tmp < ans) break;
            tmp = (v[i] + tmp) - ans;
            if(i == n - 1)
            {
                cout << ans << endl;
                return 0;
            }
        }
    }
    return 0;
}




Measured memory_bytes: 3172
#include<bits/stdc++.h>
#define llt long long  int
#define ullt unsigned long long int
using namespace std;
void fast(){
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    }
int main()
{
    fast();
    llt n,count=0,key=0;
    cin>>n;
    llt arr[n];
    for(int i=0;i<n;i++)
    cin>>arr[i];
    key=arr[0];
    for(int i=0;i<n;i++)
    if(arr[i]<=key){
    count++;
    key=arr[i];}
    cout<<count;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
 
int main() {
long long int n,k,p;cin>>n>>k;
  double x[n],sum=0,sum_next=0,ans;
  for (int i=0;i<n;i++){cin>>p;x[i]=(1+p)/2.0;}
  for(int i=0;i<k;i++)sum+=x[i];
  ans=sum;
  
 for(int i=1;i<n-k+1;i++){
   sum = sum - x[i-1] + x[i+k-1];
   ans=max(sum,ans);
 }
  printf("%.16f\n", ans);
}
   

Measured memory_bytes: 1792
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
class FC{
public:
	int p[31];
	int n;
	bool is_over(){
		return p[1]==p[2]&&p[2]==p[3]&&p[3]==p[4]&&p[4]==p[5]&&
			p[5]==p[6]&&p[6]==p[7]&&p[7]==p[8]&&p[8]==p[9]&&
			p[10]==p[11]&&p[11]==p[12]&&
			p[13]==p[14]&&p[14]==p[15]&&
			p[16]==p[17]&&p[17]==p[18]&&
			p[19]==p[20]&&p[20]==p[21]&&
			p[22]==p[23]&&p[23]==p[24]&&p[24]==p[25]&&p[25]==p[26]&&
			p[26]==p[27]&&p[27]==p[28]&&p[28]==p[29]&&p[29]==p[30];
	}
	void rot1(){
		swap(p[7],p[22]);
		swap(p[8],p[23]);
		swap(p[9],p[24]);
		swap(p[13],p[18]);
		swap(p[10],p[12]);
	}
	void rot2(){
		swap(p[1],p[28]);
		swap(p[2],p[29]);
		swap(p[3],p[30]);
		swap(p[15],p[16]);
		swap(p[19],p[21]);
	}
	void rot3(){
		swap(p[1],p[24]);
		swap(p[4],p[27]);
		swap(p[7],p[30]);
		swap(p[10],p[21]);
		swap(p[16],p[18]);
	}
	void rot4(){
		swap(p[3],p[22]);
		swap(p[6],p[25]);
		swap(p[9],p[28]);
		swap(p[12],p[19]);
		swap(p[13],p[15]);
	}
};
int main(){
	int N;
	cin>>N;
	for(int i=0; i<N; i++){
		FC now;
		for(int j=1; j<=30; j++){
			cin>>now.p[j];
		}
		now.n=0;

		queue<FC> que;
		que.push(now);
		while(!que.empty()){
			now=que.front();
			que.pop();
			if(now.is_over()){
				cout<<now.n<<endl;
				break;
			}
			FC nex1=now; nex1.rot1(); nex1.n++; que.push(nex1);
			FC nex2=now; nex2.rot2(); nex2.n++; que.push(nex2);
			FC nex3=now; nex3.rot3(); nex3.n++; que.push(nex3);
			FC nex4=now; nex4.rot4(); nex4.n++; que.push(nex4);
		}
	}
	return 0;
}



Measured memory_bytes: 13844
#include<iostream>
#include<string>
#include<typeinfo>
#include<vector>
#include<algorithm>
#include<map>
#include<iomanip>
typedef long long int ll;
using namespace std;
int main()
{
	int A,P;
	cin >> A >> P;
	cout << (A*3 + P)/2 << endl;
	
	return 0;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;

namespace cswork
{

    class Program
    {
        public Program() { }

        Scanner cin;

        void Omame()
        {
            cin = new Scanner();

            string s = cin.next();
            int ans = 0;

            int tmp = 0;
            for (int i = 0; i < s.Length; i++)
            {
                string ss = s[i].ToString();

                if(ss.Equals("A") || ss.Equals("C") || ss.Equals("G") || ss.Equals("T"))
                {
                    tmp++;
                    if (ans < tmp)
                    {
                        ans = tmp;
                    }
                }
                else
                {
                    if(ans < tmp)
                    {
                        ans = tmp;
                    }
                    tmp = 0;
                }
            }
            
            Console.WriteLine(ans);
        }

        static void Main(string[] args)
        {
            new Program().Omame();
        }
    }

    class Scanner
    {
        string[] s;
        int i;

        char[] cs = new char[] { ' ' };

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

        public string next()
        {
            if (i < s.Length) return s[i++];
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();

            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next();
        }

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

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

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

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

const ll logx=30;
const ll N=5e5+5;
const ll M=1e3+5;
const ll mod=1e9+7;
const ll INF=1e18+5;
const double PI = 3.14159265358979323846;

#define ints(n) scanf("%d",&n)
#define intp(n) printf("%d\n",n)
#define longs(n) scanf("%intd",&n)
#define longp(n) printf("%intd\n",n)

inline ll mul(ll a, ll b){ return (a * 1ll * b) % mod; }
inline ll sub(ll a, ll b){ ll c = a - b; if(c < 0) c += mod; return c; }
inline ll add(ll a, ll b){ ll c = a + b; if(c > mod) c -= mod; return c; }

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define ms(dp,val) memset(dp,val,sizeof(dp))
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1ll;x=(x*x)%m;}return ans%m;}

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

ll n,a[20][20],ans[(1<<20)];

void Subsets()
{
  for(ll mask=0;mask<(1<<n);mask++)
  {
    ll sum=0;
    for(ll i=0;i<n;i++)
    {
      ll seti=mask&(1<<i);
      if(seti==0){continue;}
      for(ll j=i+1;j<n;j++)
      {
        ll setj=mask&(1<<j);
        if(setj==0){continue;}
        sum+=a[i][j];
      }
    }
    ans[mask]=sum;
  }
}

int main()
{
  fast;
  cin>>n;
  ll maxi=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++){cin>>a[i][j];}
  }
  Subsets();
  for(ll mask=0;mask<(1<<n);mask++)
  {
    vector<ll> idx;
    for(ll i=0;i<n;i++)
    {
      ll setx=mask&(1<<i);
      if(setx==0){continue;}
      idx.pb(i);
    }
    ll m=idx.size();
    for(ll j=0;j<(1<<m);j++)
    {
      ll maskd=0;
      for(ll h=0;h<m;h++)
      {
        ll sety=j&(1<<h);
        if(sety==0){continue;}
        maskd|=(1<<idx[h]);
      }
      ans[mask]=max(ans[mask],ans[maskd]+ans[mask^maskd]);
    }
  }
  cout<<ans[(1<<n)-1]<<endl;
}


Measured memory_bytes: 768
N,K=map(int,input().split())
T=str(bin(K))
S=len(T)-2
A = list(map(int,input().split()))
lst=[[0 for e in range(N+1)] for f in range(S)]
for i in range(1,N+1):
 lst[0][i]=A[i-1]
for j in range(1,S):
 for i in range(1,N+1):
  lst[j][i]=lst[j-1][lst[j-1][i]]
ans=[[e for e in range(N+1)] for f in range(S+1)]
for k in range(S):
 if int(T[k+2])==1:
  for l in range(1,N+1):
   ans[k+1][l]=lst[S-1-k][ans[k][l]]
 else:
  for l in range(1,N+1):
   ans[k+1][l]=ans[k][l]
print(ans[S][1])


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

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

template<typename T>
constexpr T inf = static_cast<T>(pow(10, static_cast<int>(log10(numeric_limits<T>::max())))) + 7;
constexpr llong mod = 1e9+7;

int main() {
  llong N, K;
  vector<llong> A;

  cin >> N >> K;
  A.resize(N + 1);
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
  }

  vector<llong> perm(N + 1, -1);
  vector<llong> res = {1};
  llong now = 1;
  perm[1] = 0;
  while (perm[A[now]] == -1) {
    perm[A[now]] = res.size();
    res.push_back(A[now]);
    now = A[now];
  }
  llong start = perm[A[now]];
  llong goal = perm[now];

  for (int i = 0; i < res.size(); ++i) {
    cerr << i << " " << res[i] << endl;
  }
  cerr << start << " " << goal << endl;
  cerr << K - start << " " << goal - start + 1 << endl;

  if (K <= start) {
    cout << res[K] << endl;
  }
  else {
    cout << res[start + (K - start)%(goal - start + 1)] << endl;
  }

  return 0;
}

Measured memory_bytes: 8496
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <algorithm>
#include <set>
#include <deque>
#include <utility>
#include <chrono>
#include <sstream>
#include <iomanip>
#define INF 1 << 30
#define MOD 1000000007;
#define pi 3.14159265358979
#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define rer(i, l, r) for (int (i) = (int)(l); (i) <= (int)(r); (i)++)
#define reu(i, l, r) for (int (i) = (int)(l); (i) < (int)(r); (i)++)
#define D(x) cout << x << endl
#define d(x) cout << x
#define all(x) (x).begin(), (x).end()
#define pub(x) push_back(x)
#define pob() pop_back()
#define puf(x) push_front(x)
#define pof() pop_front()
#define mp(x, y) make_pair((x), (y))
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
template<typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; }
static const int dx[] = {0, 0, 1, -1};
static const int dy[] = {-1, 1, 0, 0};

int main() { 
  int a, b;
  cin >> a >> b;
  int x, y;
  cin >> x >> y;
  if (a >= b)
    D(x * b + (a - b) * y);
  else
    D(a * x);
  return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std; 
bool isprime(int X){
	if(X == 1) return false; 
	for(int i = 2; i * i <= X; i++){
		if(X % i == 0) return false; 
	}
	return true; 
}
int main(){
	int N; cin >> N; 
	for(int i = N; i <= 114514; i++){
		if(isprime(i)){
			cout << i << endl; return 0; 
		}
	}
}

Measured memory_bytes: 256
f=lambda:[*map(int,input().split())]
d=int(input())
c=f()
S,t=[],[]
for i in range(d): S+=[f()]
for i in range(d): t+=[int(input())]
v=0
D=[0]*26
for i in range(d):
  D[t[i]-1]=i+1
  v+=S[i][t[i]-1]
  for j in range(26):
    v-=c[j]*(i+1-D[j])
  print(v)

Measured memory_bytes: 9396