akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<iomanip>
#include<algorithm>

using namespace std;

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

int main(){
    string s;
    cin >> s;
    int b=0;
    int len = s.length();
    REP(i,len){
        if(s[i-b]=='B'){
            s.erase(s.begin()+i-b);
            b++;
            if(i-b>=0){
                s.erase(s.begin()+i-b);
                b++;
            }
        }
    }
    cout << s;
}

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

using namespace std;
#define INF 1e9

void solve()
{
	int dp[2][101][101];
	int n, m;
	while (cin >> n >> m, n || m)
	{
		for (int i = 0; i < 101; ++i)
		{
			for (int j = 0; j < 101; ++j)
			{
				if (i == j)
				{
					dp[0][i][j] = dp[1][i][j] = 0;
				}
				else
				{
					dp[0][i][j] = dp[1][i][j] = INF;
				}
			}
		}
		int a, b, cost, time;
		for (int i = 0; i < n; ++i)
		{
			cin >> a >> b >> cost >> time;
			dp[0][a][b] = cost;
			dp[0][b][a] = cost;
			dp[1][a][b] = time;
			dp[1][b][a] = time;
		}
		for (int k = 1; k <= m; ++k)
		{
			for (int i = 1; i <= m; ++i)
			{
				for (int j = 1; j <= m; ++j)
				{
					dp[0][i][j] = min(dp[0][i][j], dp[0][i][k] + dp[0][k][j]);
					dp[1][i][j] = min(dp[1][i][j], dp[1][i][k] + dp[1][k][j]);
				}
			}
		}
		int inq;
		cin >> inq;
		for (int i = 0; i < inq; ++i)
		{
			int p, q, r;
			cin >> p >> q >> r;
			cout << dp[r][p][q] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

int main(){
    string a,b;
    cin >> a >> b;
    if (a == "H" && b == "H"){
        cout << "H"<<endl;
    }else if (a == "H" && b == "D"){
        cout << "D" << endl;
    }else if(a == "D" && b == "H"){
        cout<< "D" << endl;
    }else{
        cout << "H" << endl;
    }

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

int main(){
    int n;
    cin >> n;
    vector<int> a(n),b(n);
    rep(i,n){
        cin >> a[i] >> b[i];
    }
    
    int mx = 0;
    rep(ss,n){
        rep(tt,n){
            if(ss==tt)continue;
            int res = 0;
            int p = a[ss]-a[tt];
            int q = b[ss]-b[tt];
            rep(i,n){
                rep(j,n){
                    if(a[i]+p==a[j]&&b[i]+q==b[j]){
                        res++;
                    }
                }
            }
            mx = max(mx,res);
        }
    }
    cout << n-mx << endl;
    return 0;
}

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MAXN 100001
#define MOD 2019

int gcd(int a, int b) {
    if(a < b) swap(a, b);
    if (b==0) return a;
    return gcd(b, a%b);
}
int main() {
    ll l, r;
    cin >> l >> r;
    ll ans = ll(INF);

    ll until = min(ll(2019), r-l) + l+1;

    ll i, k;
    for(i=l; i<until; i++) {
        for(k=i+1; k<until; k++) {
            if(i!=k) {
                ans = min(i*k%MOD, ans);
                if (ans == 0) break;
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
from collections import defaultdict, Counter

H, W, N = map(int, input().split())

D = defaultdict(int)

for _ in range(N):
  a, b = map(int, input().split())
  for i in range(a - 2, a + 1):
    if 1 <= i <= H - 2:
      for j in range(b - 2, b + 1):
        if 1 <= j <= W - 2:
          D[(i, j)] += 1

print((H - 2) * (W - 2) - len(D))

c = Counter(D.values())
for i in range(1, 10):
  print(c.get(i, 0))

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

int main(){
	static int n,m,iti=1,c[100000+2],l[100000+2];
	scanf("%d%d",&n,&m);
	for(int i=0;i<n+1;i++){
		c[i]=0;
	}
	for(int i=1;i<n;i++){
		scanf("%d",&l[i]);
	}
	for(int i=0;i<m;i++){
		int a;
		scanf("%d",&a);
		for(int j=min(iti,iti+a);j<max(iti,iti+a);j++){
			c[j]++;
		}
		iti+=a;
	}
	int ans=0;
	for(int i=1;i<n;i++){
		ans+=l[i]*c[i];
		ans=ans%100000;
	}
	printf("%d\n",ans);
}

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

using namespace std;

const long long MOD = 1000000007;
const long long MAX = 2000000;

template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

template<typename T>
T lcm(T a, T b) {
    return a * b / gcd(a, b);
}


int main() {
   int N;
   cin>>N;
   string S;
   cin>>S;
   int sum=0;
   for(int i=0;i<N-2;i++){
       if(S[i]=='A'&&S[i+1]=='B'&&S[i+2]=='C'){
           sum++;
       }
   }
   cout<<sum<<endl;




}


Measured memory_bytes: 256
#include<iostream>
#include<string>
using namespace std;
int main(){
	string sa,sb,sc;
	int a,b,c;
	a=b=c=0;
	cin>>sa>>sb>>sc;
	char c1=sa[a++];
	while(1)
	{
		if(c1=='a'){
			if(sa.size()==a){
				cout<<'A'<<endl;
				return 0;
			}
			c1=sa[a++];
		}
		else 
		  	if(c1=='b'){
			if(sb.size()==b){
				cout<<'B'<<endl;
				return 0;
			}
			c1=sb[b++];
		}
		else 
		  	if(c1=='c'){
			if(sc.size()==c){
				cout<<'C'<<endl;
				return 0;
			}
			c1=sc[c++];
		}
	}
	return 0;
}

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

int main() {
  int N, Y;
  cin >> N >> Y;
  int res10000 = -1, res5000 = -1, res1000 = -1;
  for (int a = 0; a <= N; ++a) {
      for (int b = 0; b + a <= N; ++b) {
          int c = N - a - b;
          int total = 10000*a + 5000*b + 1000*c;
          if (total == Y) {
              res10000 = a;
              res5000 = b;
              res1000 = c;
          }
      }
  }

  cout << res10000 << " " << res5000 << " " << res1000 << endl;

  return 0;
}

Measured memory_bytes: 3632
#include<stdio.h>

int main(){
	char s[5];
	scanf("%s",s);
	if(s[0]==s[1]){
		puts("Bad");
	}else if(s[1]==s[2]){
		puts("Bad");
	}else if(s[2]==s[3]){
		puts("Bad");
	}else puts("Good");
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#include <math.h>
#define ALL(a)    a.begin(), a.end() 
#define rep(i, K, N)  for(long long int i = K; i <= N; i++)
#define ll long long int
using namespace std;

vector<ll> V;

void prime_factor(ll N){

bool C = 1;

while(C){

C = 0;

    rep(i, 2, sqrt(N)){

    if(N%i == 0){V.push_back(i);N /= i;C = 1;break;}
   
    }

}

V.push_back(N);

}

int main() {
ll N, M, Ans = 1;cin >> N >> M;
map<ll, ll> P;

prime_factor(M);

for(auto v : V )P[v]++;

for(auto x : P ){
  Ans *= powl(x.first, (x.second/N));
}
cout << Ans << endl;
}

Measured memory_bytes: 3620
#include<bits/stdc++.h>
#define N 41
using namespace std;
typedef long long ll;
ll n,m,a,b,p,q,d;
vector<ll> v[2],x[2];

int main(){
  cin>>n>>m>>a>>b>>p>>q;
  ll ti=min(n,m/(p+q));
  ll ans=m;
  if(a==1&&b==1)
    ans=min(ans,min(m-ti*(p+q),
		    abs(min(ti+1,n)*(p+q)-m)));
  ll A=1,B=1,T=min((ll)N,n);
  for(int i=0;i<T;i++){
    d=p*A+q*B;
    if(i<T/2)v[0].push_back(d);
    else v[1].push_back(d);
    if(d>m)break;
    A*=a,B*=b;
  }
  int len[2]={v[0].size(),v[1].size()};
  for(int i=0;i<2;i++){
    v[i].push_back(0);
    for(int j=0;j<(1<<len[i]);j++){
      ll sum=0;
      for(int k=0;k<len[i];k++)
	if((j&(1<<k)))sum+=v[i][k];
      x[i].push_back(sum);
    }
    sort(x[i].begin(),x[i].end());
  }
  for(int i=0;i<x[0].size();i++){
    ll S=m-x[0][i];
    vector<ll>::iterator r=lower_bound(x[1].begin(),x[1].end(),S);
    if(r!=x[1].end())ans=min(ans,(*r)-S);
    if(r!=x[1].begin())ans=min(ans,S-(*(r-1)));
  }
  cout<<ans<<endl;
  return 0;
}

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

#define ll long long 
#define pll pair<ll,ll> 

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	ll a,b,c;
	cin>>a>>b>>c;
	if(a<=b*c)
		cout<<"Yes";
	else cout<<"No";
	cout<<endl;
}


Measured memory_bytes: 3660
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
int main() {
  int N,K;
  int total = 1;
  cin >> N >> K;
  rep(i, N) {
    if(total <= K){
      total += total;
    }else{
      total += K;
    }
  }
  cout << total;
}

Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
#define N 3100
#define mod 1000000007
int n,m,ans;
int f[N][N][2];
int main()
{
	//freopen("tt.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i=0;i<=n;i++)
		f[0][i][0]=1;
	for(int i=0;i<m;i++)
		for(int j=0;j<=n;j++)
		{
			int k=n-j;
			if(j>=1)
			{
				//jj
				(f[i+1][j-1][j==1]+=f[i][j][0])%=mod;
		 		(f[i+1][j-1][1]+=f[i][j][1])%=mod;
				//jk
				(f[i+1][j][j==1]+=f[i][j][0])%=mod;
				(f[i+1][j][1]+=f[i][j][1])%=mod;
			}
			if(k>=1)
			{
				//kj
				(f[i+1][j][j==0]+=f[i][j][0])%=mod;
				(f[i+1][j][1]+=f[i][j][1])%=mod;
				//kk
				(f[i+1][j+1][j==0]+=f[i][j][0])%=mod;
				(f[i+1][j+1][1]+=f[i][j][1])%=mod;
			}
		}
	for(int i=0;i<=n;i++)
		(ans+=f[m][i][1])%=mod;
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 73984
n, k = map(int, input().split())
lis = list(map(int, input().split()))
rt = [-1]*n
cnt = 0
idx = 0
rt[idx] = cnt
while True:
    if cnt==k:
        print(idx+1)
        exit()
    idx = lis[idx] - 1
    cnt += 1
    if rt[idx] == -1:
        rt[idx]=cnt
    else:
        can_jump=(k-cnt)//(cnt-rt[idx])
        if can_jump:
            cnt+=can_jump*(cnt-rt[idx])


Measured memory_bytes: 32372
# ABC096 C - Grid Repainting 2
h, w = map(int,(input().split()))
s= [[0]*(w+2)]*(h+2)
ans= 'Yes'
for i in range(1,h+1):
        s[i] = [0] + list(input()) + [0]

for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '#':
            if s[i+1][j]=='#' or s[i-1][j]=='#' or s[i][j+1]=='#' or s[i][j-1]=='#':
                pass
            else:
                ans='No'
                break

print(ans)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
long long sum=0;
for(int i=0;i<k;i++)
{
int num;
cin>>num;
sum+=num;
}
if(sum>=n)
cout<<"Yes\n";
else
cout<<"No\n";
return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
    #include <math.h>
 
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    # define M_PI  3.14159265358979323846
 
 
    const int M=1e9+7;
    long long mod(long long x){
        return ((x%M + M)%M);
    }
    long long add(long long a, long long b){
        return mod(mod(a)+mod(b));
    }
    void solve(){
        int n;
        cin>>n;
        double k;
        cin>>k;
        int total=0;
        for(int i=0;i<n;i++){
            long long x,y;
            cin>>x>>y;
            double d=(double)sqrt((x*x)+(y*y));
            if(d<=k)
                total++;
        }
        cout<<total;
    }
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cout<<fixed;
        cout<<setprecision(10);
//        freopen("timber_input.txt", "r", stdin);
//        freopen("timber_output.txt", "w", stdout);
        int t=1;
//        cin>>t;
        for(int i=1;i<=t;i++){
//            cout<<"Case #"<<i<<": ";
            solve();
        }
        return 0;
    }
 

Measured memory_bytes: 3804
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
int main(){
    int n;
    cin>>n;
    if(n==1){
        cout<<"Hello World"<<endl;
    }
    else if(n==2){
        int a,b;
        cin>>a>>b;
        cout<<a+b<<endl;
    }
    return 0;
}

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

int main() {
    cout << fixed << setprecision(10);
    int N; cin >> N;
    vector<int> ord(N);
    for (int i = 0; i < N; i++) ord[i] = i;
    vector<int> x(N), y(N);
    for (int i = 0; i < N; i++) cin >> x[i] >> y[i];

    double ans = 0;
    int cnt = 0;
    do {
        double sum = 0;
        for (int i = 0; i < N - 1; i++) {
            sum += sqrt(pow(x[ord[i]] - x[ord[i + 1]], 2) + pow(y[ord[i]] - y[ord[i + 1]], 2));
        }
        ans += sum;
        cnt++;
    } while(next_permutation(ord.begin(), ord.end()));

    cout << ans / cnt << endl;
}


Measured memory_bytes: 256
N = int(input())
a = [int(w) for w in input().split()]
if len(a) != N: raise

def factorizeByTwo(n):
    e = 0
    while n > 0 and n % 2 == 0:
        n //= 2
        e += 1
    return e

print(sum(map(factorizeByTwo, a)))


Measured memory_bytes: 4148
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define rep2(i,a,n) for(int i=(a); i<(n); i++)
#define all(vec) vec.begin(),vec.end()
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

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

Measured memory_bytes: 3612
#include <stdio.h>
#include <vector>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
 
#define rep(i,n) for (int i=0;i<(n);++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef pair<int, int> pi;


int main() {
  int n, k;
  cin >> n >> k;
  vi h(n);
  rep(i, n) cin >> h[i];

  sort(h.begin(), h.end());

  ll ans = 0;
  rep(i, n-k) ans += h[i];
  cout << ans << endl;
  return 0;
}

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

int main() {
    int n,sum=0,temp;
    cin>>n;
    vector<int> p(n);
    rep(i,n) {
        cin>>p[i];
        sum+=p[i];
    }
    sort(p.begin(),p.end());
    temp=p.back()/2;
    sum-=temp;
    cout<<sum<<endl;
}


Measured memory_bytes: 256
a,b,c,x,y=map(int,input().split())
ans=0
if a+b<=c*2:
    print(a*x+b*y)
    exit()
elif x>=y:
    ans+=c*y*2
    x-=y
    if a>=c*2:
        ans+=c*2*x
    else:
        ans+=a*x
elif y>x:
    ans+=c*x*2
    y-=x
    if b>=c*2:
        ans+=c*2*y
    else:
        ans+=b*y
print(ans)


Measured memory_bytes: 38336
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
#include<string>
#include<stdio.h>
#include<math.h>
#include<unordered_map>
#include<map>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

mat com;
ll MOD=1000000007;

ll C(ll a, ll b)
{
	if (b < 0)return 0;
	if (a < b)return 0;
	if (a == b)return 1;
	if (b == 0)return 1;
	if (com[a][b] != -1)return com[a][b];
	return com[a][b] = (C(a - 1, b - 1) + C(a - 1, b)) % MOD;
}

int main()
{
	int N, K; cin >> N >> K;
	com = mat(2*N, vec(2*N, -1));

	for (int i = 1; i <= K; ++i)
	{
		//cout << C(K - 1, i-1) << endl;
		//cout << C(N - K + 1, i) << endl;
		cout << C(K - 1, i-1)*C(N - K + 1, i) % MOD << endl;
	}

	return 0;
}

Measured memory_bytes: 125568
import java.util.Scanner;
public class Main {
public static void main(String[]srgs) {
	Scanner scan=new Scanner(System.in);
	int a=scan.nextInt();
	String str1=scan.next();
	String str2=scan.next();
	int len=str1.length()<str2.length()?str1.length():str2.length();
	int lon=0;
	for(int i=0;i<len;i++) {
		if(str1.substring(i,str1.length()).equals(str2.substring(0,str2.length()-i))){
			lon=len-i;
			break;
		}
	}
	System.out.println(str1.length()+str2.length()-lon);
	}
}

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

int main () {
	int n;
  	cin >> n;
  	map<int,int> m;
  	for(int i = 1;i<=n;i++) m[i] = 0;
  	for(int i=0;i<n-1;i++) {
    	int a;
      	cin >> a;
      	m[a]++;
    }
  	for (auto i : m ) cout << i.second << endl;
}

Measured memory_bytes: 12972
#include <bits/stdc++.h>
using namespace std;
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")";}

// #define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif
typedef long long ll;
typedef double ld;
#define pll pair<ll,ll>
#define pii pair<int, int>
#define pb push_back
#define mp(x, y) make_pair((x), (y))
#define F first
#define S second
#define I insert
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()
   
const ll Mod = 1e9 + 7;
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
    string s;
    cin >> s;
    map<char , int> st;
    for(int i = 0 ;i < 4 ; i++)
        st[s[i]]++;
    bool q = true;
    for(auto it : st){
        if(it.S != 2)
            q = false;
    }
    if(q)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
    int INF = 100000000;
    int v[10] = {0,2,5,5,4,5,6,3,7,6};
    int b[9] = {1,7,4,5,3,2,9,6,8};
    int N,M;
    cin >> N >> M;
    vector<int> A(M);
    for (int i=0;i<M;i++) {
        cin >> A[i];
    }
    vector<int> dp(N+1, -INF);
    dp[0] = 0;
    for (auto bb : b) {
        if (find(A.begin(), A.end(), bb)!=A.end()) {
            int va = v[bb];
            for (int n=0;n<=N;n++) {
                if (n-va>=0) {
                    dp[n] = max(dp[n], dp[n-va] + 1);
                }
            }
        }
    }
    int n = N;
    string ans;
    for (int i=9;i>0;i--) {
        if (find(A.begin(), A.end(), i)!=A.end()) {
            while(n-v[i]>=0 && dp[n-v[i]]==dp[n]-1) {
                ans += (char)('0'+i);
                n -= v[i];
            }
        }
    }
    cout << ans << '\n';
}

Measured memory_bytes: 384
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
    int a,b,c,d1,d2;
    scanf("%d%d%d",&a,&b,&c);
    d1=abs(a-b);
    d2=abs(a-c);
    if(d1>=d2)
        printf("B\n");
    else
        printf("A\n");
    return 0;
}


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

int main(){
  int N,T;
  cin>>N>>T;
  std::vector<int> v(N+1);
  for(int i=0;i<N;i++)cin>>v[i];
  v[N]=0;
  int m=v[0];
  int k=0;
  int h=0;
  int i=0;
  while(v[i]>=v[i+1])i++;
  m=v[i];
  int M=v[i];
  for(i=i+1;i<N+1;i++){
    M=max(M,v[i]);
    if(m>v[i]){
      if(h==M-m)k++;
      if(h<M-m){
        k=1;
        h=M-m;
      }
      m=v[i];
      M=v[i];
    }
  }
  cout<<k<<endl;
}


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

int main(void)
{
  int N;
  std::cin >> N;
  std::vector<std::string>Name;
  for(int i = 0;i < N;i++)
  {
    std::string n;
    std::cin >> n;
    Name.push_back(n);
  }
  
  long long m=0,a=0,r=0,c=0,h=0;
  for(int i=0;i<N;i++)
  {
    if(Name[i][0] == 'M')
    {
      m++;
    }
    else if(Name[i][0] == 'A')
    {
      a++;
    }
    else if(Name[i][0] == 'R')
    {
      r++;
    }
    else if(Name[i][0] == 'C')
    {
      c++;
    }
    else if(Name[i][0] == 'H')
    {
      h++;
    }
  }
  
  long long answer;
  answer = m*a*r+m*a*c+m*a*h+m*r*c+m*r*h+m*c*h+a*r*c+a*r*h+r*c*h+a*c*h;
  
  std::cout << answer << std::endl;
  
  
  return 0;
}


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

int main() {
  int A,B;
  cin>>A>>B;
  if(A+B>=A-B&&A+B>=A*B){
    cout<<A+B<< endl;
    return 0;
  }
  if(A-B>=A+B&&A-B>=A*B){
    cout<<A-B<< endl;
    return 0;
  }
  if(A*B>=A+B&&A*B>=A-B){
    cout<<A*B<< endl;
  }
}


Measured memory_bytes: 256
def F(a, b):
    def _(n):
        c = 0
        while n:
            n //= 10
            c += 1
        return c

    return _(max(a, b))


N = int(input())
ans = float('inf')
for a in range(1, N + 1):
    b = N // a
    if a > b:
        break
    if b != N / a:
        continue
    ans = min(ans, F(a, b))
print(ans)


Measured memory_bytes: 3060
import sys


# import math
# import heapq

# input = sys.stdin.readline
# import defaultdict


def main():
    N = int(input())
    ans = 1
    q = [1] *(N+1)
    for i in range(1, N + 1):
        k = i
        for j in range(2, i + 1):
            while k % j == 0:
                k /= j
                q[j] += 1

    for i in range(N + 1):
        ans *= q[i]
        ans %= 10 ** 9 + 7
    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 73836
#include<bits/stdc++.h>
using namespace std;
 
char field[10][10];
bool row[10],column[10];
vector<int>order;
bool endFlag;
 
 
//check whether there exists a queen in a row, column, dpos, dneg.
bool check(){
    
    //行についてqueenの判定
    for(int i=0;i<8;i++){ 
        int cnt=0;
        
        //ある行に存在するqueenの数を数えている
        for(int j=0;j<8;j++)if(field[i][j]=='Q')cnt++; 
        
        //ある行に２つ以上queenが存在したらアウト
        if(cnt>1)return false; 
    }
    
    //列についてqueenの判定
    for(int j=0;j<8;j++){
        
        int cnt=0; 
        
        //ある列に存在するqueenの数を数えている
        for(int i=0;i<8;i++)if(field[i][j]=='Q')cnt++; 
        
        //ある列に２つ以上queenが存在したらアウト
        if(cnt>1)return false; 
    }
    
    //左上から右下への対角線より左側の領域について判定
    for(int i=0;i<8;i++){
        
        int cnt=0;
        
        for(int j=0;j<8-i;j++){ 
            if(field[i+j][j]=='Q')cnt++; 
        }
        
        //左斜に２つ以上queenが存在したらアウト
        if(cnt>1)return false; 
    }
    
    //左上から右下への対角線より右側の領域について判定
    for(int j=0;j<8;j++){
        
        int cnt=0;
        
        for(int i=0;i<8-j;i++){ 
            if(field[i][i+j]=='Q')cnt++;
        }
        
        //左斜に２つ以上queenが存在したらアウト
        if(cnt>1)return false; 
    }
    
    //右上から左下への対角線より右側の領域について判定
    for(int i=0;i<8;i++){
        
        int cnt=0;
        
        for(int j=0;j<8-i;j++){ 
            if(field[i+j][7-j]=='Q')cnt++;
        }
        
        //右斜に２つ以上queenが存在したらアウト
        if(cnt>1)return false; 
    }
    
    //右上から左下への対角線より左側の領域について判定
    for(int j=0;j<8;j++){
        
        int cnt=0;
        
        for(int i=0;i<=j;i++){ 
            if(field[i][j-i]=='Q')cnt++;
        }
        
        //右斜に２つ以上queenが存在したらアウト
        if(cnt>1)return false; 
    }
    
    //チェックを通過するならば,１つのqueenの配置に成功している
    return true;
}
 
void dfs(int t){
    
    if(endFlag)return; 
    
    if(t==8){
        if(check()){
            
            endFlag=true;
            
            for(int i=0;i<8;i++){
                for (int j = 0; j < 8; j++) cout << field[i][j];
                cout << endl;
            }
            
        }
        return;
    }
 
    if(row[t])dfs(t+1); //その行にqueenが配置済みならば,その行に新規のqueenを配置できない 
    else{ 
        //その行にはqueenがいない
    
        for(int i=0;i<8;i++){
            
            //その列にqueenが存在するならば,新規のqueenは配置できない 次へ行く
            if(column[i])continue; 
            
            column[i]=true; //その列にqueenを配置済みにする
            field[t][i]='Q'; //queenをあるマスに置いてみる
            dfs(t+1); //次の行の探索へ移動
            field[t][i]='.'; //探索に失敗したときは, queenを仮置したマスをもとに戻す
            column[i]=false; //列にはqueenがいない状態に戻す
        }
    }
    
}
 
int main(){
    int n;
    cin >> n;
    
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 8; j++) field[i][j] = '.';
    }
    
    for (int i = 0; i < n; i++){
        int x, y;
        cin >> x >> y;
        field[x][y] = 'Q';
        column[y] = true;
        row[x] = true;
    }
 
    dfs(0);
    return 0;
}


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

#define int long long
const int _ = 2e5 + 7;
int N , T , dp[_][50]; struct thing{int A , B;}now[_];

signed main(){
	cin >> N >> T; memset(dp , 0x3f , sizeof(dp)); dp[0][0] = 1;
	for(int i = 1 ; i <= N ; ++i){cin >> now[i].A >> now[i].B; ++now[i].B;}
	sort(now + 1 , now + N + 1 , [&](thing p , thing q){return p.B * q.A < p.A * q.B;});
	for(int i = 1 ; i <= N ; ++i)
		if(now[i].A == 0){
			sort(now + i , now + N + 1 , [&](thing p , thing q){return p.B < q.B;});
			int mx = 0;
			for(int j = 0 ; j < 50 ; ++j)
				if(dp[i - 1][j] <= T + 1){
					int pos = i , sum = dp[i - 1][j]; while(pos <= N && (sum += now[pos].B) <= T + 1) ++pos;
					mx = max(mx , j + pos - i);
				}
			cout << mx << endl; return 0;
		}
		else{
			dp[i][0] = dp[i - 1][0];
			for(int j = 1 ; j < 50 ; ++j)
				if(dp[i - 1][j - 1] <= T) dp[i][j] = min(dp[i - 1][j] , dp[i - 1][j - 1] * (now[i].A + 1) + now[i].B);
				else dp[i][j] = dp[i - 1][j];
		}
	int mx = 0; while(dp[N][mx] <= T + 1) ++mx;
	cout << --mx << endl;
	return 0;
}


Measured memory_bytes: 81536
lim = 1000000
while true do
  i=0
  h = Hash.new
  n=STDIN.gets.to_i
  break  if n ==0
#h[id] = [jyun,uri]
  0.upto(n-1){|jun|
    id, tanka, ryou = gets.chomp.split.map{|x| x.to_i}
    if h.has_key?(id) then
      h[id][1] += tanka*ryou
    else
      h[id] = [jun, tanka*ryou]
    end
  }
#[[id,[jun,uriage]],...]
  f = h.to_a.sort{|a,b| a[1][0]<=>b[1][0]}.select{|x|x[1][1] >= lim}.map{|x| x[0]}
  puts f.length==0 ? "NA" : f
end

Measured memory_bytes: 8784
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
typedef long double lb;
typedef pair<int,int> P;
const int MOD = 1000000007;
const ll INF = 1e16;
const double EPS = 1e-10;
const double PI = 3.141592653589793;
template<class T> inline bool chmin(T& a, T b){if(a>b){a = b;return 1;}return 0;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a = b;return 1;}return 0;}
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 
#define int long long
#define double long double

////////////////////////////////////////////////////////////////////
ll modpow(ll a,  ll n, const ll &MOD = MOD){
  ll ret = n == 0 ? 1 : modpow(a, n/2, MOD);
  (ret *= ret) %= MOD;
  if(n%2)((ret *= a) %= MOD);
  return ret;
}
///////////////////////___modinv___////////////////////
ll modinv(ll d, const ll &MOD = MOD){
  return modpow(d, MOD-2, MOD);
}
///////////////////////___prime_factor___////////////////////
map<int, int> prime_factor(int n){
  map<int, int> ret;
  for(int i = 2;i*i <= n;i++){
    while(n % i == 0){
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1){
    ret[n] = 1;
  }
  return ret;
}

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

int n,x,a[2100],vec[2100];
signed main(){
  cin>>n>>x;
  rep(i,n)cin>>a[n-i-1];
  rep(i,n){
    int x = INF;
    rep(j,n+1){
      int ind = (i-j+n)%n;
      chmin(x, a[ind]);
      vec[j] += x;
    }
  }
  int ans = INF;
  rep(i,n+1){
    chmin(ans, vec[i]+x*i);
  }
  cout << ans << endl;
}




Measured memory_bytes: 256
integer :: n,m,p,x
integer,allocatable :: t(:)
integer :: i
integer :: total

read *,n
allocate(t(n))
read *,t
read *,m
total = sum(t)
do i = 1, m
   read *,p,x
   print '(i0)',total - t(p) + x
enddo
end

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;

namespace ABC120D
{
    class MainClass
    {

        static int[] nm;
       
        public static void Main(string[] args)
        {
            nm = Array.ConvertAll(Console.ReadLine().Split().ToArray(), int.Parse);
            var ablst = new List<int[]>();
            for(int i = 0; i < nm[1]; i++)
            {
                int[] ab = Array.ConvertAll(Console.ReadLine().Split().ToArray(), int.Parse);
                ablst.Add(ab);
            }

            UnionFind uni = new UnionFind(nm[0]);

            long fuben = ((long)nm[0] * ((long)nm[0] - 1)) / 2;
            var anslst = new List<long>();
            for(int i = ablst.Count - 1; i >= 0; i--)
            {
                anslst.Add(fuben);
                int a = ablst[i][0]-1;
                int b = ablst[i][1]-1;
                long asize = uni.Size(a);
                long bsize = uni.Size(b);
                if (uni.Connect(a, b))
                {
                    //Console.WriteLine("a " + a + " b " + b +" size(a) : " + uni.Size(a) + " size(b) : " + uni.Size(b));
                    fuben -= asize * bsize;
                }
            }

            anslst.Reverse();
            anslst.ForEach(Console.WriteLine);

        }
    }

    class UnionFind
    {
        public List<int> Parent;
        public UnionFind(int n)
        {
            //親の番号をそれぞれ格納する。自分が親なら、-(サイズ)
            this.Parent = Enumerable.Repeat(-1, n).ToList();
        }

        public int Size(int a)
        {
            if (Parent[a] < 0)return -Parent[a];
            else return -Parent[Root(a)];
        }

        public int Root(int a)
        {
            if (Parent[a] < 0) return a;
            else
            {
                int root = Root(Parent[a]);
                Parent[a] = root;
                return root;
            }
        }

        public bool Connect(int a, int b)
        {
            a = Root(a);
            b = Root(b);
            if (a == b) return false;
            else
            {
                //a > bにする
                if (a < b)
                {
                    int tmp = a;
                    a = b;
                    b = tmp;
                }
                //bの親をaにして、aのsizeにbのぶんを足す
                Parent[a] += Parent[b];
                Parent[b] = a;
                return true;
            }
        }
    }
}

Measured memory_bytes: 28864
N = int(input())
l = list(map(int,input().split()))
l_a = []
ans = 0
for i in range(N-1):
    if l[i] >= l[i+1]:
        ans += 1
    else:
        l_a.append(ans)
        ans = 0
l_a.append(ans)
print(max(l_a))


Measured memory_bytes: 63984
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; ++i)
#define MOD 1000000007
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<class T> inline bool chmin(T& a, T b) { if(a>b) {a=b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) { if(a<b) {a=b; return true;} return false;}

int main() {
  string S; cin >> S;
  if(S.size() >= 3) {
    rep(i, S.size()-2) {
      if(S[i] == S[i+1] || S[i] == S[i+2] || S[i+1] == S[i+2]) {
        cout << i+1 << " " << i+3 << endl;
        return 0;
      }
    }
  } else {
    if(S[0] == S[1]) {
      cout << "1 2" << endl;
      return 0;
    }
  }
  cout << "-1 -1" << endl;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
    for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
    for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < v.size(); ++i) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    if (v.size() == 0) return os;
    for (int i = 0; i < v.size() - 1; ++i) {
        os << v[i] << " ";
    }
    os << v[v.size() - 1];
    return os;
}

void solve() {
    int n;
    cin >> n;
    ll ans = 0;
    FOR(i, 1, n) {
        int a = i;
        int b = (n - 1) / a;
        if (a >= 1 && b >= 1) {
            ans += b;
        }
    }
    cout << ans << endl;
}


int main() {
    solve();
    char tmp;
    while (cin >> tmp) {
        cin.putback(tmp);
        solve();
    }
}



Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[105];
    int main(){
    	ll n,m,x=0,y=0; cin>>n>>m;
    	for (int i=1;i<=m;i++){
    		cin>>a[i];
    		if (a[i]&1){
    			if (!x)     x=i;
    			else if(!y) y=i;
    			else return cout<<"Impossible\n",0;
    		}
    	}
    	if (x) swap(a[1],a[x]);
    	if (y) swap(a[y],a[m]);
    	for(int i=1;i<=m;i++) cout<<a[i]<<' ';
    	m+=(m==1);
    	a[1]--, a[m]++;
    	int j=(a[1]?1:2);
    	cout<<endl<<' '<<m-j+1<<endl;
    	for (int i=j;i<=m;i++) cout<<a[i]<<' ';
		return 0;
    }


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
using namespace std;
using vi = vector<int>;
using vl = vector<long long int>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using ll = long long int;

int main(){
    long double W, H, x, y; cin >> W >> H >> x >> y;
    cout << setprecision(10) << (W*H)/2.0 << " ";
    if(x*2 == W&&y*2==H){
        cout << 1;
    }
    else{
        cout << 0;
    }
}

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

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

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    bool f = true;
    REP(i, n) {
        int a;
        cin >> a;
        if(a % 2 != 0) f = false;
    }
    if(f) cout << "second" << endl;
    else cout << "first" << endl;

    return 0;
}

Measured memory_bytes: 256
//#include "bits/stdc++.h"
#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <complex>
#include <bitset>
#include <functional>
#include <stack>
#include <regex>
#include <tuple>
#include <iomanip>

#define int long long
#define REP(i,l,r) REPEAT(i,l,r,true) // [l, r)
#define rep(i,n) REP(i,0,n)           // [0, n)
#define REPEAT(i,l,r,condition) for(int i=(condition)?l:r-1;(condition)?i<r:i>=l;(condition)?++i:--i) // false<-[l, r)->true
#define all(e) e.begin(),e.end()
#define rall(e) e.rbegin(),e.rend()
#define pb push_back
#define fs first
#define sc second
#define show(...) cerr<<#__VA_ARGS__<<" = ";_DEBUG(__VA_ARGS__)
#define showlr(n,l,r) cerr<<#n<<" = ";for(int i=l;i<r;i++){cerr<<n[i]<<", ";}cerr<<endl // [l, r)

#define YN(condition) puts((condition)?"YES":"NO")
#define Yn(condition) puts((condition)?"Yes":"No")
#define yn(condition) puts((condition)?"yes":"no")
#define YES puts("YES")
#define Yes puts("Yes")
#define yes puts("yes")
#define NO  puts("NO")
#define No  puts("No")
#define no  puts("no")

#define case(i) printf("Case #%lld: ",i)

using namespace std;

using vi=vector<int>;
using pint=pair<int,int>;

struct io{io(){cin.tie(0);ios::sync_with_stdio(false);cout.tie(0);cout<<fixed<<setprecision(20);}}io;

template<class T> istream& operator >>(istream &is, vector<T> &v){for(T &e:v)is>>e;return is;}
template<class T> ostream& operator <<(ostream &os, vector<T> v){os<<"{";for(T &e:v)os<<e<<(v.size()-(int)(&e-&v[0])>1?", ":"");os<<"}";return os;}

void _DEBUG(){}
template<typename H,typename... T> void _DEBUG(H a,T...b){cerr<<a<<(sizeof...(b)?",":"\n");_DEBUG(b...);}

inline void in(){}
template<typename H,typename... T>void in(H &a, T&... b){cin>>a;in(b...);}
inline void out(){}
template<typename H,typename... T> void out(H a, T... b){cout<<a<<(sizeof...(b)?" ":"\n");out(b...);}

template<class T> void resz(int n,T& v){v.resize(n);}
template<class H,class... T> void resz(int n,H& a,T&... b){a.resize(n);resz(n,b...);}

template<typename V,typename H> void resize(vector<V>& v,const H a){v.resize(a);}
template<typename V,typename H,typename... T> void resize(vector<V>& v,const H& a,const T... b){v.resize(a);for(auto &v:v) resize(v,b...);}

const int INF=1LL<<55;
const int MOD=1000000007;
const double EPS=1e-8;

int N,W;
vi w,v;
vector<vi> dp;

signed main(){

    in(N,W);
    resize(dp,N+10,W+10);
    resz(N,w,v);
    rep(i,N){
        in(w[i],v[i]);
    }

    rep(i,N){
        rep(j,W+1){
            if(j<w[i]){
                dp[i+1][j]=dp[i][j];
            }else{
                dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
            }
        }
    }
    out(dp[N][W]);

    return 0;
}


Measured memory_bytes: 86528
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);

        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();

        sc.close();

        boolean flg = false;

        if (a + b == c || a + c == b || b + c == a) {
            flg = true;
        }

        System.out.println(flg ? "Yes" : "No");

    }
}

Measured memory_bytes: 21972
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
    const string Yes = "Yes";
    const string No = "No";
    const long Mod = 1000000007;

    static void Main(string[] args) {
#if DEBUG
        MyDebugger.Test(process, "../TextFile1.txt");
#else
        object ret = process(new StreamReader(Console.OpenStandardInput()));
        Console.WriteLine(ret);
#endif
    }

    static object process(TextReader input) {
        var s = input.ReadLine().ToCharArray();
        var t = input.ReadLine().ToCharArray();

        Array.Sort(s);
        Array.Sort(t, (x,y)=>y-x);

        for(int i=0; i<Math.Min(s.Length, t.Length); i++) {
            if (s[i] < t[i]) return Yes;
            if (s[i] > t[i]) return No;
        }

        return s.Length < t.Length ? Yes : No;
    }

    static int[] ToIntArray(string strs, int n) {
        var ret = new int[n];
        var spl = strs.Split(' ');
        for (int i = 0; i < n; i++)
            ret[i] = int.Parse(spl[i]);
        return ret;
    }
}


Measured memory_bytes: 11732
#include <bits/stdc++.h>
using namespace std;
int main(){
	string s;
  	cin >> s;
  	if(s == "Sunny"){
    	cout << "Cloudy";
    }
  else if(s == "Cloudy"){
  		cout << "Rainy";
  }
  else{
  	cout << "Sunny";
  }
  return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}
const int dy[] = {0, 1, 0, -1};
const int dx[] = {1, 0, -1, 0};

int N;
int D[511][511];
bool use[511][511];

bool in(int y, int x) {
    return 0 <= y && y < N && 0 <= x && x < N;
}

void MAIN() {
    scanf("%d", &N);
    REP (i, N) REP (j, N) {
	D[i][j] = min({i+1, j+1, N-i, N-j});
    }

    LL ans = 0;
    REP ($, N*N) {
	int p;
	scanf("%d", &p);
	p--;
	int y = p / N, x = p % N;

	use[y][x] = true;
	ans += D[y][x] - 1;
	int m = D[y][x];

	REP (d, 4) {
	    int yy = y + dy[d];
	    int xx = x + dx[d];
	    if (in(yy, xx)) amin(m, D[yy][xx]);
	    else { m = 0; break; }
	}

	if (D[y][x] > m) {
	    D[y][x] = m;
	    VI cur, nxt;
	    cur.push_back(y*1024 + x);
	    while (cur.size()) {
		while (cur.size()) {
		    y = cur.back() / 1024;
		    x = cur.back() % 1024;
		    cur.pop_back();
		    REP (d, 4) {
			int yy = y + dy[d];
			int xx = x + dx[d];
			if (in(yy, xx)) {
			    if (use[yy][xx]) {
				if (D[yy][xx] > D[y][x]) {
				    D[yy][xx] = D[y][x];
				    cur.push_back(yy*1024 + xx);
				}
			    } else {
				if (D[yy][xx] > D[y][x] + 1) {
				    D[yy][xx] = D[y][x] + 1;
				    nxt.push_back(yy*1024 + xx);
				}
			    }
			}
		    }
		}
		cur.clear();
		swap(cur, nxt);
	    }
	}

    }

    printf("%lld\n", ans);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



Measured memory_bytes: 5496
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]

sAB = sorted(AB, key=lambda x: x[1])

cnt = 0
for itr, ab in enumerate(sAB):
  a, b = ab
  if itr == 0:
    x = b -1
    cnt += 1
  else:
    if a > x:
      x = b -1
      cnt += 1
print(cnt)
    

Measured memory_bytes: 29848
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define INF (ll)(1e18)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }




const int N = 2 * 82 * 82;
int dp[81][81][N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int h, w;
    cin >> h >> w;
    int a[h][w][2];
    rep(k, 2) rep(i, h) rep(j, w) cin >> a[i][j][k];
    
    int off = 81 * 81;
    rep(k, 2) {
        dp[0][0][off + a[0][0][k] - a[0][0][k ^ 1]] = 1;
    }
    
    rep(i, h) rep(j, w) rep(k, N) {
        if (i == 0 && j == 0) continue;
        
        // dp[i][j]を作る
        if ((j && dp[i][j - 1][k]) || (i && dp[i - 1][j][k])) {
            rep(l, 2) {
                dp[i][j][k + a[i][j][l] - a[i][j][l ^ 1]] = 1;
            }
        }
    }
    
    int ans = inf;
    rep(k, N) {
        if (dp[h - 1][w - 1][k]) {
            chmin(ans, abs(off - k));
        }
    }
    cout << ans << endl;
    
    /*
    rep(i, h) rep(j, w) {
        cout << i << " " << j << endl;
        rep3(k, off - 10, off + 11) cout << dp[i][j][k] << " "; cout << endl;
    }
    */
}


Measured memory_bytes: 340864
#include<bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define double long double
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll inf=1e9+7;
const ll mod=998244353;
int main(){
ll a,b;cin>>a>>b;
cout<<max(0LL,a-2*b)<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FOR0(i,n) for(i=0;i<n;i++)
#define FOR(i,j,n) for(i=j;i<n;i++)
#define FORD(i,j,k) for(i=j;i>=k;i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 1000000000
#define ninf -1000000000
#define endl '\n' 
#define she_told_me_dont_worry ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define MOD 1000000007
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long int b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< ii > vii;
ll fast_exp(ll base, ll exp)
{
  ll res=1;
  while(exp>0)
    {
      if(exp%2==1) res=(res*base)%MOD;
      base=(base*base)%MOD;
      exp/=2;
    }
  return res%MOD;
}

ll getInverse(ll n)
{
  return fast_exp(n, MOD-2) ;
}

ll divide(ll a, ll b)
{
  a=a%MOD ;
  b=b%MOD ;
  return (a*(getInverse(b)%MOD))%MOD ;
}
ll cnt[1000005] = {};
int main()
{
    she_told_me_dont_worry
    ll n;
    cin>>n;
    ll a[n+5];
    ll i;
    FOR0(i, n)
    {
        cin>>a[i];
        ll cur = a[i];
        ll j = 2;
        ll cv = 0;
        while( j*j <= cur)
        {
            cv = 0;
            while(cur%j == 0)
            {
                cv++;
                cur /= j;
            }
            cnt[j] = MAX2(cnt[j], cv);
            j++;
        }
        if(cur > 1){
            cnt[cur] = MAX2(cnt[cur], 1);
        }
    }
    ll lcm = 1;
    FOR(i, 1, 1e6+1)
    {
        if(cnt[i] > 0)
        {
            lcm *= fast_exp(i, cnt[i]);
            lcm %= MOD;
        }
    }
    ll ans = 0;
    FOR0(i, n)
    {
        ans += divide(lcm, a[i]);
        ans %= MOD;
    }
    cout<<ans<<endl;
    cerr <<endl<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
}

Measured memory_bytes: 6656
#include <bits/stdc++.h>
using namespace std;
#define N 200010

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

    int n,k;
    cin>>n>>k;

    vector<int> a(n);

    for(int i=0;i<n;i++)cin>>a[i];

    sort(a.begin(),a.end());

    int low=0;
    int high=a[n-1];
    int m=low+(high-low)/2;

    while(low!=m)
    {
        int cnt=0;
        for(int i=n-1;i>=0;i--)
        {
            int n=(a[i]-1)/m;
            if(n==0)break;
            cnt+=n;
            if(cnt>k)break;
        }

        if(cnt>k)low=m;
        else high=m;

        m=low+(high-low)/2;
    }

    cout<<high;

    return 0;
}



Measured memory_bytes: 3968
#!python3

import numpy as np


# input
N = int(input())
a = input()


def main():
    num, fac = [0] * (N + 1), [0] * (N + 1)
    num[0] = num[1] = 0
    fac[0] = fac[1] = 0
    for i in range(2, N, 2):
        num[i] = 1 + num[i // 2]
        fac[i] = fac[i+1] = fac[i-1] + num[i]
    
    m = np.zeros(N, dtype=int)
    for i in range(N):
        if fac[N-1] == fac[i] + fac[N-1-i]:
            m[i] = 1
    
    x = np.array([int(e) for e in a])
    x -= 1

    if (x * m).sum() % 2 == 1:
        print(1)
        return
    if 1 in x:
        print(0)
        return

    x //= 2
    if (x * m).sum() % 2 == 1:
        print(2)
    else:
        print(0)


if __name__ == "__main__":
    main()


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

#define N 100010
int n, a[N];

int main() {
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; ++i) {
			scanf("%d", a + i);
		}
		sort(a + 1, a + 1 + n);
		int mid = n / 2;
		if (a[mid] == a[mid + 1]) {
			puts("0");
		} else {
			printf("%d\n", a[mid + 1] - a[mid]);
		}
	}
	return 0;
}


Measured memory_bytes: 640
import java.util.Scanner;

/**
 * Created by zzt on 17-1-29.
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long x = scanner.nextLong();
        long ret = (x/11)*2;
        long mod = x % 11;
        if (mod!=0)
            ret++;
        if (mod>6)
            ret++;
        System.out.println(ret);
    }
}

Measured memory_bytes: 9808