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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
 public:
  int n;
  vector<int> a, b;
  Solver(LL n) : n(n), a(n), b(n){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> b[i];
    }
    random_device rand;
    mt19937 mt(rand());
    uniform_int_distribution<LL> dist(1, 1e7);
    LL base[2] = {dist(mt), dist(mt)}, p[2] = {MOD, MOD + 2};
    vector<vector<vector<LL>>> hash(30, vector<vector<LL>>(n, vector<LL>(2)));
    for (int i = 0; i < 30; ++i) {
      vector<int> abit;
      for (int j = 0; j < n; ++j) {
        abit.push_back((a[j] >> i) & 1);
      }
      auto ahash = get_hash(abit, base, p);
      hash[i][0] = ahash;
      vector<LL> c = {modpow(base[0], n - 1, p[0]), modpow(base[1], n - 1, p[1])};
      for (int j = 1; j < n; ++j) {
        auto tmp = hash[i][j - 1];
        for (int h = 0; h < 2; ++h) {
          tmp[h] -= abit[j - 1] * c[h];
          tmp[h] = (tmp[h] + p[h]) % p[h];
          tmp[h] = tmp[h] * base[h] % p[h];
          tmp[h] = (tmp[h] + abit[j - 1]) % p[h];
        }
        hash[i][j] = tmp;
      }
    }
    vector<vector<vector<LL>>> bhash(30, vector<vector<LL>>(2, vector<LL>(2)));
    for (int i = 0; i < 30; ++i) {
      vector<int> bbit, bnegbit;
      for (int j = 0; j < n; ++j) {
        bbit.push_back((b[j] >> i) & 1);
        bnegbit.push_back(1 - ((b[j] >> i) & 1));
      }
      bhash[i][0] = get_hash(bbit, base, p);
      bhash[i][1] = get_hash(bnegbit, base, p);
    }
    vector<LL> ans(n);
    for (int i = 0; i < 30; ++i) {
      for (int j = 0; j < n; ++j) {
        if (hash[i][j] == bhash[i][1]) {
          ans[j] += (1 << i);
        } else if (hash[i][j] == bhash[i][0]) {
        } else {
          ans[j] = -LINF;
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      if (ans[i] >= 0) {
        cout << i << " " << ans[i] << endl;
      }
    }
  }
  long long modpow(long long a, long long n, long long mod = MOD) {
    long long i = 1, ret = 1, p = a;
    while (i <= n) {
      if (i & n) ret = (ret * p) % mod;
      i = (i << 1);
      p = (p * p) % mod;
    }
    return ret;
  }
  vector<LL> get_hash(vector<int> &a, LL b[], LL p[]) {
    vector<LL> ret{0ll, 0ll};
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 2; ++j) {
        ret[j] = (ret[j] * b[j] + a[i]) % p[j];
      }
    }
    return ret;
  }
};  // namespace Problem

}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


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

using namespace std;

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

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

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

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

bool tab[60][60];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};
void dfs(int x, int y){
  if(!tab[y][x]) return;
  tab[y][x] = false;
  for(int i=0;i<8;++i)
	dfs(x+dx[i], y+dy[i]);
}

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

  while(true){
	int W, H; cin >> W >> H;
	if(W == 0) break;
	int ans = 0;
	fill((bool*)tab, (bool*)tab+60*60, false);
	REP(y,H) REP(x,W){
	  int c; cin >> c;
	  tab[y+1][x+1] = c == 1;
	}
	for(int y=1;y<=H;++y)
	  for(int x=1;x<=W;++x)
		if(tab[y][x]){
		  ++ans;
		  dfs(x,y);
		}
	cout << ans << endl;
  }
  
  return 0;
}

Measured memory_bytes: 1272
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define sqr(x) (x) * (x)

using namespace std;
using namespace __gnu_pbds;

mt19937_64 gen(time(0));

typedef tree<
pair <int, int>,
null_type,
less<pair <int, int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//const int mod = 1e9 + 7;
//const int inf = 2e9 + 6555;
const int maxn = 2e5 + 44;



int bpw (int a, int n, int mod)
{
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return (bpw(a, n - 1, mod) * a) % mod;
	else
    {
		int b = bpw(a, n / 2, mod);
		return (1ll * b * b) % mod;
	}
}

int ans[maxn];
int otv[maxn];

signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else

#endif
    int n;
    string s;
    cin >> n >> s;
    int cnt = 0;
    for(int i = 0; i < n; ++i)
        if (s[i] == '1') ++cnt;
    int ost1 = -1;
    int ost2 = -1;
    if (cnt > 1)
    {
        ost1 = 0;
        for(int i = 0; i < n; ++i)
        {
            ost1 *= 2;
            if (s[i] == '1') ost1++;
            ost1 %= cnt - 1;
        }
    }
    if (cnt < n)
    {
        ost2 = 0;
        for(int i = 0; i < n; ++i)
        {
            ost2 *= 2;
            if (s[i] == '1') ost2++;
            ost2 %= cnt + 1;
        }
    }
    for(int i = 0; i < n; ++i)
    {
        if (s[i] == '0')
        {
            ans[i] = (ost2 + bpw(2, n - i - 1, cnt + 1)) % (cnt + 1);
        }
        else
        {
            if (cnt == 1)
            {
                ans[i] = -1;
            }
            else
            {
                ans[i] = (ost1 - bpw(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
            }
        }
    }
    for(int i = 1; i < cnt + 1; ++i)
    {
        int gg = __builtin_popcount(i);
        otv[i] = otv[i % gg] + 1;
    }
    for(int i = 0; i < n; ++i)
        if (ans[i] == -1) cout << "0\n";
            else          cout << otv[ans[i]] + 1 << '\n';
    return 0;
}


Measured memory_bytes: 4920
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    N, M = map(int,input().split())
    ab = [None] * M
    for m in range(M):
        a, b = map(int,input().split())
        ab[m] = [a,b]
    ab.sort(key=lambda x:x[1])
    ret = 1
    TMP = ab[0][1]
    for x in ab[1:]:
        if TMP<=x[0]:
            TMP = x[1]
            ret += 1
    print(ret)

if __name__ == '__main__':
    main()

Measured memory_bytes: 21340
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n;
string s;
int main(){
	cin >> n >> s;
	int c = 0,add = 0;
	for(int i=0;i<n;i++){
		c += (s[i]=='(')?1:-1;
		if(c == -1){
			add++; c++;
		}
	}
	int add2 = c,nxt = 0;
	for(int i=0;i<add;i++) cout << "(";
	cout << s;
	for(int i=0;i<add2;i++) cout << ")";
	cout << endl;
}

Measured memory_bytes: 256
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq

#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

A, B, C = map(int, input().split())
ans = 0

temp = A
A = B
B = temp
temp = A
A = C
C =temp


print(A, B, C)



Measured memory_bytes: 38764
#include <stdio.h>
#include <string.h>
 
#define MAX     (1000000)
 
int main(void)
{
    char flag[MAX + 1];
    int count;
    int i, j;
    int input;
     
    memset(flag, 1, MAX);
    flag[0] = flag[1] = 0;
    for (i = 2; i <= 1001; i++) if (flag[i]) for (j = i * 2; j <= MAX; j += i) flag[j] = 0;
     
    while (scanf("%d", &input) != EOF){
        count = 0;
        for (i = 0; i <= input; i++) if (flag[i]) count++;
        printf("%d\n", count);
    }
    return (0);
}

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

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll X;
    cin >> X;
    if (X >= 30)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef std::pair<int, int> ii;
typedef tuple<int,int,int> i3;
typedef std::pair<int, ii> iii;
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
typedef std::vector<ii> vii;
typedef std::vector<iii> viii;
const int INF=10000000;
#define pb(a) push_back(a)
#define m0(x) memset(x,-1,sizeof(x))
#define print(x) cout<<x<<endl;
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);  // greater than or equal
#define ub(v,n) upper_bound(v.begin(), v.end(), n);  // greater than
#define all(x) (x).begin(), (x).end()
const int N=4005;
int n;
char color[N];
int num[N];
int white_index[N];
int black_index[N];
int memo[N][N];
int case1[N][N];
int case2[N][N];

int dp(int w, int b){
  if (w==0 and b==0) return 0;
  if (w<0 or b<0) return INF;
  if (memo[w][b]!=-1) return memo[w][b];
  //cout<<w<<" "<<b<<" | "<<case1[w][b]<<" "<<case2[w][b]<<endl;
  return memo[w][b]=min(dp(w-1,b)+case1[w][b],dp(w,b-1)+case2[w][b]);
}

int bin_search(vector<int> &v, int val){
  int ans=0;
  int l=-1; int r=v.size();
  while(r-l>1){
    int mid=(l+r)/2;
    if (v[mid]<=val) l=mid;
    else r=mid;
  }
  return max(0,l+1);
}



int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  memset(memo,-1,sizeof memo);
  memset(case1,0,sizeof case1);
  memset(case2,0,sizeof case2);
  int t;
  char s;
  cin >> n;
  
  for (int i=0; i<2*n; i++){
    cin >> color[i] >> num[i];
    if (color[i]=='B') black_index[num[i]]=i;
    if (color[i]=='W') white_index[num[i]]=i;
  }

  for (int white=0; white<=n; white++){ // for very white ball
    int index=white_index[white];  // critial region [index,2n)
    vector<int> ws; // collection of white balls
    vector<int> bs; // collection of black balls
    for (int t=index+1; t<2*n; t++){
      if (color[t]=='B') bs.push_back(num[t]);
      if (color[t]=='W') ws.push_back(num[t]);
    }

    sort(ws.begin(),ws.end());
    sort(bs.begin(),bs.end());
    
    for (int black=0; black<=n; black++){
      case1[white][black]=bin_search(ws,white)+bin_search(bs,black);
    }
    
  }

  for (int black=0; black<=n; black++){ // for very black ball
    int index=black_index[black];  // critial region [index,2n)
    vector<int> ws; // collection of white balls
    vector<int> bs; // collection of black balls
    for (int t=index+1; t<2*n; t++){
      if (color[t]=='B') bs.push_back(num[t]);
      if (color[t]=='W') ws.push_back(num[t]);
    }

    sort(ws.begin(),ws.end());
    sort(bs.begin(),bs.end());
    

    for (int white=0; white<=n; white++){
      //cerr<<white<<" | "<<bin_search(ws,white)<<" "<<temp<<endl; 
      case2[white][black]=bin_search(bs,black)+bin_search(ws,white);
    }
    
  }


  //for (auto i: a) cout<<i.first<<" "<<i.second<<endl;
// cout<<"3 1 4 1 5 9 2 6 5 3 8 9 7 2 8 4 6 7 "<<endl;
//   cout<<"W B B W B W W B W B W B W B B W W B"<<endl;
  

  cout<<dp(n,n);


/*
3 1 4 1 5 9 2 6 5 3 8 9 7 2 8 4 6 7 
W B B W B W W B W B W B W B B W W B

1 2 3 1 3 2
B W B W W B
*/

}


Measured memory_bytes: 191948
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <tuple>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cmath>

using namespace std;

int main() {
    int n, W;
    cin >> n >> W;
    vector<int64_t> w(n), v(n);
    for (int i = 0; i < n; i++) {
        cin >> w[i] >> v[i];
    }

    vector<vector<int64_t>> dp(n, vector<int64_t>(W+1));
    // dp[i][j] : i 番目までの品物を，重さが j 以下になるように入れたときの価値の最大値
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= W; j++) {
            if (i == 0) {
                if (w[0] <= j) dp[0][j] = v[0];
            } else {
                // i 番目の品物を入れるとき
                if (j-w[i] >= 0) {
                    dp[i][j] = dp[i-1][j-w[i]]+v[i];
                }
                // i 番目の品物を入れないとき
                dp[i][j] = max(dp[i][j], dp[i-1][j]);
            }
        }
    }

    cout << dp[n-1][W] << endl;
}


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

int main() {
  int n;
  std::cin >> n;
  std::vector<int> S;
  for (int i = 0; i < n; ++i) {
    int s;
    std::cin >> s;
    S.push_back(s);
  }
  int q;
  std::cin >> q;
  int cnt = 0;
  for (int i = 0; i < q; ++i) {
    int t;
    std::cin >> t;
    for (int s : S) {
      if (s == t) {
        ++cnt;
        break;
      } 
    }
  }
  std::cout << cnt << std::endl;
}

Measured memory_bytes: 3108
c=[]
for i in range(3):
    c.append(list(map(int,input().split())))
a=[0,c[1][0]-c[0][0],c[2][0]-c[0][0]]
b=[c[0][0],c[0][1],c[0][2]]

ok=0
for i in range(3):
    for j in range(3):
        if c[i][j] != a[i]+b[j]:
            ok=1
            break
print("Yes" if ok==0 else "No")

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
int main(void){

 int A , B , C , D;
 cin >> A >> B >> C >> D;
 int train = (A < B) ? A : B;
 int bus = (C < D) ? C : D;

 printf("%d", train + bus);


return 0;
}

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

int main(){
    int n, v, j;
    cin >> n;
    int num[n];
    for(int i = 0; i < n; i++ ){
        cin >> num[i];
        if(i < n-1) cout << num[i] << " ";
        else cout << num[i] << endl;
    }
    
    for(int i = 1; i < n; i++){
        v = num[i];
        j = i - 1;
        while(j >= 0 && num[j] > v){
            num[j+1] = num[j];
            j--;
        }
        num[j+1] = v;
        cout << num[0];
        for(int h = 1; h < n; h++){
            cout << " " << num[h];
        }
        cout << endl;
    }
    
    return 0;
}


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

using namespace std;

int main()
{
	cin.sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	vector<int>a(N),b(N),c(M),d(M);
	vector<int>dis(M);
	for (int i = 0;i < N;i++) {
		cin >> a[i] >> b[i];
	}
	for (int i = 0;i < M;i++) {
		cin >> c[i] >> d[i];
	}
	for (int i = 0;i < N;i++) {
		for (int j = 0;j < M;j++) {
			dis[j]=abs(a[i] - c[j]) + abs(b[i] - d[j]);
		}
		sort(dis.begin(), dis.end());
		int count = 0;
		int distance;
		while (1) {
			distance = abs(a[i] - c[count]) + abs(b[i] - d[count]);
			if (dis.front() == distance) {
				cout << count + 1 << endl;
				break;
			}
			count++;
		}

	}

}

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

#define rep(i,a,n) for (int i = a;i<=n;++i)
#define per(i,a,n) for (int i = n;i>=a;--i)
#define all(x) (x).begin(), (x).end()     
#define SZ(x) ((int)x.size())

typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

mt19937 mrand(random_device{}());
ll rnd(ll a, ll b) { return a + mrand() % (b - a + 1); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int N = 4500;
ll cnt[N][3], n;
string s;
map<int, int> code;

int main() {
      ios::sync_with_stdio(false); cin.tie(0);
#ifdef arrias
      freopen("a.txt", "r", stdin);
#endif
      code['R'] = 0;
      code['G'] = 1;
      code['B'] = 2;
      cin >> n >> s;
      s = '#' + s;
      rep(i, 1, n) {
            cnt[i][code[s[i]]]++;
            rep(v, 0, 2) cnt[i][v] += cnt[i - 1][v];
      }
      ll res = 0;
      rep(i, 1, n) {
            rep(j, i + 1, n) {
                  if (s[i] == s[j]) continue;
                  int ost = 3 - code[s[i]] - code[s[j]];
                  res += cnt[i - 1][ost];
                  int delta = j - i;
                  if (i - delta > 0 && code[s[i - delta]] == ost) res--;
            }
      }
      cout << res << "\n";
      return 0;
}

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

int a[100005];
int grundy[100005];
int main() {
  int n,k;
  cin >> n >> k;

  for(int i = 0; i < n; i++) cin >> a[i];
  
  for(int i = a[0]; i <= k; i++) {
    set<int> s;
    for(int j = 0; j < n; j++) {
      if(a[j] > i) break;
      s.insert(grundy[i-a[j]]);
    }
    int res = 0;
    while(s.count(res) == 1) res++;
    grundy[i] = res;
  }

  if(grundy[k]) cout << "First" << endl;
  else cout << "Second" << endl;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define N 1000000007
typedef long long ll;
#define all(v) (v).begin(),(v).end()
#define sz(x) int(x.size())
int main(){
	string s;
	cin>>s;
	int f=1;
	rep(i,sz(s)){
		if(i%2==1){
			if(s.at(i)=='L'||s.at(i)=='U'||s.at(i)=='D'){
				continue;
			}else{
				f=0;
			}
		}else{
			if(s.at(i)=='R'||s.at(i)=='U'||s.at(i)=='D'){
				continue;
			}else{
				f=0;
			}
		}
	}
	if(f==1){
		cout<<"Yes"<<endl;
	}else{
		cout<<"No"<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
"""
【01ナップザック問題】
重さと価値がそれぞれw_i,v_iであるようなn個の品物がある.
これらの品物の中から, 重さの総和がWを超えないように選んだ時の,
価値の総和の最大値を求めよ.
1<=n<=100, 1<=w_i,v_i<=100, 1<=W<=10000
"""
#------------------------------------------------------------------------------

#入力,準備
n,W= map(int,input().split())
w=[0]*n
v=[0]*n
for i in range(n):
    w[i],v[i]=map(int,input().split())
dp=[0]*(W+1)

#メイン
#dp[j] : 重さの総和がjを超えないように選んだ時の価値の総和の最大値
for i in range(n):
    j=W
    while j>=w[i]:
        dp[j]=max(dp[j],dp[j-w[i]]+v[i])
        j-=1

print(dp[W])

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

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  std::vector<int> P(N);
  std::vector<int> Q(N);
  int p, q;
  for (int i = 0; i < N; i++) A[i] = i + 1;
  for (int i = 0; i < N; i++) std::cin >> P[i];
  for (int i = 0; i < N; i++) std::cin >> Q[i];

  int i{1};
  do {
    if (A == P) p = i;
    if (A == Q) q = i;
  } while (++i, std::next_permutation(A.begin(), A.end()));

  printf("%d\n", std::abs(q - p));
}

Measured memory_bytes: 256
#include <iostream>
#include <limits.h>
#include <algorithm>
using namespace std;

int n, a[100010], b[100010], c[100010], dp[3][100010];

int solve(int j, int l) {
    if (dp[j][l] > 0) return dp[j][l];
    
    if (j == 0) dp[j][l] = max(solve(1, l - 1), solve(2, l - 1)) + a[l];
    if (j == 1) dp[j][l] = max(solve(2, l - 1), solve(0, l - 1)) + b[l];
    if (j == 2) dp[j][l] = max(solve(0, l - 1), solve(1, l - 1)) + c[l];
    
    return dp[j][l];
}


int main(void){
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i] >> c[i];
        for (int j = 0; j < 2; j++) dp[j][i] = 0;
    }
    dp[0][0] = a[0], dp[1][0] = b[0], dp[2][0] = c[0];
    
    cout << max(max(solve(0, n - 1), solve(1, n - 1)), solve(2, n - 1)) << endl;
    
    
}


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

#define MAX 600002

int n;
int m;

vector<pair<int, int> > v[MAX];
vector<int> id[MAX];
vector<int> nex[MAX];

int dist[MAX ];

priority_queue<pair<int, int> ,vector<pair<int,int> > ,greater<pair<int,int> > > q;

int main(){
	cin >> n >> m;
	for (int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		a--;
		b--;
		v[a].push_back(make_pair(b, c));
		v[b].push_back(make_pair(a, c));
		id[a].emplace_back(c);
		id[b].emplace_back(c);
	}
	for (int i = 0; i < n; i++){
		sort(id[i].begin(), id[i].end());
		id[i].erase(unique(id[i].begin(), id[i].end()), id[i].end());
	}
	int siz = n;
	for (int i = 0; i < n; i++){
		int sizz = id[i].size();
		for (int j = 0; j < sizz; j++){
			nex[i].emplace_back(siz);
			siz++;
		}
	}
	for (int i = 0; i < siz; i++){
		dist[i] = INT_MAX;
	}
	for (int i = 0; i < n; i++){
		for (int j = 0; j < v[i].size(); j++){
			int oc = v[i][j].second;
			int ii = lower_bound(id[i].begin(), id[i].end(), oc) - id[i].begin();
			int iii = lower_bound(id[v[i][j].first].begin(), id[v[i][j].first].end(), oc) - id[v[i][j].first].begin();
			v[nex[i][ii]].emplace_back(make_pair(nex[v[i][j].first][iii],0) );
		}
		for (int j = 0; j < id[i].size(); j++){
			v[nex[i][j]].emplace_back(make_pair(i,0));
		}
		v[i].clear();
		for (int j = 0; j < id[i].size(); j++){
			v[i].push_back(make_pair(nex[i][j], 1));
		}
	}
	q.push(make_pair(0, 0));
	dist[0] = 0;
	while (!q.empty()){
		int b = q.top().second;
		if (dist[b] != q.top().first){
			q.pop();
			continue;
		}
		q.pop();
		int sz = v[b].size();
		for (int i = 0; i < sz; i++){
			int &go = v[b][i].first;
			int cost = dist[b] + v[b][i].second;
			if (dist[go]>cost){
				dist[go] = cost;
				q.push(make_pair(cost, go));
			}
		}
	}
	int ans = dist[n - 1];
	for (int i = 0; i < id[n - 1].size(); i++){
		ans = min(ans, dist[nex[n - 1][i]]);
	}
	if (ans == INT_MAX){
		puts("-1");
		return 0;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 72180
#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define outval(x) cerr<<#x" = "<<x<<endl
#define outv(x) cerr<<#x" = "<<x<<"  "
#define outtag(x) cerr<<"--------------"#x"---------------"<<endl
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
	For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
#define User_Time ((double)clock()/CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector <int> vi;
typedef pair <int,int> pii;
LL read(){
	LL x=0,f=0;
	char ch=getchar();
	while (!isdigit(ch))
		f=ch=='-',ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int mod=998244353;
int Pow(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(LL)x*x%mod)
		if (y&1)
			ans=(LL)ans*x%mod;
	return ans;
}
void Add(int &x,int y){
	if ((x+=y)>=mod)
		x-=mod;
}
void Del(int &x,int y){
	if ((x-=y)<0)
		x+=mod;
}
int Add(int x){
	return x>=mod?x-mod:x;
}
int Del(int x){
	return x<0?x+mod:x;
}
void ckmax(int &x,int y){
	if (x<y)
		x=y;
}
void ckmin(int &x,int y){
	if (x>y)
		x=y;
}
const int N=305;
int n;
char s[N];
int dp[N][N][N],g[N][N][N],f[N][N][N];
int main(){
	cin>>(s+1);
	n=strlen(s+1);
	dp[0][0][0]=1;
	For(a,0,n)
		Fod(i,n,0)
			Fod(j,n,0){
				if (dp[a][i][j]){
					f[a][i][j]=1;
					if (a<=n-2){
						f[a+1][i][j]=1;
					}
					if (i>0){
						if (i+j>=2)
							dp[a][i-1][j]=1;
						if (a<n){
							dp[a+1][i][j]=1;
							if (s[a+1]=='0')
								dp[a+1][i-1+1][j]=1;
							else
								dp[a+1][i-1][j+1]=1;
						}
					}
					if (j>0){
						if (i+j>=2)
							dp[a][i][j-1]=1;
						if (a<n){
							dp[a+1][i][j]=1;
							if (s[a+1]=='0')
								dp[a+1][i+1][j-1]=1;
							else 
								dp[a+1][i][j-1+1]=1;
						}
					}
					if (a<=n-2){
						For(k,a+1,a+2){
							if (s[k]=='0')
								dp[a+2][i+1][j]=1;
							else
								dp[a+2][i][j+1]=1;
						}
					}
				}
				if (f[a][i][j]){
					if (a<n){
						if (i>0)
							f[a][i-1][j]=1;
						if (j>0)
							f[a][i][j-1]=1;
					}
				}
			}
	g[n+1][0][0]=1;
	int ans=1;
	Fod(b,n,1){
		For(i,0,n)
			For(j,0,n)
				g[b][i][j]=g[b+1][i][j];
		if (s[b]=='1'){
			For(i,1,n)
				For(j,0,n)
					Add(g[b][i][j],g[b][i-1][j]);
		}
		else {
			For(i,0,n)
				For(j,1,n)
					Add(g[b][i][j],g[b][i][j-1]);
		}
		For(i,0,n)
			For(j,0,n){
				if (i+j>b+1)
					continue;
				if (b<n){
					if (s[b+1]=='0')
						f[b][i][j]|=f[b+1][i+1][j];
					else
						f[b][i][j]|=f[b+1][i][j+1];
				}
				if (f[b][i][j])
					Add(ans,g[b][i][j]);
			}
	}
	cout<<ans<<endl;
	return 0;
}
/*
001
001 0 1 01 10
*/

Measured memory_bytes: 195512
import java.io.PrintWriter;
import java.util.Scanner;

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

		int h = Integer.parseInt(scan.next());
		int w = Integer.parseInt(scan.next());
		int a = Integer.parseInt(scan.next());
		int b = Integer.parseInt(scan.next());

		String x = "";
		String y = "";
		PrintWriter out = new PrintWriter(System.out);
		for (int i = 0; i < w; i++) {
			if (i < a) {
				x = x.concat("0");
				y = y.concat("1");
			} else {
				x = x.concat("1");
				y = y.concat("0");
			}
		}
		for (int i = 0; i < h; i++) {
			if (i < b) {
				out.println(x);
			} else {
				out.println(y);
			}
		}
		out.flush();
	}
}

Measured memory_bytes: 25812
import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();
        String s=sc.nextLine();
        int i=-1,cntr=0;
        while(true)
        {
            i=s.indexOf("ABC",i+1);
            if(i==-1)
            break;
            cntr++;
        }
        System.out.println(cntr);
    }
}


Measured memory_bytes: 23892
#include<bits/stdc++.h>
using namespace std;
int main(){
  long N,K;cin>>N>>K;
  long R,S,P;cin>>R>>S>>P;
  string T;cin>>T;
  
 
  vector<string>st(K,"");
  for(int i=0;i<N;i++){
    st[i%K]+=T[i];
  }
  long ans=0;
  for(int i=0;i<K;i++){
    char last='x';
    for(int j=0;j<st[i].length();j++){
      if(st[i][j]=='r' && last!='r')ans+=P;
      if(st[i][j]=='s' && last!='s')ans+=R;
      if(st[i][j]=='p' && last!='p')ans+=S;
      if(last!= st[i][j])last=st[i][j];
      else last='x';
    }
  }
  cout<<ans;
  
}



Measured memory_bytes: 5120
#include <stdio.h>
#include <stdlib.h>

int compare_int(const void *b,const void *a){
  if(*(long long*)a-*(long long*)b > 0)
    return 1;
  return -1;
}

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

int main(void){
  int N,M;
  scanf("%d%d",&N,&M);

  long long x[N],y[N],z[N];
  long long sum[N];
  for(int i = 0;i < N;i++){
    scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);
  }

  long long array[N];
  long long ans = 0;
  for(int i = 0;i < 1<<3;i++){
    int sign[3];
    for(int j = 0;j < 3;j++){
      sign[j] = (i>>j)&1==1 ? 1:-1;
    }
    for(int j = 0;j < N;j++){
      array[j] = x[j]*sign[0]+y[j]*sign[1]+z[j]*sign[2];
    }
    qsort(array,N,sizeof(long long),compare_int);
    long long res = 0;
    for(int j = 0;j < M;j++){
      res += array[j];
    }
    ans = max(ans,res);
  }

  printf("%lld\n",ans);

  return 0;
}

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

int main() {
    int A, B, M;
    cin >> A >> B >> M;
    vector<int> a(A), b(B);
    rep(i, A) cin >> a.at(i);
    rep(i, B) cin >> b.at(i);
    int minA = *min_element(a.begin(), a.end());
    int minB = *min_element(b.begin(), b.end());
    int ans = minA + minB;

    rep(i, M) {
        int x, y, c;
        cin >> x >> y >> c;
        x--;
        y--;
        ans  = min(ans, a[x] + b[y] - c);
    }

    cout << ans << endl;
}


Measured memory_bytes: 1024
# import bisect
# from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    N,K=map(int,input().split())
    X=list(map(int,input().split()))
    mintime=10**10
    for i in range(N-K+1):
        l=X[i]
        r=X[i+K-1]
        mintime=min(min(abs(l)+r-l,abs(r)+r-l),mintime)
    print(mintime)

resolve()

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

#define mkpr make_pair

using namespace std;

typedef pair<int, int> pii;

const int MAXN = 100, MOD = 1e9 + 7;
int n, h[MAXN + 5];

inline int qkpow ( int a, int b, const int p = MOD ) {
	int ret = 1;
	for ( ; b; a = 1ll * a * a % p, b >>= 1 ) ret = 1ll * ret * ( b & 1 ? a : 1 ) % p;
	return ret;
}

inline pii solve ( const int l, const int r, const int cut ) {
	if ( l > r ) return mkpr ( 1, 0 );
	int height = 0x3f3f3f3f;
	for ( int i = l; i <= r; ++ i ) height = height < h[i] ? height : h[i];
	vector<int> lowpos; lowpos.push_back ( l - 1 );
	for ( int i = l; i <= r; ++ i ) if ( height == h[i] ) lowpos.push_back ( i );
	lowpos.push_back ( r + 1 );
	int lowcnt = ( int ) lowpos.size () - 2, prdfirst = 1, prdboth = 1;
	for ( int i = 0; i < ( int ) lowpos.size () - 1; ++ i ) {
		pii tmp ( solve ( lowpos[i] + 1, lowpos[i + 1] - 1, height ) );
		prdfirst = 1ll * prdfirst * tmp.first % MOD;
		prdboth = 1ll * prdboth * ( tmp.first + tmp.second ) % MOD;
	}
	int pwr = qkpow ( 2, height -= cut );
	return lowpos.clear (),
		   mkpr ( 1ll * pwr * prdfirst % MOD, ( 1ll * qkpow ( 2, lowcnt ) * prdboth % MOD + ( pwr - 2ll ) * prdfirst % MOD ) % MOD );
}

int main () {
	ios :: sync_with_stdio ( false );
	cin >> n;
	for ( int i = 1; i <= n; ++ i ) cin >> h[i];
	cout << ( solve ( 1, n, 0 ).second + MOD ) % MOD << endl;
	return 0;
}

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

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    bool reach = false;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == 'o') {
            reach = false;
        } else if (reach) {
            cout << cnt << endl;
            return 0;
        } else {
            reach = true;
        }
        cnt++;
    }
    cout << cnt << endl;
    return 0;
}

Measured memory_bytes: 3324
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto X = RD;
	auto A = RD;

	writeln(X < A ? 0 : 10);
	stdout.flush();
	debug readln();
}

Measured memory_bytes: 256
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ld long double
#define al(a) (a).begin(),(a).end()
#define mk make_pair
#define check cout<<"?"<<endl;

ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;

int main() {
    int n,ans=0,cnt; string s;
    cin>>n>>s;
    rep(i,n-1){
        cnt=0;
        rep(j,n-1-i){
            if(s[j]==s[j+1+i]){
                if(i+1>cnt){
                cnt++;
                ans=max(ans,cnt);
                }
            }
            else{
                cnt=0;
            }
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 384
def make_divisors(n):
    divisors=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            divisors.append(i)
            if i!=n//i:
                divisors.append(n//i)
    divisors.sort()
    return divisors
def judge(d):
    R=[]
    for a in A:
        R.append(a%d)
    R.sort()
    R_rev=[]
    for r in R:
        R_rev.append(d-r)
    R_acc=[0]*(n+1); R_rev_acc=[0]*(n+1)
    for i in range(n):
        R_acc[i+1]=R_acc[i]+R[i]
    for i in reversed(range(n)):
        R_rev_acc[i]=R_rev_acc[i+1]+R_rev[i]
    tmp=float('inf')
    for i in range(n+1):
        tmp=min(tmp,max(R_acc[i],R_rev_acc[i]))
    return tmp<=k

n,k=map(int,input().split())
A=tuple(map(int,input().split()))
s=sum(A)
D=make_divisors(s)
ans=1
for d in D:
    if judge(d):
        ans=d
print(ans)

Measured memory_bytes: 43612
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define FOR(e, c) for (auto &e : c)
#define SORT(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define rvisort(v) sort(v.begin(), v.end(), greater<int>());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
 
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vs = vector<string>;
using vd = vector<double>;
using ul = unsigned long;
 
template<class T, class C> void chmax(T& a, C b){ a>b?:a=b; }
template<class T, class C> void chmin(T& a, C b){ a<b?:a=b; }

int main()
{
  cin.tie( 0 );
  ios::sync_with_stdio( false );
  
  string s;cin>>s;
  reverse(s.begin(),s.end());
  // cout(s);
  int ans=0;
  string before="";
  int n=s.size();
  int i=0;
  while(i<n){
    string now=s.substr(i,1);
    if(i==n-1){
      if(now!=before)ans++;
      break;
    }
    if(before!=now){
      before=now;
      i++;
      ans++;
      continue;
    }
    now=s.substr(i,2);
    if(before!=now){
      before=now;
      i+=2;
      ans++;
      continue;
    }
  }
  cout(ans);
  return 0;
}

Measured memory_bytes: 720
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
#define MAX_N 300005
#define MOD 1000000007

//どこかでmake()することを忘れずに
ll inv[MAX_N],fac[MAX_N],finv[MAX_N];

void make()
{
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i=2;i<MAX_N;i++){
		inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
		fac[i] = fac[i-1] * (ll) i % MOD;
		finv[i] = finv[i-1] * inv[i] % MOD;
	}
}

ll comb(ll a,ll b)
{
    if(b==0)return 1;
    if(b<0)return 0;
	if(a<b){
		return 0;
	}
	return fac[a] * (finv[b] * finv[a-b] % MOD) % MOD;
}

int main(){
    ll mod = MOD;
    make();
    ll n,m,k;
    cin >> n >> m >> k;
    ll ans = 0;
    for(ll i=0;i<=k;i++){
        ll x = i;
        ll y = k-i;
        ll tmp = comb(n+2*x+m+2*y,n+2*x);
        tmp *= (mod+comb(n+x+x,x)-comb(n+x+x,x-1))%mod;
        tmp %= mod;
        tmp *= (mod+comb(m+y+y,y)-comb(m+y+y,y-1))%mod;
        tmp %= mod;
        ans += tmp;
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}


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

int main() {
  int D;
  cin >> D;
  if(D == 25) {
    cout << "Christmas" << endl;
  }
  else if(D == 24) {
    cout << "Christmas Eve" << endl;
  }
  else if(D == 23) {
    cout << "Christmas Eve Eve" << endl;
  }
  else if(D == 22) {
    cout << "Christmas Eve Eve Eve" << endl;
  }
}

Measured memory_bytes: 256
import math
import sys
n=int(input())
k=int(input())
s=str(n)
l=len(s)
dp0=[[0 for j in range(k+1)] for i in range(l)]
dp1=[[0 for j in range(k+1)] for i in range(l)]

x=int(s[0])
dp0[0][1]=x-1
dp0[0][0]=1
dp1[0][1]=1
for i in range(1,l):
    x=int(s[i])
    dp0[i][0]=1

    for j in range(1,k+1):
        dp0[i][j]+=dp0[i-1][j-1]*9+dp0[i-1][j]
        if x>0:
            dp0[i][j]+=dp1[i-1][j-1]*(x-1)+dp1[i-1][j]
            dp1[i][j]=dp1[i-1][j-1]
        else:
            dp1[i][j]=dp1[i-1][j]

print(dp0[l-1][k]+dp1[l-1][k])


Measured memory_bytes: 3064
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in), 1);
		String s;
		String sl[];

		s = r.readLine();
		sl = s.split(" ");

//		//int n = Integer.parseInt(sl[0]);
//		//long k = Long.parseLong(sl[1]);
//
//		long v[] = new long[200];
//
//		s = r.readLine();
//		sl = s.split(" ");
//		for (int i = 0; i < n; i++) {
//			v[i] = Long.parseLong(sl[0]);
//		}

		System.out.println(s.substring(0, 3));
	}
}

Measured memory_bytes: 32420
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

int main() {
  uint N = 0;
  std::cin >> N;
  std::set<uint> s = {1, 2, 3, 5, 6, 9, 10, 13, 17};

  std::cout << (s.find(N) == s.end() ? "Yes" : "No") << std::endl;

  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define endl '\n'
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int n,memo[100001];
vector<int>vec;
int solve(int idx){
    if(idx==n-1)
        return 0;
    int &cur=memo[idx];
    if(cur!=-1)
        return cur;
    cur=2e9;
    if(idx<n-2){
        cur=min(cur,abs(vec[idx]-vec[idx+1])+solve(idx+1));
        cur=min(cur,abs(vec[idx]-vec[idx+2])+solve(idx+2));
    }
    else{
        cur=min(cur,abs(vec[idx]-vec[idx+1])+solve(idx+1));
    }
    return cur;
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    memset(memo,-1,sizeof(memo));
    cin>>n;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        vec.push_back(a);
    }
    cout<<solve(0);
}

Measured memory_bytes: 5880
/*  -*- coding: utf-8 -*-
 *
 * b.cc: B - Distance
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

/* typedef */

typedef long long ll;

/* global variables */

/* subroutines */

/* main */

int main() {
  int n, d;
  scanf("%d%d", &n, &d);
  ll dd = (ll)d * d;

  int c = 0;
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);

    if ((ll)x * x + (ll)y * y <= dd) c++;
  }

  printf("%d\n", c);
  return 0;
}


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

using namespace std;

int main() {

	string s;

	cin >> s;

	if (s[2] == s[3] && s[4] == s[5]) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}

	return 0;
}

Measured memory_bytes: 256
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

class Seg():
    def __init__(self, na, default, func):
        if isinstance(na, list):
            n = len(na)
        else:
            n = na
        i = 1
        while 2**i <= n:
            i += 1
        self.D = default
        self.H = i
        self.N = 2**i
        if isinstance(na, list):
            self.A = [default] * (self.N) + na + [default] * (self.N-n)
            for i in range(self.N-1,0,-1):
                self.A[i] = func(self.A[i*2], self.A[i*2+1])
        else:
            self.A = [default] * (self.N*2)
        self.F = func

    def find(self, i):
        return self.A[i + self.N]

    def update(self, i, x):
        i += self.N
        self.A[i] = x
        while i > 0:
            i = (i-1) // 2
            self.A[i] = self.merge(self.A[i*2], self.A[i*2+1])

    def merge(self, a, b):
        return self.F(a, b)

    def total(self):
        return self.A[0]

    def query(self, a, b):
        A = self.A
        l = a + self.N
        r = b + self.N
        res = self.D
        while l < r:
            if l % 2 == 1:
                res = self.merge(res, A[l])
                l += 1
            if r % 2 == 1:
                r -= 1
                res = self.merge(res, A[r])
            l >>= 1
            r >>= 1

        return res

def main():
    n = I()
    a = LI()

    def sf(a,b):
        if a < b:
            return a
        return b

    s1 = Seg([c if i%2==0 else inf for c,i in zip(a,range(n))],inf,sf)
    s2 = Seg([c if i%2==1 else inf for c,i in zip(a,range(n))],inf,sf)
    d = {}

    for i in range(n):
        d[a[i]] = i

    def f(l,r):
        if l % 2 == 0:
            t = s1.query(l,r)
            ti = d[t]
            u = s2.query(ti+1,r)
        else:
            t = s2.query(l,r)
            ti = d[t]
            u = s1.query(ti+1,r)
        ui = d[u]
        nl = []
        if l < ti:
            nl.append((l, ti))
        if ui - ti > 1:
            nl.append((ti+1, ui))
        if ui < r-1:
            nl.append((ui+1, r))
        return ((t,u), nl)

    q = []
    heapq.heappush(q, f(0,n))
    r = []
    while q:
        t,nl = heapq.heappop(q)
        r.append(t[0])
        r.append(t[1])
        for t,u in nl:
            heapq.heappush(q, f(t,u))

    return ' '.join(map(str,r))

print(main())





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

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

    int res = 1;
    int D = 0;
    for (int i=0; i<N; i++) {
        int L;
        cin >> L;
        D += L;
        if (D <= X) {
            res++;
        } else {
            break;
        }
    }

    cout << res << "\n";
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
const ll mod = 1000000007;

ll modpow(ll b, ll e) {
  if(e == 0) return 1LL;
  if(e % 2 == 1) return modpow(b, e - 1) * b % mod;
  ll m = modpow(b, e >> 1);
  return m * m % mod;
}

ll inv(ll a) {
  return modpow(a, mod - 2);
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> fact;
  fact.push_back(1LL);
  for(ll i = 1; i <= N; i++)
    fact.push_back(i * fact[i - 1] % mod);
  K = min(N - 1, K);
  ll ans = 0LL;
  for(ll k = 0; k <= K; k++) {
    ll comb0 = fact[N] * inv(fact[k]) % mod;
    comb0 = comb0 * inv(fact[N - k]) % mod;

    ll comb1 = fact[N - 1] * inv(fact[k]) % mod;
    comb1 = comb1 * inv(fact[N - k - 1]) % mod;
    ll now = comb0 * comb1 % mod;
    ans = (ans + now) % mod;
  }
  cout << ans << endl;
  return 0;
}

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

int main(){
  int n;
  cin >> n;
  vector<int> v(n);
  for(int i=0;i<n;++i) cin >> v[i];
  int out=0;
  while(1){
    for(int i=0;i<n;++i){
      if(v[i]%2==1 or v[i]==0) goto label;
      else {
        v[i]/=2;
      }
    }
    out++;
  }
  label:
  cout << out << endl;
}

Measured memory_bytes: 256
using System;
using System.Linq;
class p
{
  static void Main()
  {
    int N = int.Parse(Console.ReadLine());
    int[] arr = new int[N];
    for (int i = 0; i < N; i++) {
      arr[i] = int.Parse(Console.ReadLine());
    }
    Console.WriteLine(arr.Distinct().Count());
  }
}

Measured memory_bytes: 13152
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma comment(linker, "/STACK:2000000")

#include "bits/stdc++.h"

using namespace std;

#define pb push_back
#define F first
#define S second
#define f(i,a,b)  for(int i = a; i < b; i++)
// #define endl '\n'
using ll = long long;
#define int ll
using db = long double;
using ii = pair<int, int>;

const int N = 2e5 + 5, LG = 19, MOD = 1e9+7;
const int SQ =320;
const long double EPS = 1e-7;
vector<int> adj[100005];
int n, p, k, ans = 0;
int dfs(int node){
  int mx = 0;
  for(auto child : adj[node]){
    int x = dfs(child) + 1;
    if(x == k + (node == 1)){
      ans++;
    } else mx=max(mx,x);
  }
  return mx;
}
int32_t main(){
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif

  cin >> n >> k;

  f(i,1,n+1){
    cin >> p;
    if(i==1)ans+=p!=1;
    else adj[p].pb(i);
  }

  dfs(1);
  cout<<ans<<'\n';

  return 0;
}


Measured memory_bytes: 9216
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdint.h>
#include<vector>
#include<deque>
#include<stack>
#include<string>
#include<cstring>
#include<time.h>
#include<array>
#include<iomanip>
#include<list>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<bitset>
#include <queue>
#include <map>


using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;


//BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney,chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu, topologicalsort, kyourenketuseibun


#define PRI(s) cout << s << endl
#define PRIY PRI("Yes")
#define PRIN PRI("No")

int ctoi(char c) {
	switch (c)
	{
	case '0': return 0;
	case '1': return 1;
	case '2': return 2;
	case '3': return 3;
	case '4': return 4;
	case '5': return 5;
	case '6': return 6;
	case '7': return 7;
	case '8': return 8;
	case '9': return 9;
	default:
		cout << "ERR_ctoi" << endl;
		return -1;
	}
}
//最大公約数
template<typename T>
constexpr T gcd(T a, T b)
{
	if (a % b == 0)
	{
		return(b);
	}
	else
	{
		return(gcd(b, a % b));
	}
}

//最小公倍数
template<typename T>
constexpr T lcm(T a, T b)
{
	return a * b / gcd(a, b);
}
int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a + b < c) {
		PRIN;
	}
	else PRIY;
	return 0;
}

Measured memory_bytes: 3600