akhauriyash/Code-Regression huggingface dataset:
K, S = map(int, input().split())

cnt = 0
K += 1

#TLE CODE
'''
for x in range(K):
    for y in range(K):
        for z in range(K):
            if x + y + z == S:
                cnt += 1
'''

for x in range(K):
    for y in range(K):
        if 0 <= (S - (x + y)) < K:
            cnt += 1

print(cnt)


Measured memory_bytes: 2940
using namespace std;
typedef long long ll;
#include <string>
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>

#define out(S) cout<<(S)<<endl;
#define mod(i) ((i)%(ll)(1000000000 + 7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a * modpow((ll)b,(ll)(1e9+5))))
#define For(i,a,b) for(size_t i=(a);i<(b);i++)
#define rFor(i,a,b) for(size_t i=(a)-1;i>=(b);i--)
#define Rep(i,a) for(size_t i=0;i<(a);i++)
#define rRep(i,a) for(size_t i=(a)-1;i>=0;i--)
#define pb(i) push_back(i);
/*#define Foreach(item,collection) for(auto item:collection)*/
using vi = vector<int>;


int main(){
	int N;
	int m;
	vi ans;
	while (cin >> N&&N)
	{
		int m = 0;
		For(n, 1, N / 2 + 2) {
			int sum = 0;
			bool fflag = false;
			For(i, n, N / 2 + 2) {
				sum += i;
				if (i == n + 1) fflag = true;
				if (sum >= N) break;
			}
			if (sum == N&&fflag) m++;
		}
		ans.pb(m);
	}
	for (auto a : ans) out(a);
	return 0;
}

Measured memory_bytes: 3004
import sys
input = sys.stdin.readline

A, B, M = map(int, input().split())

a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 10 ** 6
for _ in range(M):
    x, y, c = map(int, input().split())
    ans = min(a[x - 1] + b[y - 1] - c, ans)

a.sort()
b.sort()

ans = min(ans, a[0] + b[0])

print (ans)

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

const int N = 1e5 + 10;

ll n, m, a[N];

map<ll, ll> cnt;

ll ans, s[N];

int main() {
	cin >> n >> m;
	for(int i = 1 ; i <= n ; ++ i) cin >> a[i], s[i] = s[i - 1] + a[i];
	cnt[0] = 1;
	for(int i = 1 ; i <= n ; ++ i) {
		ll t = s[i] % m;
		ans += cnt[t];
		++ cnt[t];
	}
	cout << ans << endl;
}

Measured memory_bytes: 5760
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;

int rec(vector<int> val, vector<int> weight, int i, int w)
{
    if (i == 0 || w == 0)
        return 0;
    // if(w < 0) return 0;
    if (weight[i - 1] > w)
    {
        rec(val, weight, i - 1, w);
    }
    return max(val[i - 1] + rec(val, weight, i - 1, w - weight[i - 1]), rec(val, weight, i - 1, w));
}

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

    vector<int> val(n);
    vector<int> weight(n);

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

    // cout << "weight" << endl;
    // for (auto i : weight)
    //     cout << i << " ";
    // cout << endl;
    // cout << "val" << endl;
    // for (auto i : val)
    //     cout << i << " ";
    // cout << endl;

    vector<vector<unsigned long long>> dp(n + 1, vector<unsigned long long>(w + 1, 0));

    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= w; j++)
        {
            if (i == 0 || j == 0)
                dp[i][j] = 0;
            else if (weight[i - 1] <= j)
            {
                dp[i][j] = max(val[i - 1] + dp[i - 1][j - weight[i - 1]], dp[i - 1][j]);
            }
            else
                dp[i][j] = dp[i - 1][j];
        }
    }

    cout << dp[n][w];
    // cout << rec(val, weight, n, w);
    return 0;
}

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

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))
#define rep(i,a,b) for(int (i)=int(a);(i)<int(b);(i)++)
#define rrep(i,a,b) for(int (i)=int(a);(i)>=int(b);(i)--)

#define put(a) cout << (a) << endl
#define puts(a) cout << (a) << " "

#define llint long long int

#define INF (long long)(1e9+1)
#define MOD (long long)(1e9+7)
#define INF64 (long long)(1e18+1)

#define F first
#define S second

#define Pii  pair<int,int>
#define Pll  pair<long long,long long>
#define Piii pair<int,pair<int,int>>
#define Plll pair<long long,pair<long long,long long>>

#define Vll(a,b,c)    vector<vector<long long>> (a)((b),vector<long long>((c))
#define Vlll(a,b,c,d) vector<vector<vector<long long>>> (a)((b),vector<vector<long long>>((c),vector<long long>((d)))

#define MAX_N 1000000

llint my_binary_search(llint ary[], llint key, llint left, llint right) {
	llint middle;
	if (right < left) {
		return -(INF64);
	} else {
		middle = left + (right - left) / 2;
		if (ary[middle] > key) {
			return my_binary_search(ary, key, left, middle - 1);
		} else if (ary[middle] < key) {
			return my_binary_search(ary, key, middle + 1, right);
		} else {
			return middle;
		}
	}
}

struct UnionFind {
	vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

	UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
		for(int i = 0; i < N; i++) par[i] = i;
	}

	int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
		if (par[x] == x) return x;
		return par[x] = root(par[x]);
	}

	void unite(int x, int y) { // xとyの木を併合
		int rx = root(x); //xの根をrx
		int ry = root(y); //yの根をry
		if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
		par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
	}

	bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}
};

int main (void)
{
	//INPUT
	llint H,W;
	cin >> H >> W;
	// vector<llint> A(N);
	// rep(i,0,N) {
	// 	cin >> A[i];
	// }
	// string S;
	// cin >> S;
	// llint len;
	// len = S.length();


	//SOLVE
	llint ans=0;

	if ((W==1)||(H==1)) {
		cout << 1 << endl;
		return 0;
	}


	llint l1 = W/2;
	if (W%2 == 1) {
		l1++;
	}

	llint l2 = W/2;

	llint h1 = H/2;
	if (H%2 == 1) {
		h1++;
	}
	llint h2 = H/2;

	ans = l1*h1+l2*h2;


	//OUTPUT
	cout << ans << endl;

	return 0;
}


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

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

    vector<int> dp(x + 1);
    dp[0] = 1;
    for (int h = 100; h <= 105; h++) {
        for (int i = h; i <= x; i++) {
            dp[i] |= dp[i - h];
        }
    }

    cout << dp[x] << endl;

    return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
#define MOD 998244353
#define INF 10000000000000001
#define F first
#define S second
#define LB lower_bound
#define UB upper_bound
#define vc vector
#define vll vector<long long>
#define pll pair<long long,long long> 
#define pb push_back
#define all(v) v.begin(),v.end()
#define T ll test;cin>>test; while(test--)
#define rep(i,a,n) for(ll i=a;i<(long long)n;++i)
#define repr(i,n,a) for(ll i=n;i>(long long)a;--i)
#define MAX 1000005
#define ll long long
using namespace std;
int main()
{
	ll n,m;
	cin>>n;
	ll ar[n+1],br[n];
	rep(i,0,n+1)
	cin>>ar[i];
	rep(i,0,n)
	cin>>br[i];
	ll ans=0,val=0;
	rep(i,0,n)
	{
		ans+=min(ar[i]+ar[i+1],br[i]);
		if(br[i]>ar[i])
		{
			ar[i+1]=max(ar[i+1]-(br[i]-ar[i]),val);
		}
	}
	cout<<ans<<endl;

	
}

Measured memory_bytes: 1792
mod = 998244353

class Combination:
    def __init__(self, N):
        self.fac = [1] * (N + 1)
        for i in range(1, N + 1):
            self.fac[i] = (self.fac[i - 1] * i) % mod
        self.invmod = [1] * (N + 1)
        self.invmod[N] = pow(self.fac[N], mod - 2, mod)
        for i in range(N, 0, -1):
            self.invmod[i - 1] = (self.invmod[i] * i) % mod

    def calc(self, n, k):  # nCk
        return self.fac[n] * self.invmod[k] % mod * self.invmod[n - k] % mod

n, m, k = map(int, input().split())
C = Combination(n)
ans = 0

for i in range(k+1):
    q = n - i - 1
    tmp = m*pow(m-1, q, mod) * C.calc(n-1, i)%mod
    ans = (ans+tmp)%mod

print(ans)

Measured memory_bytes: 24568
import collections

n = int(input())
s = [str(input()) for _ in range(n)]
m = int(input())
t = [str(input()) for _ in range(m)]

s,t = collections.Counter(s), collections.Counter(t)
ans = 0

for i in s.most_common():
    ans = max(ans, i[1] - t[i[0]])

print(ans)

Measured memory_bytes: 3316
#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>
#include <unordered_map>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#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 T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } 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 N;
string s[3];

int main() {
    while (cin >> N) {
        for (int i = 0; i < 3; ++i) cin >> s[i];
        long long res = 0;
        for (int i = 0; i < N; ++i) {
            set<char> se;
            for (int j = 0; j < 3; ++j) se.insert(s[j][i]);
            res += (int)se.size() - 1;
        }
        cout << res << endl;
    }
}













Measured memory_bytes: 256
#define _GLIBCXX_DEBUG
#include "bits/stdc++.h"
//#include <intrin.h>  //AtCoder (gcc) 上ではこれがあると動かない。__popcnt用のincludeファイル。
using namespace std;
using Graph = vector<vector<int>>;

typedef long long ll;
typedef long double ld;


#define int long long
#define rep(i, n) for(long long i = 0; i < (n); i++)
#define sqrt(d) pow((long double) (d), 0.50)

const int INF = 2e9;
const ll large_P = 1e9 + 7;



//繰り返し2乗法
//N^aの、Mで割った余りを求める。
ll my_pow(ll N, ll a, ll M) {
	ll tempo;
	if (a == 0) {
		return 1;
	}
	else {
		if (a % 2 == 0) {
			tempo = my_pow(N, a / 2, M);
			return (tempo * tempo) % M;
		}
		else {
			tempo = my_pow(N, a - 1, M);
			return (tempo * N) % M;
		}
	}
}



//N_C_a を M で割った余り
ll my_combination(ll N, ll a, ll M) {
	ll answer = 1;

	rep(i, a) {
		answer *= (N - i);
		answer %= M;
	}

	rep(i, a) {
		answer *= my_pow(i + 1, M - 2, M);
		answer %= M;
	}

	return answer;
}



ll my_gcd(ll& a, ll& b) {
	if (b == 0) return a;
	ll temp = a % b;
	return my_gcd(b, temp);
}



// long long型 n を、base 進法での、i 桁目の数が入った vector にする。
void ll_to_vector(ll base, ll n, vector<ll>& v) {
	ll tempo = n;

	ll n_digit = floor(log10(n) / log10(base)) + 1;
	v.assign(n_digit, 0);

	//ll n_digit = v.size();

	for (ll i = 0; i < n_digit; i++) {
		v.at(i) = tempo / pow(base, n_digit - 1 - i);
		tempo -= v.at(i) * pow(base, n_digit - 1 - i);
	}
}



int char_to_int(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4;
	case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9;
	default: return 0;
	}
}


//エラトステネスの篩で、prime で ないところに false を入れる。
//true で初期化された vector を代入する必要がある。
void prime_judge(vector<bool>& prime_or_not) {

	prime_or_not.at(0) = false;
	prime_or_not.at(1) = false;

	ll n = prime_or_not.size() - 1;
	ll region = sqrt(n) + 1;

	for (ll i = 2; i <= n / 2; i++) {
		prime_or_not.at(2 * i) = false;
	}


	for (ll i2 = 1; i2 < n / 2; i2++) {
		ll i = 2 * i2 + 1;  //ここからは奇数のみ探索
		if (i < region && prime_or_not.at(i)) {
			ll j = i * i;
			while (j < n + 1) {
				prime_or_not.at(j) = false;
				j += 2 * i;
			}
		}
	}
};



map<int, int> divide_to_prime(int target) {
	map<int, int> res;

	//sqrt(target) まで調べる。
	int upper_lim = ceil(sqrt(target));
	vector<bool> prime_or_not(upper_lim + 1, true);

	if (upper_lim < 20) prime_or_not.assign(25, true);
	prime_judge(prime_or_not);

	int tempo = target;
	rep(i, upper_lim + 1) {
		if (prime_or_not.at(i)) {
			while (tempo % i == 0) {
				tempo /= i;
				res[i]++;
			}
		}
	}

	if (tempo != 1) res[tempo]++; //sqrt(target) より大きな素因数は高々1つしかない。



	return res;
}





signed main() {
	ll X;
	cin >> X;

	int res = 0;
	int money = 100;

	while (money < X) {
		res++;
		money += money / 100;
	}

	cout << res << endl;
}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}


const int MAX_N = 112345;
int A[MAX_N];

int main2() {
  int N = nextInt();
  int K = nextInt();
  int S = nextInt();

  if (S == 1000000000) {
    REP(i, N) A[i] = 1;
    REP(i, K) A[i] = S;
  } else {
    REP(i, N) A[i] = S+1;
    REP(i, K) A[i] = S;
  }
  REP(i, N) {
    if (i > 0) printf(" ");
    printf("%d", A[i]);
  }
  printf("\n");

  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}


Measured memory_bytes: 1664
N,*a=gets.to_i-1;1.step(N,3){|i|(~i%2).step(N,2){|j|a<<[i,j]}};1.step(N,2){|i|a<<[i,0]};(~N%2).step(N,2){|i|a<<[N,i]};a.uniq!;puts a.size,a

Measured memory_bytes: 22644
R,G,B,N=(int(x) for x in input().split())
result=0

for r in range(N+1):
    N2=N-r*R
    if N2<0:
        break
    for g in range(N+1):
        N3=N2-g*G
        if N3<0:
            break
        if N3%B==0:
            result+=1

print(result)


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

using namespace std;

#define int long long

signed main(){

  int a = 0, b = 0;
  int N, M, X;

  cin>>N>>M>>X;

  for(int i = 1; i <= M; i++){
    int A;

    cin>>A;

    if(A > X) a++;
    else b++;
  }

  cout<<min(a, b)<<endl;

  return 0;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _001_FindingMissingCards
{


    class Program
    {

        static int cnt = 0;


        static void insetionSort(long []A, long n, long g)
        {
            for (long i = g; i < n; i++)
            {
                var v = A[i];
                var j = i - g;
                while (j >= 0 && A[j] > v)
                {
                    A[j + g] = A[j];
                    j = j - g;
                    cnt++;
                }
                A[j + g] = v;
            }
        }

        static void Main()
        {
            var n = Convert.ToInt64(Console.ReadLine());
            var d = new long[20];
            d[0] = 1;
            for (int i = 1; i < 20; i++)
            {
                d[i] = d[i - 1] * 3 + 1;
            }
            long[] g = Array.FindAll(d, tmp => tmp <= n);
            Array.Reverse(g);
            Console.WriteLine(g.Length);
            for (int i = 0; i < g.Length - 1; i++)
            {
                Console.Write("{0} ", g[i]);
            }
            Console.WriteLine("{0}", g[g.Length - 1]);

            long []A=new long [n];
            for (int i = 0; i < n; i++)
            {
                A[i] = Convert.ToInt64(Console.ReadLine());
            }

            for (int i = 0; i < g.Length; i++)
            {
                insetionSort(A, n, g[i]);
            }
            Console.WriteLine(cnt);
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine(A[i]);
            }
            
        }
    }
}

Measured memory_bytes: 23816
//Bismillahir Rahman-ir Rahim
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000002
#define PI acos(-1)
//#define m (pow(10, 9)+7)
typedef unsigned long long ull;
typedef long long ll;

int gcd(int a, int b){
    int temp;
    while(b != 0) {
        temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;
    
    int h = gcd(2, n);
    int lcm = 2*n/h;

    cout << lcm << endl;



    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef long double D;
//typedef complex<D> P;
#define F first
#define S second
//const ll MOD=1000000007;
const ll MOD=998244353;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){int i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}

ll N;
vector<vector<int>> edge;
ll ans=0;
vector<int> child;
vector<ll> A;
vector<ll> B;
vector<ll> dp;
ll all=0;

void dfs(int u,int p){
  child[u]=1;
  ll mi=0;
  int c=p==-1?0:1;
  for(auto &v:edge[u]){
    if(v==p){continue;}
    dfs(v,u);
    mi=max(mi,dp[v]);
    child[u]+=child[v];
  }
  ans+=mi;
  if(c){
    A[u]-=mi;
    A[p]+=mi;
  }
  for(auto &v:edge[u]){
    if(v==p){continue;}
    ans+=(mi-dp[v])*child[v];
    A[u]-=dp[v];
  }
  if(B[u]>A[u]){dp[u]=B[u]-A[u];}
  else if(B[u]<A[u]){dp[u]=0; A[p]+=A[u]-B[u]; ans+=child[u]*(A[u]-B[u]);}
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>N;
  edge.resize(N);
  child.resize(N);
  A.resize(N);
  B.resize(N);
  dp.resize(N);
  for(int i=1;i<N;i++){
    int a,b;
    cin>>a>>b;
    a--; b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  cin>>A>>B;
  for(auto &I:A){all+=I;}
  dfs(0,-1);
  cout<<ans<<endl;

  return 0;
}



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

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll k, n;
	cin >> k >> n;
	vector<ll> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	ll dmax = abs(a[n - 1] - k) + a[0];
	ll s = dmax;
	for (int i = 1; i < n; i++)
	{
		ll c = abs(a[i] - a[i - 1]);
		if (dmax < c)
			dmax = c;
		s += c;
	}
	s -= dmax;
	cout << s;
	return 0;
}


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

int main() {
	string s;
	while (getline(cin,s)) {
		int a = s.size();
		string ans;
		
		for (int i = 0;i < a;i++) {
			if (s[i] == '@') {
				char sa = s[i + 1];
				for (int j = 0;j < sa - '0';j++) {ans.push_back(s[i + 2]);}
				i += 2;
			}
			else ans.push_back(s[i]);
		}
		cout << ans << endl;
	}
	return 0;
}

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

int main() {
    string s;
    cin>>s;
    int r=0,p=0;
    long ans=0;
    for(int i=0;i<s.length();i++){
        if(s[i]=='p'){
            if(p==r){
                ans--;
                r++;
            }else {
                p++;
            }
        }else if(s[i]=='g'){
            if(p==r){
                r++;
            }else{
                ans++;
                p++;
            }
        }
    }
    cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 512
#ifdef _WIN32
#include "stdc++.h"
#else
#include <bits/stdc++.h>
#endif

// 継承可能な型
template <class Type>
struct IN : Type
{
	IN()
	{
		std::cin >> *this;
	}
};

// 継承不可能な型
template <class Type>
struct IN_
{
	Type val;

	IN_()
	{
		std::cin >> val;
	}

	operator Type&()
	{
		return val;
	}

	IN_<Type>& operator=(const Type& _value)
	{
		val = _value;
		return *this;
	}
};

template <class Char, class Type>
std::basic_ostream<Char>& operator <<(std::basic_ostream<Char>& os, const IN_<Type>& value)
{
	return os << value.val;
}

template <class Char, class Type>
std::basic_istream<Char>& operator >>(std::basic_istream<Char>& is, IN_<Type>& value)
{
	return is >> value.val;
}

template <class Type>
void OUT(const Type& value)
{
	std::cout << value << std::endl;
}

template <class Type, class ... Args>
void OUT(const Type& value, const Args& ... args)
{
	std::cout << value << ' ';
	OUT(args...);
}

void Yes()
{
	OUT("Yes");
}
void No()
{
	OUT("No");
}

using ll = long long;
using inll = IN_<ll>;
using vll = std::vector<ll>;
using setll = std::set<ll>;
using msetll = std::multiset<ll>;
using instr = IN<std::string>;

#define FOR(i, m, n) for (ll (i) = (m); (i) < (n); ++(i))
#define REP(i, n) for (ll (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (ll (i) = (n); (i) >= 0; --(i))
#define ITRREP(it, v) for (auto (it) = (v).begin(); (it) != (v).end(); ++(it))
#define ITRREPR(it, v) for (auto (it) = (v).rbegin(); (it) != (v).rend(); ++(it))
#define DIVUP(a, b) (((a) + (b) - 1) / (b))

void Main();

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

	Main();

#ifdef _DEBUG
	system("pause");
#endif

	return 0;
}

using namespace std;

//////////////////////////////

void Main()
{
	inll a, b, c;

	if (a + b >= c)
		Yes();
	else
		No();
}


Measured memory_bytes: 256
n,h = (int(i) for i in input().split())
a = []
b = []
cnt = 0
for i in range(n):
  x,y = (int(i) for i in input().split())
  a.append(x)
  b.append(y)
max = max(a)
b.sort()
for i in b[::-1]:
  if i >= max:
    h -= i
    cnt += 1
 #   print(i,h,cnt)
  if h <= 0:
    print(cnt)
    exit()
import math
cnt += int(math.ceil(h / max))
print(cnt)

Measured memory_bytes: 11788
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<cmath>
#include<string>

#define ls (t<<1)
#define rs ((t<<1)+1)
#define mid ((l+r)>>1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 100005
#define M 200005
#define seed 23333

using namespace std;
int i,j,m,n,p,k,Ans[N];
char s[N];
int work(int x)
{
		Ans[1]=x&1;
		Ans[2]=(x&2);
		Ans[2]=min(1,Ans[2]);
		int i;
		for (i=3;i<=n;++i)
		{
			if (s[i-1]=='o'&&Ans[i-1]==0) Ans[i]=Ans[i-2];
			else if (s[i-1]=='x'&&Ans[i-1]==1) Ans[i]=Ans[i-2];
			else Ans[i]=Ans[i-2]^1;
		}
		if ((Ans[n-1]^Ans[1])!=(Ans[n]^(s[i-1]=='x'))) return 0;
		if ((Ans[n]^Ans[2])!=(Ans[1]^(s[1]=='x'))) return 0;
		for (i=1;i<=n;++i) putchar(Ans[i]?'W':'S');
		exit(0);
}
int main()
{
		scanf("%d",&n);
		scanf("%s",s+1);
		for (i=0;i<4;++i) work(i);
		puts("-1"); 
}


Measured memory_bytes: 768
n = gets.to_i
ss = gets.chomp

r = ss.count("R")
g = ss.count("G")
b = ss.count("B")
ans = r * g * b

0.upto(n) do |i|
  j = 1

  while i + 2 * j < n
    k = i + 2 * j

    a = ss[i]
    b = ss[i + j]
    c = ss[k]
    ans -= 1 if a != b && b != c && c != a

    j += 1
  end
end

puts ans


Measured memory_bytes: 14364
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000000LL;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


int main() {
    int n, m;
    cin >> n >> m;
    vector<int> cnt(100);
    rep(i, m){
        int a, b; cin >> a >> b;
        cnt[a]++; cnt[b]++;
    }   
    rep(i,n)
        cout <<cnt[i+1] << ln;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
  
using namespace std;
  
int D,x,y,p[100],ans;
string a[100];
  
void solve(int d,int sum,int pos){
    ans = max(ans,sum);
  
    if(pos == x + y || d == 0){
	return;
    }
  
    int size = a[pos].size();
    if(size == 1){
	if(a[pos] == "D" && d > 0){
	    solve(d-1,sum+p[pos],pos+1);
	}
    }else{
	if(a[pos] == "DD" && d > 1){
	    solve(d-2,sum+p[pos],pos+1);
	} 
    }
  
    solve(d,sum,pos+1);
}
  
int main(){
    cin >> D >> x;
    for(int i = 0 ; i < x ; i++){
	cin >> a[i] >> p[i];
    }
    cin >> y;
    for(int i = x ; i < x + y ; i++){
	cin >> a[i] >> p[i];
    }
  
    solve(D,0,0);
    cout << ans << endl;
    return 0;
}

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

struct Point{
  int w,h;
};

int main(){
  int n,pla,idx;
  int minw,maxw,minh,maxh;
  Point p[200];
  p[0].w = p[0].h = 1;
  while(cin>>n,n){
    minw=1;maxw=1;minh=1;maxh=1;
  for(int i = 1; i < n; ++i){
    cin >> pla >> idx;
    switch (idx){
    case 0:
      p[i].w = p[pla].w - 1;
      p[i].h = p[pla].h;
      break;
    case 1:
      p[i].h = p[pla].h + 1;
      p[i].w = p[pla].w;
      break;
    case 2:
      p[i].w = p[pla].w + 1;
      p[i].h = p[pla].h;
      break;
    case 3:
      p[i].h = p[pla].h - 1;
      p[i].w = p[pla].w;
    }
  }
  for(int i = 0; i < n; ++i){
    if(p[i].w > maxw)maxw = p[i].w;
    if(p[i].w < minw)minw = p[i].w;
    if(p[i].h > maxh)maxh = p[i].h;
    if(p[i].h < minh)minh = p[i].h;
  }
  cout << maxw - minw + 1 << " " << maxh - minh +1 << endl;
  }
}

Measured memory_bytes: 1160
defmodule Main do
  def main do
    [n, _m, x] =
      IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)

    list =
      Enum.reduce(1..n, [], fn _, acc ->
        IO.read(:line)
        |> String.trim()
        |> String.split(" ")
        |> Enum.map(&String.to_integer/1)
        |> List.duplicate(1)
        |> Kernel.++(acc)
      end)

    Enum.reduce(1..n, [], fn i, acc ->
      combination(list, i) ++ acc
    end)
    |> Enum.filter(&pass?(&1, x))
    |> Enum.map(&bill/1)
    |> Enum.min(fn -> -1 end)
    |> IO.puts()
  end

  defp pass?(list_of_lists, x) do
    List.zip(list_of_lists)
    |> tl()
    |> Enum.map(&(Tuple.to_list(&1) |> Enum.sum()))
    |> Enum.all?(&(&1 >= x))
  end

  defp bill(list_of_lists) do
    List.zip(list_of_lists) |> hd() |> Tuple.to_list() |> Enum.sum()
  end

  defp combination(_, 0), do: [[]]
  defp combination([], _), do: []

  defp combination([x | xs], n) do
    for(y <- combination(xs, n - 1), do: [x | y]) ++ combination(xs, n)
  end
end

Measured memory_bytes: 32660
#include <bits/stdc++.h>
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = ((int)(n)-1); i >= 0; i--)
#define all(x) (x).begin(),(x).end()
 
using namespace std;
using ll = long long;
 
struct edge { int to, cost; };
 
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> P;
 
const int INF = 1e9;
const double EPS = 1e-10;
const int MOD = 1e9+7;
const double PI = acos(-1.0);

const int mx4[] = {0,1,0,-1};
const int my4[] = {1,0,-1,0};
const int mx8[] = {0,1,1,1,0,-1,-1,-1};
const int my8[] = {1,1,0,-1,-1,-1,0,1};

int main(){
	int n;
	cin >> n;
	vi a(n);
	ll mi = INF,cnt = 0,sum = 0;
	rep(i,n){
		cin >> a[i];
		if(a[i] < 0){
			cnt++;
			a[i] *= -1;
		}
		mi = min(mi,(ll)a[i]);
		sum += a[i];
	}
	if(cnt%2 == 1) sum -= mi*2;
	cout << sum << endl;

	return 0;
}


Measured memory_bytes: 640
#include<iostream>
#include<string>
#include<stack>
using namespace std;

stack<int> st;
int main()
{
  int a;
  int train[1000];
 
  while(1)
    { cin >>a;
      if(a!=0)
	st.push(a);
      else 
	if (st.empty())
	  break;
	else{
	
	  cout <<st.top()<<"\n";
	  st.pop();}
      
    }
  
return 0;
}

Measured memory_bytes: 0
import sys
input = sys.stdin.readline
def main():
    n = int(input())
    an = list(map(int, input().split()))
    humi = 0
    maxi = an[0]
    for i in range(1, n):
        if an[i] < maxi:
            hu = maxi -an[i]
            humi += hu
        maxi = max(maxi, an[i])

    print(humi)
if __name__ == '__main__':
    main()

Measured memory_bytes: 31504
n = int(input())
l = [0] * (n + 1)
l[0] = 2
l[1] = 1
if n == 1:
    print(1)
else:
    for i in range(2, n + 1):
        l[i] = l[i - 1] + l[i - 2]
    print(l[n])

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

int main() {
    int N, M;
    while(cin >> N >> M, N) {
        int r; cin >> r;
        vector<int> pc(N, -1);
        vector<array<short, 1261>> v(M);
        for(int i=0; i<r; ++i) { // O(rt)
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if(s == 1) {
                pc[n-1] = t;
            } else {
                fill(v[m-1].begin() + pc[n-1], v[m-1].begin()+t, 1);
                pc[n-1] = -1;
            }
        }

        for(int i=0; i<v.size(); ++i) {
            for(int j=1; j<v[i].size(); ++j) {
                v[i][j] += v[i][j-1];
            }
        }
        int q;
        cin >> q;
        for(int i=0; i<q; ++i) {
            int ts, te, m;
            cin >> ts >> te >> m;
            cout << v[m-1][te-1] - v[m-1][ts-1] << endl;
        }
    }
}


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

int main() {
    int N;
    cin >> N;
    vector<int> avec(N);
    for (int i = 0; i < N; i++) {
        cin >> avec.at(i);
    }

    int now = 1, cnt = 0;
    while(1) {
        if (now == 2) {
            cout << cnt << endl;
            break;
        }
        if (cnt >= N) {
            cout << -1 << endl;
            break;
        }
        now = avec.at(now-1);
        cnt++;
    }
    return 0;
}


Measured memory_bytes: 640
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] nums = new int[n - 1];
    for (int i = 0; i < n - 1; i++) {
      nums[i] = sc.nextInt();
    }
    long sum = nums[0] + nums[nums.length - 1];
    for (int i = n - 3; i >= 0; i--) {
      sum += Math.min(nums[i], nums[i + 1]);
    }
    System.out.println(sum);
  }
}


Measured memory_bytes: 22868
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <iomanip>
#include <queue>
#include <stack>
#include <functional>

using namespace std;
int main() {
	int n, m, l, A[1010101], ans[3] = {};
	cin >> n >> m >> l;
	for (int i = 0; i < m; i++)
		cin >> A[i];
	for (int i = 0; i < m; i++)
		if (l < A[i])
			ans[0]++;
		else
			ans[1]++;
	cout << min(ans[0], ans[1]) << endl;
	return 0;
}

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

int H, W;
int farm[100][100];
bool used[100][100];
char tmp[100];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

void dfs(int x, int y, int fruit) {
    used[x][y] = true;
    for (int i = 0; i < 4; i++) {
        int x1 = x+dx[i], y1 = y+dy[i];
        if (x1 >= 0 && x1 < H && y1 >= 0 && y1 < W 
            && farm[x1][y1] == fruit && !used[x1][y1]) {
            dfs(x1,y1,fruit);
        }
    }
}

void solve() {
    int res = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (!used[i][j]) {
                dfs(i,j,farm[i][j]);
                res++;
            }
        }
    }
    cout << res << "\n";
}

int main() {
    while (cin>>H>>W && (H||W)) {
        for (int i = 0; i < H; i++) {
            cin >> tmp;
            for (int j = 0; j < W; j++) {
                if (tmp[j] == '@') farm[i][j] = 0;
                else if (tmp[j] == '#') farm[i][j] = 1;
                else if (tmp[j] == '*') farm[i][j] = 2;
                used[i][j] = false;
            }
        }
        solve();
    }
}


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

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

    int n,k,tot=0;
    cin>>n>>k;
    while(n>0){
        n/=k;
        tot++;
            //cout<<n<<"\n";
    }

    cout<<tot;



}

Measured memory_bytes: 256
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define veci vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vec>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))


using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
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 T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
//template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
//template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
    for(T& x:vec) is >> x;
    return is;
}
const ll mod=1e9+7;

void rec(ll v,const vector<vector<ll>> &G,vector<bool> &seen){
    seen[v]=true;
    for(auto next:G[v]){
        if(seen[next]) continue;
        rec(next,G,seen);
    }
    return;
}

int main()
{
    ll n,m;
    cin >> n >> m;
    
    vector<ll> a(m);
    vector<ll> b(m);
    rep(i,m){
        ll p,q;
        cin >> p >> q;
        p--;
        q--;
        a[i]=p;
        b[i]=q;
        
    }
    ll ans=0;
    rep(i,m){
        vector<vector<ll>> G(n);
        vector<bool> seen(n,false);
        rep(j,m){
            if(j==i) continue;
            G[a[j]].push_back(b[j]);
            G[b[j]].push_back(a[j]);
        }
        rec(0,G,seen);
        bool flag=true;
        rep(j,n){
            if(!seen[j]){
                flag=false;
                break;
            }
        }
        if(!flag){
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
} 


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  long long n; cin >> n;
  vector<long long> a(3), b(3);
  REP(i, 3) cin >> a[i];
  REP(i, 3) cin >> b[i];
  vector<long long> bef, aft;
  REP(i, 3) {
    if (a[i] < b[i]) {
      bef.emplace_back(a[i]);
      aft.emplace_back(b[i]);
    }
  }
  if (bef.size() == 1) {
    long long g = n / bef[0];
    long long tmp = n - bef[0] * g;
    tmp += g * aft[0];
    n = max(n, tmp);
  } else if (bef.size() == 2) {
    long long korede = 0;
    REP(i, n + 1) {
      long long tmp = n, g = i / bef[0], s = (n - i) / bef[1];
      tmp -= g * bef[0] + s * bef[1];
      tmp += g * aft[0] + s * aft[1];
      korede = max(korede, tmp);
    }
    n = max(n, korede);
  } else if (bef.size() == 3) {
    long long korede = 0;
    REP(i, n + 1) REP(j, n - i + 1) {
      long long tmp = n;
      long long g = i / bef[0], s = j / bef[1], b = (n - i - j) / bef[2];
      tmp -= g * bef[0] + s * bef[1] + b * bef[2];
      tmp += g * aft[0] + s * aft[1] + b * aft[2];
      korede = max(korede, tmp);
    }
    n = max(n, korede);
  }
  bef.clear();
  aft.clear();
  REP(i, 3) {
    if (b[i] < a[i]) {
      bef.emplace_back(b[i]);
      aft.emplace_back(a[i]);
    }
  }
  if (bef.size() == 1) {
    long long g = n / bef[0];
    long long tmp = n - bef[0] * g;
    tmp += g * aft[0];
    n = max(n, tmp);
  } else if (bef.size() == 2) {
    long long korede = 0;
    REP(i, n + 1) {
      long long tmp = n, g = i / bef[0], s = (n - i) / bef[1];
      tmp -= g * bef[0] + s * bef[1];
      tmp += g * aft[0] + s * aft[1];
      korede = max(korede, tmp);
    }
    n = max(n, korede);
  } else if (bef.size() == 3) {
    long long korede = 0;
    REP(i, n + 1) REP(j, n - i + 1) {
      long long tmp = n;
      long long g = i / bef[0], s = j / bef[1], b = (n - i - j) / bef[2];
      tmp -= g * bef[0] + s * bef[1] + b * bef[2];
      tmp += g * aft[0] + s * aft[1] + b * aft[2];
      korede = max(korede, tmp);
    }
    n = max(n, korede);
  }
  cout << n << '\n';
  return 0;
}


Measured memory_bytes: 256
import sys
# from math import sqrt, gcd, ceil, log
# from bisect import bisect
from collections import defaultdict, Counter
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))

# sys.setrecursionlimit(10**6)



def solve():
	ans = ""
	n = int(input()); count = Counter(read())
	s = sum([i * count[i] for i in count])
	for _ in range(int(input())):
		a, b = read(); 
		s += (b-a)* count[a]
		count[b] += count[a]
		count[a] = 0
		ans += str(s)+"\n"
	print(ans)
		

if __name__ == "__main__":
	solve()

Measured memory_bytes: 22988
import java.util.Scanner;


public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String[] input = sc.nextLine().split(" ");

		int num1 = Integer.parseInt(input[0]);
		int num2 = Integer.parseInt(input[1]);
		int num3 = Integer.parseInt(input[2]);

		int count = 0;

		for (int i = num1 ; i < num2+1; i++) {
			if(i % num3 == 0) {
				count++;
			}
		}

		System.out.println(count);

	}

}


Measured memory_bytes: 35668
n = int( input() )
s = []
for _ in range( n ):
    s_i = str( input() )
    s.append( s_i )

count = {}
for s_i in s:
    try:
        count[ s_i ] += 1
    except KeyError:
        count[ s_i ] = 1

max_count = 0
for t in count.keys():
    if count[ t ] > max_count:
        max_count = count[ t ]

max_t = []
for t in count.keys():
    if count[ t ] == max_count:
        max_t.append( t )

max_t.sort()

for t in max_t:
    print( t )

Measured memory_bytes: 38444
import java.util.*;

public class Main {// Main
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
   int n = sc.nextInt();
   int m = sc.nextInt();
   for(int i =1 ; i <=m ;i++) {
	   n-=sc.nextInt();
   }
    if(n>=0)
    System.out.println(n);
    else {
    	System.out.println(-1);
    }
	}

	
}

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

#define CIN_ONLY if(1)
struct cww{cww(){
    CIN_ONLY{
        ios::sync_with_stdio(false);cin.tie(0);
    }
}}star;
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define DEBUG if(0)
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l,T r)
{bool a=l>r;if(a)l=r;return a;}
template <typename T>inline bool chmax(T &l,T r)
{bool a=l<r;if(a)l=r;return a;}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
    for(auto &it:v)is>>it;
    return is;
}

typedef vector<LL> V;
int main(){
    int n,m;
    cin>>n>>m;
    
    V x(4*m,0);
    V y(4*m,0);
    V a(n);
    cin>>a;
    LL sum=0;
    REP(i,n)a[i]--;
    REP(i,n-1){
        int l=a[i];
        int r=a[i+1];
        if(r<l)r+=m;
        sum+=r-l;
        x[l+1]++;
        x[r+1]--;
        y[l+1]+=l;
        y[r+1]-=l;
        l+=m;
        r+=m;
        x[l+1]++;
        x[r+1]--;
        y[l+1]+=l;
        y[r+1]-=l;
    }
    REP(i,4*m-1){
        x[i+1]+=x[i];
        y[i+1]+=y[i];
    }
    LL res=sum;
    FOR(i,m,2*m){
        LL cnt=x[i];
        LL st=y[i];
        //cout<<i-m<<" "<<sum-(i-1)*cnt+st<<endl;
        chmin(res,sum-(i-1)*cnt+st);

    }
    cout<<res<<endl;
    return 0;
}


Measured memory_bytes: 7296
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Main {
    public static void main(final String[] args) throws Exception {

        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) {
            List<String> inputString = Arrays.asList(br.readLine().split(""));
            Set<String> removeDuplication = new HashSet<>();
            for (String str : inputString) {
                removeDuplication.add(str);
            }

            String ans = "no";
            if (inputString.size() == removeDuplication.size()) {
                ans = "yes";
            }

            System.out.println(ans);
        } catch (IOException | NumberFormatException e) {
            e.printStackTrace();
            System.exit(0);
        } catch (final Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
}

Measured memory_bytes: 21460
#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <ios> 
#include <vector>
#include <string>
#include <algorithm>
//#include <functional>
#include <queue>
//#include <stack>
//#include <set>
#include <cmath>
//#include <bitset>
#include <map>
using namespace std;
typedef long long ll;


bool cmp(pair<string, pair<int,int>> a, pair<string, pair<int,int>>b)
{
  if(a.first != b.first){
    return a.first > b.first;
  }
  else{
    return (a.second).first < (b.second).first;

  }
}


int main() {
  string s;
  cin >> s;
  int n=0;
  int c=0;
  for(auto i : s){
    if(i=='o')  n++;
    c++;
  }
  if((15 - c) + n >= 8 ){
  cout << "YES" << endl;
  }
  else cout << "NO" << endl;

  return 0;
   

}


Measured memory_bytes: 256
import numpy as np
from numba import njit
  
# @njit('(i4[::1],)', cache=True)
@njit
def solve(A):
    count = np.zeros(10**6 + 10, np.int32)
    for x in A:
        if count[x] > 1:
            continue
        count[::x] += 1
    ret = 0
    for x in A:
        ret += count[x] == 1
    return ret

N = int(input())
A = np.array(list(map(int,input().split())), np.int32)
 
print(solve(A))


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

using namespace std;

int N;
vector< int > g[100000];
int dp[100000];
int depth[100000];

void dfs(int x, int par = -1)
{
  for(int to : g[x]) {
    if(par != to) {
      dfs(to, x);
      depth[x] = max(depth[x], depth[to] + 1);
    }
  }
}

void rec(int x, int dep = 0, int par = -1)
{


  priority_queue< pair< int, int >, vector< pair< int, int > >, greater< pair< int, int > > > que;
  que.emplace(dep, -1);
  while(que.size() > 2) que.pop();
  for(int to : g[x]) {
    if(par != to) {
      que.emplace(depth[to] + 1, to);
      while(que.size() > 2) que.pop();
    }
  }
  auto obj2 = que.top();
  que.pop();
  auto obj1 = que.size() ? que.top() : obj2;

  for(int to : g[x]) {
    if(par != to) {
      if(obj1.second == to) rec(to, obj2.first + 1, x);
      else rec(to, obj1.first + 1, x);
    }
  }
  dp[x] = obj1.first;
}
int main()
{
  cin >> N;
  for(int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0);
  rec(0);
  for(int i = 0; i < N; i++) {
    cout << (N - 1) * 2 - dp[i] << endl;
  }
}

Measured memory_bytes: 28136
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,K;
    cin >> N >> K;
    vec<ll> P(N);
    bool allminus = true;
    for(int i=0;i<N;i++){
        cin >> P[i];
        allminus &= P[i]<0;
    }
    if(K==N){
        mint ans = 1;
        for(int i=0;i<K;i++) ans *= P[i];
        cout << ans << "\n";
        return 0;
    }
    if(allminus && K%2==1){
        sort(P.begin(),P.end(),[](ll a,ll b){return abs(a)<abs(b);});
        mint ans = 1;
        for(int i=0;i<K;i++) ans *= P[i];
        cout << ans << "\n";
        return 0;
    }

    sort(P.begin(),P.end(),[](ll a,ll b){return abs(a)>abs(b);});
    mint ans = 1;
    if(P[K-1]==0){
        cout << 0 << "\n";
        return 0;
    }
    vec<ll> plus,minus;
    for(int i=0;i<K;i++){
        ans *= P[i];
        if(P[i]>0) plus.push_back(P[i]);
        else if(P[i]<0) minus.push_back(P[i]);
    }
    if(minus.size()%2==0){
        cout << ans << "\n";
        return 0;
    }
    ll p = -1,m = 1;
    for(int i=K;i<N;i++){
        if(p<0 && P[i]>=0) p = P[i];
        if(m>0 && P[i]<0) m = P[i];
    }
    if(m>0 || plus.empty()){
        ans /= minus.back();
        ans *= p;
        cout << ans << "\n";
    }else if(p<0 || minus.empty()){
        ans /= plus.back();
        ans *= m;
        cout << ans << "\n";
    }else{
        if(abs(p*plus.back())<abs(m*minus.back())){
            ans /= plus.back();
            ans *= m;
        }else{
            ans /= minus.back();
            ans *= p;
        }
        cout << ans << "\n";
    }
}


Measured memory_bytes: 6892
/*
  In the name of ALLAH
  Author : Raashid Anwar
*/
 
#include <bits/stdc++.h>
using namespace std;
 
#define int int64_t
const int M1 =  998244353;
const int M2 =  1000000007;

int trie[2000000][26];
int cnt[2000000][26];
int tot;

void insert(string s) {
  reverse(s.begin(), s.end());
  int n = s.size();
  int a[26] = {};
  for(int i = 0; i < n; i++)
    a[s[i] - 'a']++;
  for(int j = 0; j < 26; j++)
      cnt[0][j] += (a[j] > 0);
  int u = 0;
  for(int i = 0; i < n; i++) {
    a[s[i] - 'a']--;
    if(!trie[u][s[i] - 'a'])
      trie[u][s[i] - 'a'] = tot++;
    u = trie[u][s[i] - 'a'];
    for(int j = 0; j < 26; j++)
      cnt[u][j] += (a[j] > 0);
  }
}

int get(string s) {
  reverse(s.begin(), s.end());
  int n = s.size();
  int u = 0;
  for(int i = 0; i < n - 1; i++) {
    if(!trie[u][s[i] - 'a'])
      return 1;
    u = trie[u][s[i] - 'a'];
  }
  return cnt[u][s[n - 1] - 'a'];
}


void solve() {
  int n;
  cin >> n;
  tot = 1;
  string s[n];
  int ans = 0;
  for(int i = 0; i < n; i++) {
    cin >> s[i];
    insert(s[i]);
  }
  for(int i = 0; i < n; i++)
    ans += get(s[i]) - 1;
  cout << ans << "\n";
}
 
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}

Measured memory_bytes: 410368