akhauriyash/Code-Regression huggingface dataset:
n,a,b,c=map(int,input().split())
l=[]
for i in range(0,n):
    s=input()
    l.append(s)
final=[]
for i in range(0,len(l)):
    if l[i]=='AB':
        if a>b:
            b+=1
            a-=1
            final.append('B')
        elif a<b:
            b-=1
            a+=1
            final.append('A')
        else:
            if a==0:
                print('No')
                break
            else:
                if i!=len(l)-1:
                    if l[i+1]=='AB':
                        a+=1
                        b-=1
                        final.append('A')
                    elif l[i+1]=='AC':
                        a+=1
                        b-=1
                        final.append('A')
                    else:
                        a-=1
                        b+=1
                        final.append('B')
                else:
                    a-=1
                    b+=1
                    final.append('B')
    elif l[i]=='AC':
        if a>c:
            c+=1
            a-=1
            final.append('C')
        elif a<c:
            c-=1
            a+=1
            final.append('A')
        else:
            if a==0:
                print('No')
                break
            else:
                if i!=len(l)-1:
                    if l[i+1]=='AB':
                        a+=1
                        c-=1
                        final.append('A')
                    elif l[i+1]=='AC':
                        a+=1
                        c-=1
                        final.append('A')
                    else:
                        a-=1
                        c+=1
                        final.append('C')
                else:
                    a-=1
                    c+=1
                    final.append('C')
    else:
        if b>c:
            c+=1
            b-=1
            final.append('C')
        elif b<c:
            c-=1
            b+=1
            final.append('B')
        else:
            if b==0:
                print('No')
                break
            else:
                if i!=len(l)-1:
                    if l[i+1]=='AB':
                        b+=1
                        c-=1
                        final.append('B')
                    elif l[i+1]=='BC':
                        b+=1
                        c-=1
                        final.append('B')
                    else:
                        b-=1
                        c+=1
                        final.append('C')
                else:
                    b-=1
                    c+=1
                    final.append('C')
else:
    print('Yes')
    for i in range(0,len(final)):
        print(final[i])


Measured memory_bytes: 17124
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            new Main().solve(sc);
        }
    }

    void solve(Scanner sc) {
        int n = sc.nextInt();
        int q = sc.nextInt();
        char[] s = sc.next().toCharArray();
        int[] acnum = new int[n + 1];
        acnum[0] = acnum[1] = 0;
        for (int i = 2; i <= n; i++) {
            if (s[i - 2] == 'A' && s[i - 1] == 'C') {
                acnum[i] = acnum[i - 1] + 1;
            } else {
                acnum[i] = acnum[i - 1];
            }
        }

        for (int i = 0; i < q; i++) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            System.out.println(acnum[r] - acnum[l]);
        }
    }
}


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

using namespace std;

typedef long long ll;

const double pi = 3.14159265358979323846;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define MOD 1000000007LL
#define PS(p) cout << setprecision(15) << fixed << p << endl;
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
#define ALL(c) c.begin(), c.end()
#define CIN(v) cin >> v;
#define COUT(v) cout << v << "\n";

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

  string S, T;
  CIN(S >> T);
  cout << T << S << endl;

  return 0;
}


Measured memory_bytes: 256
//
// Created by koshikawa on 2020/09/19.
//

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
typedef map<string, int> msi;
typedef pair<int, int> PII;

int n, m, q;
vector<int> a, b, c, d;
int ans;

void dfs(vector<int> A){
    if(A.size() == n+1){
        int now = 0;
        rep(i, q){
            if(A[b[i]] - A[a[i]] == c[i]) now += d[i];
        }
        ans = max(ans, now);
        return;
    }
    A.push_back(A.back());
    while(A.back()<= m){
        dfs(A);
        A.back()++;
    }
}

int main() {
    cin >> n >> m >> q;
    a = b = c = d = vector<int>(q);
    rep(i, q){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    dfs(vector<int>(1,1));
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3596
import java.util.*;
import java.math.*;
import java.lang.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力
		int n = sc.nextInt();
		int t = sc.nextInt();
		int minCost = 1001;
		boolean flag = false;

		for(int i=0;i<n;i++){
			int c = sc.nextInt();
			int tx = sc.nextInt();
			if(t>=tx){
				flag = true;
				minCost = Math.min(c,minCost);
			}
		}
			
		if(!flag){

			System.out.println("TLE");
		}else{
			
		System.out.println(minCost);
		}
	}
}

Measured memory_bytes: 23252
#include<bits/stdc++.h>
#define ALL(c) (c).begin(),(c).end()
#define EACH(i,c) for(auto i=(c).begin();i!=(c).end();++i)
using namespace std;
#define int long long

int f(int b, int n) {
	int res = 0;
	while(n > 0) {
		res += n % b;
		n /= b;
	}
	return res;
}

signed main(){
	int n, s;
	cin >> n >> s;
	if(s == n) {
		cout << n + 1 << endl;
		return 0;
	}
	if(s > (n + 1) / 2) {
		cout << -1 << endl;
		return 0;
	}
	int res = n;
	for(int i = 2; i * i <= n; ++i) {
		if(f(i, n) == s) {
			res = min(res, i);
		}
	}
	for(int i = 1; i * i <= n; ++i) {
		int l = n / (i + 1) + 1;
		int m = n / i;
		if(n / l !=  n / m) continue;
		int c = f(l, n);
		int d = f(m, n);
		if(s < d || c < s) continue;
		if((c - s) % i == 0) {
			int t = (c - s) / i + l;
			res = min(res, t);
		}
	}
	cout << res << endl;
	//cout << f(res, n) << endl;
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<complex>

#define EPS 1e-3

using namespace std;

typedef complex<double> P;

double cross(P v1,P v2){
	return v1.real()*v2.imag()-v1.imag()*v2.real();
}

bool che(P v1,P v2,P v3,P v){
	if(cross(v2-v1,v-v1)<-EPS&&cross(v3-v2,v-v2)<-EPS&&cross(v1-v3,v-v3)<-EPS)return true;
	else if(cross(v2-v1,v-v1)>EPS&&cross(v3-v2,v-v2)>EPS&&cross(v1-v3,v-v3)>EPS)return true;
	return false;
}

int main(){
	double x1,y1,x2,y2,x3,y3,xP,yP;
	while(cin>>x1>>y1>>x2>>y2>>x3>>y3>>xP>>yP){
		P v=P(xP,yP);
		P v1=P(x1,y1);
		P v2=P(x2,y2);
		P v3=P(x3,y3);
		
		if(che(v1,v2,v3,v))cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
		
		
	}
}

Measured memory_bytes: 1216
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#define pb push_back
using namespace std;
const int N=1e5+5;

string s;

string nox;
string half1,half2;

vector <int> nox_ind;

int last1,first2=-1;

int abs(int x)
{
    if(x < 0)
        return -x;

    return x;
}

vector <int> cnt_x1,cnt_x2;

int ans;

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

   int i;

   cin>>s;
   for(i=0;i<s.size();i++)
   {
       if(s[i] != 'x')
       {
        nox.pb(s[i]);

        nox_ind.pb(i);
       }
   }

   if(nox.empty())
   {
       cout<<0<<endl;

       return 0;
   }

   for(i=0;i<nox.size()/2;i++)
   {
    half1.pb(nox[i]);

    last1=nox_ind[i];
   }

   for(i=nox.size()/2;i<nox.size();i++)
   {
       if(nox.size()%2==1 && i == nox.size()/2)
       {
           first2=nox_ind[i];

           continue;
       }

       half2.pb(nox[i]);

       if(first2 == -1)
        first2=nox_ind[i];
   }

   reverse(half2.begin(),half2.end());

   if(half1 != half2)
   {
       cout<<-1<<endl;

       return 0;
   }

   int cnt;
   if(nox.size()%2 == 1) /// we have to look only at first2
   {
       cnt=0;
       for(i=first2-1;i>=0;i--)
       {
           if(s[i] == 'x')
            cnt++;
           else
           {
               cnt_x1.pb(cnt);

               cnt=0;
           }
       }
       cnt_x1.pb(cnt);

       cnt=0;
       for(i=first2+1;i<s.size();i++)
       {
           if(s[i] == 'x')
            cnt++;
           else
           {
               cnt_x2.pb(cnt);

               cnt=0;
           }
       }

       cnt_x2.pb(cnt);
   }
   else
   {
       cnt=0;
       for(i=last1-1;i>=0;i--)
       {
           if(s[i] == 'x')
            cnt++;
           else
           {
               cnt_x1.pb(cnt);

               cnt=0;
           }
       }
       cnt_x1.pb(cnt);

       cnt=0;
       for(i=first2+1;i<s.size();i++)
       {
           if(s[i] == 'x')
            cnt++;
           else
           {
               cnt_x2.pb(cnt);

               cnt=0;
           }
       }

       cnt_x2.pb(cnt);
   }

   for(i=0;i<cnt_x1.size();i++)
    ans+=abs(cnt_x1[i] - cnt_x2[i]);

   cout<<ans<<endl;


   return 0;
}

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

int main(){
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int t1, t2;
    t1 = max(a, c);
    t2 = min(b, d);
    if(t2 - t1 < 0){
        cout << '0' << endl;
        return 0;
    }
    cout << t2 - t1 << endl;
    return 0;
}

Measured memory_bytes: 256
# coding: utf-8


def greatest_common_divisor(bigger, smaller):
    
    if bigger < smaller:
        bigger, smaller = smaller, bigger
    while bigger % smaller != 0:
        bigger, smaller = smaller, bigger % smaller
    
    return smaller
    

def main():
    
    a, b = map(int, input().split(' '))
    least_common_multiple = a * b // greatest_common_divisor(a, b)
    print(least_common_multiple)
    
    return 0
    
    
if __name__ == '__main__':
    main()


Measured memory_bytes: 2940
/*
    飲んだ魔剤で家が建つ。
    created at: 2020-04-12 18:01:19
*/

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

#define all(x) (x).begin(),(x).end()
#define rev(x) greater<x>()
#define MOD 1000000007
#define INF 1000000000

typedef long long LL;
typedef long double LD;

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

LL modpower(LL n, LL m, LL mod)
{
    if (m == 0) {
        return 1;
    } else if (m % 2 == 0) {
        LL p = modpower(n, m/2, mod);
        return p*p % mod;
    } else {
        return n*modpower(n, m-1, mod) % mod;
    }
}

LL modinv(LL n, LL mod)
{
    return modpower(n, mod-2, mod);
}

LL modcombination(LL n, LL r, LL mod)
{
    LL ret = 1;

    for (int i = 0; i < r; i++) {
        ret *= (n-i);
        ret %= mod;
        ret *= modinv(i+1, mod);
        ret % mod;
    }

    return ret % mod;
}

int gcd(int k, int l)
{
    if (l > 0) return gcd(l, k%l);
    else return k;
}

struct UnionFind
{
    vector<int> par;

    UnionFind(int n) : par(n) {
        for (int i = 0; i < n; i++) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        par[rx] = ry;
    }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> x(n);
    for (int i = 0; i < n; i++)
        cin >> x[i];

    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += 2*min(x[i], k-x[i]);
    }

    cout << ans << endl;

    return 0;
}


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

int N;
double A[110];
int main(){
	cin >> N;
	double sum = 0;
	for(int i=0;i<N;i++){
		cin >> A[i];
		sum += A[i];
	}
	sum /= N;
	int ans = -1;
	double l = 1e9;
	for(int i=0;i<N;i++){
		if(abs(sum-A[i])<l){
			ans = i;
			l = abs(sum-A[i]);
		}
	}
	cout << ans << endl;
}

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

int main(){
    int H, W, N, sr, sc;
    cin >> H >> W >> N;
    cin >> sr >> sc;
    string S, T;
    cin >> S;
    cin >> T;
    bool awin = false;
    int bwinl = 1;
    int bwinr = W;
    int bwinu = 1;
    int bwind = H;
    for (int i = N-1; i >= 0; i--) {
        if (T[i] == 'R') bwinl = max(bwinl-1, 1);
        if (T[i] == 'L') bwinr = min(bwinr+1, W);
        if (T[i] == 'U') bwind = min(bwind+1, H);
        if (T[i] == 'D') bwinu = max(bwinu-1, 1);
        if (S[i] == 'R') bwinr--;
        if (S[i] == 'L') bwinl++;
        if (S[i] == 'U') bwinu++;
        if (S[i] == 'D') bwind--;
        if (bwinr < bwinl) awin = true;
        if (bwind < bwinu) awin = true;
    }
    if (!awin && bwinl <= sc && sc <= bwinr && bwinu <= sr && sr <= bwind) cout << "YES" << endl;
    else cout << "NO" << endl;
}

Measured memory_bytes: 776
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし

N = I()
S = LI2()
ans = 0
for i in range(1000):
    x = str(i).zfill(3)
    X = [int(x[0]),int(x[1]),int(x[2])]
    j = 0
    k = 0
    while j < N:
        if S[j] == X[k]:
            if k == 2:
                ans += 1
                break
            else:
                k += 1
        j += 1

print(ans)


Measured memory_bytes: 75104
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <set>
#include <iomanip>
#include <stdio.h>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
int main(void){
  long double n,t,a;
  cin>>n>>t>>a;
  vector<long long int> h(n);
  for(long long int i=0; i<n; i++){
    cin>>h.at(i);
  }
  long long int ans;
  long double now=99999999999;
  for(long long int i=0; i<n; i++){
    if(abs(a-(t-h.at(i)*0.006))<now){
      ans=i;
      now=abs(a-(t-h.at(i)*0.006));
    }
  }
  cout<<ans+1<<endl;
}

Measured memory_bytes: 3732
def v():
 for e in iter(input,'0 0'):
  r=int(e.split()[0])
  d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
  a=0
  for m in range(1<<~-r):
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=max(c,r-c)
   if a<t:a=t
  print(a)
if'__main__'==__name__:v()


Measured memory_bytes: 6916
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;
int a[4],n,ans,k;
int main(){
	for(int i=1;i<=3;i++) scanf("%d",&a[i]);
	scanf("%d",&k);
	sort(a+1,a+4);
	for(int i=1;i<=k;i++){
    	a[3]=a[3]*2;
	}
	for(int i=1;i<=3;i++)
	ans+=a[i];
	printf("%d\n",ans);
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
typedef long long ll;
ll MOD = 1e9+7;

using namespace std;

int main()
{
	ll n,k;
	ll loop;
	ll a[50];
	
	cin>>k;
	n=50;
	fill(a,a+n,49);
	loop = k/n;
	for(int i=0;i<n;i++)
	{
		a[i]+=loop;
	}

	loop = k%n;

	for(int i=0;i<loop;i++)
	{
		a[i]+=n;
		for(int j=0;j<n;j++)
		{
			if(j==i)
				continue;
			a[j]--;
		}
	}
	cout<<n<<endl;
	for(int i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}

Measured memory_bytes: 256
# include <cstdio>
# include <cmath> 
# include <algorithm>
# define ll long long

using namespace std;

int main()
{
	int n;
	ll a[150];
	bool flag = false;
	ll l = 999,r =0;
	
	scanf("%d",&n);
	
	for(int i = 0;i < n;i++)
	{
		scanf("%lld",&a[i]);
//		sum += a[i];
		l = min(l,a[i]);
		r = max(r,a[i]);
		if(i != 0 && a[i] != a[i-1])
		{
			flag = true;
		}	
	}
//	printf("%d %d",l,r);
	if(flag)
	{
		ll ans = 0,tem = 999999999;
		for(int y = l;y <= r;y++){
			ans = 0;
			for(int i = 0;i < n;i++)
			{
			
				ans += (a[i] - y) * (a[i] - y);
			}
			tem = min(ans,tem);
//				printf("ans = %d\n",ans);
		}
		
		printf("%lld\n",tem);
	}else{
		printf("0\n");
	}
	
	
	return 0;
}

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

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

    int n;
    cin >> n;
    vector<int> A;
    A.push_back(0);
    REP(i, n){
        int a;
        cin >> a;
        A.push_back(a);
    }
    A.push_back(0);

    int sum = 0;
    for(int i = 1; i < A.size(); i++){
        sum += abs(A[i] - A[i - 1]);
    }

    for(int i = 1; i < A.size() - 1; i++){
        int diff = 0;
        if(A[i - 1] <= A[i] and A[i] <= A[i + 1]);
        else if(A[i - 1] >= A[i] and A[i] >= A[i + 1]);
        else{
            diff = abs(abs(A[i + 1] - A[i]) + abs(A[i] - A[i - 1]) - abs(A[i + 1] - A[i - 1]));
        }
        cout << sum - diff << endl;
    }

    return 0;
}


Measured memory_bytes: 1784
import java.util.*;

public class Main {
    static int[] A;
    static int N;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        A = new int[N];
        for(int i = 0; i < N;i ++){
            A[i] = sc.nextInt();
        }
        int b = goldSectionSearchDeko((int)-1e9 - 9, (int)1e9+9);
        long res = solve(b);
        System.out.println(res);
    }
    
    public static long solve(int b)
    {
        long res = 0;
        for (int i = 0; i < N; i++)
        {
            res += (Math.abs(A[i] - b - i - 1));
        }
        return res;
    }
    
    public static int goldSectionSearchDeko(int left,int right){
        double GRATIO=1.6180339887498948482045868343656;
        int lm = left + (int)((right-left)/(GRATIO+1.0));
        int rm = lm + (int)((right-lm )/(GRATIO+1.0));
        long fl = solve(lm);
        long fr = solve(rm);
        while(right - left > 10){
            if(fl < fr){
                right = rm;
                rm = lm;
                fr = fl;
                lm = left + (int)((right-left)/(GRATIO+1.0));
                fl = solve(lm);
            }else{
                left = lm;
                lm = rm;
                fl = fr;
                rm = lm + (int)((right-lm )/(GRATIO+1.0));
                fr = solve(rm);
            }
        }
        long minScore = Long.MAX_VALUE;
        int resIndex = left;
        for(int i = left ; i < right + 1 ; i++){
            long score = solve(i);
            if(minScore > score){
                minScore = score;
                resIndex = i;
            }
        }
        return resIndex;
    }
}


Measured memory_bytes: 65772
#include<stdio.h>
int main(){
  int a,b;
  scanf("%d%d",&a,&b);
  if(a>b){
    printf("a > b\n");
  }
      if(a<b){
        printf("a < b\n");
      }
      if(a==b){
        printf("a == b\n");
      }
  return 0;
}

Measured memory_bytes: 2508
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileio freopen("in.in", "r", stdin),freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define pss printf
#define MOD 1000000007
#define INF 1e18
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout<<n1<<endl
const int N = 1e5;  // limit for array size
ll n;  // array size
 
//  Utility method to convert lleger llo string
string toString(ll n)
{
    string str;
    stringstream ss;
    ss << n;
    ss >> str;
    return str;
}
 
// Method to convert n to base negBase
string toNegativeBase(ll n, ll negBase)
{
    //  If n is zero then in any base it will be 0 only
    if (n == 0)
        return "0";
 
    string converted = "";
    while (n != 0)
    {
        // Get remainder by negative base, it can be
        // negative also
        ll remainder = n % negBase;
        n /= negBase;
 
        // if remainder is negative, add abs(base) to
        // it and add 1 to n
        if (remainder < 0)
        {
            remainder += (-negBase);
            n += 1;
        }
 
        // convert remainder to string add llo the result
        converted = toString(remainder) + converted;
    }
 
    return converted;
}
 
//  Driver code to test above methods
int main()
{
    ll n;cin>>n;
    ll negBase = -2;
 
    cout << toNegativeBase(n, negBase);
 
    return 0;
}

Measured memory_bytes: 256
def cmb(n, r, p):
  if (r < 0) or (n < r):
    return 0
  r = min(r, n - r)
  return fac[n] * finv[r] * finv[n-r] % p

N = pow(10,6)
MOD = pow(10,9)+7

fac = [-1]*(N+1); fac[0] = 1; fac[1] = 1
finv = [-1]*(N+1); finv[0] = 1; finv[1] = 1
inv = [-1]*(N+1); inv[0] = 0; inv[1] = 1
for i in range(2,N+1):
  fac[i] = fac[i-1]*i%MOD
  inv[i] = MOD - inv[MOD%i]*(MOD//i)%MOD
  finv[i] = finv[i-1]*inv[i]%MOD


X, Y = map(int,input().split())
if (X+Y)%3 != 0:
  print("0")
  exit(0)

M = (X+Y)//3 #M回動く必要がある。
D = abs(X-Y) #D回差がある。
A = (M-D)//2
#print(M,D,A)
ans = cmb(M,A,MOD)
print(ans)




Measured memory_bytes: 121764
#include <string>
#include <iostream>
#include <stack>
#include <queue> // priority_queue も入ってる
#include <set>   // 要素数は size() で取得 multisetもある
#include <map>
#include <array> // sizeはコンパイル時定数で固定
#include <vector>
#include <numeric>   // accumulate, gcd
#include <algorithm> // count_ifに必要
#include <iomanip>   // cout << setprecision(15) << x で小数の出力精度を指定
#include <tuple>
#include <utility> //pair

using namespace std;
// 問題が起こるまでintは使用禁止2020-04-13
using ll = long long;

template <class InputIterator, class T>
bool contains(InputIterator iterator, const T &value)
{
    if (find(iterator.begin(), iterator.end(), value) == iterator.end())
        return false;
    else
        return true;
}

template <class InputIterator>
auto sum(InputIterator iterator)
{
    return accumulate(iterator.begin(), iterator.end(), 0);
}

// endは排他的(exclusive)なので注意
vector<ll> range(ll begin, ll end)
{
    vector<ll> result;
    for (ll i = begin; i < end; ++i)
    {
        result.push_back(i);
    }
    return result;
}

// =========ここまでテンプレ============

int main()
{
    ll n;
    cin >> n;
    map<ll, ll> sums;
    ll count = 0;
    for (ll i = 1; i <= n; ++i)
    {
        ll a;
        cin >> a;
        count += sums[i - a];
        sums[a + i]++;
    }
    cout << count << endl;
}

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

#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(a) a.begin(), a.end()

const int mod = 1e9+7;
const int inf = 2e18;

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

	string s;
	string t;
	cin>>s;
	cin>>t;
	int cnt = 0;
	for(int i=0;i<s.size();i++)
	{
		cnt+=(s[i]!=t[i]);
	}
	cout<<cnt<<'\n';
	return 0;
}	

Measured memory_bytes: 3808
import java.util.*;


public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int g[][] = new int[114][114];
		for (int i=0; i<114; i++) for (int j=0; j<114; j++) {
			if (i==j) g[i][j] = 0;
			else g[i][j] = 123456789;
		}
		
		for (int i=0; i<n; i++) {
			int v = scan.nextInt();
			int k = scan.nextInt();
			for (int j=0; j<k; j++) {
				int to = scan.nextInt(); int cost = scan.nextInt();
				g[v][to] = cost;
			}
		}
		
		for (int k=0; k<n; k++) {
			for (int i=0; i<n; i++) {
				for (int j=0; j<n; j++) {
					g[i][j] = Math.min(g[i][j], g[i][k]+g[k][j]);
				}
			}
		}
		
		int ans = 0;
		for (int i=0; i<n; i++) {
			System.out.println(i+" "+g[0][i]);
		}
	}
}




Measured memory_bytes: 40348
from collections import defaultdict
iim = lambda: map(int, input().rstrip().split())
 
def resolve():
    N = int(input())
    A = list(iim())
    ans = 0
    dp = [0]*N
    for i, ai in enumerate(A):
        x = i + ai
        if x < N:
            dp[x] += 1
        x = i - ai
        if x >= 0:
            ans += dp[x]
    print(ans)
resolve()

Measured memory_bytes: 33904
#include<iostream>
using namespace std;
int main(){
    int a,b,c;
    char d,e;
    cin >> a >> d >> b >> e >> c;
    if(a>=2019 && b>=5){
        cout << "TBD" << endl;
    }else if(a==2019 && b==4 && c==30){
        cout << "Heisei" << endl;
    }else{
        cout << "Heisei" << endl;
    }
}

Measured memory_bytes: 256
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]


if __name__ == "__main__":
    N, M = map(int, input().split()) # N人、M個の関係
    union_find = UnionFind(N)
    for _ in range(M):
        A, B = map(int, input().split())
        A -= 1
        B -= 1
        union_find.union(A, B)

    answer = 0
    for i in range(N):
        answer = max(answer, union_find.size(i))

    print(answer)

Measured memory_bytes: 14684
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#define N 2100
using namespace std;
int n,m,q,a[N][N],s[N][N],s1[N][N],s2[N][N],cnt,h[N][N];
char st[N];
int main()
{
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",st+1);
		for(int j=1;j<=m;j++) 
		{
			a[i][j]=st[j]-'0';
			s[i][j]=a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
			h[i][j]=h[i-1][j]+h[i][j-1]-h[i-1][j-1];
			if(a[i][j] && a[i-1][j]) h[i][j]++;
			if(a[i][j] && a[i][j-1]) h[i][j]++;
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++) if(a[i][j] && a[i-1][j]) s1[i][j]=1,cnt++;
		for(int j=1;j<=m;j++) s1[i][j]+=s1[i][j-1];
	}
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++) if(a[j][i] && a[j][i-1]) s2[i][j]=1,cnt++;
		for(int j=1;j<=n;j++) s2[i][j]+=s2[i][j-1];
	}
	while(q--)
	{
		int x1,y1,x2,y2,v,e;scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		v=s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1];
		e=h[x2][y2]-h[x1-1][y2]-h[x2][y1-1]+h[x1-1][y1-1];
		e-=s1[x1][y2]-s1[x1][y1-1]+s2[y1][x2]-s2[y1][x1-1];
		printf("%d\n",v-e);
	}
	return 0;
}

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

int bubblesort(int a[], int n) {
        int sw = 0;
        int flag =  1;          //????????£??\????´?????????¨??????
        while(flag) {
                flag = 0;
                for(int j = n - 1; j > 0; j--) {
                        if(a[j] < a[j - 1]) {
                                swap(a[j], a[j - 1]);
                                flag = 1;
                                sw++;
                        }
                }
        }
        return sw;
}

int main() {
        int N;
        cin >> N;
        int input[N];
        for(int i = 0; i < N; i++) {
                cin >> input[i];
        }

        int sw = bubblesort(input, N);
        for(int i = 0; i < N; i++) {
                if(i) cout << " ";
                cout << input[i];
        }
        cout << endl;
        cout << sw << endl;

        return 0;
}

Measured memory_bytes: 3092
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ins insert
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define up_b upper_bound
#define low_b lower_bound
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=b;i>=a;i--)
#define boost ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL)
#define nl '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;

const int N=1501;
const int MXN=1501;

int main(){
  int n;
  cin>>n;
  string s;
  cin>>s;
  if(n%2)cout<<"No";
  else{
    string t="";
    for(int i=0;i<n/2;i++){
      t+=s[i];
    }
    string q="";
    for(int i=n/2;i<n;i++){
      q+=s[i];
    }
    if(t==q)cout<<"Yes";
    else cout<<"No";
  }
}


Measured memory_bytes: 256
/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

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

#define DEBUG
#ifndef DEBUG
#define endl "\n"
#define log(args...)
#else
#define log(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); logf(_it, args); }
void logf(istream_iterator<string> it) {
}
template<typename T, typename ... Args>
void logf(istream_iterator<string> it, T a, Args ... args) {
	cout << *it << " = " << a << endl;
	logf(++it, args...);
}
#endif

#define forn(i,k,n) for(ll i=(k); i<(n); i++)

const double PI = acos(-1);
typedef long long ll;
#define fori(n) for(ll i=0; i<(n); i++)

#define cins(s) string s; cin>>s;
#define cini(i) int i; cin>>i;
#define cinll(l) ll l; cin>>l;
#define cind(d) double d; cin>>d;
#define cinai(a, n) vi a(n); fori(n) { cin>>a[i]; }
#define cinall(a, n) vll a(n); fori(n) { cin>>a[i]; }
#define cinas(s, n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a, n) vd a(n); fori(n) { cin>>a[i]; }

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;
typedef vector<ll> vll;

void solve() {
	cins(s);
	cins(t);
	cini(a);
	cini(b);
	cins(u);
	if (s == u)
		a--;
	else
		b--;
	cout << a << " " << b << endl;
}

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



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

int main() {
    string s, t;
    cin >> s >> t;
    int count = 0;
    rep(i, 3) {
        if(s[i] == t[i]) count++;
    }
    cout << count << endl;
}


Measured memory_bytes: 256
n, m = map(int, input().split())
c = set()
d = set()
for i in range(m):
    a, b = map(int, input().split())
    if a == 1:
        c.add(b)
    if b == n:
        d.add(a)
ans = "POSSIBLE" if len(c & d) else "IMPOSSIBLE"
print(ans)

Measured memory_bytes: 19024
#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> I(n);
    iota(I.begin(), I.end(), 0);
    sort(I.begin(), I.end(), [&](int i, int j) { return a[i] < a[j]; });
    ll ans = 0;
    for(int i : I) {
        ans += ll(min(m, b[i])) * a[i];
        m = max(0, m - b[i]);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1408
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n = in.nextInt();
        int[] p = new int[n];
        int result = 0;
        int tempX = 0;
        for (int i = 0; i < n; i++) {
            p[i] = in.nextInt();
            tempX += 2 * p[i];
            result += p[i] * p[i];
        }
        int min = Integer.MAX_VALUE;

        for (int i = 1; i <= 100; i++) {
            min = Math.min(min, n * i * i - tempX * i);
        }
        System.out.println(result + min);
    }
}


Measured memory_bytes: 23636
N = int(input())
people = [[] for _ in range(N)]
for i in range(N):
    a = int(input())
    for _ in range(a):
        x, y = map(int, input().split())
        x -= 1
        people[i].append([x, y])
ans = 0  # だんだん更新されていく
for mask in range(1 << N):  # 2のd乗になる
    binary_status = []

    for i in range(N):
        binary_status.append((mask >> i) & 1)
    # 矛盾なく考えていきたい
    flag = True
    for i in range(len(binary_status)):
        if binary_status[i] == 0:
            continue
        for j in range(len(people[i])):
            # print("hoge")
            # print(people[i][j])
            # print(binary_status)

            if binary_status[people[i][j][0]] != people[i][j][1]:
                flag = False
    if flag:
        if sum(binary_status) > ans:
            ans = sum(binary_status)

print(ans)


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

int main(){
  int n; cin >> n;
  
  vector<int> a(n);
  
  int x = 0;
  
  for (int i = 0; i < n; i++){
    cin >> a.at(i);
  }
  
  int coef = 1;
  
  for (int i = 0; i < n; i++){
    x += coef * a.at(i);
    coef *= -1;
  }
  cout << x << ' ';
  
  for (int i = 1; i < n; i++){
    x = - x + 2 * a.at(i- 1);
    cout << x;
    if (i != n - 1) cout << ' ';
  }

}

Measured memory_bytes: 1664
#include<bits/stdc++.h>
using namespace std;
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
int main()
{
    optimize();
    string st;
    cin>>st;
    string s=st.substr(5,2);
    if(s=="01"||s=="02"||s=="03"||s=="04")
        cout<<"Heisei"<<"\n";
    else
        cout<<"TBD"<<"\n";
    return 0;

}


Measured memory_bytes: 3632
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#include<tgmath.h>
#include<stdbool.h>
#include<sys/queue.h>
#include<time.h>
#define ll long long
#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

const ll mod = 1000000007ll;
ll modpow(ll x, ll y, ll m){
    if(y == 0ll)return 1ll;
    ll res = 1ll, tmp = x;
    while(y){
        if(y&1) res = res * tmp % m;
        tmp = tmp * tmp % m;
        y >>= 1;
    }
    return res;
}
ll pom(ll x, ll y){
    return modpow(x, y, mod);
}
ll COM(ll x, ll y){
    if(x < y) return 0;
    if(x < 0 || y < 0) return 0;
    ll res = 1;
    for(int i=0; i<y; i++){
        res = res * (x-i) % mod * pom(y-i, mod-2) % mod;
    }
    return res;
}

ll x, y;

int main(){
	scanf("%lld%lld", &x, &y);
	if((2*x-y)%3!=0 || (2*y-x)%3!=0){
		puts("0");
		return 0;
	}
	ll i = (2*x-y)/3, j = (2*y-x)/3;
	printf("%lld\n", COM(i+j, i));
	return 0;
}

Measured memory_bytes: 128
def cost(p1, p2):
    return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])


def submit():
    h, w, d = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(h)]
    
    pos = {}
    for i in range(h):
        for j in range(w):
            pos[a[i][j]] = (i, j)

    dist = {}
    for i in range(1, d + 1):
        dist[i] = {i: 0}
        nxt = i + d
        while nxt <= h * w:
            dist[i][nxt] = dist[i][nxt - d] + cost(pos[nxt - d], pos[nxt])
            nxt += d
    
    q = int(input())
    ans = []
    for _ in range(q):
        l, r = map(int, input().split())
        base = l % d
        if base == 0:
            base = d
        ans.append(dist[base][r] - dist[base][l])
        
    for a in ans:
        print(a)


submit()
            
    


Measured memory_bytes: 57312
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順

// 2019 = 3 * 673
int main() {
    ll L, R;
    cin >> L >> R;
    ll mi = 2019;
    if (R - L >= 2019) {
        cout << 0 << endl;
        return 0;
    }
    for (ll i = L; i < R; i++) {
        for (ll j = i + 1; j <= R; j++) {
            mi = min(mi, (i * j) % 2019);
        }
    }
    cout << mi << endl;
}



Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <ctime>
#include <queue>
#include <sstream>
#include <utility>
#include <bitset>

using namespace std;

#define MP make_pair 
#define PB push_back 
#define INF (1000000007) 
#define eps 1e-8

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

int n,m;
int l[100005];
pair<ll,ll> p[100005],p2[100005];  //<?x,?>

inline void update(ll a,ll b)
{
	if(b>a)
	{
		p[b+1].second+=b-a;
		p[a+1].first--;
		p[a+1].second+=b+1;
		p[b+1].first++;
		p[b+1].second-=b+1;
		p[1].second+=b-a;
		p[a+1].second-=b-a;
	}
	else
	{
		p[a+1].first--;
		p[a+1].second+=b+1+m;
		p[b+1].second+=m-a+b;
		p[a+1].second-=m-a+b;
		p[1].first--;
		p[1].second+=b+1;
		p[b+1].first++;
		p[b+1].second-=b+1;
	}
}

int main()
{
	ll ans=1e18+7;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>l[i];
	for(int i=2;i<=n;i++) 
	{
		update((ll)l[i-1],(ll)l[i]);
//		memset(p2,0,sizeof(p2));
//		p2[1]=p[1];
//		for(int j=2;j<=m;j++)
//		{
//			p2[j].first=p2[j-1].first+p[j].first;
//			p2[j].second=p2[j-1].second+p[j].second;
//		}
//		for(int j=1;j<=m;j++) cout<<p2[j].first<<" "<<p2[j].second<<endl;
//		system("pause");
	}
	for(int i=2;i<=m;i++)
	{
		p[i].first+=p[i-1].first;
		p[i].second+=p[i-1].second;
	}

	for(int i=1;i<=m;i++) ans=min(ans,p[i].first*(ll)i+p[i].second);
	cout<<ans;
	return 0;
}

Measured memory_bytes: 2176
#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define PB push_back
#define MP make_pair

int MOD;

//累乗 O(log N)
long long power(long long x, long long N){
	if(N == 0) return 1LL;
	if(N == 1) return x;
	long long tmp = power(x, N/2);
	if(N%2 == 0) return tmp * tmp % MOD;
	else return tmp * tmp % MOD * x % MOD;
}

//逆元 O(log x)
long long inverse(long long x){
	return power(x, MOD-2);
}

//コンビネーション
struct Combination{
private:
	int N;
	vector<long long> factorial, inv;
public:
	void init(int n){ //初期化する O(N)
		N = n;
		factorial.push_back(1);
		inv.push_back(1);
		for(long long i=1; i<=N; i++){
			factorial.push_back(factorial.back()*i%MOD);
			inv.push_back(inverse(factorial.back()));
		}
	}
	long long fact(int n){
		return factorial[n];
	}
	long long comb(int n, int k){ //nCkを求める O(1)
		return factorial[n] * inv[k] % MOD * inv[n-k] % MOD;
	}
};

int main(){
	int p;
	scanf("%d", &p);
	MOD = p;
	Combination comb;
	comb.init(p);
	int b[2999] ={};
	int pow[2999][2999];
	for(int i=0; i<p; i++){
		pow[i][0] = 1;
		for(int j=1; j<p; j++){
			pow[i][j] = pow[i][j-1] * i % p;
		}
	}
	for(int i=0; i<p; i++){
		int a;
		scanf("%d", &a);
		if(a == 0) continue;
		for(int j=0; j<p; j++){
			b[j] -= ((p-j-1)%2 == 0 ? 1 : -1) * (/*power(i, p-j-1)*/pow[i][p-j-1] * comb.comb(p-1, j)) % p;
			b[j] = (b[j] + 5*p) % p;
		}
		b[0]++;

	}
	for(int i=0; i<p; i++){
		if(i != 0) printf(" ");
		printf("%d", b[i] % p);
	}
	cout << endl;
}

Measured memory_bytes: 35584
#include<bits/stdc++.h>
using namespace std;
int main()
{int n, i;
 unsigned long long int sum=0;
 cin>>n;
 
 for(i=1 ; i<=n ; i++)
 {
   if(i%3!=0 && i%5!=0)
     sum+=i;
 }
 
 cout<<sum;
 
 return 0;
}

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

using namespace std;

int main(){
	string s;
	getline(cin, s);
	vector<char> ch;
	vector<long> rng;
	long c = 0;
	for(string::iterator it = s.begin(); it != s.end(); it++){
		if(*it != 'x'){
			ch.push_back(*it);
			rng.push_back(c);
			c = 0;
		}
		else
			c++;
	}
	rng.push_back(c);
	long sz = ch.size();
	for(long i = 0; i < sz / 2 + (sz & 1); i++){
		if(ch[i] != ch[sz - 1 - i]){
			cout << -1;
			return 0;
		}
	}
	long r = 0;
	sz = rng.size();
	for(long i = 0; i < sz / 2; i++)
		r += abs(rng[i] - rng[sz - 1 - i]);
	cout << r;
	return 0;
}

Measured memory_bytes: 1656
#include <stdio.h>
#include <string.h>
int main(void)
{
    int  a,b[2000],i,j,n=0,m,x,y,z=0;
    char c[10100],s[10100];

    scanf("%d",&a);
    scanf("%s",s);
    n=strlen(s);
    for (size_t i = 0; i < n; i++)
    {
     if(s[i]+a>90) printf("%c",s[i]-26+a);
     else printf("%c",s[i]+a);
    }
    printf("\n");
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long


#if 0
const int MOD = ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;



int main() {
#if 0
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int n, k, i;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    string str;
    cin >> n >> k >> str;
    k = min(k, 15);

    string ans = str;
    str += string(str.rbegin(), str.rend());

    for(i = 0; i <= n; i++) {
        string cur = str.substr(i, n);
        if(k > 1) {
            int j = n - 1;
            while(cur[j] == cur[n - 1]) {
                j--;
            }
            int len = n - j - 1;
            string aux;
            aux.resize(min(len * (1 << (k - 1)), n), cur[n - 1]);
            while(aux.size() < n) {
                aux.push_back(cur[j--]);
            }
            swap(cur, aux);
        }
        ans = min(ans, cur);
    }
    cout << ans;

    return 0;
}


Measured memory_bytes: 384
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    int ans = 0;
    int a[100010];
    rep(i, N) cin >> a[i];
    rep(i, N) {
        while (a[i] % 2 == 0) {ans++; a[i] /= 2;}
    }
    cout << ans << endl;

    return 0;
}


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

int main() {
    string s;
    int k;
    cin >> k >> s;
    if(s.size() <= k) {
        cout << s << endl;
    }else {
        cout << s.substr(0,k) + "..." << endl;
    }
    return 0;
}

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


using namespace std;
using llong = long long int;


#define rep(i, n) for (int i = 0; i < n; ++i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define stl_repr(itr, x) for (auto itr = x.rbegin(); itr != x.rend(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()


const static int MOD = 1000000007;
const static int IINF = INT_MAX;
const static llong LINF = LLONG_MAX;
const static int dx[4] = {1, 0, -1, 0};
const static int dy[4] = {0, 1, 0, -1};


llong my_gcd(llong a, llong b) {
    return (a ? my_gcd(b % a, a) : b);
}
llong my_lcm(llong a, llong b) {
    return a * b / my_gcd(a, b);    
}

llong facmod(llong n) {
    if (n == 0) return 1;
    llong res = n;
    for (int i = n - 1; i > 0; --i) {
        res = (res * i) % MOD;
    }

    return res;
}
llong powmod(llong a, llong n) {
    llong res = 1;
    while (n > 0) {
        if (n & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        n >>= 1;
    }

    return res;
}
llong modinv(llong n) {
    return powmod(n, MOD - 2);
}
llong combmod(llong n, llong k) {
    llong res = 1;
    for (int i = 0; i < k; ++i) {
        res = (res * (n - i)) % MOD;
    }
    res = (res * modinv(facmod(k))) % MOD;

    return res;
}


int main (int argc, char *argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, a, b;
    cin >> n >> a >> b;
    if (n == 2) {
        cout << 0 << endl;
        return 0;
    }

    llong ans = powmod(2, n) - 1;
    ans = (ans - combmod(n, a)) % MOD;
    if (ans < 0) ans += MOD;
    ans = (ans - combmod(n, b)) % MOD;
    if (ans <  0) ans += MOD;

    cout << ans % MOD << endl;

    return 0;
}

Measured memory_bytes: 256
from bisect import bisect_left as bisectL

N, A, B, C, D = map(int, input().split())
S = input()
walls = []
prev = ''
for i, c in enumerate(S):
  if prev == '#' and c == '#':
    walls.append(i)
  prev = c

if bisectL(walls, A-1) == bisectL(walls, C-1) and bisectL(walls, B-1) == bisectL(walls, D-1):
  if C < D:
    print('Yes')
    exit()
  else:
    if '...' in S[B-2:D+1]:
      print('Yes')
      exit()
print('No')


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

using namespace std;

#define ok1 printf("ok1\n");
#define ok2 printf("ok2\n");
#define M 1000000000000000000LL
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n)　for(int i=(s);i<(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(n);i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define pb push_back
#define pf push_front
#define MIN(a,b) a=min((a),(b))
#define MAX(a,b) a=max((a),(b))
#define SIZE(v) (int)v.size()
#define Eunique(v) v.erace(unique(all(v)),v.end())
#define pvec(v)　rep(i,v.size()){cout << v[i] << " ";}
#define INF 0x3f3f3f3f
#define mset(m,v) memset(m,v,sezeof(m))
#define mod 1e9 +7;

const double pi = acos(-1.0);

typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef deque<ll> dll;
typedef pair<ll,ll> P;
ll dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};

ll gcd(ll x,ll y)
{
    ll r;
    while((r = x % y) != 0)
    {
        x = y;
        y = r;
    }
    return y;
}

ll lcm(ll x,ll y)
{
    x /= gcd(x,y);
    y /= gcd(x,y);
    return (x*y);
}

int main()
{
    ll a,b,c;
    cin >> a >> b >>c;
    cout << a*b/2 << endl;
    return 0;
}

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

using namespace std;

int main() {
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    int f = a/d + ((a%d)>0);
    int s = c/b + ((c%b)>0);
    if(f >=s){
        cout << "Yes";
    }
    else{
        cout << "No";
    }
}


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

int main()
{
    int n;
    cin >> n;
    vector<int> p(n);
    rep(i, n) cin >> p[i];
    int ans = 0;
    rep(i, n - 2) if ((max({p[i], p[i + 1], p[i + 2]}) != p[i + 1]) && min({p[i], p[i + 1], p[i + 2]}) != p[i + 1]) { ans++; };
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <climits>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000007


using namespace std;
#define ll long long
#define dbg if(0)
#define ISRANGE(val,lo,hi) ((lo<=val)&&(val<hi))

bool comp(pair<ll,ll> &a,pair<ll,ll> &b){
    return a.second < b.second;
}

bool isin(ll a,pair<ll,ll> hanni){
    return hanni.first<a&&a<hanni.second;
}
//無理

int main(){
    ll ans=0,n;
    cin>>n;
    vector< pair<ll,ll> > a;

    for(int i=0;i<n;i++){
        ll x,l;
        cin >>x>>l;
        a.push_back(make_pair(x-l+1,x+l));
    }
    sort(a.begin(),a.end(),comp);

    ll bef=a[0].second;
        //cout <<a[0].first<<" "<<a[0].second<<endl;;
    ans++;
    for(int i=1;i<n;i++){
        if (a[i].first>bef){
            ans++;
            bef=a[i].second;
        }
        //cout <<a[i].first<<" "<<a[i].second<<endl;;
    }

    cout <<ans<<endl;

    return 0;

}

Measured memory_bytes: 2420