akhauriyash/Code-Regression huggingface dataset:
/*
　　　  ∧＿∧　やあ
　　 （´・ω・｀)　　　　　/　　　　　ようこそ、バーボンハウスへ。
　　 ／∇y:::::＼　　　 [￣]　　　　　このテキーラはサービスだから、まず飲んで落ち着いて欲しい。
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|　うん、「また」なんだ。済まない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　  仏の顔もって言うしね、謝って許してもらおうとも思っていない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|
　　　　∇　∇　∇　∇　　　／.／|　　　でも、この提出を見たとき、君は、きっと言葉では言い表せない
　　　　┴　┴　┴　┴　／ ／　  |　　　「ときめき」みたいなものを感じてくれたと思う。
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　殺伐としたコンテストの中で、そういう気持ちを忘れないで欲しい
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　そう思って、この提出を投げたんだ。
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 じゃあ、判定を聞こうか。
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    string s;
    cin >> s;
    string ans = "No";
    string t[6] = {"abc", "acb", "bac", "bca", "cab", "cba"};
    for (int i=0; i<6; ++i){
        if(t[i] == s){
            ans = "Yes";
            break;
        }
    }
    cout << ans << "\n";
    return 0;
}

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

int main() {
    int A, B, C, X, Y;
    cin >> A >> B >> C >> X >> Y;
    int ans = 10000000000;
    for (int i=0; i<100001; i++) {
        //ABセットをi枚購入した場合の所要金額
        int sum = 0;
        sum = i * 2 * C + max(0, X-i) * A + max(0, Y-i) * B;
        ans = min(sum, ans);
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
object Main {
  def main(args: Array[String]): Unit = {
    solve
  }

  def solve(): Unit = {
    val sc = new java.util.Scanner(System.in)
    val s: Array[Char] = sc.nextLine.toCharArray
    val k = sc.nextLong

    val index = s.indexWhere(_ != '1')
    if (index >= 0 && index + 1 <= k) println(s(index))
    else println(1)
  }
}


Measured memory_bytes: 25812
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
// #include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define REP(i, n) for(int i=0; i<(int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  string input;
  int R;
  cin >> input >> R;
  vi num;
  REP(i, input.length()){
    switch(input[i]) {
    case '+':
      num.push_back(-1);
      break;
    case '*':
      num.push_back(-2);
      break;
    default:
      num.push_back(input[i] - '0');
      break;
    }
  }

  int M = num[0];
  for(int i = 1; i < num.size(); i+=2) {
    if(num[i] == -1) {
      M += num[i + 1];
    }else {
      M *= num[i + 1];
    }
  }

  for(int i = 1; i < num.size(); i+=2) {
    if(num[i] == -2) {
      num[i + 1] = num[i - 1] * num[i + 1];
      num[i - 1] = 0;
      num[i] = -1;
    }
  }

  int L = num[0];
  for(int i = 1; i < num.size(); i+=2) {
    if(num[i] == -1) {
      L += num[i + 1];
    }else {
      L *= num[i + 1];
    }
  }

  if(M == R && L == R) {
    cout << 'U' << endl;
  }else if(M==R) {
    cout << 'L' << endl;
  }else if(L==R) {
    cout << 'M' << endl;
  }else{
    cout << 'I' << endl;
  }
}

Measured memory_bytes: 3224
# include <stdio.h>

int main(){
    int N = 0;
    scanf("%d", &N);
    int a[N];
    for(int i=0; i<N; ++i){
        scanf("%d", &a[i]);
    }
    // output if needs
    for(int k = 0; k < N - 1; k++){
        printf("%d ", a[k]);
    }
    printf("%d\n", a[N-1]);
    // your code
    int j = 0; 
    for(int i = 1; i < N; i++){
        if(a[i] < a[i-1]){ // if needs
            int temp = a[i];
            for(j = i - 1; j >= 0 && temp < a[j]; j--){ // shift
                a[j+1] = a[j];
            }
            a[j+1] = temp; // need +1
        }
        for(int k = 0; k < N - 1; k++){
            printf("%d ", a[k]);
        }
        printf("%d\n", a[N-1]);
    }
}



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

int main() {
  int64_t N;
  cin >> N;
  int64_t ans, sum;
  ans =0;
  sum =0;
  vector<int>A(N);
  for(int i=0; i<N; i++){
    cin >> A.at(i);
    sum += A.at(i);
  }
  
  for(int i=0; i<N; i++){
    sum-= A.at(i);
    ans += A.at(i)*(sum%1000000007);
    ans %= 1000000007;
  }
  
  cout << ans <<endl;
  
  
  
  
}


Measured memory_bytes: 4032
A,B,C,X,Y = map(int,input().split())

if X<Y:
        t = X
        X = Y
        Y = t
        t = A
        A = B
        B = t

ans = A*X+B*Y
ans = min(ans,A*(X-Y)+C*2*Y)
ans = min(ans,C*2*X)

print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (int i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PL = pair<long long, long long>;
const int INF = 1001001007;
const int MOD = 1000000007;
template <typename T>
void printvec(vector<T> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename S>
bool range(S a, S b, S x){
    return (a <= x && x < b);
}

int main(){
    ll a, b, x; cin >> a >> b >> x;
    ll nb = b / x;
    ll na;
    if (a % x == 0) na = a / x;
    else na = a / x + 1;
    cout << nb - na + 1 << endl; 
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int acs(const void *a, const void *b) { 
  return *(int*)a - *(int*)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
  return *(int*)b - *(int*)a;
} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b) { 
  return *(char*)a - *(char*)b;
} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) {
  return strcmp(*(const char **)a, *(const char **)b);
} /* aaa,aab.. */
#define readi(x) scanf("%d", &x)
#define reads(s) scanf("%s", s)
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define MAX 100001
#define MOD 1000000007

typedef long long int lli;

int main(void) {
  char s[4];
  reads(s);

  int ans = 700;
  rep(i, 0, 3) {
    if (s[i] == 'o') ans += 100;
  }
  printf("%d\n", ans);
  return 0;
}


Measured memory_bytes: 128
from collections import deque
N,M=map(int,input().split())
p=list(map(int,input().split()))
fromto=[]
tree=[[] for _ in range(N)]
for i in range(N):
    fromto.append([i,p[i]-1])
cnt=0
for i in range(M):
    x,y=map(int,input().split())
    tree[x-1].append(y-1)
    tree[y-1].append(x-1)
col=[-1]*N
for i in range(N):
    if col[i]==-1:
        d = deque()
        d.append(i)
        while len(d)>0:
            v=d.popleft()
            col[v]=i
            for j in tree[v]:
                if col[j]==-1:
                    d.append(j)
            #print(col,d)
            #input()
#print(col,fromto)
for i in range(N):
    x=fromto[i][0]
    y=fromto[i][1]
    if col[x]==col[y]:
        cnt=cnt+1
print(cnt)


Measured memory_bytes: 83692
#include <stdio.h>

int	main(void)
{
	int x = 0;
	int y = 0;
	int z = 0;
	int i = 0;
	int j;
	int n, yen;
	scanf("%d%d",&n, &yen);
	if (n * 1000 == yen)
	{
		printf("%d %d %d\n", x, y, n);
		return (0);
	}
	else if (n * 5000 == yen)
	{
		printf("%d %d %d\n", x, n, z);
		return (0);
	}
	else if (n * 10000 == yen)
	{
		printf("%d %d %d\n", n, y, z);
		return (0);
	}
	while (i <= n)
	{
		j = 0;
		while (j <= n)
		{
			if ((yen - ((10000*i) + (5000*j)) == (n-(i+j)) * 1000) && (n-(i+j)) >= 0)
			{
				x = i;
				y = j;
				z = n - (i+j);
				break ;
			}
			j++;
		}
		i++;
	}
	if (x + y + z != n)
	{
		x = -1;
		y = -1;
		z = -1;
		printf("%d %d %d\n", x, y, z);
	}
	if (x + y + z == n)
		printf("%d %d %d\n", x, y, z);
	return (0);
}


Measured memory_bytes: 128
#include <iostream>

using namespace std;

int main()
{
    int freq[30]={};
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++){
        if(islower(s[i]))freq[s[i]-'a']++;
    }
    for(int i=0;i<30;i++)if(freq[i]%2!=0){
        cout<<"No"<<endl;
        return 0;
    }
     cout<<"Yes"<<endl;
    return 0;
}


Measured memory_bytes: 256
n, k = map(int, input().split())
xx = tuple(map(int, input().split()))

ans = 10 ** 9
for i in range(n - k + 1):
    if xx[i + k - 1] <= 0:
        ans = min(ans, -xx[i])
    elif xx[i] >= 0:
        ans = min(ans, xx[i + k - 1])
    else: #if xx[i] <= 0 and xx[i + k - 1] >= 0:
        ans = min(
            ans,
            -xx[i] * 2 + xx[i + k - 1],
            -xx[i] + 2 * xx[i + k - 1]
        )
print(ans)


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

ll dp[201010][16][2], dp2[201010][2];

int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A[i];

    rep(i, N-1) rep(k, 2) dp2[i][k] = 1e18;

    rep(k, 2) {
        
        rep(j, 16) dp[N-1][j][k] = j;

        for (int i=N-2; i>=0; --i) {
            rep(j, 16) {
                ll a = (A[i] << (2*j));
                int j2 = 0;
                while (a > (A[i+1]<<(2*j2))) j2++;
                if (j2 <= 15) {
                    dp[i][j][k] = dp[i+1][j2][k] + j;
                }
                else {
                    dp[i][j][k] = dp[i+1][15][k] + j + (N-1-i) * (j2-15);
                }
                dp2[i][k] = min(dp2[i][k], dp[i][j][k]);
            }
        }
        reverse(A.begin(), A.end());
    }

    ll ans = 1e18;
    rep(i, N+1) {
        ll ret = i + 2 * (dp2[i][0] + dp2[N-i][1]);
        ans = min(ans, ret);
    }
    cout << ans << endl;
    return 0;
}


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

using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define print(x) cout << x << endl
 
ll gcd(ll a, ll b) { return b ? gcd(b,a%b) : a;}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); } 
typedef pair <ll,ll> P;
static const ll dy[4] = {0,1,0,-1}, dx[4] = {1,0,-1,0};

template <typename T> 
ostream& operator<<(ostream& os, const vector<T>& v) 
{ 
    for (int i = 0; i < (int)v.size(); ++i) { 
        os << v[i]; 
        if (i != (int)v.size() - 1) 
            os << " "; 
    } 
    return os; 
} 

ll nCrModp(ll n, ll r, ll p)
{
    ll C[r+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;

    for (ll i = 1; i <= n; i++)
    {
        for (ll j = min(i, r); j > 0; j--)
            C[j] = (C[j] + C[j-1])%p;
    }
    return C[r];
}

int main()
{
    ll N, M;
    cin >> N >> M;
    vector<bool> AC(N);
    vector<ll> WA(N);
    ll cntA = 0;
    rep(i, M) {
        ll p;
        string tmp;
        cin >> p >> tmp;
        if (tmp == "AC") {
            if (!AC[p-1]) {
                cntA++;
                AC[p-1] = true;
            }
        } else {
            if (!AC[p-1]) {
                WA[p-1]++;
            }
        }
    }
    ll cntW = 0;
    rep(i, N) {
        if (AC[i]) cntW += WA[i];
    }
    print(cntA << " " << cntW);
    return 0;
}

Measured memory_bytes: 1024
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 各種変数の読み込み
            var nkq = Readints();
            var n = nkq[0];
            var k = nkq[1];
            var q = nkq[2];
            var a = Readints(q, true)[0];
            var points = new int[n];
            for (int i = 0; i < q; i++)
            {
                points[a[i] - 1]++;
            }
            for (int i = 0; i < n; i++)
            {
                if (k - q + points[i] > 0)
                {
                    Console.WriteLine("Yes");
                }
                else
                {
                    Console.WriteLine("No");
                }
            }
#if DEBUG
            Console.ReadKey();
#endif
        }


        private static int Readint()
        {
            // 1行に1つのintが入力されたのを読む
            return int.Parse(Console.ReadLine());
        }

        private static int[] Readints()
        {
            // 1行に複数のintが入力されたのを読む
            var s = Console.ReadLine().Split(' ');
            var output = new int[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                output[i] = int.Parse(s[i]);
            }
            return output;
        }

        private static int[][] Readints(int n, bool transpose = false)
        {
            // n行それぞれにintが入力されたのを読む
            // output[行番号][列番号]で返す。
            // transposeがtrueのときは、output[列番号][行番号]で返す。
            var s = Console.ReadLine().Split(' ');
            int rownum = s.Length;
            if (transpose)
            {
                var outputs = new int[rownum][];
                for (int j = 0; j < rownum; j++)
                {
                    outputs[j] = new int[n];
                }
                for (int i = 0; i < s.Length; i++)
                {
                    outputs[i][0] = int.Parse(s[i]);
                }
                for (int l = 1; l < n; l++)
                {
                    var temps = Console.ReadLine().Split(' ');
                    for (int i = 0; i < rownum; i++)
                    {
                        outputs[i][l] = int.Parse(temps[i]);
                    }
                }
                return outputs;
            }
            else
            {
                var outputs = new int[n][];
                outputs[0] = new int[rownum];
                for (int i = 0; i < s.Length; i++)
                {
                    outputs[0][i] = int.Parse(s[i]);
                }
                for (int l = 1; l < n; l++)
                {
                    outputs[l] = new int[rownum];
                    var temps = Console.ReadLine().Split(' ');
                    for (int i = 0; i < rownum; i++)
                    {
                        outputs[l][i] = int.Parse(temps[i]);
                    }
                }
                return outputs;
            }
        }
    }
}


Measured memory_bytes: 17704
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<queue>
#define REP(i,a) for (int i = 0;i < (a);++i)
#define FOR(i,a,b) for (int i = (a);i < (b); ++i)
#define FORR(i,a,b) for (int i = (a);i >= (b); --i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;
int main(){
    int n;
    cin >> n;
    vector<int>a(n),hist(100100);
    int answer = 0;
    REP(i,100100)hist[i] = 0;
    REP(i,n){
        cin >> a[i];
        hist[a[i]]++;
    }
 
    if(n == 1){
        cout << 1 << endl;
        return 0;
    }
    else if(n == 2){
        if(abs(a[0]-a[1])<=2){
            cout << 2 << endl;
        }
        else{
            cout << 1 << endl;
        }
        return 0;
    }
 
    FOR(i,1,n-1){
        answer = max(answer,hist[i-1]+hist[i]+hist[i+1]);
    }
    cout << answer << endl;
    return 0;
}

Measured memory_bytes: 1024
import sys
H,W=map(int,input().split())

mat=[]
mat.append([True]*(W+2))
for i in range(H):
  array=[True]+[x=="#" for x in input()]+[True]
  mat.append(array)
mat.append([True]*(W+2))
#print(mat)

for i in range(1,H+1):
  for j in range(1,W+1):
    if mat[i][j]:
      if not (mat[i-1][j] or mat[i+1][j] or mat[i][j-1] or mat[i][j+1]):
        print("No")
        sys.exit(0)
else:
  print("Yes")

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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  int n;
  cin >> n;
  string a,b,c;
  cin >> a >> b >> c;
  int ans=0;
  for(int i=0;i<n;i++){
    if(a[i]==b[i]&&b[i]==c[i]){

    }else if(a[i]==b[i]||b[i]==c[i]||c[i]==a[i]){
      ans++;
    }else{
      ans+=2;
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec.at(i);
    }
    sort(vec.begin(), vec.end());
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            int t = vec.at(i) + vec.at(j);
            int r = lower_bound(vec.begin(), vec.end(), t) - vec.begin();
            int l = i + 1;
            cnt += r - l;
        }        
    }
    cout << cnt << endl;
}


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

using namespace std;

typedef long long lli;
typedef double lld;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<double> vdl;
typedef vector<vector<lli>> mat;
typedef vector<vdl> mad;
typedef unordered_map<lli,unordered_map<lli,lli>> graph;
typedef complex<double> cmp;
typedef vector<cmp> vcl;

lli k,a,b;

int main(){
  cin >> k >> a >> b;
  if(b-a > 2){
    lli x = k-(a-1);

    cout << 1+a-1+(b-a)*(x/2)+x%2 << endl;
  }else{
    cout << k+1 << endl;
  }

}


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

int mini,ans,n,R[200000];

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>R[i];
	}
	ans=R[1]-R[0];
	mini=R[0];
	for(int i=1;i<n;i++){
		mini=min(mini,R[i-1]);
		ans=max(ans,R[i]-mini);
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 1940
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
//     tree_order_statistics_node_update>;
 
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
typedef long long int ll;
typedef long double ld;
const ll N = 2e5+9;
const ll m = 1e9 + 7;
// const ll inf= 1e14;
const ll mod =163577857;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pld pair<ld,ld>
ll powm(ll a, ll b) { a = a%mod;ll res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }

int main()
{   
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout); 
    #endif     
    ll T= 1;
    // cin >> T;
    while(T--){
        ll n;cin>>n;
        ll a[n],ind[n+1];
        for(int i = 0;i<n;i++){
            cin>>a[i];
            ind[a[i]] = i;
        }
        set<ll> s;
        ll ans = 0;
        for(int i = 1;i<=n;i++){
            auto u = s.lower_bound(ind[i]);
            ll l = *u,r;
            if(u == s.end()){
                l = n;
            }
            if(u==s.begin()){
                r = -1;
            }
            else{
                u--;
                r = *u;
            }
            // cerr<<l<<" "<<r<<'\n';
            ll le= (l-ind[i])*(ind[i]-r);
            // cout<<(l-ind[i])<<" "<<(ind[i]-r)<<'\n';
            ans+=(i*le);
            s.insert(ind[i]);
        }
        cout<<ans<<'\n';

    }
   
}   

Measured memory_bytes: 16104
#include<stdio.h>
#define N 100000
#define Q 50000
int cnt = 0, a[N];
int max,min,mid;
void bi(int key){
    
    while(min <= max){
            mid = (min + max) / 2;
            if(a[mid] == key) {
                cnt++; 
                break;
            }else if(a[mid] < key) min = mid + 1;
            else if(a[mid] > key) max = mid - 1; 
    }

}
int main(){
    
    int n,q,i,j,k,u = 0,flg = 0;
    int b[Q],c[Q];
    
    scanf("%d",&n);
    for( i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    
    scanf("%d",&q);
    for( i = 0; i < q; i++){
        scanf("%d",&b[i]);
    }
    for( i = 0; i < q; i++){
        max = n - 1;
        min = 0;
        bi(b[i]);      
    }
    printf("%d\n",cnt);
    
    return 0;
}


Measured memory_bytes: 2328
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for(int i=0; i<n; i++){
      int a = sc.nextInt();
      if(a>max){
        max = a;
      }
      if(a<min){
        min = a;
      }
    }

    int sub = max - min;
    System.out.println(sub);
  }
}

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

const int inf=1e9;

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	ll k;
	cin>>k;
	vector<int> d(k,inf);
	vector<bool> vst(k,false);
	deque<pair<int,int>> dq;
	d[1]=1;
	dq.emplace_back(1,d[1]);
	while(!dq.empty()){
		int u=dq.front().first;
		int cost=dq.front().second;
		dq.pop_front();
		if(cost>d[u]) continue;
		vst[u]=true;
		int nxt=(u+1)%k;
		if(!vst[nxt]&&cost+1<d[nxt]){
			d[nxt]=cost+1;
			dq.emplace_back(nxt,d[nxt]);
		}
		nxt=(u*10)%k;
		if(!vst[nxt]&&cost<d[nxt]){
			d[nxt]=cost;
			dq.emplace_front(nxt,d[nxt]);
		}
	}
	cout<<d[0]<<'\n';
}




Measured memory_bytes: 4100
N,K = map(int,input().split())
import string
numbers = "0123456789"
alphabets = string.ascii_letters # a-z+A-Zをロード
characters = numbers + alphabets

def base_cvt(value, n=2):
    try:
        tmp = int(value)
    except:
        raise ValueError('Invalid value:', value)
    if n < 2 or n > len(characters):
        raise ValueError('Invalid n:', value)
    result = ''
    tmp = int(value)
    while tmp >= n:
        idx = tmp%n
        result = characters[idx] + result
        tmp = int(tmp / n)
    idx = tmp%n
    result = characters[idx] + result
    return result
print(len(base_cvt(N,K)))

Measured memory_bytes: 38512
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//DSU
//UNION BY SIZE
const ll maxs = 2e5 + 6;
ll PARENT[maxs];
ll SIZE[maxs];
void SET(ll V)
{
    PARENT[V] = V;
    SIZE[V] = 1;
}
ll FIND_SET(ll V)
{
    if(V == PARENT[V])
        return V;
    return PARENT[V] = FIND_SET(PARENT[V]);
}
void UNION(ll A,ll B)
{
    A = FIND_SET(A);
    B = FIND_SET(B);
    if(A != B)
    {
        if(SIZE[A] < SIZE[B])
            swap(A,B);
        PARENT[B] = A;
        SIZE[A] += SIZE[B];
    }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int testCases = 1;
    //cin >> testCases;
    
    while(testCases--)
    {
        int n,m;
        int i,j;
        cin >> n >> m;
        int a,b;
        for(i = 1;i <= n;++i)
            SET(i);
        map<int,int>mp;
        while(m--)
        {
            cin >> a >> b;
            UNION(a,b);    
        }
        
        for(i = 1;i <= n;++i)
        {
            int res = FIND_SET(i);
            mp[PARENT[i]]++;
        }
        
        //int ans = n - S.size() + 1;
       // ans++;
        
        int ans = 0;
        
        for(auto it : mp)
        {
            ans = max(ans,it.second);
        }
        
        cout << ans;
        
        cout << '\n';
    }
    
}


Measured memory_bytes: 15936
#include <iostream>
#include <sstream> // std::stringstream
#include <istream> // std::getline
#include <set>

using namespace std;

bool is_valid(const int n, const set<int>& set_n) {
	auto str = to_string(n);
	bool is_valid = true;
	auto itr = set_n.begin();
	for (itr; itr != set_n.end(); ++itr) {
		if(str.find(to_string(*itr)) != string::npos) is_valid = false;
	}
	return is_valid;
}

int main() {
	int n, k;
	cin >> n >> k;
	string buf;
	set<int> set_n;

	while (std::getline(cin, buf, ' ')) {
		set_n.insert(stoi(buf));
	}

	while(!is_valid(n, set_n)) {
		n += 1;	
	}
	cout << n << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N, K;
  cin >> N >> K;
  int ans = 0;
  for(int n = 0; n < N; n++){
    int x;
    int dist_A = 0, dist_B = 0;
    cin >> x;
    dist_A = x * 2;
    dist_B = abs(x - K) * 2;

    if(dist_A >= dist_B){
      ans += dist_B;
    }else{
      ans += dist_A;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
function *main() {
  
  const [N, M] = (yield).split` `.map(s => +s);
  console.log((N == M) ? "Yes" : "No");
  
}
const iter = main();
iter.next();
require("readline").createInterface({input: process.stdin, output: process.stdout}).on("line", line => iter.next(line));


Measured memory_bytes: 29968
#include <stdio.h>

int main(){
    int a, b, ans;
    char op;
    while(op != '?'){
        scanf("%d %c %d", &a, &op, &b);
        if(op == '+'){
            printf("%d\n", a+b);
        }else if(op == '-'){
            printf("%d\n", a-b);
        }else if(op == '*'){
            printf("%d\n", a*b);
        }else if(op == '/'){
            printf("%d\n", a/b);
        }
    }
    return 0;
}


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


void f(int x,int y,int s){
  int ans=0;

  FOR(i,1,1000){
    FOR(j,1,1000){
      if((i*(100+x)/100)+(j*(100+x)/100)==s){
        int z = (i*(100+y)/100)+(j*(100+y)/100);
        
        ans = max(ans,z);
      }
    }
  }

  cout<<ans<<endl;
}


int main(){
  while(1){
    int x,y,s;
    cin>>x>>y>>s;
    if(x==0) break;
    f(x,y,s);
  }

  return 0;
}


Measured memory_bytes: 3016
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<set>
using namespace std;
#define pb push_back
#define POP pop_back()
#define ll long long
#define db double
#define POP pop_back()
#define endl '\n'
#define PII pair<int,int>
#define FI first
#define SE second
#define VI vector<int>
#define QI queue<int>
#define SI stack<int>
#define debug cout<<"debug"<<endl;
#define PLL pair<ll,ll>
#define PDD pair<double,double>
#define ULL unsigned long long
#define fo(i,a,b) for (int i=a;i<=b;++i)
#define fd(i,a,b) for (int i=a;i>=b;--i)
#define mem(x,a) memset(x,a,sizeof(x))
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
char s[maxn];
int len , ans[3000];
int main(){
	cin >> s;
	len = strlen(s);
	int now = 0 , cnt = 0 , p = 1;
	ans[0] = 1;
	for(int i = len - 1;i >= 0;i--){
		now = (now + (s[i] - '0')*p)%2019;
		cnt+=ans[now];
		ans[now]++;
		p = p * 10 % 2019;
	}
	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 3720
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl/*, greater<ll>*/> pql; //降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, n) for(ll i = 1; i <= ll(n); i++)
//#define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
//#define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';

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

  ll a, b, x;
  cin >> a >> b >> x;
  cout << (a<=x &&  x<=a+b ? "YES" : "NO") << newl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
void debug() {cout<<endl;}
template<typename T,typename... Args>
void debug(T x,Args... args) {cout<<x<<" ";debug(args...);}
#define forn(i,a,b) for(int i=a;i<b;++i)
#define pb push_back
#define F first
#define S second
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;


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

  int n, m;
  cin >> n >> m;
int c; cin >> c;
  vector<vector<int> > mat(n, vector<int>(m));
	vector<int> b(m);
  forn(i, 0, m)cin >> b[i];
  forn(i, 0, n){
	  forn(j, 0, m){
		  cin >> mat[i][j];
	  }
  }
int ans = 0;
  forn(i, 0, n){
	  ll sum = c;
	  forn(j, 0, m){
		  sum += mat[i][j]*b[j];
	  }
	  if(sum > 0)ans++;
  }
  cout << ans << endl;

}




Measured memory_bytes: 3644
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vec=vector<ll>;
using mat=vector<vec>;
#define For(i,a,b) for(ll i=a;i<b;i++)
#define bFor(i,a,b) for(ll i=a;i>=b;i--)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N-1,0)
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define mod 1000000007
#define endl "\n"
#define output(v) do{bool f=0;for(auto i:v){cout<<(f?" ":"")<<i;f=1;}cout<<endl;}while(0)
using datas=pair<ll,ll>;
using tdata=pair<ll,datas>;

ll gcd(ll a,ll b){if(!b)return a;return (a%b==0)?b:gcd(b,a%b);}

vec eratosthenes(ll N){
  vec ch(N+1,1),p;
  ch[0]=ch[1]=0;
  rep(i,N+1){
    if(ch[i]){
      p.push_back(i);
      for(ll j=2;i*j<=N;j++){
        ch[i*j]=0;
      }
    }
  }
  return ch;
}

int main(){
  ll i,N,x,ans;
  cin>>N;ans=N;
  while(N--){
    cin>>x;
    for(i=2;i*i<=x;i++){
      if(x%i==0){
        ans--;
        break;
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 3092
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
ll N;
ll ans;
vector<int> v;

void dfs(int idx, ll score) {
    /*
    cerr << "------" << idx << " " << score << "-------" << endl;
    for(auto val : v) {
        cerr << val << " ";
    }
    cerr << endl;
    */
    if(idx == v.size()) {
        chmin(ans, score);
        return;
    }
    if(v[idx] == 0) {
        dfs(idx + 1, score);
        return;
    }
    ll sub = v[idx];
    for(int j = idx; j < v.size(); j++) {
        v[j] = (v[j] + 10 - sub) % 10;
        dfs(idx + 1, score + 1);
    }
    for(int j = idx; j < v.size(); j++) {
        v[j] = (v[j] + sub) % 10;
    }
    return;
}

void solve() {
    ans = 1e18;
    v.resize(N);
    string S, T;
    cin >> S >> T;
    for(int i = 0; i < N; i++) {
        v[i] = (int)(S[i] - T[i] + 10);
        v[i] %= 10;
    }
    dfs(0, 0);
    cout << ans << endl;
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> N) {
        if(N == 0) break;
        solve();
    }
    return 0;
}


Measured memory_bytes: 3032
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,M = LI()
ans = 1
def prime_factor(num):
    prime_factor = collections.defaultdict(int)
    for i in range(2,int(num**0.5)+1):
        while num%i==0:
            prime_factor[i] += 1
            num //= i
    if num>1:
        prime_factor[num]=1
    return prime_factor
def nCr(n,r,mod):
    comb_count = 1
    for i in range(r):
        comb_count *= n-i
        comb_count %= mod
    for j in range(1,r+1):
        comb_count *= pow(j,mod-2,mod)
        comb_count %= mod
    return comb_count
for v in prime_factor(M).values():
    ans *= nCr(v+N-1,v,10**9+7)
    ans %= 10**9+7
print(ans)


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

int main() {
    ll n,a,mn=1e16;
    cin>>n;
    for (int i=0;i<5;i++) {
        cin>>a;
        mn=min(mn,a);
    }
    cout<<(n+mn-1)/mn+4<<endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 1001001001;

ll pow_mod(ll x, ll y) {
	ll res = 1;
	for (ll i = 0; i < y; i++) res = res * x % mod;
	return res;
}

int main() {
	string s;
	cin >> s;
	int ans = 0;
	int cnt = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == 'A' || s[i] == 'C' || s[i] == 'G' || s[i] == 'T') {
			cnt++;
			ans = max(cnt, ans);
		}
		else {
			cnt = 0;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3556
#include<bits/stdc++.h>
#define REP(i, n) for(ll i = 0; i < (ll)n; i++)
#define REPvec(itr, mp) for(auto itr = mp.begin();itr != mp.end(); itr ++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007//1e9+7
using namespace std;
typedef long long ll;
typedef vector<int >    vecint;
typedef vector<ll >    vecll;
typedef vector<string > vecstr;
////////////////////////////////////////////////
int main()
{
  int n, t;
  cin >> n >> t;
  vecint a(n,0);
  REP(i,n) cin >> a[i];
  int ans = 0;
  REP(i,n - 1){
    ans += min(a[i + 1] - a[i], t);
  }
  ans += t;
  cout << ans << endl;
} 


Measured memory_bytes: 1024
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
from decimal import *
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

a, v = MAP()
b, w = MAP()
t = INT()
if a <= b:
    c = a + v * t
    d = b + w * t
    if c >= d:
        print('YES')
    else:
        print('NO')
else:
    c = a - v * t
    d = b - w * t
    if c <= d:
        print('YES')
    else:
        print('NO')

Measured memory_bytes: 72752
#include<iostream>
#include<string>
#include<map>
#include <vector>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define out(x) cout << x << endl;
// for (int i = 0; i < N; ++i) cin >> A[i];
typedef vector<int> vint;
typedef vector<long> vl;
typedef vector<double> vdouble;
typedef vector<string> vstr;
typedef vector<bool> vbool;
typedef map<string, int> mapstri;
typedef map<string, int> mapii;

int main () {
    string s;
    cin >> s;
    if (s[2] == s[3] && s[4] == s[5]) {
        out("Yes")
    }
    else {
        out("No")
    }
    return 0;
}

Measured memory_bytes: 888
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010


int main(){
  int n;
  int a;
  map<int, int> ss;

  cin >> n;

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

  pair<int,int> u[3];

  if(ss.size() == 1) {
    if(ss.begin()->first == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
    return 0;
  }

  if (ss.size() == 2) {
    auto it = ss.begin();

    u[0] = *it; it++;
    u[1] = *it; it++;

    if (u[1].second == n/3) swap(u[0], u[1]);

    if (u[0].second == n/3 && u[0].first == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;

    return 0;
  }

  if (ss.size() != 3 || n%3 != 0){
    puts("No");
    return 0;
  }

  auto it = ss.begin();

  u[0] = *it; it++;
  u[1] = *it; it++;
  u[2] = *it; it++;

  if ((u[0].first ^ u[1].first) == u[2].first &&
      u[0].second == n/3 && u[1].second == n/3)
    puts("Yes");
  else
    puts("No");

  return 0;
}


Measured memory_bytes: 3840
import collections
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    S = [input() for _ in range(n)]
    word = collections.Counter(S).most_common()
    maxi = word[0][1]
    ans = []
    for w in word:
        if w[1] == maxi:
            ans.append(w[0])
        else:
            break
    ans.sort()
    a = "\n".join(ans)
    print(a)

if __name__ == '__main__':
    main()

Measured memory_bytes: 49556
results = Hash.new
N = gets.to_i
N.times do
  blue = gets.strip
  results[blue].nil? ? results[blue] = 1 : results[blue] += 1  
end
M = gets.to_i
M.times do
  red = gets.strip
  results[red].nil? ? results[red] = -1 : results[red] -= 1  
end
answer = results.max_by { |k, v| v }
puts answer[1] < 0 ? 0 : answer[1]

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

int main(){
  int n,h[40];
  string a;
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a,h[i]=a.size();
    for(int i=0;i<n;i++){
      int now=i,nt=0,nn=0,f=0;
      while(1){
	if(nt==5){
	  f=1;
	  break;
	}
	if(nt==0||nt==2){
	  if(nn>5)break;
	  if(nn==5)nt++,nn=0;
	}else{
	  if(nn>7)break;
	  if(nn==7)nt++,nn=0;
	}
	nn+=h[now];
	now++;
      }
      if(f){
	cout<<i+1<<endl;
	break;
      }
    }
  }
  return 0;
}

Measured memory_bytes: 3168
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e10)
#define PI (acos(-1))

int N;
string s[10010];
int lng[10010];

int a = 0, b = 0, ab = 0, Cab = 0;

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> s[i];
		lng[i] = s[i].length();
	}


	for (int i = 0; i < N; i++)
	{

		//ABあるか調べる

		for (int j = 0; j < lng[i] - 1; j++)
		{
			if (s[i][j] == 'A' && s[i][j + 1] == 'B') Cab++;
		}

		if (s[i][0] == 'B' && s[i][lng[i] - 1] == 'A')
		{
			ab++;
			continue;
		}
		if (s[i][0] == 'B')
		{
			b++; continue;
		}
		if (s[i][lng[i] - 1] == 'A')
		{
			a++; continue;
		}
	}

	if (a == 0 && b == 0 && ab == 0)
	{
		cout << Cab << endl;
		return 0;
	}

	if (a == 0 && b == 0)
	{
		cout << (ab - 1) + Cab << endl;
		return 0;
	}

	int tmp = min(a, b) + ab;

	cout << tmp + Cab << endl;
}


Measured memory_bytes: 896
h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
for i in range(h):
    a[i] = [aij % 2 for aij in a[i]]

cnt, ans, rvs = 0, [], -1
for i, ai in enumerate(a):
    rvs *= -1
    for j in range(w)[::rvs]:
        if cnt == 1:
            ans.append([bi + 1, bj + 1, i + 1, j + 1])
            ai[j] += 1
        cnt = int(ai[j] == 1)
        bi, bj = i, j

print(len(ans))
for q in ans:
    print(*q)


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

#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SIZE(array) (sizeof(array) / sizeof(array[0]))

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

#define MAX_V 100000

#define MOD 1000000007L
#define INF 100000000000

signed main() {
	string s;
	cin >> s;
	int apos = -1;
	int zpos = -1;
	for (size_t i = 0; i < s.size(); i++) {
		if (s[i] == 'A' && apos == -1) {
			apos = i;
		}
		if (s[i] == 'Z') {
			zpos = i;
		}
	}
	cout << (zpos - apos + 1) << endl;
}


Measured memory_bytes: 640
#include <stdio.h>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

using ll = long long;

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

	int h = n / 100;
	int t = (n % 100) / 10;
	int s = (n % 100) % 10;


	if (7 == h || 7 == t || s == 7) {
		cout << "Yes" <<endl;
	}
	else {
		cout << "No" <<endl;
	}

	

	//system("pause");
	return 0;
}


Measured memory_bytes: 3644
from heapq import *

def solve(p:list):
    s = 0
    n = len(p)
    data = [[] for _ in range(n+1)]
    for i,j in p:
        data[min(i,n)].append(j)
    H = []
    heapify(H)
    for i in range(n,0,-1):
        for j in data[i]:
            heappush(H,-j)
        if H:
            s -= heappop(H)
    return s
    
t = int(input())
for _ in range(t):
    n = int(input())
    ans = 0
    pl = []
    pr = []
    for _ in range(n):
        k,l,r = map(int,input().split())
        m = min(l,r)
        ans += m
        l,r = l-m,r-m
        if l:
            pl.append((k,l))
        else:
            pr.append((n-k,r))
    print(ans+solve(pl)+solve(pr))

Measured memory_bytes: 115488
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits>

typedef long long int64;

static bool isPalind(const std::string& s) {
  int n = s.size();
  if (n % 2) {
    for (int i = 0; i < (n - 1) / 2; i++) {
      if (s.at(i) != s.at(n - 1 - i)) {
        return false;
      }
    }
  }
  else {
    for (int i = 0; i <= (n - 1) / 2; i++) {
      if (s.at(i) != s.at(n - 1 - i)) {
        return false;
      }
    }
  }
  return true;
}

int main(void) {
  std::string s;
  std::cin >> s;
  int n = s.size();
  std::string rs = s.substr(0, (n - 1) / 2);
  std::string ls = s.substr((n + 3) / 2 - 1);

  //std::cout << rs << std::endl;
  //std::cout << ls << std::endl;

  if (isPalind(s) && isPalind(rs) && isPalind(ls)) {
    std::cout << "Yes" << std::endl;
  }
  else { 
    std::cout << "No" << std::endl;
  }

  return 0;
}


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

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    x = I()
    a = I()
    b = I()

    return (x-a) % b



print(main())




Measured memory_bytes: 5588
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <limits>
using namespace std;

double inf = numeric_limits<double>::infinity();

int main() {
    int n, m;
    cin >> n >> m;
    
    double d[n][n];
    vector<int> as, bs;
    vector<double> cs;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) d[i][j] = 0;
            else d[i][j] = inf;
        }
    }
    
    for (int i = 0; i < m; i++) {
        int a, b;
        double k;
        cin >> a >> b >> k;
        a--;
        b--;
        d[a][b] = k;
        d[b][a] = k;
        as.push_back(a);
        bs.push_back(b);
        cs.push_back(k);
    }
    
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
    
    int cnt = 0;
    for (int i = 0; i < m; i++) {
        if (cs[i] != d[as[i]][bs[i]]) cnt++;
    }
    cout << cnt << endl;
}

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

int main(){
  int A, B, C, X; cin >> A >> B >> C >> X;
  int ans = 0;
  for(int a = 0; a <= A; a++){
    for(int b = 0; b <= B; b++){
      for(int c = 0; c <= C; c++){
        if(a * 500 + b * 100 + c * 50 == X)
          ans++;
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256