akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN=3e5+50;
const ll mod =1e9+7;
ll s[MAXN];
int main()
{
    ll n;
    ll sum=0;
    int k=60;
    scanf("%lld",&n);
    for(int i=0;i<n;i++){
        scanf("%lld",&s[i]);
    }
    for(int i=0;i<k;i++){
        int cnt=0;
        for(int j=0;j<n;j++)
            if(s[j]>>i&1)
                cnt++;
        sum=(sum+((((1LL<<i)%mod*cnt)%mod)*(n-cnt))%mod)%mod;
    }
    printf("%lld\n",sum);
    return 0;
}


Measured memory_bytes: 2560
def minHeapify(x)
  l = 2*x
  r = 2*x+1
  if l <= $n && $heap[l][0] < $heap[x][0]
    minimum = l
  else
    minimum = x
  end
  if r <= $n && $heap[r][0] < $heap[minimum][0]
    minimum = r
  end
  if minimum != x
    $heap[x], $heap[minimum] = $heap[minimum], $heap[x]
    minHeapify(minimum)
  end
end
 
def extract
  key = $heap[1]
  $heap[1] = $heap[$n]
  $n -= 1
  minHeapify(1)
  key
end
 
def insert(key)
  $n += 1
  $heap[$n] = key
  i = $n
  while i > 1 && $heap[i/2][0] > $heap[i][0]
    $heap[i], $heap[i/2] = $heap[i/2], $heap[i]
    i /= 2
  end
end

def dijkstra(n, l)
  done = (n+1).times.map{ [false] * (l+1) }
  while true
    e, s, r = extract
    return e if s == n
    next if done[s][r]
    done[s][r] = true
    $route[s].each do |t|
      insert([e + $enemy[s][t], t, r])
      insert([e, t, r - $dist[s][t]]) if r >= $dist[s][t]
    end
  end
end

loop do
  n, m, l = gets.split.map(&:to_i)
  break if n == 0
  $heap = []
  $n = 0
  $route = (n+1).times.map{ [] }
  $dist = (n+1).times.map{ [0] * (n+1) }
  $enemy = (n+1).times.map{ [0] * (n+1) }
  m.times.map do
    s, t, d, e = gets.split.map(&:to_i)
    $route[s] << t
    $route[t] << s
    $dist[s][t] = $dist[t][s] = d
    $enemy[s][t] = $enemy[t][s] = e
  end
  insert([0, 1, l])
  p dijkstra(n, l)
end

Measured memory_bytes: 13116
#include <bits/stdc++.h>
    using namespace std;
     
    int main(){
      int n;
      cin>>n;
      int x=200001,ans=0;
      for(int i=0; i<n; i++){
        int a;
        cin>>a;
        if(x>=a) {
          ans++;
          x=a;
        }
      }
      cout << ans << endl;
    }

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

int main()
{
 long long int n,a,b, dem=0, d;
   cin>>n>>d;
   for (long long int i=1;i<=n;i++)
   {
       cin>>a>>b;
       if (sqrt(a*a+b*b)<=d) dem++;
   }
   cout<<dem;
    return 0;
}

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

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;


class Solver {
  public:
    bool solve() {
        int H, W; cin >> H >> W;
        if(H < W) swap(H, W);
        if(H % 3 == 0 or W % 3 == 0) {
            cout << 0 << endl;
        } else {
            ll ans = 1LL << 60;            
            repeat(i, 1, W) {
                ll a = (ll)H * i;
                ll b = (ll)(W - i) * (H / 2);
                ll c = (ll)(W - i) * (H / 2 + H % 2);
                ll mn = min({a, b, c});
                ll mx = max({a, b, c});
                set_min(ans, mx - mn);
            }

            if(H > 3) {
                ll a = (ll)(H / 3) * W;
                ll b = (ll)(H / 3 + (H % 3 >= 2)) * W;
                ll c = (ll)(H / 3 + (H % 3 >= 1)) * W;
                ll mn = min({a, b, c});
                ll mx = max({a, b, c});
                set_min(ans, mx - mn);
            }
            
            swap(H, W);
            repeat(i, 1, W) {
                ll a = (ll)H * i;
                ll b = (ll)(W - i) * (H / 2);
                ll c = (ll)(W - i) * (H / 2 + H % 2);
                ll mn = min({a, b, c});
                ll mx = max({a, b, c});
                set_min(ans, mx - mn);
            }

            if(H > 3) {
                ll a = (ll)(H / 3) * W;
                ll b = (ll)(H / 3 + (H % 3 >= 2)) * W;
                ll c = (ll)(H / 3 + (H % 3 >= 1)) * W;
                ll mn = min({a, b, c});
                ll mx = max({a, b, c});
                set_min(ans, mx - mn);
            }

            
            cout << ans << endl;
        }
        return 0;
    }
};

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


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a , b , c;
int main()
{
	scanf("%lld%lld%lld" , &a , &b , &c);
	if(c * b >= a) printf("Yes");
	else printf("No");
	return 0;
} 

Measured memory_bytes: 3824
#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,k,a,ans=0;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a;
		if(a>=k){
			ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

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

using namespace std;

int main(){
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	int t = (a+d-1)/d;
	int s = (c+b-1)/b;
	if(t>=s) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}

Measured memory_bytes: 3636
# coding: utf-8
# hello worldと表示する
import sys
import numpy
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
from copy import deepcopy
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,m=MI()
tree=[[] for i in range(n)]
for i in range(m):
    a,b=MI()
    a-=1;b-=1
    tree[a].append(b)
    tree[b].append(a)
#print(tree)
#count=0
#global count
count=0
def dfs(x,al):
    global count
    if len(al+[x])==n:
        count+=1
        #print(count)
        return
    #if x==0:
        #al=[]
    #al.append(x)
    #print(al)
    for y in tree[x]:
        if not (y in al):
            #print(y,al+[x])
            dfs(y,al+[x]) #return
dfs(0,[])
print(count)

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

int main(){
  vector<char> X(3);
  for(int i=0;i<3;i++){
    cin >> X.at(i);
  }
  
  if(X.at(0) == 'M'){
    cout << 6 <<endl;
  }else if(X.at(0) == 'W'){
    cout << 4 <<endl;
  }else if(X.at(0) == 'F'){
    cout << 2 <<endl;
  }else if(X.at(1) == 'H'){
    cout << 3 <<endl;
  }else if(X.at(1) == 'A'){
    cout << 1 <<endl;
  }else if(X.at(2) == 'N'){
    cout << 7 <<endl;
  }else if(X.at(2) == 'E'){
    cout << 5 <<endl;
  }
}


Measured memory_bytes: 256
// Hail god Yato
 
#include <bits/stdc++.h> 
using namespace std;
 
#define add(x) accumulate(x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end() 
#define rep(i, a, b) for (int i=a; i<(b); i++)
#define hs ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
                void solve(){
                        ll n;
                        cin>>n;
                        vll vec(n);
                        rep(i,0,n)
                                cin>>vec[i];

                        ll cnt=1,ans=0;
                        rep(i,0,n){
                                if(vec[i]==cnt){
                                        ++cnt;
                                }
                                else
                                        ++ans;
                        }
                        if(ans!=n)
                        cout<<ans;
                        else
                                cout<<-1;
                }
                int main(){ 
                        hs;
                        ll t;
                        t=1;
                        // cin>>t;
                        for (int i=1; i<=t; i++){
                                //printf("Case #%d: ", i);
                                solve();
                         }
        return 0; 
}         
 

Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
    static const double pi = 3.141592653589793;
 typedef pair<int64_t,int64_t> P;
int64_t maxt(int64_t a,int64_t b,int64_t c){
int64_t t=max(a,b);
  t=max(t,c);
return t;
}


int main() {
int64_t a;
  cin>>a;
  int64_t sea[a];
  int64_t moun[a];
  int64_t house[a];
  for(int i=0;i<a;i++)
  {
  int64_t b,c,d;
  cin>>b>>c>>d;
    sea[i]=b;
    moun[i]=c;
    house[i]=d;
  }
 int64_t dp[100002][3];
  dp[0][0]=sea[0];
  dp[0][1]=moun[0];
  dp[0][2]=house[0];
 for(int i=1;i<a;i++)
 {
 dp[i][0]=max(dp[i-1][1]+sea[i],dp[i-1][2]+sea[i]);
 dp[i][1]=max(dp[i-1][0]+moun[i],dp[i-1][2]+moun[i]);
   dp[i][2]=max(dp[i-1][0]+house[i],dp[i-1][1]+house[i]);
 }
 cout<<maxt(dp[a-1][0],dp[a-1][1],dp[a-1][2]);
   
  
  
}
 

Measured memory_bytes: 4992
n,W = map(int,input().split())
w = [0]*n
v = [0]*n
for i in range(n): w[i],v[i] = map(int,input().split())
mv = n*max(v)
dp = [[float("INF")]*(mv+1) for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
    for s in range(mv+1):
        if s - v[i] < 0: dp[i+1][s] = dp[i][s]
        else: dp[i+1][s] = min(dp[i][s], dp[i][s-v[i]] + w[i])
ans = 0
for x in range(1,mv+1):
    if dp[n][x] <= W: ans = x
print(ans)

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

int main() {
    int n;
    cin >> n;
    int S[n];
    for (int i = 0; i < n; i++) {
        cin >> S[i];
    }
    int q;
    cin >> q;
    int T[q];
    for (int i = 0; i < q; i++) {
        cin >> T[i];
    }

    int c = 0;
    for (int i = 0; i < q; i++) {
        int left = 0, right = n, mid;
        while (left < right) {
            mid = (left + right) / 2;
            if (S[mid] == T[i]) {
                c++;
                break;
            } else if (T[i] < S[mid]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
    }

    cout << c << endl;

    return 0;
}



Measured memory_bytes: 3596
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int> > dp(n, vector<int>(3));
    for(int i=0; i<n; i++) {
        for(int j=0; j<3; j++) {
            cin >> dp[i][j];
        }
    }
    for(int i=1; i<n; i++) {
        for(int j=0; j<3; j++) {
            dp[i][j] += max(dp[i-1][(j+1)%3], dp[i-1][(j+2)%3]);
        }
    }
    cout << max({dp[n-1][0], dp[n-1][1], dp[n-1][2]}) << endl;
}

Measured memory_bytes: 5760
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args)throws Exception{
		InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        
        int b, f, r, v;
    	int[][][] kousya = new int[4][3][10];
    	for (int i = 0; i < 4; i++)
    	{
    		for (int j = 0; j < 3; j++)
    		{
    			for (int k = 0; k < 10; k++)
    			{
    				kousya[i][j][k] = 0;
    			}
    		}
    	}
        	String kensu = br.readLine();
        	for (int i = 0; i < Integer.parseInt(kensu); i++)
        	{
            	String[] nyukyo = br.readLine().split(" ");
            		b = Integer.parseInt(nyukyo[0]) - 1;
            		f = Integer.parseInt(nyukyo[1]) - 1;
            		r = Integer.parseInt(nyukyo[2]) - 1;
            		v = Integer.parseInt(nyukyo[3]);
            		
            		kousya[b][f][r] += v;
        	}
        	for (int i = 0; i < 4; i++)
        	{
        		for (int j = 0; j < 3; j++)
        		{
        			for (int k = 0; k < 10; k++)
        			{
        				System.out.print(" ");
        				System.out.print(kousya[i][j][k]);
        			}
        			System.out.println();
        		}
        		if (i != 3)
        			System.out.println("####################");
        	}
        	
	}

}

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

int main(){
    int n; cin>>n;
    deque<int> d;
    rep(i,n){
        int a; cin>>a;
        int p = lower_bound(d.begin(),d.end(),a) - d.begin();
        if(p==0) d.push_front(a);
        else d[p-1] = a;
    }
    cout << d.size() << endl;
}

Measured memory_bytes: 3996
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define COUTF(x) cout << setprecision(15) << (x) << endl
#define ENDL cout << endl
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(i, v.size()) cerr << " " << v[i]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

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

signed main() {
  init();
  ll A, B;
  cin >> A >> B;
  ll ans = max(max(A + B, A * B), A - B);
  COUT(ans);
  return 0;
}

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

using namespace std;

string s[300010];
map < string , int > mp;
int n;

int main() {
	cin >> n;
//	for(register int i = 1; i <= n; i++) cin >> s[i];
	for(register int i = 1; i<=n; i++) {
		cin>>s[i];
		mp[s[i]] ++;
	}
	int jsq=0;
	map<string,int>::iterator iter;
	for(iter=mp.begin();iter!=mp.end();iter++)
	{
		if(iter->second>=1)
		jsq++;
	}
	cout<<jsq;
	return 0;
}

Measured memory_bytes: 28600
# define _CRT_SECURE_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <deque>
# include <iomanip>
# include <queue>
# include <sstream>
# include <unordered_set>
# include <unordered_map>
# include <climits>
# include <complex>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define MP make_pair
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define STRING(x) string x;cin>>x;
# define STR(x) STRING(x);
# define DOUBLE(x) double x;cin>>x;
# define DBL(x) DOUBLE(x); 
# define ALL(x) begin(x),end(x)
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<":"<<x<<endl
# define INIT(a) memset(a,-1,sizeof a)
# define CLR(a) memset(a,0,sizeof a)
# define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
//const int M = 1000000007;
template<class T> void debug(T a) { for ( auto i : a )cout << i << endl; }

int main()
{
	int N, R, T;
	cin >> N >> R >> T;
	int ans = 0;
	vi P,now(N,0),w(R+1,0);

	FOR(i, N)
	{
		INT(p);
		P.push_back(p);
		now[i] = p;
	}
	ans = N;

	queue<int> que;
	FOR(t, T-1)
	{
		FOR(i, N)
		{
			now[i] += P[i];
			now[i] %= R;
			if ( w[now[i]] <= 0 )
			{
				ans++;
			}
			else
			{
				w[now[i]]--;
			}
			que.push(now[i]);
		}

		while ( !que.empty() )
		{
			int p = que.front(); que.pop();
			w[p]++;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 3148
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using V = vector<ll>;
#define _GLIBCXX_DEBUG
#define rep(i, a) for(int i = 0; i < a; i++)
#define ALL(v) v.begin(), v.end()

int main(){
	int n; cin>>n;
    V p(n);
    rep(i,n) cin>>p[i];

    ll k=0;
    rep(i,n) if(p[i]!=i+1) k++;
    if(k<=2) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<functional>
#include <random>
#include<iostream>
#include<iomanip>
using namespace std;

long long frac[200000];

long long mod_inverse(long long M){
	long long ret = 1;
	long long pow = 1000000005;
	while (pow > 0) {
		if (pow % 2 == 1) {
			ret = (ret * M) % 1000000007;
		}
		pow = pow / 2;
		M = (M * M) % 1000000007;
	}
	return ret;
}

int main() {
	long long N, M;
	int prime_list[10000];
	int prime_order[10000];
	
	cin >> N >> M;

	frac[0] = 1;
	for (int i = 1; i < 200000; i++) {
		frac[i] = (frac[i - 1] * i) % 1000000007;
	}

	long long M_copy = M;
	int p = 2;
	int t = 0;

	while (M > 1) {
		if (M % p == 0) {
			prime_list[t] = p;
			prime_order[t] = 0;
			while (M % p == 0) {
				prime_order[t]++;
				M = M / p;
			}
			t++;
		}
		p++;
		if (p*p > M && M > 1) {
			prime_list[t] = M;
			prime_order[t] = 1;
			t++;
			break;
		}
	}

//	for (int i = 0; i < t; i++) {
//		cout << prime_list[i] << " " << prime_order[i] << "\n";
//	}

	long long ans = 1;
	for (int i = 0; i < t; i++) {
		ans = (ans * frac[N - 1 + prime_order[i]]) % 1000000007;
		ans = (ans * mod_inverse(frac[N - 1])) % 1000000007;
		ans = (ans * mod_inverse(frac[prime_order[i]])) % 1000000007;
	}
	cout << ans;
}


Measured memory_bytes: 1792
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int main(){
    fastIO;
    int n, m, h, w;
    cin >> n >> m >> h >> w;
    if(n % h == 0 && m % w == 0){
        cout << "No\n";
        return 0;
    }
    cout << "Yes\n";
    for(int i = 0 ; i < n; i ++ ){
        for(int j = 0 ; j < m ; j ++ ){
            if(i % h == 0 && j % w == 0){
                cout << 500 * (h * w - 1) - 1  << " ";
            }
            else{
                cout << -500 << " ";
            }
        }
        cout << "\n";
    }
    return 0;
}

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

//type
#define ll long long
typedef pair<int, int> P;
//定数
#define INF 1000000000000 //10^12:∞
#define MOD 1000000007    //10^9+7:合同式の法
#define MAXR 100000       //10^5:配列の最大のrange
//略記
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
#define F first      //pairの一つ目の要素
#define S second     //pairの二つ目の要素
#define Z class
// OTHER
// xの二乗を返す (関数テンプレート版)
template <typename T>
T square(T x) { return x * x; }
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))
// loop
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)

// vector
#define ALL(x) x.begin(), x.end()

// output
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n", (x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define ln cout << '\n'
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);
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * *

int N, Q;
vector<int> edges[200000];
int64_t num[200000];

void dfs(int i, int p)
{
  for (int j : edges[i])
    if (j != p)
    {
      num[j] += num[i];
      dfs(j, i);
    }
}

int main()
{
  // input
  cin >> N >> Q;
  // edges 
  for (int i = 0; i < N - 1; i++)
  {
    int a, b;
    cin >> a >> b;
    edges[a - 1].push_back(b - 1);
    edges[b - 1].push_back(a - 1);
  }
  // counter 
  for (int i = 0; i < Q; i++)
  {
    int p, x;
    cin >> p >> x;
    num[p - 1] += x;
  }

  dfs(0, -1);
  for (int i = 0; i < N; i++)
    cout << num[i] << " \n"[i == N - 1];
  return 0;
}


Measured memory_bytes: 28572
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



int main() {
  set<int> se;
  for (int i = 0; i < 3; ++i) {
	int a;
	cin >> a;
	se.insert(a);
  }
  cout << se.size() << endl;
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
from collections import defaultdict
def resolve():
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    S=[0]*(n+1)
    for i in range(n):
        S[i+1]=(S[i]+A[i])%m

    ans=0
    C=defaultdict(int)
    for s in S:
        ans+=C[s]
        C[s]+=1
    print(ans)
resolve()

Measured memory_bytes: 62576
#include<cstdio>
#include<cstring>
using namespace std;

int main(){
  int h,w;
  char s[110][110];
  scanf("%d %d",&h,&w);
  for(int i=0;i<h;i++)
    scanf("%s",s[i]);
  for(int i=0;i<w+2;i++)
    printf("#");
  printf("\n");
  for(int i=0;i<h;i++)
    printf("#%s#\n",s[i]);
  for(int i=0;i<w+2;i++)
    printf("#");
  printf("\n");
}

Measured memory_bytes: 128
//四則演算 #pragma GCC target("avx")
//並列計算 #pragma GCC optimize("O3")
//条件分岐を減らす #pragma GCC optimize("unroll-loops")
//浮動小数点演算 #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define each(i, a) for(auto &i : a)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#define line cout << "-----------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
#define stop system("pause")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }

int main() {
    init();

    int n; cin >> n;
    VI a(n); cin >> a;
    VI b(n);
    int x = 0;
    each(i, a)x ^= i;
    FOR(i, 0, n) {
        b[i] = x ^ a[i];
    }
    print(b);

    return 0;
}

Measured memory_bytes: 4776
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println(Math.max(sc.nextInt() + sc.nextInt() + sc.nextInt() + sc.nextInt(), sc.nextInt() + sc.nextInt() + sc.nextInt() + sc.nextInt()));
    }
}



Measured memory_bytes: 26220
//edpc_a
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
typedef long long ll;
#define rep(i,a,b,c) for(ll i=(a);i<(b);i+=(c))
#define INF (1LL<<60)
ll min(ll a, ll b){if(a<b)return a;else return b;}

ll N,K;
ll h[100010],dp[100010];

int main(void){
    scanf("%lld %lld",&N,&K);
    rep(i,0,N,1)scanf("%lld",&h[i]);
    dp[0]=0;
    dp[1]=abs(h[1]-h[0]);
    rep(i,2,N,1){
        dp[i]=INF;
        rep(j,0,K+1,1){
            if(i-j<0)continue;
            dp[i]=min(dp[i-j]+abs(h[i]-h[i-j]),dp[i]);
        }
    }

    printf("%lld\n",dp[N-1]);
    return 0;

}

Measured memory_bytes: 3260
mod = 10 ** 9 + 7

N = int(input())

A = list(map(int,input().split()))

lis = [0,0,0]
ans = 1

for i in range(N):

    na = A[i]
    ind = 0
    nn = 0

    for j in range(3):
        if lis[j] == na:
            nn += 1
            ind = j

    lis[ind] += 1

    if nn == 0:
        ans *= 0

    elif nn == 2:
        ans *= 2
    elif nn == 3:
        ans *= 3

    ans %= mod
    #print (lis)

print (ans % mod)


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

#define FOR(i,n,j) for(int i=(j);i<(n);++i)
#define ssort(n) sort((n).begin(),(n).end())
#define rsort(n) sort((n).begin(),(n).end(),greater<int>())
using ll=long long;
using ld=long double;
typedef pair<int,int> P;
typedef pair<P,int> COST;
#define repl(i,n) for(ll i=0;i<(n);++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf=vector<vector<int>>;

int main()
{
    ll x,y;
    cin >> x >> y;
    ll s=y/x;
    ll t=2;
    ll count=1;
    while(t<=s){
        t*=2;
        count++;
    }
    cout << count << endl;
}

Measured memory_bytes: 3624
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<28;
const ll INF=1ll<<50;
const double pi=acos(-1);
const double eps=1e-10;
const ll mod=1e9+7;
const vi emp;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,-0};
const int DX[8]={-1,-1,-1,0,0,1,1,1},DY[8]={1,0,-1,1,-1,1,0,-1};

int n,k;

int main(){
	cin>>n>>k;
	for(int i=0;i<k;i++){
		int x,y;
		cin>>x>>y;
		if(x>y) swap(x,y);
		if(x+y>n) x=n-y+1;
		int ans=x%3;
		if(!ans) ans=3;
		cout<<ans<<endl;
	}
}

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

int main() {
  int n;
  scanf("%d", &n);
  int d = 1;
  while (d + d <= n) { d += d; }
  if (d == n) {
    printf("No\n");
    return 0;
  }
  printf("Yes\n");
  if (n & 1) {
    for (int i = 2; i < n; i += 2) {
      printf("%d %d\n", i, i + 1);
      printf("%d %d\n", i + 1, 1);
      printf("%d %d\n", 1, n + i);
      printf("%d %d\n", n + i, n + i + 1);
    }
    printf("%d %d\n", n + 1, 2);
  } else {
    for (int i = 2; i < d + 2; i += 2) {
      printf("%d %d\n", i, i + 1);
      printf("%d %d\n", i + 1, 1);
      printf("%d %d\n", 1, n + i);
      printf("%d %d\n", n + i, n + i + 1);
    }
    printf("%d %d\n", n + 1, 2);
    for (int i = d + 2; i <= n; i++) {
        printf("%d %d\n", i, n + d);
        printf("%d %d\n", (((i - d) & 1) == 0) ? (i - d + 1) : (n + i - d - 1), n + i);
    }
  }
  return 0;
}


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

int main(){
    int a,b,c,d,result;
    cin >> a >> b >> c >> d;
    if(a <= b){
        result = a;
    } else {
        result = b;
    }
    if(c <= d){
        result = result + c;
    } else {
        result = result + d;
    }
    cout << result << endl;
}

Measured memory_bytes: 256
import numpy as np
mod=10**9+7
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
A=np.array(A)

def cmb(n, k, mod, fac, ifac): #calc nCk under mod
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod

def make_tables(mod, n): #Make factorial and inverse tables under mod 
    fac = [1, 1] # factorial table
    ifac = [1, 1] #factorial of inverse table
    inverse = [0, 1] #inverse
    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac
  
fac,ifac = make_tables(mod,N+1)
MAX=0
MIN=0

for i,a in enumerate(A):
  if i>=K-1:
    MAX+=cmb(i, K-1, mod, fac, ifac)*a
    MAX%=mod
  if N-i-1>=K-1:
    MIN+=cmb(N-i-1, K-1, mod, fac, ifac)*a
    MIN%=mod
print((MAX-MIN)%mod)

Measured memory_bytes: 26872
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		PrintStream out = System.out;
		int m, f, r;
		while(true){
			String[] score = in.readLine().split(" ");
			m = Integer.parseInt(score[0]);
			f = Integer.parseInt(score[1]);
			r = Integer.parseInt(score[2]);
			if(m==-1 && f==-1 && r==-1){
				break;
			}
			if(m==-1 || f==-1){
				out.println("F");
			}else if(m+f >= 80){
				out.println("A");
			}else if(m+f >= 65){
				out.println("B");
			}else if(m+f >= 50 || r>=50){
				out.println("C");
			}else if(m+f >= 30){
				out.println("D");
			}else {
				out.println("F");
			}
		}
	}
}

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

int main() {
  string s, t;
  cin >> s >> t;
  sort(s.begin(), s.end());
  sort(t.begin(), t.end(), greater<char>());
  if(s < t)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Measured memory_bytes: 256
import math
X=int(input())
flag=0
ans=0
for i in range(100000):
      for j in range(2,round(math.sqrt(X))):
          if (X+i)%j==0:
              flag=1
              break
      if flag==0:
          ans=i
          break
      else:
          flag=0
print(ans+X)


Measured memory_bytes: 2940
fun main(args: Array<String>) {
    val (N, K) = readInputLine().split(" ").map { it.toInt() }
    
    val hs = readInputLine().split(" ").map { it.toInt() }
    
    var ans = 0
    
    for (h in hs) {
        if (h >= K) {
            ans++
        }
    }
    
    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}


Measured memory_bytes: 47676
#include "bits/stdc++.h"
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define ALLR(X) (X).rbegin(), (X).rend()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define F first
#define S second

using namespace std;
 
template<class T> void max_self(T & a, const T & b) { if(a < b) a = b; }
template<class T> void min_self(T & a, const T & b) { if(a > b) a = b; }
typedef long long LL;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = (1e-9);


int main(){
  FAST_IO  

  int H, W, white = 0, minPath = 0, round = 0; cin >> H >> W;
  vector<vector<char>> grid(H, vector<char>(W));
  queue<pair<int, int>> BFS;

  pair<int, int> directions[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

  for(int r = 0; r < H; ++r){
    for(int c = 0; c < W; ++c){
      char color; cin >> color;
      if(color == '.') white++;
      grid[r][c] = color;
    }
  }

  BFS.push({0, 0});
  grid[0][0] = '#';

  auto valid = [&](int r, int c){
    if(r < 0 || r >= H || c < 0 || c >= W) return false;
    if(grid[r][c] == '#') return false;
    return true;
  };

  while(!BFS.empty()){
    int size = BFS.size();
    for(int rep = 0; rep < size; ++rep){
      auto cur = BFS.front();
      BFS.pop();
      int row = cur.F, col = cur.S;
      if(row == H - 1 && col == W - 1){
        minPath = round;
        break;
      }

      for(auto dir : directions){
        int nxtR = row + dir.F, nxtC = col + dir.S;
        if(!valid(nxtR, nxtC)) continue;
        grid[nxtR][nxtC] = '#';
        BFS.push({nxtR, nxtC});
      }
    }
    round++;
  }

  if(grid[H - 1][W - 1] == '.') cout << "-1\n";
  else cout << white - minPath - 1 << "\n";



  return 0;
}

Measured memory_bytes: 256
#![allow(unused, non_snake_case, dead_code, non_upper_case_globals)]
use {
    proconio::{marker::*, *},
    std::{cmp::*, collections::*, mem::*},
};

#[fastout]
fn main() {
    input! {//
        a:i64,b:i64,c:i64,
    }
    println!("{}", max(c - (a - b), 0));
}


Measured memory_bytes: 2124
N = int(input())

a_list = []
for i in range(N):
    a_list.append(int(input()))

b_in = a_list.index(max(a_list))
b_max = max(a_list)

a_list.pop(b_in)

c_max = max(a_list)

for i in range(N):
    if b_in != i:
        print(b_max)
    else:
        print(c_max)

Measured memory_bytes: 12532
#include<bits/stdc++.h>
    using namespace std;
     
    int main()
    {
        string s;
        cin>>s;
        int n=s.size();
        
        int c1=0,c2=0;
        for(int i=0;i<n;i++){
           if(s[i]=='0' and i%2) c1++;
           if(s[i]=='1' and !(i%2)) c1++;
        }
         
      for(int i=0;i<n;i++){
           if(s[i]=='1' and i%2) c2++;
           if(s[i]=='0' and !(i%2)) c2++;
        }
        cout<<min(c1,c2)<<endl;
      
    }

Measured memory_bytes: 512
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <climits>
#include <numeric>
#include<stdio.h>
#include <queue>
#include <iomanip>
#include <float.h>
#include <set>
#include<functional>
#include <stack>
#include <time.h>
#include <climits>
#include <bitset>
#include <fstream>
using namespace std;
typedef pair<long long,long long>p;
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
vector<long long> divisor(long long n) {
	vector< long long> ret;
	for (long long i = 1; i * i <= n; i++) {
		if (n % i == 0) {
			ret.push_back(i);
			if (i * i != n) ret.push_back(n / i);
		}
	}
	sort(begin(ret), end(ret));
	return (ret);
}
long long mod = 1e9 + 7;
struct LazySegmentTree {
private:
	int n;
	vector<int> node, lazy;
	vector<bool> lazyFlag;

public:
	LazySegmentTree(vector<int> v) {
		int sz = (int)v.size();
		n = 1; while (n < sz) n *= 2;
		node.resize(2 * n - 1);
		lazy.resize(2 * n - 1, INT_MAX);
		lazyFlag.resize(2 * n - 1, false);

		for (int i = 0; i<sz; i++) node[i + n - 1] = v[i];
		for (int i = n - 2; i >= 0; i--) node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
	}

	void lazyEvaluate(int k, int l, int r) {
		if (lazyFlag[k]) {
			node[k] = lazy[k];
			if (r - l > 1) {
				lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
				lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
			}
			lazyFlag[k] = false;
		}
	}

	void update(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		lazyEvaluate(k, l, r);
		if (b <= l || r <= a) return;
		if (a <= l && r <= b) {
			lazy[k] = x;
			lazyFlag[k] = true;
			lazyEvaluate(k, l, r);
		}
		else {
			update(a, b, x, 2 * k + 1, l, (l + r) / 2);
			update(a, b, x, 2 * k + 2, (l + r) / 2, r);
			node[k] = min(node[2 * k + 1], node[2 * k + 2]);
		}
	}

	int find(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		lazyEvaluate(k, l, r);
		if (b <= l || r <= a) return INT_MAX;
		if (a <= l && r <= b) return node[k];
		int vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
		int vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
		return min(vl, vr);
	}
};
typedef pair<int, p> pai;
signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout <<fixed<< setprecision(16);
	int n, a, b, c, d;
	cin >> n >> a >> b >> c >> d;
	string s;
	cin >> s;
	string ans = "Yes";
	for (int i = b-1; i <= d-1; i++) {
		if (s[i] == '#'&&s[i + 1] == '#')ans = "No";
	}
	for (int i = a-1; i <= c-1; i++) {
		if (s[i] == '#'&&s[i + 1] == '#')ans = "No";
	}
	if (c > d) {
		int judge = 0;
		for (int i = b - 1-1; i <= d - 1 - 1; i++) {
			if (s[i] == '.'&&s[i + 1] == '.'&&s[i + 2] == '.')judge = 1;
		}
		if (judge == 0)ans = "No";
	}
	cout << ans;
}

Measured memory_bytes: 592
from sys import stdin
input = stdin.readline

N = 100
WHITE = 0 # 未訪問
GRAY = 1 # 訪問したが、スタックに残っている
BLACK = 2 # 訪問完了済み

M = [[0]*N for _ in range(N)] # n*nの隣接行列の作成
color = [WHITE]*N  # colorの初期化
d = [0]*N # 最初の訪問時刻のタイムスタンプ
f = [0]*N # 完了時刻のタイムスタンプ

def dfs_visit(u): # uの訪問処理
    global tt
    color[u] = GRAY
    tt += 1
    d[u] = tt # 最初の訪問
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE: # 深さがあれば再帰的に処理をする
            dfs_visit(v)
    color[u] = BLACK # uに関する全ての深さを探索しきった処理
    tt += 1
    f[u] = tt # 訪問の完了

def dfs():
    global tt
    tt = 0

    for u in range(n):
        if color[u] == WHITE: # 未訪問のuを始点にする
            dfs_visit(u)
    for u in range(n): # 最終的な表示処理
        print(str(u+1) + ' ' + str(d[u]) + ' ' + str(f[u]))

n = int(input())
for _ in range(n): # 隣接行列の作成
    u, k, *vv = input().split()
    u = int(u)-1  # 0オリジンにする
    k = int(k)    # kは使わない
    for v in vv:
        v = int(v)-1
        M[u][v] = 1
dfs()



Measured memory_bytes: 5752
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
using namespace std;


typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
#define PB push


#define MOD 1000000007LL
#define EPS 1e-10

int main()
{
	int H, W;
	cin >> H >> W;
	vector<string> a;
	int h = 0;
	REP( i, H )
	{
		string tmp;
		cin >> tmp;
		bool isAllWhite = true;
		for( auto c : tmp )
		{
			if( c != '.' ) isAllWhite = false;
		}
		if( ! isAllWhite )
		{
			a.push_back( tmp );
			++h;
		}
	}
	H = h;

	REP( j, W )
	{
		bool isAllWhite = true;
		REP( i, H )
		{
			if( a[ i ][ j ] != '.' ) isAllWhite = false;
		}
		if( isAllWhite )
		{
			REP( i, H )
			{
				a[ i ] = a[ i ].substr( 0, j ) + a[ i ].substr( j + 1 );
			}
			--j;
			--W;
		}
	}

	REP( i, H )
	{
		cout << a[ i ] << endl;
	}
	return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define int long long
using P = pair<int,int>;
//#########################################
signed main(){
    int n;cin >> n;
    int a[n];
    vector<int> s(n+1);
    rep(i,n){
        cin >> a[i];
        s[i+1] = s[i] + a[i];
    }
    int top = n,bot = 0;
    while(top - bot >1){
        int mid = (top + bot)/2;
        if(s[mid]*2 < s[n])bot = mid;
        else top = mid;
    }
    int ans = min(abs(s[bot] - (s[n] - s[bot])),abs(s[top] - (s[n] - s[top])));
    cout << ans <<endl;

    return 0;
}

Measured memory_bytes: 3456
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
s = [lc() for _ in range(n)]

ans = 0

stb_eda = 0
stb = 0
eda = 0

for si in s:
    stmp = "".join(si)
    ans += stmp.count("AB")
    if si[0] == "B" and si[-1] == "A":
        stb_eda += 1
    elif si[0] == "B":
        stb += 1
    elif si[-1] == "A":
        eda += 1

if stb >= eda:
    diff = min(stb - eda, stb_eda)
    eda += diff
    stb_eda -= diff

else:
    diff = min(eda - stb, stb_eda)
    stb += diff
    stb_eda -= diff


if eda > 0 or stb > 0:
    ans += min(eda, stb) + stb_eda
else:
    ans += min(eda, stb) + max(0, stb_eda-1)

print(ans)

Measured memory_bytes: 4596