akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>

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

typedef long long int lli;
using namespace std;
// #define int long long int

inline void swap(int &a, int &b) {
    int tmp = a;
    a = b;
    b = tmp;
}

inline int euclid(int a, int b) {
    if (a > b) swap(a, b);
    int r = a % b;
    while (r > 0) {
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

int main() {
    int N, K;
    string S;
    cin >> N >> K;
    cin >> S;

    vector<int> num;
    for (int i = 0; i < N; i++) {
        int j = i;
        while (j + 1 < N && S[j] == S[j + 1]) ++j;
        num.push_back(j - i + 1);
        i = j;
    }
    int M = num.size();

    vector<int> sum;
    sum.push_back(0);
    for (int i = 0; i < M; i++) {
        sum.push_back(num[i] + sum[i]);
    }

    int ret = 0;
    int index = 0;
    if (S[0] == '0') {
        int range = min(2 * K, M);
        ret = sum[range];
        index++;
    }
    for (;index <= M; index += 2) {
        int range = min(2 * K + 1 + index, M);
        ret = max(sum[range] - sum[index], ret);
    }
    if (S[N - 1] == '0') {
        int range = max(M - 2 * K, 0);
        ret = max(sum[M] - sum[range], ret);
    }
    cout << ret << endl;
}


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

using namespace std;

#define int long long
#define ALL(x) (x).begin(),(x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int,int> PI;
typedef pair<int,pair<int,int>> PII;
static const int INF=1010000000000000017LL;
static const double eps=1e-12;
static const double pi=3.14159265358979323846;
static const int dx[4]={1,-1,0,0};
static const int dy[4]={0,0,1,-1};
static const int ddx[8]={1,-1,0,0,1,1,-1,-1};
static const int ddy[8]={0,0,1,-1,1,-1,1,-1};

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 N,K;
int A[101];

signed main(){
    cin>>N>>K;
    for(int i=0;i<K;++i){
        int d;
        cin>>d;
        for(int j=0;j<d;++j){
            int a;
            cin>>a;
            A[a]++;
        }
    }
    int ans=0;
    for(int i=1;i<=N;++i)if(A[i]==0)ans++;
    cout<<ans<<endl;
}

Measured memory_bytes: 3552
import java.util.*;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      long[] a = new long[n];
      long sum = 0;
      long ans = 0;
      for(int i = 0;i < n;i++)a[i]=sc.nextLong();
      for(int i = 0;i < n;i++){
        if(a[i]!=0)sum+=a[i];
        else{
          ans += sum/2;
          sum = 0;
        }
      }
      ans+=sum/2;
      System.out.println(ans);
    }
}


Measured memory_bytes: 59852
def gcd(a,b):
	if b>a:
		a,b = b,a
	r = a%b
	if r==0:
		return b
	else:
		return gcd(b, a%b)

def solve(a,b):
	if a==1:
		return 1,0
	if b==1:
		return 0,1
	if a>b:
		q = a//b
		r = a%b
		x, y = solve(r,b)
		return x, y-q*x
	if a<b:
		q = b//a
		r = b%a
		x, y = solve(a,r)
		return x-q*y, y

a,b = map(int, input().split())
g = gcd(a,b)
a//=g
b//=g
x, y = solve(a,b)

def p(n):
	return x+b*n
def q(n):
	return y-a*n

def f(n):
	return abs(p(n))+abs(q(n))

if a>b:
	n = y//a
	nn = n 
	m = f(n)
	if f(n-1) < m:
		nn = n-1
		m = f(n-1)
	if f(n+1) < m:
		nn = n+1
		m = f(n+1)
	x = p(nn)
	y = q(nn)
elif a<b:
	n = -x//b
	nn = n 
	m = f(n)
	if f(n-1) < m:
		nn = n-1
		m = f(n-1)
	if f(n+1) < m:
		nn = n+1
		m = f(n+1)
	x = p(nn)
	y = q(nn)
else:
	x = 0
	y = 1

print("{} {}".format(x,y))



Measured memory_bytes: 5616
N,Y=map(int,input().split())
# N=9
# Y=45000
l=0
for i in range(N+1):
    for j in range(N+1-i):
        k=N-i-j
        if (i*10000+j*5000+k*1000)==Y:
            l=[i,j,k]
            pass
if l==0:
    l=[-1,-1,-1]
print(*l,sep=' ')

Measured memory_bytes: 3060
import java.io.BufferedReader;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;

public class Main {
	public static void main(final String[] args){
    
    final Scanner sc = new Scanner(System.in);
    final int n = sc.nextInt();
    //final List<Integer> list = new ArrayList<>();
    int d = sc.nextInt();
    int sum = 0;
    int count = 0;

    int[][] list = new int[n][d];

    for(int i = 0; i < n ; i++){
        for(int j = 0; j < d ; j++){
            list[i][j] = sc.nextInt();
        }
    }

    for(int i = 0; i < n ; i++){
        for(int j = i + 1; j < n ; j++){
            for(int a = 0 ; a < d ; a++){
                sum += (list[i][a] - list[j][a])*(list[i][a] - list[j][a]);
            }
            if(Math.sqrt(sum) == (int)Math.sqrt(sum)){
                count++;
            }
            sum = 0;
        }
    }

    System.out.println(count);
    




    

    



    
  

    


    


    

    
    
    



    





}
}

Measured memory_bytes: 36004
N=int(input())
S=input()

Lis=[[] for _ in range(3)]
RGB="RGB"

for i in range(N):
    idx=RGB.index(S[i])
    Lis[idx].append(i+1)

if len(Lis[0])==0 or len(Lis[1])==0 or len(Lis[2])==0:
    print(0)
    exit()

Ng=len(Lis[2])
ans=0
for r in Lis[0]:
    for g in Lis[1]:
        n1=2*r-g
        n2=2*g-r
        n3=(r+g)//2
        Su=Ng
        if N>=n1>0:
            if S[n1-1]=="B":
                Su-=1
        if N>=n2>0:
            if S[n2-1]=="B":
                Su-=1
        if (g-r)%2==0:
            if S[n3-1]=="B":
                Su-=1
        ans+=Su
print(ans)

Measured memory_bytes: 9264
#include <stdio.h>
 
int main()
{
	long long d,t,s;
  scanf("%lld %lld %lld",&d,&t,&s);
  long long ans = t*s;
  if(ans >= d){
  	printf("Yes\n");
  }
  else{
  	printf("No\n");
  }
  return 0;
}

Measured memory_bytes: 1624
#include <bits/stdc++.h>
#define N 100020
#define ll long long
using namespace std;
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
map<ll, bool> mp;
std::vector<ll> res;
int main(int argc, char const *argv[]) {
	int n = read();
	for (int i = 1; i <= n; i++) {
		ll x = read();
		if (mp[x]) res.push_back(x), mp[x] = 0;
		else mp[x] = 1;
	}
	sort(res.begin(), res.end());
	if (res.size() < 2) return puts("0")&0;
	else cout << res[res.size()-1] * res[res.size()-2] << endl;
	return 0;
}


Measured memory_bytes: 6528
import java.io.IOException;

public class Main {

	public static void main(String[] args) {
		while (true) {
			int e = nextInt();
			if (e == 0) {
				break;
			}

			int m = Integer.MAX_VALUE;
			for (int z = 0; z * z * z <= e; z++) {
				for (int y = 0; y * y + z * z * z <= e; y++) {
					int x = e - z * z * z - y * y;
					m = Math.min(m, x + y + z);
				}
			}
			System.out.println(m);
		}

	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return -1;
	}

}

Measured memory_bytes: 20500
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define all(obj) (obj).begin(),(obj).end() 
#define dump(x)  cout << #x << " = " << (x) << endl;
typedef long long lint;
using namespace std;
int main(){
	int x;
	cin>>x;
	cout<<(x==7||x==5||x==3?"YES":"NO")<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define crap ios_base::sync_with_stdio(0);cin.tie(0)
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define endl "\n"
#define int ll

using namespace std;

double dp[310][310][310];
int st[4];
int N;

double dfs(int a,int b,int c){
	if(a+b+c==0) return 0.0;

	if(dp[a][b][c]!=-1) return dp[a][b][c];
	double ks = (double)N/(a+b+c);
	double ret=0;
	if(a){
		ret+= (dfs(a-1,b,c) + ks)*a/(a+b+c);
	}
	if(b){
		ret+= (dfs(a+1,b-1,c) + ks)*b/(a+b+c);
	}
	if(c){
		ret+= (dfs(a,b+1,c-1) + ks)*c/(a+b+c);
	}
	return dp[a][b][c] = ret;

}


int32_t main()
{
    
   
    crap;
    
    int a;
    cin>>a;
    N= a;
    for(int i=0;i<a;i++){
    	int t; cin>>t;
    	st[t]++;
    }

    for(int i=0;i<=a;i++){
    	for(int j=0;j<=a;j++){
    		for(int k=0;k<=a;k++)
    			dp[i][j][k]=-1;
    	}
    }
    double ret = dfs(st[1],st[2],st[3]);
    cout<<fixed<<setprecision(9)<<ret<<endl;


   
   
}

Measured memory_bytes: 227584
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

vec<int> C = {2,5,5,4,5,6,3,7,6};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M;
    cin >> N >> M;
    vec<int> A(M);
    for(int i=0;i<M;i++){
        cin >> A[i];
        A[i]--;;
    }
    sort(A.begin(),A.end());
    vec<int> dp(N+1,-1e9);
    dp[0] = 0;
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++) if(i+C[A[j]]<=N){
            dp[i+C[A[j]]] = max(dp[i+C[A[j]]],dp[i]+1);
        }
    }
    string ans = "";
    int now = N;
    while(now){
        for(int j=M-1;j>=0;j--) if(now-C[A[j]]>=0 && dp[now]==dp[now-C[A[j]]]+1){
            char c = '0'+(A[j]+1);
            ans += c;
            now -= C[A[j]];
            break;
        }
    }
    cout << ans << "\n";
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a)-1; (i) >= (b); (i)--)
#define PI acos(-1.0)
#define DEBUG(C) cout<< C <<endl;
#define VI vector <int>
#define VII vector <VI>
#define VL vector <LL>
#define VLL vector <VL>
#define VD vector <double>
#define VDD vector <VD>
#define PII pair <int, int>
#define PDD pair <double, double>
#define PLL pair <LL, LL>
#define VPII vector <PII>
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define REVERSE(a) reverse(ALL(a))
#define MP make_pair
#define FORE(a,b) for(auto &&a:b)

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9;
const int MOD = INF+7;
const LL LLINF = 1e18;

int H, W;
void out(vector <string> red, vector <string> blue) {
    REP(i, H) cout << red[i] << endl;
    cout << endl;
    REP(i, H) cout << blue[i] << endl;
}

vector <string> merge(vector <string> red, vector <string> blue) {
    vector <string> out(H);
    REP (i, H) REP (j, W) {
        if (red[i][j] == '#' && blue[i][j] == '#') {
            out[i] += '#';
        } else {
            out[i] += '.';
        }
    }
    // REP (i, H) {
    //     cout << out[i] << endl;
    // }
    return out;
}

int main(void) {
    cin >> H >> W;
    vector <string> a(H), red(H, string(W, '.')), blue(H, string(W, '.'));
    REP (i, H) {
        red[i][0] = '#';
        blue[i][W-1] = '#';
    }
    REP (i, H) cin >> a[i];
    REP (i, H) if (i%2 == 1) REP (j, W-1) {
        red[i][j] = '#';
    }
    REP (i, H) if (i%2 == 0) FOR (j, 1, W) {
        blue[i][j] = '#';
    }
    REP (i, H) REP (j, W) {
        if (a[i][j] == '#') {
            red[i][j] = '#';
            blue[i][j] = '#';
        }
    }
    out(red, blue);
    // if (merge(red, blue) == a) {
    //     cerr << "AC" << endl;
    // } else {
    //     cerr << "WA" << endl;
    // }
}


Measured memory_bytes: 2048
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll zero = 0;
ll INF = 10000000000;

int main(void){
    ll A,B;
    cin >> A >> B;
    ll D;
    D = __gcd(A, B);
    
    ll count = 1;
    for(ll i = 2; D >= i * i; i++){
        if(D%i  == 0){
            count++;
            while(D%i == 0){
                D = D/i;
            }
        }
    }
    if(D != 1) count++;
    printf("%lld\n", count);
}


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

//Surf Smelt Fishing Contest II
public class Main{

	int[] num, id;
	
	void update(int i){
		if(i <= 0)return;
		int L = 2*i, R = 2*i+1;
		if(num[L]==num[R]){
			num[i] = num[L]; id[i] = Math.min(id[L], id[R]);
		}
		else if(num[L] < num[R]){
			num[i] = num[R]; id[i] = id[R];
		}
		else{
			num[i] = num[L]; id[i] = id[L];
		}
		update(i>>1);
	}
	
	void run(){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), Q = sc.nextInt();
		int N = 1;
		while(N < n)N*=2;
		num = new int[2*N+1];
		id = new int[2*N+1];
		Arrays.fill(num, -1);
		for(int i=0;i<n;i++){
			id[N+i] = i+1;
			num[N+i] = 0;
			update((N+i)>>1);
		}
		while(Q--!=0){
			int a = sc.nextInt()-1, v = sc.nextInt();
			num[N+a]+=v;
			update((N+a)>>1);
			System.out.println(id[1]+" "+num[1]);
		}
	}
	
	public static void main(String[] args) {
		new Main().run();
	}
	
}

Measured memory_bytes: 114776
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  vector<int> data(5);
  int total, judge=0;
  
  for (int i = 0; i < 5; i++) {
    cin >> data.at(i);
  }
  
  for(int l=0;l<5;l++){
      for(int k=1;k<14;k++){
          if(data.at(l)<=(k*10)){
              if( (k*10-data.at(l)) > judge ){
                  judge = (k*10-data.at(l));
              }
              
              total+=(k*10);
              
              break;
          }
      }
  }
  
  total -=judge;
  
 
      cout << total << endl;
  
  
  
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORit(it,a) for(auto it=a.begin();it!=a.end();it++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define vec vector
#define vi vec<int>
#define vll vec<ll>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(),x.end()
#define testcases ll t;cin>>t;while(t--)
#define mem(a,k) memset(a,k,sizeof(a))
#define FF first
#define SS second
#define MP(x,y) make_pair(x,y)
#define rt return
#define br break
#define ct continue
#define elif else if
#define ii pair<int,int>
#define vecin(a,n,index) for(int i=index;i<n;i++)cin>>a[i]
#define vecout(a,n,index) for(int i=index;i<n;i++)cout<<a[i]<<" ";cout<<endl;
//ll mod = 1000000007;
int fun(int k) {
	int div = 1;
	FOR(i, 2, int(sqrt(k)) + 1) {
		if (k % i == 0) {
			int cnt = 0;
			while (k % i == 0) {cnt++; k /= i;}
			div = div * (cnt + 1);
		}
	}

	if (k > 1) {div = div * (2);}
	rt div;
}
void solve() {
	int n;
	cin >> n;
	int ans = 0;
	FOR(c, 1, n) {
		int k = n - c;
		ans += (fun(k));
	}
	cout << ans << endl;

}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);	cin.tie(NULL);	cout.tie(NULL);
	// START FROM HERE :)
	solve();
}

Measured memory_bytes: 3612
//cara pikir 
//pake recursive tapi gak 3 x
// C++ program for implementation of Ford Fulkerson algorithm 
#include <iostream> 
#include <limits.h> 
#include <string.h> 
#include <queue> 
#include <iostream> 
#include <set> 
#include <iterator> 
#include <algorithm>

using namespace std; 

int main(){
set<int>k;
int num1,num2,num3;
cin>>num1>>num2>>num3;
k.insert(num1);
k.insert(num2);
k.insert(num3);
if(k.size()==2){
cout<<"Yes"<<endl;
}else{
  cout<<"No"<<endl;
}
return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 998244353;
const ll MAX = 4000001;
const double eps = 1E-18;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

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

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};

class mint {
	long long x;
public:
	mint(long long x = 0) : x((x% MOD + MOD) % MOD) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += MOD - a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= MOD;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}

	mint operator^(const ll& t)const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}
	// for prime MOD
	mint inv() const {
		return pow(MOD - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}

	friend ostream& operator<<(ostream& os, const mint& m) {
		os << m.x;
		return os;
	}
};

///////////////////////////

vector<vector<ll>> g;
vector<ll> par;

void dfs(ll u, ll v) {
	for (ll w : g[v]) {
		if (u == w) { continue; }
		par[w] = v;
		dfs(v,w);
	}
}


int main() {
	ll N;
	cin >> N;
	g.resize(N + 1);
	repn(i, N - 1) {
		ll a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	par.assign(N + 1, 0);
	dfs(0, 1);

	ll M;
	cin >> M;
	vector<vector<bool>> path(M, vector<bool>(N + 1, 0));
	
	rep(i, M) {
		ll u, v;
		cin >> u >> v;

		for (ll x = u; par[x] != 0; x = par[x]) {
			path[i][x] = !path[i][x];
		}
		for (ll x = v; par[x] != 0; x = par[x]) {
			path[i][x] = !path[i][x];
		}
	}

	ll ans = 0;

	rep(k, 1 << M) {
		vector<bool> use(N + 1,0);
		ll c = 0;
		rep(j, M) {
			if (((k >> j) & 1) == 0) { continue; }
			repn(i, N) {
				if (path[j][i]) { use[i] = 1; }
			}
			c++;
		}

		ll t = 1;
		repn(i, N-1) {
			if (use[i+1] == 0) { t *= 2; }
		}

		//cout << k << " " << t << endl;

		if (c % 2 == 0) { ans += t; }
		else { ans -= t; }
		
	}

	cout << ans << endl;
	
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define int long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9+7
#define f first
#define s second
#define ylol1 y1
#define endl '\n'
#define pii pair<int,int>
#define piii pair<pii,int>
#define precise(i) cout<<fixed<<setprecision(i)
#define codejam cout<<"Case #"<<ii+1<<": ";
#define impossible cout<<"IMPOSSIBLE"<<endl;
#define error(s) throw runtime_error(s)
using namespace std;
const long double PI = atan(1.0)*4;
const int64_t INF=1e14;
const int32_t LOG=21;
const int MOD=MOD1;

int go[200005][65];
int a[200005];

void dfs(int i)
{
     
}

int32_t main()
{ 
      getFaster;
      int tests=1;
      //cin>>tests;
      rep(ii,0,tests)
      {
            int n,k;
            cin>>n>>k;
            rep(i,1,n+1)cin>>a[i];
            
            rep(i,1,n+1)
            {
            	go[i][0]=a[i];
            }

          
            rep(j,1,64)
            {
            	rep(i,1,n+1)
            	{
            		int x=go[i][j-1];
            		go[i][j]=go[x][j-1];
            	}
            }

            int cur=1;
            rev(i,61,0)
            {
            	if((1LL<<i)&k)
            	{
                   cur=go[cur][i];
            	}
            }

            cout<<cur<<endl;
            

      }
      return 0;                         
}

/*
 TIPS:
 1.) Check for corner cases (like 0 is exception case)
 2.) Check for global memory sizes,memory refreshments and its complexity 
 3.) Check for sorting criteria (if used 1-based indexing)
 4.) Check for numbers overflow (while multiplying and doing MOD)
 5.) Check for typecasting issues like int to float( while dividing numbers)
*/

Measured memory_bytes: 106780
#include <stdio.h>

int main(void)
{
    int     datanum = 0;
    int     data[100] = {0};
    int     count = 0;
    int     i = 0;

    scanf("%d", &datanum);
    for (i = 1; i <= datanum; i++) {
        scanf("%d", &data[i]);
    }

    for (i = 1; i <= datanum; i++) {
        if ((i % 2) == 0) {
            continue;
        }
        if ((data[i] % 2) != 0) {
            count++;
        }
    }

    printf("%d\n", count);

    return 0;
}

Measured memory_bytes: 1684
#include <bits/stdc++.h>
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
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; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
    詰まった時に、別の分野の問題として考え直す（これdpでは？、グラフとしてみればいいのでは？）
    多くの問題はパターンマッチだが、パターンに落とし込むまでが難しく、そのための訓練としてわからない問題をあれこれ色々な角度から考察してみるのではないか
    cin.tie(0);
    ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう

*/

const ll mod = 1e9+7;

void chmod(ll &M){
    if(M >= mod) M %= mod;
    else if(M < 0){
        M += (abs(M)/mod + 1)*mod;
        M %= mod;
    }
}

ll modpow(ll x, ll n){
    if(n==0) return 1;
    ll res=modpow(x, n/2);

    if(n%2==0) return res*res%mod;
    else return res*res%mod*x%mod;
}

int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };

/* <--------------------------------------------> */

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

    ll H, W; cin >> H >> W;
    int n; cin >> n;
    map<pair<int, int>, int> mp;

    rep(i, n){
        int a, b; cin >> a >> b;
        repr(j, a-2, a+1) repr(k, b-2, b+1){
            if(1<=j && j<=H-2 && 1<=k && k<=W-2) mp[make_pair(j, k)]++;
        }
    }

    vector<ll> ans(10, 0);
    ans[0] = (H-2)*(W-2);
    for(auto itr=mp.begin(); itr!=mp.end(); ++itr){
        ans[itr->second]++;
        ans[0]--;
    }

    rep(i, 10) cout << ans[i] << endl;

    return 0;
}


Measured memory_bytes: 56576
k,n=map(int,input().split())
a=[int(i) for i in input().split()]
array=[]
result=a[n-1]-a[0]
array.append(result)
for i in range(1,n):
    result=k+a[i-1]-a[i]
    array.append(result)
print(min(array))

Measured memory_bytes: 26436
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n,val[30],price[30];
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>val[i];
  }
  for(int i=0;i<n;i++){
    cin>>price[i];
  }
  int sum=0;
  for(int i=0;i<n;i++){
    sum+=max(0,val[i]-price[i]);
  }
  cout<<sum<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	
	int n, k; cin >> n >> k;
	int bad[10] = {};
	rep(i, k){
		int t; cin >> t; bad[t] = 1;
	}
	while(1){
		bool ok = 1;
		for(int m = n; m; m /= 10) if(bad[m % 10]) { ok = 0; break; }
		if(ok){
			cout << n << endl; break;
		}
		n++;
	}
	
	return 0;
}

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

int main(){
  int N;
  cin >> N;
  if(N/1000 == (N%1000)/100 && (N%1000)/100 == (N%100)/10 
     ||(N%1000)/100 == (N%100)/10  &&  (N%100)/10 == N%10)
     cout << "Yes" << endl;
  else cout << "No" << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include <string>
#include <algorithm>
#include <initializer_list> 
using namespace std;

int main(){
	int cnt = 0;
	int f[114514];
	int t[114514];
	while (true){
	int N;
	int a[11451];
		cin >> N;
		if (N == 0){ break; }
		t[cnt] = N;
		f[cnt] = 0;
		for (int i = 0; i < N; i++){
			cin >> a[i];
			if (a[i] == 0){
				t[cnt]--;
			}
			if (a[i] >= 2){
				f[cnt] = 1;
			}
		}

		cnt++;
	}

	for (int i = 0; i < cnt; i++){
		if (f[i] == 0){
			cout << "NA" << endl;
		}
		else{
			cout << t[i] + 1 << endl;
		}
	}
}

Measured memory_bytes: 1220
#include<iostream>
#include<string>
#include<array>
#include<algorithm>
#include<deque>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<list>

int main(void) {
	using namespace std;
	/*int N, A,res;
	cin >> N >> A;
	res = N * N - A;
	cout << res << endl;*/

	/*int a, b,c;
	cin >> a >> b;
	c = a * b;
	c = c % 2;
	if (c == 0) {
		cout << "Even" << endl;
	}
	else if(c == 1){
		cout << "Odd" << endl;
	}*/

	/*int A, B, a, b, c;
	cin >> A >> B;
	a = A + B;
	b = A - B;
	c = A * B;
	if (a >= b && a >= c) {
		cout << a << endl;
	}
	else if (b >= a && b >= c) {
		cout << b << endl;
	}
	else if (c >= a && c >= b) {
		cout << c << endl;
	}*/

	/*int K, S,res = 0,Z;
	cin >> K >> S;
	for (int X = 0; X <= K;X++) {
		for (int Y = 0; Y <= K; Y++) {
			Z = S - X - Y;
			if (Z >= 0 && Z <= K) {
				if (X + Y + Z == S) {
					res += 1;
				}
			}
		}
	}
	cout << res << endl;*/

	/*int N,a;
	cin >> N;
	set<int>s;
	for (int i = 0; i < N;i++) {
		cin >> a;
		s.insert(a);
	}
	cout << s.size() << endl;*/

	/*int N, a, res;
	vector<int>vec;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> a;
		vec.push_back(a);
	}
	sort(vec.begin(), vec.end());
	res = vec[vec.size() - 1] - vec[0];
	cout << res << endl;*/

	/*int Alice = 0, Bob = 0,a,N;
	deque<int>dec;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> a;
		dec.push_back(a);
	}
	sort(dec.rbegin(), dec.rend());
	for (int i = 0; i < N;i++) {
		if (i % 2 == 0) {
			Alice += dec[0];
			dec.pop_front();
		}
		else if (i % 2 == 1) {
			Bob += dec[0];
			dec.pop_front();
		}
	}
	cout << Alice - Bob << endl;*/

	/*int A, B, C, X,res = 0;
	cin >> A >> B >> C >> X;
	for (int i = 0; i <= A;i++) {
		for (int j = 0; j <= B; j++) {
			for (int k = 0; k <= C; k++) {
				if (i * 500 + j * 100 + k * 50 == X) {
					res += 1;
				}
			}
		}
	}
	cout << res << endl;*/

	int N,a,count = 0,b = 0;
	vector<long long int>vec;
	vector<long long int>res;
	cin >> N;
	for (int i = 0; i < N;i++) {
		cin >> a;
		vec.push_back(a);
	}
	sort(vec.rbegin(), vec.rend());
	for (int i = 0; i < N - 1;i++) {
		if (vec[i] == vec[i + 1]) {
			res.push_back(vec[i]);
			i += 1;
			count += 1;
		}
		if (count == 2) {
			break;
		}
	}
	if (count >= 2) {
		cout << res[0] * res[1] << endl;
	}
	else {
		cout << b << endl;
	}
}

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

int main(){
    char a, b, c;
    int x, y, z;
    cin >> a >> b >> c;

    if(a == 'o')x = 100;
    else x = 0;
    if(b == 'o')y = 100;
    else y = 0;
    if(c == 'o')z = 100;
    else z = 0;

    cout << 700 + x + y + z << endl;

    return 0;

}

Measured memory_bytes: 256
import java.util.Scanner;

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 L[] = new int[N];
        int R[] = new int[N];

        L[0] = 0;
        R[N-1] = 0;
        for (int i = 1; i < N; i++) {
            L[i] = gcd(A[i - 1], L[i - 1]);
            R[N - 1 - i] = gcd(A[N - i], R[N - i]);
        }

        int maxGcd = 0;
        for (int i = 0; i < N; i++) {
            int gcd = gcd(L[i], R[i]);
            if (maxGcd < gcd) maxGcd = gcd;
        }

        System.out.println(maxGcd);
    }

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


Measured memory_bytes: 50264
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstdio>

using namespace std;

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

vector<int> g[1010];
int n, m;
 
vector<bool> visited, matched;
vector<int> levels, matching;
 
bool augment(int left) {
  if(left == n) return true;
  if(visited[left]) return false;
  visited[left] = true;
  rep(i, g[left].size()) {
    int right = g[left][i];
    int next = matching[right];
    if (levels[next] > levels[left] && augment(next)) {
      matching[right] = left;
      return true;
    }
  }
  return false;
}
 
int bimatch() {
  matching.assign(m, n);
  matched.assign(n, false);
  int ret = 0;
  bool cont;
  do {
    levels.assign(n+1, -1);
    levels[n] = n;
    queue<int> q;
    rep(left, n) if (!matched[left]) {
      q.push(left);
      levels[left] = 0;
    }
    while(!q.empty()) {
      int left = q.front();
      q.pop();
      rep(i, g[left].size()) {
        int right = g[left][i];
        int next = matching[right];
        if (levels[next] < 0) {
          levels[next] = levels[left] + 1;
          q.push(next);
        }
      }
    }
    visited.assign(n, false);
    cont = false;
    /*
    rep(i,n){
      cout << "match:" << i << " : " << matched[i] << endl;
    }
    rep(i,n){
      cout << "levels:" << i << " : " << levels[i] << endl;
    }
      */
    rep(left, n) if(!matched[left] && augment(left)){
      matched[left] = cont = true;
      ret++;
    }
  } while(cont);
  //printf("ret:%d\n",ret);
  //return count(matched.begin(), matched.end(), true);
  return ret;
}

int blue[510],red[510];

int main(){
  while(scanf("%d%d",&n,&m),n|m){
    //init
    rep(i,n+m)g[i].clear();
    //visited.clear();
    //matched.clear();
    //levels.clear();
    //matching.clear();
    
    
    
    rep(i,n)scanf("%d",blue+i);
    rep(i,m)scanf("%d",red+i);

    rep(i,n){
      rep(j,m)if( __gcd(blue[i],red[j])!=1 ){
        g[i].push_back(n+j);
        g[n+j].push_back(i);
      }
    }
    //
    /*
    rep(i,n+m){
      printf("%d: ",i);
      rep(j,g[i].size())printf("%d ",g[i][j]);
      puts("");
    }
      */
    m += n;
    printf("%d\n",bimatch());
  }
  return 0;
}

Measured memory_bytes: 3076
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define pb push_back
#define f first
#define s second

int  main() {
    ll x , n;
    cin >> x >> n;
    map<ll,ll>mp ;
    ll i ;
    for (i = 0 ; i< n ; i++) {
        ll  y ;
        cin >> y ;
        mp[y]++ ;
    }
    ll del = 0 ;
    while(1) {
        if (!mp[x-del]) return cout << x-del << endl , 0 ;
        if (!mp[x+del]) return cout << x+del << endl , 0 ;
       // if (!mp[x-del]) return cout << x-del << endl , 0 ;
        del++ ;
    }

}


Measured memory_bytes: 3664
object Main extends App {
  import scala.io.StdIn._
  val n = readInt()
  val a = readLine().split(" ").map(_.toInt)

  val evens = a.filter(_ % 2 == 0)
  val evensSelect = evens.filter(x => x % 3 == 0 || x % 5 == 0)
  println(if(evens.length == evensSelect.length) "APPROVED" else "DENIED")
}

Measured memory_bytes: 25516
private class C {
    fun solve() {
        var x = nextLong()
        var k = nextLong()
        val d = nextLong()
        var ans = 0L
        x = Math.abs(x)
        val count = x / d
        if (count > k) {
            ans = x - k * d
        } else {
            var y = x - d * count
            k -= count
            if (k != 0L && y - d >= 0) {
                k--
                y-=d
            }
            if (k % 2 == 0L) {
                ans = y
            } else {
                ans = y - d;
            }
        }
        println(Math.abs(ans))
    }

    var tokenBuffer = listOf<String>()
    var bufferPosition = 0

    private fun checkBuffer() {
        if (bufferPosition == tokenBuffer.size) {
            tokenBuffer = nextLine().split(" ")
            bufferPosition = 0
        }
    }

    fun nextLine() = readLine()!!
    fun nextInt(): Int {
        checkBuffer()
        return tokenBuffer[bufferPosition++].toInt()
    }

    fun nextLong(): Long {
        checkBuffer()
        return tokenBuffer[bufferPosition++].toLong()
    }

    fun nextDouble(): Double {
        checkBuffer()
        return tokenBuffer[bufferPosition++].toDouble()
    }

    fun nextToken(): String {
        checkBuffer()
        return tokenBuffer[bufferPosition++]
    }
}

fun main() {
    val reader = C().solve()

}


Measured memory_bytes: 36524
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
    cin>>a[i];
    int ans=0,maxx=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]>=maxx)
        {
            maxx=a[i];
            ans++;
        }
    }
    cout<<ans<<endl;
        return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    if (a + b == c || b + c == a || c + a == b) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
}

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

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

using namespace std;

using P = pair<int, int>;

int n, m;
int l[300000];
int r[300000];
P v[300000];
int lazy[1 << 18];
int val[1 << 18];

void add(int a, int b, int l = 0, int r = 1 << 17, int k = 0){
  if(b <= l || r <= a){
    return;
  }
  if(a <= l && r <= b){
    ++lazy[k];
    return;
  }
  int m = (l + r) / 2;
  add(a, b, l, m, k * 2 + 1);
  add(a, b, m, r, k * 2 + 2);
}
int get(int i, int l = 0, int r = 1 << 17, int k = 0){
  if(i + 1 <= l || r <= i){
    return 0;
  }
  if(i <= l && r <= i + 1){
    return val[k] + lazy[k];
  }
  val[k] += lazy[k];
  lazy[k * 2 + 1] += lazy[k];
  lazy[k * 2 + 2] += lazy[k];
  lazy[k] = 0;
  int m = (l + r) / 2;
  return get(i, l, m, k * 2 + 1) + get(i, m, r, k * 2 + 2);
}

int main(){
  cin >> n >> m;
  rep(i, n){
    cin >> l[i] >> r[i];
    v[i] = P(r[i] - l[i], i);
  }
  sort(v, v + n);
  int p = 0;
  for(int i = 1; i <= m; ++i){
    while(p < n && v[p].first < i){
      int k = v[p++].second;
      add(l[k], r[k] + 1);
    }
    int a = n - p;
    for(int j = 0; j <= m; j += i){
      a += get(j);
    }
    cout << a << endl;
  }
  return 0;
}

Measured memory_bytes: 6784
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define rep(i,n) for(int i=0;i<n;i++)
const long long MOD = 1000000007;
const long long INF = MOD*MOD;
typedef pair<int,int> P;
typedef long long ll;
ll n,m,k, ans;
string s;


int main(){
  cin >> n >>  s;
  int l = 0,r = 0;
  //ペアがあるかどうか
  rep(i,n){
    if(s[i] == '(') l++;
    else if(l > 0) l--;
  }
  rep(i,n){
    if(s[n-1-i] == ')') r++;
    else if(r > 0) r--;
  }
  rep(i,r) s = '(' + s;
  rep(i,l) s = s + ')';
  
  cout << s << "\n";
}

Measured memory_bytes: 256
//abc144_b.cpp
//Sun Oct 27 20:49:50 2019

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

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

	for (int i=1;i<10;i++){
		if (n%i==0 && n/i<10){
			cout << "Yes" << endl;
			return 0;
		}
	}

	cout << "No" << endl;
//	printf("%.4f\n",ans);
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

int t[100000];
signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    ll x;
    cin >> x;
    ll res = x / 11LL * 2LL;
    x %= 11;
    res += 2LL;
    if(x <= 6LL)--res;
    if(x == 0LL)--res;
    pri(res);

/*/

//*/ break;
  }
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<set>

#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++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;

typedef long long int ll;



template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
	for (auto& e : t) fill_v(e, v);
}


#define ARRAY_MAX 100005
const ll INF = 1e9 + 7;


int main() {


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

	ll ans = 0;

	for (int i = 0; i < n-1; i++)
	{
		if (a[i] > x)
		{
			ans += a[i] - x;
			a[i] -= a[i] - x;
		}
		if (a[i+1] + a[i] > x)
		{
			ans += a[i + 1] + a[i] - x;
			a[i + 1] -= (a[i + 1] + a[i] - x);
		}
	}

	cout << ans << endl;


	return 0;


}





Measured memory_bytes: 1024
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        new Main().solve();
    }

    private void solve() throws IOException {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        int n = Integer.parseInt(f.readLine());
        char[] seq = f.readLine().toCharArray();
        int left = 0;
        int right = n - 1;
        int moves = 0;
        while (left < right) {
            if (seq[left] == 'R') {
                left++;
            } else {
                while (right > left && seq[right] == 'W') right--;
                if (seq[right] == 'R') moves++;
                right--;
                left++;
            }
        }

        out.println(moves);
        out.close();
    }
}

Measured memory_bytes: 36384
import java.util.*;
public class Main
{
	public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	int x=sc.nextInt();
	long n=sc.nextInt();
	int count=0;
	for(int i=0;i<x;i++)
	{
	    long a=sc.nextLong();
	    long b=sc.nextLong();
	   
	    if( Math.sqrt((a*a)+(b*b))<=n)
               count++;	    
	}
	System.out.println(count);
}
}

Measured memory_bytes: 59008
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>

typedef long long ll;
using std::vector;
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) {
    vec.resize(len);
    for (auto &v : vec) ndarray(v, args...);
}

int main() {
    //std::ifstream in("0_normal_2");
    //std::cin.rdbuf(in.rdbuf());
    ll N, K;
    
    std::cin >> N >> K;
    
    ll bitmax = 63;
    vector<ll> cnt(bitmax);
    for (ll i = 0; i < N; i++) {
        ll a;
        std::cin >> a;
        for (ll bit = 0; bit < bitmax; bit++) {
            if ( ((a >> bit) & 1) == 1 ) {
                cnt[bit] += 1;
            }
        }
    }
    bool f = true;
    ll ans = 0;
    for (ll b = bitmax-1; b >= 0; b--) {
        if (f) {
            //ここまでの桁、XとKは一致
            ll kb = (K >> b) & 1LL;
            if (kb == 1) {
                //Xが1の時、f = true
                //Xが0の時、f = false
                //どちらでも良い場合、0にしておくことで今後の選択肢が増え、有利
                if (cnt[b] >= N-cnt[b]) {
                    f = false;
                }
                ans += std::max(cnt[b], N - cnt[b]) * (1LL << b);
            } else {
                //Xは0しか取れず、f = true
                ans += cnt[b] * (1LL << b);
            }
        } else {
            //不一致：Xは任意、f = false
            ans += std::max(cnt[b], N - cnt[b]) * (1LL << b);
        }
        //std::cout << b << ": " << ans << std::endl;
    }
    
    std::cout << ans << std::endl;
    
    return 0;
}


Measured memory_bytes: 256
n = gets.to_i
a = gets.chomp.split.map(&:to_i)
b = gets.chomp.split.map(&:to_i)

ans = 0

n.times do |i|
  if a[i] >= b[i]
    ans += b[i]
    a[i] -= b[i]
    b[i] = 0
  else
    ans += a[i]
    b[i] -= a[i]
    a[i] = 0
    if a[i+1] >= b[i]
      ans += b[i]
      a[i+1] -= b[i]
      b[i] = 0
    else
      ans += a[i+1]
      b[i] -= a[i+1]
      a[i+1] = 0
    end
  end
end

puts ans


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



int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll a,b,x;cin>>a>>b>>x;
    cout<<b/x-((a==0&&x!=1)?(-1):((a-1)/x))<<endl;
    return 0;
}

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

int main(){
    int H, W, N;
    cin >> H;
    cin >> W;
    cin >> N;

    int i = 1;

    if(H <= W){
        while(true){
            if(W*i >= N){
                break;
            }else{
                i++;
            }
        }
    }else{
        while(true){
            if(H*i >= N){
                break;
            }else{
                i++;
            }
        }
    }
    cout << i << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep2(i, m, n) for(int i=int(m); i<int(n); ++i)
#define drep2(i, m, n) for(int i=int(m-1); i>=int(n); --i)
#define rep(i, n) rep2(i, 0, n)
#define drep(i, n) drep2(i, n, 0)
#define all(a) a.begin(), a.end()
#define fst first
#define snd second
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
template<typename T> inline bool chmin(T& a, T b) {if(a>b) {a=b; return true;} return false;}
template<typename T> inline bool chmax(T& a, T b) {if(a<b) {a=b; return true;} return false;}
inline int Log2(ll x) {int k; for(k=0; x>0; ++k) x>>=1; return k;} // number of binary digits
template<typename T> inline int count_between(vector<T>& a, T l, T r) {
  return lower_bound(all(a), r) - lower_bound(all(a), l); // [l, r)
}
template<typename T> istream& operator>>(istream& is, vector<T>& v) {
  rep(i, v.size()) is >> v[i]; return is;
}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
  rep(i, v.size()) os << v[i] << " "; os << endl; return os;
}
const int INF  = 1<<30;
const ll INFll = 1ll<<62;
const ld EPS   = 1e-10;
const int MOD  = int(1e9)+7;


template<typename T>
struct BIT {
  int n; vector<T> d;
  BIT(int n=0) : n(n), d(n+1) {}
  void add(int i, T a=1) {
    for (i++; i <= n; i += i & -i) d[i] += a;
  }
  T sum(int i) {
    if (i == 0) return 0;
    T x = 0;
    for (; i > 0; i -= i & -i) x += d[i];
    return x;
  }
};


int main() {
  ll n; cin >> n;
  V a(n);
  cin >> a;

  ll K = (n*(n+1)/2 + 1) / 2;

  auto bfun = [&] (ll x) {
    V s(n+1);
    rep(i, n) s[i+1] = s[i] + (a[i]>=x ? 1 : -1);
    rep(i, n+1) s[i] += n;

    BIT<int> cnt(2*n+1);
    ll res = 0;
    rep(r, n+1) {
      res += cnt.sum(s[r]+1);      
      cnt.add(s[r]);
    }
    return res >= K;
  };


  ll ok = 0;
  ll ng = 1e9+5;
  while (llabs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (bfun(mid)) ok = mid;
    else ng = mid;
  }

  ll ans = ok;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 1808
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
       	int ary[] = new int[num];
      
      	for(int i = 0; i < num; i++)
        {
        	ary[i] = sc.nextInt();
        
        }
      	
      	Arrays.sort(ary);
     	int ans = ary[num-1] - ary[0];
      	System.out.println(ans);
      
    }
}

Measured memory_bytes: 23124
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;
ul over = 1000000007;

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  string s;
  cin >> s;
  int x=0;
  for (int i=0; i<s.size(); ++i) {
    if (s[i]=='x') x++;
  }
  cout << (x >= 8 ? "NO" : "YES") << endl;
  return 0;
}


Measured memory_bytes: 256