akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v
#define NUM(x,v) (POSU(x,v)-POSL(x,v))
const ll INF=1e9+7;
const ll N = 100002;
ll n,p=0,g=0,ans=0;
string s;

int main(){
	cin>>s;
	n=s.size();
	FOR(i,0,n){
		if(s[i]=='g'&&(g>p)){
			p++;
			ans++;
		}else if(s[i]=='g'){
			g++;
		}
		if(s[i]=='p'){
			if(g>p){
				p++;
			}else{
				g++;
				ans--;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

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

const int MAX = 1000000;

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;
}

int main()
{
	COMinit();
	int X,Y;
	cin >> X >> Y;
	int a = 2*X - Y, b = 2*Y - X;
	if (a < 0 || b < 0)
	{
		cout << "0" << endl;
		return 0;
	}

	a /= 3;
	b /= 3;

	if ((a * 3 != 2*X - Y) || (b * 3 != 2*Y - X))
	{
		cout << "0" << endl;
		return 0;
	}

	if (a == 0)
	{
		cout << "1" << endl;
		return 0;
	}
	else if (b == 0)
	{
		cout << "1" << endl;
		return 0;
	}
	else
	{
		ll ans = 1;
		int c = min(a,b);
		//cout << a << " " << b << " " << c << endl;

		cout << COM(a+b,c) << endl;
		/*
		for (ll i = 1; i <= c; ++i)
		{
			ans = (((ans * (a+b-c+i)) % MOD) / i ) % MOD;
		}
		cout << ans << endl;
		*/
	}
	return 0;
}

Measured memory_bytes: 23680
#include <iostream>
#include <queue>

using namespace std;

const int MAX = 100007;
int n, a, b, A[MAX];

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> a >> b; a -= b;
    for (int i = 0; i < n; ++i) cin >> A[i];

    int lo = 0, hi = 1e9;
    while (lo < hi) {
		int md = lo + ((hi - lo) >> 1);

        int64_t cnt = 0;
        for (int i = 0; i < n; ++i) if (A[i] > 1ll * md * b) {
			cnt += (A[i] - 1ll * md * b + a - 1) / a;
        }
        if (cnt > md) lo = md + 1;
        else hi = md;
    }
    cout << lo;
}

/*

priority_queue<int> pq;
int n, a, b, cnt;

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> a >> b; a -= b;
	for (int i = 0; i < n; ++i) { int x; cin >> x; pq.push(x); }

    while (pq.top() > cnt * b) {
        int x = pq.top();
        pq.pop();
        pq.push(x - a);
        ++cnt;
    }

    cout << cnt;
}

*/


Measured memory_bytes: 640
import sys
sys.setrecursionlimit(10 ** 6)

D,G=map(int,input().split())
p=[]
c=[]
for i in range(D):
  a,b=map(int,input().split())
  p += [a]
  c += [b]

def solve(bit):
  if bit >= (1<<D):
    return 1000
  p_sum=0
  num=0
  for i in range(D):
    if bit & (1<<i):
      p_sum += c[i] + p[i] * 100*(i+1)
      num += p[i]
  if p_sum >= G:
    return min(num,solve(bit+1))
  else:
    for i in reversed(range(D)):
      if bit & 1<<i:
        continue
      for j in range(p[i]):
        if p_sum >= G:
          break
        p_sum += 100*(i+1)
        num += 1
      else:
        return solve(bit+1)
    return min(num,solve(bit+1))

print(solve(0))

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

struct Dice {
    int a, b, c, d, e, f;
    Dice(int a, int b, int c, int d, int e, int f) {
        setFaces(a, b, c, d, e, f);
    }
    void setFaces(int a, int b, int c, int d, int e, int f) {
        this->a = a;
        this->b = b;
        this->c = c;
        this->d = d;
        this->e = e;
        this->f = f;
    }
    void rotate(char dir) {
        switch (dir) {
            case 'N':
                setFaces(b, f, c, d, a, e);
                break;                
            case 'E':
                setFaces(d, b, a, f, e, c);
                break;                
            case 'S':
                setFaces(e, a, c, d, f, b);
                break;                
            case 'W':
                setFaces(c, b, f, a, e, d);
                break;
            case 'L':
                setFaces(a, d, b, e, c, f);
                break;
            case 'R':
                setFaces(a, c, e, b, d, f);
                break;
        }
    }
    void roll(string s) {
        for (int i=0; i<s.size(); i++)
            rotate(s[i]);
    }
    void alignTop(int top) {
        if (a != top) roll("N");
        if (a != top) roll("N");
        if (a != top) roll("N");
        if (a != top) roll("E");
        if (a != top) roll("EE");
    }
    void alignTopFront(int top, int front) {
        alignTop(top);
        if (b != front) roll("L");
        if (b != front) roll("L");
        if (b != front) roll("L");
    }
};

int main() {
    int a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    Dice dice(a, b, c, d, e, f);

    int n, x, y;
    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> x >> y;
        dice.alignTopFront(x, y);
        cout << dice.c << endl;
    }
}



Measured memory_bytes: 3100
import cmath
import itertools
# cmath.phase:C to (-pi,pi]
n = int(input())
C = sorted([complex(*map(int,input().split())) for _ in range(n)], key=cmath.phase)
score = 0
for i,j in itertools.product(range(n),range(n)):
    if i>j: continue # i+1<=j<=nでループ回すのをサボった
    score = max(score,abs(sum(C[:i]+C[j:])))
    score = max(score,abs(sum(C[i:j])))
print(score)

Measured memory_bytes: 40560
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <locale>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int main() {
    cin.tie(0);

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

    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define PRIME1 99999883
#define PRIME2 99999893
#define EPS 0.00000001
using namespace std;

struct Info{
	char title[51],author[51];
	int year,month,day;
};

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

bool partialCmp(char* base, char* comp){

	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 < length2)return false;

	if(length2 == 1 && comp[0] == '*')return true;

	bool FLG;

	int limit = length1-length2;

	for(int left=0;left <= limit; left++){
		FLG = true;
		for(int i = 0; i < length2; i++){
			if(base[left+i] != comp[i]){
				FLG = false;
				break;
			}
		}
		if(FLG)return true;
	}
	return false;
}

int main(){

	int N;
	scanf("%d",&N);
	Info info[N];

	char TITLE[51],AUTHOR[51],P_DATE[11];

	for(int i = 0; i < N; i++){
		scanf("%s %s %s",TITLE,AUTHOR,P_DATE);
		strcpy(info[i].title,TITLE);
		strcpy(info[i].author,AUTHOR);

		info[i].year = 1000*(P_DATE[0]-'0') + 100*(P_DATE[1]-'0') + 10*(P_DATE[2]-'0')+(P_DATE[3]-'0');
		info[i].month = 10*(P_DATE[5]-'0')+(P_DATE[6]-'0');
		info[i].day = 10*(P_DATE[8]-'0')+(P_DATE[9]-'0');
	}

	int Q;

	scanf("%d",&Q);

	char Q_title[51],Q_author[51],Q_from[11],Q_to[11];

	int from_year,from_month,from_day,to_year,to_month,to_day;

	for(int loop = 0; loop < Q; loop++){

		scanf("%s %s %s %s",Q_title,Q_author,Q_from,Q_to);

		if(Q_from[0] == '*'){
			from_year = 0;
		}else{
			from_year = 1000*(Q_from[0]-'0') + 100*(Q_from[1]-'0') + 10*(Q_from[2]-'0')+(Q_from[3]-'0');
			from_month = 10*(Q_from[5]-'0')+(Q_from[6]-'0');
			from_day = 10*(Q_from[8]-'0')+(Q_from[9]-'0');
		}

		if(Q_to[0] == '*'){
			to_year = 9999;
		}else{
			to_year = 1000*(Q_to[0]-'0') + 100*(Q_to[1]-'0') + 10*(Q_to[2]-'0')+(Q_to[3]-'0');
			to_month = 10*(Q_to[5]-'0')+(Q_to[6]-'0');
			to_day = 10*(Q_to[8]-'0')+(Q_to[9]-'0');
		}

		if(loop > 0)printf("\n");

		for(int i = 0; i < N; i++){
			if(partialCmp(info[i].title,Q_title) == true && partialCmp(info[i].author,Q_author) == true){ //??????????????¨???????????¨????????´???OK
				if((from_year < info[i].year) || (from_year == info[i].year && from_month < info[i].month) ||
						(from_year == info[i].year && from_month == info[i].month && from_day <= info[i].day)){ //from??\???
					if((to_year > info[i].year) || (to_year == info[i].year && to_month > info[i].month) ||
							(to_year == info[i].year && to_month == info[i].month && to_day >= info[i].day)){
						printf("%s\n",info[i].title);
					}
				}
			}
		}
	}

    return 0;
}

Measured memory_bytes: 3168
n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
now = a[0]
for i in range(n-1):
    if now == a[i+1]:
        continue
    else:
        b.append(a[i+1])
        now = a[i+1]
n = len(b)
if n == 1:
    print(1)
    exit()
i = 0
ans = 0
while i < n-1:
    s = b[i]
    t = b[i+1]
    if s < t:
        state = 1
    else:
        state = 0
    temp = state
    while True:
        if state:
            if b[i] < b[i+1]:
                i += 1
            else:
                ans += 1
                i += 1
                break
        else:
            if b[i] > b[i+1]:
                i += 1
            else:
                ans += 1
                i += 1
                break
        if i == n-1:
            break

print(ans+1)



Measured memory_bytes: 14480
class Array
  def gcd
    self.inject{|a,b| a.gcd(b)}
  end
end

n, init = gets.chomp.split.map(&:to_i)
x = gets.chomp.split.map(&:to_i)
x << init
x.sort!
# 全部の値の距離を出して、最大公約数を求める
ds = []
x.size.times do |i|
  next if i == 0
  ds << (x[i-1] - x[i]).abs
end
puts ds.gcd



Measured memory_bytes: 11376
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_C main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i,s,e) for (int i = int(s); i < int(e); ++i)
#define REP(i,e) FOR(i,0,e)
#define INF (INT_MAX/2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX/2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long; using ull = unsigned long long;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vl = vector<ll>; using vvl = vector<vl>;
using vd = vector<double>; using vs = vector<string>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
// functions
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a%b);
}
#endif


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

    int N; cin >> N;
    vl A(N); REP(i, N) cin >> A[i];

    // gl[i] in [0,i]
    vi gl(N); 
    gl[0] = A[0];
    FOR(i,1,N) {
        gl[i] = gcd(gl[i - 1], A[i]);
    }
    // gr[i] in [i,N-1]
    vi gr(N); 
    gr[N-1] = A[N-1];
    for (int i = N - 2; i >= 0; i--) {
        gr[i] = gcd(gr[i + 1], A[i]);
    }

    ll ans = max(gl[N-2], gr[1]);
    FOR(i,1,N-1) {
        ans = max(ans, gcd(gl[i - 1], gr[i + 1]));
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1792
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 100006
#define LL long long
#define INF 1000000000
int n,ans,cnt;
char s[MAXN];
int main()
{
	scanf("%s",s);
	int len=strlen(s);
	for(int i=0;i<len;i++)
	{
		if(s[i]=='g')
		{
			if(cnt) cnt--,ans++;
			else cnt++;
		}
		else if(s[i]=='p')
		{
			if(cnt) cnt--;
			else cnt++,ans--;
		}
	}
	printf("%d\n",ans);
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int fun(int n)
{
    if(n>0)
        return n;
    else
        return -n;
}
int main()
{
    int a[10],sum;
    while(scanf("%d%d%d",&a[0],&a[1],&a[2])==3)
    {
        sum=0;
        sort(a,a+3);
        for(;a[2]-a[1]>1;)
        {
            a[1]=a[1]+2;
            //printf("%d\n",a[1]);
            sum++;
        }
        for(;a[1]-a[0]>1;)
        {
            a[0]=a[0]+2;
            //printf("%d\n",a[0]);
            sum++;
        }
        sort(a,a+3);
        while(1)
        {
            if(a[0]==a[1]&&a[1]==a[2])
                break;
            else
            {
               // printf("%d %d\n",a[0],a[1]);
                a[0]++,a[1]++;
                sort(a,a+3);
                sum++;
            }

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


Measured memory_bytes: 128
n = int(input())
m = n
a = 1
count = 1
while a <= n:
    if a*26 >= m:
        break
    a*= 26
    m -= a
    count+=1
m -= 1
ans = []
for i in range(count):
    ans.append(m%26)
    m //= 26
ansa = ""
for i in ans:
    ansa += chr(97+i)
print(ansa[::-1])

Measured memory_bytes: 9184
#include<bits/stdc++.h>
using namespace std;
int main(){
    int A, B, C, D, N, ans=0;
    vector<int> V(3);
    char tmp1, tmp2;
    string S;
    bool flag=true;
    cin >> tmp1 >>tmp2;

    if(tmp1=='D')
	flag = !flag;
    if(tmp2=='D')
	flag = !flag;

    if(flag)
    	puts("H");
    else
	puts("D");
    
    // cout <<ans<<endl;
    

    // string S;
    // cin >>S;
    // cout << S + "pp"<<endl;
    // if(A+B==C || B+C==A || C+A==B)
    // 	puts("Yes");
    // else
    // 	puts("No");
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
const double PI = acos(-1.0);
const double eps = 1e-10;
int dcmp(double x) { if(fabs(x) < eps) return 0; else return x<0?-1:1; }
const int N = 1e5+5;
const ll MOD = 1e9+7;

int a[N];
ll s1[2*N], s2[2*N];

int main() {
    memset(s1, 0, sizeof(s1));
    memset(s2, 0, sizeof(s2));
    int n, m, l, r;
    ll ans = 0;
    scanf("%d%d%d", &n, &m, &l);
    for(int i = 1; i < n; i++) {
        scanf("%d", &r);
        if(l > r) r += m;
        ans += r-l;
        s1[l+1]++, s1[r+1]--;
        s2[l+1] += l+1, s2[r+1] -= l+1;
        l = r%m;
    }
    for(int i = 1; i <= 2*m; i++) s1[i] += s1[i-1], s2[i] += s2[i-1];
    ll maxa = 0;
    for(int i = 1; i <= m; i++) {
        ll tmp = s1[i]*(ll)i-s2[i]+s1[i+m]*(ll)(i+m)-s2[i+m];
        maxa = max(tmp, maxa);
    }
    printf("%lld\n", ans-maxa);
    return 0;
}


Measured memory_bytes: 3328
// =========
use std::cmp::{max, min};
use std::collections::{HashMap, HashSet};
use std::process::exit;

const MOD: usize = 1000000007;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    // var... 変数の識別子, $t...型を一つよむ
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        //ここで繰り返し
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    //
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    // 配列の最後のNestではここで型が指定されてparseされる
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// =========
fn gcd(a: usize, b: usize) -> usize {
    if b == 0 {
        return a;
    } else if a < b {
        return gcd(a, b % a);
    } else {
        return gcd(b, a % b);
    }
}

fn main() {
    input! {
        a: usize,
        b: usize,
    }
    println!("{:?}", a * b / gcd(a, b));
}


Measured memory_bytes: 4352
require 'pp'
# サイズが H, W の文字の2次元配列を返却する
def gets_HW_char
  h, w = gets.split.map(&:to_i)
  board = h.times.map { gets.chomp.chars }
  return h, w, board
end

H, W, b = gets_HW_char

b1 = []
b.each do |row|
  b1.push(row) if row.include?("#")
end

b2 = []
b1.transpose.each do |line|
  b2.push(line) if line.include?("#")
end

b2.transpose.each do |row|
  puts row.join
end


Measured memory_bytes: 2684
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <climits>
#include <cmath>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
using namespace std;

#define pb push_back
#define fst first
#define snd second

typedef long long ll;
typedef pair<int,int> pii;
template<typename T> using min_queue=priority_queue<T,vector<T>,greater<T>>;

const ll MOD=1e9+7;

int N;
int f[111][10];
ll p[111][11];

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

  cin>>N;
  for(int i=0;i<N;i++) for(int j=0;j<10;j++) cin>>f[i][j];
  for(int i=0;i<N;i++) for(int j=0;j<=10;j++) cin>>p[i][j];

  ll res=-1e9;
  for(int m=1;m<(1<<10);m++) {
    ll tmp=0;
    for(int i=0;i<N;i++){
      int c=0;
      for(int j=0;j<10;j++) if((m&(1<<j)) && f[i][j]) c++;
      tmp += p[i][c];
    }
    res=max(res,tmp);
  }
  cout<<res<<endl;

  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
int ball[11],r[11],l[11],flag;
void senten(int x,int y,int k)
{
	if(x==10)
	{
		flag=1;
		return ;
	}	
	if(flag)
	{
	return;
	}
	if(ball[x]>r[y-1])
		{
			r[y]=ball[x];
		senten(x+1,y+1,k);
		}
	if(ball[x]>l[k-1])
	{
	l[k]=ball[x];
	senten(x+1,y,k+1);
	}

}
	
int main()
{
	int t;
	scanf("%d",&t);
	for(int i=0;i<t;i++)
	{
		memset(r,0,sizeof(r));
		memset(l,0,sizeof(l));
		flag=0;
		for(int j=0;j<10;j++)
			scanf("%d",&ball[j]);
		 senten(0,1,1);
		 if(flag)
		 printf("YES\n");
		 else
		 printf("NO\n");
	}
}
	

Measured memory_bytes: 1660
#include <iostream>
#include <string>

using namespace std;

string hantei(int);

main(){
  int n, in;
  while(1){
    cin >> n;
    if(n==-1) break;
    string now="0000000";
    for(int i=0;i<n;i++){
      cin >> in;
      string tmp=hantei(in);
      string out="";
      for(int j=0;j<7;j++){
	if(now[j]==tmp[j]) out+='0';
	else{
	  out+='1';
	}
      }
      cout << out << endl;
      now=tmp;
    }
  }
  return 0;
}


string hantei(int n){
  if(n==0)
    return "0111111";
  if(n==1)
    return "0000110";
  if(n==2)
    return "1011011";
  if(n==3)
    return "1001111";
  if(n==4)
    return "1100110";
  if(n==5)
    return "1101101";
  if(n==6)
    return "1111101";
  if(n==7)
    return "0100111";
  if(n==8)
    return "1111111";
  if(n==9)
    return "1101111";
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
#define rep(i,n) for (ll i=0;i<(n);++i)
#define rep2(i,a,b) for (ll i=(a);i<(b);++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(),(v).end()
const ll MOD=1e9+7;
//const ll MOD=998244353;
const ll INF=1e9;
const ll IINF=1e18;

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    vector<string> S(N);
    for (int i=0;i<N;++i) cin >> S[i];
    int ans=0,cntA=0,cntB=0,cntAB=0;
    for (int i=0;i<N;++i){
        int s=S[i].size();
        for (int j=0;j+1<s;++j){
            if (S[i][j]=='A'&&S[i][j+1]=='B') ++ans;
        }
        if (S[i][0]=='B'&&S[i][s-1]=='A') ++cntAB;
        else if (S[i][0]=='B') ++cntB;
        else if (S[i][s-1]=='A') ++cntA;
    }
    if (cntAB){
        ans+=cntAB-1;
        if (cntA) ++ans,--cntA;
        if (cntB) ++ans,--cntB;
    }
    ans+=min(cntA,cntB);
    cout << ans << '\n';
}

Measured memory_bytes: 768
#include<bits/stdc++.h>
namespace csx_std {
	using namespace std;
	typedef long long ll;
	#define FOR(i,a,b) for (register int i=(a);i<=(b);i++)
	#define For(i,a,b) for (register int i=(a);i>=(b);i--)
	#define mem(i,j) memset(i,j,sizeof(i))
	#define pii pair<int,int>
	#define MP make_pair
	#define fi first
	#define se second
	#define GO(u) for (register int j=f[u];j!=-1;j=nxt[j])
	const int N=1e5+5;
	const int mod=1e9+7;
	const double eps=1e-9;
	const double pi=3.1415926535897;
	inline int qpow(int x,int y) {int ret=1;for (;y;y>>=1,x=1LL*x*x%mod) if (y&1) ret=1LL*ret*x%mod;return ret;}
	inline int Inv(int x) {return qpow(x,mod-2);}
	inline void upd(int &x,int y) {x=(1LL*x+y)%mod;return;}
	inline int chkmax(int &x,int y) {return (x<y)?(x=y,1):0;}
	inline int chkmin(int &x,int y) {return (x>y)?(x=y,1):0;}
	inline int read()
	{
		int x=0,f=1;
		char c=getchar();
		while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
		while (c>='0'&&c<='9') {x=(x<<1)+(x<<3)+c-'0';c=getchar();}
		return f*x;
	}
	inline void write(int x)
	{
		if (x<0) x=-x,putchar('-');
		if (x>9) write(x/10);
		putchar(x%10+'0');
		return;
	}
}
using namespace csx_std;
double a,b,x,ans;
int main()
{
	scanf("%lf%lf%lf",&a,&b,&x);
	x/=a;
	if (2*x<a*b) ans=atan((b*b)/(2*x))*180/pi;
	else x=a*b-x,ans=atan((2*x)/(a*a))*180/pi;
	printf("%.7lf\n",ans);
	return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
#include <climits>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define srep(i, begin, end) for (__typeof(end) i = begin; i != end; i++)
#define si(x) int x = scanInt();
#define sll(x) LL x = scanLong();
#define sci(x) int x; scanf("%d", &x);
#define scll(x) LL x; scanf("%lld", &x);
#define pi(x) printf("%d ", x)
#define pll(x) printf("%lld ", x)
#define nl printf("\n")
#define clr(a) memset(a, 0, sizeof(a))
#define PB push_back
#define MP make_pair
using namespace std;
typedef unsigned int UI; // 32 bit integer
typedef long int LI; // 32 bit integer
typedef unsigned long int ULI; // 32 bit unsigned integer
typedef long long int LL; // 64 bit integer
typedef unsigned long long int ULL; // 64 bit unsigned integer
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef deque<int> DI;
typedef deque<LL> DLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const LL MOD = 1e9+7;

/* Fast I/O */
inline int scanInt() {
	int n = 0;
	char ch = getchar();
	int sign = 1;
	while(ch < '0' || ch > '9') {
		if(ch == '-')	sign = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		n = (n<<1)+(n<<3)+(int)(ch-'0');
		ch = getchar();
	}
	return n*sign;
}

inline LL scanLong() {
	LL n = 0;
	char ch = getchar();
	LL sign = 1;
	while(ch < '0' || ch > '9') {
		if(ch == '-')	sign = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		n = (n<<1)+(n<<3)+(LL)(ch-'0');
		ch = getchar();
	}
	return n*sign;
}

const LL MAXN = 1e6+10;
LL N, M;
string grid[MAXN];
LL dist[MAXN];
deque<LL> Q;

LL getIndex(LL i, LL j) { return (i*M + j + 1); }


int main() {
	N = scanLong(); M = scanLong();
	rep(i, 0, N)	cin >> grid[i];
	rep(i, 0, (LL)MAXN)	dist[i] = LONG_MAX;
	rep(i, 0, N) {
		rep(j, 0, M) {
			if(grid[i][j] == '#')	Q.push_back(getIndex(i, j));
		}
	}
	rep(it, Q.begin(), Q.end())	dist[*it] = 0;


	while(!Q.empty()) {
		LL curr = Q.front(); Q.pop_front();
		LL col = (curr - 1) % M;
		LL row = (curr - col - 1) / M;
		if(row > 0 && grid[row-1][col] == '.') {
			grid[row-1][col] = '#';
			Q.push_back(getIndex(row-1, col));
			dist[getIndex(row-1, col)] = dist[curr] + 1;
		}
		if(row+1 < N && grid[row+1][col] == '.') {
			grid[row+1][col] = '#';
			Q.push_back(getIndex(row+1, col));
			dist[getIndex(row+1, col)] = dist[curr] + 1;
		}
		if(col > 0 && grid[row][col-1] == '.') {
			grid[row][col-1] = '#';
			Q.push_back(getIndex(row, col-1));
			dist[getIndex(row, col-1)] = dist[curr] + 1;
		}
		if(col+1 < M && grid[row][col+1] == '.') {
			grid[row][col+1] = '#';
			Q.push_back(getIndex(row, col+1));
			dist[getIndex(row, col+1)] = dist[curr] + 1;
		}
	}
	LL ans = 0;
	rep(i, 0, N) {
		rep(j, 0, M) {
			ans = max(ans, dist[getIndex(i, j)]);
		}
	}
	pll(ans); nl;
}


Measured memory_bytes: 25104
#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)
#define Pr(a) (#a)<<"="<<(a)<<" "
#define pr1(a) cerr<<Pr(a)<<endl;
#define pr2(a,b) cerr<<Pr(a)<<Pr(b)<<endl;
#define pr3(a,b,c) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<endl;
#define pr4(a,b,c,d) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<endl;
#define pr5(a,b,c,d,e) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<endl;
#define pr6(a,b,c,d,e,f) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<Pr(f)<<endl;
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}
      
signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int h,w;
  cin>>h>>w;
  vector<string> A(h);
  cin>>A;
  
  int cnt[26]={};
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++) cnt[A[i][j]-'a']++;

  int a = 0, b = 0;
  for(int i=0;i<26;i++){
    if(cnt[i]%4 == 0) continue;
    if(cnt[i]%2 == 0) a+=2;
    else b++;
  }

  int ans = 1;
  if(h % 2 && w % 2){
    if(b > 1 || (h+w-2) < a) ans = 0;
  }
  else if(h % 2 || w % 2){
    if(b || a > (w%2)*h + (h%2)*w) ans = 0;
  }
  else if(a || b) ans = 0;
  
  cout<<(ans? "Yes":"No")<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
class Card {
  public:
    char type;
    int number;
    Card(char _type, int _number) {
      type = _type;
      number = _number;
    }
    // Card(const Card &original) { // Copy Constractor
      // type = new char[1];
      // type = original.type;
      // number = new int[1];
      // number = original.number;
    // }
    string str() {
      stringstream ss;
      ss << type << number;
      // cout << ss.str() << endl;
      // return ss.str();
      return ss.str();
    }
    bool operator > (const Card &right) {
      return (number > right.number);
    }
    bool operator < (const Card &right) {
      return (number < right.number);
    }
    bool operator >= (const Card &right) {
      return (number >= right.number);
    }
    bool operator <= (const Card &right) {
      return (number <= right.number);
    }
    bool operator == (const Card &right) {
      return (number == right.number);
    }
    bool operator != (const Card &right) {
      return (number != right.number);
    }
};
 
void pResult(vector<Card> &, vector<Card> &);
vector<Card> BubbleSort(vector<Card>);
vector<Card> SelectionSort(vector<Card>);
string cardsToStr(vector<Card> &);
 
 
int main() {
  int n;
  cin >> n;
  vector<Card> cards;
  for(int i = 0; i < n; ++i) {
    string s_card;
    cin >> s_card;
    char c_card;
    int d_card;
    stringstream ss;
    ss << s_card;
    ss >> c_card;
    ss >> d_card;
    Card card(c_card, d_card);
    cards.push_back(card);
  }
  vector<Card> result1 = BubbleSort(cards);
  vector<Card> result2 = SelectionSort(cards);
  pResult(result1, result1);
  pResult(result2, result1);
  return 0;
}
 
void pResult(vector<Card> &sorted, vector<Card> &original) {
  string s_result = cardsToStr(sorted);
  cout << s_result << endl;
  cout << ((s_result == cardsToStr(original)) ? "Stable" : "Not stable") << endl;
}
 
string cardsToStr(vector<Card> &cards) {
  stringstream result;
  for(auto i = cards.begin(); i != cards.end(); ++i) {
    result << (*i).str();
    // cout << (*i).str() << endl;
    // cout << cards[0].str() << endl;
    if(i != cards.end() - 1) result << ' ';
  }
  return result.str();
}
 
vector<Card> BubbleSort(vector<Card> cards) {
  // result.resize(cards.size());
  // copy(cards.begin(), cards.end(), result.begin());
  for(int i = 0; i < cards.size(); ++i) {
    for(int j = cards.size() - 1; j > 0; --j) {
      if(cards[j] < cards[j - 1]) swap(cards[j], cards[j - 1]);
    }
  }
  return move(cards);
}
 
vector<Card> SelectionSort(vector<Card> cards) {
  for(auto i = cards.begin(); i != cards.end(); ++i) {
    auto itr_min = min_element(i, cards.end());
    iter_swap(i, itr_min);
  }
  return move(cards);
}


Measured memory_bytes: 3200
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>

#define rep(i, N) for(int (i) = 0; (i) < (N); (i) ++)
#define INF 1 << 30
#define MIN_INF (-1)
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 (b<a) { a = b; return 1; } return 0; }
typedef long long ll;

using namespace std;
typedef pair<int, int> pii;

map<string, vector<ll>> memo;
vector<string> chars = {"A", "G", "C", "T"};
int N;
int mod = 1e+9;

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

int main(){
    int N;
    cin >> N;
    vector<int> as(N);
    rep(i, N){
        cin >> as[i];
    }
    vector<int> ls(N);
    vector<int> rs(N);

    ls[0] = as[0];
    rep(i, N - 1){
        ls[i + 1] = gcd(ls[i], as[i + 1]);
    }

    rs[N - 1] = as[N - 1];
    for(int i = N - 1; i > 1; i --){
        rs[i - 1] = gcd(rs[i], as[i - 1]);
    }


    int ans = 0;
    for(int i = 1; i < N - 1; i ++){
        int tmp = gcd(ls[i - 1], rs[i + 1]);
        ans = max(ans, tmp);
    }
    ans = max(ans, rs[1]);
    ans = max(ans, ls[N - 2]);
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1408
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define ll long long
int main(){
    string s;
    cin >>s >>s;
    cout <<"A"<<s[0]<<"C";
    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>
#include <assert.h>
#include <unordered_set>
#include <random>



using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)

























int main(){
    ll n;
    cin >> n;
    vector<pair<string, pair<ll, ll>>> v(n);
    REP(i, n){
        cin >> v[i].first >> v[i].second.first;
        v[i].second.first *= -1;
        v[i].second.second = i;
    }
    sort(v.begin(), v.end());
    vector<ll> ans(n);
    REP(i, n){
        ans[i] = v[i].second.second + 1;
    }
    REP(i, n){
        cout << ans[i] << endl;
    }
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main() {
    long long i = 100;
    long long x;
    cin >> x;
    int y = 0;
    for (int j = 0; ; j++) {
        y++;
        i *= 1.01;
        if (i >= x) {
            cout << y << endl;
            return 0;
        }
    }
}

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

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

ll todec(string bi_n) {
    ll out = 0;
    ll len = bi_n.length();
    for(ll i = 0; i < len; i++) {
        if(bi_n[i] == '1') {
            out += pow(2, len - i - 1);
        }
    }
    return out;
}

string tobin(ll n) {
    string out = "";
    ll len = 1;
    ll div = 1;
    while(1) {
        if(n / div < 2) {
            break;
        }
        len++;
        div *= 2;
    }
    for(ll i = 0; i < len; i++) {
        if(n / div >= 1) {
            out = out + "1";
            n -= div;
        } else {
            out = out + "0";
        }
        div /= 2;
    }
    return out;
}

int main() {
    ll i, j, k;
    ll N;
    cin >> N;
    string Xstr;
    cin >> Xstr;
    ll X = todec(Xstr);
    ll origin_1 = 0;
    for(i = 0; i < N; i++) {
        if(Xstr[i] == '1') {
            origin_1++;
        }
    }
    ll init_a = origin_1 - 1;
    ll init_b = origin_1 + 1;
    vll resi_a, resi_b;
    if(init_a > 0) {
        resi_a.push_back(1 % init_a);
    }
    resi_b.push_back(1 % init_b);
    for(i = 1; i < N; i++) {
        if(init_a > 0) {
            resi_a.push_back(resi_a[i - 1] * 2 % init_a);
        }
        resi_b.push_back(resi_b[i - 1] * 2 % init_b);
    }
    ll x_a = 0, x_b = 0;
    for(i = 0; i < N; i++) {
        if(Xstr[N - 1 - i] == '1') {
            if(init_a > 0) {
                x_a = x_a + resi_a[i];
            }
            x_b = x_b + resi_b[i];
        }
    }

    for(i = N - 1; i >= 0; i--) {
        ll ans = 0;
        ll x;
        if(Xstr[N - 1 - i] == '1') {
            if(init_a == 0) {
                cout << 0 << endl;
                continue;
            }
            x = (x_a - resi_a[i]) % init_a;
        } else {
            x = (x_b + resi_b[i]) % init_b;
        }
        ans++;
        while(x != 0) {
            string xstr = tobin(x);
            ll num1 = 0;
            for(j = 0; j < xstr.length(); j++) {
                if(xstr[j] == '1')
                    num1++;
            }
            x %= num1;
            ans++;
        }
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 7592
import sys

import numpy as np

s = raw_input()
x, y = [int(x) for x in raw_input().split(" ")]

s = s + "T"

a = []
b = []

cum_len = 0
turn = 0
for command in s:
    if command == "F":
        cum_len += 1
    elif command == "T":
        (a if turn == 0 else b).append(cum_len)
        turn ^= 1
        cum_len = 0

a0 = a.pop(0)

alpha = abs(x-a0)
beta = abs(y)

targetX = sum(a) - alpha
targetY = sum(b) - beta

def search(a, target):
    forwardTable = np.zeros((len(a), target+1))
    forwardTable[:,0] = 1
    for i_i in xrange(len(a)):
        new_item = a[i_i]
        if i_i == 0:
            forwardTable[i_i, 2*new_item:2*new_item+1] = 1
        else:
            forwardTable[i_i] += forwardTable[i_i-1]
            if new_item>0:
                forwardTable[i_i,2*new_item:] += forwardTable[i_i-1,:-2*new_item]
        if forwardTable[i_i,-1] > 0:
            forwardTable[-1,-1] = 1
            break

    return (forwardTable[-1,-1]>0)

if (targetX < 0) or (targetY < 0):
    print "No"
    sys.exit()
else:
    if (targetX > 0):
        if not search(a, targetX):
            print "No"
            sys.exit()
    if (targetY > 0):
        if not search(b, targetY):
            print "No"
            sys.exit()
    print "Yes"

Measured memory_bytes: 37284
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  string S;
  cin >> S;
  
  int cnt = 0;
  int cntMax = 0;
  for (int i = 0; i < S.size(); i++) {
    char a = S.at(i);
    if (a == 'A' || a == 'C' || a == 'G' || a == 'T') {
      cnt++;
      for (int j = i + 1; j < S.size(); j++) {
        a = S.at(j);
        if (a == 'A' || a == 'C' || a == 'G' || a == 'T') {
          cnt++;
        } else {
          break;
        }
      }
    }
    if (cnt > cntMax) {
      cntMax = cnt;
    }
    cnt = 0;
  }
  
  cout << cntMax << endl;
}

Measured memory_bytes: 256
def acinput():
    return list(map(int, input().split(" ")))


def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
            #print(result)
            result%=(10**9+7)

    return result


X,Y=acinput()

import numpy as np
from numpy.linalg import solve
 

x=solve([[1,2],[2,1]],[X,Y])

#print(x)

if np.all(x%1.0==0) and np.all(x>=0):
    print(cmb(int(np.sum(x)),int(np.min(x))))
    #print(cmb(int(np.sum(x)),int(np.min(x)),10**9+7))

else:
    print(0)



Measured memory_bytes: 55400
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define lb lower_bound
#define maxn 100005
#define lowbit(x) (x)&(-x)
#define forup(i,a,b) for(int i=(a);i<=(b);i++)
#define fordown(i,a,b) for(int i=(a);i>=(b);i--)
typedef long long  ll;
typedef unsigned long long ull;
template<class T> inline
void read(T &num)
{num=0; bool f=true; char ch=getchar();
 while(ch<'0'||ch>'9') {if(ch=='-')  f=false;  ch=getchar();}
 while(ch>='0'&&ch<='9') {num=num*10+ch-'0';ch=getchar();}
 num=f?num:-num;
}
int out[100];
template<class T> inline
void  write(T x,char ch)
{if(x==0) {putchar('0');putchar(ch);return;}
 if(x<0) {putchar('-'); x=-x;}
 int num=0;
 while(x){out[num++]=(x%10);x=x/10;}
 fordown(i,num-1,0) putchar(out[i]+'0');
 putchar(ch);
}
int n;
int a[maxn];
int main()
{ cin>>n;
 forup(i,1,n) read(a[i]);
 int cnt=0;
 forup(i,2,n)
 {if(a[i]==a[i-1]) {cnt++; a[i]=10000-cnt;}
 }
cout<<cnt<<endl;	
	
return 0;
 } 

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <utility>
#include <algorithm>
#include <set>
#include <cmath>
#include <array>
#include <bitset>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,n) for(int i=1;i<n+1;i++)
int main(){
    int N;
    cin>>N;
    cout<<(N+1)/2;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define mat vector<vector<int>>
const int inf = 1 << 30;
const ll linf = 1e18;
const db EPS = 1e-7;
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

string S;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> S;
    int N = S.length();
    for(int i = 0; i < N; ++i){
        if(S[i] == '?') S[i] = 'D';
    }
    cout << S << endl;
    return 0;
}

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

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

  LL cnt = 0, bcnt = 0;
  for (int i = 0; i < s.size(); i ++) {
    if (s.at(i) == 'W' && i > 0 ) {
      cnt += bcnt;
    } else if (s.at(i) == 'B') {
      bcnt ++;
    }
  }

  cout << cnt << endl;
}


Measured memory_bytes: 640
#include<iostream>
#include<complex>
using namespace std;
int main()
{
    int n,m,h[100000],i,a,b,ans=0;
    bool f[100000];
    cin >> n >> m;
    for(i=0;i<n;i++){
        cin >> h[i];
        f[i]=1;
    }
    for(i=0;i<m;i++){
        cin >> a >> b;
        if(h[a-1] >= h[b-1]) f[b-1]=0;
        if(h[a-1] <= h[b-1]) f[a-1]=0;
    }
    for(i=0;i<n;i++) ans+=f[i];
    cout << ans;
    return 0;
}

Measured memory_bytes: 4108
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mat = lambda x, y, v: [[v]*y for _ in range(x)]
ten = lambda x, y, z, v: [mat(y, z, v) for _ in range(x)]
mod = 1000000007
sys.setrecursionlimit(1000000)

K = ri()

if K % 2 == 0:
	print(-1)
	exit()
r = 0
for i in range(10000000):
	r = (r*10+7) % K
	if r == 0:
		print(i+1)
		exit()
print(-1)



Measured memory_bytes: 69408
#abc145_d
m = 10**9+7
x,y = map(int,input().split())
fac =[0]*(10**6+1)
a = max(x,y)
b = min(x,y)
cnt = 0
fac[0]=1

while a != b:
    a -= 2
    b -= 1
    cnt +=1
    fac[cnt] = (fac[cnt-1]*cnt)%m

    if a<0 or b <0:
        print(0)
        exit()
c = a//3
d = cnt
if a%3 != 0:
    print(0)
    exit()
    
else:
    while a != 0:
        a -= 1
        b -= 2
        cnt +=1
        fac[cnt] = (fac[cnt-1]*cnt)%m

        a -= 2
        b -= 1
        cnt +=1
        fac[cnt] = (fac[cnt-1]*cnt)%m

n = ((fac[cnt])%m)*(pow(fac[c+d],m-2,m))*(pow(fac[c],m-2,m))
print(n%m)


Measured memory_bytes: 73272
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll x,y;cin >> x >>y;
    ll ans=INF;
    if(y-x>=0)chmin(ans,y-x);
    if(y+x>=0)chmin(ans,y+x+1);
    if(-x-y>=0)chmin(ans,-x-y+1);
    if(y-x<0)chmin(ans,2+x-y);
    cout << ans <<endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#define MAX 100000
int N;
char inst[7];
class Node {
public:
    Node* left;
    Node* right;
    Node* parent;
    int key;
    int val;
};

class BST {
public:
    BST() { root = NULL; }
    Node* data[MAX];
    void insert(Node* n);
    Node* createNode(int k);
    Node* root;
    void printInOrder(Node* start_node);
    void printPreOrder(Node* start_node);
};

Node* BST::createNode(int k)
{
    Node* node_ptr = new Node;
    node_ptr->key = k;
    node_ptr->val = k;
    node_ptr->left = NULL;
    node_ptr->right = NULL;
    node_ptr->parent = NULL;
    return node_ptr;
}

void BST::insert(Node* n)
{
    Node* parent_node = NULL;
    Node* compared_node = root;
    while (compared_node != NULL) {
        parent_node = compared_node;
        if (n->key < compared_node->key) {
            compared_node = compared_node->left;
        } else {
            compared_node = compared_node->right;
        }
    }
    n->parent = parent_node;
    if (parent_node == NULL) {
        root = n;
    } else if (n->key < parent_node->key) {
        parent_node->left = n;
    } else {
        parent_node->right = n;
    }
}

void BST::printInOrder(Node* start_node)
{
    if (start_node != NULL) {
        printInOrder(start_node->left);
        printf(" %d", start_node->val);
        printInOrder(start_node->right);
    }
}

void BST::printPreOrder(Node* start_node)
{
    if (start_node != NULL) {
        printf(" %d", start_node->val);
        printPreOrder(start_node->left);
        printPreOrder(start_node->right);
    }
}

int main(void)
{
    //freopen("in.txt", "r", stdin);
    scanf("%d", &N);
    BST bst;
    for (int i = 0; i < N; ++i) {
        scanf("%s", inst);
        if (inst[0] == 'i') {
            int k;
            scanf("%d", &k);
            Node* n = bst.createNode(k);
            bst.insert(n);
        } else {
            bst.printInOrder(bst.root);
            printf("\n");
            bst.printPreOrder(bst.root);
            printf("\n");
        }
    }
    return 0;
}

Measured memory_bytes: 26152
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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 ll;

int n,k;
string s,t;

int main() {
  cin >> n;
  map<int,int> m; int a;
  rep(i,n){
    cin >> a; m[a]++;
  }
  int ans=0;
  for(auto p : m){
    if(p.second<p.first) ans+=p.second;
    if(p.second>p.first) ans+=p.second-p.first;
  }
  cout << ans << endl;
}

Measured memory_bytes: 4736
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 2e5+55;
#define LL long long
LL a[maxn];
int n;
LL f(LL x)
{
	LL ans=0;
	for(int i=1;i<=n;i++){
		ans+=1LL*abs(a[i]-x);
	}
	return ans;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		a[i]-=i;
	}
	sort(a+1,a+1+n);
	LL ans=1e16;
	ans=min(ans,f(a[n/2]));
	ans=min(ans,f(a[n/2+1]));
	printf("%lld\n",ans);

	return 0;
}


Measured memory_bytes: 1920
#include <bits/stdc++.h>
using namespace std;
pair<int, int> arr[3005];
int N, T, memo[3001][3001];

int func(int depth, int Time)
{
    if(Time == T || depth == N)
        return 0;
    int &ret = memo[depth + 1][Time];
    if(ret != -1)
        return ret;
    ret = 0;
    int nextTime;
    nextTime = Time + arr[depth].first;
    ret = max(ret, func(depth + 1, Time));
    if(nextTime >= T)
        nextTime = T;
    ret = max(ret, func(depth + 1, nextTime) + arr[depth].second);
    return ret;
}

int main()
{
    scanf("%d %d", &N, &T);
    for(int i = 0; i < N; i++)
    {
        scanf("%d %d", &arr[i].first, &arr[i].second);
    }
    sort(arr, arr + N);
    memset(memo, -1, sizeof memo);
    printf("%d", func(0, 0));
}

Measured memory_bytes: 35584
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

void swap(int &a, int &b){
	int tmp;
	tmp = a;
	a = b;
	b = tmp;
}

struct die_t {
public:
	int top, front, right, left, back, bottom;

	die_t (int t, int f, int r, int l, int bk, int b)
		: top(t), front(f), right(r), left(l), back(bk), bottom(b) { }

	void rotate_swap (int &a, int &b, int &c, int &d) {
		swap(a, b); swap(b, c); swap(c, d);
	}

	void rotate_right ( void ) { rotate_swap(top, left, bottom, right); }
	void rotate_left ( void ) { rotate_swap(top, right, bottom, left); }
	void rotate_front ( void ) { rotate_swap(top, back, bottom, front); }
	void rotate_back ( void ) { rotate_swap( top, front, bottom, back); }
	void rotate_cw ( void ) { rotate_swap(back, left, front, right); }
	void rotate_ccw ( void ) { rotate_swap(back, right, front, left); }
};

int main() {
	int n;

	while( true ) {
		cin >> n;

		if( !n ) {
			break;
		}

		vector< string > news(n);

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

		struct die_t dice(1, 2, 3, 4, 5, 6);

		int ans = dice.top;

		for(int i = 0; i < n; i++) {
			if(news[i] == "North") {
				dice.rotate_back();
				ans += dice.top;
			} else if(news[i] == "East") {
				dice.rotate_right();
				ans += dice.top;
			} else if(news[i] == "West") {
				dice.rotate_left();
				ans += dice.top;
			} else if(news[i] == "South"){
				dice.rotate_front();
				ans += dice.top;
			} else if(news[i] == "Right") {
				dice.rotate_cw();
				ans += dice.top;
			} else if(news[i] == "Left") {
				dice.rotate_ccw();
				ans += dice.top;
			}
		}

		cout << ans << endl;

	}
}

Measured memory_bytes: 1492