akhauriyash/Code-Regression huggingface dataset:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		int[] freq = new int[(int) 1e5 + 1];
		long sum = 0;

		StringTokenizer st = new StringTokenizer(br.readLine());
		for (int i = 0; i < N; i++) {
			int num = Integer.parseInt(st.nextToken());
			sum += num;
			freq[num]++;
		}

		int Q = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();
		for (int q = 0; q < Q; q++) {
			String[] tokens = br.readLine().split(" ");
			int b = Integer.parseInt(tokens[0]);
			int c = Integer.parseInt(tokens[1]);
			sum += (c - b) * freq[b];
			freq[c] += freq[b];
			freq[b] = 0;
			sb.append(sum + "\n");
		}
		System.out.println(sb);
	}

}

Measured memory_bytes: 52496
x,n = map(int, input().split())
p = list(map(int, input().split()))
if n == 0:
	print(x)
	exit(0)
if x in p:
	a,b = 0,0
	for i in range(1,102):
		if x - i in p:
			continue
		else:
			a = x - i
			break
	for j in range(1,102):
		if x + j in p:
			continue
		else:
			b = x + j
			break
	if a < 0:
		print(b)
	elif b > 101:
		print(a)
	elif abs(x-a) <= abs(x-b):
		print(a)
	else:
		print(b)
else:
	print(x)

Measured memory_bytes: 9152
#coding:utf-8

n = int(input())

T = []
X = []
Y = []
for _ in range(n):
    t,x,y = map(int, input().split())
    T.append(t)
    X.append(x)
    Y.append(y)

prev_t = 0
prev_x = 0
prev_y = 0
ans = 'Yes'
for i in range(n):
    dt = T[i] - prev_t
    dis = abs(X[i] - prev_x) + abs(Y[i] - prev_y)
    if dt < dis or (dt - dis)%2 == 1:
        ans = 'No'
        break
    prev_t = T[i]
    prev_x = X[i]
    prev_y = Y[i]

print(ans)

Measured memory_bytes: 11776
#include<iostream>
using namespace std;
int main()
{

    long long n,current,prev,stool=0;
    cin>>n>>prev;
    for(int i=0;i<n-1;i++){
        cin>>current;
        if(current<prev) {stool+=prev-current;current+=prev-current;}
        prev=current;
    }
    cout<<stool;
}

Measured memory_bytes: 3608
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <cctype>
#define ll long long
#define PI 3.14159265358979323846
using namespace std;
const int maxn = 1e6+7;

int main(){
	int n;
	cin>>n;
	double *a=new double[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	sort(a,a+n);
	double res=0;
	for(int i=0;i<n-1;i++){
		res=(double)(a[i]+a[i+1])/2.0;
		a[i+1]=res;
	}
	cout<<res<<endl;
	return 0;
}


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

// chmin, chmax関数
template<typename T, typename U, typename Comp=less<>>
bool chmax(T& xmax, const U& x, Comp comp={}) {
    if(comp(xmax, x)) {
        xmax = x;
        return true;
    }
    return false;
}
template<typename T, typename U, typename Comp=less<>>
bool chmin(T& xmin, const U& x, Comp comp={}) {
    if(comp(x, xmin)) {
        xmin = x;
        return true;
    }
    return false;
}

// Yes-Noを出力する問題で楽をする
void Ans(bool f){
    if(f) cout << "Yes" << endl;
    else cout << "No" << endl;
}

//---------------------------

int main(){
    int n,m; cin >> n >> m;
    cout << (n-1) * (m-1) << endl;
    return 0;
}

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

typedef long long int LL;
typedef long long int LLint;

//Faster Program
#define endl "\n"

//Faster Coding
#define ED return
#define GET(a) int a;cin >> a
#define DGET(a) double a;cin >> a
#define SGET(a) string a;cin >> a
#define CGET(a) char a;cin >> a
#define PRINT(a) cout << a << "\n"
#define ANS(a,b) int a;cin >> a;cout << b << "\n"
#define FOR(n) for(int i = 0;i < n;i++)
#define SORT(vec) sort(vec.begin(),vec.end())
#define REV(vec) reverse(vec.begin(),vec.end())

//As Function
#define UP(a,b) ((a+(b-1))/b)
#define ipow(x,y) LL(pow(x,y))

//Debug
#define TEST cout << "OK" << endl

const double PI = M_PI;
const long long INF = int(10e9-1);
const long long MOD = int(10e9+7);

//Settings
const bool DO_BENCHMARK = false;
const int DOUBLE_ACCURACY = 15;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(DOUBLE_ACCURACY);
    void solve();
    if(DO_BENCHMARK){
        clock_t start = clock();
        solve();
        clock_t end = clock();
        const double time = static_cast<double>(end-start)/CLOCKS_PER_SEC*1000.0;
        cout << "\n" << time << "[ms]\n";
    }
    else solve();
}

void solve(){
   GET(N);
   ANS(K,(UP(N,2)>=K?"YES":"NO"));
}


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

using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

const int INF = 1<<30;

int main(){
  int n, ans=0;cin>>n;
  char c1='!', c2;
  while(n--){
    cin>>c2;
    if(c1!=c2)ans++;
    c1=c2;
  }
  cout<<ans<<endl;
}


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

using namespace std;

long long x, y;

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

    cin >> x >> y;

    if (abs(x - y) < 2) {
        cout << "Brown" << endl;
    } else {
        cout << "Alice" << endl;
    }

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i,s,n)for(ll i=s;i<n;i++)
#define repe(i,s,n)for(ll i=s;i<=n;i++)

map<ll, ll> mp;
int main() {
	ll n; cin >> n;
	rep(i, 0, n) {
		ll a; cin >> a;
		mp[a]++;
	}

	ll cnt = 0;
	for (map<ll, ll>::iterator it = mp.begin(); it != mp.end(); it++) {
		if (it->first > it->second) {
			cnt += it->second;
		}
		else if (it->first < it->second) {
			cnt += it->second - it->first;
		}
	}
	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 6272
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e9
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;
int a[100010];
ll mod=1e9+7;
signed main(){
   int n;
   cin>>n;
   ll ans=1;
REP(i,n) cin>>a[i];
    ll cnt[3]={0,0,0};
    for (int i = 0; i < n; i++)
    {
        ll tmp=0;
        for (int j = 0; j < 3; j++)
        {
            if(a[i]==cnt[j]){
                tmp++;    
            }
        }
        ans*=tmp;
        ans%=mod;
        for (int j = 0; j < 3; j++)
        {
            if(a[i]==cnt[j]){
                cnt[j]++;
                break;    
            }
        }
    }
    
   cout<<ans%mod<<endl;
    return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}

int main(){
    ll x,g;
    cin>>x;
    g=x/500*1000;
    x=x%500;
    g+=x/5*5;
    cout<<g<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
typedef string str;
typedef std::pair<ll,ll> pl;
typedef std::map<string, ll> msl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
const long double EPS = 1e-9;
 
#define rep(i, n) for(ll (i) = 0; (i) < (n) ; (i)++ )
#define For(i, a, b) for(ll (i) = (a); (i) < (b) ; (i)++ )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define tol(c) char((c)+0x20)
#define tou(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a,b) cout << (a) << " " << (b) << endl
#define prs(a) cout << (a) << " "
#define yn(condition) if ((condition)) prl("Yes"); else prl("No");
#define YN(condition) if ((condition)) prl("YES"); else prl("NO");
#define rpr(i,n,ans) rep((i),(n)) prs((ans[(i)]))
#define cpr(i,n,ans) rep((i),(n)) prl((ans[(i)]))
#define in1(a) cin >> (a)
#define in2(a,b) cin >> (a) >> (b)
#define in3(a,b,c) cin >> (a) >> (b) >> (c)
#define in4(a,b,c,d) cin >> (a) >> (b) >> (c) >> (d)
#define rin1(i,n,a) rep((i),(n)) cin >> (a[(i)])
#define rin2(i,n,a,b) rep((i),(n)) cin >> (a[(i)]) >> (b[(i)])
#define rin3(i,n,a,b,c) rep((i),(n)) cin >> (a[(i)]) >> (b[(i)]) >> (c[(i)])
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for(auto &(kv): (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define beto(a,b,c) (a) < (b) && (b) < (c)
#define betc(a,b,c) (a) <= (b) && (b) <= (c)
#define rsort(a,n) sort(a,a+n,greater<>())
 
const ll mod = 1e9 + 7;
const ll INF = mod * mod;
const ll MAX_N = 100;
 
int main(void){
    ll a;

    in1(a);
    if (a < 1200) prl("ABC");
    else prl("ARC");

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#define INF 1e9
using namespace std;

int main() {
	int N;
	cin >> N;
	int w[N];
	int ans = INF;
	for (int i  = 0; i < N; ++i) cin >> w[i];
	for (int i = 0; i < N; ++i) {
		int right = 0;
		for (int j = 0; j <= i; ++j) {
			right+=w[j];
		}
		int left = 0;
		for (int j = N-1; j > i; j--) {
			left+=w[j];
		}
		int answer = abs(left-right);
		if (answer < ans) ans = answer;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<utility>
#include<algorithm>
#include<cstring>
#include<sstream>
#include <iomanip>

using namespace std;

typedef long long ll ;
typedef double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< pair<int,int> > vii;
const double pi = 2*acos(0) ;

#define pf		  	printf
#define sf			  scanf
#define pb(a)         push_back(a)
#define mp			make_pair
#define fi			 first
#define se			 second
#define for0(i,n)        for(int i=0;i<n;i++)
#define for1(i,n)        for(int i=1;i<=n;i++)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define lcm(a, b)     ((a)*((b)/gcd(a,b)))
#define sq(a)		   (a)*(a)
#define nw				"\n"
#define abs(x)		fabs(x)
#define pcase(z,x)    cout<<"Case "<<z<<": "<<x<<"\n"
int main()
{
	ll n,m,x,c=0;
	cin>>n>>m;
	x = min(n,m/2);
	c = x;
	n -= x;
	m -= 2*x;
	if(n>0  && m>=2){
		x = min(n,m/2);
		n -= x;
		m -= 2*x;
		c += x;
		}
	if(n==0 && m>0){
		x = m/4;
		m -= 4*x;
		c += x;
		}
	cout<<c<<nw;
	return 0;
}

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

int main(void){
	int n, a, b;
	cin >> n >> a >> b;
	vector<int> p;
	p.resize(n);
	for(int i = 0; i < n; ++i){
		cin >> p[i];
	}
	sort(p.begin(), p.end());
	vector<int> ans(3, 0);
	for(int i = 0; i < n; ++i){
		if(p[i] <= a){
			ans[0]++;
		}else if(p[i] <= b){
			ans[1]++;
		}else{
			ans[2]++;
		}
	}
	cout << min(ans[0], min(ans[1], ans[2])) << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

const int N = 500000;

signed main()
{
    #ifdef ONLINE_JUDGE
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    #endif // ONLINE_JUDGE
    int n, a, b;
    cin >> n >> a >> b;
    vector<int> d(n);
    forn(i, n) cin >> d[i];
    if (n == 1)
    {
        cout << abs(b - d[0]);
        return 0;
    }
    cout << max(abs(b - d[n - 1]), abs(d[n - 2] - d[n - 1]));
}


Measured memory_bytes: 256
s_l = list(input())

for i in range(1,len(s_l)):
    s_l2 = s_l[:-i]
    if len(s_l2)%2 != 0:
        continue
    for j in range(len(s_l2)//2):
        if s_l2[j] != s_l2[j+len(s_l2)//2]:
            find = False
            break
        find = True
    if find:
        break

print(len(s_l2))

Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <vector>
#define REP(i, n) for(int i = 0; i < (n); ++i)
typedef long long ll;
const ll MOD = 1000000007;
using namespace std;
int main(){
    int d, g; cin >> d >> g;
    int ans = 100000000;
    vector<int> p(d), c(d);
    REP(i, d) cin >> p[i] >> c[i];
    
    REP(i, (1<<d)){
        int hampa=-1, score=0, cnt=0;
        REP(j, d){
            if(i & (1<<j)){
                score += p[j] * 100 * (j+1) + c[j];
                cnt += p[j];
            }
            else hampa = j;
        }
        if(hampa >= 0){
            if(score + (p[hampa]-1) * 100 * (hampa+1) < g) continue;
            else cnt += max((g - score + 100 * (hampa+1) -1) / (100 * (hampa+1)), 0);
            // 天井関数にしないとダメ
        }else if(score < g) continue;
        
        ans = min(ans, cnt);
    }
    
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
const int INF = 1e9;
int N, A[100][2];
int main() {
	int i, j, h;
	scanf("%d", &N);
	for (i = 0; i < N; ++i) scanf("%d", &A[i][0]), A[i][0]--;
	for (i = 0; i < N; ++i) scanf("%d", &A[i][1]), A[i][1]--;
	int Res = INF;
	for (j = 0; j < (1 << N); ++j) {
		vector<int> Nums(N);
		for (i = 0; i < N; ++i) {
			Nums[i] = A[i][(j >> i) & 1];
		}
		vector<int> Z = Nums;
		sort(Z.begin(), Z.end());
		vector<int> ind(50);
		for (i = N - 1; i >= 0; --i) ind[Z[i]] = i;
		vector<vector<int>> sums(N, vector<int>(2));
		for (i = 0; i < N; ++i) {
			int it = ind[Nums[i]];
			int dist = (abs(i - it) + ((j >> i) & 1)) % 2;
			sums[it][dist]++;
		}
		int ok = 1;
		for (i = 0; i < N; ++i) {
			if (sums[i][1] > sums[i][0]) ok = 0;
			if (sums[i][0] > sums[i][1] + 1) ok = 0;
		}
		if (!ok) continue;
		vector<vector<vector<int>>> Occ(50, vector<vector<int>>(2));
		for (i = 0; i < N; ++i) {
			Occ[Z[i]][i & 1].push_back(i);
		}
		vector<int> kohde(N);
		for (i = N - 1; i >= 0; --i) {
			int dist = (i + ((j >> i) & 1)) % 2;
			kohde[i] = Occ[Nums[i]][dist].back();
			Occ[Nums[i]][dist].pop_back();
		}
		// for (i = 0; i < N; ++i) {
		// 	printf("%d ", kohde[i]);
		// }
		// puts("");
		vector<int> Dp(N);
		int C = 0;
		for (i = 0; i < N; ++i) {
			int x = kohde[i];
			for (h = x + 1; h < N; ++h) {
				C += Dp[h];
			}
			Dp[x] = 1;
		}
		// printf("=%d\n", C);
		Res = min(Res, C);
	}
	if (Res == INF) printf("-1\n");
	else printf("%d\n", Res);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#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 srep(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 maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define print(x)cout<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

template< typename T >
struct WeightedUnionFind {
  vector< int > data;
  vector< T > ws;

  WeightedUnionFind() {}

  WeightedUnionFind(int sz) : data(sz, -1), ws(sz) {}

  int find(int k) {
    if(data[k] < 0) return k;
    auto par = find(data[k]);
    ws[k] += ws[data[k]];
    return data[k] = par;
  }

  T weight(int t) {
    find(t);
    return ws[t];
  }

  bool unite(int x, int y, T w) {
    w += weight(x);
    w -= weight(y);
    x = find(x), y = find(y);
    if(x == y) return false;
    if(data[x] > data[y]) {
      swap(x, y);
      w *= -1;
    }
    data[x] += data[y];
    data[y] = x;
    ws[y] = w;
    return true;
  }

  T diff(int x, int y) {
    return weight(y) - weight(x);
  }
};

int main() {
  int n,m;
  cin>>n>>m;
  WeightedUnionFind<ll> tree(n);
  rep(i,m){
    int l,r;
    ll d;
    cin>>l>>r>>d;
    l--;r--;
    if(tree.find(l) == tree.find(r)){
      if(abs(tree.diff(r,l)) !=d){
        cout<<"No"<<endl;
        return 0;
      }
    }
    else{
      tree.unite(l,r,d);
    }
  }
  cout<<"Yes"<<endl;
  return 0;
}

Measured memory_bytes: 1536
N = int(input())
A = list(map(int, input().split()))

Abs_A = list(map(abs, A))
sum_Abs = sum(Abs_A)
count = 0

for i in range(N):
    if A[i] < 0:
        count += 1

if count % 2 == 0:
        print(sum_Abs)
else:
        print(sum_Abs - min(Abs_A) * 2)


Measured memory_bytes: 14332
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <list>
#include <deque>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
#define dump(a) cout << (a) << endl;
typedef long long ll;
ll mod = 1e9+7;

int f(int x) {
    ll res = 1;
    while (res <= x) res *= 2;
    return res - x;
}

int main() {
    int n;
    cin >> n;
    int cnt = 0;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        int tmp;
        cin >> tmp;
        mp[tmp]++;
    }
    auto itr = mp.end();
    itr--;
    while (true) {
        int l = f(itr->first);
        while (mp[itr->first] > 0) {
            mp[itr->first]--;
            if (mp.find(l) != mp.end() && mp[l] > 0) {
                mp[l]--;
                cnt++;
            }
            else {
                mp[itr->first]++;
                break;
            }
        }
        if (itr == mp.begin()) break;
        itr--;
    }
    dump(cnt);
    return 0;  
}

Measured memory_bytes: 9600
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <bits/stdc++.h>
#include <iomanip>
#include <numeric>
#include <utility>
#include <map>
#include <algorithm>
typedef long long ll;
using namespace std;
int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll a;
    long double b;
    cin >> a >> b;
    ll c = a * b;
    cout << c;
}


Measured memory_bytes: 3744
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
  ll N;
  cin>>N;
  vector<string>vec(N);
  for(ll i=0;i<N;i++){
    cin>>vec.at(i);
  }
  sort(vec.begin(),vec.end());
  ll a=0;
  vector<ll>veco(1);
  vector<string>ve(1);
  ve.at(0)=vec.at(0);
  for(ll i=1;i<N;i++){
    if(vec.at(i-1)==vec.at(i)){
      veco.at(veco.size()-1)+=1;
      a=max(veco.at(veco.size()-1),a);
    }
    else{
      veco.push_back(0);
      ve.push_back(vec.at(i));
    }
  }
  vector<string>result(0);
  for(ll i=0;i<veco.size();i++){
    if(veco.at(i)==a){
      result.push_back(ve.at(i));
    }
  }
  sort(result.begin(),result.end());
  for(ll i=0;i<result.size();i++){
    cout<<result.at(i)<<endl;
  }
}

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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const int INF = 1e9;
const ll MOD = 1e9 + 7;

const int MAXN = 1e5 + 5;

vector<pii> v[4];
int r, c;

stack<int> stk;

void process_point(int i, int j, int ind) {
	if (i == 0) {
		v[0].pb(mp(j, ind));
	} else if (i == r) {
		v[2].pb(mp(j, ind));
	} else if (j == 0) {
		v[3].pb(mp(i, ind));
	} else {
		v[1].pb(mp(i, ind));
	}
}

int is_good(int i, int j) {
	return (i == 0 || i == r) || (j == 0 || j == c);
}

void solve(){
	int n;
	scanf("%d%d%d", &r, &c, &n);
	REP(i, 0, n) {
		int i1, j1, i2, j2;
		scanf("%d%d%d%d", &i1, &j1, &i2, &j2);
		if (is_good(i1, j1) && is_good(i2, j2)) {
			process_point(i1, j1, i);
			process_point(i2, j2, i);
		}
	}
	REP(i, 0, 4) {
		SORT(v[i]);
		if (i == 2 || i == 3) {
			reverse(BE(v[i]));
		}
	}
	REP(i, 0, 4) {
		REP(j, 0, SZ(v[i])) {
			int a = v[i][j]._2;
			//printf("# %d\n", a);
			if (!stk.empty() && stk.top() == a) {
				stk.pop();
			} else {
				stk.push(a);
			}
		}
	}
	if (!stk.empty()) {
		printf("NO\n");
	} else {
		printf("YES\n");
	}
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}


Measured memory_bytes: 2520
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n=int(input())
Edges=[[] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    Edges[a].append(b)
    Edges[b].append(a)
while sum([len(Edges[i]) for i in range(n)]):
    for i in range(n):
        if len(Edges[i])==1:
            j=Edges[i][0]
            Edges[i].remove(j)
            Edges[j].remove(i)
            for k in Edges[j]:
                Edges[k].remove(j)
                if not Edges[k]:
                    print('First')
                    exit()
            Edges[j]=[]
print('Second')

Measured memory_bytes: 22324
#include<iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <fstream>

using namespace std;
using ll = long long;

#define PRI(n) cout << n << " "<<endl;
#define PRI2(n, m) cout << n << " " << m << " "<<endl;

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

const char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                           's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
const ll MOD = 1e9 + 7;

struct edge;
struct UnionFind;

bool isPrime(ll x);

ll GCD(ll a, ll b);

ll LCM(ll a, ll b);

ll nCr(int n, int r);


struct UnionFind {
    vector<int> par;
    vector<int> rank;

    UnionFind(int N) {
        for (int i = 0; i < N; ++i) {
            par.push_back(i);
            rank.push_back(0);
        }
    }

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

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)return;
        if (rank[x] < rank[y])par[x] = y;
        else {
            par[y] = x;
            if (rank[x] == rank[y])rank[x]++;
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

ll N, M;
string S;
vector<edge> X[100010];
bool B[100010];
int D[100010];

int main() {
    cin >> N;
    vector<int> A(N);
    REP(i, N) {
        cin >> A[i];
    }
    int cnt = 0;
    sort(A.begin(), A.end());
    int c = A[0];
    bool flag = 1;
    FOR(i, 1, N - 1) {
        if (c == A[i]) {
            flag = !flag;
            continue;
        }

        if (flag)cnt++;
        flag = true;
        c = A[i];
    }
    if (flag)cnt++;
    PRI(cnt)
    return 0;
}

struct edge {
    int to, cost;
    bool flag = false;
};

bool isPrime(ll x) {
    if (x == 0)return 0;
    if (x == 1)return 0;
    if (x == 2)return 1;
    if (x % 2 == 0)return 0;
    FOR(i, 3, x - 1) {
        if (x % i == 0)return 0;
    }
    return 1;
}

ll GCD(ll a, ll b) {
    if (b == 0)return a;
    return GCD(b, a % b);
}

ll LCM(ll a, ll b) {
    ll gcd = GCD(a, b);
    return a / gcd * b;
}

ll nCr(int n, int r) {
    vector<ll> C(r + 1);
    C[0] = 1;
    FOR(i, 1, n) for (int j = min(i, r); j < 1; --j)C[j] = (C[j] + C[j - 1]) % MOD;
    return C[r];
}


Measured memory_bytes: 2944
chomp (my ($n, $p) = split / /, <STDIN>);
my %h;

factorization($p);
if ($p==1) {
  print "1\n";
} elsif ($n==1) {
  print "$p\n";
} else {
  my $ans = 1;
  for my $k (keys %h) {
    my $v = $h{$k};
    while ($v >= $n) {
      $ans *= $k;
      $v -= $n;
    }
  }
  print "$ans\n";
}
sub factorization {
  my $arg = shift;
  while ($arg % 2 == 0) {
    $h{2}++;
    $arg /= 2;
  }
  for my $i (3..sqrt($arg)) {
    if ($arg % $i == 0) {
      $h{$i}++;
      return ($i, factorization($arg / $i));
    }
  }
  $h{$arg}++ if $arg > 0;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int main(){
  int x,y;
  while(cin>>x>>y,x){
    int s[1000000]={};
    int c=0,gc=__gcd(x,y);
    x/=gc,y/=gc;
    while(!s[x]){
      s[x]=++c;
      x=(x*10)%y;
    }
    if(x==0)c=s[x]-1;
    cout<<s[x]-1<<" "<<c-s[x]+1<<endl;
  }
  return 0;
}

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

#define MOD 1000000007
#define lli long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = n - 1; ~i; i--)
#define pb push_back
#define mkp make_pair
#define endl "\n"

using namespace std;

bool enable_test_case = false;
bool enable_google = false;

void solve()
{
    int n;
    cin >> n;
    lli a[n][3], dp[n][3];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cin >> a[i][j];
            dp[i][j] = a[i][j];
        }
    }

    for (int i = 1; i < n; i++)
    {
        for (int cur = 0; cur < 3; cur++)
        {
            for (int prev = 0; prev < 3; prev++)
            {
                if (cur != prev)
                {
                    dp[i][cur] = max(dp[i][cur], dp[i - 1][prev] + a[i][cur]);
                }
            }
        }
    }

    lli ans = INT_MIN;
    for (int j = 0; j < 3; j++)
    {
        ans = max(dp[n - 1][j], ans);
    }

    cout << ans << endl;
}

void google_contests(int t)
{
    cout << "Case #" << t + 1 << ": ";
}

void test_case()
{
    if (enable_test_case)
    {
        lli t;
        cin >> t;
        rep(i, t)
        {
            if (enable_google)
                google_contests(i);
            solve();
        }
    }
    else
    {
        solve();
    }
}

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

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    test_case();

    return 0;
}


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

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD = 1e9 + 7;

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

    int n;cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    int res=0;
    int flag=1;
    while(flag){
        flag=0;
        for(int j=n-1;j>=1;j--){
            if(a[j]<a[j-1]){
                swap(a[j],a[j-1]);
                res++;
                flag=1;
            }
        }
    }
    for(int i=0;i<n-1;i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
    cout<<res<<endl;
}

Measured memory_bytes: 3204
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int N, a; string S;
int main(){
    vector<int> vec;
    cin >> N;
    for(int i=0; i<N; ++i){
        cin >> a;
        vec.push_back(a);
    }
    sort(vec.begin(), vec.end());
    int cnt = 1;
    for(int i=1; i<N; ++i){
        if(vec[i] != vec[i-1]) cnt++;
    }
    cout << cnt << endl;
    return 0;
}

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

int main() {
    string n;
    int k, len, i;
    cin >> n >> k;
    len = n.length();
    long long dp0[len][4], dp1[len][4]; // 1がちょうど
    int top = n.at(0) - '0';

    dp0[0][0] = 1;
    dp0[0][1] = top - 1;
    dp0[0][2] = 0;
    dp0[0][3] = 0;
    for(i = 0; i < len; i++) dp1[i][0] = 0;
    dp1[0][1] = 1;
    dp1[0][2] = 0;
    dp1[0][3] = 0;

    for(i = 1; i < len; i++){
        int j;
        top = n.at(i) - '0';
        dp0[i][0] = 1;
        for(j = 1; j <= k; j++){
            dp0[i][j] = dp0[i-1][j-1] * 9 + dp0[i-1][j];
            if(top != 0){
                dp0[i][j] += dp1[i-1][j-1] * (top-1);
                dp0[i][j] += dp1[i-1][j];
            }
        }
        dp1[i][1] = dp1[i-1][1] && (top == 0);
        dp1[i][2] = (dp1[i-1][1] && (top != 0)) || (dp1[i-1][2] && (top == 0));
        dp1[i][3] = (dp1[i-1][2] && (top != 0)) || (dp1[i-1][3] && (top == 0));
    }

    cout << dp0[len-1][k] + dp1[len-1][k]<< endl;
}

Measured memory_bytes: 256
// </> : towhid1zaman

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair< int, int > pii;
typedef vector<pii>vip;
typedef pair< pii, int > ppi;
#define fill(a,b) memset(a,b,sizeof(a))
#define all(v) (v).begin(),(v).end()
#define sp(k) cout<<setprecision(k)<<fixed;
#define rep(i,a) for(int i=0;i<a;i++)
#define rep1(i,a,b) for(int i=(a);i<=(b);++i)
#define irep(i,b,a) for(int i=(b);i>=(a);--i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)),(v).erase(unique(all(v)),(v).end())
#define _ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define sqr(a) ((a)*(a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f;// (int)3e18;
const int maxn = 10000000;


int main(){
        _
        string s,t; cin >> s >> t;
        int ans = 0;
        for(int i = 0; i<max(sz(s),sz(t)); i++){
            if(s[i]!=t[i])ans++;
        }
        cout << ans << endl;
return 0;
}



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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            long N = long.Parse(Console.ReadLine());
            var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();

            long previousHeight = 0;

            long ans = 0;

            for(int i =0;i< N; i++)
            {
                if(previousHeight > A[i])
                {
                    ans += previousHeight - A[i];
                }
                else
                {
                    previousHeight = A[i];
                }
            }

            Console.WriteLine(ans);
        }
    }
}

Measured memory_bytes: 47432
# Aizu Problem 0001: List of Top 3 Hills
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


for h in sorted([int(input()) for _ in range(10)], reverse=True)[:3]:
    print(h)

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

int main() {
	
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n; cin >> n;
	int cntA = 0, cntB = 0, cntAB = 0, cnt = 0;
	while (n--) {
		char buf[20]; cin >> buf;
		int len = strlen(buf);
		if (buf[0] == 'B') cntB++;
		if (buf[len - 1] == 'A') cntA++;
		if (buf[0] == 'B' && buf[len - 1] == 'A') cntAB++;
		for (int i = 1; i < len; i++) if (buf[i - 1] == 'A' && buf[i] == 'B') cnt++;
	}
	if (cntA == cntB && cntB == cntAB && cntAB) cnt += cntAB - 1;
	else cnt += min(cntA, cntB);
	cout << cnt << "\n";
	
	return 0;
}


Measured memory_bytes: 256
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
            int A = sc.nextInt();
            int B = sc.nextInt();
            int C = sc.nextInt();
        if (C > A-B) {
            System.out.println(C-(A-B));
        } else {
            System.out.println(0);
        }
    }
}

Measured memory_bytes: 22608
#include<bits/stdc++.h>
using namespace std;
char s[100];
bool check(int a,int b){
	for(int i=a,j=b;j>i;++i,--j){
		if(s[i]!=s[j]) return false;
	}
	return true;
}
int main()
{
	scanf("%s",s);
	int l=strlen(s);
	if(check(0,l-1)&&check(0,(l-3)/2)&&check((l+1)/2,l-1)) printf("Yes");
	else printf("No");
}

Measured memory_bytes: 256
taro=0;hanako=0
for i in range(int(input())):
    l=list(map(str,input().split()))
    if l[0]>l[1]:
        taro+=3
    elif l[0]<l[1]:
        hanako+=3
    else:
        taro+=1;hanako+=1
print(taro,hanako)


Measured memory_bytes: 5600
#include <iostream>
#include <string>
#define REP(i,k,n) for(int(i)=(k);(i)<(n);++(i))
using namespace std;

int main()
{
  long A,B,C;
  cin >> A >> B >> C;

  long i;
  if (A%2==0 && A == B && B == C) {
    i = -1;
  }
  else {
    i = 0;
    // すべて偶数の限り続ける
    while (A%2==0 && B%2==0 && C%2==0) {
      long A0 = A, B0 = B, C0 = C;
      A = (B0+C0)/2;
      B = (C0+A0)/2;
      C = (A0+B0)/2;
      ++i;
    }
  }
  cout << i << endl;

  return 0;
}


Measured memory_bytes: 256
use proconio::input;

fn main() {
    input! {
        mut n: u128
    }

    solve(n);
}

fn solve(mut n: u128) {
    let mut ans: Vec<char> = vec![];
    while n > 0 {
        n -= 1;
        let x = to_char((n % 26) as u32);
        ans.push(x);
        n /= 26;
    }
    ans.reverse();

    for c in ans {
        print!("{}", c);
    }
    println!("");
}

fn to_char(num: u32) -> char {
    match num {
        0 => 'a',
        1 => 'b',
        2 => 'c',
        3 => 'd',
        4 => 'e',
        5 => 'f',
        6 => 'g',
        7 => 'h',
        8 => 'i',
        9 => 'j',
        10 => 'k',
        11 => 'l',
        12 => 'm',
        13 => 'n',
        14 => 'o',
        15 => 'p',
        16 => 'q',
        17 => 'r',
        18 => 's',
        19 => 't',
        20 => 'u',
        21 => 'v',
        22 => 'w',
        23 => 'x',
        24 => 'y',
        _ => 'z',
    }
}


Measured memory_bytes: 2068
#pragma GCC optimize("O3")
    #pragma comment(linker, "/stack:200000000")
    #pragma GCC optimize("Ofast")
    #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define int long long
    # define mo 1000000007
    using namespace std;
    #define goku ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    signed main()
    {
        goku
       char ch;
       cin>>ch;
       char re = (char)(ch+1);
       cout<<re;
        return 0;
    }


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

int main() {
  int a,b;
  cin >> a >> b;
  
  if(a%2==0 || b%2==0){
    cout << "Even" << endl;
  }
  
  if(a%2==1 && b%2==1){
    cout << "Odd" << endl;
  }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

int main()
{
  int N, K;
  cin >> N >> K;
  ll ans = 1;
  for (ll i = K; i <= N; i++)
  {
    ans = (ans + (i * (N - i + 1) + 1) % MOD) % MOD;
  }
  cout << ans << endl;
}

Measured memory_bytes: 3568
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define all(x) (x).begin(),(x).end()

constexpr auto INF = INT_MAX/2;
constexpr auto MOD = 1000000007;

int main() {

	int n, c;
	cin >> n >> c;
	vvi d(c, vi(c));
	rep(i, c)rep(j, c)cin >> d[i][j];
	vvi cc(n, vi(n));
	rep(i, n)rep(j, n) { cin >> cc[i][j]; cc[i][j]--; }


	vvi dis(3, vi(c, 0));

	rep(l,3)rep(k, c)rep(i, n)rep(j, n)if ((i + j) % 3 == l)dis[l][k] += d[cc[i][j]][k];
	int ans = INF;

	rep(i, c)rep(j, c)rep(k, c) {
		if (i == j || j == k || k == i)continue;
		ans = min(ans, dis[0][i] + dis[1][j] + dis[2][k]);
	}

	cout << ans << endl;
}

Measured memory_bytes: 1280
h = int(input())
w = int(input())
n = int(input())

a = n / h
b = n / w

if a.is_integer():
    a -= 1

if b.is_integer():
    b -= 1

if a < b:
    print(int(a) + 1)
else:
    print(int(b) + 1)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
#define MAX 100005
using ll =long long;
using namespace std;

int main(){
    int k,a,b;
    cin>>k>>a>>b;
    for(int i=a;i<=b;i++){
        if(i%k==0){
            cout<<"OK"<<endl;
            return 0;
        }
    }
    cout<<"NG"<<endl;
    return 0;
}

Measured memory_bytes: 3636
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		
		int[][] m = new int[h + 2][w+ 2];
		int[][] fin = new int[h + 2][w + 2];
		int[] x = new int[h * w + 1];
		int[] y = new int[h * w + 1];
		for(int i = 0; i < h + 2; i++) {
			for(int j = 0; j < w + 2; j++) {
				m[i][j] = 100000000;
				fin[i][j] = 0;
			}
		}
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				m[i + 1][j + 1] = sc.nextInt();
			}
		}
		for(int i = 1; i <= h; i++) {
			for(int j = 1; j <= w; j++) {
				x[m[i][j]] = i;
				y[m[i][j]] = j;
			}
		}
		
		int ans = 0;
		int[] dx = {1, 0, -1, 0};
		int[] dy = {0, 1, 0, -1};
		for(int k = 1; k <= h * w; k++) {
			int r = 0;
			for(int v = 0; v < 4; v++) {
				int nx = x[k] + dx[v];
				int ny = y[k] + dy[v];
				
				if(nx >= 0 && nx <= h && ny >= 0 && ny <= w && m[nx][ny] < k) {
					if(r == 0) {
						r = fin[nx][ny];
					}else if(r != fin[nx][ny]) {
						r = -1;
					}
					
				}
			}
			
			if(r == 0) {
				fin[x[k]][y[k]] = k;
			}else {
				fin[x[k]][y[k]] = r;
			}
			
			if(r == -1) {
				ans++;
			}
		}
		
		System.out.println(ans);
	}
}


Measured memory_bytes: 299536
#include <iostream>
#include <iomanip>
#include <cassert>

#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <limits>
#include <queue>
#include <unordered_map>
#include <list>
#include <set>
#include <numeric>
using namespace std;

using ll = long long;

#define rep_init(i, init, n) for(int i = (init); i < (n); ++i)
#define rep(i, n) rep_init(i, 0, n)

int main()
{
	ll N, M, K;
	cin >> N >> M >> K;

	vector<ll> A;
	vector<ll> B;

	ll sumA = 0;
	A.push_back(0);
	bool isOver = false;
	rep(i, N)
	{
		int a;
		cin >> a;
		if (!isOver)
		{
			sumA += a;
			if (sumA > K)
			{
				isOver = true;
				continue;
			}
			A.push_back(sumA);
		}
	}

	ll sumB = 0;
	B.push_back(0);
	isOver = false;
	rep(i, M)
	{
		int b;
		cin >> b;
		if (!isOver)
		{
			sumB += b;
			if (sumB > K)
			{
				isOver = false;
				continue;
			}
			B.push_back(sumB);
		}
	}

	int indexA = A.size() - 1;
	int indexB = 0;

	int ans = 0;
	for(; indexA >= 0; --indexA)
	{
		for (; indexB < B.size(); ++indexB)
		{
			if (A[indexA] + B[indexB] > K)
			{
				indexB = max(0, indexB - 1);
				break;
			}
		}

		indexB = min(indexB, static_cast<int>(B.size()) - 1);

		ans = max(ans, indexA + indexB);
	}

	cout << ans << endl;
}

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

int N;
int A[210];         // 最大 200 個なので余裕を持って 210 に --- 200 以上ならなんでもよいです

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

    int res = 0;

    // 操作が行える限り操作を繰り返す
    while (true) {
        bool exist_odd = false;  // A[i] がすべて偶数かどうかを判定するフラグ
        for (int i = 0; i < N; ++i) {
            if (A[i] % 2 != 0) exist_odd = true;  // 奇数があったらフラグを立てる
        }

        if (exist_odd) break;  // 奇数があったら break

        // 操作を行えるなら操作を実際に行う
        for (int i = 0; i < N; ++i) {
            A[i] /= 2;
        }
        ++res;  // 操作回数をインクリメント
    }

    cout << res << endl;
}

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

int main(){
  ll n;
  cin >> n;
  int ans = 100;
  for(int i=1; (ll)i*i<=n; ++i){
    if(n%i == 0){
      ll x = n/i;
      int nans = 0;
      while(x>0){
        x /= 10;
        nans++;
      }
      ans = min(ans,nans);
    }
  }
  cout << ans << endl;
}

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

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

    rep(i,2*K) {
        if (i==2*K-1) {
            break;
        } else if (i==2*K-2){
            cout << X-K+i+1;
        } else {
            cout << X-K+i+1 << " ";
        }
    }
    // cout << max(A+B, max(A-B, A*B));

    return 0;
}


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

using namespace std;


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

	string S1, S2;
	S1 = S.substr(0, S.length() / 2);
	S2 = S.substr((S.length() + 1) / 2, S.length());
	reverse(S2.begin(), S2.end());

	int answer = 0;
	for (int i = 0; i < S1.length(); i++)
	{
		if (S1[i] != S2[i])
		{
			answer++;
		}
	}

	cout << answer << endl;

	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main() {
	int x = 100;
	int n; scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		x = (x*21-1)/20+1;
	}
	printf("%d\n", x*1000);
	return 0;
}

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

void main_()
{
    ll N;
    cin >> N;
    string S;
    cin >> S;

    vector<ll> wsums; // i番目までのWの人数
    transform(begin(S), end(S), back_inserter(wsums),
        [w = 0](auto ch) mutable {
            if (ch == 'W')
                w++;
            return w;
        });

    vector<ll> changes;
    transform(begin(S), end(S), begin(wsums), back_inserter(changes),
        [N, wtotal = wsums.back(), i = 0](auto ch, auto wsum) mutable {
            ++i;
            auto wchanges = wsum - (ch == 'W' ? 1 : 0);
            auto echanges = N - i - (wtotal - wsum);
            return wchanges + echanges;
        });

    ll ans = *min_element(begin(changes), end(changes));
    cout << ans << endl;
}
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    main_();
    return 0;
}


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

//#define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<bool> > vvb;
typedef vector<vector<double> > vvd;
typedef vector<vector<string> > vvs;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);

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;}
template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
signed main(){
    gearup;
    ll n;cin >> n;
    vl a(n);
    rep(i,n)cin>>a[i];

    ll j = 0;
    rep1(i,n+10){
        j = a[j];
        if(j==2){
            out(i);
            return 0;
        }
        j--;
    }
    out(-1);
}



Measured memory_bytes: 1024
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int Cin()
{
    int n;
    cin >> n;
    return n;
}

int main()
{
    int N = Cin();
    int ans = 0, count = 1;
    for (int i = 0; i < N; ++i)
    {
        int ai = Cin();
        if (ai == count)
        {
            ++count;
        }
        else
        {
            ++ans;
        }
    }
    cout << (ans != N ? ans : -1) << endl;
}


Measured memory_bytes: 256
def gcd(a,b):
  if min(a,b)==a:
    if b%a==0:return a
    else:b%=a
  while 1:
    if a%b==0:break
    a%=b
    if b%a==0:break
    b%=a
  return min(a,b)
n=int(input())
a=list(map(int,input().split()))
for oAo in range(len(a)-1):
  a[0]=gcd(a[0],a[1])
  del a[1]
print(a[0])

Measured memory_bytes: 15020
from itertools import combinations_with_replacement
from sys import stdin
input = stdin.readline

N,M,Q = map(int, input().split())
L = [None for _ in range(Q)]
A = [0]*M
for q in range(Q):
    a,b,c,d = map(int, input().split())
    L[q] = [a,b,c,d]
maxtotal = 0
for tup in combinations_with_replacement(list(range(1,M+1)),N):
    total = 0
    for lis in L:
        total += lis[3] if tup[lis[1]-1] - tup[lis[0]-1] == lis[2] else 0
    maxtotal = max(maxtotal, total)
print(maxtotal)

Measured memory_bytes: 9244
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; ++i)
    std::cin >> a[i];

  // counting vector
  std::vector<int> c;
  {
    std::map<int, int> mp;
    for (auto v : a)
      mp[v] += 1;
    for (auto kv : mp)
      c.push_back(kv.second);
  }
  std::sort(c.begin(), c.end());

  // area vector
  std::vector<long long> s(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    // #{j; c[j] >= i}
    int d = c.end() - std::lower_bound(c.begin(), c.end(), i);
    s[i] = s[i - 1] + d;
  }

  for (long long k = 1; k <= n; ++k) {
    int ok = 0;
    int ng = n + 1;
    while (ng - ok > 1) {
      int mid = ok + (ng - ok) / 2;
      if (k * mid <= s[mid]) {
        ok = mid;
      } else {
        ng = mid;
      }
    }
    std::cout << ok << std::endl;
  }

  return 0;
}


Measured memory_bytes: 17140