akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <utility>
using namespace std;

typedef long long ll;
ll MOD = 1e9+7;

ll dp[3001][3001];//dp[i][j]...区間[i,j)において先手-後手の最大値
ll a[3000];
int main()
{
	int n;
	int ans=0;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i];
	fill(dp[0],dp[3001],0);
	for(int i=0;i<n;i++)
		dp[i][i+1]=a[i];
	for(int i=2;i<n+1;i++)
	{
		for(int j=i;j<n+1;j++)
		{
			dp[j-i][j]=max(a[j-i]-dp[j-i+1][j],a[j-1]-dp[j-i][j-1]);
		}
	}
	cout<<dp[0][n]<<endl;
	return 0;
}

Measured memory_bytes: 70656
#include<bits/stdc++.h>
using namespace std;
int dp[100003],n,m,ans;
vector<int>v[100003];
int dfs(int x){
	if(dp[x])return dp[x];
	for(int i=0;i<v[x].size();i++)
		dp[x]=max(dfs(v[x][i])+1,dp[x]);
	ans=max(ans,dp[x]);
	return dp[x];
}
int main(){
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		v[a].push_back(b);
	}
	for(int i=0;i<n;i++)
		if(!dp[i])
			dfs(i);
	cout<<ans;
}

Measured memory_bytes: 8192
#include <iostream>

using namespace std;

int main()
{
  int n, m, s, t[101];
  char str[101][9], st[9];
  bool f;

  while(cin >> n >> m && n && m){
    for(int i = 0; i < n; i++){
      cin >> str[i] >> t[i];
    }
    s = 0;
    for(int i = 0; i < m; i++){
      cin >> st;
      for(int k = 0; k < n; k++){
	f = true;
	for(int j = 7; j >= 0; j--){
	  if(str[k][j] == '*'){
	    break;
	  } else if(str[k][j] != st[j]){
	    f = false;
	    break;
	  }
	}
	if(f){
	  s += t[k];
	}
      }
    }
    cout << s << endl;
  }

  return 0;
}

Measured memory_bytes: 0
function Main(input) {
  input = input.split("\n");
  var N = parseInt(input)%10;
  var ans = "pon";
  if(N==2||N==4||N==5||N==7||N==9){
    ans="hon";
  }else if(N==3){
    ans="bon";
  }

  console.log('%s', ans);
  return ans;
}

function debug(n){
  var input = document.getElementById("input"+n).value;
  var ans=Main(input);
  var result="WA";
  if(ans==document.getElementById("output"+n).value.split("\n")[0]){
    result="AC";
  }
  document.getElementById("result"+n).innerHTML=result;
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 30132
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )

using namespace std;

typedef pair<int, int> P;

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

template <typename T>
using vector2 = vector<vector<T>>;
template <typename T>
vector2<T> initVec2(size_t n0, size_t n1, T e = T()){
    return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T>
using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()){
    return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

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

    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    sort(ALL(a));

    int ans = a.back() - a[0];
    vector<int> p, m;
    m.push_back(a[0]);
    for(int i = 1; i < n - 1; i++){
        ans += abs(a[i]);
        if(a[i] >= 0){
            p.push_back(a[i]);
        }else{
            m.push_back(a[i]);
        }
    }

    cout << ans << endl;
    for(int x : p){
        cout << m.back() << " " << x << endl;
        m.back() -= x;
    }
    for(int x : m){
        cout << a.back() << " " << x << endl;
        a.back() -= x;
    }

    return 0;
}


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

void solve()
{
    int N, A;
    cin >> N >> A;
    int x[N + 1];
    long long ans = 0;
    int M = 0;
    x[0] = 0;
    for (int i = 1; i <= N; ++i)
    {
        cin >> x[i];
        M = max(x[i], M);
    }
    int X = max(M, A);
    long long dp[N + 5][N + 5][N * X + 5] = {};
    dp[0][0][0] = 1;
    for (int i = 1; i <= N; ++i)
    {
        for (int j = 0; j <= i; ++j)
        {
            for (int s = 0; s <= N*X+1; ++s)
            {
                if (i >= 1 && s < x[i])
                {
                    dp[i][j][s] = dp[i - 1][j][s];
                }
                else if (i >= 1 && j >=1 && x[i] <= s)
                {
                    dp[i][j][s] = dp[i - 1][j][s] + dp[i - 1][j - 1][s - x[i]];
                }
                // cout << "From i: "<< " How many j:"<< j <<"個選ぶ。"<<" s: " << s <<" dp[i][j][s]: " << dp[i][j][s] << endl;
            }
        }
    }
    for(int i = 1; i <= N; ++i){
        ans += dp[N][i][i * A];
        // cout << dp[N][i][i * A] <<endl;
    }
    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}

Measured memory_bytes: 62808
#include <iostream>

#define MAX 100001
using namespace std;

int main()
{
    long long nr;
    cin >> nr;
    long long arr[MAX];
    long long i;
    long long div4 = 0, div2 = 0, impare = 0;
    for(i = 0; i != nr; i++){
        cin >> arr[i];
        if(arr[i] % 4 == 0)div4++;
        else if(arr[i] % 2 == 0) div2++;
        else impare++;
    }

    if(div2 % 2 != 0) impare++;

    if(impare > 0 && div4 < div2){
        cout<<"No"<<endl;
        return 0;
    }
    if(impare > div4 + 1){
        cout<<"No"<<endl;
    }
    else cout<<"Yes"<<endl;

    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <stdio.h>
#include <bitset>
#include <limits.h>
#include <complex>
#include <deque>
#include <iomanip>
using namespace std;
#define pi pair<int32,int32>
#define pl pair<int64,int64>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
#define en cout << endl //セミコロンつけろ
//#define MM 1000000000
//#define MOD MM+7
const int MM = 1e9;
const int MOD = MM+7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1,0,1,0,-1,-1,1,1};
int dy[8] = {0,-1,0,1,-1,1,1,-1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
template<typename T>
T GCD(T u,T v){
    return v ? GCD(v,u%v) : u;
}
template<typename T>
T LCM(T x,T y){
    T gc = GCD(x,y);
    return x*y/gc;
}

long long Pow(long long base,long long x){
    //マクロ定義のままMODを使ったらエラーを起こした(多分long long の定義の問題(const long long などに要変更))
    const long long mod = (long long)MOD;
    if (x == 0) return 1;
    //指数が奇数の時、x^(2n+1) -> x * x^(2n)と変換
    if (x%2 == 1) return base * Pow(base,x-1) % mod;
    //指数が偶数なら x^(2n) -> x^(2n) * x^(2n)と変換
    long long y = Pow(base, x/2);
    return y * y % mod;
}
   

int main(){
    long long m,n; cin >> m >> n;
    cout << Pow(m,n) << endl;
}


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

string s[200000];
int main() {
	//A
	/*int a, b, c;
	cin >> a >> b >> c;
	if (a == b && b != c) {
		cout << "Yes" << endl;
	}
	else if (b == c && c!= a) {
		cout << "Yes" << endl;
	}
	else if (c == a && a != b) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
	*/
	//B
	/*int n;
	int a[100];
	cin >> n;
	int i;
	for (i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] % 2 == 0) {
			if (a[i] % 3 == 0) {

			}
			else if (a[i] % 5 == 0) {

			}
			else {
				cout << "DENIED" << endl;
				return 0;
			}
		}
	}
	cout << "APPROVED" << endl;
	return 0;
	*/
	//C
	/*int i, n;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> s[i];
	}
	sort(s, s + n);
	//cout<< endl << "sort" << endl;
	//for (i = 0; i < n; i++) {
	//	cout << s[i] << endl;
	//}
	//cout << "sort" << endl << endl;
	string t;
	t = s[0];
	int counter = 1, maxc = 0;
	for (i = 1; i < n; i++) {
		if (s[i] == t) {
			counter++;
		}
		else {
			t = s[i];
			if (counter > maxc)maxc = counter;
			counter = 1;
		}

	}
	if (counter > maxc)maxc = counter;

	t = s[0]; counter = 1;
	for (i = 1; i < n; i++) {
		if (s[i] == t) {
			counter++;
		}
		else {
			t = s[i];
			if (counter  == maxc)cout << s[i-1] << endl;
			counter = 1;
		}

	}
	if (counter == maxc) {
		cout << s[n - 1] << endl;
	}
	return 0;*/
			
	//D
	/*int n,k;
	cin >> n >> k;
	int i;
	long long int a[200000];
	for (i = 0; i < n; i++) {
		cin >> a[i];
	}*/
	//E
	string s;
	cin >> s;
	int i, j;
	int flag = 0;
	long long int sum = 0;
	for (i = s.size() - 1; i >= 0; i--) {
		j = s[i] - '0' + flag;
		if (j == 10) {
			j = 0;
			flag = 1;
		}
		else {
			flag = 0;
		}
		if (j < 5) {
			sum = sum + j;
		}
		else if (j == 5) {
			if (i == 0) {
				sum = sum + 5;
			}
			else if (s[i-1] - '0' < 5) {
				sum = sum + 5;
			}
			else {
				sum = sum + 5;
				flag = 1;
			}
		}
		else {
			sum = sum + 10 - j;
			flag = 1;
		}
	}
	cout << sum + flag << endl;
	return 0;


}

Measured memory_bytes: 11572
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 2147483647 / 2;
const long long int llINF = 9223372036854775807 / 2;

using namespace std;
using ll = long long int;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;

typedef pair<ll, ll> pll;
bool paircomp(const pll &a, const pll &b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first < b.first;
}

#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define AUTO(i, m) for (auto &i : m)
#define ALL(a) (a).begin(), (a).end()
#define MAX(vec) *std::max_element(vec.begin(), vec.end())
#define MIN(vec) *std::min_element(vec.begin(), vec.end())
#define ARGMAX(vec)                                                            \
  std::distance(vec.begin(), std::max_element(vec.begin(), vec.end()))
#define ARGMIN(vec)                                                            \
  std::distance(vec.begin(), std::min_element(vec.begin(), vec.end()))
#define REV(T) greater<T>()
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define VVL(a, b, c) vector<vector<ll>>(a, vector<ll>(b, c))
#define VV(T, a, b, c) vector<vector<T>>(a, vector<T>(b, c))
#define VVVL(a, b, c, d)                                                       \
  vector<vector<vector<ll>>>(a, vector<vector<ll>>(b, vector<ll>(c, d)))
#define VVV(T, a, b, c, d)                                                     \
  vector<vector<vector<T>>>(a, vector<vector<T>>(b, vector<T>(c, d)))
#define SP(a) setprecision(a)
#define SQRT(a) sqrt((long double)(a))
#define DPOW(a, b) pow((long double)(a), (long double)(b))
#define UNIQUE(vec)                                                            \
  do {                                                                         \
    sort(ALL((vec)));                                                          \
    (vec).erase(std::unique(ALL((vec))), (vec).end());                         \
  } while (0)

ll POW(ll n, ll m) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    ll tmp = POW(n, m / 2);
    return (tmp * tmp);
  } else {
    return (n * POW(n, m - 1));
  }
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N;
  vl A;
  string S;
  cin >> S;
  REP(i, 3) cout << S[i] << "";
  cout << endl;
  return 0;
}


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

long long A;
long long B;
long long N;

long long floor_func(long long x) {
    return (A * x / B) - A * (x / B);
}

int main() {
    cin >> A >> B >> N;
    long long ans = floor_func(N);
    if (N >= B) {
        long long x = (N / B) * B - 1;
        while(true) {
            long long tmp = floor_func(x);
            if (ans < tmp) {
                ans = tmp;
                x -= B;
            }
            else {
                break;
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3528
a=int(input())
b=int(input())
c=int(input())
x=int(input())
num=0
for i in range(a+1):
	x_i=x-500*i
	if x_i<0:
		break
	for j in range(b+1):
		#print('j:',j)
		x_j=x_i-100*j
		if x_j<0:
			break
		if x_j/50<=c:
			num+=1
			#print(x_j)
print(num)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL -1
using namespace std;
const int64_t MOD = 1e9 + 7;

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

	vector<int> v(N);
	int sum = 0;

	int m = 100;
	for (int i = 0; i < N; i++) {
		cin >> v.at(i);
		sum += v.at(i);

		if (v.at(i) % 10) {
			if (v.at(i) < m) m = v.at(i);
		}
	}

	if (sum % 10) {
		cout << sum << endl;
	}
	else {
		if (m == 100) cout << 0 << endl;
		else cout << sum - m << endl;
	}
}

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

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!p)
#define until(p) while(!p)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

// I want to delete this stupid solution ;(
#define sandwich(x) [](auto a, auto b){return (x)(a, b);}

const ll MOD = 1e9 + 7;

template <typename T>
using F2 = std::function<T(T,T)>;

template <typename T, int N, typename F = std::plus<T> >
struct SegmentTree{
    SegmentTree(T u, F f = F()){
        size = 1;
        while(size < N){
            size <<= 1;
        }

        unit = u;
        func = f;

        init();
    }
    void init(){
        for(int i=0;i<size;++i){
            data[i+size] = unit;
        }

        for(int i=size-1;i>0;--i){
            data[i] = func(data[i*2], data[i*2+1]) % MOD;
        }
    }
    void update(int k, int v){
        k += size;
        data[k] = v;

        while(k > 1){
            k >>= 1;
            data[k] = func(data[k*2], data[k*2+1]) % MOD;
        }
    }
    inline T query(int l, int r){
        return _query(l, r, 1, 0, size);
    }
    T _query(int a, int b, int k, int l, int r){
        if(b <= l || r <= a){return unit;}
        if(a <= l && r <= b){return data[k];}
        int mid = (l + r) >> 1;
        return func(_query(a, b,   2*k,   l, mid),
                    _query(a, b, 2*k+1, mid, r  )) % MOD;
    }

    T unit, data[2098000];
    int size;
    F func;
};

SegmentTree<ll, 1000100> segtree(0);
ll N;
ll dp[1000100];

ll rec(int k){
    ll res = dp[k-1]; // 1*... pattern
    res = (res + (N-1) * (N-1) % MOD) % MOD; // 233... pattern
    // 211*... pattern
    res = (res + segtree.query(0, k - 2)) % MOD;
    res = (res + N + 1 - k) % MOD;

    segtree.update(k, res);
    return dp[k] = res;
}

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

    std::cin >> N;

    memset(dp, -1ll, sizeof(dp));
    dp[0] = 1;
    dp[1] = N;
    segtree.update(0, 1);
    segtree.update(1, N);

    for(int k=2;k<=N;++k){
        rec(k);
    }
    
    std::cout << dp[N] << std::endl;
}


Measured memory_bytes: 24448
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  int a,b;
  int c,d;
  a=N/500;
  c=a*1000;
  a*=500;
  N-=a;
  b=N/5;
  d=b*5;
  int e;
  e=c+d;
  cout << e <<endl;
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

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

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s, f) cout << fixed << setprecision(f) << s << endl;


int main()
{
	ll a, b;
	cin >> a >> b;

	if (a <= b)
	{
		println("unsafe");
	}
	else
	{
		println("safe")
	}
}

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

int n, p[30001], a[30001], b[30001], r[30001];

int main()
{
    cin >> n;
    for(int i = 1; i<=n;++i)
        cin >> p[i];
    for(int i = 1; i<=n; ++i)
        r[p[i]] = i;

    for(int i=1; i<=n;++i)
        a[i] = i*30000;
    for(int i=1; i<=n; ++i)
        b[i] = 30000*(n-i) + r[i];

    for(int i=1; i<=n;++i)
        cout << a[i] <<' ';
    cout <<'\n';
    for(int i=1; i<=n; ++i)
        cout << b[i] << ' ';


    return 0;
}


Measured memory_bytes: 896
A,B,C = map(int,input().split())

if any(map(lambda x:x%2 == 1, (A,B,C))):
    print(0)
elif A == B == C:
    print(-1)
else:
    jk = 0
    since1992 = A + B + C
    while all(map(lambda x :x % 2 == 0, (A,B,C))):
        A = (since1992 - A) / 2
        B = (since1992 - B) / 2
        C = (since1992 - C) / 2
        jk += 1
    print(jk)


Measured memory_bytes: 3064
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
typedef long long ll;
//max ll constant  = LLONG_MAX

using namespace std;


vector <int> A(3);
vector <vector <int>> dp(100000, vector <int> (3, -1));

int idx,N;
int f,s,ans = -1;

int main() {
  cin >> N;
  for(int j = 0; j < N; j++ ){
    cin >> A[0] >> A[1] >> A[2];
    f = s = -1;
    for(int n = 0; n < 3; n++){
      if(A[n] > f){
        f = A[n];
        idx = n; 
      }
    }
    if(j == 0){
      for(int x = 0; x < 3; x++){
        if(A[x] == f){
          dp[j][x] = f;
        }
      }
    }
    else{
      for(int n = 0; n < 3; n++){
        for(int r = 0; r < 3; r++){  
          if(r == n || dp[j-1][r] == -1){
            continue;
          }
          else{
            dp[j][n] = max(dp[j][n], A[n] + dp[j-1][r]);
          }
        }
      }
    }
  }
  // for(int u = 0; u < N; u++){
  //   cout << dp[u][0] << " " << dp[u][1] << " " << dp[u][2] << endl;
  // }
  for(int l = 0; l < 3; l++){
    if(dp[N-1][l] > ans){
      ans = dp[N-1][l];
    }
  }
  cout << ans;
}


Measured memory_bytes: 8620
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int n,a,b,i,x=0,t=0;
	cin >> n;
	cin >> a;
	for(i=1;i<n;i++){
		cin >> b;
		if(b>a){
			if(t<x)
				t=x;
			x=0;
		} 
		else{
			x++;
		}
		a=b;
	}
	if(t<x)
		t=x;
	cout << t << endl; 
	return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

int main(){
	int n;
	int i;
	while(scanf("%d",&n)){
		int dig=1;
		long long int ans=0;
		if(n==-1){
			break;
		}
		
		for(i=0;i<21;i+=2){
			if(n&1<<i){
				ans+=dig*1;
			}
			if(n&1<<(i+1)){
				ans+=dig*2;
			}
			dig*=10;
		}
		printf("%lld\n",ans);
	}
	
	return 0;
}

Measured memory_bytes: 600
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    m = [0] * n
    m[0] = 0
    for i in range(1, n):
        s = max(0, i-k)
        m[i] = min([ml + abs(hl - h[i]) for ml, hl in zip(m[s:i], h[s:i])])
    print(m[-1])

main()


Measured memory_bytes: 13976
Scanf.scanf "%d %d" (fun n q ->
    Scanf.scanf " %s" (fun s ->
        let a = Array.make n 0 in
        let rec loop i prev cur =
            if i < n then (
                let cur = if prev = 'A' && s.[i] = 'C' then cur + 1 else cur in
                a.(i) <- cur;
                loop (i + 1) s.[i] cur
            )
        in
        loop 1 s.[0] 0;

        for i = 1 to q do
            Scanf.scanf " %d %d" (fun l r ->
                Printf.printf "%d\n" @@
                    if l = 1 then a.(r - 1)
                             else a.(r - 1) - a.(l - 1)
            )
        done
    )
)

Measured memory_bytes: 6400
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<string>
using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

pair<int, int> f(int x) {
    int a = x % 10;
    int b = 0;
    while (x) {
        b = x;
        x /= 10;
    }
    return pair<int, int>(a, b);
}

int main(){
    int n;
    cin >> n;
    map<pair<int, int>, int> mp;
    for ( int i = 1; i <= n; ++i ) {
        pair<int, int> p = f(i);
        ++mp[p];
    }
    ll ans = 0;
    for ( int i = 1; i <= n; ++i ) {
        pair<int, int> p = f(i);
        pair<int, int> q(p.second, p.first);
        ans += mp[q];
    }
    cout << ans << endl;
    return 0;
}

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

int main(){

	//freopen("in.txt","r",stdin);

	int n,a,b;
	scanf("%d%d%d",&n,&a,&b);

	if(a + b - 1 > n || a * 1LL * b < n) return puts("-1"), 0;

	vector<vector<int> > v;
	v.resize(a);
	for(int i=n - a + 1,j=0;i <= n;i++, j++){
		v[j].pb(i);
	}

	int cb=0,in=0;
	while(cb < n - a){
		cb++;
		int mx=min(cb + (b - 2),n - a);
		for(int i=mx;i >= cb;i--){
			v[in].pb(i);
		}
		in++;
		cb=mx;
	}
	for(int i=0;i < v.size();i++){
		for(int j=0;j < v[i].size();j++){
			printf("%d ",v[i][j]);
		}
	}

	return 0;
}


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

int main(){
    int n, m, c;
    cin >> n >> m >> c;
    vector<int> b(m);
    for (int i = 0; i < m; i++)
    {
        cin >> b[i];
    }
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int temp;
            cin >> temp;
            sum += temp * b[j];
        }
        if(sum+c>0)
        {
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <math.h>

int main()
{
    int n;
    scanf("%d",&n);
    char nstr[11];
    sprintf(nstr,"%d",n);

    int sn=0;
    for(int i=0;;i++)
    {
        if(nstr[i]=='\0') break;
        sn+=nstr[i]-'0';
    }
    if(n%sn) printf("No\n");
    else printf("Yes\n");
}

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

int N , R , L ;
int d , t , x ;
int mt = 0 ;
int num[100010] = {} ;
int tree[262144][2] = {} ;

void f( int p ){
	if( p==0 ) return ;
	
	if( tree[p*2][0] >= tree[p*2+1][0] ){
		tree[p][0] = tree[p*2][0] ;
		tree[p][1] = tree[p*2][1] ;
	}
	else {
		tree[p][0] = tree[p*2+1][0] ;
		tree[p][1] = tree[p*2+1][1] ;
	}
	
	f( p/2 ) ;
}

main(){
	cin >> N >> R >> L ;
	for( int i=1 ; i<=N ; i++ ) tree[pnt+i-1][1] = i ;
	f( pnt/2 ) ;
	for( int i=0 ; i<=R ; i++ ){
		if( i==R ) num[ tree[1][1] ] += L - mt ;
		else {
			cin >> d >> t >> x ;
			num[ tree[1][1] ] += t - mt ;
			mt = t ;
			tree[pnt+d-1][0] += x ;
			f( (pnt+d-1)/2 ) ;
		}
	}
	int mx = 0 , mxd = 0 ;
	for( int i=1 ; i<=N ; i++ ){
		if( mx < num[i] ){
			mx = num[i] ;
			mxd = i ;
		}
	}
	cout << mxd << endl ;
}

Measured memory_bytes: 3136
#include <stdio.h>
#include <string.h>

int main(void)
{
  char buff[100];
  fgets(&buff, sizeof buff, stdin);

  long a, b;
  a = atol(strtok(buff, " "));
  b = atol(strtok(NULL, " "));
  
  long ax =1, bx = 1;
  while (a * ax != b * bx) {
    if (a * ax < b * bx) {
      ax++;
    } else {
      bx++;
  	}
  }
  printf("%ld", a * ax);
  return 0;
}


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

int main() {
    int n;
    cin >> n;
    vector<string> s(n);
    for (int i = 0; i < n; i++) cin >> s[i];
    
    sort(s.begin(), s.end());
    vector<string> s_list;
    int s_max = 0, s_count = 1;
    string si = s[0];
    for (int i = 1; i < n; i++) {
        if (si == s[i])
            s_count++;
        else {
            if (s_max < s_count) {
                s_list.erase(s_list.begin(), s_list.end());
                s_list.push_back(s[i-1]);
                s_max = s_count;
            }
            else if (s_max == s_count)
                s_list.push_back(s[i-1]);
            s_count = 1;
            si = s[i];
        }
    }
    if (s_max < s_count) {
        s_list.erase(s_list.begin(), s_list.end());
        s_list.push_back(s[n-1]);
        s_max = s_count;
    }
    else if (s_max == s_count)
        s_list.push_back(s[n-1]);
    
    for (int i = 0; i < s_list.size(); i++)
        cout << s_list[i] << endl;
}

Measured memory_bytes: 15988
// BEGINNING WITH NAME OF ALMIGHTY GOD ALLAH
// AUTHOR:: MOHAMMAD FAISAL
// ATCODER BEGINNERS PROBLEMS
//#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.flush();
   	char c;
    int ans =0;
  	while(cin >> c)
      if(c=='1') ans++;
    cout<<ans;
    return 0;
    // Bye Bye 2019
}



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

using namespace std;

typedef long long ll;

int main(){
  int N, x , y;
  cin >> N;
  bool ans = false;
  for (int i = 1; i <= 9; i++){
    if (i * (N/i) == N && (N/i) <=9){
      ans = true;
      //cout << i << ' ' << (N/i) << endl;
      break;
    } 
  }
  if (ans == true) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}



Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:https://atcoder.jp/contests/abc134/tasks/abc134_f>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

const ll MOD = 1e9+7;
template<class Type>
Type solve(Type res = Type()){
    ll n,k; cin >> n >> k;
    auto dp = make_v<ll>(n+1,n+1,n*n+1);
    
    auto add = [&](ll& l,const ll& v){
        (l += v)%=MOD;
    };
    dp[0][0][0] = 1;
    for(int i = 0; i < n;i++){
        for(int j = 0; j < n;j++){
            for(int k = 0; k <= n*n;k++){
                if(dp[i][j][k] == 0) continue;
                
                // 点をつながない
                add(dp[i+1][j+1][k+2*(j+1)],dp[i][j][k]);
                
                // i+1ペア同士をつなぐ
                add(dp[i+1][j][k+2*j],dp[i][j][k]);
                
                // i+1ペアとiペア以前の点をつなぐ
                add(dp[i+1][j][k+2*j],dp[i][j][k]*2*j);
                
                if(j){//i+1ペアの両方をiペア以前のどれかにつなぐ
                    add(dp[i+1][j-1][k+2*(j-1)],dp[i][j][k]*j*j);
                }
            }
        }
    }
    res = dp[n][0][k];
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    // solve(0);
    cout << fixed << setprecision(15) << solve<ll>() << endl;
    return 0;
}


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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3") 
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x) 
#endif
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);

/* -----  2019/05/25  Problem: ABC 127 B / Link: http://abc127.contest.atcoder.jp/tasks/abc127_b  ----- */


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

	LL r, D, x; cin >> r >> D >> x;
	FOR(i, 0, 10) {
		LL nx = r * x - D;
		cout << nx << endl;
		x = nx;
	}

	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define sz(arr) (int)arr.size()
#define all(arr) arr.begin(), arr.end()

using namespace std;
typedef long long ll;

void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...) \
  T __VA_ARGS__;    \
  _cin(__VA_ARGS__)

int main() {
  Cin(int, a, b);

  int truth = 0;
  for (int i = 1; i < (b - a); ++i) truth += i;

  cout << truth - a << endl;

  return 0;
}

Measured memory_bytes: 256
n,m = map(int,input().split(' '))
data_ori = input().split(' ')
data = [int(data_ori[i]) for i in range(m)]
sum = 0
for i in range(m):
    sum += data[i]
if(n - sum < 0):
    print(-1)
else:
    print(n - sum)
    

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

using i64 = int64_t;

int main(){
    i64 n;
    cin >> n;
    i64 ans = 1e13;
    for(i64 i=1;i*i<=n;++i){
        if(n%i != 0)continue;
        i64 j = n / i;
        ans = min(ans, i+j-2);
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
K = gets.chop.to_i

arr = (1..9).to_a

def lulun(num)
  tail = num.split('')[-1]

  prev = (tail.to_i - 1).to_s
  forw = (tail.to_i + 1).to_s

  if tail == '0'
    return [(num + tail).to_i, (num + forw).to_i]
  elsif tail == '9'
    return [(num + prev).to_i, (num + tail).to_i]
  else
    return [(num + prev).to_i, (num + tail).to_i, (num + forw).to_i]
  end
end

def generate(arr1, arr2, count) # 完成した配列と更新したい配列を渡す
  tmp = [] # 新しく作った分を入れる

  arr2.each do |el|
    tmp.concat(lulun(el.to_s))
  end

  arr1.concat(tmp)

  if arr1.count > count
    return arr1
  else
    generate(arr1, tmp, count)
  end
end

luluns = generate(arr, arr, K)

puts luluns[K - 1]


Measured memory_bytes: 5284
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
#include<functional>
#include<ios>
#include<iomanip>

using namespace std;
typedef long long ll;
typedef pair<int,int>P;
string SS(450000,'.');
int main()
{
	string S;
	cin>>S;
	int sp=210000;
	int lp=sp;
	for(int i=0;i<S.length();i++)
	{
		SS[lp++]=S[i];
	}
	int Q;
	cin>>Q;
	bool flip=false;
	for(int i=0;i<Q;i++)
	{
		int n,f;
		char c;
		cin>>n;
		if(n==1)
		{
			flip=!flip;
		}
		else
		{
			cin>>f>>c;
			if((!flip&&f==1)||(flip&&f==2))
			{
				SS[--sp]=c;
			}
			else
			{
				SS[lp++]=c;
			}
		}

	}
	S=SS.substr(sp,lp-sp);
	if(flip)reverse(S.begin(),S.end());
	cout<<S<<endl;
}

Measured memory_bytes: 1436
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const ll MOD = (ll)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll cmb(ll r, ll c) {return fac[r] * inv[c] % MOD * inv[r - c] % MOD;}

int vec[100];

int main() {
    int n;
    scanf("%d", &n);

    for (int i  = 0; i < n; i++) {
        scanf("%d", &vec[i]);
    }

    int ans = 0;
    sort(vec, vec  +n);

    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            for (int k = j+1; k<n;k++) {
                if(vec[i]==vec[j] || vec[j]==vec[k]) continue;
                if (vec[i]+vec[j]>vec[k])ans++;
            }
        }
    }

    printf("%d", ans);
}

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

int main() {
    while(cin){
        int n, m;
        cin >> n >> m;
        if (n == 0 && m == 0) break;
        vector<long long> p(n);
        rep(i, n) {
            cin >> p[i];
        }

        vector<long long> patterns;
        rep(i, n) patterns.push_back(p[i]);
        rep(i, n) rep(j, n) patterns.push_back(p[i] + p[j]);
        sort(patterns.begin(), patterns.end());

        long long ans = 0;
        // rep(i, (int)patterns.size()){
        //     cout << patterns[i] << " ";
        // }
        // cout << endl;

        for (int i = 0; i < (int)patterns.size(); i++) {
            int maxi = upper_bound(patterns.begin(), patterns.end(), m - patterns[i]) - patterns.begin();
            maxi--;
            if (maxi >= 0) {
                ans = max(ans, patterns[i] + patterns[maxi]);
                // cout << i << " " << maxi << endl;
            }
        }
        cout << ans << endl;
    }
}


Measured memory_bytes: 18864
#include <iostream>
#include<algorithm>
#include<cmath>
#include <string>

 int main (){

	 int a, b, c, d;
	 int ans = 0;

	 std::cin >> a >> b >> c >> d;

	 if (a*b > c*d) {
		 ans += a*b;
	 }
	 else if (a*b < c*d) {
		 ans += c * d;
	 }
	 else {
		 ans += a * b;
	 }

	 std::cout << ans << std::endl;

	 return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <map>
#include <string>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<string> s(n);
  for(int i=0;i<n;i++)
    cin>>s[i];
  int sum=0;
  sort(s.begin(),s.end());
  s.erase(unique(s.begin(),s.end()),s.end());
  cout<<s.size()<<endl;
}


Measured memory_bytes: 9600
import sys
s = sys.stdin.read().lower()
count = [0 for i in range(26)]
for j in s:
    num = ord(j) - ord('a')
    if num >= 0 and num <= 25:
        count[num] += 1
for k in range(26):
    print(chr(k + 97) + " : " + str(count[k]))


Measured memory_bytes: 5568
#include<iostream>
using namespace std;
int V,E,R,S[500010],T[500010],D[500010];
int C[100010];
const int inf=10000*100000+100;
int main() {
    cin>>V>>E>>R;
    for(int i=0;i<E;++i){
        cin>>S[i]>>T[i]>>D[i];
    }
    for(int j=0;j<V;++j){
        if(j==R) C[R]=0;
        else C[j]=inf;
       
    }
    //cout<<C[0]<<C[1]<<endl;
    for(int t=0;t<V;++t){
        bool update=false;
        for(int i=0;i<E;++i){
            int s=S[i],t=T[i],d=D[i];
            if(C[s]<inf&&C[t]>C[s]+d){
                C[t]=C[s]+d;
                update=true;
            }
        }
        if(!update) break;
    }
    for(int j=0;j<V;++j){
        if(C[j]==1000000100){
            cout<<"INF"<<endl;
        }
       else{
        cout<<C[j]<<endl;
        }
    }
}

Measured memory_bytes: 9108
/*.....Bismillahir Rahmanir Rahim.....*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <unordered_map>
#include <queue>
#define   mod  998244353
#define   int  long long 
#define   ld long double
#define   pb push_back
#define  sz(x)  (int)x.size()
#define  ff first
#define  ss second
#define  pii  pair<int,int>
using namespace std;

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

void solve(){ 
	int  n,s=0;
	cin >> n ;
	for (int i=1;i<=n;i++) {
		for (int j=1;j<=n;j++) {
			for (int k=1;k<=n;k++) {
				int p= gcd(i,j);
				s += gcd(p,k);
			}
		}
	}
	cout << s << endl; 
}

signed main() {
	solve();
    return 0;
}


Measured memory_bytes: 3592
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

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

    ll n = s.size();

    ll k;
    cin >> k;

    int b = 1;
    char c = '0';
    for (int i = 0; i < min(n, k); i++) {
        if (s[i] != '1') {
            b = 0;
            c = s[i];
            break;
        }
    }

    if (b) {
        cout << 1 << endl;
    } else {
        cout << c << endl;
    }

    return 0;
}

Measured memory_bytes: 256
import numpy as np
N,les=list(map(int,input().split()))
data=np.array([list(map(int,input().split())) for _ in range(N)])
cost=0

t=data[:,0]
for n in np.argsort(data[:,0]):
    if data[n][1]>les:
        cost+=les*data[n][0]
        break
    else:
        les-=data[n][1]
        cost+=data[n][0]*data[n][1]
print(cost)

Measured memory_bytes: 37860
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class ForStudy
    {
        public static void Main(string[] args)
        {
            int[] nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

             int n = nums[0];
            int m = nums[1];
            int l = nums[2];

            var a = ReadMatrix(n, m);
            var b = ReadMatrix(m, l);
            var c = MatrixMultiply(a, b);
            ShowMatrix(c);
            
        }
        public static int[,] ReadMatrix(int row, int column)
        {
            var matrix = new int[row, column];
            for (int i = 0; i < row; i++)
            {
                int[] rowVector = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                for(int j= 0; j < column; j++)
                {
                    matrix[i, j] = rowVector[j];
                }

            }
            return matrix;
        }
        public static long[,] MatrixMultiply(int[,] a,int[,] b)
        {
            int row = a.GetLength(0);
            int indexNum = a.GetLength(1);
            int column = b.GetLength(1);
            long[,] result = new long[row,column];

            for(int i=0; i < row; i++)
            {
                for(int j = 0; j < column; j++)
                {
                    for(int k = 0; k < indexNum; k++)
                    {
                        result[i, j] += a[i, k] * b[k, j];
                    }
                }

            }

            return result;
        }
        public static void ShowMatrix(long[,] m)
        {
            for(int i = 0; i < m.GetLength(0); i++)
            {
                for(int j=0;j<m.GetLength(1); j++)
                {
                    Console.Write(m[i, j]);
                    if (j + 1 != m.GetLength(1))
                    {
                        Console.Write(" ");
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
            }
        }
    }
}



Measured memory_bytes: 20424
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

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

    string T = "";
    
    for (int i = 0; i< S.length(); i++){
        T += "x";
    }
    
    cout << T << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int memo[31];

int getways( int n );

int main( void )
{
	int n;
	int year;
	
	while( 1 )
	{
		scanf("%d",&n);
		if( n == 0 )break;
		year = (getways(n)/10)/365+1;
		
		printf("%d\n",year);
	}
	return 0;
}

int getways( int n )
{
	int ways;
	if( memo[n] != 0 )
	{
		return memo[n];
	}
	
	if( n <= 1 )
	{
		return 1;
	}
	if( n == 2 )
	{
		return 2;
	}
	
	return (memo[n] = getways(n-1) + getways(n-2) + getways(n-3) );
}

Measured memory_bytes: 600
import re
aflag = 0
bflag = 0
cnt = 0
cntna = 0

N = int(input())
for i in range(N):
    s = input()
    if s[-1:] == "A":
        aflag += 1
    if s[:1] == "B":
        bflag += 1
    if re.search("AB", s):
        cnt += 1
    if re.search("AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB.*AB", s):
        cnt += 1
    if re.search("ABABABABAB", s):
        cnt += 1
    if s[-1:]=="A" and s[:1]=="B":
        cntna +=1

cnt += min(aflag, bflag)
if min(aflag,bflag) == N:
    cnt -= 1
if cntna == min(aflag, bflag) and aflag == bflag and aflag != 0 and bflag != 0:
    cnt -= 1
if aflag == bflag == N:
    cnt += 1
print(cnt)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
#define ll          long long int
#define pb          push_back
#define pll         pair<ll,ll>
#define pq          priority_queue
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod         1000000007
#define inf         1000000000000000000LL
#define vl          vector<ll>
#define vll         vector<pll>
#define F           first
#define S           second
#define db          long double
#define sz(x)       (ll)x.size()
#define fix(n)      cout<<fixed<<setprecision(n)
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define all(x)      x.begin(),x.end()
#define mset(x)     memset(x,0,sizeof x)
#define pi          3.14159265358979323

char a[505][505];
ll vis[505][505],n,m,c1=0,c2=0,ans=0;

void dfs(ll i, ll j)
{
    vis[i][j]=1;
    if(a[i][j]=='.') c1++;
    else if(a[i][j]=='#') c2++;
    if(i-1>=0 && a[i-1][j]!=a[i][j] && !vis[i-1][j]) dfs(i-1,j);
    if(i+1<n && a[i+1][j]!=a[i][j] && !vis[i+1][j]) dfs(i+1,j);
    if(j-1>=0 && a[i][j-1]!=a[i][j] && !vis[i][j-1]) dfs(i,j-1);
    if(j+1<m && a[i][j+1]!=a[i][j] && !vis[i][j+1]) dfs(i,j+1);
}

void solve()
{
    cin>>n>>m;
    rep(i,0,n)
        rep(j,0,m)
            cin>>a[i][j];
    rep(i,0,n)
        rep(j,0,m)
            if(!vis[i][j])
               {
                   c1=0,c2=0;
                   dfs(i,j);
                   ans+=c1*c2;
               }
    cout<<ans;
}

int main()
{
    speed_up
    ll T=1;
    //cin>>T;
    while(T--)
        solve();
    return 0;
}


Measured memory_bytes: 17024
#include<iostream>
#include<vector>
#include<string>
#include<climits>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<queue>
#include<math.h>

int main(){
	int n, q;
	std::cin>>n>>q;

	std::vector<std::vector<int>> graph(n);

	std::vector<int> aList(n-1);
	std::vector<int> bList(n-1);
	std::vector<int> pList(q);
	std::vector<int> xList(q);

	for(int i=0; i<n-1; i++)
		std::cin>>aList[i]>>bList[i];

	for(int i=0; i<q; i++)
		std::cin>>pList[i]>>xList[i];

	for(int i=0; i<n-1; i++){
		graph[aList[i]-1].push_back(bList[i]-1);
		graph[bList[i]-1].push_back(aList[i]-1);
	}

	std::vector<bool> isAlreadyUsed(n, false);

	std::queue<int> que;
	que.push(0);
	isAlreadyUsed[0]= true;

	std::vector<long long> treeDP(n, 0);
	std::vector<long long> ans(n, 0);

	for(int i=0; i<q; i++)
		treeDP[pList[i]-1]+= xList[i];

	ans[0]= treeDP[0];

	while(!que.empty()){
		int cur= que.front();
		que.pop();

		for(int i=0; i<graph[cur].size(); i++){
			if(!isAlreadyUsed[graph[cur][i]]){
				isAlreadyUsed[graph[cur][i]]= true;
				que.push(graph[cur][i]);
				ans[graph[cur][i]]= ans[cur]+ treeDP[graph[cur][i]];
			}
		}
	}

	for(int i=0; i<n; i++){
		std::cout<<ans[i];
		if(i<n-1)
			std::cout<<" ";
	}

	std::cout<<std::endl;

	return 0;
}

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

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;

const int INF = 1e9;
const ll INFLL = 1e18;
const int MOD = 1e9 + 7;
const int NIL = -1;

#define rep(i,n) for(int i=0; i<(n); ++i)
#define all(n) begin(n),end(n)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define DEBUG

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	int n;
	cin >> n;
	int g = ((n - (n % 15)) / 15) * 200;
	cout << 800 * n - g << endl;

	return 0;
}

Measured memory_bytes: 256
#![allow(unused_imports, unused_macros, non_snake_case)]
use std::cmp::{max, min, Ordering};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

//{{{ macros missing in 1.15.1
#[macro_use]
mod macros {
    macro_rules! eprint {
        ($($t:tt)*) => {{
            use ::std::io::Write;
            let _ = write!(::std::io::stderr(), $($t)*);
        }};
    }
    macro_rules! eprintln {
        () => { eprintln!(""); };
        ($($t:tt)*) => {{
            use ::std::io::Write;
            let _ = writeln!(::std::io::stderr(), $($t)*);
        }};
    }
    macro_rules! dbg {
        ($val:expr) => {
            match $val {
                tmp => {
                    eprintln!(
                        "[{}:{}] {} = {:#?}",
                        file!(),
                        line!(),
                        stringify!($val),
                        &tmp
                    );
                    tmp
                }
            }
        };
    }
}
//}}}

fn run() {
    let stdin = std::io::stdin();
    let mut sc = Scanner::new(stdin.lock());
    let cs = sc.read_chars();
    let mut last = "".to_string();
    let mut cur = 0;
    let mut answer = 0;
    while cur < cs.len() {
        if last == format!("{}", cs[cur]) {
            if cur + 1 == cs.len() {
                break;
            }
            last = "".to_string();
            cur += 2;
            answer += 1;
        } else {
            last = format!("{}", cs[cur]);
            cur += 1;
            answer += 1;
        }
    }
    println!("{}", answer);
}

fn main() {
    std::thread::Builder::new()
        .name("run".to_string())
        .stack_size(1024 * 1024 * 1024)
        .spawn(run)
        .unwrap()
        .join()
        .unwrap()
}

//{{{ Scanner
pub struct Scanner<R> {
    reader: R,
}

impl<R> Scanner<R> {
    pub fn new(r: R) -> Scanner<R> {
        Scanner { reader: r }
    }
}

fn is_whitespace(b: u8) -> bool {
    b == b' ' || b == b'\n' || b == b'\r' || b == b'\t'
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        let buf = std::io::Read::bytes(self.reader.by_ref())
            .map(|b| b.expect("Read failed"))
            .skip_while(|&b| is_whitespace(b))
            .take_while(|&b| !is_whitespace(b))
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error")
    }

    pub fn read_vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }

    pub fn read_chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
//}}}


Measured memory_bytes: 8572