akhauriyash/Code-Regression huggingface dataset:
# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>

//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, q, last, cnt, sum[10000], ans, dp[10000][10000], a[10000],b[10000];
string str, ss;
bool f;
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ -1,0 } ,{ 0,-1 } };
struct Edge { LL to, cost; };
vector<LL>vec[100100];
vector<Pll>v[200000];
map<Pll, LL>ma;
set<LL>st;
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}

int main() {
	rep(i, 12)
		cin >> a[i];
	sort(a, a + 12);
	rep(i, 3) {
		cnt = a[i * 4];
		rep(j, 4) {
			if (cnt != a[i * 4 + j])f = 1;
		}
	}
	cout << (f ? "no" : "yes") << endl;
	return 0;
}



Measured memory_bytes: 10264
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int ctoi(const char c) {
    if ('0' <= c && c <= '9') return (c - '0');
    return -1;
}
vector<int> input(int n) {
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec.at(i);
    }
    return vec;
}
void output(vector<int> vec) {
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
    return;
}
vector<vector<int>> input(int n, int m) {
    vector<vector<int>> table(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> table. at(i).at(j);
        }
    }
    return table;
}
void output(vector<vector<int>> table) {
    for (int i = 0; i < table.size(); i++) {
        for (int j = 0; j < table.at(0).size(); j++) {
            cout << table.at(i).at(j) << " ";
        }
        cout << endl;
    }
}
long long perm(int n, int r) {
    if (n < r) {
        cout << "error" << endl;
        return 0;
    }
    long long perm = 1;
    for (int i = n; i > n - r; i--) {
        perm *= i;
    }
    return perm;
}
long long comb(int n, int r) {
    if (n < r) {
        cout << "error" << endl;
        return 0;
    }
    long long comb = perm(n,r);
    for (int i = r; i > 0; i--) {
        comb /= i;
    }
    return comb;
}
long long homo(int n, int r) {
    return comb(n + r - 1, n - 1);
}
long long fact(int n) {
    long long fact = 1;
    for (int i = n; i > 0; i--) {
        fact *= i;
    }
    return fact;
}
int gcd(int a, int b)
{
    if (a % b == 0)
    {
        return(b);
    }
    else
    {
        return(gcd(b, a % b));
    }
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
int main() {
  int h,w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i,h){
    cin >> s[i];
  }
  rep(i,h){
    rep(j,2){
      cout << s[i] << endl;
    }
  }
}


Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

long long int N = 100000;

class Stack {
public:
	int s[10000];
	int top;

	Stack() {
		top = 0;
	}

	void push(int x) {
		if(top >= N-1) {
			fprintf(stderr, "Error : Stack is overflow\n");
			exit(0);
		}
		s[++top] = x;
	}

	int pop() {
		if(top == 0) {
			fprintf(stderr, "Error : Stack is underflow\n");
			exit(0);
		}
		top--;
		return s[top+1];
	}
};

int main(void) {
	Stack stack;
	char s[100];

	while(scanf("%s", s) != EOF) {
		if(s[0] == '+') {
			int a = stack.pop();
			int b = stack.pop();
			stack.push(a+b);
		} else if(s[0] == '-') {
			int b = stack.pop();
			int a = stack.pop();
			stack.push(a-b);
		} else if(s[0] == '*') {
			int a = stack.pop();
			int b = stack.pop();
			stack.push(a*b);
		} else {
			stack.push(atoi(s));
		}
	}

	printf("%d\n", stack.pop());

	return 0;
}


Measured memory_bytes: 3184
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

vector<vector<int>>money{
	{0,300,500,600,700,1350,1650},
	{0,0,350,450,600,1150,1500},
	{0,0,0,250,400,1000,1350},
	{0,0,0,0,250,850,1300},
	{0,0,0,0,0,600,1150},
	{0,0,0,0,0,0,500},
	{ 0,0,0,0,0,0,0 },

};
vector<int>dis{ 6,7,5,5,20,15 };

int main() {
	while (1) {
		int d; cin >> d;
		if (!d)break;
		int start,goal;
		{

			int h, m; cin >> h >> m;
			start = h * 60 + m;
		}
		int a; cin >> a;
		{

			int h, m; cin >> h >> m;
			goal = h * 60 + m;
		}
		d--; a--;
		if (d < a)swap(d, a);
		int cost = money[a][d];
		cost /= 50; 
		int distance = 0;
		for (int i = a; i < d; ++i) {
			distance += dis[i];
		}
		bool flag = false;
		if ((start <= 1170 || 1050 <= goal)&&!(start<1050&&1170<goal))flag = true;
		if (distance <= 40 && flag)cost = (cost + 1) / 2;
		cost *= 50;
		cout << cost << endl;
	}
	return 0;
}

Measured memory_bytes: 3132
#include <iostream>
#include <algorithm>

using namespace std;

#define MOD 1000000007

int main() {
    int N, M; cin >> N >> M; int A, visited[N+1] = {}; long long fib[N+1] = {1};
    for (int i = 1; i <= M; i++) cin >> A, visited[A] = 1;
    if (not visited[1]) fib[1] = 1;
    for (int i = 2; i <= N; i++) if (not visited[i]) fib[i] = (fib[i-1]+fib[i-2]) % MOD;
    cout << fib[N] << endl;
    return 0;
}


Measured memory_bytes: 1408
#include <bits/stdc++.h>
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 (b<a) { a=b; return 1; } return 0; }

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007LL;

ll pow(ll a, ll n, ll m) {
	ll ret = 1LL;
	for (; n > 0LL; n >>= 1LL, a = a * a % m) {
		if (n % 2LL == 1LL) {
			ret = ret * a % m;
		}
	}
	return ret;
}

int main() {
  ll N;
  cin>>N;
  vector<ll> A(N);
  for(ll i = 0LL; i < N; i++) cin>>A[i];
  map<ll,ll> m;
  for(auto x:A){
    ll y = x;
    map<ll,ll> mx;
    for(ll i = 2LL; i*i<=x;i++){
      while(y%i==0LL){
        y/=i;
        mx[i]++;
      }
    }
    if(y!=1LL) mx[y]++;
    for(auto t:mx) chmax(m[t.first],t.second);
  }
  ll all=1LL;
  for(auto x:m){
    all*=pow(x.first,x.second,mod);
    all%=mod;
  }
  ll ans = 0LL;
  for(auto x:A){
    ans+=all*pow(x,mod-2,mod);
    ans%=mod;
  }
  cout<<ans<<endl;
}

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

ll n,dpf[300001],dpl[300001],ans=-1e18;
vector<ll> a(300001);
vector<ll> vf,vl;
map<ll,ll> mpf,mpl;

int main(){
    cin >> n;
    for(int i = 0;i < 3*n;i ++){
        cin >> a[i];
        if(i<n) mpf[a[i]] ++,dpf[n-1]+=a[i];
        if(i>2*n-1) mpl[a[i]] ++,dpl[2*n]+=a[i];
    }

    for(int i = n;i < 2*n;i ++){
        if(a[i]>mpf.begin()->first){
            dpf[i] = dpf[i-1]+a[i]-mpf.begin()->first;
            mpf.begin()->second --;
            if(!mpf.begin()->second) mpf.erase(mpf.begin());
            mpf[a[i]] ++;
        }else{
            dpf[i] = dpf[i-1];
        }
    }
    for (int i = 2*n-1;i >= n;i --){
        if(a[i]<mpl.rbegin()->first){
            dpl[i] = dpl[i+1]+a[i]-mpl.rbegin()->first;
            mpl.rbegin()->second --;
            if(!mpl.rbegin()->second) mpl.erase(mpl.rbegin()->first);
            mpl[a[i]] ++;
        }else{
            dpl[i] = dpl[i+1];
        }
    }
    for(int i = n-1;i < 2*n;i ++){
        ans = max(ans,dpf[i]-dpl[i+1]);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 17920
const input = require('fs').readFileSync('/dev/stdin', 'utf8');
const cards = input.split('\n');
let full = [];
let str = "";

for(let i = 1; i <= 13; i++) full.push(`S ${i}`);
for(let i = 1; i <= 13; i++) full.push(`H ${i}`);
for(let i = 1; i <= 13; i++) full.push(`C ${i}`);
for(let i = 1; i <= 13; i++) full.push(`D ${i}`);

for(let i = 0; i < 52; i++){
  if(cards.indexOf(full[i]) !== -1) continue;
  str += full[i] + '\n';
}

if(str !== '') console.log(str.trim());


Measured memory_bytes: 15356
#include<iostream>

using namespace std;


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

	int *value = new int[n];

	for(int i=0; i<n; i++)
		cin>>value[i];
		
	for(int i=0; i<n-1; i++)
	{
		for(int j=0; j<n-i-1; j++)
		{
			if(value[j]>value[j+1])
			{
				temp = value[j];
				value[j] = value[j+1];
				value[j+1] = temp;
			}
		}
	}

	float sum = 0.5 * (float)(value[0]+value[1]);

	for(int i=2; i<n; i++)
	{
		sum = 0.5 * (sum + (float)(value[i]));
	}

	cout<<sum;
	delete[] value;
	return 0;
}


Measured memory_bytes: 256
def isCLKWISE(ph) : return not ((ph[-1][0] - ph[-3][0])*(- ph[-3][1] + ph[-2][1]) - (ph[-2][0] - ph[-3][0])*(- ph[-3][1] + ph[-1][1] ) < 0)
      
def ConvexHullScan(P) :
    P = sorted(P)
    phU = [P[0],P[1]]
    for p in P[2:] :
        phU.append(p)
        while(True) :
            if isCLKWISE(phU) : break
            else :
                del phU[-2]
                if len(phU) == 2 : break
    phL = [P[-1],P[-2]]
    for p in P[-3::-1] :
        phL.append(p)
        while(True) :
            if isCLKWISE(phL) : break
            else :
                del phL[-2]
                if len(phL) == 2 : break
    ph = phU + phL[1:-1]
    return ph
  
n = range(int(input()))
P = [[int(x) for x in input().split()] for _ in n]
Q = ConvexHullScan(P)
Q.reverse()
print(len(Q))
idx = min([[x[1][1],x[1][0],x[0]] for x in enumerate(Q)])[2]
R = Q[idx:] + Q[:idx]
for r in R : print(r[0],r[1])

Measured memory_bytes: 33868
ABC = list(map(int,input().split()))
ans = 'NO'
count_5 = 0
count_7 = 0

for abc in ABC:
    if abc==5:
        count_5 += 1
    elif abc==7:
        count_7 += 1
if count_5==2 and count_7==1:
    ans = 'YES'
print(ans)


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

int main() {
	int a[100];
	long long dp[100][21] = {};
	int n;
	cin >> n;
	for (int i = 0;i < n-1;i++)cin >> a[i];
	int s;
	cin >> s;

	dp[0][a[0]]++;
	for (int i = 1;i < n;i++) {
		for (int j = 0;j <= 20;j++) {
			if (dp[i - 1][j] != 0) {
				if (j + a[i] >= 0 && j + a[i] <= 20) {
					dp[i][j + a[i]] += dp[i - 1][j];
				}
				if (j - a[i] >= 0 && j - a[i] <= 20) {
					dp[i][j - a[i]] += dp[i - 1][j];
				}
			}
		}
	}
	cout << dp[n - 2][s] << endl;
	getchar();
	getchar();
}

Measured memory_bytes: 3088
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){

long N, K;
int ans;

scanf("%ld", &N);

if(N == 22) printf("Christmas Eve Eve Eve\n");
if(N == 23) printf("Christmas Eve Eve\n");
if(N == 24) printf("Christmas Eve\n");
if(N == 25) printf("Christmas\n");


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

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

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> L(M), R(M);
    for (int i=0; i<M; i++) {
        cin >> L[i] >> R[i];
    }
    
    int Lmax = 0;
    int Rmin = 100001;
    
    for (int i=0; i<M; i++) {
        Lmax = max(Lmax, L[i]);
        Rmin = min(Rmin, R[i]);
    }
    
    if (Lmax > Rmin) {
        cout << 0 << endl;
        return 0;
    }
    if (N >= Rmin) cout << Rmin - Lmax + 1 << endl;
    else if (Lmax <= N <= Rmin) cout << N - Lmax + 1 << endl;
    else if (N < Lmax) cout << 0 << endl;
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
using P = pair<int,int>;

map<int,vector<int>> g;
map<int,int> m; map<int,bool> used;
int dfs(int u){
  if(used[u]) return -1;
  if(m[u]!=0) return m[u];

  int res = 0;
  used[u] = true;
  m[u] = 1;
  for(int v:g[u]) {
    int res = dfs(v);
    if(res<0) return m[u] = -1;
    m[u] = max(m[u],res+1);
  }
  used[u] = false;
  return m[u];
}
int main() {
  int n;
  cin >> n;
  auto id = [n](int i, int j){
    if(i>j) swap(i,j);
    return i*n+j;
  };
  vector<int> v;
  rep(a,n){
    int p;
    rep(i,n-1){
      int b;
      cin >> b;
      --b;
      int np = id(a,b);
      if(i>0) g[p].emplace_back(np);
      else v.emplace_back(np);
      p = np;
    }
  }
  int ans = 0;
  rep(i,v.size()){
    int temp = dfs(v[i]);
    if(temp<0){
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans,temp);
  }
  cout << ans << endl;
}


Measured memory_bytes: 101888
def main():
    N, K = list(map(int, input().split()))
    P = list(map(int, input().split()))

    E = [((p + 1) / 2.0) for p in P]

    total = 0
    m = 0
    for i in range(N):
        if i < K:
            total += E[i]
        else:
            total += E[i] - E[i - K]

        if total > m:
            m = total

    print(m)


if __name__ == '__main__':
    main()


Measured memory_bytes: 24548
N = int(input())
P = list(map(int,input().split()))
ans = 0
for i in range(N - 1):
  if P[i] == i+1:
    P[i],P[i+1] = P[i+1],P[i]
    ans += 1
if P[-1] == N:
  ans += 1
print(ans)

Measured memory_bytes: 14008
import java.util.Scanner

fun main(args: Array<String>){
    val scanner = Scanner(System.`in`)
    val sa = scanner.next()
    val sb = scanner.next()
    val a = Integer.parseInt(sa)
    val b = Integer.parseInt(sb)
    if (a == b){
        println("Yes")
    } else {
        println("No")
    }
}

Measured memory_bytes: 31272
#include<stdio.h>

int main(){
  int A, B, T;
  scanf("%d %d %d", &A, &B, &T);
  int sum = 0, time = 0;

  while(time + A<= T){
    sum += B;
    time += A;
  }

  printf("%d\n", sum);

  return 0;
}


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

int main() {
  
  int a,b;
  cin >> a >> b;
  
  if(a <= b) cout << a << endl;
  else cout << a-1 << endl;
  
}


Measured memory_bytes: 640
#include <stdio.h>

int main()
{
   int i,s;
   char a[101];
   gets(a);
   if(strlen(a)==3)
   {
       for(i=strlen(a)-1;i>=0;i--)
       {
           printf("%c",a[i]);
       }
   }else{
       printf("%s",a);
   }

    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
int main() {
    int w,h,x,y;
    cin>>w>>h>>x>>y;
    double ans=(double)w*h/2;
    int num=(w==x*2&&h==y*2)?1:0;
    printf("%.10f %d\n",ans,num);
}

Measured memory_bytes: 256
import math

n = int(input())

c=0
for i in range(1,n+1):
    if i==1:
        c += (n*n)
        continue
    for j in range(1,n+1):
        t = math.gcd(i,j)
        if t==1:
            c += n
            continue
        for k in range(1,n+1):
            c += math.gcd(t,k)

print(c)

Measured memory_bytes: 9192
#include<bits/stdc++.h>
using namespace std;
#define all(a)  (a).begin(),(a).end()
typedef long long ll;
ll mod=1000000007;

int main() {
  int n;cin>>n;
  vector<int> v;
  for(int i=0;i<n;++i){
    int t;cin>>t;
    v.push_back(t);
  }
  int val =0;
  for(int i=0;i<n;++i){
    int t;cin>>t;
    if(t<v[i]) val += v[i] - t;
  }
  cout << val << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pair;
const int inf = 1000000000; //10^9
const ll inff = 1000000000000000000; //10^18

int ev(int i) {
    int a = 0, b = i;
    while(b > 0) {
        a++;
        b /= 10;
    }
    return a;
}

int main(){
    int n;
    cin >> n;
    int ans  = 0;
    for(int i = 1; i <= n; i++) {
        int c = ev(i);
        if(c%2 == 1) ans++;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3576
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>> vec_p; //vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;


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

  ll N,ans=1;
  cin >> N;

  for (ll i = 0; i < N; i++) {
    ans*=(i+1);
    ans = ans % MOD;
  }

  cout << ans << endl;

}

Measured memory_bytes: 256
h, w, n = gets.split.map(&:to_i)
sr, sc = gets.split.map(&:to_i)

s = gets.chomp
t = gets.chomp

a = [sc, sc, sr, sr]
n.times do |i|
    case s[i]
    when ?L
        a[0] -= 1
    when ?R
        a[1] += 1
    when ?U
        a[2] -= 1
    else
        a[3] += 1
    end
    
    break if a[0] == 0 || a[1] > w || a[2] == 0 || a[3] > h

    case t[i]
    when ?L
        a[1] -= 1 if a[1] > 1
    when ?R
        a[0] += 1 if a[0] < w
    when ?U
        a[3] -= 1 if a[3] > 1
    else
        a[2] += 1 if a[2] < h
    end
end or (puts "NO"; exit)

puts "YES"


Measured memory_bytes: 2632
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define Time (double)clock()/CLOCKS_PER_SEC
vector <int> get(int n) {
    if (n == 1) return {};
    if (n & 1) {
        auto t = get(n - 1);
        t.insert(t.begin(), (int)t.size() + 1);
        return t;
    }   
    else {
        auto t = get(n >> 1);
        t.app((int)t.size() + 1);
        return t;
    }   
}   
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    int n;
    cin >> n;
    vector <int> ans = get(n + 1);
    cout << ans.size() * 2 << '\n';
    for (int e : ans) cout << e << ' ';
    for (int i = 1; i <= ans.size(); ++i) cout << i << ' ';
    cout << '\n';
}

Measured memory_bytes: 256
import java.util.*;
public class Main {
    public static void main(String[] args){
      // 入力
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int X = sc.nextInt();
      int Y = sc.nextInt();
      
      // 算出
      int[] dist = new int[N];
      for(int i = 0; i < N; i++){
        dist[i] = 0;
      }
      
      for(int i = 1; i <= N-1; i++){
        for(int j = i+1; j <= N; j++){
          int index = getDistance(i, j, X, Y);
          dist[index]++;
        }
      }
      
      // 出力
      for(int i = 1; i < N; i++){
        System.out.println(dist[i]);
      }
    }
    
    // 2頂点間の最短距離を求める関数
    public static int getDistance(int p1, int p2, int X, int Y){
      // 近道を使用しない場合の距離
      int dist1 = p2 - p1;
      
      // 近道を使用する場合の距離
      int dist2 = abs(X - p1) + abs(Y - p2) + 1;
      
      if(dist1 < dist2){
        return dist1;
      }else{
        return dist2;
      }
    }
    
    // 絶対値を求める関数
    public static int abs(int num){
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    }

}

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

using namespace std;

vector<int>& operator *= (vector<int> &p, vector<int> q) {
  vector<int> r = p;
  for (int i = 0; i < (int) p.size(); ++i) {
    p[i] = q[r[i]];
  }
  return p;
}

vector<int> operator * (vector<int> p, vector<int> q) {
  return p *= q;
}

vector<int> inv(const vector<int> &p) {
  int n = (int) p.size();
  vector<int> res(n);
  for (int i = 0; i < n; ++i) {
    res[p[i]] = i;
  }
  return res;
}

vector<int> power(vector<int> x, int y) {
  int n = (int) x.size();
  vector<int> res(n);
  for (int i = 0; i < n; ++i) {
    res[i] = i;
  }
  while (y) {
    if (y & 1) {
      res *= x;
    }
    x *= x;
    y >>= 1;
  }
  return res;
}

void output(vector<int> ans) {
  for (int i = 0; i < (int) ans.size(); ++i) {
    if (i) {
      cout << " ";
    }
    cout << ans[i] + 1;
  }
  cout << "\n";
}

int main() {
#ifdef wxh010910
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<int> q(n);
  for (int i = 0; i < n; ++i) {
    cin >> q[i];
    --q[i];
  }
  if (m == 1) {
    output(p);
  } else if (m == 2) {
    output(q);
  } else {
    m -= 2;
    vector<int> inv_p = inv(p);
    vector<int> inv_q = inv(q);
    vector<int> ans(n);
    for (int i = 0; i < n; ++i) {
      ans[i] = i;
    }
    ans *= power(inv_q * p * q * inv_p, m / 6);
    switch (m % 6) {
      case 0:
        ans *= q;
        break;
      case 1:
        ans *= inv_p * q;
        break;
      case 2:
        ans *= inv_q * inv_p * q;
        break;
      case 3:
        ans *= inv_q * p * inv_q * inv_p * q;
        break;
      case 4:
        ans *= inv_q * p * p * inv_q * inv_p * q;
        break;
      case 5:
        ans *= inv_q * p * q * p * inv_q * inv_p * q;
        break;
    }
    ans *= power(p * inv_q * inv_p * q, m / 6);
    output(ans);
  }
  return 0;
}

/*
(-2 1 2 -1) * (i - 2) / 6 + f[(i - 2) % 6] + (1 -2 -1 2) * (i - 2) / 6 
f[0] = {2}
f[1] = {-1, 2}
f[2] = {-2, -1, 2}
f[3] = {-2, 1, -2, -1, 2}
f[4] = {-2, 1, 1, -2, -1, 2}
f[5] = {-2, 1, 2, 1, -2, -1, 2}
 */ 


Measured memory_bytes: 7268
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
constexpr ll mod = 1e9+7;
constexpr ll inf = LLONG_MAX;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b); 
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/* ----------------------end----------------------- */


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

Measured memory_bytes: 3596
import sys
import math


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    K = int(input())
    A = list(map(int, input().split()))
    min_ch = 2
    max_ch = 3
    if A[-1] != 2:
        print(-1)
        return
    for i in range(K):
        n = K - i - 1
        if max_ch < A[n]:
            print(-1)
            return
        a = math.ceil(min_ch / A[n])
        b = math.floor(max_ch / A[n])
        min_ch = A[n] * a
        if min_ch > max_ch:
            print(-1)
            return
        max_ch = A[n] * (b + 1) - 1
    print(min_ch, max_ch)


if __name__ == "__main__":
    main()


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

using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;

const int MOD = int(1e9) + 7;

int n, a, b, c, f;
int d[1001], nd[1001];
int C[1001][1001];

int mul[1001];

int main() {
    forn(i, 1001) C[0][i] = 0;
    C[0][0] = 1;
    for (int i = 1; i < 1001; ++i) {
        C[i][0] = 1;
        for (int j = 1; j < 1001; ++j) {
            C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
            if (C[i][j] >= MOD) {
                C[i][j] -= MOD;
            }
        }
    }
    cin >> n >> a >> b >> c >> f;
    memset(d, 0, sizeof d);
    d[n] = 1;
    for (int i = a; i <= b; ++i) {
        memcpy(nd, d, sizeof nd);
        mul[0] = 1;
        for (int j = 1; i * j <= n; ++j) {
            mul[j] = (LL)mul[j - 1] * C[i * j - 1][i - 1] % MOD;
        }
        forn(rem, n + 1) {
            if (d[rem] == 0) {
                continue;
            }
            for (int j = c; j <= f; ++j) {
                if (i * j > rem) {
                    break;
                }
                nd[rem - i * j] = (nd[rem - i * j] + (LL)d[rem] * C[rem][i * j] % MOD * mul[j]) % MOD;
            }
        }
        memcpy(d, nd, sizeof nd);
    }
    cout << d[0] << endl;
    return 0;
}


Measured memory_bytes: 4224
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
#include<math.h>
using namespace std;
#define INF 110000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> P;

int main(){
  bool flag=true;
  char end;
  string W[110];
  int N;
  cin>>N;
  cin>>W[0]; end=*(W[0].end()-1);
  for(int i=1;i<N;i++){
    cin>>W[i];
    for(int j=0;j<i;j++){
      if(W[i]==W[j]){
        flag=false;
        break;
      }
    }

    if(end!=W[i][0]){
      flag=false;
      break;
    }
    end=*(W[i].end()-1);
  }
  if(flag) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}


Measured memory_bytes: 504
N, L = map(int, input().split())
S = [input() for i in range(N)]
c = 0
while c < N:
    for i in range(0, N-1-c):
        if S[i] > S[i+1]:
            S[i], S[i+1] = S[i+1], S[i]
    c += 1
print(''.join(S))


Measured memory_bytes: 3060
n, k = map(int, input().split())
X = [int(x) for x in input().split()]
Xp = sorted([x for x in X if x >= 0])
Xm = sorted([x for x in X if x < 0 ])[::-1]
if not Xp: #Xpが空
  print(-Xm[k-1])
elif not Xm: #Xmが空
  print(Xp[k-1])
else:
  res = 10**10
  for m in range(k - min(k, len(Xp)), min(k, len(Xm)) + 1):
    p = k - m
    if m == 0:
      res = min(res, Xp[k-1])
    elif p == 0:
      res = min(res, -Xm[k-1])
    else:
      res = min(res, 2*Xp[p-1]-Xm[m-1], -2*Xm[m-1]+Xp[p-1])
  print(res)
  


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

def main():
    al = set(list('abcdefghijklmnopqrstuvwxyz'))
    S = input()
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    elif len(S)==26:
        for i in range(len(S)-1,-1,-1):
            for x in sorted(list(S[i:])):
                if x > S[i-1]:
                    print(S[:i-1]+x)
                    exit()

    else:
        for i in S:
            al-=set(i)
        al = sorted(list(al))
        print(S+al[0])

if __name__ == '__main__':
    main()

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

int main(void){
	int n, x, m[110];
	int sum=0,b=10000;

	cin >> n >> x;
	int ans = n;
	for (int i = 0; i < n; i++){
		cin >> m[i];
		x -= m[i];
		b = min(b, m[i]);
	}
	if (x == 0) cout << ans << endl;
	else{
		ans += (x / b);
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
 
#define REP(i,a,n) for(int i=a;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define REV(i,a,n) for(int i=n;i>=a;--i)
#define all(e) e.begin(),e.end()
#define rall(e) e.rbegin(),e.rend()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define show(n) cerr<<#n<<" = "<<n<<endl
#define shows(n) for(auto z:n){cerr<<z<<", ";}cerr<<endl
 
//ここからAtCoder向けdefine
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
 
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using pint = pair<int,int>;
 
const int INF = ~(1 << 31);

const int maxN=100000;
 
vector<bool> isprime;
int c[maxN+2]; 

void sieve(size_t max){
    if(max+1 > isprime.size()){
        isprime.resize(max+1,true);
    } 
    isprime[0] = false;
    isprime[1] = false;

    for(size_t i=2; i*i<=max; ++i)
        if(isprime[i])
            for(size_t j=2; i*j<=max; ++j)
                isprime[i*j] = false;
}

int main(){
    sieve(maxN+1);

    REP(i,1,maxN+1){
        if(isprime[i]&&isprime[(i+1)/2]) c[i]++;
    }

    REP(i,1,maxN+1) c[i]+=c[i-1];

    int q;
    cin>>q;
    int l,r;
    rep(i,q){
        scanf("%d" "%d" ,&l,&r);
        //show(c[r]);
        //show(c[l-1]);
        printf("%d\n",c[r]-c[l-1]);
    }
    
    return 0;
}

Measured memory_bytes: 1024
#include <stdio.h>
#include <iostream>
#include <vector>
#include <assert.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <memory.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
int main()
{
	bool used[128] = {};
	string s;
	cin >> s;
	for (auto x : s)
		used[x] = true;
	if (s.size() == 26)
	{
		string o = s;
		if (!next_permutation(s.begin(), s.end()))
			s = "-1";
		else {
			while (o.substr(0, s.size() - 1) != s.substr(0, s.size() - 1))
				s.pop_back();
		}
		cout << s << endl;
		return 0;
	}
	else {
		for (char c = 'a'; c <= 'z'; ++c)
			if (!used[c]) {
				s += c;
				break;
			}
		cout << s << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
#define REP(i,a,n) for(int (i)=(a); (i)<(int)(n); (i)++)
#define rep(i,n) REP(i,0,n)
#define vec vector 
 
 
int main(){
  ll N, A, B;
  cin >> N >> A >> B;
  
  
  ll repeat = N/(A+B);
  ll res;
  
  if((N - repeat*(A+B)) > A){
      res = repeat * A + A;
    }else{
      res = repeat * A + (N - repeat*(A+B));
    }


  //cout << repeat << " " << (N - repeat*(A+B)) << endl;
  cout << res;
  
}

Measured memory_bytes: 256
import sys
 
chash = {}
for i in range( ord( 'a' ), ord( 'z' )+1 ):
    chash[ chr( i ) ] = 0
 
while True:
    line = sys.stdin.readline().rstrip()
    if not line:
        break
    for i in range( len( line ) ):
        if line[i].isalpha():
            chash[ line[i].lower() ] += 1
 
for i in range( ord( 'a' ), ord( 'z' )+1 ):
    print( "{:s} : {:d}".format( chr( i ), chash[ chr( i ) ] ) )

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

int main(){

    int n,k;cin>>n>>k;
    int h[n];
    int cnt=0;

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

    for(int i=0;i<n;++i){
        if(h[i]>=k){
            cnt++;
        }
    }

    cout<<cnt<<endl;


}


Measured memory_bytes: 640
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main(){
	int count[1001] = {};
	int i, j,k;
	for (i = 2; i <= 1000; i++) {
		for (j = 1; j <= 1001 - i; j++) {
			int csum = 0;
			for (k = j; k <= j + i - 1; k++) {
				csum += k;
				if (csum > 1000)break;
			}
			if (csum <= 1000) {
				count[csum]++;
			}
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0)break;
		cout << count[n] << endl;
	}
	return 0;
}


Measured memory_bytes: 3048
#include <stdio.h>

int main(int argc, char *argv[]) {
    int a, b;
    
    int c, n = 0, i;
    
    scanf("%d %d", &a, &b);
    
    if (a <= b) {
        c = a;
        n = b;
    } else {
        c = b;
        n = a;
    }
    
    for (i = 0; i < n; i++) {
        printf("%d", c);
    }
    
    printf("\n");
    
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
signed main() {
	int n;
	cin >> n;
	string s[n];
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	int Cmp[n][26] = {};
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < s[i].size(); j++) {
			Cmp[i][(char)s[i][j] - 'a']++;
		}
	}
	for (int i = 0; i < 26; i++) {
		int Min = INF;
		for (int j = 0; j < n; j++) {
			Min = min(Min, Cmp[j][i]);
		}
		for (int k = 0; k < Min; k++) {
			char C = 'a' + i;
			cout << C;
		}
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	string s;
	cin>>s;
	string t;
	cin>>t;
	int res = 0;
	for(int i = 0; i < n; ++i){
		string a = s.substr(i);
		string b = t.substr(0, n - i);
		if(a == b){
			cout<<2 * n - a.size()<<endl;return 0;
		}
	}
	cout<<2 * n<<endl;
	return 0;
}


Measured memory_bytes: 256
using System;
class Program
{
    static void Main(string[] args)
    {
        // 整数の入力
        int k = int.Parse(Console.ReadLine());
        // スペース区切りの整数の入力
        string[] input = Console.ReadLine().Split(' ');
        int a = int.Parse(input[0]);
        int b = int.Parse(input[1]);

        for (int i = a; i < b + 1; i++)
        {
            if (i % k == 0)
            {
                Console.WriteLine("OK");
                return;
            }
        }
        Console.WriteLine("NG");
    }
}

Measured memory_bytes: 17904
//
//  main.cpp
//  120_D_Decayed_Bridges
//
//  Created by tolemy on 2019/08/15.
//  Copyright © 2019 tolemy. All rights reserved.
//


#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <iterator>

using namespace std;

//Union-Find木
class UnionFind
{
    //parents[i]：iの親
    vector<int> parents;
    vector<int> sizes;

public:
    UnionFind(int n)
    {
        parents.clear();
        for(int i=0;i<=n;i++)
        {
            parents.push_back(i);
            sizes.push_back(1);
        }
    }
    
    int findRoot(int x)
    {
        if(parents[x]==x) return x;
        //ついでに代入
        return parents[x] = findRoot(parents[x]);
        //return findRoot(parents[x]);
        
    }
    
    void unite(int x, int y)
    {
        int rx = findRoot(x);
        int ry = findRoot(y);
        
        if(rx == ry) return;
        
        parents[rx] = ry;
        
        //サイズの更新
        sizes[rx] = sizes[ry] = sizes[rx] + sizes[ry];
    }
    
    int size(int x)
    {
        return sizes[findRoot(x)];
    }
};

int main(int argc, const char * argv[]) {
    int n, m = 0;
    
    cin >> n >> m;
    
    //島の連結状態を表す集合の１次元配列
    UnionFind groups(n);
    
    //橋
    typedef pair<int, int> bridge;
    bridge *bridges = new bridge[m];
    for(int i=0;i<m;i++)
    {
        cin >> bridges[i].first >> bridges[i].second;
    }
    
    //回答方針
    //橋を取り除きながら行き来できなくなる島を探すのは難しい。
    //橋を逆順に追加しながら行き来できるようになる島を数えていく。
    
    //橋iを追加した時のペアの数
    long *counts = new long[m+1]();
    
    for(int i=m-1;i>=0;i--)
    {
        long count = 0;
        bridge b = bridges[i];
        
        int g1 = groups.findRoot(b.first);
        int g2 = groups.findRoot(b.second);
        
        //集合が異なる場合はマージ
        if(g1!=g2)
        {
            //行けるようになった島のペアを計算
            count = groups.size(g1) * groups.size(g2);
            
            //集合をマージ
            groups.unite(g1, g2);
        }
        
        counts[i] = counts[i+1] + count;
    }
    
    //答えの表示
    //不自由度 = 接続された島の最大値 - 接続数
    long max_connection_count = (long)n * (n - 1) / 2;
    
    for(int i=1;i<=m;i++)
    {
        cout << max_connection_count - counts[i] << endl;
    }
    
    return 0;
}


Measured memory_bytes: 3828
import java.util.*;

public class Main {
    public void main(Scanner sc) {
        int n = sc.nextInt();
        int k = sc.nextInt();
        int l[] = new int[n + 1];
        int r[] = new int[n + 1];
        int ln = 0, rn = 0;
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            if (x < 0) {
                l[ln++] = -x;
            } else if (x == 0) {
                k--;
            } else {
                r[++rn] = x;
            }
        }
        sc.close();

        l = Arrays.copyOf(l, ln + 1);
        r = Arrays.copyOf(r, rn + 1);

        int ans = 4 * 100_000_000;
        for (int i = 0; i <= k; i++) {
            if (i <= ln && (k - i) <= rn) {
                ans = Math.min(ans, r[k - i] * 2 + l[ln - i]);
                ans = Math.min(ans, r[k - i] + l[ln - i] * 2);
            }
        }
        System.out.println(ans);
    }

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


Measured memory_bytes: 64404
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N; cin>>N;
  vector<int>A(N);vector<int>O((N+1)/2);vector<int>E(N/2);
  for(int i=0;i<N;i++){
    cin>>A[i];
    if(i%2==0)O[i/2]=A[i];
    else E[i/2]=A[i];
  }
  sort(A.begin(),A.end());sort(O.begin(),O.end());sort(E.begin(),E.end());
  int o=0,e=0;
  for(int i=0;i<N;i++){
    if(i%2==0){
      for(int j=0;j<(N+1)/2;j++){
        if(A[i]==O[j])break;
        if(A[i]<O[j]){o++;break;}
        if(j==(N+1)/2-1)o++;
      }
    }
  }
  cout<<o<<endl;
}

Measured memory_bytes: 4052
#include <bits/stdc++.h>  // ver2.1.9
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(),(v).end()
#define Vi vector<int>
#define Pii pair<int,int>
#define Tiii tuple<int,int,int>
#define PQi priority_queue<int>
#define PQir priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define sortr(v) sort(v,greater<>())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repeq(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>b;i--)
#define repreq(i,a,b) for(int i=a;i>=b;i--)
#define leng(n) (int)(log10(n)+1)
#define dem(a,b) ((a+b-1)/(b))
#define Vout(a) rep(lZ,0,a.size()-1)cout<<a[lZ]<<' ';cout<<a.back()<<endl
#define VVout(a) rep(lW,0,a.size()){Vout(a[lW]);}
#define INF 9000000000000000000  //  9.0 * 10^18
#define MOD 1000000007  //  10^9 + 7
using namespace std;
 
int gcd(int a,int b){return b?gcd(b,a%b):a;}  //  最大公約数gcd
int lcm(int a,int b){return a/gcd(a,b)*b;}  //  最小公倍数lcm
void uniq(Vi &v){sort(ALL(v)); v.erase(unique(ALL(v)),v.end());}  //  uniqのやつ
 
int mypow(int x, int n, int m){  //  累乗x^n(mod m)  O(log(n))
if(n==0)return 1;if(n%2==0)return mypow(x*x%m,n/2,m);else return x*mypow(x,n-1,m)%m;}
 
signed main() {
  cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(12);
  int n,ans=0,a=0,b=0,ba=0;
  cin >> n;
  string s;
  
  rep(i,0,n){
    cin >> s;
    if(s[0]=='B' && s.back()=='A') ba++;
    if(s[0]=='B' && s.back()!='A') b++;
    if(s[0]!='B' && s.back()=='A') a++;
    rep(j,0,s.size()-1){
      if(s[j]=='A' && s[j+1]=='B') ans++;
    }
  }
  
  if(a==0&&b==0&& ba!=0) ans += ba-1;
  else ans += min(ba+a,ba+b);
  
  cout << ans << endl;
  
  return 0;
}

Measured memory_bytes: 256
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;
#define sim template < class c
#define int long long
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << '\n'; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
typedef tree< pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
const ll MOD = 1e9 + 7 ;
const ll N = 2e5 + 10 ;
const ll INF = 1e18 + 10 ;
signed main(){
    fastio
    int n ; 
    cin >> n ; 
    vector<int> v(n) ; 
    bool ok = false ;
    for(int i = 0; i < n; i++){
    	cin >> v[i] ;
    	if ( v[i] == 0 ) {
    		ok = true ;
    	}
    }
    if ( ok ) {
    	cout << 0 << endl ;
    	return 0 ; 
    }
    int ans = 1 ;
    int limit = 1e18 ; 
    for(int i = 0; i < n; i++){
    	int &x = v[i] ;
    	if ( ans > limit / x ) return cout << -1 << endl, 0 ; 
    	ans *= x ; 
    }
    cout << ans << endl;
    return 0; 
}

Measured memory_bytes: 3992
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  int n,m,r;
  cin >> n >> m >> r;
  int a[r];
  rep(i,r) {
    R a[i];
    a[i]--;
  }
  ll d[n][n];
  rep(i,n)rep(j,n) d[i][j]=MAX;
  rep(i,n)d[i][i]=0;
  rep(i,m) {
    ll x,y,z;
    cin >> x >> y >> z;
    x--,y--;
    d[x][y]=min(d[x][y],z);
    d[y][x]=min(d[y][x],z);
  }
  rep(k,n)rep(i,n)rep(j,n)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
  ll ans=MAX;
  sort(a,a+r);
  do {
    ll sum=0;
    rep(i,r-1) sum+=d[a[i]][a[i+1]];
    ans=min(ans,sum);
  } while(next_permutation(a,a+r));
  pr(ans);
}

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


Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>

#define rep(i, a, b) for ( int i = (a); i < (b); i++ )
#define per(i, a, b) for ( int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template<typename T>
struct Edge {
    int u, v;
    T cost;
    Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
    bool operator< (const Edge &e) const {return cost < e.cost;}
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18+99);
static const int INF = 1e9+99;
const int MAX = 3005;


ll ans;
ll dp[MAX][MAX][4];

int main(void) {
    int R, C, K;
    cin >> R >> C >> K;
    vector<vector<ll>> M(R+5, vector<ll> (C+5, 0));
    rep(i, 0, K) {
        int r, c;
         ll v;
         cin >> r >> c >> v;
         M[r][c] = v;
    }

    rep(i, 1, R+1) rep(j, 1, C+1) {
        rep(k, 1, 4) {
            dp[i][j][k] = max(dp[i][j-1][k-1]+M[i][j], dp[i][j-1][k]);
            dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][3]+M[i][j]); 
        }
    }

    ans = dp[R][C][3];
    
    cout << ans << endl;
    return 0;
}





Measured memory_bytes: 355588
#include <iostream>
using namespace std;
int main() {
	int A, B, C, X;
	cin >> A >> B >> C >> X;
	int SUM[401] = { 0 };
	for (int i = 0; i <= A; i++) {
		for (int j = 0; j <= B; j++) {
			for (int k = 0; k <= C; k++) {
				if ((i * 10 + j * 2 + k) == 0 || (i * 10 + j * 2 + k) > 400) {
				}
				else {
					SUM[i * 10 + j * 2 + k]++;
				}
				
			}
		}
	}
	cout << SUM[X / 50] << endl;
	return 0;
}


Measured memory_bytes: 256
#ifdef LOCAL
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstring>
#else
#include <bits/stdc++.h>
#endif
using namespace std ;

typedef long long ll ;
#define rep(i , n ) for ( int i =0; i < n ; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

/*
__attribute__((constructor))
void initial()
{
	cin.tie(NULL);
	ios::sync_with_stdio(false);
}
*/

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

	ll N, A, B, C, D;
	
	cin >> N >> A >> B >> C >> D;

	ll r = B - A;
	bool re = false;

	rep(i, N)
	{
		ll left = C * (N-1-i) - D * i;
		ll right = D * (N-1-i) - C * i;
		if ((left <= r) && (r <= right))
		{
			re = true;
			break;			
		}
	}

	if (re)
	{
		cout << "YES" << endl;
	}
	else
	{
		cout << "NO" << endl;
	}
	
	return 0;
}


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

int N;
typedef unsigned long long ll;
ll Nsum;



int main()
{
    cin >> N;
    Nsum = 0;
    int N2;
    if(N % 2 == 0) N2 = N / 2;
    else N2 = N / 2 + 1;

    for(int i = 1; i <= N2; i++)
    {
        int invi = N - i + 1;
        if(i % 3 != 0 && i % 5 != 0) Nsum += i;
        if(invi != i && invi % 3 != 0 && invi % 5 != 0) Nsum += invi; 
    }
    
    
    
    cout << Nsum << endl;
}

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

int main()
{
    long long x;
    cin >> x;
    long long m = x % 11LL;
    long long q = x / 11LL;
    long long ans = 0LL;

    if (1 < m)
        ans++;
    if (7 <= m)
        ans++;
    ans += 2 * q;
    if (m == 1)
        ans++;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 384