akhauriyash/Code-Regression huggingface dataset:
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

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

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

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

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

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

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

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

fn main() {
    input! {
        n: usize,
        s: [chars; n],
    }

    let mut ans = 0;
    let mut a = 0;
    let mut b = 0;
    let mut all = true;

    for s in s.iter() {
        for i in 1..s.len() {
            if s[i - 1] == 'A' && s[i] == 'B' {
                ans += 1;
            }
        }
        if s[0] == 'B' {
            b += 1;
            if s[s.len() - 1] != 'A' {
                all = false;
            }
        }
        if s[s.len() - 1] == 'A' {
            a += 1;
            if s[0] != 'B' {
                all = false;
            }
        }
    }

    // dbg!((ans, a, b));

    ans += std::cmp::min(a, b);
    if all && std::cmp::min(a, b) > 0 {
        ans -= 1;
    }

    println!("{}", ans);
}


Measured memory_bytes: 4352
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>

using namespace std;

int main(){
	
	vector<long> a(300,0);
	a[0]=300000;
	a[1]=200000;
	a[2]=100000;
	long bonus=400000,sum=0;
	
	int x,y;
	cin >> x >> y;
	sum+=a[x-1]+a[y-1];
	if(x==1&&y==1)sum+=bonus;
	
	cout << sum << endl;
	
	return 0;
}


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

typedef long long ll;

ll INF = 100000000000000;

int main() {
    int N, M, X;
    ll ans = INF;
    cin >> N >> M >> X;
    vector<ll>c(N);
    ll A[N][M];
    for (int i = 0; i < N; i++) {
        cin >> c[i];
        for (int j = 0; j < M; j++) {
            cin >> A[i][j];
        }
    }

    for (ll bits = 0; bits < (1 << N); bits++) {
        ll cost = 0;
        vector<ll>total(M);
        for (int i = 0; i < N; i++) {
            if (bits & (1 << i)) {
                cost += c[i];
                for (int j = 0; j < M; j++) {
                    total[j] += A[i][j];
                }
            }
        }
        bool check = true;
        for (int i = 0; i < M; i++) {
            if (total[i] >= X) {
                continue;
            } else {
                check = false;
                break;
            }
        }
        if (check) {
            ans = min(ans, cost);
        }
    }
    if (ans == INF) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}


Measured memory_bytes: 3596
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    ll h;
    cin >> h;

    ll ans = 0, d = 1;
    while(h >= 1) {
        ans += d;
        h /= 2;
        d *= 2;
    }

    cout << ans << endl;
}


Measured memory_bytes: 256
H, W = [int(n) for n in input().split()]


def calc(H, W):
  res = 10000000000
  for h in range(1, H):
    S1 = h*W
    S2a = (H-h)*(W//2)
    S3a = (H-h)*(W-W//2)
    S2b = (H-h)//2*W
    S3b = ((H-h)- (H-h)//2)*W
    dS = min( max(S1, S2a, S3a)-min(S1, S2a, S3a), max(S1, S2b, S3b) - min(S1, S2b, S3b))
    if dS < res:
      res = dS
  return res

print(min(calc(H,W), calc(W,H)))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000009;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

int a[55],b[55],c[55],d[55];
int n,m,q;
int ans;
Vi v;
void dfs(int cur)
{
	int N=v.size();
	int tmp=0;
	fup(i,1,q,1)
	{
		if(b[i]>=N)continue;
		if(v[b[i]]-v[a[i]]==c[i])tmp+=d[i];
	}
	ans=max(ans,tmp);
	if(N==n)return;
	if(cur<m)dfs(cur+1);
	v.pb(cur);
	dfs(cur);
	v.pop_back();
}
int main() {
	scanf("%d%d%d",&n,&m,&q);
	fup(i,1,q,1)scanf("%d%d%d%d",a+i,b+i,c+i,d+i),a[i]--,b[i]--;
	dfs(1);
	printf("%d",ans);
}

Measured memory_bytes: 3832
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+7;
struct Node{
    int key;
    Node* parent;
    Node* left;
    Node* right;
};
Node* T;
void insert_num(int z){
    Node* s = (Node*)malloc(sizeof(Node));
    s->key = z;
    s->parent = NULL;
    s->left = NULL;
    s->right = NULL;

    Node* y = NULL; // parent of x
    Node* x = T;
    while(x != NULL){
        y = x; // set the parent
        if(z < x->key){
            x = x->left; // move to the left child
        }else{
            x = x->right; // move to the right child
        }
    }
    s->parent = y;
    if(y == NULL){
        T = s;
    } // T is empty
    else if(z < y->key){
        y->left = s;
    }// z is the left child of y
    else{
        y->right = s;
    }// z is the right child of y
}
void in(Node* T){
    if(T==NULL) return;
    in(T->left);
    printf(" %d", T->key);
    in(T->right);
}
void pre(Node* T){
    if(T==NULL) return;
    printf(" %d", T->key);
    pre(T->left);
    pre(T->right);
}
void print(){
    in(T);
    printf("\n");
    pre(T);
    printf("\n");
}

int main(){
    T = NULL;
    int n;
    scanf("%d", &n);
    char s[10];
    for(int i = 0; i < n; i++){
        scanf("%s", s);
        if(s[0] == 'i'){
            int num;
            scanf("%d", &num);
            insert_num(num);
        } else{
            print();
        }

    }
    return 0;
}



Measured memory_bytes: 26648
#include <bits/stdc++.h>
#define int long long
#define Matrix vector<vector<int> >
// #define Matrix vector<vector<int> >
#define double long double
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define td(v) v.begin(),v.end()
#define tdr(v) v.rbegin(),v.rend()
#define endl "\n"
#define Matrix vector<vector<int> >
using namespace std;
const int MOD = 1e9+7;
const long long INF = 5e18;
const double pi = acos(-1.0);
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int gcd(int a, int b){return (b == 0 ? a : gcd(b, a%b));}
int lcm(int a,int b){return (a*b)/gcd(a,b);}
 
inline long long mod(long long n, long long m){
    long long ret = n%m;
    if(ret < 0) ret += m;
    return ret;
}
 
double rad(double x){
    return x*pi/180.0;
}
 
bool isleft(pair<int,int> a, pair<int,int> b, pair<int,int> c){
    int det = (b.first-a.first)*(c.second-a.second) - (c.first-a.first)*(b.second-a.second);
    if(det>=0) return true;
    if(det<0) return false;
    return false;
}
 
int exp(int a, int b){
    int result = 1;
    while (b > 0){
        if (b & 1)
            result = mod(result*a,MOD);
 
        b >>= 1;
        a = mod(a*a,MOD);
    }
    return result;
}

void solve(){
	int n,k;
	cin>>n>>k;
	vector<int>v(n);
	for(int &i : v)
		cin>>i;
	vector<int>vet(1e5+1,0);
	for(int i=0;i<=v[0];i++){
		vet[i]=1;
	}
	vector<int>prefix;
	prefix.push_back(vet[0]);
	for(int i=1;i<=1e5; i++){
		int x = prefix.back();
		prefix.push_back(vet[i]+x);
	}
	for(int i=1;i<n;i++){
		vector<int>newvet(1e5+1,0);
		for(int j=0;j<=k;j++){
			int bot = max(0ll, j-v[i-1]);
			int top = min(j,v[i]);
			if(bot>top) continue;
			newvet[j]=prefix[j-bot]-prefix[j-top]+vet[j-top];
			newvet[j]=mod(newvet[j],MOD);
		}
		vet = newvet;
		prefix.clear();
		prefix.push_back(vet[0]);
		for(int j=1;j<=1e5; j++){
			if(vet[j]>0) v[i]=j;
			int x = prefix.back();
			prefix.push_back(vet[j]+x);
			prefix[j]%=MOD;
		}
	}
	cout<<vet[k]<<endl;
}

signed main(){
    fastio;
    int t=1;
    // cin>>t;
    while(t--)
        solve();
}

Measured memory_bytes: 5608
import re

N = int(input())

startB = 0
stopA = 0
BtoA = 0
AB = 0

for i in range(N):
    s = input()
    if s.startswith("B") and s.endswith("A"):
        BtoA += 1
    elif s.startswith("B"):
        startB += 1
    elif s.endswith("A"):
        stopA += 1
    
    AB += len(re.findall("AB", s))


if BtoA == 0:
    ans = AB + min(startB, stopA)
elif startB + stopA > 0:
    ans = AB + min(startB, stopA) + BtoA
else:
    ans = AB + min(startB, stopA) + BtoA - 1
print(ans)

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

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int main(){
  int N,K;
  cin >> N >> K;
  vector<int> v(N);
  
  for(int i = 0; i < N; i++){
    cin >> v.at(i);
  }
  
  UnionFind tree(N);
  for(int i = 0; i < K; i++){
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    tmp1--;
    tmp2--;
    tree.unionSet(tmp1,tmp2);
  }
  
  int ans = 0;
  for(int i = 0; i < N; i++){
    if(tree.root(i) == tree.root(v.at(i)-1)){
      ans++;
    }
  }
  cout << ans << endl;
}



Measured memory_bytes: 1024
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String args[]) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] strArr = br.readLine().split(" ");
		long a = Long.parseLong(strArr[0]);
		long b = Long.parseLong(strArr[1]);
		String mark = (a > b? ">" : (a == b ? "==" : "<"));
		System.out.printf("a %s b%n", mark);
	}
}

Measured memory_bytes: 17040
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N=200010,INF=1e14;
int main(){
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	ll n,i,j,dp[N],cap,t,val=0,a[N],b[N],best;
	cin>>n>>cap;
	for(i=0;i<n;++i){
		cin>>a[i]>>b[i];
		val+=b[i];
	}
	for(i=1;i<=val;++i) dp[i]=INF;
	dp[0]=0;
	for(i=0;i<n;++i){
		for(j=val;j>=b[i];--j){
			dp[j]=min(dp[j-b[i]]+a[i],dp[j]);
		}
	}
	for(i =val;i>=0;i--){
		if(dp[i] <= cap){
			cout << i << endl;
			break;
		}
	}
	return 0;
}

Measured memory_bytes: 4736
// 作者：杨成瑞先生
import java.io.*;
import java.util.*;

public class Main {

	static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e){e.printStackTrace();}
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {st = new StringTokenizer(br.readLine());}
            catch (Exception e) {e.printStackTrace();}
            return st.nextToken();
        }

        public int nextInt() {return Integer.parseInt(next());}

        public long nextLong() {return Long.parseLong(next());}

        public double nextDouble() {return Double.parseDouble(next());}

        public String nextLine() {
            String line = "";
            if(st.hasMoreTokens()) line = st.nextToken();
            else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
            while(st.hasMoreTokens()) line += " "+st.nextToken();
            return line;
        }
    }

	public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        PrintWriter pw = new PrintWriter(System.out);

        int n = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        int[] c = new int[n];

        for(int i=0;i<n;i++) {
            a[i] = sc.nextInt(); b[i] = sc.nextInt(); c[i] = sc.nextInt();
        }

        int[][] dp = new int[n][3];
        dp[0][0] = a[0];
        dp[0][1] = b[0];
        dp[0][2] = c[0];

        for(int i=1;i<n;i++) {
            dp[i][0] = Math.max(dp[i-1][1] + a[i], dp[i-1][2] + a[i]);
            dp[i][1] = Math.max(dp[i-1][0] + b[i], dp[i-1][2] + b[i]);
            dp[i][2] = Math.max(dp[i-1][0] + c[i], dp[i-1][1] + c[i]);
        }

        pw.println(Math.max(dp[n-1][0], Math.max(dp[n-1][1], dp[n-1][2])));
        pw.close();
	}

}

Measured memory_bytes: 50424
n, m = map(int, input().split())
a = []
for i in range(n):
    b = [int(j) for j in input().split()]
    for k in range(1, b[0] + 1):
        a.append(b[k])
c = 0
for i in range(1, m + 1):
    if a.count(i) == n:
        c += 1
print(c)

Measured memory_bytes: 3060
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

#define mp make_pair
#define pb push_back


typedef long long ll;
typedef long double ld;

using namespace std;

int n;
vector<pair<int, int> > vv;

const ll MOD = 1e9 + 7;


int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		int x;
		scanf("%d", &x);
		vv.push_back(make_pair(x, 0));
	}
	for (int i = 0; i < n; ++i) {
		int x;
		scanf("%d", &x);
		vv.push_back(make_pair(x, 1));
	}
	sort(vv.begin(), vv.end());
	int c = 0;
	ll ans = 1;
	for (int i = 0; i < (int)vv.size(); ++i) {
		if (vv[i].second == 0) {
			if (c < 0)
				ans = (ans * (-c)) % MOD;
			++c;
		}
		else {
			if (c > 0)
				ans = (ans * c) % MOD;
			--c;
		}
	}
	cout << ans << "\n";
	return 0;
}




Measured memory_bytes: 2420
#include <bits/stdc++.h>
using namespace std;
#define pie 3.141592653589793238462643383279
#define int long long
#define mod 1000000007
#define INF LLONG_MAX/5
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
int x[100000],y[100000],z[100000];
signed main() {
	int n,ans=0;
	cin>>n;
	f(i,n)cin>>x[i];
	f(i,n)cin>>y[i];
	f(i,n-1)cin>>z[i];
	f(i,n){
		ans+=y[x[i]-1];
		if(i&&x[i]-x[i-1]==1)ans+=z[x[i-1]-1];
	}
	cout<<ans<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include<iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define INF 1e9+7
#define rep(i,m,n) for(int i=m;i<n;i++)
#define mrep(i,m,n) for(int i=m;i>n;i--)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;


//(int)'a'は97 (int)'A'は65 (int)'1'は49
//おまじない

int main() {

	double a, b, c, d, e, f;
	cin >> a >> b >> c >> d >> e >> f;
	vector < double > cw, cs;

	for (int i = 0; i*a * 100 <= f; i++) {
		for (int j = 0; j*b * 100 <= f; j++) {
			if ((i*a + j * b) * 100 <= f && i*a + j * b>0)cw.push_back((i*a + j * b) * 100);
		}
	}

	for (int i = 0; i*c <= f; i++) {
		for (int j = 0; j*d <= f; j++) {
			if (i*c+j*d <= f && i*c + j * d > 0)cs.push_back(i*c + j * d);
		}
	}

	double con = 0, x = 100*a, y = 0;

	for (int i = 0; i < cw.size(); i++) {
		for (int j = 0; j < cs.size(); j++) {
			double lim = cw[i] / 100 * e;
			if (cw[i] + cs[j] <= f && cs[j] <= lim && con <= cs[j] * 100 / (cw[i] + cs[j])) {
				con = cs[j] * 100 / (cw[i] + cs[j]);
				x = cw[i] + cs[j], y = cs[j];
			}
		}
	}

	cout << (int)x << ' ' << (int)y << endl;

	return 0;
}

Measured memory_bytes: 2420
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
	int a,b,c;
	cin >> a;
	int d = 0;
	while(a--){
		cin >> b >> c;
		d += c - b + 1;
	}
	cout << d;
}

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

using namespace std;

int main()
{
  int a ,b, c,d;
  cin >> a >> b >> c >> d;
  int s = (a-1) / d;
  int t = (c-1) / b;

  if(s < t) cout << "No"<< endl;
  else cout << "Yes" << endl;
}


Measured memory_bytes: 3636
N, M = map(int,input().split())
A = []
B = []
ta = 0
tb = 0
# for i in range(N):
#     ta, tb = map(int, input().split())
#     A.append(ta)
#     B.append(tb)
# print(A,B)
L = [list(map(int,input().split())) for i in range(N)]
#print(L)
L.sort(key=lambda x: x[0])
#print(L)
c = 0
for i in range(N):
    if M <= L[i][1]:
        print(c + L[i][0] * M)
        exit()
    else:
        M -= L[i][1]
        c += L[i][0] * L[i][1]


Measured memory_bytes: 29028
#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <climits>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <functional>
#include <cstring>

using namespace std;

#define mp make_pair
#define FOR(i, a, b) for(int (i)=a;(i)<(b);++(i))
#define rep(i, n)  FOR(i,0,n)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort((v).begin(), (v).end());					//小さい順
#define rvsort(v) sort(v.begin(), v.end(),greater<>());		//大きい順
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define all(c) (c).begin(),(c).end()
#define ll long long
#define lb long double
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define Print(p) cout<<(p)<<endl
#define F first
#define S second
#define pb push_back
#define mod 1000000007LL
#define INF 1234567890123456
typedef pair<int, int> P;
typedef pair<int, P> PP;

const int maxn =1e3 + 10;

//initialization here
//
P cost[90005];
ll dp[90005];
//
//initialization finish


// your function is here
//

//
// your function finished



int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	// your code is here
	//
	int h, w, d; cin >> h >> w >> d;


	rep(i, h)rep(j, w)
	{
		int a; cin >> a;
		cost[a] = mp(i, j);
	}


	rep(i, d)dp[i] = 0;

	rep(i, d)for(int cnt = i+d ; cnt <= h*w ; cnt += d)
	{
		dp[cnt]= dp[cnt-d] + abs(cost[cnt].first - cost[cnt -d].first) + abs(cost[cnt].S - cost[cnt-d].S);
	}



	int q; cin >> q;
	rep(i, q)
	{
		int l,r; cin >> l >> r;
		Print(dp[r] - dp[l]);
	}

	//
	// your code finished


	return 0;
}

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

string S,T;
int main(){
	cin >> S >> T;
	int N = S.size();
//	S += S;
	bool judge = false;
	for(int i=0;i<N;i++){
		bool t = true;
		for(int j=0;j<N;j++){
			if(S[(i+j+N)%N]!=T[j]) t = false;
		}
		if(t){
			judge = true;
			break;
		}
	}
	if(judge) cout << "Yes" << endl;
	else cout << "No" << endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#define MAX 100000
int A[MAX] = {0};

void showM(int A[], int n) {
	int i;
	
	for (i = 0; i < n; ++i) {
		if (i != n -1) {
			printf("%d ", A[i]);
		} else {
			printf("%d\n", A[i]);
		}
	}
	//printf("\n");
}

int partition(int A[], int p, int r) {
	int x, tmp, i, j;
	
	x = A[r-1];
	i = p - 1;

	for (j = p; j < r- 1; ++j) {
//		showM(A, r);
//		printf("A[j] = %d, x = %d\n", A[j], x);
		if (A[j] <= x) {
			++i;
			tmp = A[i];
			A[i] = A[j];
			A[j] = tmp;
		}
	}
	tmp = A[i+1];
	A[i+1] = A[r-1];
	A[r-1] = tmp;
	return i + 1;
}

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

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

	cnt = partition(A, 0, n);

	for (i = 0; i < cnt; ++i) {
		printf("%d ", A[i]);
	}

	printf("[%d]", A[cnt]);

	if (cnt != n -1) {
		putchar(' ');
	} else {
		putchar('\n');
	}
	
	for (i = cnt + 1; i < n; ++i) {
		printf("%d", A[i]);
			
		if (i != n -1) {
			putchar(' ');
		} else {
			putchar('\n');
		}
	}

	
	
	return 0;
}

Measured memory_bytes: 992
def main():
    n = int(input())
    a_lst = [(int(input()), i + 1) for i in range(n)]
    a_lst_descend = sorted(a_lst, reverse=True)

    max_value = a_lst_descend[0][0]
    max_index = a_lst_descend[0][1]
    second_value = a_lst_descend[1][0]

    for i in range(1, n + 1):
        if i == max_index:
            print(second_value)
        else:
            print(max_value)


if __name__ == "__main__":
    main()


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

int main(){
    int64_t n, m; cin >> n >> m;
    if(m/2 <= n) cout << m/2 << endl;
    else cout << (2*n+m)/4 << endl;
}

Measured memory_bytes: 256
from bisect import bisect_left

N,M= [int(i) for i in input().split()]
n = sorted([int(x) for x in input().split()])

s =[0]*(N+1)
s[0]=n[0]
for i in range(1,N+1):
        s[i] = s[i-1]+n[i-1]
MM=n[-1]*2
mm=n[0]*2

while True:
        c=0        
        r=(mm+MM)//2
        for i in range(N):
                c += N-bisect_left(n,r-n[i])
        if (MM-mm) < 2:
                break
        elif c>=M:
                mm=r
        elif c<M:
                MM=r

v=0
for i in range(N):
        j=bisect_left(n,r-n[i])
        v+= (s[N] - s[j]) + (N-j)*n[i]

v = v - (c-M)*(r)

print(v)        

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

using namespace std;

using ull = unsigned long long;

int main() {
    int field[100000] = {0}, N, splt = 0, mid = 0;
    string S;
    cin >> S;
    N = S.size();
    for(int i = 0; i < N - 1; i++) {
        if(S[i] != S[i + 1]) {
            if(S[i] == 'L') {
                field[mid - 1] = (i + 1 - splt) / 2;
                field[mid] = (i + 1 - splt) / 2;
                if((i + 1 - splt) % 2) {
                    if(splt % 2 == mid % 2) {
                        field[mid]++;
                    } else {
                        field[mid - 1]++;
                    }
                }
                splt = i + 1;
                mid = 0;
            } else {
                mid = i + 1;
            }
        }
    }
    if(mid != 0){
        field[mid - 1] = (N - splt) / 2;
        field[mid] = (N - splt) / 2;
        if((N - splt) % 2) {
            if(splt % 2 == mid % 2) {
                field[mid]++;
            } else {
                field[mid - 1]++;
            }
        }
    }
    for(int i = 0; i < N; ++i) {
        cout << field[i] << (i == N - 1 ? '\n' : ' ');
    }
    return 0;
}


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

int main() {
	// your code goes here
	int n,p;
	cin>>n>>p;
	string s;
	cin>>s;
	//int val[n];
	ll ans=0;
	if(p==2 || p==5)
	{
		for(int i=0;i<n;i++)
		{
			if((s[i]-'0')%p==0)
			ans+=i+1;
		}
		cout<<ans;
		return 0;
	}
	
	unordered_map<ll,ll>mp;
	ll sum[n+1];
	sum[n]=0;
	int power=1;
	for(int i=n-1;i>=0;i--)
	{
		sum[i]=sum[i+1]+power*(s[i]-'0');
		sum[i]=sum[i]%p;
		power=power*10;
		power=power%p;
		// mp[sum[i]]++;
	}
	for(int i=0;i<=n;i++)
	mp[sum[i]]++;
	for(auto x:mp)
	ans+=((x.second-1)*x.second)/2;
	cout<<ans;
	
	
	

     
	return 0;
}

Measured memory_bytes: 2436
#!/usr/bin/env ruby
while s = gets.split(' ')
    if s == ["0", "0"]
        break
    end

    a = []
    for i in 0..s.length-1
        a[i] = s[i].to_i
    end

    if a[0] > a[1]
        puts "#{a[1]} #{a[0]}"
    else
        puts "#{a[0]} #{a[1]}"
    end
end



Measured memory_bytes: 6872
#include<bits/stdc++.h>
using namespace std;
#define int           long long
#define	pb            push_back
#define all(a)        (a).begin(),(a).ant()
#define sz(x)         (int)x.size()
#define F             first
#define S             second
#define endl          "\n"
#define inf           1000000007
#define FOR(i, a, b)  for(int i=a; i<b; i++)
#define bigboi        1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); 
#define SHUF(v) shuffle(aint(v), RNG);
// Use mt19937_64 for 64 bit random numbers. 

void solve(){
	string s;
	cin>>s;
	if(s=="ABC"){
		cout<<"ARC"<<endl;
	}
	else{
		cout<<"ABC"<<endl;
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); 	
	int t=1;
	//cin>>t;
	FOR(i, 1, t+1){		
	//	cout<<"Case #"<<i<<":"<<endl;
		solve();
	}
	cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
	return 0;
 
}


Measured memory_bytes: 3684
#include <iostream>
#include <map>
#include <string>
using namespace std;
long long n,m,i,p,nra,nrg;
string s;
bool ma[100005];
int nrgr[100005];
int main()
{
    cin>>n>>m;
    for(i=1;i<=m;i++)
    {
        cin>>p>>s;
        if(s=="AC")
        {
            if(ma[p]==false)
            {
                nra++;
                nrg+=nrgr[p];
            }
            ma[p]=true;
        }
        else
        {
            if(ma[p]==false)
                nrgr[p]++;
        }
    }
    cout<<nra<<' '<<nrg;
}


Measured memory_bytes: 768
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(unsigned x){
  int s=0;
  char f[10];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(unsigned long long x){
  int s=0;
  char f[21];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(double x){
  printf("%.15f",x);
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    putchar_unlocked(c[i]);
  }
}
inline void wt_L(string &x){
  int i=0;
  for(i=0;x[i]!='\0';i++){
    putchar_unlocked(x[i]);
  }
}
template<class T> inline T pow2_L(T a){
  return a*a;
}
int main(){
  int R;
  rd(R);
  wt_L(pow2_L(R));
  wt_L('\n');
  return 0;
}
// cLay varsion 20191123-1

// --- original code ---
// {
//   int R;
//   rd(R);
//   wt(R**2);
// }


Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include <stack>
using namespace std;
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define ll long long
#define llint long long int
#define sort(s) sort(s.begin(),s.end())
#define reverse(v)  reverse(v.begin(), v.end());
#define Yes(ans) if(ans)cout<<"Yes"<<endl; else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl; else cout<<"NO"<<endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<int>>
constexpr auto mod = 1000000007;


//2のn乗を求めるよ！！！
int ni(int n) {
	if (n == 0)return 1;
	int x = ni(n / 2);
	x *= x;
	if (n % 2 == 1)x *= 2;
	return x;
}

//フィボナッチ数列のx番目を求めるよ！
int f(int x,vector<int> s) {
	if (x == 0)return 0;
	if (x == 1)return 1;
	if (s[x] != 0)return s[x];
	s[x] = f(x - 1,s) + f(x - 2,s);
	return s[x];
}

//aとｂの最大公約数を求めるよ！
llint gcd(llint a, llint b) {
	if (a < b)swap(a, b);
	if (b == 0)return a;
	return gcd(b, a % b);
}



// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

// mod. m での a の逆元 a^{-1} を計算するよ！
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}


//aCbをmod.mで割った余りを求める
ll int c(ll int a, ll int b,ll int m) {
	ll int ans = 1;
	for (ll int i = 0; i < b; i++) {
		ans *= a - i;
		ans %= m;
	}
	for (ll int i = 1; i <= b; i++) {
		ans *= modinv(i, m);
		ans %= m;
	}
	return ans;
}

//m進数のn桁を全列挙やで
void dfs(int m, int n) {

	stack<string> st;
	st.push("");
	while (!st.empty()) {
		string now = st.top();
		st.pop();
		if (now.size() == n) {
			cout << now << endl;
		}
		else {
			for (int i = m-1; i >= 0; i--) {
				string next = now + to_string(i);
				st.push(next);
			}
		}
	}
}

int main() {
	ll n, m;
	ll ans = 0;
	cin >> n>>m;
	if (2 * n >= m)ans = m / 2;
	else {
		ans = n + (m - (2 * n)) / 4;
	}
	cout << ans << endl;
	return 0;
}





  


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

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "{";
    rep(i, (int)v.size()) {
        if (i)
            os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
    for (auto val : array)
        std::cout << val << ", ";
    std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

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

    int n; std::cin >> n;
    vector<pi> ba;
    rep(i, n) {
        int a, b; std::cin >> a >> b;
        ba.pb({b, a});
    }
    sort(rng(ba));
    bool ok = true;
    int cur = 0;
    rep(i, n) {
        cur += ba[i].se;
        if (cur > ba[i].fi) ok = false;
    }
    std::cout << (ok ? "Yes" : "No") << "\n";
}


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

using namespace std;

int main() {
    std::ios::sync_with_stdio(false); 
    cin.tie(nullptr);
    
    int H, W, N, sr, sc;
    cin >> H >> W >> N >> sr >> sc;
    string S, T;
    cin >> S >> T;

    constexpr int L = 0, R = 1, U = 2, D = 3;
    map<char, int> m{{'L', L}, {'R', R}, {'U', U}, {'D', D}};
    for (int i = 0; i < N; ++i) {
        S[i] = m[S[i]];
        T[i] = m[T[i]];
    }
    
    {
        int lb = sc, rb = sc, cntls = 0;
        for (int i = 0; i < N; ++i) {
            cntls += (S[i] == L);
            if (lb - cntls <= 0) ++lb;
            if (lb > rb || rb + cntls <= 0) {
                cout << "NO\n";
                return 0;
            }
            lb -= (T[i] == L);
            lb = max(lb, 1);
            rb += (T[i] == R);
            cntls -= (rb > W && cntls);
            rb = min(rb, W);
        }
    }
    {
        int lb = sc, rb = sc, cntrs = 0;
        for (int i = 0; i < N; ++i) {
            cntrs += (S[i] == R);
            if (rb + cntrs > W) --rb;
            if (lb > rb || lb + cntrs > W) {
                cout << "NO\n";
                return 0;
            }
            lb -= (T[i] == L);
            cntrs -= (lb <= 0 && cntrs);
            lb = max(lb, 1);
            rb += (T[i] == R);
            rb = min(rb, W);
        }
    }
    {
        int ub = sr, db = sr, cntds = 0;
        for (int i = 0; i < N; ++i) {
            cntds += (S[i] == D);
            if (db + cntds > H) --db;
            if (ub > db || ub + cntds > H) {
                cout << "NO\n";
                return 0;
            }
            ub -= (T[i] == U);
            cntds -= (ub <= 0 && cntds);
            ub = max(ub, 1);
            db += (T[i] == D);
            db = min(db, H);
        }
    }
    {
        int ub = sr, db = sr, cntus = 0;
        for (int i = 0; i < N; ++i) {
            cntus += (S[i] == U);
            if (ub - cntus <= 0) ++ub;
            if (ub > db || db + cntus <= 0) {
                cout << "NO\n";
                return 0;
            }
            ub -= (T[i] == U);
            ub = max(ub, 1);
            db += (T[i] == D);
            cntus -= (db > H && cntus);
            db = min(db, H);
        }
    }
    
    cout << "YES\n";

    return 0;
}

Measured memory_bytes: 920
n = int(input())
ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(a,b)
if a == b == c != 0:
    ans = ab + min(a,b) - 1
print(ans)

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

using namespace std;


int main()
{
  int N, M, E[200000];
  bool used[200001] = {};

  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    cin >> E[i];
  }
  for(int i = M - 1; i >= 0; i--) {
    if(used[E[i]]++) continue;
    cout << E[i] << endl;
  }
  for(int i = 1; i <= N; i++) {
    if(used[i]++) continue;
    cout << i << endl;
  }
}

Measured memory_bytes: 3680
#[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Bytes, Chars, Isize1, Usize1},
};

#[allow(unused_macros)]
macro_rules! echo {
    ($($e:expr),+) => ( { $(println!("{}", $e))+ } );
}

fn main() {
    input! {
        a: char,
    }

    if a.is_ascii_uppercase() {
        echo!('A');
    } else {
        echo!('a');
    }
}


Measured memory_bytes: 2132
#include<iostream>
#define MAX 10000
using namespace std;
int main(){
    int n,mini=MAX,data[1000],x[1000],pos[MAX+1]; cin>>n;
    for(int i=0;i<n;++i){
	cin>>data[i];
	x[i]=data[i];
	if(mini>data[i]) mini=data[i];
    }
    for(int i=0;i<n-1;++i)
	for(int j=n-1;j>i;--j)
	    if(x[j-1]>x[j]) swap(x[j-1],x[j]);
    for(int i=0;i<n;++i) pos[x[i]]=i;
    int sumn=0,flag[1000]={};
    for(int i=0;i<n;++i){
	if(flag[i]) continue;
	int m=MAX,sum=0,cnt=0,cur=i;
	do{
	    int val=data[cur];
	    flag[cur]=1;
	    sum+=val;
	    cur=pos[val];
	    if(m>val) m=val;
	    ++cnt;
	}while(!flag[cur]);
	if(sum+(cnt-2)*m<m+sum+(cnt+1)*mini) sumn+=sum+(cnt-2)*m;
	else sumn+=m+sum+(cnt+1)*mini;
    }
    cout<<sumn<<endl;
    return 0;
}



Measured memory_bytes: 3132
#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;

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

    vector<string> s_vec;
    rep(i, 0, s.size())
        s_vec.push_back(s.substr(i, s.size() - i));
    sort(s_vec.begin(), s_vec.end());

    set<string> str_set;
    rep(i, 0, s_vec.size())
    {
        rep(j, 0, s_vec[i].size())
        {
            str_set.insert(s_vec[i].substr(0, j + 1));
        }
        if (str_set.size() > k) break;
    }

    vector<string> strs;
    for (auto ss: str_set) strs.push_back(ss);
    sort(strs.begin(), strs.end());

    cout << strs[k - 1] << endl;
    
   
    return 0;
}

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

d = {}

for i in range(1, m + 1):
    d[i] = 0

for i in range(n):
    k, *a = map(int, input().split())

    for j in a:
        d[j] += 1

print(len(list(filter(lambda x: x == n, d.values()))))


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

int N, Y;

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

    int yukichi = -1;
    int higuchi = -1;
    int hideyo = -1;

    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= N; j++) {
            if (10000*i + 5000*j + 1000*(N - i - j) == Y && N - i - j >= 0) {
                yukichi = i;
                higuchi = j;
                hideyo = N - i - j;
            }
        }
    }

    cout << yukichi << " " << higuchi << " " << hideyo << endl;
    
    return 0;
}


Measured memory_bytes: 256
// Bismillahir Rahmanir Rahim
// AtCoder-Coffee


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

int main(){
	string s;  cin>>s;
	if((s[2]==s[3])&&(s[4]==s[5]))
	cout<<"Yes\n";
	else cout<<"No\n";
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <iomanip>
#include <regex>
using namespace std;
#define PI acos(-1)
typedef long long ll;
typedef pair<int, int> p_ii;
const ll INF = 1LL << 60;

int main() {
	
	string str; cin >> str;
	cout << (regex_match(str, regex("A?KIHA?BA?RA?")) ? "YES" : "NO") << endl;

	return 0;
}

Measured memory_bytes: 384
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    if n >= m:
        print(0)
        exit()
    minimum = x[0]
    maximum = x[-1]
    if n == 1:
        print(maximum-minimum)
        exit()
    arr = []
    for i in range(len(x)-1):
        arr.append(abs(x[i+1]-x[i]))

    arr.sort()

    print(maximum-minimum-sum(arr[-(n-1):]))


if __name__ == '__main__':
    main()


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

const int MAX_N = 13;
int H,W,K;
string g[MAX_N];
int cmp;
int cnt[MAX_N][1002];
int tot;
map<int,int> col;
int comp[MAX_N];

int go()
{
	int ans = 0;
	col.clear();
	for(int j=0;j<W;++j){
		for(int gr=0;gr<tot;++gr){
			col[gr]+=cnt[gr][j];
		}
		bool ok = true;
		for(int gr=0;gr<tot;++gr)if(col[gr]>K){
			ok=false;
			break;
		}
		if(!ok){
			ans++;
			for(int gr=0;gr<tot;++gr){
				col[gr]=cnt[gr][j];
			}
		}
	}
	return ans;
}

void solve()
{
	const int hc = H-1;
	int ans = H*W;
	for(int i=0;i<(1<<hc);++i){
		memset(cnt,0,sizeof(cnt));
		tot = __builtin_popcount(i)+1;
		int cur_g = tot-1;
		for(int r=H-1;r>=0;--r){
			if(i>>r&1) cur_g--;
			comp[r]=cur_g;
		}
		for(int c=0;c<W;++c){
			for(int r=0;r<H;++r){
				int gr = comp[r];
				cnt[gr][c]+=(g[r][c]=='1');
				if(cnt[gr][c]>K) goto out;
			}
		}
		ans=min(ans, tot-1+go());
		out: continue;
	}
	cout << ans << '\n';
}

int main()
{
	cin >> H >> W >> K;
	for(int i=0;i<H;++i) cin >> g[i];
	solve();
	return 0;
}

Measured memory_bytes: 3708
#include <iostream>
#include <algorithm>

#define MAX_ 17
#define IF 16000000

#define lengthof(x) (sizeof(x) / sizeof(*(x)))

using namespace std;

int main(int argc, char const *argv[])
{
	int n;
	unsigned int data[MAX_][MAX_];
	unsigned int dp[1<<(MAX_-1)][MAX_];
	unsigned int min_;

	while(1){
		cin>>n;
		if(n==0) break;

		fill((unsigned int*)data,(unsigned int*)(data+lengthof(data)),0);
		fill((unsigned int*)dp,(unsigned int*)(dp+lengthof(dp)),IF);

		//dp[]

		for(int i1=0;i1<n;i1++){
			for(int i2=0;i2<n+1;i2++){
				cin>>data[i1][i2];
			}
		}


		for(int i1=0;i1<n;i1++){
			dp[1<<i1][0]=data[i1][0];
		}


		for(int i1=0;i1<n-1;i1++){
			for(int i2=0;i2<(1<<n);i2++){
				if(dp[i2][i1]!=IF) {
					for(int i3=0;i3<n;i3++){
						int i4=i2|(1<<i3);
						if(i2!=i4){
							
							min_=data[i3][0];
							for(int i5=0;i5<n;i5++){
								
								if((i2&(1<<i5))!=0) {
									min_=min(min_,data[i3][i5+1]);
									//cout<<"min_ "<<min_<<" "<<data[i3][i5+1]<<endl;
								}
							}
							dp[i4][i1+1]=min(dp[i4][i1+1],dp[i2][i1]+min_);
							//i3のステージをi2の武器でこす
						}
					}
				}
			}
		}

		/*for(int i1=0;i1<(1<<n);i1++){
			for(int i2=0;i2<n;i2++){
				cout<<dp[i1][i2]<<"  ";
			}
			cout<<endl;
		}*/

		cout<<dp[(1<<n)-1][n-1]<<endl;




	}
	return 0;
}

Measured memory_bytes: 5508
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

int main(){
  int N,K;
  cin>>N>>K;
  ll a[N];
  FOR(i,0,N) cin>>a[i];
  ll plus[N+1]; // +だけの累積和
  ll all[N+1]; // 全部の累積和
  plus[0] = 0;
  all[0] = 0;
  FOR(i,0,N){
    all[i+1] = all[i] + a[i];
    if(a[i]>0) plus[i+1] = plus[i] + a[i];
    else plus[i+1] = plus[i];
  }
  ll ans = -999999999999999;
  for(int i=0;i+K-1<N;i++){
    int j = i + K - 1;
    ll l = plus[i] - plus[0];
    ll r = plus[N] - plus[j+1];
    ll med = all[j+1] - all[i];
    med = max(0LL, med);
    ans = max(ans, l+med+r);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 2560
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        PrintWriter out = new PrintWriter(System.out);
        Scanner sc = new Scanner(System.in);
        TaskC task = new TaskC();

        task.solve(sc, out);
        out.flush();
        sc.close();
    }
    static class TaskC{
        void solve(Scanner sc, PrintWriter out){
            int N = nint(sc);
            long[] lLine = longLine(sc, N);
            long[] AminusN = new long[N];
            for (int i = 0; i < N; i++) {
                AminusN[i] = lLine[i] - (i+1);
            }
            Arrays.sort(AminusN);
            long b;
            if (N%2==0){
                b = AminusN[N/2 -1];
            }else{
                b = AminusN[(N+1)/2 -1];
            }
            long result = 0;
            for (long a: AminusN){
                result += Math.abs(a - b);
            }
            out.println(result);

        }
    }
    static int nint(Scanner sc){
        return Integer.parseInt(sc.next());
    }
    static long nlong(Scanner sc){
        return Long.parseLong(sc.next());
    }
    static double ndouble(Scanner sc){
        return Double.parseDouble(sc.next());
    }
    static float nfloat(Scanner sc){
        return Float.parseFloat(sc.next());
    }
    static String nstr(Scanner sc){
        return  String.valueOf(sc.next());
    }
    static long[] longLine(Scanner sc, int size){
        long[] lLine = new long[size];
        for (int i = 0; i < size; i++) {
            lLine[i] = nlong(sc);
        }
        return lLine;
    }
    static int[] intLine(Scanner sc, int size){
        int[] iLine = new int[size];
        for (int i = 0; i < size; i++) {
            iLine[i] = nint(sc);
        }
        return iLine;
    }
    static String[] strLine(Scanner sc, int size){
        String[] strLine = new String[size];
        for (int i = 0; i < size; i++) {
            strLine[i] = nstr(sc);
        }
        return strLine;
    }
}

Measured memory_bytes: 49556
#include <bits/stdc++.h>
using namespace std;
int main(){
    int K;
    string S;
    cin >> K >> S;
    if(S.size() <= K) cout << S << endl;
    else{
        cout << S.substr(0, K);
        for(int i = 0; i < 3; i++){
            cout << '.';
        }
        cout << endl;
    }
}

Measured memory_bytes: 3596
#include <stdio.h>

int main(void)
{
	int a, b, c, d;
	
	while (scanf("%d", &a) != EOF) {
		b = 0;
		for (c = 1; c <= a; c++) {
			b += c;
		}
		printf("%d\n", b + 1);
	}
	
	return (0);
}
	

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

int main(){
    long long N;
    cin >> N;
    long long i, i_copy, keta, counta = 0;
    for(i = 1; i <= N; i++){
        i_copy = i;
        keta = 0;
        while(i_copy){
            keta++;
            i_copy /= 10;
        }
        if(keta % 2 == 1) counta++;
    }
    cout << counta << endl;
    system("pause");
    return 0;
}

Measured memory_bytes: 504
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

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








int main(){
    int N, K, mx(0); std::cin >> N >> K;
    std::vector<int> A(N);
    for(int i(0); i < N; ++i){
        std::cin >> A[i];
        chmax(mx, A[i]);
    }
    
    int ng(0), ok(mx);
    while(ok > ng + 1){
        int mid((ng + ok) / 2);
        long long cnt(0);
        for(int i(0); i < N; ++i){
            cnt += (A[i] - 1) / mid;
        }
        if(cnt <= K) ok = mid;
        else ng = mid;
    }
    std::cout << ok << std::endl;
    return 0;
}


Measured memory_bytes: 4052