akhauriyash/Code-Regression huggingface dataset:
//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
char s[100003];
int main() {
	int N;
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>s[i];
	}

	long long Jsum=0,Osum=0,Isum=0;
	long long R=0;
	long long TJ[100000]={},TO[100000]={},TI[100000]={};

	///J
	Jsum=1;
	for(int i=0;i<N;i++){
		if(s[i]=='J')Jsum+=1;
		if(s[i]=='O')Osum+=Jsum;
		if(s[i]=='I')Isum+=Osum;
	}
	R=max(R,Isum);
//O
	if(s[0]=='J')TJ[0]=1;
	for(int i=1;i<N;i++){
		TJ[i]=TJ[i-1];
		TO[i]=TO[i-1];
		TI[i]=TI[i-1];
		if(s[i]=='J')TJ[i]+=1;
		if(s[i]=='O')TO[i]+=1;
		if(s[i]=='I')TI[i]+=1;
	}
	long long moto;
	Jsum=0;
	Osum=0;
	Isum=0;
	for(int i=0;i<N;i++){
		if(s[i]=='J')Jsum+=1;
		if(s[i]=='O')Osum+=Jsum;
		if(s[i]=='I')Isum+=Osum;
	}
	moto=Isum;
	long long x;
	for(int i=0;i<N;i++){
		x=TJ[i]*(TI[N-1]-TI[i]);
		R=max(R,moto+x);
	}


	////I
	Jsum=0;
	Osum=0;
	Isum=0;
	for(int i=0;i<N;i++){
		if(s[i]=='J')Jsum+=1;
		else if(s[i]=='O')Osum+=Jsum;
		else if(s[i]=='I')Isum+=Osum;
	}
	Isum+=Osum;
	R=max(R,Isum);
	cout<<R<<endl;
	return 0;
}

Measured memory_bytes: 4748
A, B, K = map(int, input().split())
ans = []
if A == B:
    print(A)
    exit()
for i in range(A,A+K):
    if B <= i:
        break
    ans.append(i)
for i in range(B-K+1,B+1):
    if i <= A:
        continue
    ans.append(i)
ans2 = set(ans)
ans3 = list(ans2)
ans3.sort()
for i in ans3:
    print(i)

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

using namespace std;

constexpr int N = 100000;
constexpr int M = 100000;

set<pair<long long, int>> active;
vector<int> e_open[N + 1];
vector<int> e_close[N + 1];
int l[M + 1];
int r[M + 1];
long long c[M + 1];
long long dist[N + 1];
long long comp[N + 1];

int main() {
	int n, m, p, i, j;

	scanf("%d%d", &n, &m);

	for (i = 1; i <= m; i++) {
		scanf("%d%d%lld", l + i, r + i, c + i);
		e_open[l[i]].push_back(i);
		e_close[r[i]].push_back(i);
	}

	dist[1] = 0;

	// Pushing initial ranges.
	for (j = 0; j < e_open[1].size(); j++) {
		p = e_open[1][j];
		comp[p] = dist[1] + c[p];
		active.insert({comp[p], p});
	}

	for (i = 2; i <= n; i++) {
		if (active.empty()) {
			break;
		}

		dist[i] = active.begin()->first;

		for (j = 0; j < e_open[i].size(); j++) {
			p = e_open[i][j];
			comp[p] = dist[i] + c[p];
			active.insert({comp[p], p});
		}

		for (j = 0; j < e_close[i].size(); j++) {
			p = e_close[i][j];
			active.erase({comp[p], p});
		}
	}

	if (i <= n) {
		printf("-1\n");
	}
	else {
		printf("%lld\n", dist[n]);
	}

	return 0;
}

Measured memory_bytes: 14464
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <random>
#include <chrono>
#include <queue>
#include <ctime>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001


int main() {
    int k, x; cin >> k >> x;
    for(int i=x-k+1; i<=x+k-1; i++) cout << i << endl;
    
    return 0;
    
}





Measured memory_bytes: 256
nlist = list(map(int, str(input())))
k = int(input())

helper = {}


def fun(digit, remaining_non_zero, edge_case):
    if str(digit)+ ',' + str(remaining_non_zero)+ ',' + str(edge_case) in helper.keys():
        return helper[str(digit)+ ',' + str(remaining_non_zero)+ ',' + str(edge_case)]

    if remaining_non_zero < 0:
        return 0
    if digit == 0 and remaining_non_zero == 0:
        return 1
    if digit == 0 and remaining_non_zero >= 1:
        return 0

    if edge_case == True:
        ans = 0
        for i in range(nlist[len(nlist) - digit] + 1):
            if i == 0:
                if nlist[len(nlist) - digit] == 0:
                    ans += fun(digit - 1, remaining_non_zero, True)
                else:
                    ans += fun(digit - 1, remaining_non_zero, False)
            elif i == nlist[len(nlist) - digit]:
                ans += fun(digit - 1, remaining_non_zero - 1, True)
            else:
                ans += fun(digit - 1, remaining_non_zero - 1, False)
        helper[str(digit)+ ',' + str(remaining_non_zero)+ ',' + str(edge_case)] = ans
        return ans
    if edge_case == False:
        ans = 0
        for i in range(10):
            if i == 0:
                ans += fun(digit - 1, remaining_non_zero, False)
            else:
                ans += fun(digit - 1, remaining_non_zero - 1, False)
        helper[str(digit)+ ',' + str(remaining_non_zero)+ ',' + str(edge_case)] = ans
        return ans

print(fun(len(nlist), k, True))

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vi=vector<int>;

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

//pow(llpow,modpow)
template<class T> ll llpow(ll x,T n){ll ans=1;if(x==0)ans=0;while(n){if(n&1)ans*=x;x*=x;n>>=1;}return ans;}
long long modpow(long long a, long long n, long long mod) {long long res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}
//最大公約数
template<class T> inline T gcd(T x,T y){if(y==0)return x; else {return gcd(y,x%y);}}
//最小公倍数
template<class T> inline T lcm(T x,T y){return x/gcd(x,y)*y;}
//逆元
long long modinv(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)

int d(ll x){
    if(x>=1&&x<10)return 1;
    else if(x>=10&&x<100)return 2;
    else if(x>=100&&x<1000)return 3;
    else if(x>=1000&&x<10000)return 4;
    else if(x>=10000&&x<100000)return 5;
    else if(x>=100000&&x<1000000)return 6;
    else if(x>=1000000&&x<10000000)return 7;
    else if(x>=1000000&&x<10000000)return 8;
    else if(x>=10000000&&x<100000000)return 9;
    else if(x>=100000000&&x<1000000000)return 10;
    else return -1;
}
long long INF = 1LL<<60;;
int main( ){
    ll K; cin>>K;
    int way[]={-1,0,1};
    queue<ll> q;
    rep(i,1,10){
        q.push(i);
        if(--K==0){cout<<i<<endl;return 0;}
    }
    while(K>0){
        ll v=q.front();q.pop();
        ll ld=v%10;
        rep(w,0,3){
            ll ldd=ld+way[w];
            if(ldd<0||ldd>=10)continue;
            else {
                ll pv=v*10+ldd;
                if(K-1==0){cout<<pv<<endl;return 0;}
                else {
                    q.push(pv);K--;
                }
            }
        }
    }
    return 0;
}




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

using i64=int64_t;

#define rep(i,x,y) for(i64 i=i64(x),i##_max_for_repmacro=i64(y); i<i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
 
const int inf=1.01e9;
const i64 inf64=4.01e18;
const double eps=1e-9;
 
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
        os << v << ",";
    }
    os << "]";
    return os;
}

void solve(){
    string S;
    cin >> S;

    i64 ans=0,mod=1000000007;
    vector<i64> lq(S.size()),rq(S.size());
    vector<i64> la(S.size()),ra(S.size());
    vector<i64> lc(S.size()),rc(S.size());
    i64 Q=0;
    rep(i,0,S.size()) if(S[i]=='?') ++Q;
    vector<i64> table(100050);
    table[0]=1;
    rep(i,1,100050) table[i]=table[i-1]*3%mod;
    rep(i,1,S.size()){
        lq[i]=lq[i-1];
        la[i]=la[i-1];
        lc[i]=lc[i-1];
        if(S[i-1]=='?') ++lq[i];
        if(S[i-1]=='A') ++la[i];
        if(S[i-1]=='C') ++lc[i];
    }
    for(int i=S.size()-2; i>=0; --i){
        rq[i]=rq[i+1];
        ra[i]=ra[i+1];
        rc[i]=rc[i+1];
        if(S[i+1]=='?') ++rq[i];
        if(S[i+1]=='A') ++ra[i];
        if(S[i+1]=='C') ++rc[i];
    }
    rep(i,0,S.size()){
        if(S[i]!='B' and S[i]!='?') continue;
        int x=S[i]=='?';
        ans+=la[i]*rc[i]%mod*table[Q-x]%mod;
        ans%=mod;
        ans+=lq[i]*rc[i]%mod*table[Q-1-x]%mod;
        ans%=mod;
        ans+=la[i]*rq[i]%mod*table[Q-1-x]%mod;
        ans%=mod;
        ans+=lq[i]*rq[i]%mod*table[Q-2-x]%mod;
        ans%=mod;
        print(i);
        print(ans);
    }

    cout << ans << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}


Measured memory_bytes: 5968
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define per(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;

typedef long long ll;
const int mod = 1e9+7;
const int MAXN = 2e5 + 7;

struct node{
	double x, y;
}s[MAXN];
int main(int argc, char const *argv[])
{
	int n;
	scanf("%d", &n);
	rep(i, 1, n)	scanf("%lf %lf", &s[i].x, &s[i].y);
	sort(s+1, s+1+n, [](const node &a, const node &b){
		return atan2(a.y, a.x) < atan2(b.y, b.x);
	});
	rep(i, 1, n)	s[i + n] = s[i];
	double ans = 0;
	rep(i, 1, n){
		double xx = 0, yy = 0;
		rep(j, 0, n-1){
			xx += s[i+j].x;
			yy += s[i+j].y;
			ans = max(ans, xx*xx + yy*yy);
		}
	}
	printf("%.20lf\n", sqrt(ans));
	return 0;
}

Measured memory_bytes: 512
S = list(input())

A = int(''.join(S[:2]))
B = int(''.join(S[2:]))

ans = ''
if 1 <= A <= 12 and 1 <= B <= 12:
    ans = 'AMBIGUOUS'
elif (A > 12 or A <= 0) and 1 <= B <= 12:
    ans = 'YYMM'
elif 1 <= A <= 12 and (B<=0 or B > 12):
    ans = 'MMYY'
else:
    ans = 'NA'

print(ans)


Measured memory_bytes: 38384
import sys
sys.setrecursionlimit(10**6)
n,m=map(int,input().split())
l=[[]for _ in range(n+1)]
for _ in range(m):
    x,y=map(int,input().split())
    l[x].append(y)

memo=[0]*(n+1)
def chain(v):
    if memo[v]!=0:
        return memo[v]
    else:
        for a in l[v]:
            memo[v]=max(chain(a)+1,memo[v])
        return memo[v]
res=0
for i in range(1,n+1):
    res=max(res,chain(i))
print(res)

Measured memory_bytes: 59056
#include "bits/stdc++.h"

using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef priority_queue<int> PQ_DESC;
typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
 
#define SORT_ASC(c) sort((c).begin(), (c).end())
#define SORT_DESC(c) sort((c).begin(), (c).end(), greater<typeof((c).begin())>())
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SIZE(a) int((a).size())
 
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INT_LARGE = 1000000100;


//debug func
template<typename T>
void vecprint(vector<T> v) {
    for(auto x : v) {
        cout << x << " ";
    }
    cout << endl;
}

template<typename T>
void vecvecprint(vector<vector<T>> v) {
    for(auto x :v) {
        REP(i, x.size()-1) {
            cout << x[i] << " ";
        }
        cout << x[x.size()-1] <<endl;
    }
}

template<typename T>
void pqprint(priority_queue<T> q) {
    while(!q.empty()) {
        cout << q.top() << " ";
        q.pop();
    }
    cout << endl;
}

template<typename T>
void qprint(queue<T> q) {
    while(!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
}

template<typename T>
void vecqprint(vector<queue<T>> v) {
    for(int i = 0; i < v.size(); i++) {
        qprint(v[i]);
    }
}

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
{
   /* Credits: Thomas Draper */
   if ((first == last) || (first == k) || (last == k))
      return false;
   Iterator itr1 = first;
   Iterator itr2 = last;
   ++itr1;
   if (last == itr1)
      return false;
   itr1 = last;
   --itr1;
   itr1 = k;
   --itr2;
   while (first != itr1)
   {
      if (*--itr1 < *itr2)
      {
         Iterator j = k;
         while (!(*itr1 < *j)) ++j;
         iter_swap(itr1,j);
         ++itr1;
         ++j;
         itr2 = k;
         rotate(itr1,j,last);
         while (last != j)
         {
            ++j;
            ++itr2;
         }
         rotate(k,itr2,last);
         return true;
      }
   }
   rotate(first,k,last);
   return false;
}

int main(void) {
    int n;
    string s;
    cin >> n >> s;
    
    int idx = 0;
    while(idx < s.size() - 1) {
        if(s[idx] == s[idx+1]) s.erase(idx, 1);
        else idx++;
        //cout << s << endl;
    }
    cout << s.size() << endl;
}


Measured memory_bytes: 512
S = list(input())
l = len(S)

ans = 0

for bit in range(1 << (l-1)):
    # print(bit)
    f = S[0]
    for i in range(l-1):
        if bit & (1 << i):
            f += "+"
        f += S[i+1]

    ans += sum(map(int, f.split("+")))
print(ans)


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



template<int um> struct UF {
	vector<int> d; vector<pair<int, int>> history;
	UF() { d = vector<int>(um); clear(); }
	void clear() { rep(i, 0, um) d[i] = i; history.clear(); }
	int operator[](int x) { return d[x] == x ? x : d[x] = operator[](d[x]); }
	void operator()(int x, int y) {
		x = operator[](x); y = operator[](y); if (x == y) return;
		if (d[x] > d[y]) swap(x, y);
		if (d[x] == d[y]) history.push_back(make_pair(x, d[x])), d[x]--;
		history.push_back(make_pair(y, d[y]));
		d[y] = x;
	}
	void snapshot() { history.clear(); }
	void rollback() { while (history.size()) { d[history.back().first] = history.back().second; history.pop_back(); }}
};
//-----------------------------------------------------------------
int N, M;
UF<101010> uf;
int idx[101010];
//-----------------------------------------------------------------
int main() {
	cin >> N >> M;

	rep(i, 1, M + 1) idx[i] = -1;

	rep(i, 0, N) {
		int K; scanf("%d", &K);
		rep(j, 0, K) {
			int L; scanf("%d", &L);
			if (idx[L] < 0)
				idx[L] = i;
			else
				uf(i, idx[L]);
		}
	}

	bool ok = true;
	rep(i, 0, N) if (uf[0] != uf[i]) ok = false;

	if (ok)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

Measured memory_bytes: 2168
n = int(input())
listA= list(map(int, input().split()))
listSum=[]

sum =0
index=0
while index < len(listA):
    if index==0:
        sum+=listA[index]
        listSum.append(sum)
    else:
        sum = listSum[index-1]+listA[index]
        listSum.append(sum)
    index+=1

allSum = listSum[len(listSum)-1]
index2=0
minDelta =2*(10**9)
while index2 < len(listSum)-1:
    if max(allSum - listSum[index2], listSum[index2]) - min(allSum - listSum[index2], listSum[index2]) < minDelta:
        minDelta = max(allSum - listSum[index2], listSum[index2]) - min(allSum - listSum[index2], listSum[index2]) 
    index2+=1

print(minDelta)


Measured memory_bytes: 23800
#include<bits/stdc++.h>
#include<atcoder/segtree.hpp>

#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"
typedef long long ll;
using namespace std;
using namespace atcoder;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();
// freopen("input.txt", "r", stdin);

int e() { return 0;}
int mx(int a, int b) {return max(a, b);}
int main() {
    int n, k, val;
    cin >> n >> k;
    int upper = 300001;
    segtree<int, mx, e> seg(upper);
    int ans = 0;
    //cout << "shit" << endl;
    for(int i = 0; i < n; i++) {
        cin >> val;
        int neibor_mx = seg.prod(max(val-k, 0), min(val+k+1, upper));
        // cout << "neibor_mx " << neibor_mx << endl;
        ans = max(ans, neibor_mx+1);
        seg.set(val, neibor_mx+1);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 8480
#include <bits/stdc++.h>
#define FOR(x,n) for(int x = 0; x < n; x++)
#define ALL(a) (a).begin(), (a).end()
#define FIN ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;

int main() {
  ll N, K; cin >> N >> K;
  ll ans = K;
  FOR(x,N-1) ans *= K-1;
  cout << ans << "\n";
}

Measured memory_bytes: 256
#include <algorithm>
#include <boost/optional.hpp>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for(int i = 0, i##_MACRO = (n); i < i##_MACRO; i++)
#define RANGE(i, a, b) for(int i = (a), i##_MACRO = (b); i < i##_MACRO; i++)
#define EACH(e, a) for(auto&& e : a)
#define ALL(a) std::begin(a), std::end(a)
#define RALL(a) std::rbegin(a), std::rend(a)
#define FILL(a, n) memset((a), n, sizeof(a))
#define FILLZ(a) FILL(a, 0)
#define INT(x) (static_cast<int>(x))
#define PRECISION(x) std::fixed << std::setprecision(x)

using namespace std;

using ll = long long;
using VI = vector<int>;
using VI2D = vector<vector<int>>;

constexpr int INF = 2e9;
constexpr double EPS = 1e-10;
constexpr double PI = acos(-1.0);

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

template <typename T>
constexpr int sign(T x) {
	return x < 0 ? -1 : x > 0 ? 1 : 0;
}

template <>
constexpr int sign(double x) {
	return x < -EPS ? -1 : x > EPS ? 1 : 0;
}

template <typename T, typename U>
constexpr void chmax(T& m, U x) {
	m = max(m, x);
}

template <typename T, typename U>
constexpr void chmin(T& m, U x) {
	m = min(m, x);
}

template <typename T>
constexpr T square(T x) {
	return x * x;
}

const int I_MAX = 26;

int calc_point(const int& D, const VI& c, const VI2D& s, const VI& t) {
	int point = 0;
	VI last(I_MAX + 1, 0);
	REP(i, D) {
		point += s[i][t[i]];
		last[t[i]] = i + 1;
		int down = 0;
		REP(j, I_MAX) { down += c[j] * ((i + 1) - last[j]); }
		point -= down;
	}

	return point;
}

int main() {
	int D;
	cin >> D;
	VI c(I_MAX);
	REP(i, I_MAX) { cin >> c[i]; }
	VI2D s(D, VI(I_MAX));
	REP(i, D) {
		REP(j, I_MAX) { cin >> s[i][j]; }
	}
	VI t(D);
	REP(i, D) {
		int tmp;
		cin >> tmp;
		t[i] = tmp - 1;
	}
	int m;
	cin >> m;
	VI d(m);
	VI q(m);
	int prev = calc_point(D, c, s, t);
	REP(i, m) {
		int tmp1, tmp2;
		cin >> tmp1 >> tmp2;
		d[i] = tmp1 - 1;
		q[i] = tmp2 - 1;
		int old = t[d[i]];
		t[d[i]] = q[i];
		int now = calc_point(D, c, s, t);
		cout << now << endl;
		// if(prev > now) {
		// 	t[d[i]] = old;
		// } else {
		// 	prev = now;
		// }
	}

	return 0;
}


Measured memory_bytes: 4044
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-8;

long long int N, M, K, L, R, H, W;
//int N, M, K, L, R, H, W;

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

	cin >> H >> W >> K;
	vector<string>s(H);
	for (auto &i : s)cin >> i;
	int ans = MOD;
	for (int i = 0; i < 1 << (H - 1); i++) {
		vector<vector<int>>bag;
		vector<int>c;
		c.push_back(0);
		for (int j = 0; j < H-1; j++) {
			if ((i >> j) & 1) {
				bag.push_back(c);
				c.clear();
				c.push_back(j + 1);
			}
			else {
				c.push_back(j + 1);
			}
		}
		bag.push_back(c);
		vector<int>num(bag.size());
		int box = bag.size() - 1;
		for (int j = 0; j < W; j++) {
			vector<int>add(bag.size());
			for (int k = 0; k < bag.size(); k++) {
				for (auto l : bag[k]) {
					if (s[l][j] == '1')add[k]++;
				}
			}
			bool flag = true;
			for (int k = 0; k < bag.size(); k++) {
				if (num[k] + add[k] > K) {
					flag = false;
				}
				if (add[k] > K) {
					box = MOD;
					flag = false;
				}
			}
			if (flag) {
				for (int k = 0; k < bag.size(); k++) {
					num[k] += add[k];
				}
			}
			else {
				box++;
				num = add;
			}
		}
		ans = min(ans, box);
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
N, M = map(int, input().split(" "))
destination = list(map(int, input().split(" ")))
dis = []
ans = 0
destination.sort()
for i in range(M-1):
    dis.append(destination[i+1] - destination[i])
dis.sort()
for j in range(M-N):
    ans += dis[j]
print(ans)


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

int main() {
    int i = 1;
    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        cout << "Case " << i << ": " << n << endl;
        i++;
    }
}



Measured memory_bytes: 3048
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=10000005;
int n,m,w[maxn],v[maxn],f[maxn];
signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%lld%lld",&w[i],&v[i]);
	for(int i=1;i<=n;i++)
		for(int j=m;j>=w[i];j--)
			f[j]=max(f[j],f[j-w[i]]+v[i]);
	printf("%lld",f[m]);
}

Measured memory_bytes: 5120
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1<<29);

//四近傍
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int main() {

    int H, W, sx, sy, gx, gy, cnt = 0;
    cin >> H >> W;
    
    
    vector<string> field(H);
    for (int i = 0; i < H; i++) {
        cin >> field[i];
        for (int j = 0; j < W; j++) {
            if (field[i][j] == '.') cnt++;
        }
    }

    sx = sy = 0;
    vector<vector<int>> dist(H, vector<int>(W, -1));
    dist[sx][sy] = 0;
    
    queue<pair<int, int>> q;
    q.push(make_pair(sx, sy)); //start

    // BFS
    while (!q.empty()) {
        pair<int, int> now = q.front(); // キューから先頭頂点を取り出す
        int x = now.first;
        int y = now.second;
        q.pop(); //popopopopopop

        // v から辿れる頂点をすべて調べる
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];

            if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if (field[nx][ny] == '#') continue;

            if (dist[nx][ny] == -1) {
                q.push(make_pair(nx, ny));
                dist[nx][ny] = dist[x][y] + 1;
            }
        }
    }

    if (dist[H-1][W-1] == -1)
        cout << -1 << endl;
    else
        cout << cnt - dist[H-1][W-1] - 1 << endl;
    

    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,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; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); }
const lint MOD = 1e9 + 7, INF = 1e18;

lint N, T;
lint arr[100000];
map<lint, lint> cnt;
int main() {
	cin.tie(0); ios_base::sync_with_stdio(false);
	cin >> N >> T;
	REP(i, N) {
		cin >> arr[i];
	}
	lint max_dist = 0;
	lint minv = arr[0];
	bool flag = true;
	REP(i, N - 1) {
		if (minv > arr[i + 1]) {
			flag = true;
			minv = arr[i + 1];
		}
		if (max_dist < arr[i + 1] - minv) {
			flag = true;
			max_dist = arr[i + 1] - minv;
		}
		chmax(max_dist, arr[i + 1] - minv);
		if (max_dist == (arr[i+1] - minv) && flag) {
			flag = false;
			cnt[max_dist]++;
		}
	}
	cout << cnt[max_dist] << endl;
}


Measured memory_bytes: 7296
#include<cstdio>
using namespace std;
long long n,m,s;

int main(){
    scanf("%lld",&n);
    m=n;
    while(m){
        s+=m%10;
        m/=10;
    }
    if(n%s==0) printf("Yes\n");
    else printf("No\n");
    return 0;
}


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

using namespace std;

int get(int x) {
  int res = 0;
  for (int i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      res++;
      if (x / i != i) res++;
    }
  }
  return res;
}

main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i += 2) {
    ans += (get(i) == 8);
  }
  cout << ans;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
#define rep(i,b) for(ll i=0;i<b;i++)
#define ll long long
using namespace std;
/*--Input//////////////////////////////////////////////////*/
inline void IN(void){return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
    cin >> first;
    IN(rest...);
    return;
}
#define SS(T, ...)      T __VA_ARGS__; IN(__VA_ARGS__);
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
/*--Output/////////////////////////////////////////////////*/
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
inline int p(void){cout << endl; return 0;}
template<class Head> int p(Head&& head){cout << head;p();return 0;} template<class Head,class... Tail> int p(Head&& head,Tail&&... tail){cout<<head<<" ";p(forward<Tail>(tail)...);return 0;}
struct ProconInit {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;


#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
#define all(x) (x).begin(),(x).end()
#define bit(n) (1LL<<(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; }
bool cmp(pair<ll,ll> a, pair<ll,ll> b) { return a.second < b.second; }

const int MOD = 1000000007;

int main() {
    SS(string,s)
    s[s.length()-1]=='s'?p(s+"es"):p(s+"s");
}

Measured memory_bytes: 3580
z = gets.chomp
n = z.split(' ')[0].to_i
k = z.split(' ')[1].to_i

x = gets.chomp
a = Array.new
a = x.split(' ')

b = a.group_by{|an| an}
c = b.sort{|(key1, val1), (key2, val2)| val2.length <=> val1.length}
if c.length <= k
    puts 0
else

	tmp = 0

	for i in 0..k-1
    	tmp += c[i][1].length
	end

	puts n - tmp
end

Measured memory_bytes: 58104
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

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

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int solve(string s){
    int a=stoi( s.substr(0,2) );
    int b=stoi( s.substr(3,2) );
    
    return a*60+b;
}


int main() {
    int n,q;
    cin>>n>>q;
    
    string at[20],a[20],bt[20],b[20];
    
    
    rep(i,n){
        cin>>at[i]>>a[i]>>bt[i]>>b[i];
    }
    vector<pair<string,int> > ans;
    
    for(int i=1;i<n;i++){
        int res=solve(at[i])-solve(bt[i-1]);
        if(res>=q){
            ans.pb(pair<string,int>(a[i],res));
        }
    }
    cout<<ans.size()<<endl;
    rep(i,ans.size()){
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }
    
    return 0;
}

Measured memory_bytes: 1228
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
using namespace std;
int main() {
  string s;
  stringstream ss;
  int n = 0;
  int max = 0;
  int sum = 0;
  cin >> n;
  for(int i=0;i<n;i++){
    int l = 0;
    cin >> l;
    sum += l;
    if(max < l) max = l;
  }
  if(max * 2 < sum) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

a, b, k = rm()
t = math.gcd(a, b)
cnt = 0
for i in range(t, 0, -1):
  if t % i == 0:
    cnt += 1
    if cnt == k:
      print(i)
      exit()
  
    










Measured memory_bytes: 27208
n, m = map(int, input().split())

A = [i for i in range(1, m+1)]
ANS = []
for i in range(n):
    B = list(map(int, input().split()))
    n = B[0]
    B = sorted(B[1:])
    T = []
    for i in B:
        if i in A:
            T.append(i)
    ANS = T
    A = ANS

print(len(ANS))

Measured memory_bytes: 38452
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i,a,n) for(int i=a;i<n;i++)
#define ve vector
#define ss second 
#define ff first
#define pf pop_front
#define pob pop_back
#define psf push_front
#define res(x) cout<<x<<'\n'
#define read(t) ll t;cin>>t

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    read(n);
    bool ans=true;
    ll a[n];
    map<ll,ll>m;
    f(i,0,n){
        cin>>a[i];
        m[a[i]]++;
    }
    f(i,0,n){
        if(m[a[i]]>1){
            ans=false;
            break;
        }
    }
    if(ans){
        cout<<"YES"<<endl;
    }
    else{
        cout<<"NO"<<endl;
    }
    
    
    


   

}

Measured memory_bytes: 14336
#include <bits/stdc++.h>
#define forr(a,b,v) for(int v=a; v<=b; v++)
using namespace std;

int main () {
    int n;
    int k;
    string s;

    cin >> n;
    cin >> k;
    cin >> s;

    forr(0,n-1,i) {
        if (i+1==k) {
            cout<< (char) (s[k-1]+32);
        }
        else {
            cout << s[i];
        }
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main() {
    int n,k;
    cin>>n;
    int i,j,ar[n][3],dp[n][3];
    for(i=0;i<n;i++)
    {
        cin>>ar[i][0]>>ar[i][1]>>ar[i][2];
        if(i==0)
        {
            dp[0][0]=ar[0][0];
            dp[0][1]=ar[0][1];
            dp[0][2]=ar[0][2];
            continue;
        }
        dp[i][0]=ar[i][0]+max(dp[i-1][1],dp[i-1][2]);
        dp[i][1]=ar[i][1]+max(dp[i-1][0],dp[i-1][2]);
        dp[i][2]=ar[i][2]+max(dp[i-1][0],dp[i-1][1]);
    }
    cout<<max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]));
	return 0;
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

ll gcd(ll a,ll b){
	if(!b) return a;
	return gcd(b,a%b);
}

ll n;
vl a,c,d;

int main(){
	cin>>n;
	a=d=vl(n+1);
	ll g=0;
	fill(d.begin(),d.end(),1);
	for(auto &i:a){
		cin>>i;
		g=gcd(g,abs(i));
	}
	for(int i=2;i<=n;i++) if(d[i]){
		for(int j=2*i;j<=n;j+=i) d[j]=0;
		if(abs(a[n])%i==0){
			vl b(i-1);
			for(int j=0;j<n;j++) b[j%(i-1)]+=a[j];
			bool B=1;
			for(auto j:b) if(abs(j)%i!=0) B=0;
			if(B) c.push_back(i);
		}
	}
	for(int i=2;i*i<=g;i++) if(g%i==0){
		c.push_back(i);
		while(g%i==0) g/=i;
	}
	if(g>1) c.push_back(g);
	sort(c.begin(),c.end());
	c.erase(unique(c.begin(),c.end()),c.end());
	for(auto i:c) cout<<i<<endl;
}

Measured memory_bytes: 512
import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto rd = readln.split.to!(int[]), n = rd[0], h = rd[1];
  auto a = new int[](n), b = new int[](n);
  foreach (i; 0..n) {
    auto rd2 = readln.splitter;
    a[i] = rd2.front.to!int; rd2.popFront();
    b[i] = rd2.front.to!int;
  }

  auto am = a.reduce!max;
  b.sort!"a>b";

  auto s = 0, ans = h;
  foreach (i; 0..n) {
    s += b[i];
    if (s >= h) {
      ans = min(ans, i+1);
      break;
    }
    ans = min(ans, i+1+(h-s+am-1)/am);
  }

  writeln(ans);
}


Measured memory_bytes: 3964
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int r = sc.nextInt();

		int rate = 0;

		if (n >= 10) {
			rate = r;
		} else {
			rate = r + (100 * (10 - n));
		}

		System.out.println(rate);
	}

}

Measured memory_bytes: 21844
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int maxn =110;

int main()
{
	string a,b;
	cin>>a>>b;
	if(a.size()<b.size())
	{
		puts("LESS");
		return 0;
	}
	else if(a.size()>b.size())
	{
		puts("GREATER");
		return 0;
	}
	else
	{
		for(int i=0;i<a.size();i++)
		{
			if(a[i]>b[i])
			{
				puts("GREATER");
				return 0;
			}
			else if(a[i]<b[i])
			{
				puts("LESS");
				return 0;
			}
			else
			{
				continue;
			}
		}
		puts("EQUAL");
	}
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
#include<math.h>
#include<stack>
#include<map>
#include<queue>
#define ls(x) (x<<1)
#define rs(x) ((x<<1)|1)
#define eps 1e-9
using namespace std;
int n,m;
int mp[505][505];
struct nod
{
	int x1,y1,x2,y2;
}ans[250005];
int cnt;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			scanf("%d",&mp[i][j]);
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<m;j++)
		{
			if(mp[i][j]&1)
			{
				mp[i][j+1]++;
				ans[++cnt]=(nod){i,j,i,j+1};
			}
		}
	}
	for(int i=1;i<n;i++)
	{
		if(mp[i][m]&1)
		{
			mp[i+1][m]++;
			ans[++cnt]=(nod){i,m,i+1,m};
		}
	}
	printf("%d\n",cnt);
	for(int i=1;i<=cnt;i++)
	{
		printf("%d %d %d %d\n",ans[i].x1,ans[i].y1,ans[i].x2,ans[i].y2);
	}
}

Measured memory_bytes: 6912
#include<iostream>
using namespace std;
typedef long long ll;

int main() {
    cin.tie(0);
    cin.sync_with_stdio(0);
    
    string s, t;
    cin >> s >> t;

    int n = s.size() - 1;
    int m = t.size() - 1;
    bool ok = false;
    int cnt = 0;
    for (int i = n; i >= m; i--) {
        ok = true;
        for (int j = 0; j <= m; j++) {
            if(s[i - j] != t[m - j] && s[i - j] != '?') ok = false;
        }
        if (ok) {
            for (int j = 0; j <= m; j++) {
                s[i - j] = t[m - j];
            }
            break;
        }
    }

    if (ok) {
        for (int i = 0; i <= n; i++) {
            if (s[i] == '?') cout << 'a';
            else cout << s[i];
        }
        cout << endl;
    } else {
        cout << "UNRESTORABLE" << endl;
    }
}

Measured memory_bytes: 3128
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define vcout(v) rep(i,v.size()) cout << v[i] << " "; cout << endl
#define mcout(m) rep(j,m.size()){vcout(m[j]);}
const ll MOD = 1e9 + 7;

void mainsolve(){

	string s,t;
	cin >> s >> t;
	ll a,b;
	cin >> a >> b;
	string u;
	cin >> u;

	if(s==u) cout << a-1 << " " << b << endl;
	else cout << a << " " << b-1 << endl;
	return;
}

int main(){
	
	//ll TC;cin>>TC;rep(I,TC){cout<<"case:"<<I+1<<"---"<<endl; // comment out!
		mainsolve();
	//} // comment out!
	return 0;
}	

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

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

  int A = 0, Z = 0;

  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) == 'A') {
      A = i;
      break;
    }
  }

  for (int i = S.size() - 1; i >= 0; i--) {
    if (S.at(i) == 'Z') {
      Z = i;
      break;
    }
  }

  cout << Z - A + 1 << endl;
}

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

#define pb push_back
#define pf push_front
#define pp pop_back
#define sz(a) (int)(a.size())
#define mp make_pair
#define F first
#define S second
#define next _next
#define prev _prev
#define left _left
#define right _right
#define y1 _y1
#define all(x) x.begin(), x.end()
#define what_is(x) #x << " is " << (x)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = (int)1e6 + 123;
const ll INF = (ll)1e18 + 123;
const int inf = (int)1e9 + 123;
const int MOD = (int)1e9 + 7;

void megaRandom() {
	unsigned int FOR;
 	asm("rdtsc" : "=A"(FOR));
  srand(FOR);
}

int a, b;

int main() {
	megaRandom();
	cin >> a >> b;
	if(a <= 0 && 0 <= b) {
		cout << "Zero";
		return 0;
	}
	if(a > 0) {
		cout << "Positive";
		return 0;
	}
	if(b < 0 && (b - a + 1) % 2 == 0) {
		cout << "Positive";
		return 0;
	}
	cout << "Negative";
	return 0;  
}


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

typedef long long ll;
using namespace std;

const ll MOD = 1000000007;
const ll maxn = 200005;
ll t[maxn];
ll t2[maxn];
ll inv[maxn];

void add(ll t[], ll i, ll value) {
  for (; i < maxn; i += (i + 1) & -(i + 1))
    t[i] = (t[i] + value)%MOD;
}

// sum[0..i]
ll sum(ll t[], ll i) {
  ll res = 0;
  for (; i >= 0; i -= (i + 1) & -(i + 1))
    res = (res + t[i])%MOD;
  return res;
}

ll exp(ll a, ll b){
    if(b==0)
        return 1;
    else if (b%2==0){
        ll y = exp(a,b/2);
        return (y*y)%MOD;
    }
    return (a*exp(a,b-1))%MOD;
}

int main()
{
    ll N;
    cin>>N;
    ll A[N];
    for(ll i=0; i<N; i++){
        cin>>A[i];
    }
    ll freq[N+1];
    for(ll i=0; i<N+1; i++){
        freq[i] = 0;
    }
    for(ll i=0; i<N; i++){
        freq[A[i]]++;
    }
    for(ll i=N-1; i>=0; i--){
        freq[i] = freq[i+1]+freq[i]-1;
    }

    for(ll i=1; i<N+1; i++){
        if(freq[i]==0){
            cout<<0<<endl;
            return 0;
        }
    }

    ll partial[N+1];
    ll check[N+1];
    ll pcheck[N+1];
    for(ll i=0; i<N+1; i++)
        check[i] = 0;
    partial[0] = 1;
    for(ll i=1; i<N+1; i++){
        partial[i] = (partial[i-1]*(freq[i]-1))%MOD;
        partial[i] = (partial[i] * exp(freq[i],MOD-2))%MOD;
        if(partial[i]==0){
            partial[i] = 1;
            check[i] = 1;
            // signify 0
        }
    }
    pcheck[0] = 0;
    ll u = 0;
    for(ll i=1; i<N+1; i++){
        if(check[i]==1){
            for(ll j=u; j<i; j++){
                pcheck[j] = i;
            }
            u = i;
        }
    }
    for(ll j=u; j<N+1; j++){
        pcheck[j] = N+1;
    }

    ll ans = 0;
    for(ll i=0; i<N; i++){
        ll carry = sum(t,A[i]);
        ans =  (ans + partial[A[i]]*carry)%MOD;
        add(t,A[i],exp(partial[A[i]],MOD-2));
        add(t,pcheck[A[i]],-exp(partial[A[i]],MOD-2));
    }


    for(ll i=0; i<N/2; i++){
        ll b = A[N-1-i];
        A[N-1-i] = A[i];
        A[i] = b;
    }

    for(ll i=0; i<N; i++){
        ll carry = sum(t2,A[i]);
        ll counter = sum(inv, A[i]-1);
        ans =  (ans + 2*counter - partial[A[i]]*carry)%MOD;
        add(t2,A[i]+1,exp(partial[A[i]],MOD-2));
        add(t2,pcheck[A[i]],-exp(partial[A[i]],MOD-2));
        add(inv, A[i], 1);

    }


    ans = (ans * exp(2,MOD-2))%MOD;

    for(ll i=1; i<N+1; i++){
        ans = (ans*freq[i])%MOD;
    }
    while(ans<0)
        ans+=MOD;
    cout<<ans<<endl;






    return 0;
}


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

#define EPS (1e-7)
#define INF 1e18
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define PI (acos(-1))

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

int main() {

    ll N, K;
    cin >> N >> K;
    vector<ll> A(N);
    REP(i, N) cin >> A[i];

    vector<ll> S(N+1);
    S[0] = 0;
    REP(i, N)  S[i + 1] = (S[i] + A[i] - 1 )% K;
    //REP(i, N+1) cout << S[i] << " "; cout << endl;

    map<ll, ll> m;
    ll ans = 0;
    REP(i, min(N + 1, K)){
        m[S[i]]++;
    }
    REP(i, N+1){
        ll tmp = S[i];
        ans += m[tmp] - 1;
        //cout << tmp << " " << (m[tmp] - 1) << endl;
        m[tmp]--;
        if (i + K - 1 < N) m[S[i + K]]++;
    }
    cout << ans << endl;



    return 0;
}


Measured memory_bytes: 15872
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <bitset>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <set>
#include <complex>

using namespace std;

const int SIZE = 1 << 10;

int pointer = SIZE;
char buffer[SIZE];

char Advance() {
    if (pointer == SIZE) {
        fread(buffer, 1, SIZE, stdin);
        pointer = 0;
    }
    return buffer[pointer++];
}

int Read() {
    int answer = 0;
    char ch = Advance();
    while (!isdigit(ch))
        ch = Advance();
    while (isdigit(ch)) {
        answer = answer * 10 + ch - '0';
        ch = Advance();
    }
    return answer;
}

const int MAXN = 5000;
const int MAXK = 5;
const int SIGMA = 26;

int k, n;
char s[1 + MAXN], t[1 + MAXK + 1];

bool Find(int m) {
    if (m > n)
        return false;
    for (int i = 1; i + m - 1 <= n; i++) {
        bool ok = true;
        for (int j = i; j <= i + m - 1; j++)
            if (s[j] != t[j - i + 1])
                ok = false;
        if (ok)
            return true;
    }
    return false;
}

void Backtracking(int m) {
    for (char ch = 'a'; ch <= 'z'; ch++) {
        m++;
        t[m] = ch;
        if (Find(m)) {
            k--;
            if (!k) {
                t[m + 1] = NULL;
                printf("%s\n", t + 1);
                exit(0);
            }
            Backtracking(m);
        }
        m--;
    }
}

int main() {
    //freopen("tema.in", "r", stdin);
    //freopen("tema.out", "w", stdout);
    scanf("%s%d", s + 1, &k);
    n = strlen(s + 1);
    Backtracking(0);
    return 0;
}


Measured memory_bytes: 128
#define DEBUG 0

/**
 * File    : C2.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-5-28 11:49:49
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <functional>
#include <random> // auto rd = bind(uniform_int_distribution<int>(0, 9), mt19937(10101010));
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

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

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, Q;
string S;
int l[100010];
int r[100010];

int main()
{
  cin >> N >> Q >> S;
  for (auto i = 0; i < Q; i++)
  {
    cin >> l[i] >> r[i];
  }
  vector<int> sum(N + 1, 0);
  for (auto i = 1; i < N; i++)
  {
    if (S[i - 1] == 'A' && S[i] == 'C')
    {
      sum[i + 1] = sum[i] + 1;
    }
    else
    {
      sum[i + 1] = sum[i];
    }
  }
  for (auto i = 0; i < Q; i++)
  {
    cout << sum[r[i]] - sum[l[i]] << endl;
  }
}

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

int main() {
  long long N, K;
  cin >> N >> K;

  long long digits = 1;
  for (int i = 1; i <= N; i++) {
    if (pow(K, i) <= N) {
      digits++; 
    } else {
      cout << digits << endl;
      return 0;
    }
  }
  cout << digits << endl;
}


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

#define ONLINE_JUDGE

#ifndef ONLINE_JUDGE
#define dbg(x...)                             \
	{                                         \
		cerr << "\033[32;1m" << #x << " -> "; \
		err(x);                               \
	}
void err()
{
	cerr << "\033[39;0m" << endl;
}
template <typename T, typename... A>
void err(T a, A... x)
{
	cerr << a << ' ';
	err(x...);
}
#else
#define dbg(...)
#endif

typedef long long LL;

const int N = 1e3 + 5, M = N * N;

int n, m;

char mp[N][N];

int id[N][N];

namespace shortestpath
{
struct node
{
	int u, dis;
	node(int u = 0, int dis = 0) : u(u), dis(dis)
	{
	}
	bool operator<(const node &other) const
	{
		return dis < other.dis;
	}
};
void go(int s, int dist[], int n, const vector<pair<int, int>> G[])
{
	memset(dist, -1, sizeof(int) * (n+1));
	dist[s] = 0;
	queue<int> Q;
	Q.push(s);
	while (!Q.empty())
	{
		int now = Q.front(); Q.pop();
		for (auto p : G[now])
		{
			int v = p.first;
			if (dist[v] == -1)
			{
				dist[v] = dist[now] + 1;
				Q.push(v);
			}
		}
	}
	// priority_queue<node, vector<node>, greater<node>> pq;
	// pq.emplace(s, 0);
	// while (!pq.empty())
	// {
	// 	auto now = pq.top();
	// 	pq.pop();
	// 	if (dist[now.u] < now.dis)
	// 		continue;
	// 	for (const auto &p : G[now.u])
	// 	{
	// 		int v = p.first, w = p.second;
	// 		if (dist[now.u] + w < dist[v])
	// 		{
	// 			dist[v] = dist[now.u] + w;
	// 			pq.emplace(v, dist[v]);
	// 		}
	// 	}
	// }
}
} // namespace shortestpath

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

vector<pair<int, int>> G[M];

void addEdge(int u, int v, int w)
{
	G[u].emplace_back(v, m);
}

int main(int argc, char const *argv[])
{
	scanf("%d%d", &n, &m);
	int s = 1, tot = s;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			id[i][j] = ++tot;
	for (int i = 0; i < n; ++i)
	{
		scanf("%s", mp[i]);
		for (int j = 0; j < m; ++j)
		{
			if (mp[i][j] == '#')
			{
				addEdge(s, id[i][j], 0);
			}
			for (int k = 0; k < 4; ++k)
			{
				int tx = i + dx[k], ty = j + dy[k];
				if (tx >= 0 && tx < n && ty >= 0 && ty < m)
				{
					addEdge(id[i][j], id[tx][ty], 1);
				}
			}
		}
	}
	static int dist[M];
	shortestpath::go(s, dist, tot, G);

	int ans = 0;
	for (int i=1; i<=tot; ++i)
		ans = max(ans, dist[i]);
	printf("%d\n", ans-1);
	return 0;
}

Measured memory_bytes: 91496
#include<iostream>
using namespace std;
int P,Q,R,C;

int cal(int,int,int,int,int);

int main(){
  int n,i[1000],p[1000],q[1000],r[1000],c[1000],ans,t;
  while(1){
  cin>>n;
  if(n==0) break;
  t=0;
  for(int j=0;j<n;j++){
    cin>>i[j]>>p[j]>>q[j]>>r[j];
    c[j]=p[j]*4+q[j]*9+r[j]*4;
  }
  cin>>P>>Q>>R>>C;
  for(int j=0;j<n;j++){
    ans=cal(i[j],p[j],q[j],r[j],c[j]);
    if(ans!=0){
   cout<<ans<<endl;
   t=1;
    }
  }
    if(t==0) cout<<"NA"<<endl;
  }

  return 0;
}

int cal(int i,int p,int q,int r,int c){
  int count=0;
  if(p<=P) count++;
  if(q<=Q) count++;
  if(r<=R) count++;
  if(c<=C) count++;
  if(count==4) return i;
  else return 0;
}

Measured memory_bytes: 1180