akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N, M;
int X[100005];
int cnt[100005];
int mod[100005];
int pena[100005];

int main() {
    cin >> N >> M;
    rep(i, N) cin >> X[i];
    rep(i, N) cnt[X[i]]++;
    rep(i, N) mod[X[i] % M]++;
    int ans = 0;
    rep(i, M / 2 + 1) {
        if (i == ((M - i) % M)) {
            ans += mod[i] / 2;
            pena[i] = mod[i];
            continue;
        }
        int a = min(mod[i], mod[M - i]);
        ans += a;
        // printf("%d-%d: %d\n", i, M - i, a);
        pena[i] = pena[M - i] = a;
    }
    // cout << ans << endl;
    int m = *max_element(X, X + N);
    // rep(i, m + 1) printf("%d: %d %d\n", i, cnt[i], pena[i % M]);
    rep(i, m + 1) {
        if (cnt[i] % 2 == 1) {
            cnt[i]--;
            pena[i % M]--;
        }
    }
    // rep(i, m + 1) printf("%d: %d %d\n", i, cnt[i], pena[i % M]);
    rep(i, M) {
        if (pena[i] > 0) {
            ans -= (pena[i] + 1) / 2;
            /*
            for (int j = i; j < m + 1; j += i) {
                int a = min(pena[i], cnt[j]);
                cnt[j] -= a;
                pena[i] -= a;
                if (pena[i] == 0) break;
            }
            */
        }
    }
    // rep(i, m) if (cnt[i]) printf("%d: %d\n", i, cnt[i]);
    rep(i, m + 1) ans += cnt[i] / 2;
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1792
n=int(input())
ans=n
for n1 in range(n+1):
  n2=n-n1
  cnt1=0
  while n1>0:
    cnt1+=n1%6
    n1=n1//6
  cnt2=0
  while n2>0:
    cnt2+=n2%9
    n2=n2//9
  ans=min(ans,cnt1+cnt2)
print(ans)

Measured memory_bytes: 9132
#include <iostream>
using namespace std;
int main()
{
  int n,a,b,c;
  cin >> n;
  if(n == 1){
    cout << "Hello World" << endl;
  }else if(n == 2){
    cin >> a;
    cin >> b;
    c = a + b;
    cout << c << endl;
  }
  return(0);
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <vector>
#include <deque>
#include <cstdlib>
#include <numeric>
#include <cstring>
#include <ctime>
#include <climits>
#include <sstream>
#include <complex>
#define Mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define Rep(__i,__n) for(int __i = 0 ; __i < (__n);__i++)
#define Range(_ind,_a,_b) for(int _ind = (_a) ; _ind <(int)(_b);_ind++)
#define iRange(_i,_a,_b,_c) for(int _i = (_a) ; _i <(int)_b;_i+=_c)
#define All(__cont)  (__cont).begin(),(__cont).end()
#define ForEach(_itr,_cont) for(auto _itr  = _cont.begin();_itr !=  _cont.end();_itr++)


using namespace std;
typedef complex<double> P;
typedef long long ll;
typedef vector<int> vi ;
typedef vector<double> vd;
typedef vector<vector <int> > vvi;
typedef vector<vector<double> > vvd;
typedef pair<int,int> pii;
const int inf = INT_MAX / 3;
const double eps = 1e-9;

template <typename T >
void output(T a,string label = ""){
#ifdef DEBUG
  cout<<"[" <<(label==""?"":label + ":")<< a <<  "]"<< endl;
#endif
}
template <typename T>
void output(vector<T> &vec){
#ifdef DEBUG
  cout << "[";
  ForEach(itr,vec){
    if(itr != vec.begin())cout << ",";
    cout << *itr;
  }
  cout << "]"<<endl;;
#endif
}
#ifndef __NOT__MAIN
vi route;
int DFS(vvi &g, vi &visited, const int now, const size_t count){
  int ans = 0;
  route.pb(now);
  visited[now] = true;
  if (count+1 == g.size()) {
    ans = 1 ;
  } 
  ForEach(next, g[now]) {
    if(!visited[*next]){
      ans += DFS(g, visited, *next, count+1);
    }
  }  
  visited[now] = false;
  route.pop_back();
  return ans;
}
int main(){
  int N, M;
  cin >> N >> M;
  vvi graph(N);
  Rep(_i, M){
    int a, b;
    cin >> a >> b;
    a--;b--;
    graph[a].pb(b);
    graph[b].pb(a);
  }
  vi visited(N,false);
  cout << DFS(graph, visited, 0, 0) << endl;
}
#endif


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

int main(void)
{
    int i, n;
    char s[100001];
    scanf("%s", s);

    n = strlen(s);

    int ans = n;
    for (i = 0; i < 2; i++)
    {
        int cnt = 0;
        for (int j = 0; j < n; j++)
        {
            if(j % 2 == 0 && s[j] != '0' + i) cnt++;
            if(j % 2 == 1 && s[j] == '0' + i) cnt++;
            }
            if(ans > cnt)
                ans = cnt;
    }

    printf("%d\n", ans);
    return 0;
}

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

N = -N

if N == 0:
  print(0)
  exit()
  
t = []
while N != 0:
  s = N % -2
  if s == 0:
    t.append(0)
  else:
    t.append(1)
    
  N //= -2
  
t = list(reversed(t))
print(''.join(map(str, t)))


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

int main() {
  int N;
  cin>>N;
  double sum = 0;
  
  double x;
  string u;
  for(int i=0; i<N; i++){
    cin >> x >> u;
    if(u=="BTC"){
      sum += x*380000;
    }
    else sum += x;
  }
  
  cout << sum << endl;
}

Measured memory_bytes: 256
#include <stdio.h>

#define MAX_MAS		( 1028 )

int masMission[ MAX_MAS + 1 ];

int main( void )
{
	int numOfMas;
	int numOfDice;
	int i;
	int nowMas;
	int dice;
	int step;
	
	while( 1 )
	{	
		scanf( "%d%d", &numOfMas, &numOfDice );
		if( numOfMas == 0 && numOfDice == 0 )break;
		for( i = 0; i < numOfMas; i++ )
		{
			scanf( "%d", &masMission[i] );
		}
		
		nowMas = 0;
		step = numOfDice + 1;
		for( i = 0; i < numOfDice; i++ )
		{
			scanf( "%d", &dice );
			nowMas += dice;
			if( nowMas >= numOfMas ) nowMas = numOfMas - 1;
			nowMas += masMission[ nowMas ];
			if( nowMas >= numOfMas ) nowMas = numOfMas - 1;
			
			if (step > i && nowMas == numOfMas - 1) step = i;
		}
		printf( "%d\n", step + 1 );
	}
	
	return (0);
}

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

int main(){
	int s1s2s3;
	cin >> s1s2s3;
	cout << s1s2s3 / 100 % 10 + s1s2s3 / 10 % 10 + s1s2s3 / 1 % 10 << endl;
}

Measured memory_bytes: 256
let rec q()=Scanf.scanf"%d %d
"(fun m d->if m<>0&&d<>0then(let rec($)i z=if i>m-2then z else(i+1)$((z+[|31;29;31;30;31;30;31;31;30;31;30;31|].(i))mod 7)in q(print_endline[|"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"|].((d+(0$2))mod 7))));;q()

Measured memory_bytes: 2228
#include<iostream>
using namespace std;
int main()
{
    string s;
    cin >> s;
    int count = 0;
    for(int i = 0; i < s.length(); ++i){
      if(s[i] == '1'){
        ++count;
      }
    }
    cout << count << endl;

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int arr[n];
  int k;
  cin>>k;
	for(int i=0;i<n;i++)
		cin>>arr[i];
	int dp[n];
	dp[0]=0;
 	//dp[1]=abs(arr[1]-arr[0]);
  int inx=0;
 
//   for(inx=1;inx<k;inx++)
//   {
//   	 int temp=INT_MAX;
//   	for(int j=1;j<=inx;j++)
// 	temp=min(temp,abs(arr[inx]-arr[inx-j]));

// 	dp[inx]=temp;
//   }
  for(int i=1;i<n;i++)
	{	
     int a=INT_MAX;
		for(int j=1;j<=k&&j<=i;j++)
        {
          a=min(a,abs(arr[i]-arr[i-j])+dp[i-j]);
        }
      dp[i]=a;
	}

	cout<<dp[n-1];

}


Measured memory_bytes: 1024
import numpy as np
N,M,X = map(int,input().split())

clist=[]
for i in range(N):
    clist.append(list(map(int,input().split())))

#print(clist)

total_all=[]
for i in range(2**N):
    total=np.zeros(M+1)
    for j in range(N):
        
        # iを2進数表記した場合の下から数えてj番目が1ならtrue
        if ((i >> j) & 1):
            total=total+np.array(clist[j])
    total_all.append(list(total))

#print(total_all)
ans=[]
#print(total_all[1][1:M+1])

for i in range(len(total_all)):
    if min(total_all[i][1:M+1])>=X:
        ans.append(total_all[i][0])

if len(ans)>=1:
    print(int(min(ans)))
else:
    print("-1")

Measured memory_bytes: 28996
#include<bits/stdc++.h> 
//#define <- プログラムの文字列を置換する
//typedef <- 新しい型に名前を付けるためのキーワード

using namespace std;

#define MAX_N 1
#define INF 1000000000 //1e9
#define PI 3.14159265359
#define mod 1000000007

int main(){
    int H,A;
    cin >> H >> A;
    int count = 0;
    if( A > H) {
        cout << 1 << endl;
        return 0;
    }
    for(int i = H; i > 0 ; i -= A){
        count++;
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int,int>
#define mod 1000000007
#define endl "\n"

int power(int a,int b,int m=mod)
{
	if(b==0)	return 1;
	if(b==1)	return a;
	int res=power(a,b/2,m);
	res=(res*res)%m;
	if(b&1)	res=(res*a)%m;
	return res;
}

int modinv(int a){ return power(a,mod-2);}

void solve()
{
	double pi=3.14159;
	int n;
	cin>>n;
	cout<<2*pi*n<<endl;
}

int32_t main()
{
	ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
	int t=1;//cin>>t;
	while(t--)	solve();
	return 0;
}

Measured memory_bytes: 3800
//***Wherever you are, and whatever you do, be in love.***
 
#include<bits/stdc++.h>
using namespace std;
 
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long 
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl "\n"
 
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;
 
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
#define PI 3.14159265 

int main(){
   ll h1,m1,h2,m2,k;
   cin>>h1>>m1>>h2>>m2>>k;

   cout<<(h2-h1)*60+(m2-m1)-k;
}

Measured memory_bytes: 3632
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import bisect
import numpy as np
import math
from collections import deque
from collections import defaultdict
from math import gcd


x1, x2, x3, x4, x5 = map(int, input().split())

if x1 == 0:
    print(1)
if x2 == 0:
    print(2)
if x3 == 0:
    print(3)
if x4 == 0:
    print(4)
if x5 == 0:
    print(5)

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

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

int main(){
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  int m =0;
  for(int i=0; i<N; i++){
    cin >> A[i];
    m = max(m, A[i]);
  }
  int g = A[0];
  for(int i=1; i<N; i++){
    g = gcd(g, A[i]);
  }
  if(K%g==0 && K<=m){
    cout << "POSSIBLE" << endl;
    return 0;
  }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}

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

int main()
{
  int A[100010];
  int n;
  
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> A[i];
  }

  int ans = 0;
  int count = 0;
  int index = n - 2;
  while (index >= 0)
  {
    if (A[index] >= A[index+1])
    {
      ++count;
      ans = max(ans, count);
    }
    else
    {
      count = 0;
    }
    --index;
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int N;
	int max = -1;
	int res ;
	int v, j, i;

	cin >> N;
	for(i = 1; i <= N; i++) {
		for(j = 0, v = i; v % 2 == 0; v /= 2, j++) {
		}
		if(j > max) {
			max = j;
			res = i;
		}
	}

	cout << res << endl; 	
}


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

bool check(lint cand, lint m, lint v, lint p, vector<lint> a) {
    lint modified = a[cand] + m;
    lint up = a.end() - upper_bound(a.begin(), a.end(), modified);
    p -= up + 1;
    if (p < 0) {
        return false;
    }
    v -= cand + up;
    if (v <= 0) {
        return true;
    }
    lint ub = upper_bound(a.begin(), a.end(), modified) - a.begin();
    lint limit = 0;
    for (int i = cand + 1; i < ub - p; i++) {
        limit += min(modified - a[i], m);
    }
    return limit >= v * m;
}

bool check2(lint cand, lint m, lint v, lint p, vector<lint>& a) {
    lint n = a.size();
    if (n - cand <= p) {
        return true;
    }
    lint modi = a[cand] + m;
    // cout << a[n - p] << " " << modi << endl;
    if (a[n - p] > modi) {
        return false;
    }
    v -= p + cand;
    if (v <= 0) {
        return true;
    }
    lint limit = 0;
    for (int i = cand + 1; i < n - p + 1; i++) {
        //    cout << i << " " << min(modi - a[i], m) << endl;
        limit += min(modi - a[i], m);
    }
    // cout << "n-p+1:" << n - p + 1 << endl;
    // cout << "limit:" << limit << endl;
    return limit >= v * m;
}

int main() {
    lint n, m, v, p;
    cin >> n >> m >> v >> p;
    vector<lint> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    lint ok = n - 1, no = -1;
    while (abs(ok - no) > 1) {
        lint mid = (ok + no) / 2;
        if (check2(mid, m, v, p, a)) {
            ok = mid;
        } else {
            no = mid;
        }
    }
    cout << n - ok << endl;
}

Measured memory_bytes: 1152
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
  int H,W;
  char A[101][101];
  scanf("%d %d",&H,&W);
  for(int i=1;i<=H;i++){
    for(int j=0;j<=W;j++) scanf("%c",&A[i][j]);
  }
  for(int i=1;i<=W+2;i++) printf("#");
  printf("\n");
  for(int i=1;i<=H;i++){
    printf("#");
    for(int j=1;j<=W;j++) printf("%c",A[i][j]);
    printf("#\n");
  }
  for(int i=1;i<=W+2;i++) printf("#");
  printf("\n");
  return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <ios> 
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
#include <set>
#include <cmath>
#include <map>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)

using namespace std;
typedef long long int lli;
//const int MAX=;

int main(){
	int N;
	cin>>N;
	cout<<(((N-1)/111)+1)*111;
}

Measured memory_bytes: 256
import java.io.IOException;
import java.util.Scanner;

public class Main{
	
	static int B;
	static int num;
	static int left ,right;
	
	public static void main(String args[]) throws IOException {
		Scanner scan = new Scanner(System.in);

		for(B = scan.nextInt();B > 0;B = scan.nextInt()) {
			left = 1;
			right = 1;

			num = 0;

			for(int i = 1;;i++) {
				num += i;
				right = i;
				if(num >= B) { break; }
			}

			while(true) {
				if(num == B) {
					System.out.println(left + " " + (right - left + 1));
					break;
				}else if(num > B) {
					Numdec();
				}else if(num < B) {
					NumInc();
				}
			}
		}

		scan.close();
	}

	public static void NumInc() {
		while(num < B) {
			right++;
			num += right - left;
			left++;
		}
		if(num / 2 > B ) {
			left = B;
			right = B;
		}
	}

	public static void Numdec() {
		while(num > B) {
			num -= left;
			left++;
		}
	}
}


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

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define each(i,c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(v) v.begin(), v.end()
#define mset(a, n) memset(a, n, sizeof(a))

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

const int INF = 1000000000;
const int MOD = 1000000007;
const double eps = 1e-9;

int main(int argc, char *argv[]) {
    // cin.tie(0);
    // ios::sync_with_stdio(false);
    ll N, K;
    cin >> N >> K;
    vector<pii> vp(N);
    rep(i, N) {
        int a, b;
        cin >> a >> b;
        vp[i] = make_pair(a, b);
    }

    sort(vp.begin(), vp.end());
    ll num = 0;
    rep(i, vp.size()) {
        if (K <= num + vp[i].second) {
            cout << vp[i].first << endl;
            return 0;
        }
        num += vp[i].second;
    }
    return 0;
}



Measured memory_bytes: 1024
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
	//min(a,b)...a,bの小さいほう
	//max(a,b)...     大きいほう
	int a;
	int b;
	int c;
	int d;
	int e;
	std::cin >> a >> b >> c;
	if (a <= c && a + b >= c) {
		cout << "YES" << "\n";
	}
	else {
		cout << "NO" << "\n";
	}
	std::cin >> e;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int,int> pi;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    string s;
    cin>>s;
    bool ans = true;
    if (s[0]!='A') ans = false;
    else {
        if (s[1]=='C' || s[-1+s.size()]=='C') ans=false;
        else {
            bool ch=false;
            for (int i=2;i<(-1+s.size());i++) {
                if (s[i]=='C') {
                    if (ch) ans = false;
                    ch = true;
                }
                else {
                    if (s[i]>='A' && s[i]<='Z') {
                        ans = false;
                    }
                }
            }
            if (!ch) ans=false;
        }
        if (s[1]>='A' && s[1]<='Z') {
            ans = false;
        }
        if (s[-1+s.size()]>='A' && s[-1+s.size()]<='Z') {
            ans = false;
        }
    }

    if (ans) cout<<"AC";
    else cout<<"WA";
}


Measured memory_bytes: 256
#include <iostream>
#include <math.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>

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

typedef long long lint;

using namespace std;
lint p= 1000*1000*1000+7;

int main(){
  string S;
  cin>>S;
  lint A,AB,ABC,N;
  N=1;
  A=0;
  AB=0;
  ABC=0;
  for(auto c:S){
    if(c=='A'){
      A=A+N;
      A%=p;
    }else if(c=='B'){
      AB=A+AB;
      AB%=p;
    }else if(c=='C'){
      ABC=ABC+AB;
      ABC%=p;
    }else {
      lint Ad=A;
      lint ABd=AB;
      lint ABCd=ABC;
      A=N+Ad*3;
      AB=Ad+ABd*3;
      ABC=ABd+ABCd*3;
      N*=3;
      N%=p;
      A%=p;
      AB%=p;
      ABC%=p;
    }
    // cerr<<A<<" "<<AB<<" "<<ABC<<endl;
  }
  cout<<ABC<<endl;
  return 0;
}


Measured memory_bytes: 512
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define str to_string
#define endl "\n"
#define PI 3.141592653589
using namespace std;
using lint = long long;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

//(*'ω'*)ACほちぃ...
int main(){
    int n;cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    sort(all(a));
    vector<int> done(1000001);
    vector<int> check(1000001);
    for(int i=0;i<n;i++){
        int x=a[i];
        if(done[x]){
            check[x]=1;continue;
        }
        for(int j=x;j<=1000000;j+=x){
            done[j]=1;
            if(j!=x)check[j]=1;
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        if(check[a[i]]==0)ans++;
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 12020
from itertools import accumulate
from bisect import bisect_right

N, K = map(int, input().split())
A = [3] + [int(input()) for i in range(N)]

diff = [a - K for a in A]
diff = list(accumulate(diff))


class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= (i & -i)
        return s

    def add(self, i, x):
        while i <= self.size:
            self.bit[i] += x
            i += (i & -i)

    def reset(self):
        self.bit = [0] * (self.size + 1)


BIT = BinaryIndexedTree(N + 1)

temp = sorted(diff)
order = [bisect_right(temp, d) for d in diff]
ans = 0

for x in order:
    ans += BIT.sum(x)
    BIT.add(x, 1)

print(ans)


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

int main(){
	bitset<64> x(0);
	int n;
	cin>>n;
	lp(i,n){
		int a;
		cin>>a;
		unsigned long long b;
		switch(a){
			case 0:
				cin>>b;
				if(x[b]) cout<<1<<endl;
				else cout<<0<<endl;
				break;
			case 1:
				cin>>b;
				x.set(b);
				break;
			case 3:
				cin>>b;
				x.flip(b);
				break;
			case 2:
				cin>>b;
				x.reset(b);
				break;
			case 4:
				if(x.all()) cout<<1<<endl;
				else cout<<0<<endl;
				break;
			case 5:
				if(x.any()) cout<<1<<endl;
				else cout<<0<<endl;
				break;
			case 6:
				if(x.none()) cout<<1<<endl;
				else cout<<0<<endl;
				break;
			case 7:
				cout<<x.count()<<endl;
				break;
			case 8:
				b=x.to_ullong();
				cout<<b<<endl;
				break;
		}
	}
	return 0;
}



Measured memory_bytes: 3112
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<cassert>
#include<stack>
#include<fstream>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)

int main(){
	set<ll> s;
	ll n;
	cin>>n;
	rep(i,n){
		ll a; cin>>a;
		s.insert(a);
	}
	cout<<(s.size()==n?"YES":"NO")<<endl;
}


Measured memory_bytes: 9600
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;

int main() {

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

  long long int r, c;
  cin >> r >> c;

  vector< string > p;

  for ( long long int i = 0; i < r; i++ ) {

    string in;
    cin >> in;
    p.push_back( in );

  }

  queue< long long int > qx, qy, qm, qd;
  qx.push(0);
  qy.push(0);
  qm.push(0);
  qd.push(0);

  bool ans = false;

  bool f[21][21][16][4] = {};

  while( qx.size() > 0 ) {

    long long int x, y, m, d;
    x = qx.front();
    qx.pop();
    y = qy.front();
    qy.pop();
    m = qm.front();
    qm.pop();
    d = qd.front();
    qd.pop();

    for (;;) {

      //      cout << x << " " << y << " " << m << " " << d << endl;

      f[y][x][m][d] = true;

      if ( p[y][x] == '>' ) {
	d = 0;
      }else if ( p[y][x] == '<' ) {
	d = 1;
      }else if ( p[y][x] == 'v' ) {
	d = 2;
      }else if ( p[y][x] == '^' ) {
	d = 3;
      }else if ( p[y][x] == '_' ) {
	if ( m == 0 ) {
	  d = 0;
	}else {
	  d = 1;
	}
      }else if ( p[y][x] == '|' ) {
	if ( m == 0 ) {
	  d = 2;
	}else {
	  d = 3;
	}
      }else if ( p[y][x] == '@' ) {
	ans = true;
	break;
      }else if ( p[y][x] == '+' ) {
	m++;
	if ( m == 16 ) m = 0;
      }else if ( p[y][x] == '-' ) {
	m--;
	if ( m == -1 ) m = 15;
      }else if ( p[y][x] == '.' ) {
      }else if ( p[y][x] == '?' ) {
	d = 0;
	for ( int j = 1; j < 4; j++ ) {
	  int cx = ( x + dx[j] + c ) % c;
	  int cy = ( y + dy[j] + r ) % r;
	  if ( f[cy][cx][m][j] == true ) continue;
	  f[cy][cx][m][j] = true;
	  qx.push(cx);
	  qy.push(cy);
	  qm.push(m);
	  qd.push(j);
	}
      }else {
	m = p[y][x] - '0';
      }

      f[y][x][m][d] = true;
      x = ( x + dx[d] + c ) % c;
      y = ( y + dy[d] + r ) % r;
      if ( f[y][x][m][d] == true ) break;

    }

    if ( ans == true ) break;

  }

  if ( ans == true ) {
    cout << "YES" << endl;
  }else {
    cout << "NO" << endl;
  }

}

Measured memory_bytes: 1344
#include<stdio.h>
#define N 100
int main(){
  int a,i,j,k,A[N],min,head,flag=0;
  scanf("%d",&a);
  for(i=0;i<a;i++){
    scanf("%d",&A[i]);
  }
  for(i=0;i<a-1;i++){
    min = A[i];
    k = i;
    for(j=i+1;j<a;j++){
      if(A[j]<min){
	min=A[j];
	k=j;
      }
    }
    if(i!=k){
    head=A[i];
    A[i]=A[k];
    A[k]=head;
    	flag++;
    }
  }
  for(i=0;i<a;++i){
    if(i>0)printf(" ");
    printf("%d",A[i]);
  }
  printf("\n");
  printf("%d\n",flag);
  return 0;
}


Measured memory_bytes: 2104
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<int, P> E;
#define MOD (1000000007ll)
#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	b += MOD;
	a += b;
	a %= MOD;
}

vector<string> v,u;

int main(void){
	int n,m,i,ans=0,tmp;
	string s;
	cin >> n;
	for(i=0; i<n; ++i){
		cin >> s;
		v.push_back(s);
	}
	sort(v.begin(),v.end());
	cin >> m;
	for(i=0; i<m; ++i){
		cin >> s;
		u.push_back(s);
	}
	sort(u.begin(),u.end());
	for(i=0; i<n; ++i){
		tmp = (int)(upper_bound(v.begin(),v.end(),v[i])-lower_bound(v.begin(),v.end(),v[i]));
		tmp -= (int)(upper_bound(u.begin(),u.end(),v[i])-lower_bound(u.begin(),u.end(),v[i]));
		ans = max(ans,tmp);
	}
	cout << ans << endl;
	return 0;
}


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

using System.Collections;

class Program
{
    static void Main()
    {
        int[] nums = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
        int x1 = nums[0];
        int y1 = nums[1];
        int x2 = nums[2];
        int y2 = nums[3];

        int[] vector = new int[] { x2 - x1, y2 - y1 };


        //ベクトルを右に90度回転
        rot(vector);

        int x3 = x2 + vector[0];
        int y3 = y2 + vector[1];

        rot(vector);
        int x4 = x3 + vector[0];
        int y4 = y3 + vector[1];

        Console.WriteLine($"{x3} {y3} {x4} {y4}");
        
    }

    static void rot(int[] vector)
    {
        int tmp = vector[1];
        vector[1] = vector[0];
        vector[0] = -tmp;
    }
}

Measured memory_bytes: 13396
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

int main(void) {
	string s,t;
	cin >> s >> t;
	if(s.length()!=t.length()){
		printf("No\n");
		return 0;
	}
	int out=0;
	for(int i=0; i<s.length(); i++){
		int j,temp,num=s.length();
		out=0;
		for(j=0; j<s.length(); j++){
			temp=i+j;
			if(i+j>=s.length()){
				temp%=num;
			}
			if(s[j]!=t[temp]){
				out=1;
				break;
			}
		}
		if(out)continue;
		else{
			printf("Yes\n");
			return 0;
		}
	}
	printf("No\n");
	return 0;
}

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

using namespace std;

#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second

priority_queue< pii > Q;
int ans[100010];

int main() {

    int n, m;
    string s;
    cin >> n >> m;
    cin >> s;

    // cout << n << ' ' << m << ' ' << s << endl;

    fill(ans, ans + n + 1, -1);

    for(int i = n - 1, j = 1; i >= n - m; i--, j++){
        if(i < 0)break;
        if(s[i] == '1') continue;
        Q.push({-1,-i});
        ans[i] = j;
    }

    for(int i = n - m - 1; i >= 0; i--){
        if(s[i] == '1') continue;

        while(!Q.empty()){
            pii u = Q.top();

            // cout << i << ' ' << u.ff << ' ' << u.ss << endl;

            if(i + m < -u.ss){
                Q.pop();
                continue;
            }
            else {
                Q.push({u.ff-1, -i});
                ans[i] = -u.ss - i;
                break;
            }
        }
    }

    if(ans[0] == -1){
        cout << -1 << endl;
        return 0;
    }
    for(int i = 0;i < n;i += ans[i]){
        cout << ans[i] <<' ';
    }

    return 0;
}

Measured memory_bytes: 1912
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: usize,
        k: usize,
        x: [i64; n],
    } 
    let mut min = std::i64::MAX;
    for i in 0..(n as i64 - k as i64 + 1) as usize{
        let lr = i64::abs(x[i]) + i64::abs(x[i] - x[i + k - 1]);      
        if lr < min {
            min = lr;
        }
        let rl = i64::abs(x[(i + k - 1)]) + i64::abs(x[i] - x[i + k - 1]);      
        if rl < min {
            min = rl;
        }
    }
    println!("{}", min);
}

Measured memory_bytes: 6396
#ifdef DEBUG_IS_VALID
#define DEB 1 
#define _LIBCPP_DEBUG 0
#else
#define DEB 0
#define NDEBUG
#endif

#include <bits/stdc++.h>
using namespace std;

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back
#pragma GCC optimize ("-O3")

using namespace std;

#define DUMPOUT cout
#define dump(...) if(DEB) DUMPOUT<<"  "<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl<<"    "; if(DEB) dump_func(__VA_ARGS__)
template<typename T1,typename T2>ostream& operator << (ostream& os, pair<T1,T2> p){cout << "(" << p.first << ", " << p.second << ")"; return os;}
template<typename T>ostream& operator << (ostream& os, vector<T>& vec) { os << "{"; for (int i = 0; i<vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; }
template<typename T>ostream& operator << (ostream& os, set<T>& st){cout << "{"; for(auto itr = st.begin(); itr != st.end(); itr++) cout << *itr << (next(itr)!=st.end() ? ", " : ""); cout << "}"; return os;}
template<typename T1,typename T2>ostream& operator << (ostream& os, map<T1,T2> mp){cout << "{"; for(auto itr = mp.begin(); itr != mp.end(); itr++) cout << "(" << (itr->first) << ", " << (itr->second) << ")" << (next(itr)!=mp.end() ? "," : ""); cout << "}"; return os; }

void dump_func(){DUMPOUT << endl;}
template <class Head, class... Tail>void dump_func(Head&& head, Tail&&... tail){ DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...);}
template<class T> inline bool chmax(T& a,T const& b){if(a>=b) return false; a=b; return true;}
template<class T> inline bool chmin(T& a,T const& b){if(a<=b) return false; a=b; return true;}

using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

const int INF = 1<<29;
const long long LINF=1LL<<59;


void solve(long long N, long long K, string S){
  ll now = 0;
  vl s,flag;
  while(now<N){
    char fg = S[now];
    ll cnt = 0;
    while(now<N && S[now]==fg){
      cnt++; now++;
    }
    s.pb(cnt);
    flag.pb(ll(fg-'0'));
  }
  ll sz = s.size();
  vl ss(sz+1);
  
  rep(i,sz) ss[i+1] = ss[i] + s[i];
  ll ans = 0;
  rep(i,sz){
    if(flag[i]==0) chmax(ans,ss[min(i+2*K,sz)]-ss[i]);
    else chmax(ans,ss[min(i+2*K+1,sz)]-ss[i]);
  }
  cout << ans << endl;
}

int main(){
  long long N;
  scanf("%lld",&N);
  long long K;
  scanf("%lld",&K);
  string S;
  cin >> S ;
  solve(N, K, S);
  return 0;
}


Measured memory_bytes: 3052
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"

using namespace std;
using namespace __gnu_pbds;

#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
ll mod = 1e9 + 7;
ll fact(ll x) {
	ll ans = 1;
	for(ll i=1; i<=x; i++) {
		ans = (ans * i)%mod;
	}
	return ans;
}
ll power(ll x, ll y) {
	ll ans = 1;
	while(y) {
		if(y & 1) {
			ans = (ans * x)%mod;
		}
		x = (x*x)%mod;
		y/=2;
	}
	return ans;
}
int main(void)
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll x, y;
    cin>>x>>y;
    ll a, b;
    if((2*x - y)%3 || (2*x - y) < 0) {
    	cout<<0<<"\n";
    	return 0;
    }
    b = (2*x - y)/3;
    if((y - b)%2 || (y - b) < 0) {
    	cout<<0<<"\n";
    	return 0;
    }
    a = (y - b)/2;
    ll num1 = fact(a + b);
    ll num2 = fact(a);
    ll num3 = fact(b);
    ll de = (num2 * num3)%mod;
    de = power(de, mod - 2);
    num1 = (num1*de)%mod;
    cout<<num1<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
signed main(){
  int n;cin>>n;
  vector<int> c(n+1,0);
  map<int,int> memo;
  rep1(i,n){
      cin>>c[i];
  }
  int res=1;
  vector<int> dp(n+1,0);
  dp[0]=1;
  rep1(i,n){
      dp[i]=dp[i-1];
      auto it=memo.find(c[i]);
      if(it!=memo.end()&&it->second!=i-1){
          dp[i]+=dp[it->second];
      }
      else{
          ;
      }
      dp[i]%=MOD;
      memo[c[i]]=i;
  }
  rep1(i,n){
      //cout<<dp[i]<<" ";
  }
  //cout<<endl;
  cout<<dp[n]<<endl;
  return 0;
}

Measured memory_bytes: 15872
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
const int inf = 1e9 + 7;
const ll INF = 1e18;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
const int mod = 1000000007;
ll modpow(ll a, int b) { return b ? modpow(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) { rep(i, sz(vec)) { if (i) os << " "; os << vec[i]; } return os; }
template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.F << " " << p.S; return os; }
template<class T> inline void add(T &a, int b) { a += b; if (a >= mod) a -= mod; }



void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);

  int T;
  // cin >> T;
  T = 1;

  while (T--) {
      solve();
  }
}


void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v[26];
  rep(i, sz(s)) v[s[i] - 'a'].eb(i);

  int q;
  cin >> q;
  while (q--) {
    int key;
    cin >> key;
    // cout << key << endl;
    if (key == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;

      int it = lb(v[s[i] - 'a'], i);
      // cout << it << endl;
      // for (int j : v[s[i] - 'a']) cout << j << " "; cout << endl;
      v[s[i] - 'a'].erase(v[s[i] - 'a'].begin() + it);

      s[i] = c;
      it = lb(v[c - 'a'], i);
      v[c - 'a'].insert(v[c - 'a'].begin() + it, i);

      continue;
    }
    int l, r;
    cin >> l >> r;
    l--; r--;

    int cnt = 0;
    rep(i, 26) {
      int it = lb(v[i], l);
      if (it == sz(v[i])) continue;
      if (v[i][it] <= r) cnt++;
    }
    cout << cnt << endl;
  }

}


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

int main(){
    int k, n;
    cin >> k >> n;
    vector<int> houses(n);
    for (int i = 0; i < n; i++){
        cin >> houses.at(i);
    }
    sort(houses.begin(), houses.end());

    //最初の家と最後の家の距離
    int last_dis = k - houses.at(n - 1) + houses.at(0);
    int max_dis = last_dis;
    
    for (int i = 1; i < n; i++){
        max_dis = houses.at(i) - houses.at(i - 1) > max_dis ? houses.at(i) - houses.at(i - 1) : max_dis;
        /*
        if (houses.at(i) - houses.at(i - 1) > max_dis) {
            max_dis = houses.at(i) - houses.at(i - 1);
        }
        */
    }
    cout << k - max_dis << endl;
}

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

using ll = long long;
using ull = unsigned long long;
using st = string;
using ch = char;
using db = double;
using bl = bool;
using vll = vector<long long>;
using vdb = vector<db>;
using vvll = vector<vll>;
using vst = vector<st>;
using vch = vector<char>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
#define rep(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define vrep(i, vec) for(auto& i : vec)
#define vin(vec) for(auto& i : vec) cin >> i
#define all(v) v.begin(), v.end()
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;}
const ll mod = 1000000007;
const ll inf = 1LL << 60;
const db pi = acos(-1.0L);

int main(){
  ll n, d;
  cin >> n >> d;
  vvll x(n, vll(d));
  vrep(i, x)vrep(j, i) cin >> j;
  ll ans = 0;
  rep(i, 0, n) rep(j, 0, i){
    ll dis = 0;
    rep(k, 0, d) dis += (x.at(j).at(k) - x.at(i).at(k)) * (x.at(j).at(k) - x.at(i).at(k));
    bl check = false;
    rep(k, 0, 16000) if(k * k == dis) check = true;
    if(check) ans++;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  char a,b,c,d;
  cin>>a>>b>>c>>d;
  int x=(int)(a-'0'),y=(int)(b-'0'),z=(int)(c-'0'),w=(int)(d-'0');
  int sum=7-x;
  for(int i=0;i<2;i++){
    for(int j=0;j<2;j++){
      for(int k=0;k<2;k++){
        if((2*i-1)*y+(2*j-1)*z+(2*k-1)*w==sum){
          cout<<x<<((2*i-1)>0?'+':'-')<<y<<((2*j-1)>0?'+':'-')<<z<<((2*k-1)>0?'+':'-')<<w<<"=7"; return 0;}
      }
    }
  }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define N 10001
#define M 501
using namespace std;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
typedef long long ll;
string A,B,C;

ll calc(ll idx1,ll sameA,ll sameB){
  static int mem[N][2][2],used[N][2][2]={};
  if(idx1 == (int)A.size()) return 1;
  if(used[idx1][sameA][sameB]++) return mem[idx1][sameA][sameB];
  ll res = 0;
  for(ll i='0';i<='9';i++){
    if(sameA && i < A[idx1]) continue;
    if(sameB && i > B[idx1]) continue;
    ll nidx1 = idx1+1;
    ll nA = sameA & (i == A[idx1]);
    ll nB = sameB & (i == B[idx1]);      
    res = (res + calc(nidx1,nA,nB))%mod;
  }
  return mem[idx1][sameA][sameB] = res;
}

typedef pair<short,bool> Psb;
typedef pair<bool,bool> Pbb;
typedef pair<Psb,Pbb> PP;
typedef pair<short,PP> PPP;
map<PPP,int> mem;
ll dfs(ll idx1,ll idx2,ll sameA,ll sameB,ll ok){
  PP a = PP(Psb(idx2,sameA),Pbb(sameB,ok));
  PPP p = PPP(idx1,a);
  if(idx1 == (int)A.size()) return 0;
  if(mem.count(p)) return mem[p];
  
  ll res = 0;
  
  for(ll i='0';i<='9';i++){
    if(sameA && i < A[idx1]) continue;
    if(sameB && i > B[idx1]) continue;
    if(idx2 && C[idx2] != i) continue;
    ll nidx1 = idx1+1;
    ll nidx2 = C[idx2] == i? idx2+1:0;
    ll nA = sameA & (i == A[idx1]);
    ll nB = sameB & (i == B[idx1]);      
    ll nok = ok | (i != '0');
    if(nidx2 == (int)C.size()) res = (res + nok*calc(nidx1,nA,nB)) % mod;
    else res = (res + dfs(nidx1,nidx2,nA,nB,nok))%mod;
    if(nidx2 == 1) res = (res + dfs(nidx1,0,nA,nB,nok))%mod;
  }
  if(idx2 <= 1) mem[p] = res % mod;
  return res % mod;
}

signed main(){
  cin>>A>>B>>C;
  A = string(B.size()-A.size(),'0') + A;
  cout<<(dfs(0,0,1,1,0) + (A == string(A.size(),'0') && C == "0"))%mod<<endl;
  return 0;
}

Measured memory_bytes: 7240
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const ll INF=1LL<<55;
const int maxn=310;
int n,K,d[maxn],tot,h[maxn];
ll dp[maxn][maxn][maxn],ans;
int dy[maxn],tmp;
void update(ll &x,ll y) { x=min(x,y); }
int main() {
//	freopen("1.txt","r",stdin);
	read(n); read(K);
	for (int i=1;i<=n;i++) read(h[i]),d[i]=h[i];
	sort(d+1,d+n+1);
	tot=unique(d+1,d+n+1)-d-1;
	for (int i=1;i<=n;i++) dy[i]=lower_bound(d+1,d+tot+1,h[i])-d;
	for (int i=0;i<=n;i++)
	for (int k=0;k<=K;k++)
	for (int j=0;j<=tot;j++) dp[i][k][j]=INF;
	dp[0][K][0]=0;
	for (int i=1;i<=n;i++) {
		for (int k=0;k<=K;k++)
		for (int j=0;j<=tot;j++) {
			update(dp[i][k][dy[i]],dp[i-1][k][j]+max(0,h[i]-d[j]));
			if (k-(dy[i]!=j)>=0) update(dp[i][k-(dy[i]!=j)][j],dp[i-1][k][j]);
		}
	}
	ans=INF;
	for (int k=0;k<=K;k++)
	for (int j=0;j<=tot;j++) update(ans,dp[n][k][j]);
	printf("%lld\n",ans);
	return 0;
}
/*
  REMEMBER:
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?
    
  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?
    
  3. Do not make STUPID MISTAKES!
  Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/

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

b_a=0
_a=0
b_=0
ans=0

for i in range(N):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        b_a+=1
    elif s[0]=='B':
        b_+=1
    elif s[-1]=='A':
        _a+=1

    for j in range(len(s)-1):
        if s[j]=='A'and s[j+1]=='B':
            ans+=1

if b_a>0:
    ans+=b_a-1
    if _a==0 and b_==0:
        print(ans)
    elif _a==1 and b_==0:
        print(ans+1)
    elif _a==1 and b_==0:
        print(ans+1)
    elif _a==1 and b_==1:
        print(ans+2)
    else:
        print(ans+2+min(_a-1,b_-1))
else:
    print(ans+min(_a,b_))

Measured memory_bytes: 3188
#include<stdio.h>
int main(){
  int n,i;
  scanf("%d",&n);
  char s[n],t[n];
  scanf("%s%s",s,t);
  for(i=0;i<n;i++)
    {
    printf("%c%c",s[i],t[i]);
    }
  return 0;
 }
  


Measured memory_bytes: 128
import java.util.Scanner;
public class Main{

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		double n = sc.nextDouble();
		double k = sc.nextDouble();
		
		double E = 0;
		double q;
		for(double p = 1; p <= n; p++) {
			double Ep = 1/n;
			q = p;
			if(p >= k) {
				E += Ep;
			}
			if(p < k) {
				do{
					q = q * 2;
					Ep = Ep / 2;
				}while(q < k);
				
				E += Ep;
			}
		}
		System.out.println(E);

	}

}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < (NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()

#define cINF ((1ull<<62)-1)
#define cINFINT ((1<<30)-1)

template< typename T >
class Matrix
{
public:
	explicit Matrix( size_t row_size, size_t col_size )
	{
		mElement.assign( row_size, vector<T>() );
		REP( i, row_size ) mElement[i].assign( col_size, T(0) );
	}

	const vector<T>& operator[] ( size_t row ) const
	{ return mElement[row]; }
	vector<T>& operator[] ( size_t row )
	{ return mElement[row]; }

	const size_t rowSize() const
	{ return mElement.size(); }
	const size_t colSize() const
	{ return mElement[0].size(); }

protected:
	vector<vector<T>> mElement;
};

template< typename T >
class SquareMatrix : public Matrix<T>
{
public:
	explicit SquareMatrix( size_t size )
		: Matrix<T>( size, size )
	{}

	void identity()
	{
		REP( i, this->rowSize() ) REP( j, this->colSize() )
			(*this)[i][j] = i == j ? T(1) : T(0);
	}

	SquareMatrix mul ( const SquareMatrix& rhs, ll mod ) const
	{
		size_t size = min( this->colSize(), rhs.colSize() );
		SquareMatrix<T> ret( size );
		REP( i, size )
		REP( k, size )
		REP( j, size )
		{
			ret[i][j] = (ret[i][j] + (*this)[i][k] * rhs[k][j] % mod)%mod;
		}
		return ret;
	}

	SquareMatrix<T> pow ( size_t n, ll mod ) const
	{
		SquareMatrix base = *this;
		SquareMatrix ret( this->rowSize() );
		ret.identity();
		while( n > 0 )
		{
			if( n & 1 ) ret = ret.mul(base, mod);
			base = base.mul( base , mod );
			n >>= 1;
		}
		return ret;
	}
};

int main()
{
    ll pow10[19];
    pow10[0] = 1; REP(i,19)if(i!=0) pow10[i] = pow10[i-1]*10;
    
    ll l,a,b,mod; cin>>l>>a>>b>>mod;
    auto add = [&mod]    ( ll a, ll b ) { return ( a + b ) % mod; };
    auto mul = [&mod]    ( ll a, ll b ) { return a * b % mod; };

    auto solve = [&mod,&a,&b,&pow10,add,mul]
    (ll& ans, ll start, ll end, ll d)
    {
        ll n = (end-start)/b+1;
        SquareMatrix<ll> mtx(3);
        mtx[0][0] = pow10[d]%mod; mtx[0][1] = 1; mtx[0][2] = 0;
        mtx[1][0] = 0; mtx[1][1] = 1; mtx[1][2] = b%mod;
        mtx[2][0] = 0; mtx[2][1] = 0; mtx[2][2] = 1;
        mtx = mtx.pow(n,mod);
        ans = add( mul( ans, mtx[0][0] ), mul( start%mod, mtx[0][1] ) );
        ans = add( ans, mtx[0][2] );
    };

    ll start = a;
    ll end = a + b * ( l - 1 );
    ll ans = 0;
    REP(i,19) if(i!=0) if( start < pow10[i] )
    {
        ll ed = (pow10[i]-1-start)/b*b+start;
        if(ed>end) ed = end;
        solve( ans, start, ed, i );
        if(ed==end) break;
        start = ed + b;
    }

    cout << ans << endl;

    return 0;
}

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

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define REP(i,n) FOR(i,0,n)
typedef long long ll;
using ll = long long;
using vi = vector<int>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

int D;
ll G;
int p[11];
int c[11];
int ans = 100000000;

void dfs(int maxIdx, ll leftScore, int i, int solved) {
    if(i > D) {
        if(leftScore == 0) ans = min(ans, solved);
        else if(leftScore < p[maxIdx] * maxIdx * 100) {
            if(leftScore > 0) solved += (leftScore / 100 + maxIdx - 1) / maxIdx;    // (a+b+1)/b に修正．
            ans = min(ans, solved);
        }
        return;
    }
    // 解かない
    dfs(i, leftScore, i + 1, solved);
    // 解く
    dfs(maxIdx, leftScore - i * 100 * p[i] - c[i], i + 1, solved + p[i]);
}

int main()
{
    ios::sync_with_stdio(false);

    cin >> D >> G;
    REP(i, D) {
        cin >> p[i+1] >> c[i+1];
    }

    dfs(0, G, 1, 0);

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256