akhauriyash/Code-Regression huggingface dataset:
Scanf.scanf "%d" (fun n ->
    let div = Array.make 1000000 0 in
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in
    let rec loop i g =
        if i = n then g else
            let g = Scanf.scanf " %d" (fun a ->
                let g = gcd a g in
                let rec loop r i =
                    if r = 1 then () else
                    if i * i > r then div.(r) <- div.(r) + 1 else
                        if r mod i <> 0 then loop r (i + 1) else
                            let () = div.(i) <- div.(i) + 1 in
                            let rec loop2 r =
                                if r mod i = 0 then loop2 (r / i) 
                                    else loop r (i + 1)
                            in
                            loop2 r
                in
                loop a 2;
                g
            )
            in
            loop (i + 1) g
    in
    let g = loop 0 0 in
    print_endline @@
        if g <> 1 then "not coprime" else
            if Array.fold_left max 0 div <= 1 then "pairwise coprime" else "setwise coprime"
)

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

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
    int M, T, P, R;
    while (cin >> M >> T >> P >> R, M|T|P|R) {
        vvi penalty(T, vi(P));
        vector<pair<pii, int>> score(T);
        REP(i, T) {
            score[i] = {{0, 0}, i+1};
        }

        REP(i, R) {
            int m, t, p, j;
            cin >> m >> t >> p >> j;
            t--;
            p--;
            if (j == 0) {
                score[t].first.first++;
                score[t].first.second -= m + penalty[t][p];
            } else {
                penalty[t][p] += 20;
            }
        }

        RSORT(score);
        cout << score[0].second;
        FOR(i, 1, SZ(score)) {
            if (score[i].first.first  == score[i-1].first.first &&
                score[i].first.second == score[i-1].first.second) {
                cout << "=";
            } else {
                cout << ",";
            }
            cout << score[i].second;
        }
        cout << endl;
    }
}

Measured memory_bytes: 3132
N = gets.chomp.to_i
a, b,time, job, ans = [], [], 0, 0, "Yes"

N.times do |n|
  a[n] = gets.chomp.split.map(&:to_i).reverse
end

a.sort!

N.times do |n|
  time = a[n][0]
  job += a[n][1]
  if time < job
    ans = "No"
    break
  end
end

puts ans

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

//#define int long long
typedef long long ll;
const int INF = 2e9;
//const ll INF = 9e18;


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int D;
    cin >> D;
    vector<int> c(26);
    for (int i=0;i<26;i++) cin >> c[i];
    vector<vector<int>> s(D,vector<int>(26));
    for (int i=0;i<D;i++){
        for (int j=0;j<26;j++){
            cin >> s[i][j];
        }
    }
    vector<int> t(D);
    //初期t[i]の決定
    for (int i=0;i<D;i++) {
        t[i] = i % 26;
    }

    ll oldsum = -INF;
    for (int m=0;m<100000;m++) {
        //ここでt[i]を更新していく
        int process = rand()%2;
        if (process == 0){
            int dd = rand()%D;
            int qq = rand()%26;
            int oldval = t[dd];
            t[dd] = qq;
            ll sum = 0;
            vector<int> last(26);
            for (ll i=0;i<D;i++){
                sum += s[i][t[i]];
                last[t[i]] = i+1;
                for (int j=0;j<26;j++){
                    sum -= c[j]*(i+1-last[j]);
                }
            }
            if (sum > oldsum) {
                oldsum = sum;
            } else {
                t[dd] = oldval;
            }
        } else {
            int dd1 = rand()%(D-1);
            int dd2 = dd1 + 1 + rand()%(D-dd1-1);
            int qq1 = t[dd1];
            int qq2 = t[dd2];
            t[dd1] = qq2;
            t[dd2] = qq1;
            ll sum = 0;
            vector<int> last(26);
            for (ll i=0;i<D;i++){
                sum += s[i][t[i]];
                last[t[i]] = i+1;
                for (int j=0;j<26;j++){
                    sum -= c[j]*(i+1-last[j]);
                }
            }
            if (sum > oldsum) {
                oldsum = sum;
            } else {
                t[dd1] = qq1;
                t[dd2] = qq2;
            }
        }
        //=======================       
    }
    for (int i=0;i<D;i++){
        cout << t[i]+1 << "\n";
    }
}

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

int main() {
  long a,b;
  cin >> a>>b;
  b++;
  vector<long>ans(40);
  rep(i,40){
    long cob=b/(long)pow(2,i+1)*(long)pow(2,i)+max(0L,b%(long)pow(2,i+1)-(long)pow(2,i));
    long coa=a/(long)pow(2,i+1)*(long)pow(2,i)+max(0L,a%(long)pow(2,i+1)-(long)pow(2,i));
    ans.at(i)=(cob-coa)%2;
  }
  long co=0;
  rep(i,40){
    co+=(long)pow(2,i)*ans.at(i);
  }
  cout<<co<<endl;
}

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

typedef long long ll;
typedef long double ld;

#define X first
#define Y second
#define all(x) x.begin(), x.end()

using namespace std;

inline int tmin(int a, int b, int c){
    b = min(a, b);
    c = min(b, c);
    return c;
}

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

    int n, x = -1, y = -1;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int j = 0; j < n; j++)
        cin >> b[j];
    vector<int> p(n);
    for (int i = 1; i <= n; i++)
        p[i - 1] = i;
    int cnt = 0;
    while(x == -1 || y == -1){
        if (a == p) x = cnt;
        if (b == p) y = cnt;
        next_permutation(all(p));
        cnt++;
    }
    cout << abs(x - y);
    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
// #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <bitset>
using namespace std;
typedef long long LL;
typedef double db;
const int oo=2139063143;
const int N=510;
const int P=1000000007;
const db eps=1e-7;
#define pritnf printf
//char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T>
inline void sc (T &x)
{
    x=0; static int p; p=1; static char c; c=getchar();
    while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); }
    while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); }
    x*=p;
}
template <typename T>
inline void print (T x)
{
    if (x< 0) putchar('-'),x=-x;
    if (x>=10) print(x/10);
    putchar(x%10+'0');
}
template <typename T>
inline void pr (T x) { print(x),putchar('\n'); }
template <typename T1,typename T2>
void chkmax (T1 &A,T2 B) { A=A>=B?A:B; }
template <typename T1,typename T2>
void chkmin (T1 &A,T2 B) { A=A<=B?A:B; }
int n; LL a[N][N],b[N][N];
LL get (int k,int j,int i) { return b[j+1][i]+a[n][j]-a[n][k]-a[i][j]+a[i][k]; }
LL f[N][N];
int main ()
{
    // freopen (".in","r",stdin);
    // freopen (".out","w",stdout);
	sc(n);
	for (int i=1; i<=n; i++)
		for (int j=1; j<=n; j++)
			if (i!=j) sc(a[i][j]);
	for (int j=1; j<=n; j++)
		for (int i=j; i>=1; i--)
			b[i][j]=a[i][j]+b[i+1][j]+b[i][j-1]-b[i+1][j-1];
	for (int i=1; i<=n; i++)
		for (int j=1; j<=n; j++)
			a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1];
	LL ans=1e18;
	for (int i=1; i< n; i++)
	{
		f[i][0]=get (0,0,i);
		chkmin (ans,f[i][0]+get (0,i,n));
		for (int j=1; j< i; j++)
		{
			f[i][j]=1e18;
			for (int k=0; k< j; k++)
				chkmin (f[i][j],f[j][k]+get (k,j,i));
			chkmin (ans,f[i][j]+get (j,i,n));
		}
	}
	pr(ans);

    return 0;
}


Measured memory_bytes: 6144
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e6 + 555;
const int MOD = 1e9 + 7;
int mp[maxn];
int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    cout << fixed;
    int n;
    cin >> n;
    vector<int> V(n);
    for (int &i : V) {
        cin >> i;
        mp[i]++;
    }
    int ans = 0;
    for (int i : V) {
        mp[i]--;
        bool ok = true;
        for (int j = 1; j * j <= i && ok; j++) {
            if (i % j == 0) {
                ok &= !mp[j];
                ok &= !mp[i / j];
            }
        }
        ans += ok;
        mp[i]++;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 7832
import sys
import copy
import time

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline

D = None
C = None
S = None

def cnt_score(t):
    global D, C, S
    score = 0
    cnt = [-1] * 26

    for i in range(D):
        score += S[i][t[i] - 1]
        minusscore = 0
        cnt[t[i] - 1] = i
        for j in range(26):
            minusscore += (i - cnt[j]) * C[j]
        score -= minusscore
    return score


def main():
    global D, C, S
    D = int(input())
    C = [int(x) for x in input().split()]
    S = [[int(x) for x in input().split()] for _ in range(D)]

    start = time.time()

    ans = []
    for s in S:
        ans.append(s.index(max(s)) + 1)

    #ans = [1] * D

    day = 0
    while True:
        # time up
        current = time.time()
        if current - start > 1.9:
            break

        # main
        new_ans = copy.deepcopy(ans)
        old_score = cnt_score(ans)

        max_sore = old_score
        max_contest = ans[day]
        for i in range(26):
            new_ans[day] = i + 1
            new_score = cnt_score(new_ans)
            if new_score > old_score:
                max_contest = i + 1
                old_score = new_score
        ans[day] = max_contest

        # increment day
        day = (day + 1) % D

    for a in ans:
        print(a)


if __name__ == '__main__':
    main()


Measured memory_bytes: 72820
/// ****** Property of vipulks ****** \

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

#define ll long long

int main(){
	ll n,i,j,k,q;
	//scanf("%lld",&t);
	q=1;
	char s[50],t[50];
	bool ok=true;
	while(q--){
		//scanf("%lld",&n);
		scanf("\n%s\n%s",s,t);
		ll n=strlen(s),m=strlen(t);
		if(m!=n+1)
			printf("No\n");
		else{
			for(i=0;i<n && ok;i++){
				ok=s[i]-t[i]==0?true:false;
			}
			if(ok && t[n]>=97 && t[n]<=122)
				ok=true;
			else
				ok=false;
		}
		if(ok)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}

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

int main() {
    int n; scanf("%d", &n);
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v; scanf("%d%d", &u, &v);
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vector<int> dp(n), cnt(n);
    function<void(int, int)> Dfs = [&](int x, int p) {
        int ch = 0;
        for (int u : g[x]) {
            if (u == p) continue;
            Dfs(u, x);
            dp[x] += dp[u];
            ch += 1;
        }
        dp[x] -= ch / 2;
        if (ch % 2 == 0 && x != 0) dp[x]++;
    };

    Dfs(0, -1);
    int A = dp[0], B = n + 1;

    auto Check = [&](int v) {
        constexpr int kInf = 1'000'000'000;
        function<void(int, int)> Dfs = [&](int x, int p) {
            multiset<int> s;
            dp[x] = kInf;
            cnt[x] = 0;
            for (int u : g[x]) {
                if (u == p) continue;
                Dfs(u, x);
                if (dp[u] > v) return;
                s.insert(dp[u]);
                cnt[x] += cnt[u];
            }
            if (s.size() & 1) {
                int z = kInf;
                while (!s.empty()) {
                    int r = *s.rbegin();
                    s.erase(s.find(r));
                    auto it = s.upper_bound(v - r); 
                    if (it == s.begin()) {
                        z = min(z, r);
                    } else {
                        s.erase(prev(it));
                        cnt[x]--;
                    }
                }
                dp[x] = z;
                if (x != 0) dp[x]++;
                if (dp[x] > v) {
                    dp[x] = kInf;
                    return;
                }
            } else {
                auto f = s;
                bool ok = true;
                int kc = cnt[x], z = kInf;
                while (!s.empty()) {
                    int r = *s.rbegin();
                    s.erase(s.find(r));
                    auto it = s.upper_bound(v - r); 
                    if (it == s.begin()) {
                        z = min(z, r);
                    } else {
                        cnt[x]--;
                        s.erase(prev(it));
                    }
                }
                if (z + 1 > v && x > 0) {
                    dp[x] = 1;
                    cnt[x]++;
                } else {
                    dp[x] = z + 1;
                }
            }
        };
        Dfs(0, -1);
        return cnt[0] == A && dp[0] != kInf;
    };

    for (int d = 20; d >= 0; --d) {
        if (B - (1 << d) <= 0) continue;
        if (Check(B - (1 << d))) B -= (1 << d);
    }
    printf("%d %d\n", A, B);
    return 0;
}


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

int main() {
    int N;
    cin >> N;
    
    vector<int> l(N);
    for (int n = 0; n < N; ++n){
      int t;
      cin >> t;
      l[n] = t;
    }

    int cnt = 0;
    while(*max_element(l.begin(), l.end()) > 0) {
      int max = *max_element(l.begin(), l.end());
      int index = distance(l.begin(), max_element(l.begin(), l.end()));
      l[index] -= 1;
      // 左側
      for(int i = index - 1;i>=0;--i){
        if (l.at(i) < max) break;
        l[i] -= 1;
      }
      // 右側
      for(int i = index + 1;i<l.size();++i){
        if (l.at(i) < max) break;
        l[i] -= 1;
      }

      // for(int i =0;i < l.size();++i) {
      //   cout << l.at(i) << ' ';
      // }
      // cout << endl;
      cnt += 1; 
    }
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 3560
#include <cstdio>
#include <algorithm>
using namespace std;

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

    int p[n+1], m[n][n];
    
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &p[i], &p[i+1]);
    }

    for (int i = 0; i < n; i++) { m[i][i] = 0; }

    for (int l = 1; l < n; l++) {
        for (int i = 0; i < n - l; i++) {
            int j = i + l;
            m[i][j] = (1 << 21);
            for (int k = i; k < j; k++) {
                m[i][j] = min(m[i][j], m[i][k] + m[k+1][j] + p[i] * p[k+1] * p[j+1]);
            }
            // printf("m[%d][%d] = %d\n", i, j, m[i][j]);
        }
        // printf("--\n");
    }
    printf("%d\n", m[0][n-1]);
    return 0;
}


Measured memory_bytes: 2772
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define repone(i,n) for(int i=1;i<(int)(n);++i)
#define repo(i,o,n) for(int i=o;i<(int)(n);++i)
#define Rep(j,n) for(int j=0;j<(int)(n);++j)
#define Repo(j,o,n) for(int j=o;j<(int)(n);++j)
#define Repone(j,n) for(int j=1;j<(int)(n);++j)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define int long long
#define pb(a) push_back(a)
#define eraser(v,i) (v).erase(v.begin()+(i))
#define pbpair(a,b) push_back(make_pair(a,b))
#define MOD 1000000007
#define INF 9223372036854775807
#define pairint pair<int,int>
signed main()
{
  int x=0;
int n =0,k=0,r=0,s=0,p=0;
string t;
std::cin >>n>>k>>r>>s>>p>>t;
char m[t.size()];
rep(i,t.size()){
  if(i<k){
    if(t[i]=='r'){
      x+=p;
      m[i]='p';
    }
    else if(t[i]=='s'){
      x+=r;
      m[i]='r';
    }
    else if(t[i]=='p'){
      x+=s;
      m[i]='s';
    }
  }else{
    if(t[i]=='r'){
      if(t[i-k]=='r'&&m[i-k]=='p'){
        m[i]='r';
      }else{
        x+=p;
        m[i]='p';
      }
    }
    else if(t[i]=='s'){
      if(t[i-k]=='s'&&m[i-k]=='r'){
        m[i]='s';
      }else{
      x+=r;
      m[i]='r';
    }
    }
    else if(t[i]=='p'){
      if(t[i-k]=='p'&&m[i-k]=='s'){
        m[i]='p';
      }else{
      x+=s;
      m[i]='s';
    }
    }
  }
}
std::cout << x << '\n';
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#define FOR(i,k,n)  for(int i = (k);i < (n);++i)
#define REP(i,n)    FOR(i,0,n)

using namespace std;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;

int main()
{
  int n;
  cin>>n;
  vecint v(n);
  REP(i,n) cin>>v[i];
  boost::sort(v);
  double avg = v[0];
  REP(i,n) {
    avg = (avg + v[i]) / 2.0;
  }
  cout<<fixed<<setprecision(10)<<avg<<endl;
  return 0;
}


Measured memory_bytes: 256
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range(N):
	for j in range(i+1, N):
		if L[i] != L[j]:
			for k in range(j+1, N):
				if L[k] != L[j]:
					if L[i] + L[j] > L[k]:
						ans += 1
print(ans)


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

long long cnt[100010];

int main() {
  int N;
  long long K;
  cin >> N >> K;
  for (int i=0 ; i<N ; i++) {
    int a;
    int b;
    cin >> a >> b;
    cnt[a] += b;
  }
  long long sum = 0;
  int ans;
  for (int i=1 ; i<100010 ; i++) {
    sum += cnt[i];
    if (K <= sum) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= int(m); --(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define debug(x) #x << " = " << (x) << " "
using ll = long long;
using namespace std;
template <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }
template <class T> void setmin(T & a, T const & b) { if (b < a) a = b; }
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

vector<int> sieve_of_eratosthenes(int n) { // enumerate primes in [2,n] with O(n log log n)
    vector<bool> is_prime(n+1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i*i <= n; ++i)
        if (is_prime[i])
            for (int k = i+i; k <= n; k += i)
                is_prime[k] = false;
    vector<int> primes;
    for (int i = 2; i <= n; ++i)
        if (is_prime[i])
            primes.push_back(i);
    return primes;
}
map<ll,int> prime_factrorize(ll n, vector<int> const & primes) {
    map<ll,int> result;
    for (int p : primes) {
        if (n < p *(ll) p) break;
        while (n % p == 0) {
            result[p] += 1;
            n /= p;
        }
    }
    if (n != 1) result[n] += 1;
    return result;
}
ll powmod(ll x, ll y, ll p) { // O(log y)
    assert (y >= 0);
    x %= p; if (x < 0) x += p;
    ll z = 1;
    for (ll i = 1; i <= y; i <<= 1) {
        if (y & i) z = z * x % p;
        x = x * x % p;
    }
    return z;
}

const int mod = 1e9+7;
int main() {
    int n; cin >> n;
    vector<int> primes = sieve_of_eratosthenes(sqrt(n) + 3);
    map<ll,int> factors;
    repeat_from (i,1,n+1) {
        for (auto it : prime_factrorize(i, primes)) {
            int p, cnt; tie(p, cnt) = it;
            factors[p] += cnt;
        }
    }
    ll ans = 1;
    for (auto it : factors) {
        int cnt = it.second;
        ans = ans * (1 + cnt) % mod;
    }
    cout << ans << endl;
    return 0;
}


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

struct LIST {
	int key;
	struct LIST* former;
	struct LIST* next;
};

typedef struct LIST* LIST_POS;

LIST_POS top = NULL;
LIST_POS tail = NULL;

void Insert(int newKey) {
	LIST_POS newEl = (struct LIST*)malloc(sizeof(struct LIST));
	newEl->key = newKey;
	newEl->former = NULL;
	if (top == NULL) {
		top = newEl;
		newEl->next = NULL;
		tail = newEl;
	}
	else {
		top->former = newEl;
		newEl->next = top;
		top = newEl;
	}
}

void DelFirst() {
	LIST_POS delPos;
	if (top != NULL) {
		delPos = top;
		if (tail != top) top->next->former = NULL;
		else tail = NULL;
		top = top->next;
		free(delPos);
	}
}

void DelLast() {
	LIST_POS delPos = tail;
	if (tail != NULL) {
		if (top != tail) {
			delPos->former->next = NULL;
			tail = tail->former;
		}
		else {
			top = NULL;
			tail = NULL;
		}
		free(delPos);
	}

}

void Delete(int delKey) {
	LIST_POS pos = top;
	while (pos != NULL && pos->key != delKey)pos = pos->next;
	if (pos == NULL)return;
	else if (pos->key == delKey) {
		if (pos == top) DelFirst();
		else if (pos->next == NULL) DelLast();
		else {
			pos->next->former = pos->former;
			pos->former->next = pos->next;
			free(pos);
		}
	}

}

void OpenKeys() {
	LIST_POS runP;
	for (runP = top; runP != NULL; runP = runP->next) {
		printf("%d", runP->key);
		if (runP->next != NULL)printf(" ");
	}
	printf("\n");
}



int main() {
	int N;
	char s[20]; int inpKey;

	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		scanf("%s", s);
		if (!strcmp(s, "deleteFirst")) DelFirst();
		else if (!strcmp(s, "deleteLast")) DelLast();
		else {
			scanf("%d", &inpKey);
			if (!strcmp(s, "insert"))Insert(inpKey);
			else if (!strcmp(s, "delete"))Delete(inpKey);
		}
	}

	OpenKeys();

	return 0;
}


Measured memory_bytes: 33936
S = input()

num_eraser = S.count('eraser')
num_erase = S.count('erase') - num_eraser
num_dreamer = S.count('dreamer') - S.count('dreamerase')
num_dream = S.count('dream') - num_dreamer
# dream + erase(r) で dreamer が含まれるのに注意

# print(num_eraser, num_erase, num_dreamer, num_dream)
cnt = num_eraser * 6 + num_erase * 5 + num_dreamer * 7 + num_dream * 5
# print('cnt', cnt)
if len(S) == cnt:
    print('YES')
else:
    print('NO')


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


int main(){

  int A,B,C,D,E;
  cin>>A>>B>>C>>D>>E;


  if(A > 0) cout<<(B-A)*E<<endl;
  if(A == 0) cout<<D+(B*E)<<endl;
  if(A < 0) cout<<D+(B*E)+C*(-A)<<endl;

 return 0;
}


Measured memory_bytes: 3092
#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,sum,ans;
int b[20];
int g[20][20],s[20][20];
bool check(){
	for(int i=1;i<=n;i++){
		if(!b[i])continue;
		for(int j=1;j<=g[i][0];j++)
			if(b[g[i][j]]!=s[i][j])
				return false;
	}
	return true;
}
void dfs(int now){
	if(now>n){
		if(check())
			ans=max(ans,sum);
		return;
	}
	++sum;
	b[now]=1;
	dfs(now+1);
	--sum;
	b[now]=0;
	dfs(now+1);
}
int main(){
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>g[i][0];
		for(j=1;j<=g[i][0];j++)
			cin>>g[i][j]>>s[i][j];
	}
	dfs(1);
	cout<<ans<<endl;
}

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

int a, b, k, ans=0, imax=99999999;
vector< vector<char> > board;
vector< vector<int> > seen;
vector< vector<int> > dis;
queue<int> qh, qw, dh, dw, dk;

void bfs(int h, int w, int hh, int ww, int kk){
	//移動してきた時の処理
	if(seen[h][w]) return;
	seen[h][w]=1;//一度踏んだらもう踏めなくする（いる？）
//cout<< h << " "  << w <<endl;
	//移動する処理
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(i==0 && j==0) continue;
			if(i*j!=0) continue;//8方向にする場合はコメントアウト
			if(i*hh==-1 || j*ww==-1) continue;
			if(kk!=k && (i*hh==1 || j*ww==1)) continue;
			for(int l=1;l<=k;l++){
				if(-1<h+i*l && h+i*l<a && -1<w+j*l && w+j*l<b){
					if(board[h+i*l][w+j*l]=='@') break;
					if(dis[h+i*l][w+j*l]<=dis[h][w]+1 || seen[h+i*l][w+j*l]==1) continue;
					qh.push(h+i*l);
					qw.push(w+j*l);
					dh.push(i);
					dw.push(j);
					dk.push(l);
					dis[h+i*l][w+j*l]=dis[h][w]+1;
				}else{
					break;
				}
			}
		}
	}
}

int main(){

	cin>> a >> b >> k;
	int x1, y1, x2, y2;
	char tmp;
	cin>> x1 >> y1 >> x2 >> y2;
	x1--;
	y1--;
	x2--;
	y2--;
	board.resize(a);
	dis.resize(a);
	seen.resize(a);
	for(int i=0;i<a;i++) board[i].resize(b);
	for(int i=0;i<a;i++) dis[i].resize(b, imax);
	for(int i=0;i<a;i++) seen[i].resize(b, 0);

	//1字ずつ入力する場合
	for(int i=0;i<a;i++){
		for(int j=0;j<b;j++){
			cin>> tmp;
			board[i][j]=tmp;
		}
	}

	qh.push(x1);
	qw.push(y1);
	dh.push(0);
	dw.push(0);
	dk.push(k);
	//cout<< qh.front() << " " <<qw.front() <<endl;
	dis[x1][y1]=0;
  	while (!qh.empty()) {
    	bfs(qh.front(), qw.front(), dh.front(), dw.front(), dk.front());
    	qh.pop();
    	qw.pop();
    	dh.pop();
    	dw.pop();
    	dk.pop();
  	}

	//dis表示
	/*for(int i=0;i<a;i++){
		for(int j=0;j<b;j++){
			if(dis[i][j]==imax) cout<< "x ";
			else cout<< dis[i][j] << " ";
		}
		cout<< endl;
	}*/
	
	if(dis[x2][y2]==imax) cout<< "-1" <<endl;
	else cout<< dis[x2][y2] <<endl;

	return 0;
}

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

int main() {
    string s;
    int K;
    cin >> s >> K;
    vector<string> subStrs;
    int n = s.size();
    for (int i=0; i<n; i++) {
        for (int j=1; j<=min(5, n - i); j++) {
            subStrs.push_back(s.substr(i, j));
        }
    }
    map<string, int> mp;
    rep(i, subStrs.size()) mp[subStrs[i]]++;
    int cnt = 1;
    for(auto p: mp) {
        if (cnt == K) {
            cout << p.first << endl;
            break;
        }
        cnt++;
    }
    return 0;
}

Measured memory_bytes: 2556
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);

		String S = sc.nextLine();
		String U = S.toUpperCase();
		String L = S.toLowerCase();


		for (int i=0; i<S.length(); i++) {
			if (S.charAt(i) == U.charAt(i)) {
				System.out.print(L.charAt(i));
			}else {
				System.out.print(U.charAt(i));
			}
		}
		System.out.println();
	}
}



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

int main() {
	int N, max;
    cin >> N;
    max = (N/4)+1;
    bool Ans = 0;
    for (int i=0; i<=max; i++){
      	for (int j=0; j<=max-i; j++){
          	if ((4*i+7*j)==N){
                Ans = 1;
                break;
            }
        }
        if (Ans == 1) break;
    }
    if (Ans == 1) cout << "Yes" << endl;
    else cout << "No" << endl;
}


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

using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
using D = double;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;

#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define rep(i, x)  for(int i = 0; i < (x); i++)
#define rrep(i, x) for(int i = (x - 1); i >= 0; i--)
#define sqrt(x) sqrt(abs(x))
#define y1 y1_1234413
#define j1 j1_235
#define y0 y0_235
#define j0 j0_256
#define fi first
#define se second
#define re return
#define prev PREV
#define next NEXT
#define sz(x) ((int)x.size())

template<typename T> T sqr(T a) { re a * a; }
template<typename T> T gcd(T a, T b) { re b ? gcd(b, a % b) : a; }
template<typename T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }
template<typename T> T abs(T a) { re a > 0 ? a : -a; }

const int inf = 2e9;
const ld st = 0.000001;
const ld pi = acos((ld)-1);
const int MAXN = 100000;
const int mod = 1e9 + 7;


#define FILENAME ""


int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    //  freopen(FILENAME ".in", "r", stdin);
    //  freopen(FILENAME ".out", "w", stdout);
    int a, b;
    cin >> a >> b;
    int k = 1;
    ll ans = 0;
    while (k < b) {
        --k;
        k += a;
        ans++;
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
//#define int long long  
#define INF 1145141919810893
signed main(){
    int N,K;
    int A[214514];
    int d=0;
    int f=0;
    int maxA=0;
    int S[214514]={};
    int Ans=0;
    cin >> N >> K;
    REP(i,N){cin >> A[i];S[A[i]]++;maxA=max(maxA,A[i]);}
    maxA++;
    REP(i,maxA){if(S[i]!=0){d++;}}
    sort(S,S+maxA);
    REP(j,maxA){if(S[j]!=0){f=j;break;}}
    //REP(i,maxA){cout << S[i] << " ";}cout << endl;
    //cout << f;
    REP(i,d-K){
        Ans+=S[i+f];
    }
   // cout << maxA << endl;
    cout << Ans  << endl;
	return 0;
}

Measured memory_bytes: 1920
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>   
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int main(){
	int n;cin>>n;
	int out=inf;
	loop(i,1,n){
		int a=i,b=n-a;
		int c=0;
		while(a){
			c+=a%10;
			a/=10;
		}
		while(b){
			c+=b%10;
			b/=10;
		}
		out=min(out,c);
	}
	
	cout<<out<<endl;
}











Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
int main(){
    int N,i;
    cin >> N;
    long long int mod = 1000000007,ans=1;
    for(i=1;i<N+1;i++){
        ans *= i;
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<stack>
#include<sstream>
#include<iomanip>
typedef long long ll;

#define pi 3.14159265358979323846
#define VI vector<int>
#define VLL vector<long long>
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()

using namespace std;

template<typename T>
T gcd(T a, T b) {
	return b ? gcd(b, a % b) : a;
}
template<typename T>
T lcm(T a, T b) {
	return a * b / gcd(a, b);
}

template<typename T>
T finder(vector<T> vec, T number) {
	auto itr = find(vec.begin(), vec.end(), number);
	size_t index = distance(vec.begin(), itr);
	if (index != vec.size()) { // 発見できたとき
		return 1;
	}
	else { // 発見できなかったとき
		return 0;
	}
}

/*char r[4];
int yaju[4];
char op[3];
int sum = 0;
scanf("%s", r);
for (int i = 0;i < 4;i++) {
	yaju[i] = r[i] - '0';
}
*/
int main() {
	ll N,M; cin >> N>>M;
	VLL X(M);
	VLL dis(M - 1);
	ll ans=0;
	for (int i = 0;i < M;i++) cin >> X[i];
	sort(all(X));
	for (int j = 0;j < M-1;j++) {
		dis[j] = X[j + 1] - X[j];
	}
	sort(all(dis));
	for (int i = 0;i<M -N ;i++) {
		ans += dis[i];
	}
	cout << ans << endl;
}


Measured memory_bytes: 1792
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String a=sc.next();
		String b=sc.next();
		String c=sc.next();
		if(a.charAt(a.length()-1)==b.charAt(0) && b.charAt(b.length()-1)==c.charAt(0))System.out.println("YES");
		else System.out.println("NO");
	}

}


Measured memory_bytes: 23508
X, N = map(int, input().split())
p_list = [int(e) for e in input().split()]

t = 0
for i in range(N + 1):
    x = X - i
    y = X + i
    if x in p_list:
        t += 0
    else:
        print(x)
        break
    if y in p_list:
        t += 0
    else:
        print(y)
        break

Measured memory_bytes: 9200
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>

#define rep(i,x,y) for (int i=x;i<=y;i++)
#define dep(i,y,x) for (int i=y;i>=x;i--)
#define sz(x) (int)(x.size())

using namespace std;

typedef long long LL;   
typedef unsigned long long uLL;

const int maxn=400000+7;

int R,C,n,x1,y1,x2,y2,st,k,size,Q[maxn],a[maxn];
bool vis[maxn];

vector<pair<int,int> > v[5];

bool onborad(int x,int y)
{
    if ((x==R)||(x==0)) return true;
    if ((y==C)||(y==0)) return true;
    return false;
}

void deal(int x,int y,int k)
{
    if (x==0)
    {
        v[1].push_back(make_pair(y,k));
        return;
    }

    if (x==R)
    {
        v[3].push_back(make_pair(y,k));
        return;
    }

    if (y==0)
    {
        v[2].push_back(make_pair(x,k));
        return;
    }

    if (y==C)
    {
        v[4].push_back(make_pair(x,k));
    }
}

int main()
{
    scanf("%d%d%d",&R,&C,&n);
    rep(i,1,n)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if (onborad(x1,y1)&&onborad(x2,y2)) deal(x1,y1,i),deal(x2,y2,i);
    }

    rep(i,1,4) sort(v[i].begin(),v[i].end());
    
    dep(i,sz(v[1])-1,0) a[++size]=v[1][i].second;
    rep(i,0,sz(v[2])-1) a[++size]=v[2][i].second;
    rep(i,0,sz(v[3])-1) a[++size]=v[3][i].second;
    dep(i,sz(v[4])-1,0) a[++size]=v[4][i].second;

    memset(vis,0,sizeof(vis));
    st=0;

    rep(i,1,size)
    {
        k=a[i];
        if (!vis[k])
            Q[++st]=k,vis[k]=1;
        else
        {
            if (Q[st]!=k) {st=-1;break;} else --st;
        }
    }

    if (st==-1) printf("NO\n"); else printf("YES\n");

    // system("pause");
    return 0;
}


Measured memory_bytes: 3480
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;
//const int MOD = 998244353;

// Lib
//

int main(){
#ifdef MYLOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif
    int N; cin >> N;
    vector<ll> s(N);
    for (int i = 0; i < N; ++i) cin >> s[i];

    ll res = 0;
    for (int p = 1; p <= N-1; ++p) {
        if ((N-1) % p == 0) {
            ll tmp = 0;
            ll cur = 0;
            int i = 0, j = N-1;
            for (; i < j; i += p, j -= p) {
                cur += s[i] + s[j];
                tmp = max(tmp, cur);
            }
            res = max(res, tmp);
        }
        else {
            ll tmp = 0;
            ll cur = 0;
            int i = 0, j = N-1;
            for (; i < N-1 && j > p; i += p, j -= p) {
                cur += s[i] + s[j];
                tmp = max(tmp, cur);
            }
            res = max(res, tmp);
        }
    }
    cout << res << endl;
    return 0;
}


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

using namespace std;

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << ((a / d + (a % d == 0 ? 0 : 1)) >= (c / b + (c % b == 0? 0 : 1)) ? "Yes" : "No") << endl;
    return 0;
}

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

// 総数を1000000007で割った余り
const long long mod = 1e9 + 7;

using ll  = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

void printVec(vector<int>& vec) {
    for (auto& e : vec) {
        cout << e << " ";
    }
    cout << endl;
}

enum moji {
    A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
    I,
    J,
    K,
    L,
    M,
    N,
    O,
    P,
    Q,
    R,
    S,
    T,
    U,
    V,
    W,
    X,
    Y,
    Z
};

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

    // int N=1;
    // string S="ABC";
    // cout << 'A'+1 << endl;
    // cout << 'B'-0 << endl;
    // cout << 'Z'-25 << endl;
    // cout << static_cast<char>('Z'-25)	 << endl;
    // cout << static_cast<char>(S[0]+1)	 << endl;

    // 65-90
    for (int i = 0; i < S.size(); i++) {
        int moji = S[i] + N;
        if (moji > 90) {
            moji -= 26;
        }
        cout << static_cast<char>(moji);
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 256
Scanf.scanf "%d %d %d\n" (fun a b k ->
  let mv (a, b) =
    let a = if a mod 2 = 1 then a-1 else a in
    let b = b + (a/2) in
    let a = a - (a/2) in
    (a,b)
  in
  let rec loop i (a,b) =
    if i=k then (a,b) else
    if i mod 2 = 0 then loop (i+1) (mv (a,b)) else
    let (b,a) = mv (b,a) in loop (i+1) (a,b)
  in
  let (a,b) = loop 0 (a,b) in
  Printf.printf "%d %d\n" a b
)

Measured memory_bytes: 384
N = int(input())
alph = {}
p = 1
for i in range(97, 97+26):
    alph[str(p%26)] = chr(i)
    p += 1
k = 0
ans = []
while(N > 0):
    ans.append(alph[str(N % 26)])
    N = (N-1) // 26
print("".join(ans[::-1]))

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

int main(){
  int N=0;
  string result="No";
  cin >> N;
  for (int i=1;i<10;i++){
    for(int j=1;j<10;j++){
      if (N == i * j) result= "Yes";
    }
  }
  cout << result;
}

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


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

    //Insert code
    
    ll n,tmp;
    cin>>n;
    set<ll> a;
    ll b[139225];
    memset(b,0,sizeof(b));
    for (int i=0;i<n;i++){
        cin>>tmp;
        a.insert(tmp);
        b[tmp]++;
    }
    ll cnt=0,iter=0,iter2=0;
    bool check=false;
    for (auto i:a){
        cnt+=b[i];
        if (cnt==n/2){
            iter=i;
        }
        if (cnt>n/2){
            iter2=i;
            check=true;
        }
        if (check) break;
    }

    if (iter!=0 && iter2!=0) cout<<iter2-iter;
    else cout<<0;
    
    //End code

    cerr << "\nTime elapsed: " << 1000*clock() / CLOCKS_PER_SEC << "ms\n";
    
}

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

// vector<int>版

void dfs_int(int keta, int tmp, vector<int> &ans_v, set<int> set_n){
    if(set_n.size() == 0){
        ans_v.push_back(tmp);
        return;
    }else{
        for(int num : set_n){
            set<int> s_new = set_n; 
            s_new.erase(num);
            // setの各要素を
            dfs_int(keta+1, tmp*10+ num, ans_v, s_new);
        }
    }
}

int main(){
    int N; cin >> N;
    int p=0;
    int q=0;
    rep(i, N){
        int t; cin >> t;
        p = p*10+t;
    }
    rep(i, N){
        int t; cin >> t;
        q = q*10+t;
    }


    vector<int> ans_v;
    set<int> set_n;
    for(int i = 1;i<N+1;i++) set_n.insert(i);
    dfs_int(0, 0, ans_v, set_n);

    int a_v=0;
    int b_v=0;
    rep(i, ans_v.size()){
        if(ans_v.at(i) == p) a_v=i;
        if(ans_v.at(i) == q) b_v=i;
    }


    cout << abs(a_v -b_v) << endl;


    return 0;
}

Measured memory_bytes: 640
f=lambda:[*map(int,input().split())]
n,k=f()
p,c=f(),f()
p=[*map(lambda x:x-1,p)]
a=-10**9
for i in range(n):
  x=i
  l=[]
  s=0
  while 1:
    x=p[x]
    l+=[c[x]]
    s+=c[x]
    if x==i: break
  m=len(l)
  t=0
  for j in range(m):
    if j+1>k: break
    t+=l[j]
    a=max(a,t+(k-j-1)//m*s*(s>0))
print(a)

Measured memory_bytes: 84100
n,m,X,Y=[int(x) for x in input().split()]
x=[int(x) for x in input().split()]
y=[int(x) for x in input().split()]
zmax=min(Y,min(y))
zmin=max(X+1,max(x)+1)
if zmax<zmin:
    print("War")
else:
    print("No War")

Measured memory_bytes: 38384
#include<iostream>
using namespace std;
char maze[21][21];
int maz[21][21];
int w,h;
int f(int x,int y){
	if(maz[y][x]||maze[y][x]=='#')return 0;
	maz[y][x]=1;
	y<h-1&&f(x,y+1);
	0<y&&f(x,y-1);
	x<w-1&&f(x+1,y);
	0<x&&f(x-1,y);
	return 0;
}
int main(){
	int x,y;
	for(;cin>>w>>h,w;){
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>maze[i][j];
				maz[i][j]=0;
				if(maze[i][j]=='@'){y=i;x=j;}
			}
		}
		f(x,y);
		int res=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				res+=maz[i][j];
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

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

#ifdef _DEBUG
#define debug(x) cout<<#x<<": "<<x<<endl
#else
#define debug(x)
#endif

using namespace std;

int main() {
    int A, B, C, D; cin >> A >> B >> C >> D;
    if ((A + D - 1) / D >= (C + B - 1) / B) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

Measured memory_bytes: 3672
#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <map>
#include <complex>
#include <ctime>
#include <numeric>
#include <set>

using namespace std;

typedef long long ll;

typedef struct node {
    int s, e, val, pos;

    bool operator<(const node &n) const {
        if (e != n.e) return e < n.e;
        return val > n.val;
    }

    node() {};

    node(int ss, int ee, int v, int p) {
        s = ss, e = ee, val = v, pos = p;
    }
} node;

int occs[(int) 1e6 + 5], tree[1000005], n, ans[600000];

void update(int x, int val) {
    while (x <= n) {
        tree[x] += val;
        x += x & -x;
    }
}

int query(int x) {
    int ret = 0;
    while (x > 0) {
        ret += tree[x];
        x -= x & -x;
    }
    return ret;
}

int main() {

    int q, s, e;
    vector<node> v;
    scanf("%d", &n);
    scanf("%d", &q);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &s);
        v.push_back(node(i, i, s, -1));
    }
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &s, &e);
        v.push_back(node(s, e, 0, i));
    }

    sort(v.begin(), v.end());

    for (int i = 0; i < v.size(); i++) {
        if (v[i].val == 0) {
            ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
        } else {
            if (occs[v[i].val] != 0) update(occs[v[i].val], -1);
            update(v[i].s, 1);
            occs[v[i].val] = v[i].s;
        }
    }

    for (int i = 0; i < q; i++) {
        printf("%d\n", ans[i]);
    }

    return 0;
}



Measured memory_bytes: 25392
a, b = map(int, input().split())

Si = 0
Sj = 0
k = 1
flag = False
for i in range(1, 1000):
  Si += i
  for j in range(k, i):
    Sj += j
    if (Si - Sj) == (b - a):
      flag = True
      break
    k = j+1
  if flag:
    break
print(Sj - a)

Measured memory_bytes: 3064
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353
using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> E;

string s;
map<llint, llint> mp;

int main(void)
{
	cin >> s;
	llint n = s.size();
	s = "#" + s;
	
	llint sum = 0; mp[0] = 0;
	for(int i = 1; i <= n; i++){
		llint c = s[i]-'a';
		sum ^= 1<<c;
		
		llint tmp = inf;
		if(mp.count(sum)) tmp = mp[sum]+1;
		for(int j = 0; j < 26; j++){
			if(mp.count(sum^(1<<j))) tmp = min(tmp, mp[sum^(1<<j)]+1);
		}
		if(!mp.count(sum)) mp[sum] = inf;
		mp[sum] = min(mp[sum], tmp);
		if(i == n) cout << tmp << endl;
	}
	
	return 0;
}


Measured memory_bytes: 12992
#include <stdio.h>

int F[45];

int f(int n)
{ 
  if(n==0||n==1) return F[n]=1;
  if(F[n]!=-1) return F[n];

  return F[n]=f(n-2)+f(n-1);
}


int main(){

      int n,i;
  
  scanf("%d",&n);

  for(i=1;i<=n;i++)F[i]=-1;
  
  printf("%d\n",f(n));

  return 0;
  
}



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

using namespace std;

int main()
{
    int n,v,l,i,j,m,p,f,k;
    cin>>n;
    int x[3][n],a[n],b[n],c[n];
    cin>>a[0]>>b[0]>>c[0];
    for(i=1;i<n;i++)
    {
        cin>>x[0][i]>>x[1][i]>>x[2][i];
        a[i]=x[0][i]+(max(b[i-1],c[i-1]));
        b[i]=x[1][i]+(max(a[i-1],c[i-1]));
        c[i]=x[2][i]+(max(a[i-1],b[i-1]));
    }
    cout<<max(a[n-1],(max(b[n-1],c[n-1])));
}


























Measured memory_bytes: 2560
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val br = BufferedReader(InputStreamReader(System.`in`))
val out = PrintWriter(System.out)

fun main() {
    br.use { out.use {
        solve()
        out.flush()
    } }
}

fun solve() {
    val s = br.readLine()!!
    val k = readInteger()

    // dp[i][smaller][k] 上位桁からi桁目に0がK個あるときの場合の数
    val dp = Array(s.length + 1) { Array(2) { LongArray(k + 2) } }
    dp[0][0][0] = 1
// 桁dpのお気持ち
//    for (i in s.indices) {
//        val n = s[i] - '0'
//        for (d in 0..9) {
//            dp[i + 1][1] = dp[i][1]
//        }
//
//        for (d in 0 until n) {
//            dp[i + 1][1] = dp[i][0]
//        }
//
//        dp[i + 1][0] = dp[i][0]
//    }

    for (i in s.indices) {
        val n = s[i] - '0'
        for (j in 0..k) {
            dp[i + 1][1][j] += dp[i][1][j]
            dp[i + 1][1][j + 1] += dp[i][1][j] * 9

            if (n == 0) {
                dp[i + 1][0][j] += dp[i][0][j]
            } else {
                dp[i + 1][1][j] += dp[i][0][j]
                dp[i + 1][1][j + 1] += dp[i][0][j] * (n - 1)

                dp[i + 1][0][j + 1] += dp[i][0][j]
            }
        }
    }
    out.println(dp[s.length][1][k] + dp[s.length][0][k])
}

fun readInteger() = Integer.parseInt(br.readLine())
fun readLong() = java.lang.Long.parseLong(br.readLine())
fun readStringList() = br.readLine()!!.split(' ')
fun readIntegerList() = readStringList().map(Integer::parseInt)
fun readLongList() = readStringList().map(java.lang.Long::parseLong)
fun readDoubleList() = readStringList().map(java.lang.Double::parseDouble)


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

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

    vector<int> d(N);

    for (int i = 0; i < N; i++) {
        cin >> d.at(i);
    }

    sort(d.begin(), d.end());
    reverse(d.begin(), d.end());

    int ans = 1;

    for (int i = 1; i < N; i++) {
        if (d.at(i - 1) <= d.at(i))
            continue;
        ans++;
    }

    cout << ans << endl;
}

Measured memory_bytes: 256