akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i,a,b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i,a,b) for (ll i = (a); i <= (ll)(b); ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } }

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using Tuple = tuple<int, int, int>;


int main() {
    //ll n,a,b; cin >> n >> a >> b;
    int n; cin >> n;
    vector<int> a(n);
    rep(i, n) {
        cin >> a[i];
        a[i]--;
    }
    set<int> st;
    int now = 0;
    int ans = 0;
    st.insert(0);
    while(true) {
        now = a[now];
        ans++;
        if(now == 1) {
            cout << ans << endl;
            return 0;
        }
        if(st.find(now) != st.end()) {
            cout << -1 << endl;
            return 0;
        }
        st.insert(now);
    }
}


Measured memory_bytes: 5376
N = int(input())
p = list(map(int, input().split()))

ans = 0
next = False
for i in range(N):
    
    if p[i] == i+1 and not next:
        ans += 1
        if i+1 < N and p[i+1] == i+2:
            next = True
            continue
    if next:
        next = False

print(ans)

Measured memory_bytes: 14008
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define long long long
#define LF '\n'
using namespace std;
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr int INF = 0x3f3f3f3f;


signed main()
{
  cin.tie(nullptr), ios::sync_with_stdio(false);

  int n , q;
  int a[1005];

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

  cin >> q;
  while(q--) {
    int com, s, t;
    cin >> com >> s >> t;
    if (com == 0) {
      cout << *min_element(a+s, a+t) << LF;
    } else {
      cout << *max_element(a+s, a+t) << LF;
    }
  }

  return 0;
}




Measured memory_bytes: 3200
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    if(a==b){cout<<"Draw";}
    else if(a==1 ){cout<<"Alice";}
    else if(b==1 ){cout<<"Bob";}
    else if(a>b ){cout<<"Alice";}
    else if(a<b ){cout<<"Bob";}
}

Measured memory_bytes: 3616
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<queue>
#include<list>
using namespace std;

int main(){
    int a, b, c;
    cin>>a>>b;
    c=a+b;
    if(c>=24) c-=24;
    cout<<c<<endl;
}


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

int main() {
  int N, C;
  cin >> N >> C;
  vector<vector<int>> D(C, vector<int>(C));
  vector<vector<int>> c(3, vector<int>(C));
  for (int i = 0; i < C; i++) {
    for (int j = 0; j < C; j++) {
      cin >> D[i][j];
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      int tmp;
      cin >> tmp;
      tmp--;
      c[(i + j) % 3][tmp] += 1;
    }
  }

  int ans = 1000 * 500 * 500;
  for (int x = 0; x < C; x++) {
    for (int y = 0; y < C; y++) {
      for (int z = 0; z < C; z++) {
        if (x == y || y == z || z == x) continue;
        int cost = 0;
        for (int i = 0; i < C; i++) {
          cost += c[0][i] * D[i][x];
          cost += c[1][i] * D[i][y];
          cost += c[2][i] * D[i][z];
        }
        ans = min(ans, cost);
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
# 単純な組み合わせだと計算量がO(n**2)となってしまうので工夫が必要
# 小さい順にK番目の値を求めよ
# ⇄ 判定条件「x以下のものがK個以上ある」を満たす最小のxを求めよ
# ⇄ ・判定条件を満たす最小のx：2分探索 (-10**18 ~ 10**18)
# 　 ・「数列から２個選んだ積がx以下のものが何個あるかを数えて、K個以下か判定せよ」：片方を固定して2分探索で数えていく
import sys
input = sys.stdin.readline

N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A = sorted(A)
ok, ng = 10**18, -10**18

# 判定条件を満たす最小のx：2分探索 (-10**18 ~ 10**18)
while ok - ng > 1:
    mid = (ok + ng) // 2
    ### ここから判定ロジック ###
    # 数列から２個選んだ積がx以下のものが何個あるかを数える
    s, t = 0, 0
    for i in range(N):
        if A[i] > 0:
            # 固定したA[i]が正の場合、積の数列
            # A[i]*A[0], A[i]*A[1], ... , A[i]*A[n-1]
            # つまり、{A[i]*A[j]} j=0~n-1 という数列は単調増加
            # j=JOKの時が「x以下」という条件を満たす最大値とすると、条件を満たす個数はJNG個である
            # 図解すると、
            # x以下(OK), x以下(OK), ..., x以下(OK), xより大きい(NG),...,xより大きい(NG)
            # <------------ JNG個 --------------><----------- N - JNG個 ----------->
            # 二分探索でこの境界値Jを求めにいく
            jok, jng = -1, N
            while jng - jok > 1:
                jmid = (jok + jng) // 2
                if A[i] * A[jmid] <= mid:
                    jok = jmid
                else:
                    jng = jmid
            s += jok + 1
        elif A[i] < 0:
            # 固定したA[i]が負の場合、積の数列
            # A[i]*A[0], A[i]*A[1], ... , A[i]*A[n-1]
            # は単調減少。
            # j=JOKの時が、「x以下」という条件を満たす最小のJだとすると、条件を満たす個数はJ+1個である
            # 図解すると、
            # xより大きい(NG), xより大きい(NG), ..., xより大きい(NG), x以下(OK),...,x以下(OK)
            # <--------------------- JOK個 ---------------------><----- N-JOK個 ------->
            jng, jok = -1, N
            while jok - jng > 1:
                jmid = (jok + jng) // 2
                if A[i] * A[jmid] <= mid:
                    jok = jmid
                else:
                    jng = jmid
            s += (N - jok)
        elif mid >= 0:
            s += N
        if A[i] * A[i] <= mid:
            t += 1
    # 求めたsは条件を満たす順列の個数なので、組み合わせの個数にするために/2する
    total = (s - t) // 2
    judge = total >= K
    ### ここまで判定ロジック ###
    # それがK個以上あるかを判定する
    if judge:
        ok = mid
    else:
        ng = mid
print(ok)


Measured memory_bytes: 103636
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(x,y) for(ll i=x;i<y;i++)
#define vll vector<ll>
#define sll set<ll>
#define pb push_back
#define in insert
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define tc ll t;cin>>t;while(t--)
#define rev(x) reverse(all(x))
#define low_str(x) transform(all(x),x.begin(),::tolower)
#define up_str(x) transform(all(x),x.begin(),::toupper)
int main()
{
ll x,y;
cin>>x>>y;
if(x+y==3)
    cout<<3;
else if(x+y==5)
    cout<<1;
else if(x+y==4)
    cout<<2;
return 0;
}


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

int main() {
	int N;
  	cin >> N;
  	vector<int> A(N);
  	for(int i =0; i < N; i++) cin >> A.at(i);
  
  	sort(A.begin(), A.end());
  
  	cout << A.at(N-1) - A.at(0) << endl;
}

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

int main(){
	int a,b,c,d;
	cin >> a >> b >> c >> d;
	if(a+b > c+d){
		cout << "Left" << endl;
	}else if(a+b == c+d){
		cout << "Balanced" << endl;
	}else{
		cout << "Right" << endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <bitset>
#include <tuple>
#include <set>
#include <map>
#define range(i, r) for(int i=0;i<r;i++)
#define ranges(i, l, r) for(int i=l;i<r;i++)
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int> >
#define vvl std::vector<std::vector<ll> >
#define MODs 1000000007;
#define MODn 1000000009;
typedef long long int ll;
using namespace std;

ll check(string S){
  ll count = 0;
  ll anum = 0;
  for(ll i=0;i<S.size();i++){
    if(S[i]=='A') {
      anum++;
    }else if(S[i]=='B'){
      count += anum;
    }
  }
  return count;
}
int main(int argc, char const *argv[]) {
  string s, reg="";
  ll ans = 0;
  std::cin >> s;
  for(ll i=0;i<s.size();i++){
    if(s[i]=='C' && (i==0 || s[i-1]!='B')){
      ans += check(reg);
      reg = "";
    }else if(s[i]=='B' && (i==s.size()-1 || s[i+1]!='C')){
      ans += check(reg);
      reg = "";
    }else{
      reg = reg + s[i];
    }
    if(i==s.size()-1) ans += check(reg);
  }
  std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 1092
N, M = map(int, input().split())
a = [0] * M
for i in range(M):
    a[i] = int(input())

mod = 10 ** 9 + 7

dp = [0] * 100010

j = 0
dp[0] = 1
if M > 0:
    if a[0] == 1:
        dp[1] = 0
        j += 1
    else:
        dp[1] = 1
else:
    dp[1] = 1

a += [-1]
for i in range(2, N+1):
    if M > 0:
        if i == a[j]:
            dp[i] = 0
            j += 1
            continue
    dp[i] = dp[i-1] + dp[i-2]
    dp[i] %= mod

print(dp[N] % mod)

Measured memory_bytes: 7668
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
 
#define MOD (long long int)(1e9+7)
#define ll long long int


int main(void){
	int a,b;
	cin>>a>>b;
	string s;
	cin>>s;
	for(int i=0;i<a;i++){
		if(s[i]<'0'||s[i]>'9'){
			cout<<"No"<<endl;
			return 0;
		}
	}

	for(int i=0;i<b;i++){
		if(s[i+a+1]<'0'||s[i+a+1]>'9'){
			cout<<"No"<<endl;
			return 0;
		}
	}

	if(s[a]!='-'){
		cout<<"No"<<endl;
		return 0;
	}

	cout<<"Yes"<<endl;
	return 0;	
}


Measured memory_bytes: 888
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
string s[2001];
int arr[2001][2001];
int main(){
	int h,w;
	cin>>h>>w;
	for(int i=0;i<h;i++)
		cin>>s[i],s[i]+='#';
	s[h]=string(w,'#');
	for(int i=0;i<=h;i++){
		int l=0,r=0;
		for(int j=0;j<=w;j++){
			if(s[i][j]=='#'){
				for(int k=l;k<r;k++)
					arr[i][k]+=r-l;
				l=r=j+1;
			}else
				r++;
		}
	}
	for(int j=0;j<=w;j++){
		int l=0,r=0;
		for(int i=0;i<=h;i++){
			if(s[i][j]=='#'){
				for(int k=l;k<r;k++)
					arr[k][j]+=r-l;
				l=r=i+1;
			}else
				r++;
		}
	}
	int ans=0;
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
			ans=max(ans,arr[i][j]);
	cout<<ans-1<<endl;
}


Measured memory_bytes: 19968
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
string s,t;
int n;
int dy1[6]={1,1,0,-1,-1,0};
int dx1[6]={0,-1,-1,0,1,1};
int dy2[6]={-1,-1,0,1,1,0};
int dx2[6]={0,1,1,0,-1,-1};
bool b[1000][1000];
bool c[1000][1000];
main(){
  cin>>n;
  while(n--){
    memset(b,0,sizeof(b));
    cin>>s;
    if(s=="-"){
      cout<<"true"<<endl;
      continue;
    }
    int y=500,x=500;
    b[y][x]=1;
    vector<int>X,Y;
    X.push_back(x);
    Y.push_back(y);
    rep(i,s.size()){
      x+=dx1[s[i]-'a'];
      y+=dy1[s[i]-'a'];
      b[y][x]=1;
      X.push_back(x);
      Y.push_back(y);
    }
    cin>>t;
    if(t=="-"){
      cout<<"false"<<endl;
      continue;
    }
    rep(i,2){
      rep(j,6){
        rep(l,X.size()){
          int f=0,y=Y[l],x=X[l];
          rep(k,t.size()){
            if(i){
              x+=dx2[(t[k]-'a'+j)%6];
              y+=dy2[(t[k]-'a'+j)%6];
            }else{
              x+=dx1[(t[k]-'a'+j)%6];
              y+=dy1[(t[k]-'a'+j)%6];
            }
            if(b[y][x])f++;
          }
          if(f==t.size())goto L;
        }
      }
      reverse(t.begin(),t.end());
    }
    cout<<"false"<<endl;
    if(0){L:;cout<<"true"<<endl;}
    cin>>s;
  }
}

Measured memory_bytes: 4140
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int a[3];
  rep(i,3) cin >> a[i];
  
  int array[3];
  rep(i,3) array[i] = i;
  
  int ans = 1001001001;
  do{
    int temp = abs( a[ array[1] ] - a[ array[0] ] );
    temp += abs( a[ array[2] ] - a[ array[1] ] );
    ans = min(ans,temp);
  }while(next_permutation(array,array+3));
  
  cout << ans << endl;
  
  return 0;
}
                      
                      

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

  int X,Y,Z;

  cin >> X >> Y >> Z;

  int ans=0;

  while(X>Y+Z){
    X-=Y+Z;
    ans++;
  }

  if(X<Z) ans--;
  
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;

ll dig(ll N) {
  ll dig = 0;
  while (N != 0) {
    N /= 10;
    dig++;
  }
  return dig;
}
vector<ll> div(ll N) {
  vector<ll> ret;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      ret.push_back(i);
      if (i * i != N)
        ret.push_back(N / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
int main(void) {
  ll N;
  cin >> N;
  vector<ll> divisor = div(N);
  ll ans = 1000000000000;

  for (int i = 0; i <= divisor.size() / 2; i++) {
    ll A = divisor[i];
    ll B = N / divisor[i];
    ans = min(ans, dig(max(A, B)));
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
/* #                          */

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <deque>
#include <map>
#include <iomanip>
#include <functional>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstdint>
#include <cstring>
//#pragma warning(disable:4996)

using namespace std;


/** Type Define **/
typedef long long int           ll;
typedef pair<int, int>          pii;
typedef pair<ll, ll>            pll;
typedef vector<string>          vs;
typedef vector<bool>            vb;
typedef vector<int>             vi;
typedef vector<long long>		vl;
typedef vector<vi>              vvi;
typedef vector<vl>				vvl;
typedef vector<char>            vch;
#define		it(x)  x.begin() 
#define		all(x)  x.begin(), x.end()
#define		pb(x)   push_back(x)
constexpr int           inf = 0x3f3f3f3f;
constexpr long long 	INF = 9123456789123456789;
constexpr long long     MOD = 1000000007LL;
/*************************************************/

void sol() {

}

int main() {
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    cin.tie(nullptr);

    int a, b;
    cin >> a >> b;
    int al = a * 12 + (a + 1) / 2; a++;
    int ar = a * 12 + (a - 1) / 2; a--;
    int bl = b * 10; b++;
    int br = b * 10; b--; br--;
    if (ar < bl || al > br) cout << "-1\n";
    else {
        int cl = max(al, bl);
        int cr = min(ar, br);
        if (cl <= cr) cout << cl << endl;
        else cout << "-1\n";
    }


    return 0;
}

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

int main(){
  int n, m, l;

  cin >> n >> m >> l;

  long a[n][m], b[m][l], c[n][l];

  for(int i=0; i<n; i++){
    for(int j=0; j<l; j++){
      c[i][j]=0;
    }
  }

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



  for(int i=0; i<m; i++){
    for(int j=0; j<l; j++){
      cin >> b[i][j];
    }
  }

  for(int i=0; i<n; i++){
    for(int j=0; j<l; j++){
      for(int k=0; k<m; k++){
        c[i][j] += a[i][k]*b[k][j];
      }
    }
  }
  for(int i=0; i<n; i++){
    for(int j=0; j<l-1; j++){
      cout << c[i][j] << " ";
    }
    cout << c[i][l-1] << endl;
  }
  return 0;
}



Measured memory_bytes: 3320
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int n,l[10],sum=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&l[i]);
    }
    sort(l,l+n);
    for(int i=0;i<n-1;i++){
        sum+=l[i];
    }
    if(sum>l[n-1]){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
}

Measured memory_bytes: 128
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string f[11]; int n, s[11], w[11], p[11], q[11];
bool ok() {
	for (int i = 0; i < n - 1; i++) {
		int r = 0;
		for (int j = i + 1; j < n; j++) r += w[p[j]];
		if (r > s[p[i]]) return false;
	}
	return true;
}
int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) cin >> f[i] >> w[i] >> s[i];
		for (int i = 0; i < n; i++) p[i] = i, q[i] = -1;
		do {
			if (ok()) {
				if (q[0] == -1) for (int i = 0; i < n; i++) q[i] = p[i];
				else {
					int sum1 = 0, sum2 = 0;
					for (int i = 0; i < n; i++) sum1 += w[p[i]] * (i + 1), sum2 += w[q[i]] * (i + 1);
					if (sum1 < sum2) for (int i = 0; i < n; i++) q[i] = p[i];
				}
			}
		} while (next_permutation(p, p + n));
		for (int i = 0; i < n; i++) cout << f[q[i]] << endl;
	}
}

Measured memory_bytes: 3016
nyuryoku = input()
a,b,c,d = map(lambda x :int(x),nyuryoku.split())
ab = False
bc = False
ac = False
if abs(a-b) <= d:
    ab = True

if abs(b-c) <= d:
    bc = True

if abs(a-c) <= d:
    ac = True

if(ac or (ab and bc)):
    print("Yes")
else:
    print("No")


Measured memory_bytes: 3060
#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>
#include <assert.h>
#include <unordered_map>

using namespace std;
const int N = 1e6 + 50;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 57;
const int LN = 22;
const int SN = 320;

long long n, m, u, v, k, t, q, x, d, p, a, b, c, s;



int main() {
   ios::sync_with_stdio(false);cin.tie(0);
   cin >> n >> m;
   int sol = 0;
   
   for (int i = 2; i <= 9; ++i) {
      for (int j = 2; j <= 9; ++j) {
         if (i * 10 + j <= m and i * j <= n) {
            sol++;
         }
      }
   }
   
   cout << sol << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int,int>
#define pl pair<long long,long long>
#define lg length()
#define pb push_back
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005

long long a,b,c,d,e,f,p[3005],q[3005],x,y;

double t=-1;

int32_t main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
    cin >> a >> b >> c >> d >> e >> f;
    for(int i=0;i<=3000;i++){
        for(int j=0;j<=3000;j++){
            if(i*c+j*d>f) continue;
            p[i*c+j*d]=1;
        }
    }
    for(int i=0;i<=3000;i++){
        for(int j=0;j<=3000;j++){
            if(i*a*100+j*b*100>f) continue;
            q[i*a*100+j*b*100]=1;
        }
    }
    for(int i=0;i<=3000;i++){
        for(int j=0;j<=3000;j++){
            if(p[i] && q[j] && i+j<=f && 100*i<=j*e && (double)i/(i+j)>t){
                t=(double)i/(i+j);
                x=i+j; y=i;
            }
        }
    }
    cout << x << ' ' << y;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int maxn=400100;
char str[maxn];
int beg[maxn],End[maxn];
int nex[maxn],las[maxn];
int cnt[maxn],n;
bool pd(){
	for(int i=1;i<=n;i++)
		cnt[str[i]-'a']++;
	bool bo=n&1;
	for(int i=0;i<26;i++){
		if(cnt[i]&1){
			if(bo) bo=0;
			else return 0;
		}
	}
	return 1;
}
int Abs(int x){
	return x>0?x:-x;
}
int tr[maxn];
int Query(int x){
	int res=0;
	while(x){
		res+=tr[x];
		x-=x&(-x);
	}
	return res;
}
void updata(int x,int v){
	while(x<=n){
		tr[x]+=v;
		x+=x&(-x);
	}
}
int main(){
//	freopen("A.in","r",stdin);
	scanf("%s",str+1);
	n=strlen(str+1);
	if(!pd()){
		printf("-1\n");
		return 0;
	}
	for(int i=1;i<=n;i++){
		nex[End[str[i]-'a']]=i;
		End[str[i]-'a']=i;
	}
	for(int i=n;i>=1;i--){
		las[beg[str[i]-'a']]=i;
		beg[str[i]-'a']=i;
	}
	for(int i=1;i<=n;i++)
		updata(i,1);
	int l=1,r=n;
	long long ans=0;
	int vl,vr;
	while(l<r){
		int v=n*3,p;
		for(int i=0;i<26;i++){
			if(cnt[i]<=1) continue;
			vl=Query(beg[i])+l-1;
			vr=Query(End[i])+l-1;
			if(Abs(r-vr)+Abs(vl-l)<v){
				v=Abs(r-vr)+Abs(vl-l);
				p=i;
			}
		}
		updata(End[p],-1);
		updata(beg[p],-1);
		End[p]=las[End[p]];
		beg[p]=nex[beg[p]];
		cnt[p]-=2;
		ans=ans+v;
		l++,r--;
	}
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 7424
N, K = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]

ab.sort()
now = 0
count = 0
for i in range(N):
  now = ab[i][0]
  count += ab[i][1]
  if count >= K:
    print(now)
    break
    

Measured memory_bytes: 27872
/*input
5 12
1 1 1 1 1
*/
 
#include <bits/stdc++.h> 
#define N 100005
using namespace std;
 
int n, k;
int main() {
	scanf("%d %d", &n, &k);
	int mx = 0, gcd = 0;
	for (int i = 1; i <= n; i++) {
		int x; scanf("%d", &x);
		mx = max(mx, x);
		gcd = __gcd(gcd, x);
	}
	puts(k % gcd == 0 && k <= mx ? "POSSIBLE" : "IMPOSSIBLE");
}

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

using namespace std;

int main() {
    string S, T;

    cin >> S >> T;

    int ans = S.length();
    for (int i = 0; i <= S.length() - T.length(); ++i) {
        int count = 0;
        for (int j = 0; j < T.length(); ++j) {
            if (T.at(j) != S.at(i + j)) {
                ++count;
            }
        }

        ans = min(count, ans);        
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3604
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <functional>

template<class T>
std::vector<T> getDivisors(T N){
    std::queue<T> D_lower;
    std::stack<T> D_upper;
    for(T i=static_cast<T>(1); i*i<=N; ++i){
        if(N%i == 0){
            D_lower.emplace(i);
            if(i*i != N){
                D_upper.emplace(N/i);
            }
        }
    }
    std::vector<T> D;
    while(!D_lower.empty()){
        D.emplace_back(D_lower.front());
        D_lower.pop();
    }
    while(!D_upper.empty()){
        D.emplace_back(D_upper.top());
        D_upper.pop();
    }
    return D;
}

int main(int argc, char *argv[]){
    long long N,K;
    std::cin >> N >> K;
    std::vector<long long> A(N);
    for(auto &a : A){
        std::cin >> a;
    }
    long long S = std::accumulate(A.begin(),A.end(),0);
    auto D = getDivisors(S);
    std::sort(D.begin(),D.end(),std::greater<long long>());

    long long g, gTilde;
    std::vector<long long> R(N);
    long long i;
    long long sumQuot, sumDiff;
    long long quot, quotSup;
    for(const auto d : D){
        g = d;
        gTilde = 0;
        for(i=0; i<N; ++i){
            R[i] = A[i]%g;
            gTilde += R[i];
        }
        gTilde /= g;

        sumQuot = 0;
        sumDiff = 0;
        std::sort(R.begin(),R.end(),std::greater<long long>());
        for(i=0; i<N; ++i){
            quot = std::min((K+R[i]-sumDiff)/g, gTilde-sumQuot);
            if(quot <= 0){
                break;
            }
            quot = 1;
            sumQuot += quot;
            sumDiff += quot*g - R[i];
        }
        if(sumQuot==gTilde){
            break;
        }
    }
    std::cout << g << std::endl;

    return 0;
}


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


pair<int,int> SegTree[1<<18];

void update(int l,int r,int x,int t)
{
	while (l!=r)
	{
		if(l%2)
		{
			SegTree[l] = make_pair(x,t);
			l++;
		}
		if(r%2)
		{
			SegTree[r-1] = make_pair(x,t);
			r--;
		}
		l/=2;
		r/=2;
	}
	//SegTree[l] = make_pair(x,t);
	//cout<<l<<endl;
}

int get(int i)
{
	pair<int,int> res(-1,-2);
	while(i!=0)
	{
		if(SegTree[i].second>res.second)
		{
			res = SegTree[i];
		}
		i/=2;
	}
	return res.first;
}


int main()
{
	//cout<<(1<<18)<<endl;	
	int n,q;
	cin >> n >> q;
	for(int i = 0;i<(1<<18);i++)
	{
		SegTree[i]=make_pair(INT_MAX,-1);
	}
	for(int i = 0;i<q;i++)
	{
		int op;
		cin >> op;
		if(op)
		{
			int I;
			cin >> I;
			I += (1<<17);
			cout<<get(I)<<endl;
		}
		else
		{
			int s,t,x;
			cin >> s >> t >> x;
			s+=(1<<17);
			t+=(1<<17);
			t++;
			update(s,t,x,i);

		}
		//cout<<SegTree[(1<<17)].first<<endl;
	}
}


Measured memory_bytes: 5140
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

import static java.util.Comparator.*;

public class Main {
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        Solver solver = new Solver(System.in, out);
        solver.solve();
        out.close();
    }
}
class Solver {
	Scanner sc;
	PrintWriter out;
    public Solver(InputStream in, PrintWriter out) {
    	sc = new Scanner(in);
    	this.out = out;
    }
    // ==================================================================
	public void solve() {
		int N = ni();
		char[] C = nc();
		int ans = 0, cnt = 0;
		for (int i = 0; i < N; i++) {
			if(C[i] == 'A')			cnt = 1;
			else if(cnt == 1 && C[i] == 'B')	cnt++;
			else if(cnt == 2 && C[i] == 'C') {	ans++; cnt = 0; }
			else								cnt = 0;
		}
		out.println(ans);
	}
    static class PP{
    	public int key, val;
    	public PP(int key, int val) {
    		this.key = key;
    		this.val = val;
    	}
		public int getKey() {
			return key;
		}
		public void setKey(int key) {
			this.key = key;
		}
		public int getVal() {
			return val;
		}
		public void setVal(int val) {
			this.val = val;
		}
    }

    // ------------------------------------------
    // 入力
    // ------------------------------------------
    public int ni() {
    	return sc.nextInt();
    }
    public long nl() {
    	return sc.nextLong();
    }
    public String ns() {
    	return sc.next();
    }
    public char[] nc() {
    	return sc.next().toCharArray();
    }
    public int[] ndi(int N) {
    	int[] ans = new int[N];
    	for (int i = 0; i < N; i++) {
			ans[i] = ni();
		}
    	return ans;
    }
    public long[] ndl(int N) {
    	long[] ans = new long[N];
    	for (int i = 0; i < N; i++) {
			ans[i] = nl();
		}
    	return ans;
    }
    public String[] nds(int N) {
    	String[] ans = new String[N];
    	for (int i = 0; i < N; i++) {
			ans[i] = ns();
		}
    	return ans;
    }
    public char[][] ndc(int N) {
    	char[][] ans = new char[N][];
    	for (int i = 0; i < N; i++) {
			ans[i] = nc();
		}
    	return ans;
    }
    public int[][] nddi(int N, int S) {
    	int[][] ans = new int[N][S];
    	for (int i = 0; i < N; i++) {
			for (int j = 0; j < S; j++) {
				ans[i][j] = ni();
			}
		}
    	return ans;
    }
}


Measured memory_bytes: 21844
'use strict';
const main = input => {
  const n = parseInt(input[0]);
  const d = input[1].split(' ').map(val => parseInt(val));
  // console.log(n, d);
  let count = 0;
  for(let i=0; i<n-1; i++) {
    for(let j=i+1; j<n; j++) {
      count += d[i] * d[j];
    }
  }
  console.log(count);
  
}
main(require("fs").readFileSync("/dev/stdin", "utf8").split('\n'));

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

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int a, b; cin >> a >> b;
	if (a <= 8 && b <= 8)
		cout << "Yay!" << endl;
	else cout << ":(" << endl;
  
  return 0;
}

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

int main() {
  double r,l;
  cin >> r;
  l=2*3.1415926535*r;
  printf("%.8lf",l);
}

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



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

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

#define k 10000

void CountingSort(int A[], int B[], int n) {
  int C[k+1];
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    C[A[i]]++;
  }
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i-1];
  }
  for (int i = n-1; i >= 0; i--) {
    B[C[A[i]]-1] = A[i];
    C[A[i]]--;
  }
}

int main() {
	int n;
  scanf("%d", &n);
  int A[n], B[n];
  for ( int i = 0; i < n; i++ ) scanf("%d", &A[i]);
  CountingSort(A, B, n);
  for ( int i = 0; i < n-1; i++ ) printf("%d ", B[i]);
  printf("%d\n", B[n-1]);

  return 0;
}



Measured memory_bytes: 18264
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
// #define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

long long power(long long a, long long b,long long m) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a %m;
        a = a * a %m;
        b >>= 1;
    }
    return res;
}


unordered_map<int,int>mm;
int temp=0;

signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);  
cout.tie(0);   

#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif  

    string a;
    cin>>a;
    int n=a.length();
    for(int i=0;i<n;i++)
    {
        temp^=(1<<(a[i]-'a'));  
        for(int j=0;j<26;j++)
        {
            int z=(temp^(1<<j));
            if(z==0)
                mm[temp]=1;
            else if(temp==0)
                mm[temp]=1;
            else if(mm[temp]!=0 && mm[z]!=0)
                mm[temp]=min(mm[temp],mm[z]+1);
            else if(mm[temp]==0&& mm[z]!=0)
                mm[temp]=mm[z]+1;
        }
        if(temp==0)
            mm[temp]=1;
    }
    if(temp==0)
        cout<<1;
    else
        cout<<mm[temp];
    
} 
    


Measured memory_bytes: 173228
#include <bits/stdc++.h>
#define ft first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) {if(a<b) a=b;}
#define chmin(a, b) {if(a>b) a=b;}
#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF=1e18;
static const ll MAX=1e5+7;
static const ll MOD=1e9+7;
ll max(ll a, ll b) {return a>b?a:b;}
ll min(ll a, ll b) {return a<b?a:b;}


map<ll, ll> mp;
void divisor(ll n) {
  for(ll i=1; i*i<=n; i++) {
    if(n%i==0) {
      mp[i]++;
      mp[n/i]++;
    }
  }
}

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

int main(void) {
  ll a, b;
  cin >> a >> b;
  
  divisor(gcd(a, b));
  
  ll ans=1;
  for(auto p: mp) {
    ll cnt=0;
    ll n=p.ft;
    for(ll i=2; i*i<=n; i++) {
      while(n%i==0) {
        cnt++;
        n/=i;
      }
    }
    if(n>1) cnt++;
    
    if(cnt==1) ans++;
  }
  
  pt(ans);
}


Measured memory_bytes: 640
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define rep(n, i) Rep(0, n-1, i)
#define INF 10000000

typedef long long ll;
typedef pair<ll, ll> P;
typedef std::priority_queue<int> IntPrioQueue; //Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; //A->Z

void solve(void){
    int n;
    scanf("%d\n", &n);
    ll as[n], bs[n], cs[n];
    rep(n, i) scanf("%lld", &as[i]);
    rep(n, i) scanf("%lld", &bs[i]);
    rep(n, i) scanf("%lld", &cs[i]);
    sort(as, as+n);sort(bs, bs+n);sort(cs, cs+n);

    ll saidanUp[n];
    ll upsum = 0, downsum = 0;
    rep(n, i) {
        if (bs[i] > as[0]) upsum += upper_bound(as, as+n, bs[i]-1) - as;
        saidanUp[i] = upsum;
    }
    rep(n, i) {
        if (cs[i] <= bs[0]) continue;
        downsum += saidanUp[upper_bound(bs, bs+n, cs[i]-1) - bs - 1];
    }

    cout << downsum << '\n';
}

int main(void){
  solve();
  return 0;
}


Measured memory_bytes: 3328
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<bitset>
#include<iomanip>
#include<list>
#include <numeric>
using namespace std;
typedef unsigned long long int ulint;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define RE return 0
//ios::sync_with_stdio(false);
//std::cin.tie(0);
//<< setprecision(20)
//const int mod=(int)998244353;
const llint big=(llint)(2.19e16)+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
template <class T> void soun(T& ar)
{sort(ar.begin(),ar.end());ar.erase(unique(ar.begin(),ar.end()),ar.end());}
llint gcd(llint a,llint b){if(a%b==0){return b;}else{return gcd(b,a%b);}}
llint lcm(llint a,llint b){return a/gcd(a,b) *b;}
template<class T,class U> auto LB(T& ve,U in){return lower_bound(ve.begin(),ve.end(),in);}
template<class T,class U> auto UB(T& ve,U in){return upper_bound(ve.begin(),ve.end(),in);}
template<class T,class U> auto LBI(T& ve,U in){return LB(ve,in)-ve.begin();}
template<class T,class U> auto UBI(T& ve,U in){return UB(ve,in)-ve.begin();}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
using pli=pair<llint,int>;
using daic=priority_queue<pli,vector<pli>,greater<pli>>;
int main(void){
	llint n,m;cin>>n>>m;
	cout<<abs(n-2)*abs(m-2)<<endl;
	RE;
}

Measured memory_bytes: 764
N, M = map(int,input().split())

kameasi = 4 * N

if M == kameasi:
  print('Yes')
  exit()

for i in range(1,N+1):
    # 足の数
    f = kameasi - 2*i 
    if f == M:
        print('Yes')
        break
else:
    print('No')

Measured memory_bytes: 61596
#include <stdio.h>
int main() {int A[4],B[4],C[10],i,j;while (scanf("%d %d %d %d %d %d %d %d",&A[0],&A[1],&A[2],&A[3],&B[0],&B[1],&B[2],&B[3])!=EOF) {for (i=0,j=0;i<4;i++) if (A[i]==B[i]) j++;for (i=0;i<10;C[i++]=0);for (i=0;i<4;i++) C[A[i]]++;int k=-j;for (i=0;i<4;i++) if (C[B[i]]>0) k++,C[B[i]]--;printf("%d %d\n",j,k);} return 0;}

Measured memory_bytes: 1024
def gcd(a,b):
  while b:
    a,b=b,a%b
  return a
a,b=map(int,input().split())
g=gcd(a,b)

c=1
for i in range(2,10**6+1):
  if g%i==0:
    c+=1
    while g%i==0:
      g//=i
if g!=1:
  c+=1
print(c)

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

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
    int x(0), sgn(1); char ch(getchar());
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
    for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
    return x * sgn;
}

void File() {
	freopen ("pyramid.in", "r", stdin);
	freopen ("pyramid.out", "w", stdout);
}

const int N = 2e6 + 1e3;

int n, a[N], b[N], l[N], r[N];

inline bool Con(int pos) {
	return pos > 0 && pos <= 2 * n - 1 && r[pos] > l[pos];
}

inline bool Check(int val) {
	For (i, 1, n * 2 - 1) b[i] = (a[i] >= val);
	For (i, 1, n * 2 - 1)
		if (b[i] == b[i - 1] && (i == n || i == n + 1)) return b[i];

	b[0] = b[1] ^ 1;
	For (i, 1, n * 2 - 1) 
		if (b[i] != b[i - 1]) l[i] = i; else l[i] = l[i - 1];
	b[n * 2] = b[n * 2 - 1] ^ 1;
	Fordown (i, n * 2 - 1, 1) 
		if (b[i] != b[i + 1]) r[i] = i; else r[i] = r[i + 1];

	int pos = n * 2, tmp = 0;
	For (i, 0, 2 * n - 1) if (Con(i)) if (chkmin(pos, abs(i - n))) tmp = i;

	return pos == n * 2 ? b[1] : b[tmp];
}

int main () { 

	n = read();
	For (i, 1, n * 2 - 1)
		a[i] = read();

	int l = 1, r = n * 2, ans = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (Check(mid)) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	printf ("%d\n", ans);

	return 0;

}


Measured memory_bytes: 11264
import * as fs from 'fs';

const [x, a, b]: number[] = fs.readFileSync('/dev/stdin', 'utf8').trim().split(' ').map(Number);
if (a >= b) {
    console.log('delicious');
} else if (x >= (b - a)) {
    console.log('safe');
} else {
    console.log('dangerous');
}



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

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000000000000LL
#define MOD 1000000007
#define writeln(n) cout<<n<<endl
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
//vector<tuple<long long, long long, long long>>vec;

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

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

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

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p%i == 0) return false;
	}
	return true;
}

int main()
{
int n;
cin>>n;
for(int i=31; i<=55555; i+=30)
{
    if(isprime(i)== true)
    {
        if(i!=31) cout<<" ";
        cout<<i; n--;
    }
    if(n==0) break;
}
cout<<endl;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}

int main(){
    int n;
    cin >> n;
    vi p(n), q(n);
    rep(i, n) {
        cin >> p[i];
        --p[i];
    }
    rep(i, n) {
        cin >> q[i];
        --q[i];
    }
    vi id(n);
    rep(i, n) id[i] = i;
    map<vi, int> mp;
    do {
        mp[id] = 1;
    }while(next_permutation(all(id)));
    {
        int cnt = 0;
        for(auto& p : mp) {
            p.se = cnt++;
        }
    }
    cout << abs(mp[q] - mp[p]) << endl;
    return 0;
}

Measured memory_bytes: 5248
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,big=0;
  int x=0;
  char s[110];
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>s[i];
    if(s[i]=='I') x++;
    else x--;
    big=max(x,big);
  }
  cout<<big<<'\n';
  return 0;
}

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

#define MAX_N 505

using namespace std;

char mat[2][MAX_N][MAX_N];

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

    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);

    int h, w;
    cin>>h>>w; // lee filas columas

    // colorealas disjuntamente de #'s, pero que ambas sean conexas
    int color1 = 0, color2 = 1;
    for(int i = 0; i < h; ++i) {
        for(int j = 1; j < w - 1; ++j) {
            mat[color1][i][j] = '#';
            mat[color2][i][j] = '.';
        }
        swap(color1, color2);
    }
    for(int i = 0; i < h; ++i) {
        mat[0][i][0] = '#'; mat[1][i][w - 1] = '#';
        mat[1][i][0] = '.'; mat[0][i][w - 1] = '.';
    }

    // lee la entrada y pon # en ambas si el Aij es #
    char c;
    for(int i = 0; i < h; ++i) {
        for(int j = 0; j < w; ++j) {
            cin>>c;
            if(c == '#') {
                mat[0][i][j] = c; mat[1][i][j] = c;
            }
        }
    }

    // imprime...
    for(int i = 0; i < h; ++i) {
        for(int j = 0; j < w; ++j) {
            cout<<mat[0][i][j];
        }
        cout<<"\n";
    }
    cout<<"\n";
    for(int i = 0; i < h; ++i) {
        for(int j = 0; j < w; ++j) {
            cout<<mat[1][i][j];
        }
        cout<<"\n";
    }

    return 0;
}

Measured memory_bytes: 1408
import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    [n, k] <- getIntList
    as <- sort <$> getIntList
    fs <- reverse . sort <$> getIntList
    print $ solve 0 (maximum (zipWith (*) as fs)) k (zip as fs)

training time k afs = sum (map (train time) afs) <= k
    where train t (x, y) | x * y < t = 0
                         | otherwise = x - (t `div` y)

solve t0 t1 k afs | t0 == t1 = t0
                  | training tn k afs = solve t0 tn k afs
                  | otherwise = solve (tn + 1) t1 k afs
                  where tn = (t0 + t1) `div` 2

Measured memory_bytes: 62844
#include<stdio.h>

int main(void){
	int l,r,d;
	scanf("%d %d %d",&l,&r,&d);
	if(l%d==0){
		l=(l-l%d)/d;
		r=(r-r%d)/d;
		printf("%d",r-l+1);
	}else{
		l=(l-l%d)/d;
		r=(r-r%d)/d;
		printf("%d",r-l);
	}
	return 0;
}

Measured memory_bytes: 1736
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
void _OUT() {}; template<class H, class... T> void _OUT(H &h, T &... t) { cout<<' '<<h; _OUT(t...); };
template<class H, class... T> void _P(H &h, T &... t) { cout<<h; _OUT(t...); };
#define _PN(...) _P(__VA_ARGS__); cout << endl;
#define EXEC(...) void exec() { (__VA_ARGS__ - 1) ? multi() : solve(0); }

void exec();
void solve(int t);
void multi() { int TT; cin>>TT; REP1(t,TT) solve(t); }
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    exec();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 10
#else
#define SIZE 323450
#endif

int N,M;
vector<int> A;

void solve(int _t) {
    cin>>N;
    A.resize(3*N);
    REP(i,3*N)cin>>A[i];
    int DP[2][SIZE] = {};
    {
        priority_queue<int, vector<int>, greater<int>> s;
        int sum = 0;
        REP(i,3*N) {
            s.push(A[i]);
            sum += A[i];
            if (s.size() > N) {
                int v = s.top();
                sum -= v;
                s.pop();
            }
            DP[0][i]=sum;
        }
    }
    {
        priority_queue<int> s;
        int sum = 0;
        RREP(i,3*N) {
            s.push(A[i]);
            sum += A[i];
            if (s.size() > N) {
                int v = s.top();
                sum -= v;
                s.pop();
            }
            DP[1][i]=sum;
        }
    }
    int ans = -LONG_LONG_MAX/2;
    REP(i,N+1) {
        chmax(ans, DP[0][N-1+i]-DP[1][N+i]);
    }
    _PN(ans);
}
EXEC(1)



Measured memory_bytes: 12480
import java.util.Scanner;

class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int w = sc.nextInt();
        int h = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();
        int r = sc.nextInt();
        sc.close();
        if(x-r >= 0 && x+r <= w && y-r >= 0 && y+r <= h)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

Measured memory_bytes: 26232
f=lambda:[*map(int,input().split())]
n,l,q=f()[0],f(),f()[0]
from collections import *
C,s=Counter(l),sum(l)
for _ in range(q): b,c=f(); s+=(c-b)*C[b]; C[c]+=C[b]; C[b]=0; print(s)

Measured memory_bytes: 24020
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
////////////////////////////////////unionfind

int par[100010];//親
int rnk[100010];//ランク

void init(int n){//初期化
  for(int i=0;i<n;i++){
     par[i]=i;
     rnk[i]=0;
  }
}

int find(int x){//親を求める
  if(par[x]==x) return x;
  return par[x]=find(par[x]);
}

void unite(int x,int y){//union
  x=find(x);
  y=find(y);
  if(x==y)return;
  if(rnk[x]<rnk[y]){//rankが小さい方にくっつける
    par[x]=y;
  }
  else{
   par[y]=x;
    if(rnk[x]==rnk[y])rnk[x]++;//rankが同じ時は1増やす
  }
}

bool same(int x,int y){
 return find(x)==find(y); 
}
int main(){
 init(13);
  int a,b;
  cin>>a>>b;
  unite(4,6);unite(6,9);unite(9,11);
  unite(1,3);unite(3,5);unite(5,7);unite(7,8);unite(8,10);unite(10,12);
  Yes(same(a,b));
}

Measured memory_bytes: 3576
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define LLINF (1e18)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> h(n);
    for (int i = 0; i < n; i++)
    {
        cin >> h[i];
    }
    // dp[i] := i番目の足場にいくのに最小コスト
    vector<int> dp(n + 1, INF);
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (int i = 0; i < n - 2; i++)
    {
        chmin(dp[i + 2], min(dp[i + 1] + abs(h[i + 2] - h[i + 1]), dp[i] + abs(h[i + 2] - h[i])));
    }
    cout << dp[n-1] << endl;
}

Measured memory_bytes: 1024
def main():
    s = input()
    b1,b2 = s[1],s[0]
    if len(s)==2:
        if b1 == b2:
            print("1 2")
            exit()
        else:
            print("-1 -1")
            exit()
    for i in range(2,len(s)):
        if s[i] == b1 or s[i] == b2:
            print("{} {}".format(i-1,i+1))
            exit()
        else:
            b1,b2 = s[i],s[i-1] 
    print("-1 -1")

if __name__=='__main__':
    main()

Measured memory_bytes: 39280