akhauriyash/Code-Regression huggingface dataset:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = [0] * k
for i in range(k):
    pre_cnt = ["", 0]
    for j in range(i, n, k):
        if j == i:
            if t[j] == "r":
                ans[i] += p
                pre_cnt = ["p", p]
            elif t[j] == "s":
                ans[i] += r
                pre_cnt = ["r", r]
            else:
                ans[i] += s
                pre_cnt = ["s", s]
            continue

        if t[j] == "r":
            if pre_cnt[0] == "p":
                pre_cnt[0] = ""
                continue
            ans[i] += p
            pre_cnt = ["p", p]
        elif t[j] == "s":
            if pre_cnt[0] == "r":
                pre_cnt[0] = ""
                continue
            ans[i] += r
            pre_cnt = ["r", r]
        else:
            if pre_cnt[0] == "s":
                pre_cnt[0] = ""
                continue
            ans[i] += s
            pre_cnt = ["s", s]
print(sum(ans))

Measured memory_bytes: 74812
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e3+2;
struct node{
	ll a;
	int b;
}A[MAXN];
bool cmp(node A,node B)
{
	return A.a>B.a;
}
int N;
ll dp[MAXN][MAXN], ans;
int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> A[i].a; A[i].b = i;
	}
	sort(A, A+N, cmp);
	for (int i = 0;i<N;i++) {
		for (int j = 0;i+j<N;j++) {
			int cur = i+j;
			dp[i+1][j]=max(dp[i+1][j], dp[i][j]+A[cur].a*abs(A[cur].b-i));
			dp[i][j+1]=max(dp[i][j+1], dp[i][j]+A[cur].a*abs(A[cur].b-(N-1-j)));
		}
	}
	for (int i = 0;i<=N;i++) ans = max(ans, dp[i][N-i]);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 26280
#include <iostream>
#include <math.h>
#include <cstdio>
#include <utility>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

int m,f,r;

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

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
using ld = long double;
const int MOD = 1e9+7;


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

    if(s == t) {
        cout << n << endl;
        return 0;
    }
    int ans = n * 2;
    for(int i = 0; i < n; i++){
        string tmpt = t.substr(0, i);
        string tmps = s.substr(n - i, i);
        if(tmpt == tmps) ans -= tmpt.size();
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3580
marchlist = ['M', 'A', 'R', 'C', 'H']
N = int(input())
namelist = [[], [], [], [], []]
for i in range(0, N):
	s = input()
	for j in range(0, 5):
		if s[0] == marchlist[j]:
			namelist[j].append(s)
sum = 0
for i in range(0, 3):
	for j in range(i+1, 4):
		for k in range(j+1, 5):
			sum += len(namelist[i])*len(namelist[j])*len(namelist[k])
print(sum)

Measured memory_bytes: 57816
#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#include<random>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (int)(Y);++(X))
#define reps(X,S,Y) for (int (X) = (int)(S);(X) < (int)(Y);++(X))
#define rrep(X,Y) for (int (X) = (int)(Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (int)(Y)-1;(X) >= (int)(S);--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl
#define NL <<"\n"
#define cauto const auto

using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
//#undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
#ifdef __cpp_init_captures
template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);}
template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));}
#endif
template<class A,class B> pair<A,B> operator+(const pair<A,B> &p,const pair<A,B> &q){ return {p.X+q.X,p.Y+q.Y};}
template<class A,class B,class C,class D> pair<A,B>& operator+=(const pair<A,B> &p,const pair<C,D> &q){ p.X+=p.X; q.Y+=q.Y; return p;}
template<class A,class B> pair<A,B> operator-(const pair<A,B> &p,const pair<A,B> &q){ return {p.X-q.X,p.Y-q.Y};}
template<class A,class B,class C,class D> pair<A,B>& operator-=(const pair<A,B> &p,const pair<C,D> &q){ p.X-=p.X; q.Y-=q.Y; return p;}
template<class A,class B> istream& operator>>(istream &is, pair<A,B> &p){ is>>p.X>>p.Y; return is;}
template<class T> T read(){ T re; cin>>re; return re;}
template<class T> T read(const T &dec){ T re; cin>>re; return re-dec;}
template<class T> vector<T> readV(const int sz){ vector<T> re(sz); for(auto &x:re) x=read<T>(); return move(re);}
template<class T> vector<T> readV(const int sz, const T &dec){ vector<T> re(sz); for(auto &x:re) x=read<T>(dec); return move(re);}
vv<int> readG(const int &n,const int &m){ vv<int> g(n); rep(_,m){ cauto a=read<int>(1),b=read<int>(1); g[a].pb(b); g[b].pb(a);} return move(g);}
vv<int> readG(const int &n){ return readG(n,n-1);}

const ll MOD=1e9+7; //998244353

int main(){
  ios_base::sync_with_stdio(false); cin.tie(0);
  cout<<fixed<<setprecision(0);
	cauto n=read<int>();
	cauto g=readG(n);
	ll re=0;
	reps(i,1,n+1) re+=i*(n+1ll-i);
  rep(a,n)for(auto b:g[a])if(a<b) re-=(a+1ll)*(n-b);
	cout<<re NL;
  return 0;
}


Measured memory_bytes: 14988
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <deque>
#include <utility>

int dp[5004][5004];

int main(){
  int N;
  scanf("%d", &N);
  std::string s;
  std::cin >> s;
  int ans = 0;
  dp[N][N]=0;
  for (int i=N-1; i>=0; i--){
    for (int j=N-1; j>=0; j--){
      if (s[i]==s[j]) {
	dp[i][j] = dp[i+1][j+1]+1;
	ans = std::max(ans, std::min(dp[i][j], j-i));
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
 

Measured memory_bytes: 101008
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pint = pair<int,int>;
ll mod = 1000000007,mod2 = 998244353;

void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }
const long long INF = 1LL<<60;
ll gcd(ll a,ll b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}

//重みある時
using Edge = pair<int,int>;
using Graph = vector<vector<Edge> >;

vector<string> S(100010);  // mod kごとにわける

int main() {
    bool flg = 0;
    string s,t;
    cin >> s >> t;
    for (int i = 0; i < s.size(); ++i) {
        if(s == t){
            flg = 1;
            break;
        }else{
            s = s[s.size()-1] + s.substr(0,s.size()-1);
        }
    }
    if(flg) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int A,B,K,L,ans;
  cin >> A >> B >> K;
  if(A > B)
    L = A;
  else
    L = B;
  vector<int> vec = {};
  for(int i = 1;i <= L;i++){
    if(A % i == 0 && B % i == 0)
      vec.push_back(i);
  }
  int size = vec.size();
  ans = vec.at(size - K);
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[1<<19];
int main(){
  int n;
  while(cin>>n){
        for(int i=0; i<(1<<n); ++i){
            scanf("%d",&a[i]);
        }
        int m=(1<<n);
        sort(a, a+m);
        reverse(a, a+m);
        int tmp=1;
        bool l=1;
        set<pair<int,int>>s1, s2;
        s1.insert(mk(0,a[0]));
        for(int i=1; i<m; ++i){
            s2.insert(mk(i, a[i]));
        }
        set<pair<int,int>>::iterator it1, it2;
        vector<pair<int,int>>v;
        while(s2.size()>0){
            it1=s1.begin();
            it2=s2.begin();
            v.clear();
            while(it1!=s1.end()){
                while(it2!=s2.end()){
                    if((*it2).sc<(*it1).sc){
                        break;
                    }
                    ++it2;
                }
                if(it2!=s2.end()){
                    v.push_back(*it2);
                    ++it2;
                }
                ++it1;
            }
            if(v.size()<s1.size()){
                l=0;
                break;
            }
            else{
                for(int i=0; i<v.size(); ++i){
                    s1.insert(v[i]);
                    s2.erase(s2.find(v[i]));
                }
            }
        }
        if(l)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
  }
  return 0;
}




Measured memory_bytes: 14716
import java.util.*;
public class Main {

	public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
			
			long x=sc.nextLong();
	long rem=x%11;
	long ans=x/11*2;
	if(rem==0) {
		System.out.println(ans);
	}else {
		if(rem<=6) {
			System.out.println(ans+1);
		}else {
			System.out.println(ans+2);
		}
	}
		
			
			
	}
}


Measured memory_bytes: 35676
import sys
def gcd(m, n):
    if n == 0:
        n, m = m, n
    r = m % n
    return gcd(n, r) if r else n

n = int(input())
a = list(map(int, sys.stdin.readline().split()))
l, r = [0]*(n+1), [0]*(n+1)
for i in range(1, n+1):
    l[i] = gcd(l[i-1], a[i-1])
    r[n-i] = gcd(r[n-i+1], a[n-i])
ans = 0
for i in range(n):
    m = gcd(l[i], r[i+1])
    if ans < m:
        ans = m
print(ans)

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

int main(){
  int n;
  string s;
  int r1=0;
  int r2=0;
  int r3=0;
  int r4=0;
  
  cin >> n;
  
  for(int i=0; i <n; i++){
    cin >> s;
    if(s == "AC")r1++;
    if(s == "WA")r2++;
    if(s == "TLE")r3++;
    if(s == "RE")r4++;
  }
  cout << "AC x "<< r1 <<endl;
  cout << "WA x "<< r2 <<endl;
  cout << "TLE x "<< r3 <<endl;
  cout << "RE x "<< r4 <<endl;
  return 0;
}

Measured memory_bytes: 3640
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;

int N, D[50];

int v[13];

int e[24];
int dfs(int t) {
    if (t == 0) {
        if (v[0] >= 2) {
            return 0;
        }
        e[0] = v[0];
        return dfs(t+1);
    }

    if (t == 12) {
        if (v[12] >= 2) {
            return 0;
        }

        e[12] = v[12];

        int ret = 1 << 28;
        for (int t = 0; t < 24; ++t) {
            for (int u = t+1; u < 24; ++u) {
                if (e[t] > 0 && e[u] > 0) {
                    ret = min(ret, min(abs(t-u), 24-abs(t-u)));
                }
            }
        }

        return ret;
    }

    if (v[t] == 2) {
        e[t] = 1; e[24-t] = 1;
        return dfs(t+1);
    } else if (v[t] == 1) {
        e[t] = 1; e[24-t] = 0;
        int v1 = dfs(t+1);
        e[t] = 0; e[24-t] = 1;
        int v2 = dfs(t+1);
        return max(v1, v2);
    }
    e[t] = e[24-t] = 0;
    return dfs(t+1);
}

int main() {
    cin >> N; for (int j = 0; j < N; ++j) cin >> D[j];

    fill(v, v+13, 0);
    v[0] = 1;
    for (int j = 0; j < N; ++j) {
        if (++v[D[j]] >= 3) {
            cout << 0 << endl;
            return 0;
        }
    }

    cout << dfs(0) << endl;

    return 0;
}


Measured memory_bytes: 888
x, y, a,b,c = map(int,input().split())
pa = list(map(int, input().split()))
qb = list(map(int, input().split()))
rc = list(map(int, input().split()))

pa.sort(reverse=True)
qb.sort(reverse=True)
rc.sort(reverse=True)

pa = pa[:x]
qb = qb[:y]

_all = rc + pa + qb
_all.sort(reverse=True)

ans = 0
for i in range(x+y):
    ans += _all[i]

print(ans)

Measured memory_bytes: 22560
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <cstdio>
// #include <cstdio>
// #include <bitset>
// #include <math.h>
//#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a; i<b; i++)

const int MAX_N = 50;
int n, k;
string S;

int main() {
  scanf("%d %d", &n, &k);
  cin >> S;

  // printf("%d %d\n", n, k);
  // cout << S << "\n";
  //
  // printf("%c\n", tolower('a'));
  // printf("%c\n", tolower('A'));
  // printf("%c\n", tolower('!'));
  int cnt = 1;
  for(auto tmp_c: S) {
    if (cnt==k) {
      printf("%c", tolower(tmp_c));
    } else {
      printf("%c", tmp_c);
    }
    cnt++;
  }
  printf("\n");

  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int , int> P;
typedef pair<ll, ll> PL;
const int INT_INF = 2147483646;
const ll LL_INF = 9223372036854775807;

bool SecondCompare(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second<b.second;
}
 
bool SecondCompareDes(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
}
 
 
int n;
int d[100];
int main(){
    cin >> n;
    int jj;
    for(int j = 2; j <= n; j++){
        jj = j;
        for(int i = 2; i <= j; i++){
            while(jj%i == 0){
                d[i]++;
                jj /= i;
                if(jj == 1) break;
            }
            
        }
    }

    sort(d,d+100,greater<int>());
    int ans = 0;
    int c1,c2,c3,c4,c5,num;
    c1 = c2 = c3 = c4 = num = c5 = 0;
    for(int i = 0; i < 100; i++){
        if(d[i] >= 74) ++c1;
        if(d[i] >= 24) ++c2;
        if(d[i] >= 14) ++c3;
        if(d[i] >= 4) ++c4;
        if(d[i] >= 2) ++c5;
        if(d[i] > 0)    ++num;
    }

    ans += c1;
    ans += c2*(c5-1);
    ans += c3*(c4-1);
    if((c5-c4)*(c4*(c4-1)/2) > 0)   ans += (c5-c4)*(c4*(c4-1)/2);
    if((c4*(c4-1)*(c4-2)/6) > 0) ans += (c4*(c4-1)*(c4-2)/6)*3;


    cout << ans << endl;



}

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

using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;

string s;

int main() {
    cin >> s;
    int n = s.size();
    forn(i, 8) {
        string t = s.substr(0, i) + s.substr(n - 7 + i, 7 - i);
        if (t == "keyence") {
            cout << "YES" << endl;
            return 0;
        }
    }
    cout << "NO" << endl;
    return 0;
}


Measured memory_bytes: 256
n,k=map(int,input().split())
a=list(map(int,input().split()))
pl=a[0]
pr=a[k-1]
for i in range(k,n):
    l=a[i-k+1]
    r=a[i]
    if r>pl:
        print("Yes")
    else:
        print("No")
    pl=l
    pr=r

Measured memory_bytes: 31608
#include<iostream>
// #include<vector>
// #include<algorithm>
// #include<math.h>

using namespace std;

int main(){
	int count[4];
	int a, b;
	for(int i=0; i<4; i++){
		count[i]=0;
	}

	for(int i=0; i<3; i++){
		cin >> a >> b;
		count[a-1]++;
		count[b-1]++;
	}

	for(int i=0; i<4; i++){
		if(count[i]==0 || count[i]==3){
			cout << "NO" << endl;
			return 0;
		}
	}

	cout << "YES" << endl;
	
	return 0;
} // g++ -std=c++11 test.cpp -o test && ./test

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
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; }
using P=pair<long long,long long>;
#define rep(i,n) for(long long i=0; i<(long long)n; i++)
#define req(i,n) for(long long i=n-1; i>=0; i--)
#define range(i,a,b) for(long long i=a; i<b; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define onBoard(y,x) (y>=0 && y<h && x>=0 && x<w)
#define vint vector<int>
#define pri_que priority_queue
#define vvint vector<vector<int>>
#define vstr vector<string>
#define vp vector<pair<int,int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define SUM(x) accumulate(x.begin(),x.end(),0)
#define MAX(x) *max_element(x.begin(),x.end())
#define MIN(x) *min_element(x.begin(),x.end())
#define couty cout<<"Yes"<<endl
#define coutn cout<<"No"<<endl
#define coutY cout<<"YES"<<endl
#define coutN cout<<"NO"<<endl
#define yn(x) cout<<(x?"Yes":"No")<<endl
#define YN(x) cout<<(x?"YES":"NO")<<endl
long long gcd(long long a,long long b){return b?gcd(b,a%b):a;}
long long lcm(long long a, long long b){return a/gcd(a,b)*b;}
const long long dx[8]={1,0,-1,0,1,-1,-1,1};
const long long dy[8]={0,1,0,-1,1,1,-1,-1};
const long long INF = 1e12;
const long long MOD = 1e9+7;

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(15);

    int n; cin >> n;
    vint a(n);
    deque<int> deq;
    rep(i,n){
        cin >> a[i];
        if(i%2==1) deq.push_back(a[i]);
        else deq.push_front(a[i]);
    }
    if(n%2==1) for(auto d:deq) cout<<d<<' ';
    else for(auto itr=deq.rbegin(); itr<deq.rend(); itr++) cout<<*itr<<' ';
    cout<<endl;
}

Measured memory_bytes: 6348
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
vector<int> vc;
int main()

{

   int n;
   cin>>n;
   for(int i=0;i<n;i++){int x; cin>>x; vc.push_back(x);}

bool reham=true;
    sort(vc.begin(),vc.end());
    int counter=0;
    int res;
    for(int i=0;i<n;i++){
int counter2;

        for(int j=i+1;j<n;j++){
//counter2=0;
            int low=0,high=n-1,mid;
res=-1;

            while(low<=high){

                mid=(low+high)/2;
                if(vc[i]+vc[j]>vc[mid]){
                    res=mid;
                    low=mid+1;

                }
                else {high=mid-1;}

            }

//cout<<res<<endl;

  if(res!=-1) counter+=(res-j);


        }


    }



cout<<counter;

    return 0;
}


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

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

  if (k <= a)
    cout << k << endl;
  else if (k <= a + b)
    cout << a << endl;
  else
    cout << a - (k - a - b) << endl;

  return 0;
}

Measured memory_bytes: 3644
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
void hr() { cerr << "--------------" << endl; }
const int INF = 2e9;
const int MOD = 1e9 + 7;

int main() {
   string s;
   cin >> s;
   int n = s.size();
   string t;
   t = s[0];
   t += to_string(n - 2);
   t += s[n - 1];
   cout << t << endl;
   return 0;
}

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

//#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define print(v) for(auto it=(v).begin();it!=(v).end();it++) {cout<<*it<<" ";}
#define ll long long
#define w(t) ll t;cin>>t;while(t--)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
using namespace std;

vi d(200005);
map<int,int> mp;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll a[n];
    forn(i,n)
    {
        cin>>a[i];
        a[i]--;
    }

    ll x=1;
    mp[0]=0;
    int start=0,l,h;

    forn(i,n)
    {
        d[x]=a[start];
        if(mp.count(d[x])>0)
        {
            l=mp[d[x]];
            h=x;
            break;
        }
        else{
            mp[d[x]]=x;
        }
        x++;
        start=a[start];
    }

    int loop=abs(h-l);
    if(k<h)
        cout<<d[k]+1<<"\n";
    else{
        k-=l;
        k=k%loop;
        cout<<d[l+k]+1<<"\n";
    }



    return 0;
}


Measured memory_bytes: 14696
package main

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

var sc = bufio.NewScanner(os.Stdin)

type st struct {
	ind int
	min int64
}

func main() {
	var N string
	fmt.Scan(&N)

	// sc.Split(bufio.ScanWords)
    sum :=0
    for _, c := range N{
        n,_ := strconv.Atoi(string(c))
        sum += n
    }
    state := "No"
    if sum%9 == 0{
        state ="Yes"
    }
    fmt.Println(state)
}
func bc(s string, pos int) string {
	ch := "1"
	if string(s[pos]) == "1" {
		ch = "0"
	}
	pre := string(s[:pos])
	post := ""
	if pos != len(s)-1 {
		post = string(s[pos+1:])
	}
	tmp := pre + ch + post
	return tmp
}
func popCount(n string) int {
	count := 0
	for _, s := range n {
		if string(s) == "1" {
			count++
		}
	}
	return count
}
func chc(m [][]bool, p int) [][]bool {
	ret := make([][]bool, len(m))
	for i, l := range m {
		tmp := []bool{}
		for _, c := range l {
			if i == p {
				tmp = append(tmp, false)
			} else if c == true {
				tmp = append(tmp, true)
			} else {
				tmp = append(tmp, false)
			}
		}
		ret[i] = tmp
	}
	return ret
}
func chl(m [][]bool, p int) [][]bool {
	ret := make([][]bool, len(m))
	for j, l := range m {
		tmp := []bool{}
		for i, c := range l {
			if i == p {
				tmp = append(tmp, false)
			} else if c == true {
				tmp = append(tmp, true)
			} else {
				tmp = append(tmp, false)
			}
		}
		ret[j] = tmp
	}
	return ret
}
func helper(ma [][]bool) int {
	count := 0
	for _, l := range ma {
		for _, c := range l {
			if c == true {
				count++
			}
		}
	}
	return count
}
func nextLine() string {
	sc.Scan()
	return sc.Text()
}
func nextInt() int64 {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return int64(i)
}
func MySort(a []int) {
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })

}
func SortedBinaryAppend(sortedArr []int, targetValue int) []int {
	return []int{}
}
func SortedBinaryDelete(sortedArr []int, targetValue int) []int {
	return []int{}
}
func SortedBinarySearch(sortedArr []int, targetValue int) int {
	post := len(sortedArr) - 1
	pre := 0
	for {
		div := (post + pre) / 2
		tim := sortedArr[div]
		if tim == targetValue {
			return pre + div
		} else if tim > targetValue {
			post = tim
		} else {
			pre = tim
		}
		if pre == post {
			return -1
		}
	}
}
func Abs(i int) int {
	if i < 0 {
		return -1 * i
	}
	return i
}
func Min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func Max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func CalcNum(tate int, yoko int, A int, B int, dp *map[[2]int]int) int {
	if (*dp)[[2]int{tate, yoko}] != 0 {
		return (*dp)[[2]int{tate, yoko}]
	}
	return yoko*CalcNum(tate-1, yoko, A, B, dp) + tate*CalcNum(tate, yoko-1, A, B, dp)
}
func arrSum(arr []int) int {
	sum := 0
	for _, a := range arr {
		sum += a
	}
	return sum
}
func scanNums(len int) (nums []int) {
	var num int
	for i := 0; i < len; i++ {
		fmt.Scan(&num)
		nums = append(nums, num)
	}
	return
}
func scanStrings(len int) (strings []string) {
	var str string
	for i := 0; i < len; i++ {
		fmt.Scanf("%s", &str)
		strings = append(strings, str)
	}
	return
}


Measured memory_bytes: 3264
#include<iostream>
#include<string>
#include<math.h>
using namespace std;

int main() {
	char S[20];
	string T;
	string p;
	string Q;
	double m;
	int a = 0;
	int A = 0;
	int b = 0;
	int B = 0;
	int c = 0;
	int C = 0;
	int q, w, z;
	int n = 0;
	int x = 0;
	int y = 0;
	int ans = 0;
	cin >> a;
	cout << 48 - a;
	
}

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

using namespace std;

const int N=1e6+3;

#define pb push_back
#define all(x) (x).begin(),(x).end()
typedef long long ll;

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

const int inf=1e9;

int n,m,a[N],b[N],k,q,u,v,x,y,tc,c,l,r,ans,sz[N];

char s[N];

pair<int,int> p[N];

int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(int i=0;i<n;i++)
		scanf("%d",&b[i]),sz[i]=b[i];
	sort(sz,sz+n);
	int A,B;
	int cnt=0;
	do{
		cnt++;
		bool isA=true,isB=true;
		for(int i=0;i<n;i++){
			isA&=a[i]==sz[i];
			isB&=b[i]==sz[i];
		}
		if (isA)
			A=cnt;
		if (isB)
			B=cnt;
	}while(next_permutation(sz,sz+n));
	printf("%d\n",abs(B-A));
}


Measured memory_bytes: 6400
N,K = map(int,input().split())
num = 0
dicta = {}
for i in range(N):
    a,b = map(int,input().split())
    if a not in dicta:
        dicta[a] = b
    else:
        dicta[a] += b

dicts = sorted(dicta.items(), key = lambda x:x[0])
for i,v in dicts:
    num += v
    if num >= K:
        print(i)
        break

Measured memory_bytes: 15072
let fs, ns, ans = Array.(make 10 true, make 5 0, ref max_int)
let n, _ = Scanf.(scanf " %d %d" @@ fun n k -> n, for _ = 1 to k do fs.(scanf " %d" (+) 0) <- false done)
let g s = if s >= n then ans := min !ans s
let rec f i z = if i >= 5 then g @@ Array.fold_left (fun a n -> 10 * a + n) 0 ns else for j = 0 to 9 do if fs.(j) || z && j = 0 then (ns.(i) <- j; f (i + 1) (z && j = 0)) done
let _ = f 0 true; Printf.printf "%d\n" !ans

Measured memory_bytes: 384
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long x = sc.nextLong();
		sc.close();
		long k = x / 11;
		long ans = k * 2;
		long r = x - 11 * k;
		if(r == 0) {
			System.out.println(ans);
		}else if(r <= 6) {
			System.out.println(ans + 1);
		}else {
			System.out.println(ans + 2);
		}
	}
}

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

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

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

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

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

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

int main()
{
  int N, M;

  cin >> N >> M;
  vector<int> P(N);
  for (int i = 0; i < N; i++)
  {
    cin >> P[i];
  }

  UnionFind tree(N);

  for (int i = 0; i < M; i++)
  {
    int x, y;
    cin >> x >> y;
    tree.unite(x - 1, y - 1); // x-1の木とy-1の木を併合する
  }

  int count = 0;
  for (int i = 0; i < N; i++)
  {
    if (tree.same(P[i] - 1, i))
      count++;
  }

  cout << count;
}


Measured memory_bytes: 3964
#include <bits/stdc++.h>
#include <string>
//#include <boost/multiprecision/cpp_int.hpp>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define llu  long long 
#define fp cout<<fixed<<setprecision(9);
#define ifor(j,n) for(int i=j ;i<n;i++)
#define jfor(k,n) for(ll j=k ;j<n;j++)
#define pb push_back
#define mod 1000000007
using namespace std;
//using namespace boost::multiprecision;

//Compiler version g++ 6.3.0
int search (vector <long long> v,long long m,int n) 
{ 

    int start = 0;
    int ans = -1; 
    while (start <=n) 
    { 
     int mid = (start + n) / 2;
     if (v[mid] <= m) 
         start = mid + 1;
     else
   { 
   ans = mid; 
   n = mid - 1; 

       } 

    } 
    return ans; 
} 

int main()
{
 FAST 

   int n;
   cin>>n;
	string s[n];
  	int a=0,w=0,t=0,r=0;
  ifor(0,n)
  {
    	cin>>s[i];
    if(s[i]=="WA")
      w++;
    if(s[i]=="AC")
		a++;
    if(s[i]=="TLE")
       t++;
     if(s[i]=="RE")
       r++;}
  cout<<"AC x "<<a<<endl;
    cout<<"WA x "<<w<<endl;
    cout<<"TLE x "<<t<<endl;
    cout<<"RE x "<<r<<endl;
}

Measured memory_bytes: 6712
#include <iostream>
#include<algorithm>
#include <vector>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max(x,y) (((x)>(y)) ? x:y)
#define min(x,y) (((x)<(y)) ? x:y)
#define FOR(a,b,c) for(b=(c);b<(a);b++)
#define FOR(a) for(int i=0;i<a;i++)
#define ender {return 0;}
#define tosasuretu(a) (a*(a+1)/2)
inline ll kaizyo(ll);
ll gcd(ll, ll);
ll combination(ll, ll);
int main() {
	int n, t, ci, ti;
	int mincost = 1001;//コスト≦1000なので０ならTLEを返す
	cin >> n >> t;
	FOR(n) {
		cin >> ci >> ti;
		if (ti <= t) {
			mincost = min(mincost, ci);
		}
	}
	if (mincost <= 1000) {
		cout << mincost;
	}
	else {
		cout << "TLE";
	}
	return 0;
}


inline ll kaizyo(ll x) {
	long ans = 1;
	for (int z = 0; z < x; z++) {
		ans *= (x - z);
	}
	return ans;
}
ll gcd(ll a, ll b) {
	if (b == 0)return a;
	return gcd(b, a%b);
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a)  (a).begin(),(a).end()
ll ans=0;
void Main(){
    ll x;cin>>x;
    ll y;cin>>y;
    ll a;cin>>a;
    ll b;cin>>b;
    ll c;cin>>c;
    vector<ll> p(a);
    For(i,0,a) cin>>p[i];
    vector<ll> q(b);
    For(i,0,b) cin>>q[i];
    vector<ll> r(c);
    For(i,0,c) cin>>r[i];
    sort(ALL(p));
    sort(ALL(q));
    sort(ALL(r));
    reverse(ALL(p));
    reverse(ALL(q));
    reverse(ALL(r));
    priority_queue<ll,vector<ll>, greater<ll>> red,green;
    For(i,0,x){
        red.push(p[i]);
        ans+=p[i];
    }
    For(i,0,y){
        green.push(q[i]);
        ans+=q[i];
    }
    For(i,0,c){
        if(!red.empty()&&!green.empty()){
            ll R=red.top(),G=green.top();
            if(R>G){
                if(r[i]>G){
                    green.pop();
                    ans+=(r[i]-G);
                }
            }
            else{
                if(r[i]>R){
                    red.pop();
                    ans+=(r[i]-R);
                }
            }
        }
        else if(!red.empty()){
            ll R=red.top();
            if(r[i]>R){
                red.pop();
                ans+=(r[i]-R);
            }
        }
        else if(!green.empty()){
            ll G=green.top();
            if(r[i]>G){
                green.pop();
                ans+=(r[i]-G);
            }
        }
        else{
            break;
        }
    }
    cout<<ans<<endl;
}
int main(){
    Main();
    /*
    東方風神録は神が出てくるので当然神ゲー
    */
    return 0;
}

Measured memory_bytes: 4852
#include <iostream>
#include <assert.h>
#include <vector>
#include <unordered_map>
#include <queue>
#include <climits>
#include <cmath>
#include <algorithm>
#include <unordered_set>
#include <iomanip>
using namespace std;
constexpr int64_t MOD = 1e9 + 7;
static const double pi = 3.141592653589793;

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

  double A,B,H,M;
  cin >> A >> B >> H >> M;
  double MD = (double) M / 60 * 2 * pi;
  double HD = (double) (60 * H + M) / 720 * 2 * pi;
  double res = 0;
  double diff = abs(HD-MD);
  if (diff > pi) {
    double delta = 2*pi - diff;
    double square = A*A + B*B - 2*A*B* cos(delta);
    res = sqrt(square);
  } else if (diff == pi) {
    res = abs(A+B);
  } else if (diff == 0) {
    res = abs(A-B);
  } else {
    double delta = diff;
    double square = A*A + B*B - 2*A*B* cos(delta);
    res = sqrt(square);
  }
  cout << fixed << setprecision(15) << res << endl;

  return 0;
}


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

using namespace std;

int main() {
	int n, h;
	cin >> n >> h;
	int a, b[100001] = {};
	int ma = 0;
	for (int i = 0; i < n; ++i) {
		cin >> a >> b[i];
		ma = max(ma, a);
	}
	sort(b, b + n, greater<int>());
	int ans = 0;
	for (int i = 0; i <= n; ++i) {
		if (b[i] >= ma) {
			h -= b[i];
			ans++;
		}
		else {
			ans += h / ma + !(h % ma == 0);
			h = 0;
		}
		if (h <= 0) break;
		
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 640
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
  var x = chunk.split(" ");
  x[0] = Number (x[0])
  x[1] = Number (x[1])
  x[2] = Number (x[2])
  x.sort();
  console.log(x[0] + " " + x[1] + " " + x[2])
});

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

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

    int n = s.length();
    int mid = n / 2;
    int count = 0;
    for (int i = 0; i < mid; i++) {
        if (s.at(i) != s.at(n - 1 - i)) {
            count++;
        }
    }
    cout << count << endl;

    return 0;
}

Measured memory_bytes: 256
s = input()
revs = s[::-1]
MAX = 0
for i in range(len(s)):
  if s[i]=="A":
    begin = i
    break
    
for i in range(len(s)):
  if revs[i]=="Z":
    end = i+1
    break
print(len(s)-end-begin+1)

Measured memory_bytes: 3516
import heapq
from collections import defaultdict
# setrecursionlimit(10**7)


def main():
    N,M = [int(n) for n in input().split()]
    # {期限:価値}
    jobs = defaultdict(list)
    v = 0
    for i in range(N):
        a,b = [int(n) for n in input().split()]
        if a <= M:
            jobs[a].append(-b)
    cand = []
    ans = 0
    for i in range(1,M+1):
        for e in jobs[i]:heapq.heappush(cand,e) 
        if len(cand) != 0:
            ans -= cand[0]
            heapq.heappop(cand)
    print(ans)


if __name__ == "__main__":
    main()

Measured memory_bytes: 79448
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>

#define ll long long
#define mod 1000000007

using namespace std;

int main() {
    int n, t, s, p;
    bool flag = false;
    cin >> n;
    t = 0;
    s = 0;
    for(int i = 1; i <= n; i++){
        cin >> p;
        if(p != i){
            if(flag){
                if((i != t) || (p != s)){
                    cout << "NO" << endl;
                    return 0;
                }
            }else{
                t = p;
                s = i;
                flag = true;
            }
        }
    }
 
    cout << "YES" << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
int isPrime(int x)
{
    int i;
    if(x < 2)   return 0;
    else if(x == 2) return 1;
    if(x % 2 == 0)  return 0;
    for(i=3; i*i<=x; i+=2){
        if(x%i==0)  return 0;
    }
    return 1;
}

int main(){
    int n, x, i;
    int ans = 0;
    
    scanf("%d", &n);
    
    for(i=0; i<n; i++){
        scanf("%d", &x);
        if(isPrime(x)) ans++;
    }

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

Measured memory_bytes: 1856
#include <bits/stdc++.h>
using namespace std;
struct _I { _I() {cin.tie(0);ios::sync_with_stdio(false);}} __I;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i < (n); i++)
#define all(a) begin(a),end(a)
template<class F, class S> istream& operator >> (istream& s, pair<F, S>& p)
{ cin >> p.first >> p.second; return s;}
template<class T> istream& operator >> (istream& s, vector<T>& v)
{ for (T& x: v) { s >> x; } return s;}
template<class T> void cout_h(const vector<T>& c, int n=INT_MAX)
{ rep(i, min(n, (int)c.size())) cout << c[i] << (i==c.size()-1?"\n":" "); }
template<class T> inline bool chmin(T& a, T b) {return a>b?a=b,true:false;}
template<class T> inline bool chmax(T& a, T b) {return a<b?a=b,true:false;}
#define D1(a) cout<<#a<<":"<<a<<"\n"
#define D2(a,b) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<endl 
#define D3(a,b,c) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<","<<#c<<":"<<c <<endl
#define D4(a,b,c,d) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<","<<#c<<":"<<c<<","<<#d<<":"<<d<<endl 
#define MOD 1000000007

ll solve(ll h)
{
    if (h == 0)
        return 0;
    if (h == 1)
        return 1;

    ll next_h = h / 2LL;

    return 1LL + solve(next_h) * 2;
}

int main()
{
    ll H; cin >>H;

    ll ans = solve(H);

    cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;

int N, M, A, R[2000];
pair<int, pii> L[2000];

int main(void) {
  while(cin >> N >> M >> A, N || M || A) {
    REP(i, 0, M) cin >> L[i].fi >> L[i].se.fi >> L[i].se.se;

    sort(L, L + M);
    reverse(L, L + M);

    REP(i, 1, N + 1) R[i] = i;
    REP(i, 0, M) {
      int tmp = R[L[i].se.fi];
      R[L[i].se.fi] = R[L[i].se.se];
      R[L[i].se.se] = tmp;
    }

    REP(i, 1, N + 1) if(R[i] == A) cout << i << endl;
  }

  return 0;
}

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

#define ll long long
#define ull unsigned long long
#define ld long double

#define tiii tuple < int, int, int >
#define pii pair < int, int >
#define pll pair < ll, ll >
#define pci pair < char, int >
#define psi pair < string, int >
#define pis pair < int, string >

#define vi vector < int >
#define vd vector < double >
#define vld vector < ld >
#define vll vector < ll >
#define vs vector < string >
#define vc vector < char >
#define vpi vector < pii >
#define vpll vector < pll >
#define vpis vector < pis >

#define pb push_back
#define mt make_tuple
#define gt0 get < 0 >
#define gt1 get < 1 >
#define gt2 get < 2 >

#define pi acos(-1)

#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

using namespace std;

const int maxn = 1e5 + 5;
const int mod = 1e9 + 9;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    fast;

    ll n, k, d;

    cin >> n >> k >> d;

    n = abs(n);

    ll tmp = (n / d);

    if(tmp >= k){
        cout << (n - (k * d)) << endl;
        return 0;
    }

    ll thakbe = (n - (tmp * d)), moves = (k - tmp - 1), baki = abs(thakbe - d);

    if(moves & 1) cout << thakbe << endl;
    else cout << baki << endl;

    return 0;
}


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

using namespace std;

int main()
{
  long long k; cin >> k;
  vector <long long> a(50, 49 + k / 50);

  cout << 50 << endl;

  for (long long i = 0; i < k % 50; ++i)
{
  a[i] += 50;
  for (long long j = 0; j < 50; ++j)
  if (i != j)
  a[j]--;
}

for (auto elem : a)
cout << elem << " ";
cout << endl;

  return 0;
}

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

public class Program{
  public static void Main(){
    var a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
    Console.WriteLine(Math.Max(0, Math.Min(a[1],a[3])-Math.Max(a[0],a[2])));
  }
}


Measured memory_bytes: 11092
#include <bits/stdc++.h>
using i64 = long long;

int main() {
    constexpr i64 mod = 1000000007;
    i64 n, h, d;
    std::cin >> n >> h >> d;
    std::vector<i64> a(h + 1);
    i64 sum = 0;
    i64 co = 0, f = 1;
    for (i64 i = 1; i <= n; i++) {
        f = f * i % mod;
        co = (co + f) % mod;
    }
    for (int i = 1; i <= h; i++) {
        a[i] = sum * co % mod;
        if (i <= d) a[i] = (a[i] + f) % mod;
        else sum = (sum + mod - a[i - d]) % mod;
        sum = (sum + a[i]) % mod;
    }
    std::cout << a[h] << std::endl;

    return 0;
}


Measured memory_bytes: 8064
#include<utility>
#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>


// priority is lesser.
class union_find
{

	public:

		using value_type = std::int32_t;
		using size_type
			= decltype( std::declval< std::vector < value_type > >().size() );

	private:
		std::vector<int> parent_;
		//std::vector<int> size_;

	public:

		// uf_[0,n_-1]
		union_find( size_type n_ )
			:parent_{}
			//, size_{}
		{
			parent_.resize(n_ ,-1);
//			std::iota( std::begin( parent_ ), std::end( parent_ ), 0 );
		}

	auto root( value_type x )->value_type{
		//std::cout << "root->" << x << "\n";

		auto& p_ = parent_[x];

		return 
			p_ < 0  ? x : p_ = root( p_ );
	}

	//auto root( value_type x )const->value_type{
	//	return 
	//		x == parent_[x]
	//		? x
	//		: root( parent_[x] );
	//}

	//auto croot( value_type x )const{
	//	return root( x );
	//}


	auto size()const{
		return parent_.size();
	}

	auto size( value_type x )->size_type{
		return - parent_[root( x )];
	}

	//auto size( value_type x )const{
	//	return size_[croot( x )];
	//}

	//auto csize( value_type x )const{
	//	return size( x );
	//}

	auto unite( value_type a_, value_type b_ ){

		//std::cout <<"unite-"<< a_ <<" "<< b_ << "\n";

		if( a_ == b_ )
			return;

		// mut : to swap;
		auto ra_ = root( a_ );
		auto rb_ = root( b_ );

		if( ra_ == rb_ )
			return;


		const auto sa_ = size( a_ );
		const auto sb_ = size( b_ );

		//should be ra_ < rb_
		if( sa_ > sb_ )
			std::swap( ra_, rb_ );


		//if( sa_ == sb_ )
		//	++size_[ra_];
		parent_[ra_] += parent_[rb_];

		parent_[rb_] = ra_;

	}

	auto same( value_type a_, value_type b_ ){
		return root( a_ ) == root( b_ );
	}

	auto dump(){
		for( int i{}; i < parent_.size(); ++i )
		{
			std::cout 
				<< ( i + 1 ) << "<-" 
				<<parent_[i]
				<< " |" << size( i ) << "|\n";
		}
	}

	//auto answer(){
	//	return -*std::min_element( std::begin( parent_ ), std::end( parent_ ) );
	//}

};

int main(){

	std::uint32_t N, M;
	std::cin >> N >> M;

	union_find uf_{ N };

	for( int i{}; i < M; ++i )
	{
		int A, B;
		std::cin >> A >> B;
		--A; --B;
		
		uf_.unite( A, B );

		//std::cout <<"sa_sb_:"<< uf_.size(A) << " " << uf_.size( B ) << "\n";

	}
	
//	uf_.dump();


	//if( ans_ != uf_.answer() )
	//	throw std::exception{};

	auto ans_ = decltype( uf_.size( std::declval<int>() ) ){};

	for( int i{}; i < N; ++i )
		ans_ = std::max( ans_, uf_.size( i ) );

	std::cout << ans_ << "\n";

	return 0;
}

Measured memory_bytes: 9868
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int n,s,ans;
int t[111111],d[111111];

int main(){
  cin>>n>>s;
  for(int i=0;i<n;i++)cin>>t[i];
  sort(t,t+n);

  for(int i=0;i<n;i++){
    if(i==n-1){
      d[i]=s;
      s=0;
    }
    else if(s>=t[i]){
      d[i]=t[i];
      s-=d[i];
    }
    else{
      d[i]=s;
      s=0;
    }
  }
  for(int i=0;i<n;i++)if(d[i]==t[i])ans++;
  cout<<ans<<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 x = sc.nextInt();
		int max = 0;

		if(x / 500 > 0){
			max = ((x / 500) * 1000) + max;
			if((x % 500) / 5 > 0){
				max = (((x % 500) / 5) * 5) + max;
			}
		}
		else if(x / 5 > 0){
			max = (x / 5) * 5 + max;
		}

		System.out.println(max);
		sc.close();
	}
}

Measured memory_bytes: 22992
#include <iostream>

int main(){
 long long A,B,C,K;
 std::cin>>A>>B>>C>>K;
 
 long long ans=0;
 if(K<A){
   ans=K;
 }else if(K<=A+B){
   ans=A;
 }else{
   ans=A-(K-A-B);
 }
 
 std::cout<<ans<<std::endl;
 return 0;
}

Measured memory_bytes: 3628
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int n = Integer.parseInt(sc.next());
        int[] a = new int[n*3];
        for(int i=0; i<n*3; i++){
            a[i] = Integer.parseInt(sc.next());
        }
        
        //前N個をプライオリティキューに突っ込む
        long fsum = 0L;
        PriorityQueue<Integer> fq = new PriorityQueue<>();
        for(int i=0; i<n; i++){
            fsum += a[i];
            fq.add(a[i]);
        }
        
        long[] fk = new long[n+1];
        fk[0] = fsum;
        //前2N個のうちK個捨てる。
        for(int k=1; k<=n; k++){
            fq.add(a[n+k-1]);
            fsum += a[n+k-1];
            fsum -= fq.poll();
            fk[k] = fsum;
        }
        
        //後ろN個をプライオリティキューに突っ込む
        long rsum = 0L;
        PriorityQueue<Integer> rq = new PriorityQueue<>(Comparator.reverseOrder());
        for(int i=n*3-1; i>=n*2; i--){
            rsum += a[i];
            rq.add(a[i]);
        }
        
        long[] rk = new long[n+1];
        rk[0] = rsum;
        //後ろ2N個のうちK個捨てる。
        for(int k=1; k<=n; k++){
            rq.add(a[n*2-k]);
            rsum += a[n*2-k];
            rsum -= rq.poll();
            rk[k] = rsum;
        }
        
        long max = -(1L<<60);
        //前K個+後ろK個が丁度N個になるモノで最大を取る
        for(int i=0; i<=n; i++){
            long tmp = fk[i] - rk[n-i];
            max = Math.max(max, tmp);
        }
        
        System.out.println(max);
        
    }
}



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

int main(){

  int i,j,n,count;
  double x;

  while(1){

    count=0;

    cin >> n;
    
    if(n==0){
      break;
    }

    for(i = n+1;i <= 2*n;i++){
      x = sqrt(2*i);
      for(j = 2;j <= x;j++){
	if(i%j == 0 && i > 2){
	  break;
	}
	if(j == (int)x){
	  count++;
	}
      }
    }
    cout << count << endl;
    
    
  }
}

Measured memory_bytes: 1160
#include<iostream>
#include<string>
#include<vector>
#include<sstream>


using namespace std;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n;
	vector<int> b(1000000,0);
	
	int cnt=0;
	cin>>n;
	vector<int>a(n,0);

	for(int i=0;i<n;i++){
		cin>>a[i];
		if(a[i]>100000)
			cnt++;
		else
			b[a[i]]++;
	}
	
	for(int i=1;i<=100000;i++){
		if(b[i]>i)
			cnt+=(b[i]-i);
		else if(b[i]<i)
			cnt+=b[i];
	}
	
	cout<<cnt;
	
	
	
	return 0;

}


Measured memory_bytes: 4608
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def make_prime_list(lower: int, upper: int) -> list:
    if upper <= 2:
        return []
    
    prime_list = []
    
    is_prime = [True]*upper
    is_prime[0] = False
    is_prime[1] = False
    
    n = 2
    while n**2 < upper:
        if is_prime[n]:
            res = 2*n
            while res < upper:
                is_prime[res] = False
                res += n
                
        n += 1
        
    for i in range(lower, upper):
        if is_prime[i]:
            prime_list.append(i)
            
    return prime_list

def is_like2017(num: int, prime_set) -> int:
    if num in prime_set and (num+1)//2 in prime_set:
        return 1
    else:
        return 0

from itertools import accumulate

MAX_N = 10**5+1

prime_list = make_prime_list(1,MAX_N)
prime_set = set(prime_list)

prime_cnt = [0]*MAX_N

for i in range(1,MAX_N,2):
    prime_cnt[i] += is_like2017(i, prime_set)
        
prime_cum = list(accumulate(prime_cnt))


q = ni()
for _ in range(q):
    l,r = li()
    print(prime_cum[r] - prime_cum[l-1])

Measured memory_bytes: 8660
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<queue>
#include<stack>
#include<list>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
#include<iomanip>

using namespace std;
typedef long long int ll;typedef pair<int, int> P;
const long long mod = 1e9 + 7;
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(a) (a).begin(),(a).end()
#define allg(a) (a).begin(),(a).end(),greater<>()
const double PI = 3.14159265359;

int main(void) {
	string str; cin >> str;
	if (str == "Sunny")cout << "Cloudy" << endl;
	else if (str == "Cloudy")cout << "Rainy" << endl;
	else cout << "Sunny" << endl;
	return 0;
}

Measured memory_bytes: 256