akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <cassert>
#include <functional>
#include <set>
#include <deque>
using namespace std;
typedef long long ll;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const int INF = 1<<29;
#define LNF __LONG_LONG_MAX__;
const ll MOD = 1000000007;
const int MAX = 200000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//最大公約数
int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; }
//最小公倍数
int lcm(int x, int y) { return x / gcd(x, y) * y; }
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n,long long k){
    com[0] = 1;
    for(int i=1;i<=k;i++){
        com[i] = (com[i-1]*(n-i+1)%MOD)*modinv(i,MOD)%MOD;
    }
}

int main(){
    int N; cin >> N;
    vector<int> A(N);
    vector<int> B(N);
    vector<int> C(N);
    for(int i=0;i<N;i++) cin >> A[i];
    for(int i=0;i<N;i++) cin >> B[i];
    for(int i=0;i<N;i++) cin >> C[i];
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    sort(C.begin(),C.end());
    ll out=0;
    for(int i=0;i<N;i++){
        ll cnt=0;
        int l=0,r=N;
        int mid;
        cerr << B[i] << ":\t";
        while(r-l>1){
            mid = (l+r)/2;
            if(B[i]<=A[mid]){
                r=mid;
            }else{
                l=mid;
            }
        }
        if(B[i]<=A[0]) cnt = 0;
        else if(B[i]>A[N-1]) cnt = N;
        else cnt = l+1;
        cerr << cnt << "\t";
        l=0;r=N;
        while(r-l>1){
            mid = (l+r)/2;
            if(B[i]<C[mid]){
                r=mid;
            }else{
                l=mid;
            }
        }
        if(B[i]<C[0]) cnt *= N;
        else if(B[i]>=C[N-1]) cnt = 0;
        else cnt *= (N-r);
        cerr << (N-r) << "\t" << cnt << "\n";
        out += cnt;
    }
    cout << out << endl;
    return 0;
}


Measured memory_bytes: 1408
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		for(long i = 1; i <= N; i++) {
			if(i * i > N) {
				System.out.println((i - 1) * (i - 1));
				break;
			} else if(i * i == N) {
				System.out.println(i * i);
				break;
			}
		}
	}
}


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

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	ll x; cin >> x;
	ll result = 0;
	result += (x / 500) * 1000;
	x %= 500;
	result += (x / 5) * 5;
	cout << result;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define ar array<int,2>
#define inf 1000000000000000000 
int mod=1e9+7;
int min(int a,int b){return (a<b)?a:b;}
int max(int a,int b){return (a>b)?a:b;}
int fp(int a,int b){if(b==0) return 1;int x=fp(a,b/2);x=(x*x)%mod;if(b&1) x=(x*a)%mod;return x;}


const int N=2e5+5;
int n;
int v[22][22];
int dp[22][1ll<<22];
int solve(int pos,int mask)
{ 
    if(pos==n+1)
      return 1;
    if(dp[pos][mask]!=-1)
      return dp[pos][mask];
    int x=0;
    for(int i=0;i<n;i++)
    {
      if(mask&(1ll<<i))
      {
        if(!v[pos][i+1])
          continue;
        x+=solve(pos+1,mask^(1ll<<i));
        if(x>=mod)
          x-=mod;
      }
    }
    return dp[pos][mask]=x;
}

signed main() {
 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
 memset(dp,-1,sizeof(dp));
 cin>>n;

 for(int i=1;i<=n;i++)
 {
  for(int j=1;j<=n;j++)
  {
    cin>>v[i][j];
  }
 }

 int ans=solve(1,(1ll<<n)-1);
 cout<<ans;





}

Measured memory_bytes: 724524
import scala.util.control.Breaks
object Main extends App {
  val b = new Breaks
  b.breakable {
    while (true) {
      val Seq(stra, op, strb) = readLine().split(' ').toList

      if (op == "?") b.break

      val opfcn: Function2[Int, Int, Int] = op match {
        case "+" => _+_
        case "-" => _-_
        case "*" => _*_
        case "/" => _/_
      }

      println(opfcn(stra.toInt, strb.toInt))
    }
  }
}


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

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

    int cont, cont_max, bf, r;
    bf = -1;
    cont = 0;
    cont_max = 0;
    for (int i = 0; i < 4; i++)
    {
        r = N % 10;
        N /= 10;

        if (bf == r)
        {
            cont++;
            cont_max = max(cont_max, cont);
        }
        else
        {
            bf = r;
            cont = 1;
        }
    }

    if (cont_max >= 3)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  int ans,i,len,p;
  char s[111111];
  cin>>s;
  for(len=0;s[len]!='\0';)
    len++;
  ans=len;
  for(i=1;s[i]!='\0';i++){
    if(s[i]!=s[i-1]){
      if(i<=len/2)
        p=len-i;
      else
        p=i;
      if(p<ans)
        ans=p;
    }
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (int)(n); i++ )
using namespace std;
int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a.at(i); 
    int ans = 0;
    int numodd= 0;
    while(true){
        rep(i,n){
            if(a.at(i) % 2 != 0) numodd++;
        }
        if(numodd != 0) break;
        rep(i,n) a.at(i) /= 2;
        ans++; 
    }
    cout << ans << endl;
}

Measured memory_bytes: 3600
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N, K = [int(i) for i in input().strip().split()]
    no_sweet_members = set(range(1, N+1))
    for i in range(K):
        d = int(input().strip())
        member = [int(i) for i in input().strip().split()]
        for m in member:
            if m in no_sweet_members:
                no_sweet_members.remove(m)

    return len(no_sweet_members)


if __name__ == "__main__":
    print(main())


Measured memory_bytes: 9184
#include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

void f(int n){
  int a[1000000];

  rep(i,n){
    cin>>a[i];
  }

  sort(a,a+n);
  int ans = inf;
  rep(i,n-1){
    ans = min(ans,abs(a[i]-a[i+1]));
  }

  cout<<ans<<endl;
}


int main(){
  
  while(1){
    int n;
    cin>>n;
    if(n==0) break;
    f(n);
  }

  return 0;
}



Measured memory_bytes: 3148
import java.util.*;
import java.math.*;
import java.awt.geom.*;
import java.io.*;
      
      
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String in = sc.next();
		int count = 0;
		for(int i = 0; i < in.length(); i++) {
			String a = in.substring(0,i);
			String b = in.substring(i);
			if(a.length() >= 2 && a.charAt(0) == '0') continue;
			if(b.length() >= 2 && b.charAt(0) == '0') continue;
			if(b.length() == 0) b = "0";
			if(a.length() == 0) a = "0";
			int A = Integer.parseInt(a); // A - B
			int B = Integer.parseInt(b); // A + B
			int tmp = A + B;
			if(tmp % 2 != 0) continue;
			if(tmp < 0) continue;
			tmp /= 2;
			int tmpA = B - tmp; 
			if(tmpA < 0) continue;
			count++;
		}
		System.out.println(count);
	}
 }

Measured memory_bytes: 26340
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <cmath>
#include <numeric>
#include <iomanip>
#include <deque>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <sstream>
#include <stdexcept>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, done) for (int i = (a); i < (done); ++i)
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define debug(a) cout << (a) << endl
namespace mp = boost::multiprecision;
using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;
template <class T>
bool chmax(T &a, const T &b)
{
	if (a < b)
	{
		a = b;
		return 1;
	}
	return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
	if (a > b)
	{
		a = b;
		return 1;
	}
	return 0;
}

ll M = 1000000007;

void solve()
{
	string s;
	cin>>s;
	if(s[s.size()-1]=='s'){
		cout<<s<<"es"<<endl;
	}else{
		cout<<s<<"s"<<endl;
	}

}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	solve();
	return 0;
}


Measured memory_bytes: 3640
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <climits>


#define REP(i,N) for(auto i = 0; i <N; i++)
using namespace std;
typedef long long ll;


int in(){
    int temp;
    scanf("%d",&temp);
    return temp;
}

ll lin(){
    ll temp;
    scanf("%lld",&temp);
    return temp;
}



int main() {

    int N = in();
    
    vector<int> X;
    vector<int> x;
    
    for(auto i = 0; i <N; i++){
        X.push_back(in());
        x.push_back(X.back());
    }
    
    sort(X.begin(),X.end());
    
    for(auto a: x){
        if (a <= X[N/2-1]){
            cout << X[N/2] << endl;
        }else{
            cout << X[N/2-1] << endl;
        }
        
    }
    
 
    
    return 0;
}



                          





Measured memory_bytes: 3948
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <cstdio>
#include <complex>
#include <cstdint>
#include <tuple>

#define M_PI       3.14159265358979323846

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
inline int readInt() { int x; scanf("%d", &x); return x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//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 SQ(a) ((a)*(a))
#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,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007

const int N  = 10005;
const int D = 101;
LL dp[2][N][D];

int main() {
	string str;
	int d;
	cin >> str >> d;
	int n = str.length();
	dp[0][0][0] = 1;
	REP(i, n) {
		REP(j, d){
			dp[0][i + 1][(j + str[i] - '0') % d] = (dp[0][i + 1][(j + str[i] - '0') % d] + dp[0][i][j])%MOD;
			REP(k, str[i] - '0')dp[1][i + 1][(j + k) % d] = (dp[1][i + 1][(j + k) % d] + dp[0][i][j])%MOD;
			REP(k, 10)dp[1][i + 1][(j + k) % d] = (dp[1][i + 1][(j + k) % d] + dp[1][i][j])%MOD;
		}
	}
	cout << (dp[0][n][0] + dp[1][n][0] -1+MOD)%MOD << endl;
	return 0;
}

Measured memory_bytes: 16000
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll int64_t
#define all(v) v.begin(), v.end()
#define bitrep(n) for(int bit=0;bit<(1<<n);bit++)

/*A: lかrかs 
B: 赤と青でそれぞれ小さい順で 
C:全部の順列を試す 
D: Aのgcdを求めておいてX毎に1になるところを二分探索*/
int main() {

  ll N,M,ans=0; cin >> N >> M ;
  
  if(2*N<=M){
    ans+=N;
    ll x=M-2*N;
    ans+=(x/4);
  }

  else{
    ans+=(M/2);
  }
  
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <stack>
#include <functional>
#include <math.h>
#include <iomanip>
using namespace std;
#define Would
#define you
const int INF = 999999999;
const int MOD = 1e9 + 7;

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a < b&&b < c) { cout << "Yes"; }
	else { cout << "No"; }
	cout << endl;
}


Measured memory_bytes: 3104
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
    if i < n:
        if a[i] <= b[i]:
            ans  += a[i]
            b[i] -= a[i]
            a[i]  = 0
        else:
            ans  += b[i]
            a[i] -= b[i]
            b[i]  = 0
        if a[i+1] <= b[i]:
            ans   += a[i+1]
            b[i]  -= a[i+1]
            a[i+1] = 0
        else:
            ans    += b[i]
            a[i+1] -= b[i]
            b[i]    = 0
    else:
        if a[i] <= b[i]:
            ans  += a[i]
            b[i] -= a[i]
            a[i]  = 0
        else:
            ans  += b[i]
            a[i] -= b[i]
            b[i]  = 0
print(ans)

Measured memory_bytes: 18476
def cin; gets.split.map(&:to_i) end
def cout(*x); puts x.join(" ") end

# exit if $0 != __FILE__

s = gets.chop

a = s[0..1].to_i
b = s[2..3].to_i

am = (a >= 1 && a <= 12)
bm = (b >= 1 && b <= 12)

if am && bm then
  puts 'AMBIGUOUS'
elsif am then
  puts 'MMYY'
elsif bm then
  puts 'YYMM'
else
  puts 'NA'
end


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

int main(){
int N;
  cin >>N;
  
if(N>63){cout <<"64"<<endl;}
  else if(N>31 &&N<=63){cout <<"32"<<endl;}
  else if(N>15 &&N<=31){cout <<"16"<<endl;}
    else if(N>7 &&N<=15){cout <<"8"<<endl;}
    else if(N>3 && N<=7){cout <<"4"<<endl;}
    else if(N>1 && N<=3){cout <<"2"<<endl;}
  else{cout <<"1"<<endl;}
  



















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































}

Measured memory_bytes: 256
k, a, b = [int(i) for i in input().split()]

if a+2 >= b:
    print(k+1)
elif k == a+1:
    print(b)
elif k < a+1:
    print(k+1)
else:
    if (k-a) % 2 == 0:
        print((k-a) // 2 * (b-a) + a + 1)
    else:
        print((k-a) // 2 * (b-a) + b)


Measured memory_bytes: 2940
// C.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>
using namespace std;

long long  gcd(long long  a, long long b) {

 //   if (a < b)swap(a, b);
    if (b == 0) {
        return a;
    }
    else {
        return gcd(b, a % b);
    }
}
long long lcm(long long a, long long b) {
    return a * b / gcd(a, b);
}

int main()
{
    long long a, b, c, d;

    cin >> a >> b >> c >> d;
    //cout << gcd(2, 8);

    long long l = (a - 1) / c;
    long long r = b / c;
    long long kosuu1 = r - l;

    long long l2 = (a - 1) / d;
    long long r2 = b / d;
    long long kosuu2 = r2 - l2;

    long long sk = lcm(c, d);
   // cout << sk << endl;

    long long l3 = (a - 1) / sk;
    long long r3 = b / sk;
    long long kosuu3 = r3 - l3;

    cout << (b-a+1)-(kosuu1 + kosuu2 - kosuu3) << endl;
    return 0;


}



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

using namespace std;

int K;
int v[10];

const int inf = 1e9;

int dfs(int n) {
	int res = inf;
	if (n == K) return 0;
	if (v[n] == 0) return dfs(n+1);

	int pvn = v[n];
	for (int to = n; to < K; ++to) {
		v[to] = (v[to]-pvn+10)%10;
		res = min(res, dfs(n+1)+1);
	}
	for (int to = n; to < K; ++to) {
		v[to] = (v[to]+pvn+10)%10;
	}

	return res;
}

int main() {
	while ( cin >> K, K ) {
		string s1, s2; cin >> s1 >> s2;
		for (int i = 0; i < K; ++i) {
			v[i] = ((s2[i]-s1[i])+10)%10;
		}
		cout << dfs(0) << endl;
	}
}

Measured memory_bytes: 1204
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)

n = int(input())
nums = list(map(int, input().split()))
ans = 0
l = {}
r = []
for i in range(1,n+1):
    if(i + nums[i-1] not in l):
        l[i+nums[i-1]] =0
    l[i+nums[i-1]]+=1
    if((-1*nums[i-1])+i in l):
        ans+=l[(-1*nums[i-1])+i]
print(ans)


Measured memory_bytes: 112076
n,k = map(int,input().split())
list_0 = []
list_k = []
for a in range(1,n+1):
  if k % 2 == 1:
    if a % k == 0:
      list_0.append(a)
  else:
    if a % k == 0:
      list_0.append(a)
    elif a % k == (k/2):
      list_k.append(a)
ans = len(list_0)**3 + len(list_k)**3
print(ans)

Measured memory_bytes: 11044
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <limits.h>
#include <queue>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)

ll A[100100];
int main(){
    int N;
    cin >> N;
    REP(i,N){
        ll a;
        cin >> a;
        A[i] = a;
    }
    sort(A,A+N);


    ll right, left;
    int num = 1;

    ll ans = 0;
    int head = 0;
    int tail = 1;
    bool put_high = true;
    left = right = A[N-1];
    while(num < N){
        if(N-num < 2){
            num++;
            ll last = A[head];
            if(abs(last - right) > abs(last - left)){
                ans += abs(last - right);
            }else{
                ans += abs(last - left);
            }
        }
        else{
            if(put_high){
                ll x = A[head++];
                ll y = A[head++];
                ans += abs(x-left);
                ans += abs(y-right);
                left = x;
                right = y;
                put_high = false;
                num += 2;
            }else{
                ll x = A[N-1-tail++];
                ll y = A[N-1-tail++];
                ans += abs(x-left);
                ans += abs(y-right);
                left = x;
                right = y;
                put_high = true;
                num += 2;
            }
        }
    }
    ll tmp = ans;


    num = 1;
    ans = 0;
    head = 1;
    tail = 0;
    put_high = false;
    left = right = A[0];
    while(num < N){
        if(N-num < 2){
            num++;
            ll last = A[head];
            if(abs(last - right) > abs(last - left)){
                ans += abs(last - right);
            }else{
                ans += abs(last - left);
            }
        }
        else{
            if(put_high){
                ll x = A[head++];
                ll y = A[head++];
                ans += abs(x-left);
                ans += abs(y-right);
                left = x;
                right = y;
                put_high = false;
                num += 2;
            }else{
                ll x = A[N-1-tail++];
                ll y = A[N-1-tail++];
                ans += abs(x-left);
                ans += abs(y-right);
                left = x;
                right = y;
                put_high = true;
                num += 2;
            }
        }
    }
    ans = max(ans, tmp);
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <vector>
using namespace std;
int main() {
    int N, K;
    cin >> N >> K;
    vector<int> x(N);
    for(int i=0; i<N; i++) cin >> x[i];

    int ans = 1e9;
    for(int l=0; l<N-K+1; l++) {
        int r=l+K-1;
        ans = min(ans, min(abs(x[r]), abs(x[l])) + x[r]-x[l]);
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 640
# coding: utf-8
from collections import deque
N = int(input())
S = input()
Sd = deque(S)
ans = 0
R, G, B = 0, 0, 0
for i in range(N):
    p = Sd.popleft()
    if p == "R":
        R += 1
    elif p == "G":
        G += 1
    else:
        B += 1
ans = R * G * B
for i in range(N):
    for d in range(N):
        j = i + d
        k = j + d
        if k >= N:
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

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

using namespace std;
#define int long long
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int x;
    cin>>x;
    if(x>=400 && x<600)
    {
        cout<<8;
        return 0;
    }
    if(x>=600 && x<800)
    {
        cout<<7;
        return 0;
    }
    if(x>=800 && x<1000)
    {
        cout<<6;
        return 0;
    }
    if(x>=1000 && x<1200)
    {
        cout<<5;
        return 0;
    }
    if(x>=1200 && x<1400)
    {
        cout<<4;
        return 0;
    }
    if(x>=1400 && x<1600)
    {
        cout<<3;
        return 0;
    }
    if(x>=1600 && x<1800)
    {
        cout<<2;
        return 0;
    }
    if(x>=1800 && x<2000)
    {
        cout<<1;
        return 0;
    }
    return 0;
}


Measured memory_bytes: 3652
import sys
# input処理を高速化する
input = sys.stdin.readline
# 入力
S = input()
T = input()


def chmax(a, b):
    if a >= b:
        return a
    else:
        return b


def lcs(s, t):
    dp = [[0 for i in range(len(T) + 1)] for j in range(len(S) + 1)]
    for i in range(len(S)):
        for j in range(len(T)):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = chmax(dp[i + 1][j + 1], dp[i][j] + 1)
            dp[i + 1][j + 1] = chmax(dp[i + 1][j + 1], dp[i + 1][j])
            dp[i + 1][j + 1] = chmax(dp[i + 1][j + 1], dp[i][j + 1])
    ans = ''
    i = len(S)
    j = len(T)
    while (i > 0 and j > 0):
        if dp[i][j] == dp[i - 1][j]:
            i -= 1
        elif dp[i][j] == dp[i][j - 1]:
            j -= 1
        else:
            ans += s[i - 1]
            i -= 1
            j -= 1

    print(ans[::-1])


lcs(S, T)


Measured memory_bytes: 147948
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define F first
#define S second
#define ins insert
#define mp make_pair
#define fo(i, n1, n, x) for(int i = n1; i <= n; i += x)
#define foo(i, n, n1, x) for(int i = n; i >= n1; i -= x)
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

const int N = 2e5 + 11, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 3e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 2e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-6;
const double PI = acos(-1.0);


template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }


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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n,dp[N][4];
string s;
main() {
    file("walk");
    ios;
    cin>>s;
    n=s.size();
    s='#'+s;
    dp[1][1]=1;
    fo(i,2,n,1) {
        if (s[i]!=s[i-1]) dp[i][1]=dp[i-1][1]+1;
        chmax(dp[i][1],dp[i-1][2]+1);
        dp[i][2]=dp[i-2][1]+1;
    }
    cout<<max(dp[n][1],dp[n][2]);
    return 0;
}
/**

*/


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

using namespace std;

double dp[3005][3005];

int main(){
    int N;
    cin >> N;
    dp[0][0] = 1;
    for(int i = 1; i<=N; i++){
        double p;
        cin >> p;
        for(int j = 0; j<=N; j++){
            dp[i][j] += dp[i-1][j]*(1-p);
            dp[i][j+1] += dp[i-1][j]*p;
        }
    }
    double tot = 0;
    for(int i = (N+1)/2; i<=N; i++){
        tot += dp[N][i];
    }
    printf("%.9f", tot);
}


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

using namespace std;

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;

int MOD = 1e9+7;

//ll po(ll b, ll p){ return !p?1:po(b*b%MOD,p/2)*(p&1?b:1)%MOD;}
// ll pomd(ll a, ll k, ll md) { ll p; if (k == 0){ return 1; } p = pomd(a, k / 2, md); p = p * p % md; if (k % 2){ p = p * a % md; } return p; }
// counting divsrs: vi arr(N); for(int i{1}; i<N; ++i) for(int j=i; j<N; j+=i) d[j]++;

#define mp make_pair
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)

void setIO(string name = ""){
    freopen((name+".in").c_str(), "r", stdin); 
    freopen((name+".out").c_str(), "w", stdout);
}	

string to_base(int number, int base){
	string bases = "0123456789ABCDEF";
	string result = "";
	while(number>0){
		result = bases[number%base] + result;
		number /= base;
	}
	return result;
}

int main(){
 	ios_base::sync_with_stdio(0); cin.tie(0); 
	// setIO();
 	int n,k; cin >> n >> k;
 	cout << sz(to_base(n,k)) << "\n";
}





Measured memory_bytes: 3664
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define mfill(x,y) memset(x,y,sizeof(x))
#ifdef LOCAL
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#else
#define eprintf(...) 42
#endif
#define y0 y12345
#define y1 y54321
typedef unsigned long ul;
typedef long long ll;
//typedef pair<int, int> mpair;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b;return 1;}return 0;}
using namespace std;

int main() {
	cin.tie(0);
   	ios::sync_with_stdio(false);
   	int n,m;
	cin>>n>>m;
	if(m==1)cout<<"0"<<endl;
	else cout << n-m << endl;
	return 0;
}


Measured memory_bytes: 256
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int h = in.nextInt();
    int w = in.nextInt();
    int n = in.nextInt();
    System.out.println((int)Math.ceil((double) n / Math.max(h, w)));
  }
}


Measured memory_bytes: 21716
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <limits>
#include <climits>
#include <utility>
#include <algorithm>
#include <numeric>

using namespace std;

using ll = long long;
using ull = unsigned long long;

int main()
{
    int X, Y, Z;
    cin >> X >> Y >> Z;

    int N = (X-Z)/(Y+Z);
    cout << N << endl;

    return 0;
}


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

using namespace std;

int main() {
	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>
using namespace std;

int main (){
  
  int N;
  cin>>N;
  if(N>=64){
    cout<<64<<endl;
  }
  else if (N>=32){
    cout<<32<<endl;
  }
  else if (N>=16){
    cout<<16<<endl;
  }
  else if (N>=8){
    cout<<8<<endl;
  }
  else if (N>=4){
    cout<<4<<endl;
  }
  else if (N>=2){
    cout<<2<<endl;
  }
  else {
    cout<<1<<endl;
  }
}

  
  
  
  
  
  
  
  
  


    

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   ll a,b,c,d;
   cin>>a>>b;
   ll ans=0;
   for(int i=0;i<a;i++)
   {
       cin>>c>>d;
       if(c*c+d*d>b*b)
        ans++;
   }
   cout<<a-ans<<endl;

    return 0;
}


Measured memory_bytes: 3644
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

// template from zscoder

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ll first, last,ans(0);
    cin >> first >> last;
    for(int i = first; i <= last; i++)
    {
        int j = i;
        int a = j%10; j/=10;
        int b = j%10; j/=10;
        int c = j%10; j/=10;
        int d = j%10; j/=10;
        int e = j;
        if(a==e&&b==d)ans++;
    }
    cout << ans << endl;
}

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

int main(){
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for(int i=0;i<N;i++){
    cin >> A.at(i);
  }
  map<int64_t,int> m;
  int64_t temp;
  for(int i=0;i<N;i++){
    temp=i+1+A.at(i);
    if(m.count(temp)){
      m.at(temp)++;
    }else{
      m[temp]=1;
    }
  }
  int64_t ans=0;
  for(int i=0;i<N;i++){
    temp=i+1-A.at(i);
    if(m.count(temp)){
      ans+=m.at(temp);
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 17000
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    max_a2, max_a = sorted(a)[-2:]
    print('\n'.join([str(max_a) if i != max_a else str(max_a2) for i in a]))

if __name__ == '__main__':
    main()


Measured memory_bytes: 24984
A = ['AC', 'WA', 'TLE', 'RE']
N = int(input())
ans = [0]*4
S = []
for _ in range(N):
    S += [input()]

for i in range(len(A)):
    s = A[i]
    temp = 0
    for x in S:
        if x == s:
            temp += 1
    ans[i] = temp

for i in range(len(A)):
    temp = ans[i]
    print(A[i]+" x "+str(temp))

Measured memory_bytes: 79372
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define int long long
using namespace std;

template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}

template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
	rep(i, v.size()) { os << v[i] << (i == v.size() - 1 ? "" : " "); }
	return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
	for (T& x : v) { is >> x; }
	return is;
}

const int M = 1000000007;

signed main() {
	int n, m;
	cin >> n >> m;
	vector<int> a(n), b(m);
	cin >> a >> b;

	{
		set<int> st(all(a));
		if (st.size() != a.size()) {
			cout << 0 << endl;
			return 0;
		}
	}
	{
		set<int> st(all(b));
		if (st.size() != b.size()) {
			cout << 0 << endl;
			return 0;
		}
	}

	sort(all(a));
	sort(all(b));

	int ans = 1;
	for (auto i : a) {
		auto it = lower_bound(all(b), i);
		if (*it == i) continue;
		(ans *= distance(it, b.end())) %= M;
	}
	for (auto i : b) {
		auto it = lower_bound(all(a), i);
		if (*it == i) continue;
		(ans *= distance(it, a.end())) %= M;
	}

	int unique[1005][1005] = {};
	{
		set<int> a_s;
		for (int i = n - 1; i >= 0; i--) {
			a_s.emplace(a[i]);

			set<int> ab_s(a_s);
			for (int j = m - 1; j >= 0; j--) {
				ab_s.emplace(b[j]);
				unique[i][j] = ab_s.size();
			}
		}
	}

	set<int> s;
	int used = 0;
	for (auto i : a) s.emplace(i);
	for (auto i : b) s.emplace(i);
	for (int i = n * m; i >= 1; i--) {
		if (s.count(i)) continue;
		auto dis_a = distance(lower_bound(all(a), i), a.end());
		auto dis_b = distance(lower_bound(all(b), i), b.end());
		(ans *= dis_a * dis_b - unique[n- dis_a][m-dis_b] - used) %= M;
		used++;
	}
	cout << ans << endl;
}


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

int main(){
	int a,b,c,buf;
	cin >> a >> b >> c;
	if(a>b){
		buf=a;
		a=b;
		b=buf;
	}if(b>c){
		buf=b;
		b=c;
		c=buf;
	}if(a>b){
		buf=a;
		a=b;
		b=buf;
	}
	cout << a << " "<< b << " " << c << endl;
	
	return 0;
}


Measured memory_bytes: 3116
#include "bits/stdc++.h"
#include <set>

using namespace std;
#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 rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x)
{
	std::cout << x << " ";
}
template <typename T>

void println(T x)
{
	std::cout << x << "\n";
}

signed main(void) {
	string s;
	cin >> s;
	int n = s.size();
	rep1(i, n-1) {
		if (s[i - 1] == s[i] && s[i] == s[i + 1]) {
			print("Yes");
			return 0;
		}
	}
	print("No");
}

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

// Description: ????????????
// Verifyed: various problem 
using namespace placeholders;
using R=long double;
const R EPS=1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max(x,0.0L));}

const R INF = 1E40L;
const R PI = acos(-1.0L);
using P=complex<R>;
using L=struct{P s,t;};
using VP=vector<P>;
using C=struct{P c;R r;};

auto& operator >> (istream& is,P& p){ R x,y; is >> x >> y,p=P(x,y); return is;}
auto& operator << (ostream& os,P& p){ os << real(p) << " " << imag(p); return os;}

inline R dot(P o,P a,P b){return real(conj(a-o)*(b-o));}
inline R det(P o,P a,P b){return imag(conj(a-o)*(b-o));}
inline P vec(L l){return l.t-l.s;}
auto sdot = bind(sgn,bind(dot,_1,_2,_3));
auto sdet = bind(sgn,bind(det,_1,_2,_3));

//projection verify AOJ CGL_1_A
P proj(L l,P p){ R u=real((p-l.s)/vec(l)); return (1-u)*l.s+u*l.t;}


// farthest point pair Verify AOJ CGL_4_B
R fpp(VP pol){
	int n=pol.size(),i=0,j=0;
	if(n<=2) return abs(pol[0]-pol[1]);
	R res=0.0;

	auto cmp_x=[](P a,P b)->bool{
		int sr = sgn(real(a-b)), si = sgn(imag(a-b));
		return sr ? sr < 0 :si < 0;
	};
	
	rep(k,n){
		if(!cmp_x(pol[i],pol[k]))i=k;
		if(cmp_x(pol[j],pol[k]))j=k;
	}

	int si=i,sj=j;
	while(i!=sj||j!=si){
		res = max(res,abs(pol[i]-pol[j]));
		if(sdet(0,pol[(i+1)%n]-pol[i],pol[(j+1)%n]-pol[j])<0)
			i=(i+1)%n;
		else 
			j=(j+1)%n;
	}
	return res;
}

int main(void){
	int n;
	cin >> n;
	VP pol;
	rep(i,n){
		P p;
		cin >> p;
		pol.push_back(p);
	}
	cout.precision(10);
	cout << fixed << fpp(pol) << endl;
	return 0;
}

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

int main(){
  int n,d;
  cin>>n>>d;
  int x[n][d];
  rep(i,n){
    rep(j,d){
      cin>>x[i][j];
    }
  }
  int dist[50][50];
  rep(i,50){
    rep(j,50){
      dist[i][j]=0;
    }
  }
             
  rep(i,n){
    rep(j,n){
      rep(k,d){
        int dx=(x[i][k]-x[j][k])*(x[i][k]-x[j][k]);
        dist[i][j]=dist[i][j]+dx;
      }
    }
  }
  int ans=0;
  rep(i,n){
    rep(j,n){
      REP(k,1000){
      if(i<j&&k*k==dist[i][j]){
        ans++;
      }
      }
    }
  }
  cout<<ans<<endl;
}

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

int compare_int(const void *a, const void *b){//qsort 整数比較<
	return *(int*)a - *(int*)b;
}
int compare_int2(const void *a, const void *b){//qsort 整数比較>
	return *(int*)b - *(int*)a;
}

//void func(int k,char *s,int i){
//	char *b={"A"};
//	if(i==k){
//		strcat(b,s);
//		strcpy(s,b);
//		strcat(s,"B");
//		strcat(s,"C");
//	}else{
//		strcat(b,s);
//		strcpy(s,b);
//		strcat(s,"B");
//		strcat(s,"C");
//		func(k,s,i+1);
//	}
//}

int main(void){
	int N;

	scanf("%d",&N);
	if(N<600){
		printf("8");
	}else if(N<800){
		printf("7");
	}else if(N<1000){
		printf("6");
	}else if(N<1200){
		printf("5");
	}else if(N<1400){
		printf("4");
	}else if(N<1600){
		printf("3");
	}else if(N<1800){
		printf("2");
	}else{
		printf("1");
	}

	return 0;
}


Measured memory_bytes: 1660
#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <tuple>
#include <bitset>

#include <queue>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <fstream>
#include <random>
//#include <time.h>
#include <ctime>
#pragma endregion //#include
/////////
#define REP(i, x, n) for(int i = x; i < n; ++i)
#define rep(i,n) REP(i,0,n)
/////////
#pragma region typedef
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#pragma endregion //typedef
////定数
const int INF = (int)1e9;
const LL MOD = (LL)1e9+7;
const LL LINF = (LL)1e18;
const double PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;

void solve(){
	int N;
	cin >> N;
	vector< vector<int> > X(N,vector<int>(2));
	for(int i=0;i<N;++i){
		cin >> X[i][0];
		X[i][1] = i+1;
	}
	sort(X.begin(), X.end());
	vector<int> ans(N*N,-1);
	vector<int> at(N+1,N);
	int pos = 0;
	for(int i=0;i<N;++i){
		int No = X[i][1];//対象の数
		int ter = X[i][0];//[ter-1]まででNo個置く
		if( ans[ter-1] != -1 ){
			cout << "No" << endl;return;
		}
		ans[ter-1] = No;
		int nokori = No-1;
		for(;nokori && pos<ter-1;++pos){
			if( ans[pos] == -1 ){
				ans[pos] = No;
				--nokori;
			}
		}
		if( nokori ){
			cout << "No" << endl;return;
		}
		at[No] -= No;//設置した数引く
	}
	//余ってる数を設置する。
	for(int i=0;i<N;++i){
		int No = X[i][1];//対象の数
		int ter = X[i][0];//[ter-1]まででNo個置く
		while( pos <N*N && ans[pos] != -1 ){//WA
			++pos;
		}
		if( pos < ter-1 ){//ter-1より前には置けない
			cout << "No" << endl;return;
		}
		for(;at[No] && pos<N*N;++pos){
			if( ans[pos] == -1 ){
				ans[pos] = No;
				at[No]--;
			}
		}
		if( at[No] ){//
			cout << "No" << endl;return;
		}
	}
	cout << "Yes" << endl;
	for(int i=0;i<N*N;++i){
		if(i)cout << ' ';
		cout << ans[i];
	}cout << endl;
}
#pragma region main
signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別	
	
	solve();
}
#pragma endregion //main()


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

set<int> edges[100000];
bitset<100000> deleted, odd;

vector<int> calc(int N, int st){
    vector<int> dist(N, -1);
    queue<int> que;
    que.push(st);
    dist[st] = odd[st];
    while(que.size()){
        int i = que.front(); que.pop();
        for(int j : edges[i]){
            if(deleted[j] || dist[j] >= 0) continue;
            dist[j] = dist[i] + odd[j];
            que.push(j);
        }
    }
    return dist;
}

int main(){
    int N;
    cin >> N;
    for(int i=0; i<N-1; i++){
        int a, b;
        cin >> a >> b;
        edges[a-1].insert(b-1);
        edges[b-1].insert(a-1);
    }
    string C;
    cin >> C;

    queue<int> que;
    int live = N;
    for(int i=0; i<N; i++) if(C[i] == 'B' && edges[i].size() <= 1) que.push(i);
    while(que.size()){
        int i = que.front(); que.pop();
        if(deleted[i]) continue;
        deleted[i] = true;
        live--;
        for(int j : edges[i]){
            edges[j].erase(i);
            if(C[j] == 'B' && edges[j].size() <= 1) que.push(j);
        }
    }

    if(live <= 1){
        cout << live << endl;
        return 0;
    }

    int mx = 2*(live-1);
    for(int i=0; i<N; i++){
        if(deleted[i]) continue;
        odd[i] = (C[i] == 'W') ^ (edges[i].size()%2);
        if(odd[i]) mx++;
    }

    int st = 0;
    while(deleted[st]) st++;
    auto d1 = calc(N, st);
    st = max_element(d1.begin(), d1.end()) - d1.begin();
    auto d2 = calc(N, st);
    int save = 2 * (*max_element(d2.begin(), d2.end()));

    int ans = mx - save;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 15580
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;

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

int main(){
    ll n;
    cin >> n;
    vector<pair<ll,ll>> l(n);
    rep(i,n){cin >> l[i].first;l[i].second = i;l[i].first--;}

    ll i=0;
    sort(l.begin(),l.end(),[](pair<ll,ll> & a,pair<ll,ll> & b){return a.first < b.first;});
    while(i < n){
        cout << (l[i].second + 1) << " ";
        ++i;
    }
    cout << endl;
}

Measured memory_bytes: 2432
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = [i for i in range(n)]
        self.rank = [0 for i in range(n)]
        self.siz = [1 for i in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        self.par[x] = self.find(self.par[x])
        return self.find(self.par[x])

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.rank[x] > self.rank[y]:
            self.par[y] = x
            self.siz[x] += self.siz[y]
        else:
            self.par[x] = y
            self.siz[y] += self.siz[x]
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1
    
    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return self.siz[self.find(x)]
    
n, m = map(int, input().split())
uf = UnionFind(n)
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    uf.unite(a, b)

res = 0
for i in range(n):
    res = max(res, uf.size(i))

print(res)

Measured memory_bytes: 20192
//InTheNameOfGod
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,j,k) for(int i=j;i<k;++i)
#define rrep(i,j,k) for(int i=j;i>=k;i--)
#define pb push_back
#define lb(i) i&(-i);
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int maxn =2e5+5;
int n,p[maxn],pos[maxn],dp[maxn],re;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    cin>>n;
    rep(i,0,n)
    {
        cin>>p[i];
        pos[p[i]]=i;
    }
    rep(i,0,n)
    {
        dp[i]=1;
        if(p[i]!=1&&pos[p[i]-1]<i)dp[i]=dp[pos[p[i]-1]]+1;
        re=max(re,dp[i]);
    }
    cout<<n-re;
    return 0;
}


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

int N, Q;
string s;
vector<string> t, d;

bool vanish(int n, int m){
    int x = n-1;
    int L = s.length();
    for(int i=0; i<Q; i++){
        if(t[i][0]==s[x]){
            x += (d[i][0]=='R'?1:-1);
            if(x==-1 || x==L) break;
        }
    }

    if(x==-1 && m==0){
        return true;
    }else if(x==L && m==1){
        return true;
    }else{
        return false;
    }
}

int main(){    
    cin >> N >> Q >> s;
    t = vector<string>(Q);
    d = vector<string>(Q);
    for(int i=0; i<Q; i++) cin >> t[i] >> d[i];

    int l1 = 0, r1 = N+1;    
    int Lleft = 0, Lright = N+1;
    while(r1 - l1 > 1){
        int mid = l1 + (r1 - l1)/2;
        if(vanish(mid, 0)){
            l1 = mid;
        }else{
            r1 = mid;
        }
    }

    int l2 = 0, r2 = N+1;
    while(r2 - l2 > 1){
        int mid = l2 + (r2 - l2)/2;
        if(vanish(mid, 1)){
            r2 = mid;
        }else{
            l2 = mid;
        }
    }
    
    if(l2 >= r1){
        cout << l2 - r1 + 1 << endl;
    }else{
        cout << 0 << endl;
    }
}


Measured memory_bytes: 24196
import Control.Applicative
import Control.Monad
import Data.List

f k ((a, b):xs)
    | k <= b    = a
    | otherwise = f (k - b) xs

main :: IO ()
main = do
    [n, k] <- fmap read . words <$> getLine :: IO [Int]
    as <- fmap (sortOn fst) . replicateM n $ do
        [a, b] <- fmap read . words <$> getLine :: IO [Int]
        return (a, b)
    print (f k as)

Measured memory_bytes: 92028
from math import factorial
N, K = map(int, input().split())

ans = 0

def comb(n, r):
    if n < r or r < 0:return 0
    else:return factorial(n) // (factorial(n - r) * factorial(r))  
    
print(N - K + 1)
for j in range(2, K+1):
    b_per = comb(K - 1, j - 1)
    r_per1 = comb(N - K - 1, j - 2)
    r_per2 = comb(N - K - 1, j - 1) * 2
    r_per3 = comb(N - K - 1, j)
    ans = (b_per * (r_per1 + r_per2 + r_per3)) % (10**9 + 7)
    
    print(ans)

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

bool check(int a)
{
    if(a==1)return true;
    for(int i=2;i<a;++i)
    {
        for(int j=2;pow(i,j)<=a;++j)
        {
            if(pow(i,j)==a)return true;
        }
    }
    return false;
}

int main()
{
    int n;
    cin >> n;
    for(int i=n;i>=1;--i)
    {
        if(check(i))return printf("%d\n",i),0;
    }
}

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

int ara[100000];
int dp[100000];

int f(int i, int n)
{
    if(i == n) return 0;
    if(dp[i] != -1)
        return dp[i];
    int cost = f(i+1,n) + abs(ara[i] - ara[i+1]);
    if(i+2<=n)
        cost = min(cost, f(i+2,n)+abs(ara[i] - ara[i+2]));
    dp[i] = cost;

    return cost;
}

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

   for(int i = 1; i <= n; i++)
   {
       cin>>ara[i];
   }

   memset(dp,-1,sizeof(dp));
   cout<<f(1,n)<<endl;

   return 0;
}




Measured memory_bytes: 5760