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

vector< vector<int> > adj;
vector<int> dp;
vector<bool> vis;

void dfs(int node) {
	vis[node]=true;
	
	for(int i=0; i<adj[node].size(); i++) {
		if (!vis[adj[node][i]])
			dfs(adj[node][i]);
		
		dp[node] = max(dp[node], 1+dp[adj[node][i]]);
	}
}

int main() {
	int n, m, x, y;
	cin >> n >> m;
	adj.resize(n+1);
	dp = vector<int>(n+1, 0);
	vis = vector<bool>(n+1, false);
	
	for (int i=0; i<m; i++) {
		cin >> x >> y;
		adj[x].push_back(y);
	}
	
	for (int i = 1; i <= n; i++) { 
        if (!vis[i]) 
            dfs(i); 
    } 
	
	int ans=0;
	for (int i=1; i<=n; i++)
		ans = max(ans, dp[i]);
		
	cout << ans << endl;
}

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

#define loop(i,n) for(int i=0;i<n;i++)
int c[10002];
stack<int> s;
queue<int> q;

int rensa(int p,int n){
  while(!s.empty())s.pop();
  while(!q.empty())q.pop();
  loop(i,n){
    if(i<p)s.push(c[i]);
    else if(i>p)q.push(c[i]);
  }
  int r=1;
  while(!s.empty()&&c[p]==s.top()){
    r++;
    s.pop();
  }
  while(!q.empty()&&c[p]==q.front()){
    r++;
    q.pop();
  }
  if(r<4)return s.size()+q.size()+r;
  while(!s.empty()&&!q.empty()&&s.top()==q.front()){
    int color=s.top();
    r=0;
    while(!s.empty()&&color==s.top()){
      r++;
      s.pop();
    }
    while(!q.empty()&&color==q.front()){
      r++;
      q.pop();
    }
    if(r<4)return s.size()+q.size()+r;
  }
  return s.size()+q.size();
}

int main(){
  int n;
  while(cin>>n,n){
    loop(i,n)cin>>c[i];
    int m=n;
    loop(i,n){
      c[i]=c[i]%3+1;
      m=min(m,rensa(i,n));
      c[i]=c[i]%3+1;
      m=min(m,rensa(i,n));
      c[i]=c[i]%3+1;
    }
    cout<<m<<endl;
  }
}

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

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	string s;
	cin >> n >> s;
	int ans = 0;
	for(int i = 0; i < n-2; i++){
		if(s[i] == 'A' && s[i+1] == 'B' && s[i+2] == 'C') ans++;
	}
	cout << ans << endl;
	return 0;
}

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

int main(void){
  int N,M;
  int sum=0;
  scanf("%d",&N);
  scanf("%d",&M);
  int A[M];
  for(int i;i<M;i++){
    scanf("%d",&A[i]);
    sum+=A[i];
  }
  if(sum<=N){
    printf("%d",N-sum);
  }
  else{
    printf("%d",-1);
  }
  return 0;
}


Measured memory_bytes: 1728
#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
typedef  unsigned long long int ull;
typedef pair<ll,ll> P;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
struct mint{
    using ull=unsigned long long int;
    ull v;
    mint(ll vv=0){s(vv%mod+mod);}
    mint& s(ull vv){
        v=vv<mod?vv:vv-mod;
        return *this;
    }
    //オーバーロード
    mint operator-()const{return mint()-*this;}//符号反転
    mint&operator+=(const mint&val){return s(v+val.v);}
    mint&operator-=(const mint&val){return s(v+mod-val.v);}
    mint&operator*=(const mint&val){
        v=ull(v)*val.v%mod;
        return *this;
    }
    mint&operator/=(const mint&val){return *this*=val.inv();}
    mint operator+(const mint&val){return mint(*this)+=val;}
    mint operator-(const mint&val){return mint(*this)-=val;}
    mint operator*(const mint&val){return mint(*this)*=val;}
    mint operator/(const mint&val){return mint(*this)/=val;}
    mint pow(int n)const{
        mint res(1),x(*this);
        while(n){
            if(n&1)res*=x;
            x*=x;
            n>>=1;
        }
        return res;
    }
    mint inv()const{return pow(mod-2);}
    //拡張ユークリッドの互除法
    /* mint inv()const{
        int x,y;
        int g=extgcd(v,mod,x,y);
        assert(g==1);
        if(x<0)x+=mod;
        return mint(x);
    }*/
    friend ostream& operator<<(ostream&os,const mint&val){
        return os<<val.v;
    }//出力
    bool operator<(const mint&val)const{return v<val.v;}
    bool operator==(const mint&val)const{return v==val.v;}
    bool operator>(const mint&val)const{return v>val.v;}
};
const ll MAX = 2000010;//設定
mint fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void init(){
    fac[0] = fac[1] = 1;
    for(int i=1;i<MAX;i++)fac[i]=fac[i-1]*i;
    finv[MAX-1]=fac[MAX-1].inv();
    for(int i=MAX-2;i>=0;i--)finv[i]=finv[i+1]*(i+1);
    for(int i=MAX-2;i>=1;i--)inv[i]=finv[i]+fac[i-1];
}
//階乗
mint factor(ll n,ll k){
  if (n<k) return 0;
  if (n<0 || k<0) return 0;
  return fac[n]*finv[k];
}
// 二項係数計算
mint 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];
}
int main(){
ll n,m,k;
cin>>n>>m>>k;
mint ans=0;
init();
for(int i=1;i<n;i++){
    ans+=mint(i)*mint(n-i)*mint(m*m)*COM(n*m-2,k-2);
}
for(int i=1;i<m;i++){
    ans+=mint(i)*mint(m-i)*mint(n*n)*COM(n*m-2,k-2);
}
cout<<ans<<endl;
}

Measured memory_bytes: 47104
#include <bits/stdc++.h>
using namespace std;
void Main();

using i8  = int8_t;   /* -128 ~ 127 */
using u8  = uint8_t;  /* 0 ~ 255 */
using i16 = int16_t;  /* -32,768 ~ 32,767 */
using u16 = uint16_t; /* 0 ~ 65,535 */
using i32 = int32_t;  /* -2,147,483,648 ~ 2,147,483,647 */
using u32 = uint32_t; /* 0 ~ 4,294,967,295 */
using i64 = int64_t;  /* -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 */
using u64 = uint64_t; /* 0 ~ 18,446,744,073,709,551,615 */
using f32 = float;    /* (-3.4 * 10^38) ~ (3.4 * 10^38) */
using f64 = double;   /* (-1.7 * 10^308) ~ (1.7 * 10^308) */
using f80 = __float80;
template <class T> using Vec = vector<T>;
constexpr i64 INF = 1010000000000000017;
constexpr i64 MOD = 1e9 + 7;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;
#define ALL(v) v.begin(), v.end()
#define GCD(a, b) __gcd(a, b) /* 最大公約数 */
#define LCM(a, b) a / __gcd(a, b) * b /* 最小公倍数 */

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

i64 f(i64 n) {
    return n * n * n * n * n;
}

void Main() {
    i64 X;
    cin >> X;
    for (i32 i = 0; i < 400; i++) {
        for (i32 j = 0; j < 400; j++) {
            i64 x = i - 200;
            i64 y = j - 200;
            if (f(x) - f(y) == X) {
                cout << x << " " << y << endl;
                return;
            }
        }
    }
}


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

int main()
{
	cin>>s;
	int n=s.size();
	if(s[n-1]=='1'||s[0]=='0')
	{
		puts("-1");
		return 0;
	}
	for(int i=0;i<n-1;i++)
	{
		if(s[i]!=s[n-i-2])
		{
			puts("-1");
			return 0; 
		}
	}
	int pre=1;
	for(int i=1;i<=n-1;i++)
	{
		if(s[i-1]=='1')
		{
			cout<<i+1<<' '<<pre<<endl;
			pre=i+1;
		}
		else
		{
//			cout<<pre<<endl;
			cout<<i+1<<' '<<pre<<endl; 
		}
	}
	//cout<<n-1<<ends<<n<<endl;
	return 0;
} 

Measured memory_bytes: 1664
#include <iostream>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <cassert>
#include <functional>

using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    int *h = new int[n];
    for (int i = 0; i < n; i++)
        cin >> h[i];

    bool flag = true;
    int min_tmp = 1000, res = 0;
    while (flag)
    {
        flag = false;
        for (int i = 0; i < n; i++)
        {
            if (h[i] != 0)
            {
                flag = true;
                for (int ii = 0; ii < n - i; ii++)
                {
                    min_tmp = min(min_tmp, h[i + ii]);
                    if (i + ii == n - 1 || h[i + ii + 1] == 0)
                    {
                        res += min_tmp;
                        if (ii == 0)
                            h[i] -= min_tmp;
                        else
                            for (int s = 0; s < ii + 1; s++)
                                h[i + s] -= min_tmp;
                        break;
                    }
                }
                min_tmp = 1000;
                break;
            }
        }
    }

    cout << res << endl;
    delete[] h;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,v;
    cin>>n;
    int x[n];
    for(int i=0;i<n;i++)cin>>x[i];
    int mx=-1,c=0;
    for(int i=0;i<n-1;i++)
    {
        if(x[i]>=x[i+1])c++;
        else{mx=max(mx,c);c=0;}
    }
    mx=max(mx,c);
    cout<<mx<<endl;
}


Measured memory_bytes: 640
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
 
public class Main implements Runnable {
	
    public static void main(String[] args) {
    	new Thread(null, new Main(), "", 16 * 1024 * 1024).start();
    }
    
    public void run() {
        FastScanner sc = new FastScanner();
        
        int n = sc.nextInt();
        int k = sc.nextInt();

        
        if(n<=k){
        	System.out.println(1);
        }
        else{
        	System.out.println(0);
        }
    }
}


class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}
	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}
	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
		return hasNextByte();
	}
	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}
	public int[] nextIntArray(int n){
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public Integer[] nextIntegerArray(int n){
		Integer[] a = new Integer[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}


Measured memory_bytes: 24400
#include<bits/stdc++.h>
using namespace std;
 
int main() {
  int S;
  cin>>S;
  int h,m,s,a;
  h=S/3600;
  a=S%3600;
  m=a/60;
  s=a%60;
  cout<<h<<":"<<m<<":"<<s<<endl;
}


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

//-----------------------
const int MOD = (int)1e9 + 7;
struct mint { int n; mint(int n_ = 0) : n(n_ % MOD) { if (n < 0) n += MOD; } };
mint operator+(mint a, mint b) { return (a.n += b.n) >= MOD ? a.n - MOD : a.n; }
mint operator-(mint a, mint b) { return (a.n -= b.n) < 0 ? a.n + MOD : a.n; }
mint operator*(mint a, mint b) { return 1LL * a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
ostream &operator<<(ostream &os, mint a) { return os << a.n; }
istream &operator>>(istream &is, mint& a) { return is >> a.n; }
mint inv(mint x) { long long a = x.n, b = MOD, u = 1, v = 0;
    while (b) { long long t = a/b; swap((a -= t*b), b); swap((u -= t*v), v); }
    return mint(u); }
mint operator^(mint a, long long n) { mint r = 1;
    while (n) { if (n & 1) r *= a; a *= a; n >>= 1; } return r; }
bool operator<(const mint &a, const mint &b) { return a.n < b.n; }
//-----------------------

mint dp[10101][101][2];
int main() {
    string s; cin >> s;
    int D; cin >> D;
    int n = s.size();
    dp[0][0][0] = 1;
    for (int i = 0; i < n; i++) for (bool lt: {true, false}) {
        for (int r = 0; r < D; r++) {
            int cur = s[i] - '0';
            for (int d = 0; d < 10; d++) if (lt || d <= cur) {
                dp[i + 1][(r + d) % D][lt | (d < cur)] += dp[i][r][lt];
            }
        }
    }
    cout << dp[n][0][0] + dp[n][0][1] - 1 << endl;  // 0 を除く
    return 0;
}

Measured memory_bytes: 8192
//#typedef int ll
// include
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <tuple>
// namespace
using namespace std;
// typedef
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pint;
// const
const int inf = 1e9;
const int mod = ((int)1e9 + 7);
// loop
#define rep(i, n) for(int i = 0, i##_mx = n; i < i##_mx; i++)
#define brep(i, b, e) for(int i = b; i < e; i++)
#define erep(i, b, e) for(int i = b; i <= e; i++)
#define rrep(i, b, e) for(int i = b; i >= e; i--)
// STL
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
// function
int gcd(int a, int b){return b ? gcd(b, a % b) : a;}
void prime(vint& v, int n){brep(i, 2, n){bool fg = false; for(auto e : v){if(i % e == 0){fg = true;}} if(fg){continue;} v.pb(i);}}
void in(vint& v){for(auto& i : v){cin >> i;}}

signed main(){
    string input;
    cin >> input;
    
    ll pow2[100001] = {0, 1};
    int cnt = 1;
    for(int i = 0; i < input.length(); i++){
        if(input[i] == '1'){
            cnt++;
            pow2[cnt] = (pow2[cnt - 1] * 2) % mod;
        }
    }
    cnt--;
    
    ll ans = 1;
    int i = input.length() - 1;
    ll third = 1;
    while(i >= 0){
        if(input[i] == '1'){
            ans += ((third + 1) * pow2[cnt]) % mod;
            ans %= mod;
            cnt--;
        }
        third *= 3;
        third %= mod;
        i--;
    }
    
    cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 1280
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define REPr(i,n) for(int i=(n)-1;i>=0; --i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define FORqr(i, m , n) for(int i = (n);i >=(m);--i)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define SCLLD(n) scanf("%lld",&n)
#define SCLLD2(m,n) scanf("%lld%lld",&m,&n)
#define SCLLD3(m,n,k) scanf("%lld%lld%lld",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define ARSCD(A,N) REP(i,N){SCD(A[i]);}
#define ARSCD1(A,N) FORq(i,1,N){SCD(A[i]);}
#define VSCD(v,N) REP(i,N){int (x); SCD(x); v.PB(x);}
#define VSCLLD(v,N) REP(i,N){long long (x); SCLLD(x); v.PB(x);}
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n)
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define SIN(x,S) (S.count(x) != 0)
#define M0(x) memset(x,0,sizeof(x))
#define FILL(x,y) memset(x,y,sizeof(x))
#define MM(x) memset(x,-1,sizeof(x))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long> PLL;
typedef vector<int> VI;
typedef vector < VI > VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const ll MOD = 1000000007;
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

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;
}
///////////////////////////////////////////////
/*（๑•﹏•๑｀) */
int main() {
    int N,Q;
    cin >> N >> Q;
    string S;
    cin >> S;

    static int dp[200002] = {};

    REP(i,N){
        if (i==0) {
            dp[0] = 0;
            continue;
        }

        if ((S[i] == 'C') and (S[i-1] == 'A')){
            dp[i] = dp[i-1] + 1;
        }else{
            dp[i] = dp[i-1];
        }

    }

    REP(i,Q){
        int l,r;
        cin >> l >> r;
        l--; r--;
        int ans = 0;

        ans = dp[r] - dp[l];
        cout << ans << "\n";
    }
}


Measured memory_bytes: 1408
N = int(input())
A = {}
a = [int(i) for i in input().split()]
for i in range(N):
    if a[i] in A:
        A[a[i]] += 1
    else:
        A[a[i]] = 1

ans = []
A = list(sorted(list(A.items()), reverse=True))
for i in range(N):
    if A[i][1] >= 2:
        ans.append(A[i][0])
    if A[i][1] >= 4:
        ans.append(A[i][0])

    if len(ans) >= 2:
        print(ans[0]*ans[1])
        break
else:
    print(0)


Measured memory_bytes: 21772
#include <iostream>
#include <algorithm>
#include <string> 
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;

int main()
{
	int A, B, X;
	cin >> A >> B >> X;
	if (X - A <= B && A <= X) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=6e5+5;
int n,m,x,k,a[N],tree[N<<2],ans=0;
void pushup(int i)
{
    tree[i]=max(tree[i<<1],tree[i<<1|1]);
}

void update(int i,int l,int r,int x,int k)
{
    if(l==r)
    {
        tree[i]=max(tree[i],k);
        return ;
    }
    int mid=l+(r-l)/2;
    if(x<=mid) update(i<<1,l,mid,x,k);
    else update(i<<1|1,mid+1,r,x,k);
    pushup(i);
}

int query(int i,int l,int r,int m,int n)
{
    if(m<=l && r<=n) return tree[i];
    int ans=0,mid=l+(r-l)/2;
    if(m<=mid) ans=max(ans,query(i<<1,l,mid,m,n));
    if(n>mid) ans=max(ans,query(i<<1|1,mid+1,r,m,n));
    return ans;
}

int main(){
    //memset(tree,0,sizeof(tree));
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]++;
    for(int i=1;i<=n;i++){
        x=query(1,1,300001,max(a[i]-k,1),min(a[i]+k,300001))+1;
        ans=max(ans,x);
        update(1,1,300001,a[i],x);
    }
    printf("%d",ans);
    return 0;
}

Measured memory_bytes: 8960
def main():
    n = int(input())

    pt, px, py = 0, 0, 0
    for _ in range(n):
        t, x, y = map(int, input().split())
        dt = t - pt
        dx = abs(x - px)
        dy = abs(y - py)
        if (dx + dy) % 2 != dt % 2:
            print('No')
            return
        if (dx + dy) > dt:
            print('No')
            return
    print('Yes')


if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
// #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 (int i = 0; i < n; ++i)
#define REP(i, n) for (int 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
#define eb emplace_back
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef vector<ll> vec;
typedef vector<P> pvec;
typedef vector<vector<ll>> vvec;
typedef vector<vector<P>> pvvec;
typedef priority_queue<ll> PQI;
typedef priority_queue<P> PQP;
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 n,l;
    cin>>n>>l;
    vector<string> s(n);
    rep(i,n) cin>>s[i];
    sort(all(s));
    rep(i,n) cout<<s[i];
    
    cout<<endl;
}

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

int main(){
  int N, M;
  cin >> N >> M;
  vector<int> vec(N);
  for(int i=0; i<N; i++){
    cin >> vec.at(i);
  }
  sort(vec.begin(),vec.end());
  
  vector<vector<int>> card(M,vector<int>(2));
  for(int i=0; i<M; i++){
    for(int j=0; j<2; j++){
      cin >> card.at(i).at(j);
    }
    swap(card.at(i).at(0),card.at(i).at(1));
  }
  sort(card.rbegin(),card.rend());
  
  vector<int> change(N);
  int a = 0;
  int b = 0;
  while(a < N){
    while(card.at(b).at(1) > 0){
      change.at(a) = card.at(b).at(0);
      a++;
      if(a == N){
        break;
      }
      card.at(b).at(1)--;
      if(card.at(b).at(1) == 0){
        b++;
        if(b == M){
          a = N;
        }
        break;
      }
    }
  }
  
  int64_t sum = 0;
  int j = 0;
  bool end = true;
  for(int i=0; i<N; i++){
    while(end){
      if(vec.at(i) >= change.at(j)){
        j++;
        if(j == N){
          end = false;
          break;
        }
      }
      else{
        vec.at(i) = change.at(j);
        j++;
        if(j == N){
          end = false;
          break;
        }
        break;
      }
    }
    //cout << vec.at(i) << endl;
    sum += vec.at(i);
  }
  cout << sum << endl;
}

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

#define REP(i,n) for(int i=0; i < (n); ++i)
#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)

using ll = long long;
#define INF 1e9
const int mod = 1e9+7;
vector<vector<ll>> dp;
int H, W, K;

int main(){
  cin >> H >> W >> K;
  dp.resize(H+1, vector<ll>(W+1,0));

  dp[0][0] = 1;
  REP(i, H){
    REP(j, W){
      REP(b, 1<<(W-1)){
        bool flag = true;
        REP(k, W-2){
          if((b&(1<<k)) && (b&(1<<(k+1)))) flag = false;
        }
        if(!flag) continue;
        int p = j;
        if(b & (1<<j)) p = j+1;
        else if(j > 0 && (b & (1<<(j-1)))) p = j-1;
        dp[i+1][p] += dp[i][j];
        dp[i+1][p] %= mod;
      }
    }
  }
  cout << dp[H][K-1] << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
typedef vector<int> vi;
typedef vector<ll> vll;

int gcd(int a,int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}

int GCD(int a,int b,int c)
{
    return gcd(gcd(a,b),c);
}

int main()
{
    int k;
    cin>>k;
    ll sum=0;
    for(int a=1;a<=k;a++)
    {
        for(int b=1;b<=k;b++)
        {
            for(int c=1;c<=k;c++)
            {
                sum+=GCD(a,b,c);
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}



Measured memory_bytes: 3636
#include <iostream>

using namespace std;

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;

  bool turn = true;
  while (a > 0 && c > 0) {
    if (turn) {
      c -= b;
      turn = false;
    } else {
      a -= d;
      turn = true;
    }
  }

  if (a <= 0) cout << "No\n";
  else cout << "Yes\n";

  return 0;
}

Measured memory_bytes: 3104
using System;
using System.Threading;

public class calculator
{
    public static void Main()
    {
        while (true)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            if (line[1] == "?") goto readend;
            var a = int.Parse(line[0]);
            var b = int.Parse(line[2]);
            switch(line[1])
            {
                case "+":
                    Console.WriteLine(a + b);
                    break;
                case "-":
                    Console.WriteLine(a - b);
                    break;
                case "*":
                    Console.WriteLine(a * b);
                    break;
                case "/":
                    Console.WriteLine(a / b);
                    break;
                default:
                    break;


            }
        }
    readend:;
    }
}


Measured memory_bytes: 17932
/***********************************************************
 *This code By @1353055672(Ligen)                          *
 *[Warning]You're not excepted to understand this code!    *
 *NOIP 2019 RP++                                      *
 ***********************************************************/
#pragma GCC optimize(3)
#define ADD_STACK int size = 512 << 20;\
    char *p = (char*)malloc(size) + size;  \
    __asm__("movl %0, %%esp\n" :: "r"(p))
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include<ctime>
#include<vector>
#define ll long long
#define ull unsigned long long
#define mn 200020
#define Max(x,y) ((x>y)?x:y)
#define Min(x,y) ((x<y)?x:y)
#define Abs(x) (((x)<(0))?(-(x)):(x))
#define infll (ll)(1e18)
#define infint (int)(1<<30)
#define mod (int)(19260817)
#define FOR(a,b,c) for (register int a=b;a<=c;a++)
#define FORD(a,b,c) for (register int a=b;a>=c;a--)
using namespace std;
//char buf[1<<20],*p1=buf,*p2=buf;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
inline int read(){
    int x=0,f=1;char c;
    for(c=getchar();c<'0'||c>'9';f=((c=='-')?-1:f),c=getchar());
    for(;c>='0'&&c<='9';x=x*10+c-'0',c=getchar());return x*f;
}
template<typename T>
inline void write(T a){
    if(a==0){putchar('0');return;}if(a<0)putchar('-'),a=-a;char c1[120];int h=0;
    while(a)c1[++h]=a%10+'0',a/=10;FORD(i,h,1)putchar(c1[i]);
}
inline void write_(){return;}
template<typename T,typename... Args>
inline void write_(T a,Args... b){write(a);putchar(' ');write_(b...);}
inline void writeln(){putchar('\n');return;}
template<typename T,typename... Args>
inline void writeln(T a,Args... b){write(a);putchar(' ');writeln(b...);}
//need c++11

//inline void write(ll a){
//    if(a==0){putchar('0');return;}if(a<0)putchar('-'),a=-a;char c1[120];int h=0;
//    while(a)c1[++h]=a%10+'0',a/=10;FORD(i,h,1)putchar(c1[i]);
//}
//inline void write_(ll a){write(a);putchar(' ');}
//inline void writeln(ll a){write(a);putchar('\n');}
inline ll gcd(ll a,ll b){return a==0?b:gcd(b%a,a);}
inline ll lcm(ll a,ll b){return 1ll*a/gcd(a,b)*b;}
inline ll Pow(ll n,ll a){ll b=1;while(a){if(a&1)b=1ll*b*n%mod;n=1ll*n*n%mod;a>>=1;}return b;}
//---------------------Head Files--------------------------//
struct node{
	string s;
	int x,h;
}a[120];
int n;
inline bool cmp(const node &a,const node &b){return ((a.s==b.s)?(a.x>b.x):(a.s<b.s));}
signed main(){
    n=read();
    FOR(i,1,n){
    	cin>>a[i].s;a[i].x=read();a[i].h=i;
    }
    sort(a+1,a+1+n,cmp);
    FOR(i,1,n)writeln(a[i].h);
    return 0;
}


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

int main(int argc, char const *argv[])
{
	int a, b, c, d; cin >> a >> b >> c >> d;
	if ((abs(a - b) <= d && abs(b - c) <= d) || abs(a - c) <= d) printf("Yes\n");
	else printf("No\n");
	return 0;
}

Measured memory_bytes: 256
def main():
    N, M = map(int, input().split())

    # 場所1からいける場所
    s = []

    # 場所Nへいける場所
    g = []

    for i in range(M):
        a,b = map(int, input().split())
        if a == 1:
            s.append(b)
        if b == 1:
            s.append(a)
        if a == N:
            g.append(b)
        if b == N:
            g.append(a)

    s = set(s)
    g = set(g)

    for i in s:
        if i in g:
            print('POSSIBLE')
            exit()
    print('IMPOSSIBLE')

main()

Measured memory_bytes: 72536
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forin(in, n) for(int i=0; i<n; i++) cin>>in[i]
#define forout(out, n) for(int i=0; i<n; i++) cout<<out[i]<<endl

int main(){
    int K, S; cin>>K>>S;
    int cnt=0;

    for(int x=0; x<=K; x++){
        for(int y=0; y<=K; y++){
            if(x+ y+ max(0, S-x-y) ==S && S-x-y<=K){
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
}

Measured memory_bytes: 3584
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    A = []
    for _ in range(N):
        a = int(input())
        A.append(a)

    B = []
    C = []
    for a in A:
        if a != 0:
            C.append(a)
        else:
            B.append(C)
            C = []
    if len(C) > 0:
        B.append(C)

    res = 0
    for b in B:
        res += sum(b) // 2
    print(res)


if __name__ == '__main__':
    main()


Measured memory_bytes: 52056
def warshall_floyd(w):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                w[i][j] = min(w[i][j],w[i][k]+w[k][j])
    return w

n,m = map(int,input().split())

d = [[10**7]*n for i in range(n)]
w = [[10**10]*n for i in range(n)]

for i in range(m):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    w[a][b] = c
    w[b][a] = c
    d[a][b] = c
    d[b][a] = c
for i in range(n):
    w[i][i] = 0

w = warshall_floyd(w)


res = 0
for i in range(n):
    for j in range(n):
        if w[i][j] == d[i][j]:
            res += 1
print(m-res//2)


Measured memory_bytes: 40688
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#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++)
using namespace std;

char S[4];

int main(){
  // 配列に格納
  rep(i, 4){
    cin >> S[i];
  }
  // 並び替えてから条件判定
  sort(S, S+4);
  if (S[0]==S[1] && S[1]!=S[2] && S[2]==S[3]) cout << "Yes" << endl;
  // 出力
  else cout << "No" << endl;
}

Measured memory_bytes: 3572
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
//template<class Key, class Compare = less<Key>>
//using Tree = tree<Key, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;




void Main() {
    int n, k;
    cin >> n >> k;
    cout << (((n+1)/2 >= k) ? "YES" : "NO");
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef _DEBUG
//        freopen("in.txt", "r", stdin);
//        freopen("out.txt", "w", stdout);
    #endif
    Main();
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

int main(){
    int n, m;
    cin >> n >> m;
    cout << n * (n-1) / 2 + m * (m-1) / 2 << endl;
    return 0;
}

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

using namespace std;

class Mat
{

};

int main()
{        
    while(1)
    {
        int m,f,r;
        cin >> m >> f >> r;
        if(m == -1 && f == -1 && r == -1)
        {
            return 0;
        }
        else if(m < 0 || f < 0)
        {
            cout  << "F";
        }
        else if((m + f) >= 80)
        {
            cout << "A";
        }
        else if((m + f) >= 65)
        {
            cout << "B";
        }
        else if((m + f) >= 50)
        {
            cout << "C";
        }
        else if((m + f) >= 30)
        {
            if(r >= 50)
            {
                cout << "C";
            }
            else
            {
                cout << "D";
            }
        }
        else
        {
            cout << "F";
        }
        cout << endl;
    }
}

Measured memory_bytes: 1152
#include <bits/stdc++.h>
  
using namespace std;
  
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
  
#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
  
#define ordered_set tree<int , null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update> 
  
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
  
const int inf = int(1e9);
const int mod = inf + 7;
const int N = 1e6 + 555; // TL MB
const double PI = acos(-1.0);
 
void mul(ll &x, ll y){
	x *= y;
	if(x >= mod) x %= mod;
}
 
void add(ll &x, ll y){
	x += y;
	if(x >= mod) x -= mod;
	if(x < 0) x += mod;
}
ll bp(ll a, ll n){
	ll r = 1;
	while(n){
		if(n & 1) mul(r, a);
		mul(a, a);
		n >>= 1;
	}
	return r;
}		

void solve()
{
	stack<char> st;
	string s;
	cin >> s;
	rep(i, sz(s)){
		if(st.empty() || s[i] == st.top()) st.push(s[i]);
		else st.pop();
	}
	cout << sz(s) - sz(st);
}



 
 
int main()
{
    Red;
  	// int T; cin >> T; while(T--)
    solve();
    return 0;
}

Measured memory_bytes: 512
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace _20170409_AOJ
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;

            for (; (line = Console.ReadLine()) != "";)
            {
                string[] sArr = line.Split(' ');
                int h = int.Parse(sArr[0]);
                int w = int.Parse(sArr[1]);

                if (h == 0 && w == 0)
                {
                    break;
                }

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < h; i++)
                {
                    for(int j=0; j<w;j++)
                    {
                        if((i+j)%2==0)
                        {
                            sb.Append('#');
                        }
                        else
                        {
                            sb.Append('.');
                        }
                    }
                    sb.AppendLine();
                }

                Console.WriteLine(sb.ToString());
            }

            Console.ReadLine();
        }
    }
}

Measured memory_bytes: 9068
import java.util.*;
import static java.lang.System.*;

class Guidebook implements Comparable<Guidebook>{
  public String city;
  public int score;
  public int number;
  
  public int compareTo(Guidebook obj){
    if(this.city.equals(obj.city)){
      return obj.score - this.score;
    }else{
      return this.city.compareTo(obj.city);
    }
  }
}

class Main {
  public static final int MOD = 1000000007;
  public static final int INF = 1000000000;
  public static void main(String[] args) {
    Scanner sc = new Scanner(in);
    int N = sc.nextInt();
    Guidebook X[] = new Guidebook[N];
    for(int i=0; i<N; i++){
      X[i] = new Guidebook();
      X[i].city = sc.next();
      X[i].score = sc.nextInt();
      X[i].number = i+1;
    }
    Arrays.sort(X);
    for(int i=0; i<N; i++){
      out.println(X[i].number);
    }
  }

  public static int gcd(int a, int b){
    if(b == 0) return a;
    return gcd(b, a%b);
  }
}



Measured memory_bytes: 25044
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    char ch;
  cin>>ch;
  if(ch>='a' && ch<='z') cout<<'a'<<endl;
  else cout<<'A'<<endl;
}

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

int N; vector<int>G;

int main() {
	cin >> N;
	for (int i = 2; i <= 1000; i++) {
		int S = i * i;
		while (S <= N) {
			G.push_back(S);
			S *= i;
		}
	}
	G.push_back(1);
	sort(G.begin(), G.end());
	G.erase(unique(G.begin(), G.end()), G.end());
	cout << G[G.size() - 1] << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(a);i>(b);--i)
#define eper(i,a,b) for(int i=(a);i>=b;--i)
#define fore(i, x, a) for(auto &&x:a) 
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define ALL(x) begin(x),end(x)
#define F first
#define S second
const int inf=1001001001; const long long INF=1001001001001001001;
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using vii = vector<int>;
using vll = vector<ll>;
template<class T>using  PS_queue = priority_queue<T, vector<T>, greater<T> >;
template<class T>using vv = vector<T>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );}
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T>T gcd(T a, T b){ return b ? gcd(b, a % b) : a; }
template<class T>T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct edge { int to, from, cost; };
int dy[]={0, 1, -1, 0}; int dx[]={1, 0, 0, -1};

int n;
int main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
  cin >> n;
  vii a(n);
  cin >> a;
  int win_A = 0;
  rep(i, 0, n) {
    if (a[i] % 2 == 0) win_A++;
  }
  if (win_A != n) cout << "first" << endl;
  else cout << "second" << endl;
  return 0;
}



Measured memory_bytes: 640
#include <bits/stdc++.h>
#define rep(i,n) for (int (i) = 0; (i) < (n); i++)
#define ll long long
using namespace std;
int main(){
  long double W = 0, H = 0, x = 0, y = 0;
  cin >> W >> H >> x >> y;
  cout << fixed << setprecision(12);
  cout << (W*H)/2 << " ";
  if(W/2 == x && H/2 == y){
    cout << "1";
  }
  else{
    cout << "0";
  }
}

Measured memory_bytes: 384
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

ans = 0
memo = -100
for i in range(N):
  dish = A[i]
  base = B[dish - 1]
  ans += base
  if dish - memo == 1:
    ans += C[dish - 2]
  memo = dish
  
    
print(ans)

Measured memory_bytes: 3064
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <math.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <climits>
#include <bitset>
#include <list>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int main(){
    int n;
    cin >> n;
    int v[n], c[n];
    for(int i=0; i<n; i++) cin >> v[i];
    for(int i=0; i<n; i++) cin >> c[i];
    int ans=0;
    for(int i=0; i<n; i++) ans+=max(v[i]-c[i],0);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
# coding: utf-8
import sys
import decimal

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

A, B = sr().split()
A = int(A)
B = decimal.Decimal(B) * 100
answer = A*B // 100
print(answer)


Measured memory_bytes: 10068
#include <bits/stdc++.h>
#define deb(x) cout << #x << " --> " << x << endl;
#define print(x)                         \
    for (auto _x : x) cout << _x << " "; \
    cout << endl
#define pb push_back
typedef long long ll;
using namespace std;

// 2c+2t=2x
// 2c+4t=y
// 2t=y-2x
// t=((y-2x)/2) ; c=x-t

int main() {
    int x, y;
    cin >> x >> y;
    int t, c;
    t = (y - 2 * x) / 2;
    c = (x - t);
    // deb(t)deb(c)
    if ((c + t == x) && (2 * c + 4 * t == y) && c >= 0 && t >= 0)
        cout << "Yes\n";
    else
        cout << "No\n";
}


Measured memory_bytes: 3600
from collections import defaultdict

N = int(input())
S = input()
d = defaultdict(int)
for char in S:
    d[char] += 1

temp = 1
for key in d:
    # print(key, d[key])
    temp *= d[key] + 1
    temp %= 10**9 + 7

ans = temp - 1
print(ans)


Measured memory_bytes: 9408
//abc065_b.cpp
//Tue Oct 30 22:16:42 2018

#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <math.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll=long long;
typedef pair<int,int> P;

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

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

	int current = 0;
	int flag = 0;
	int count = 0;
	while (current != 1){
		if (passed[current] != 0){
			flag = 1;
			break;
		}
		passed[current] = 1;
		current = a[current]-1;
		count++;
	}

	if (flag == 0){
		cout << count << endl;
	}else {
		cout << -1 << endl;
	}
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

const int INF = 1001001001;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    vi h(n);
    rep(i, n) cin >> h[i];
    vi dp(n+2, INF);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
        chmin(dp[i+1], dp[i] + abs(h[i+1] - h[i]));
        chmin(dp[i+2], dp[i] + abs(h[i+2] - h[i]));
    }
    cout << dp[n-1] << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
int main() {
	int n;
	string s;
	cin >> n >> s;
	int cnt = 0;
	for (char c : s)
		if(c == 'R')
			++cnt;
	int ans = 0;
	for (int i = 0; i < cnt; ++i)
		if(s[i] == 'W')
			++ans;
	cout << ans << endl;
}

Measured memory_bytes: 3728
#------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
#-------------------game starts now----------------------------------------------------
from collections import defaultdict,deque 
n,m=map(int,input().split())
d=defaultdict(list)
for i in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    d[a].append(b)
    d[b].append(a)
l=[0]
dist=[0]*n
l=deque(l)
while(l):
    s=l.popleft()
    for i in d[s]:
        if dist[i]==0:
            dist[i]=s+1
            l.append(i)
if 0 in dist[1:]:
    print("No")
else: 
    print("Yes")
    print(*dist[1:],sep="\n")
    

Measured memory_bytes: 104812
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <cmath>
#include <climits>
#include <cstdint>
#include <string>
using namespace std;

inline namespace atcoder {
    template <typename T, size_t Depth>
    struct vector_helper { using type = vector<typename vector_helper<T, Depth - 1>::type>; };
    template <typename T>
    struct vector_helper<T, 0> { using type = T; };
    template <typename T, size_t Depth>
    using vector_t = typename vector_helper<T, Depth>::type;

    template <typename T> using vec = vector_t<T, 1>;
    template <typename T> using vvec = vector_t<T, 2>;
    template <typename T> using vvvec = vector_t<T, 3>;
}

int main() {
    int n, k; cin >> n >> k;
    string s; cin >> s;

    vec<int> zsum;
    for (int i = 0; i < s.size();) {
        bool found = false;
        while (i < s.size() && s[i] == '1') ++i;
        while (i < s.size() && s[i] == '0') {
            found = true;
            ++i;
        }
        while (i < s.size() && s[i] == '1') ++i;
        if (!found) continue;
        zsum.push_back(i);
    }

    if (zsum.size() == 0) {
        cout << s.size() << endl;
        return 0;
    }

    int ans = 0;
    for (int i = 0, cnt = 0; i < s.size(); ++cnt) {
        ans = max(ans, zsum[min((size_t)(cnt + k), zsum.size()) - 1] - i);

        while (i < s.size() && s[i] == '1') ++i;
        while (i < s.size() && s[i] == '0') ++i;
    }

    cout << ans << endl;
}



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

int n, A[21], q, m[201];

bool solve(int i, int m) {
  if ( m == 0 ) return true;
  else if ( i >= n ) return false;
  bool res = solve(i + 1, m) || solve(i + 1, m - A[i]);
  return res;
}

int main() {

  cin >> n;
  for ( int i = 0; i < n; i++ ) cin >> A[i];

  cin >> q;
  for ( int i = 0; i < q; i++ ) cin >> m[i];
  for ( int i = 0; i < q; i++ ) {
    if ( solve(0, m[i]) ) cout << "yes\n";
    else cout << "no\n";
  }

  return 0;
}

Measured memory_bytes: 3060
#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

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

ll dpRec(int l, int r, vector<vector<ll>>& dp, vector<LP> rc) {
    if (dp[l][r] != INT_MAX) return dp[l][r];
    if (r - l == 1) {
        dp[l][r] = 0;
        return 0;
    }
    for (int i = l + 1; i < r; ++i) {
        chmin(dp[l][r], dpRec(l, i, dp, rc) + dpRec(i, r, dp, rc) + rc[l].first * rc[i].first * rc[r - 1].second);
    }
    return dp[l][r];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin>>n;
    vector<LP> rc(n);
    rep(i, n) cin>>rc[i].first>>rc[i].second;

    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, INT_MAX));
    // for (int i = 2; i < n + 1; ++i) {           // 行列をi個選ぶ
    //     for (int j = 0; j < n - i + 1; ++j) {   // j番目の行列以降から
    //         for (int k = 1; k < i; ++k) {       // j番目からk個までとそれ以降の積
    //             chmin(dp[i][j], dp[k][j] + dp[i - k][j + k] + rc[j].first * rc[j + k].first * rc[j + i].first);
    //         }
    //     }
    // }
    cout<<dpRec(0, n, dp, rc)<<endl;
}


Measured memory_bytes: 3272