akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"
typedef long long ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();
// freopen("input.txt", "r", stdin);

class BinaryIndexTree {
public:
    BinaryIndexTree(vector<int> nums) {
        int n = nums.size();
        arr = vector<int>(n, 0);
        bit_tree = vector<int>(n+1, 0);
        for(int i = 0; i < n; i++)
            update(i, nums[i]);
    }
    void update(int idx, int val) {
        int delta = val - arr[idx];
        arr[idx] = val;
        int x = idx + 1;
        while (x < bit_tree.size()) {
            bit_tree[x] += delta;
            x += x & (-x);
        }
    }
    int _sumRange(int x) {
        int ret = 0;
        while (x > 0) {
            ret += bit_tree[x];
            x -= x & (-x);
        }
        return ret;
    }
    int sumRange(int from, int to) {
        return _sumRange(to+1) - _sumRange(from);
    }
    int find_kth_smaller(int kth) { // log(range)^2
        int l = 0, r = arr.size()-1;
        while (l < r) {
            int mid = l + (r-l) /2;
            if (sumRange(0, mid) < kth)
                l = mid+1;
            else
                r = mid;
        }
        return l;
    }
private:
    vector<int> arr;
    vector<int> bit_tree;
};

int main() {
    int n, q, l, r;
    cin >> n >> q;
    vector<ll> color(n, 0);
    for(auto& val: color)
        cin >> val;
    vector<pair<int, int>> query;
    map<int, set<int>> order_query; 
    for(int i = 0; i < q; i++) {
        cin >> l >> r;
        l -= 1;
        r -= 1;
        query.push_back({l, r});
        order_query[r].insert(l);
    }
    unordered_map<int, int> last_pos; 
    // update right most occurrance of certain color[i]
    auto bit = new BinaryIndexTree(vector<int>(n, 0));
    map<pair<int, int>, int> ans;
    for(int i = 0; i < n; i++) {
        int c = color[i];
        if (last_pos.count(c) == 0) {
            bit->update(i, 1);
        } else {
            bit->update(last_pos[c], 0);
            bit->update(i, 1);            
        }
        last_pos[c] = i;
        if (order_query.count(i) > 0) {
            int r = i;
            for(auto l: order_query[r]) {
                ans[{l, r}] = bit->sumRange(l, r);
            }
        }        
    }
    for(int i = 0; i < q; i++) {
        cout << ans[query[i]] << endl;
    }

    return 0;
}


Measured memory_bytes: 116616
#include<stdio.h>
#include<stdlib.h>

int main(){

  int *A,N,flag=1,B,j,count=0;

  scanf("%d",&N);
  A = (int*)malloc(sizeof(int)*N);
  for(j=0;j<N;j++){
    scanf("%d",&A[j]);
  }
  
  while(flag){
    flag =0;
    for(j=N-1;j>=1;j--){
      if(A[j] < A[j-1]){
	B = A[j];
	A[j] = A[j-1];
	A[j-1] = B;
	flag = 1;
	count++;
      }
    }
  }
  printf("%d",A[0]);
  for(j=1;j<N;j++){
  printf(" %d",A[j]);
  }
  printf("\n%d\n",count);
  return 0;
}



Measured memory_bytes: 2140
#include<algorithm>
#include<cstring>
#include<cstdio>
#define INF 1<<28
using namespace std;

int sx, sy, gx, gy;
int dx[5][1111], dy[5][1111], dn[5];
int memo[5][5][1111];
int a, b;

int solve(int k, int t, int q){

  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);
  if(memo[k][t][q]) return memo[k][t][q];

  int ans = INF, d = (t+1)%5, s;
  for(int i=0;i<dn[d];i++){
    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);
    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);
    ans = min(ans, solve(k+1, d, i) + s);
  }

  return memo[k][t][q] = ans;
}

int main(){

  int w,h;
  char c;

  while(true){
    scanf("%d %d", &w, &h);
    if(!w && !h) break;

    memset(dn, 0, sizeof(dn));

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	scanf(" %c", &c);
	if(c == 'S') sx = j, sy = i;
	else if(c == 'G') gx = j, gy = i;
	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;
      }
    }
    
    b = INF;
    for(int i=0;i<5;i++){
      memset(memo, 0, sizeof(memo));
      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);
    }

    if(b == INF) printf("NA\n");
    else printf("%d %d\n", a+1, b);

  }
}

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

namespace ABC130C
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            long W = long.Parse(input[0]);
            long H = long.Parse(input[1]);
            long x = long.Parse(input[2]);
            long y = long.Parse(input[3]);
            //面積が実数になる場合がある
            double ans = (double)W * (double)H / 2;

            int plural;//答えが複数あるか　ある=1　ない=0
            // WとHは整数型なので偶数の時だけOKにする
            if (W%2==0&&H%2==0&&W / 2 == x && H / 2 == y)
            {
                //与えられた点が中央にあるとき
                plural = 1;
            }
            else
            {
                //与えられた点が中央にないとき
                plural = 0;
            }
            Console.WriteLine(ans + " " + plural);
        }
    }
}


Measured memory_bytes: 13140
#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

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

	int n; cin >> n;
	vector<int> ls(n);
	for (auto& l : ls) {
		cin >> l;
	}

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

	int64_t ans = 0;
	FOR_LT(a, 0, n - 2) {
		FOR_LT(b, a + 1, n - 1) {
			FOR_LT(c, b + 1, n) {
				if (ls[a] + ls[b] > ls[c]) ans++;
				else break;
			}
		}
	}

	cout << ans << endl;



	return 0;
}

Measured memory_bytes: 256
#include <iostream>

int main() {
  int a, b;
  std::cin >> a >> b;

  int product = a * b;
  if ( product % 2 == 0 ) {
    std::cout << "Even" << std::endl;
  } else {
    std::cout << "Odd" << std::endl;
  }

  return 0;
}


Measured memory_bytes: 256
N,K = map(int,input().split())
A =[]
B =[]
count = 0
for _ in range(K):
  d = int(input())
  B = list(map(int,input().split()))
  A += B
set(A)

for i in range(1,N+1):
  if not i in list(A):
    count += 1
print(count)

Measured memory_bytes: 73168
#include <stdio.h>

int linearSearch(int *, int);

int n;

int main(void)
{
	int i, S[10001], q, T[500], c=0;
	
	scanf("%d",&n);
	for (i=0; i<n; i++) {
		scanf("%d",&S[i]);
	}
	scanf("%d",&q);
	for (i=0; i<q; i++) {
		scanf("%d",&T[i]);
	}
	
	for (i=0; i<q; i++) {
		if( linearSearch( S, T[i] ) != -1 ) c++;
	}
	
	printf("%d\n",c);
	
	return 0;
}

int linearSearch(int *S, int key)
{
	int i;
	i = 0;
	S[n] = key;
	while ( S[i] != key ) {
		i++;
	}
	if( i==n ) return -1;
	return i;
}

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

int main()
{
	string s, t;
	cin >> s >> t;
	int ans = t.size();
	for(int i = 0; i < (int)s.size() - (int)t.size() + 1; ++i){
		int c = 0;
		for(int j = 0; j < (int)t.size(); ++j){
			if(t[j] != s[i + j]) ++c;
		}
		ans = min(ans, c);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3576
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
constexpr int mod = 1e9+7;

struct UnionFind{
  vector<int> data;
  int __size;
  UnionFind(int sz) {
  data.assign(sz, -1);
  __size = sz;
  }
 
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);//親は負でサイズを保存
    __size--;
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  bool same(int x, int y){
    return find(x) == find(y);
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }

  int union_count() {
    return (__size);
  }
};

int main(){
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  vector<int> p(n);
  rep(i,n) cin >> p[i], --p[i];
  rep(i,m) {
    int x, y;
    cin >> x >> y;
    x--; y--;
    uf.unite(x, y);
  }
  int res = 0;
  for (int i = 0; i < n; i++) res += uf.same(p[i], i);
  cout << res << endl;
  return 0;
} 

Measured memory_bytes: 1024
#ABC084

n,m,x = list(map(int,input().split()))
a = list(map(int,input().split()))
l = [0,0]
index = 0
for i in range(m):
    if a[i] > x:
        index = 1
    l[index] += 1
print(min(l[0],l[1]))

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

class UnionFindTree {
public:
  void unite(int x, int y) {
    int y_set = tree[y];
    for (int i = 0; i < tree.size(); ++i) {
      if (tree[i] == y_set)
        tree[i] = tree[x];
    }
  }
  int same(int x, int y) {
    if (tree[x] == tree[y])
      return 1;
    else
      return 0;
  }
  std::vector<int> tree;
};

int main() {
  int n, q;
  std::cin >> n >> q;
  auto uft = UnionFindTree();
  for (int i = 0; i < n; ++i) {
    uft.tree.push_back(i);
  }
  for (int i = 0; i < q; ++i) {
    int com, x, y;
    std::cin >> com >> x >> y;
    if (com == 0)
      uft.unite(x, y);
    else if (com == 1)
      std::cout << uft.same(x, y) << std::endl;;
  }
}



Measured memory_bytes: 3232
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
 
 
int main() {
    int a,b;
    cin >> a >> b;
    if(a%2==b%2) cout << (a+b)/2 << endl;
    else cout << (a+b)/2+1 << endl;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
 
// random generator
// mt19937 rndm(time(0));
 
// ll gcd(ll a, ll b)
// {
//     while (b>0)
//     {
//         ll tmp = a;
//         a = b;
//         b = tmp%b;
//     }
//     return a;
// }
 
 
// long long gcd(long long a, long long b) {
//  return a? gcd(b % a, a) : b;
// }
 
 
const int M=1e9+7;
ll powmod(ll a,ll b, ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res%mod;}/* a^b%mod */
const int N=1e3+10; 



int main() {	
    
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	
	int n, m, C;
	cin>>n>>m>>C;
	int b[m];
	for(int i=0;i<m;++i)
		cin>>b[i];
	int a[n][m];
	int ans = 0;
	for(int i=0;i<n;++i) {
		for(int j=0;j<m;++j)
			cin>>a[i][j];
		int c = 0;
		for(int j=0;j<m;++j)
			c+=a[i][j]*b[j];
		c+=C;
		if(c>0)ans++;
	}
	cout<<ans<<endl;
	return 0;
}





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

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

//?????§??¬?´???°
int gcd(int x, int y) { return y ? gcd(y, x%y) : x; }

//?????????: n??\???????´???°
vector<int> getPrimes(int n) {
	vector<char> is_prime(n + 1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i*i <= n; i++)
		if (is_prime[i]) {
			int j = i + i;
			while (j <= n) {
				is_prime[j] = false;
				j += i;
			}
		}
	vector<int> primes;
	rep(i, 0, n + 1)
		if (is_prime[i])
			primes.emplace_back(i);
	return primes;
}

//?´??????°????§£
vector<int> primeFactorization(int x) {
	vector<int> primes = getPrimes(sqrt(x)); //???x??\???????´???°???????????????????????°??????
	vector<int> factors;
	for (auto &p : primes) {
		while (x%p == 0) {
			x /= p;
			factors.emplace_back(p);
		}
	}
	if (x != 1)factors.emplace_back(x);
	return factors;
}

//??§?¨???§??? vector?????????????´??????????
//v: ??§???????????§?¨??????? ????????????
template<typename T>
void compress(vector<T> &v) {
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
}

signed main() {
	int p, q; cin >> p >> q;
	int g = gcd(p, q);
	int a = q / g;
	int ans = 1;
	vector<int> factors = primeFactorization(a);
	compress(factors);
	rep(i, 0, factors.size()) {
		ans *= factors[i];
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3224
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
template <class T> inline bool chmin(T &a, T b) {if (a > b){a = b;return true;}return false;}
template <class T> inline bool chmax(T &a, T b) {if (a < b){a = b;return true;}return false;}

template <typename T> string toString(const T& input) { ostringstream oss; oss << input; return oss.str(); }

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin>>n;
    string s; cin>>s;
    int ans =0;
    for(char c1='0'; c1<='9'; c1++){
        for(char c2='0'; c2<='9';c2++){
            for(char c3='0'; c3<='9'; c3++){
                string str = s;
                int t = str.find(c1);
                if(t == string::npos) continue;

                str = str.substr(t+1);
                t = str.find(c2);
                if(t==string::npos) continue;

                str = str.substr(t+1);
                t = str.find(c3);
                if(t!=string::npos) ans++;
            }
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 384
#include <iostream>
#include <numeric>
#include <cmath>
#include <limits>
#include <stdio.h>
#include <iomanip>
#include <cstdlib>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) {
	return max(max(a, b), c);
}
ll Min(ll(a), ll(b), ll(c)) {
	return min(min(a, b), c);
}
int modPow(long long a, long long n, long long p) {
	if (n == 0) return 1; // 0乗にも対応する場合
	if (n == 1) return a % p;
	if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
	long long t = modPow(a, n / 2, p);
	return (t * t) % p;
}
vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
	fact.resize(n + 5);
	factinv.resize(n + 5);
	inv.resize(n + 5);
	fact.at(0) = fact.at(1) = 1;
	factinv.at(0) = factinv.at(1) = 1;
	inv.at(1) = 1;
	for (ll i = 2; i < n + 5; i++) {
		fact.at(i) = (fact.at(i - 1) * i) % mod;
		inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
		factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
	}

}
ll nCk(ll n, ll k) {
	if (n < k) return 0;
	else {
		return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
	}
}



int main() {

	ll N, K;
	cin >> N >> K;
	prenCkModp(N);
	rep(i, K) {
		cout << (nCk(N - K + 1, i + 1) * nCk(K - 1, i)) % mod << endl;
	}
}



Measured memory_bytes: 3636
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<iomanip>
#include<list>
#include<deque>
using namespace std;
//#define MODE 1
#ifdef MODE
#define DEB(X) cout<< #X <<": "<<X<<" ";
#define END cout<<endl;
#else
#define DEB(X) {}
#define END {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
typedef std::pair<int,int> P;
struct edge{int to,cost;};
const int INF=100000000000000000;
const int INF2=9223372036854775807;
const int MOD=1000000007;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define min(X,Y) (((int)(X)<(int)(Y))?(X):(Y))
#define max(X,Y) (((int)(X)>(int)(Y))?(X):(Y))
#define MAX(X,Y) (*max_element(X,Y))
#define MIN(X,Y) (*min_element(X,Y))
#define NP(X,Y) next_permutation(X,Y)
#define setp(X,Y) cout<<fixed<<setprecision(Y)<<X;
int ceil2(int a,int b){if(a%b==0){return a/b;}else{return a/b+1;}}
int pow2(int a,int b){int r=1;for(int i=1;i<=b;i++){r*=a;}return r;}
int Log2(int a){int t=0;while(1){if(a==0||a==1){break;}a/=2;t++;}return t;}

int N;
int A[2010];

signed main(){
	cin>>N;
	REP(i,N){
		cin>>A[i];
	}
	sort(A,A+N);
	int ans=0;
	A[N]=INF;
	for(int i=0;i<N;i++){
		for(int j=i+1;j<N;j++){
			int a=A[i],b=A[j];
			int p1=distance(A,upper_bound(A,A+N,max(a-b,b-a)));
			int p2=distance(A,upper_bound(A,A+N,a+b-1));
			int d=p2-p1;
			DEB(d)END
			if(p1<=i&&i<p2){
				d--;
			}
			if(p1<=j&&j<p2){
				d--;
			}
			DEB(d)DEB(a)DEB(b)DEB(p1)DEB(p2)DEB(i)DEB(j)END
			ans+=max(d,0);
		}
	}
	cout<<ans/3<<endl;
	return 0;
}


Measured memory_bytes: 256
import queue
N=int(input())
M=[list(map(int,input().split()[2:]))for _ in[0]*N]
q=queue.Queue();q.put(0)
d=[-1]*N;d[0]=0
while q.qsize()>0:
 u=q.get()
 for v in M[u]:
  v-=1
  if d[v]<0:d[v]=d[u]+1;q.put(v)
for i in range(N):print(i+1,d[i])


Measured memory_bytes: 7004
import java.io.*;

public class Main
{
	public static void main(String[] args)throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		double r = Double.parseDouble(str);
		double pi = Math.PI;
		double l = pi * r * 2;
		double S = pi * r * r;
		System.out.printf("%.6f %.6f", S, l); 
	}
}

Measured memory_bytes: 24688
using System;
class p
{
  static void Main()
  {
    string[] ipt = Console.ReadLine().Split(' ');
    int a = int.Parse(ipt[0]);
    int b = int.Parse(ipt[1]);
    
    string A = "", B = "";
    for (int i = 0; i < a; i++) {
      A += ipt[1];
    }
    for (int i = 0; i < b; i++) {
      B += ipt[0];
    }
    
    string[] arr = new string[2];
    arr[0] = A;
    arr[1] = B;
    Array.Sort(arr);
    Console.WriteLine(arr[0]);
  }
}

Measured memory_bytes: 13408
import java.util.Scanner;

public class Main {

	public static long[] nums;

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		boolean flag1 = false;
		boolean flag2 = false;

		for (int i = 0; i < N; i++) {
			int D1 = scan.nextInt();
			int D2 = scan.nextInt();

			if (D1 == D2 && flag1 && flag2) {
				System.out.println("Yes");
				System.exit(0);
			}

			flag1 = flag2;
			flag2 = D1 == D2;
		}

		System.out.println("No");
	}
}

Measured memory_bytes: 36100
w, h = (int(i) for i in input().split())  
s = [[i for i in list(input())] for i in range(w)] 


for i in range(w):
    for j in range(h):
        if s[i][j] == '#':
            temp = 0
            if j-1>=0 and s[i][j-1] == '#':
                temp =1        
            if i+1<w and s[i+1][j] == '#' :
                temp = 1
            if j+1<h and s[i][j+1] == '#':
                temp = 1
            if i-1 >=0 and s[i-1][j] == '#':
                temp = 1  
            if temp == 0:
                print("No")
                exit()        

print("Yes")

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define vi vector<int>
template <class t, class u>
void chmax(t &a, u b)
{
    if (a < b)
        a = b;
}
template <class t, class u>
void chmin(t &a, u b)
{
    if (b < a)
        a = b;
}

int main()
{
    int h, w;
    cin >> h >> w;
    vector<vector<int>> vvi(h + 2, vector<int>(w + 2, 0));
    rep(i, h)
    {
        string s;
        cin >> s;
        rep(j, w)
        {
            vvi[i + 1][j + 1] = ((s[j] == '#') ? 1 : 0);
        }
    }
    auto a = vvi;
    rep(i, h)
    {
        rep(j, w)
        {
            if (vvi[i + 1][j + 1] == 1)
            {
                continue;
            }
            int cnt = 0;
            int adj[8][2] = {{-1, -1},
                             {-1, 0},
                             {-1, 1},
                             {0, 1},
                             {0, -1},
                             {1, -1},
                             {1, 0},
                             {1, 1}};
            rep(k, 8)
            {
                if (vvi[i + 1 + adj[k][0]][j + 1 + adj[k][1]] == 1)
                {
                    cnt++;
                }
            }
            a[i + 1][j + 1] = cnt;
        }
    }
    rep(i, h)
    {
        rep(j, w)
        {
            if (vvi[i + 1][j + 1] == 0)
            {
                cout << a[i + 1][j + 1];
            }
            else
            {
                cout << "#";
            }
        }
        cout << "\n";
    }

    return 0;
}

Measured memory_bytes: 256
// 解き直し.
// ABC 132解説.
// https://img.atcoder.jp/abc132/editorial.pdf
#include <bits/stdc++.h>
using namespace std;
const int MAX_V = 1e5;
int memo[321123];
vector<int> graph[321123];

// 幅優先探索.
// https://ja.wikipedia.org/wiki/幅優先探索
// グラフを幅優先探索する.
// ※bfsの動作確認用.
// @param s: 探索開始の頂点番号.
// @param g: 探索終了の頂点番号.
// @return: 特に無し.
void bfs(int s, int g){

    // 1. 空のキュー.
    queue<int> q;

    // 2. 訪問済みフラグ設定.
    // ※スタート地点は, 距離ゼロを指定.
    memo[s] = 0;

    // 3. 探索地点 s をキュー q に追加.
    q.push(s);
    
    // 4. キュー q が 空になるまで, 探索.
    while(!q.empty()) {
        
        // 5. 頂点 u を キュー q から取り出す.
        int u = q.front();
        q.pop();
        
        // 6. 頂点 u が ゴールだったら終了.
        if(u == g) break;
        
        // 7. 取り出した要素を処理.
        for(auto &e : graph[u]){
            // 7-1. 頂点u の 隣接する頂点e が, 未訪問だったら, キュー q に追加.
            if(memo[e] == 0 && e != s){
                // 頂点e を キュー q に追加.
                q.push(e);
                // 頂点e に 距離memo[u] プラス 1 を 加算.
                memo[e] += memo[u];
                memo[e]++;
            }
        }
    }
}

int main() {

    // 1. 入力情報取得.
    int N, M;
    scanf("%d %d", &N, &M);

    // 2. グラフ作成.
    // 解説通り, グラフを3倍化？してみる.
    // 3辺 u0 -> v1, u1 -> v2, u2 -> v0 を 張る.
    // 具体的には, 
    // u0 = u, u1 = u + MAX_V, u2 = u + MAX_V * 2
    // v0 = v, v1 = v + MAX_V, v2 = v + MAX_V * 2
    for(int i = 0; i < M * 3; i += 3){
        int u, v;
        scanf("%d %d", &u, &v);
        u--, v--;
        // u0 -> v1
        graph[u].push_back(v + MAX_V);
        // u1 -> v2
        graph[u + MAX_V].push_back(v + MAX_V * 2);
        // u2 -> v0
        graph[u + MAX_V * 2].push_back(v);
    }
    
    // 3. スタート地点, ゴール地点.
    int S, T;
    scanf("%d %d", &S, &T);
    S--, T--;
    
    // 4. S0 -> T0 の 最短距離 を 計算.
    bfs(S, T);
    // for(int i = 0; i < N * 3; i++) cout << memo[i] << " ";
    // cout << endl;
    
    // 5. 後処理.
    int ans = memo[T] / 3;
    if(ans == 0) printf("%d\n", -1);
    else         printf("%d\n", ans);
    return 0;

}

Measured memory_bytes: 18304
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=5e5+5;
priority_queue < ll > x,y;
ll xs,ys,vy[N],vx[N],ans, n,a[N];
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    ans=-1e16+7;
    cin>>n;
    for(int i=0;i<3*n;i++){
        cin>>a[i];
        if(i<n){
            x.push(-a[i]);
            xs+=a[i];
        }
        else
        if(i>=2*n){
            y.push(a[i]);
            ys+=a[i];
        }
    }
    vx[n]=xs;
    for(int i=n;i<2*n;i++){
        if(a[i]>-x.top()){
            xs+=x.top();
            x.pop();
            xs+=a[i];
            x.push(-a[i]);
        }
        vx[i-n]=xs;
    }
    vy[n]=ys;
    for(int i=2*n-1;i>=n;i--){
        if(a[i]<y.top()){
            ys-=y.top();
            y.pop();
            ys+=a[i];
            y.push(a[i]);
        }
        vy[i-n]=ys;
    }
    for(int i=0;i<n;i++)
        ans=max(ans,vx[i]-vy[i+1]);
    ans=max(vx[n]-vy[n],ans);
    ans=max(vx[n]-vy[0],ans);
    cout<<ans<<'\n';
    return 0;
}

Measured memory_bytes: 12148
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import Data.List (sortBy)

main :: IO ()
main = do
  (n : m : _) <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  as <- sortBy (flip compare) . map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    a0 = head as :: Int
    countAndSumGE :: Int -> (Int, Int)
    countAndSumGE k = fst . fst . foldr step (((0, 0), (0, 0)), as) $ as
      where
        step :: Int -> (((Int, Int), (Int, Int)), [Int]) -> (((Int, Int), (Int, Int)), [Int])
        step x (((c, s), (d, t)), ys) = (((c + c', s + s'), (d', t')), sf)
          where
            (pf, sf) = span (k - x <=) ys
            (u, v) = ((,) <$> length <*> sum) pf
            d' = d + u
            c' = d'
            t' = t + v
            s' = x * d' + t'
    countGE :: Int -> Int
    countGE k = fst . fst . foldr step ((0, 0), as) $ as
      where
        step x ((c, d), ys) = ((c + d', d'), sf)
          where
            (pf, sf) = span (k - x <=) ys
            d' = d + length pf
    k = unsafeBinaryMaxInt ((m <=) . countGE) (0, succ $ 2 * a0) :: Int
    (c, s) = countAndSumGE (succ k)
  print $ s + k * (m - c)

unsafeBinaryMaxInt :: (Int -> Bool) -> (Int, Int) -> Int
unsafeBinaryMaxInt p = fst . until ((==) <$> succ . fst <*> snd) halve
  where
    halve (i, j) | p m       = (m, j)
                 | otherwise = (i, m)
      where
        m = (i + j) `div` 2

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n


Measured memory_bytes: 60796
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long x,a,b,c,s=0;
    cin>>a>>b>>c>>x;
    for(int i=0;i<=a;i++)
        for(int j=0;j<=b;j++)
            for(int z=0;z<=c;z++){
                int k=i*500+j*100+z*50;
                if(k==x)
                   s++;
                if(k>x)
                   break;
            }
    cout<<s<<endl;
    return 0;
}

Measured memory_bytes: 256
n = int(input())
Bs = list(map(int, input().split()))
ans = []
flg = True

while True:
    if not Bs:
        break
        
    b_len = len(Bs)
    for B in Bs[::-1]:
        if Bs[b_len -1] == b_len:
            ans.append(Bs[B-1])
            del Bs[B-1]
            break
        else:
            b_len -= 1
    else:
        flg = False
        print("-1")
        break

if flg:
    for a in ans[::-1]:
        print(a)

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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  int n ;
  cin >> n ;
  
  int  N = n %10 ;
  string ans = "hon" ;
  
  if(N == 0) ans = "pon";
   if(N == 1) ans = "pon";
   if(N == 6) ans = "pon";
   if(N == 8) ans = "pon";
  
   if(N == 3) ans = "bon";
  
  cout << ans ;
  

    
  
}


Measured memory_bytes: 3628
#include <iostream>
//#include <iomanip>
//#include <string>
//#include <vector>
#include <algorithm>
//#include <utility>
//#include <set>
//#include <map>
//#include <queue>
//#include <bitset>
//#include <math.h>
using namespace std ;
//using ll = long long ;
//using ld = long double ;
//using vll = vector<ll> ;
//using vvll = vector<vll> ;
//using vc = vector<char> ;
//using vvc = vector<vc> ;
//using vb = vector<bool> ;
//using vvb = vector<vb> ;
//using pll = pair<ll,ll> ;

//ll mod = 1000000007 ;
//long double pie = acos(-1) ;

//string yorn(bool a){if(a) return"Yes" ; return "No" ;}
//string YorN(bool a){if(a) return"YES" ; return "NO" ;}
//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
//ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
//void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
//void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}

int main(){
	int n,a,b ;
	cin >> n >> a >> b ;
	int i,j,k ; i = j = k = 0 ;
	for(int m=0;m<n;m++){
		int p ; cin >> p ;
		if(p<=a) i++ ;
		else if(p<=b) j++ ;
		else k++ ;
	}
	cout << min(i,min(j,k)) << endl ;
}


Measured memory_bytes: 256
using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    int N;
    long[] x;
    int M;
    long K;
    int[] a;
    int[] original;
    public Solve(){}
    public static int Main(){
        new Solve().Calc();
        return 0;
    }
    void Calc(){
        getinput();
        int[] swap;
        long[] dx = new long[N-1];
        long[] adx = new long[N-1];
        original = new int[N-1];
        for(int i=0;i<N-1;i++){
            original[i] = i;
        }
        for(int i=0;i<M;i++){
            int j = original[a[i]-1];
            original[a[i]-1] = original[a[i]];
            original[a[i]] = j;
        }
        swap = m(K);
        for(int i=0;i<N-1;i++){
            dx[i] = x[i+1]-x[i];
        }
        for(int i=0;i<N-1;i++){
            adx[i] = dx[swap[i]]; 
        }
        StringBuilder sb = new StringBuilder();
        long count = x[0];
        sb.Append(count+"\n");
        for(int i=0;i<N-1;i++){
            count += adx[i];
            sb.Append(count+"\n");
        }
        Console.Write(sb.ToString());
    }
    int[] m(long k){
        if(k <= 1){
            return original;
        }
        else if(k%2 == 0){
            int[] s = m(k/2);
            int[] t = new int[N-1];
            for(int i=0;i<N-1;i++){
                t[i] = s[s[i]];
            }
            return t;
        }
        else{
            int[] s = m(k-1);
            int[] t = new int[N-1];
            for(int i=0;i<N-1;i++){
                t[i] =original[s[i]];
            }
            return t;
        }
    }
    void getinput(){
        N = int.Parse(Console.ReadLine());
        x = new long[N];
        string[] str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            x[i] = Int64.Parse(str[i]);
        }
        str = Console.ReadLine().Split(' ');
        M = int.Parse(str[0]);
        K = Int64.Parse(str[1]);
        a = new int[M];
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<M;i++){
            a[i] = int.Parse(str[i])-1;
        }
    }    
}

Measured memory_bytes: 31528
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

typedef pair<int, int> pii;

int main()
{
	int t, n, sx, sy;
	while (cin >> t >> n, t || n) {
		vector< vector<int> > masu(122, vector<int>(122, 0));
		for (int i = 0, x, y; i < n; i++) {
			cin >> x >> y;
			x += 61; y += 61;
			masu[x][y] = 1;
		}
		cin >> sx >> sy;
		sx += 61; sy += 61;
		queue< pii > q;
		masu[sx][sy] = t + 1;
		q.push(pii(sx,sy));
		int res = 0;
		while (!q.empty()) {
			int X = q.front().first, Y = q.front().second;
			res++;
			q.pop();
			if (masu[X][Y] == 1)
				continue;
			if (!masu[X - 1][Y]) {
				q.push(pii(X - 1, Y));
				masu[X - 1][Y] = masu[X][Y] - 1;
			}
			if (!masu[X][Y - 1]) {
				q.push(pii(X, Y - 1));
				masu[X][Y - 1] = masu[X][Y] - 1;
			}
			if (!masu[X - 1][Y - 1]) {
				q.push(pii(X - 1, Y - 1));
				masu[X - 1][Y - 1] = masu[X][Y] - 1;
			}
			if (!masu[X + 1][Y]) {
				q.push(pii(X + 1, Y));
				masu[X + 1][Y] = masu[X][Y] - 1;
			}
			if (!masu[X][Y + 1]) {
				q.push(pii(X, Y + 1));
				masu[X][Y + 1] = masu[X][Y] - 1;
			}
			if (!masu[X + 1][Y + 1]) {
				q.push(pii(X + 1, Y + 1));
				masu[X + 1][Y + 1] = masu[X][Y] - 1;
			}
		}
		cout << res << endl;
	}
	return 0;
}

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

using namespace std;

const int MAX_N = 1e5 + 10;

int n, c, k;
int a[MAX_N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> c >> k;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a, a + n);
    int prev = a[0];
    int cnt = 1;
    int ans = 1;
    for (int i = 1; i < n; i++) {
        if (prev + k < a[i] || cnt == c) {
            ans++;
            cnt = 1;
            prev = a[i];
        } else {
            cnt++;
        }
    }
    cout << ans << "\n";
}


Measured memory_bytes: 640
#include<iostream>
#include<bitset>
using namespace std;
int n, i, j, k, lg, nr;
bitset<2005> a[2005], ds[2005], dd[2005], aux;
char x;
int main(){
    cin>> n;
    for(i = 2; i <= n; i++){
        for(j = 1; j < i; j++){
            cin>> x;
            a[i][j] = x - '0';
            a[j][i] = 1 - a[i][j];
        }
    }
    for(i = 1; i <= n; i++){
        ds[i][i] = dd[i][i] = 1;
    }
    for(lg = 2; lg <= n; lg++){
        for(i = 1; i <= n - lg + 1; i++){
            j = i + lg - 1;
            aux = (a[i] & dd[i + 1] & ds[j]);
            ds[j][i] = aux.any();
            aux = (a[j] & dd[i] & ds[j - 1]);
            dd[i][j] = aux.any();
        }
    }
    for(i = 1; i <= n; i++){
        if(dd[1][i] == 1 && ds[n][i] == 1){
            nr++;
        }
    }
    cout<< nr;
}


Measured memory_bytes: 1792
#include<stdio.h>
 
int main()
{
  int t[4][14];
  int n, i,j,c;
  char m, d;
  for(i=0; i < 4; i++){
    for(j=0; j < 14; j++){
      t[i][j]=0;
    }
  }
 scanf("%d", &c);
  for(i = 0; i < c; i++){
    scanf("%c", &d);
    scanf("%c %d", &m, &n);
    if(m == 'S'){
      t[0][n] = 1;
    }
    else if(m == 'H'){
      t[1][n] = 1;
    }
    else if(m == 'C'){
      t[2][n] = 1;
    }
    else if(m == 'D'){
      t[3][n] = 1;
    }
  }
  for(j=1; j < 14; j++){
    if(t[0][j] != 1){
      printf("S %d\n", j);
    }
  }
 
 for(j=1; j < 14; j++){
    if(t[1][j] != 1){
      printf("H %d\n", j);
    }
  }
 
 for(j=1; j < 14; j++){
    if(t[2][j] != 1){
      printf("C %d\n", j);
    }
  }
 
 for(j=1; j < 14; j++){
    if(t[3][j] != 1){
      printf("D %d\n", j);
    }
  }
 return 0;
}



Measured memory_bytes: 2052
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
// constexpr ll MOD = 998244353;
constexpr ll MOD = 1000000007;

// struct UnionFind {
//     vector<int> par;
    
//     UnionFind(int N): par(N) {
//         for (int i=0;i<N;i++) par[i] = i;
//     }

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

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

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

int main(void) {
    int N, K;
    vector<int> h(100010);
    cin >> N >> K;
    for (int i=0;i<N;i++) cin >> h[i];

    int cnt = 0;
    for (int i=0;i<N;i++) if (h[i]>=K) cnt++;
    cout << cnt << endl;

    return 0;
}


Measured memory_bytes: 640
# -*- coding: utf-8 -*-

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    n = int(lines[0])

    return (n,)


def solve(n):


    return n * n * n


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r)
    else:
        print("%s" % result)

if __name__ == '__main__':

    main()
	

Measured memory_bytes: 3064
#include "iostream"
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;

long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;

void insertionSort(int A[],int n,int g){
  for(int i = g; i < n; i++){
    int v = A[i];
    int j = i - g;
    while(j >= 0 && A[j] > v){
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
}

void shellsort(int A[],int n){
  for(int h = 1; ;){
    if(h > n) break;
    G.push_back(h);
    h = 3*h + 1;
  }

  for(int i = G.size()-1;i >= 0;i--){
    insertionSort(A,n,G[i]);
  }
}

int main(){
  cin >> n;
  for(int i= 0;i < n;i++) scanf("%d",&A[i]);
  cnt = 0;

  shellsort(A,n);

  cout << G.size() << endl;
  for(int i = G.size()-1;i >= 0;i--){
    printf("%d",G[i]);
    if(i) printf(" ");
  }
  printf("\n");
  printf("%d\n",cnt);
  for(int i = 0;i < n;i++) printf("%d\n",A[i]);
  return 0;
}


Measured memory_bytes: 7196
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int LG = 60;

int obj(int l1, int r1, int l2, int r2) {
    //cout << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << endl;
    if (r1 < l2) {
        //cout << "sum" << endl;
        return (r1 - l1 + 1) + (r2 - l2 + 1);
    }   
    else {
        //cout << "all" << endl;
        return r2 - l1 + 1;
    }   
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    int l, r;
    cin >> l >> r;

    if (l == r) {
        cout << 1 << endl;
        exit(0);
    }   

    int b = -1;
    for (int i = LG; i >= 0; --i) {
        if ( ((l >> i) & 1) != ((r >> i) & 1) ) {
            b = i;
            break;   
        }   
    }   
    
    l &= (1ll << (b + 1)) - 1;
    r &= (1ll << (b + 1)) - 1;

    int lb = -1;
    for (int i = b - 1; i >= 0; --i) {
        if ((r >> i) & 1) {
            lb = i;
            break;
        }
    }    
    
    int tr;
    if (lb == -1)
        tr = r;
    else
        tr = r | ((1ll << lb) - 1);

    int suff = (1ll << b) - l;
    cout << obj(0, tr - (1ll << b), l, (1ll << b) - 1) + suff << endl;
}   

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)2e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

int main(){
	string str;
	cin >> str;
	int x, y;
	cin >> x >> y;
	vector<int> mx, my;
	mx.push_back(0);
	my.push_back(0);
	int tcount = 0;
	int fcount = 0;
	bool ver = false;
	bool started = true;
	foreach(i, str){
		if(started){
			if(i=='F'){
				--x;
				continue;
			}else{
				started = false;
			}
		}
		if(i=='T'){
			if(ver){
				mx.push_back(0);
				if(my.size()&&my.back()==0)my.pop_back();
			}else{
				my.push_back(0);
				if(mx.size()&&mx.back()==0)mx.pop_back();
			}
			ver = !ver;
		}else{
			if(ver)++my.back();
			else ++mx.back();
		}
	}
	bitset<24001> bs;
	bs.flip(16000);
	foreach(i, mx){
		bs = (bs << i) | (bs >> i);
	}
	if(!bs[x+16000]){
		cout << "No" << endl;
		return 0;
	}
	bs.reset();
	bs.flip(16000);
	foreach(i, my){
		bs = (bs << i) | (bs >> i);
	}
	if(!bs[y+16000]){
		cout << "No" << endl;
		return 0;
	}

	cout << "Yes" << endl;
	return 0;
}


Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include <map>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <assert.h>
#include <complex>
using namespace std;
#define eps 0.000001
#define LONG_INF 10000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define seg_size 65536*16
#define REP(i,n) for(long long i = 0;i < n;++i)
int cost[11] = { 100,2,5,5,4,5,6,3,7,6 };
int ok[11] = {};
pair<long long, long long> dp[20000];
int main() {
	int n, m;
	cin >> n >> m;
	REP(i, m) {
		int a;
		cin >> a;
		ok[a] = 1;
	}
	for (int i = 0; i < 20000; ++i) {
		dp[i] = make_pair(-1, 0);
	}
	dp[0] = make_pair(0,0);
	for (int i = 0; i < n; ++i) {
		if (dp[i].first != -1) {
			for (int t = 1; t <= 9; ++t) {
				if (ok[t] == 1) {
					int next = i + cost[t];
					if (dp[next].first < dp[i].first + 1) {
						dp[next] = make_pair(dp[i].first + 1, t);
					}else if (dp[next].first == dp[i].first + 1) {
						if (dp[next].second < t) {
							dp[next].second = t;
						}
					}
				}
			}
		}
	}
	while (n != 0) {
		cout << dp[n].second;
		n -= cost[dp[n].second];
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 512
N,K=map(int,input().split())
m=lambda x:x*(x-1)//2
M=lambda x:x*N-x*(x-1)//2
res=0
p=10**9+7
for k in range(K,N+2):
    res+=(M(k)-m(k)+1)%p
print(res%p)

Measured memory_bytes: 63976
#include<bits/stdc++.h>
#define pb(x) push_back(x) 
using namespace std;
vector<int> v[100005];
int num[100005][2];
pair<int,int> dfs(int n,int f){
	for(vector<int>::iterator it=v[n].begin();it!=v[n].end();it++){
		if(f==*it)
		continue;
		pair<int,int>p=dfs(*it,n);
		num[n][0]=max(num[n][0],p.first);
		num[n][1]=min(num[n][1],p.second);
	}
	return make_pair(num[n][0]-1,num[n][1]+1);
}
void dfs2(int n,int f){
	for(vector<int>::iterator it=v[n].begin();it!=v[n].end();it++){
		if(f==*it)
		continue;
		num[*it][0]=max(num[n][0]-1,num[*it][0]);
		num[*it][1]=min(num[n][1]+1,num[*it][1]); 
		dfs2(*it,n);
	}
} 
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
	{
		int a,b;
		scanf("%d %d",&a,&b);
		v[a].pb(b);
		v[b].pb(a); 
	}
	for(int i=0;i<=n;i++){
		num[i][0]=-1e9;
		num[i][1]=1e9;
	}
	int k;
	scanf("%d",&k);
	for(int i=0;i<k;i++){
		int v,p;
		scanf("%d %d",&v,&p);
		num[v][0]=p;num[v][1]=p;
		//printf("%d %d",num[v][0],num[v][1]);
	}
	dfs(1,0);
	dfs2(1,0);
	for(int i=1;i<=n;i++)
	if(num[i][0]>num[i][1]||(num[i][0]%2+2)%2!=(num[i][1]%2+2)%2){
		printf("No");
		return 0;
	}
	printf("Yes\n");
	for(int i=1;i<=n;i++)	
	printf("%d\n",num[i][1]);
}

Measured memory_bytes: 11904
#include<bits/stdc++.h>
const int MAXN=2001;
int n,a[MAXN];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	if(n<=2)
		return puts("0"),0;
	std::sort(a+1,a+n+1);
	int ans=0;
	for(int i=n;i>=2;--i){
		int p=0,q=i-1;
		while(p<q){
			if(a[p]+a[q]>a[i]){
				ans+=q-p;
				--q;
			} else ++p;
		}	
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR1(i,n) for(int (i)=1;(i)<(n);(i)++)
#define eFOR(i,n) for(int (i)=0;(i)<=(n);(i)++)
#define eFOR1(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i) sort((i).begin(),(i).end(), greater<int>());
#define YES(i) cout << ((i) ? "Yes" : "No") << endl;
constexpr auto INF = 1000000000;
constexpr auto LLINF = 1LL << 62;
constexpr auto mod = 1000000007;
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; }

int main() {

	int n, k;
	cin >> n >> k;
	deque<ll> v;
	FOR(i, n) {
		ll a;
		cin >> a;
		v.push_back(a);
	}

	ll ans = 0;
	eFOR(i, k)eFOR(j, i) {
		int a = j, b = i - j;
		deque<ll> c, V = v;
		FOR(l, a) {
			if (V.empty())break;
			c.push_back(V.front());
			V.pop_front();
		}
		FOR(l, b) {
			if (V.empty())break;
			c.push_back(V.back());
			V.pop_back();
		}
		SORT(c);
		ll cnt = 0;
		FOR(l, k-a-b) {
			if (c.empty())break;
			if (c.front() >= 0)break;
			c.pop_front();
		}
		while(!c.empty()) {
			cnt += c.front();
			c.pop_front();
		}
		chmax(ans, cnt);
	}
	cout << ans << endl;
}


Measured memory_bytes: 256
N = gets.to_i
A = gets.split.map(&:to_i)

odd, even = 2.times.map{ h = Hash.new(0); h[-1] = 0; h }
A.each_with_index{|v, i| (i.odd? ? odd : even)[v] += 1 }

wo = odd.max_by(2){|k,v| v}
we = even.max_by(2){|k,v| v}

puts [  we.first[0] == wo.first[0] ? N : N - we.first[1] - wo.first[1], 
        we.first[0] == wo.last[0]  ? N : N - we.first[1] - wo.last[1] ,
        we.last[0]  == wo.first[0] ? N : N - we.last[1]  - wo.first[1]  ].min


Measured memory_bytes: 17172
#include<bits/stdc++.h>
 
using namespace std;
 
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(vector<vector<T>> x){cerr<<"\n{"; int f=x.size();for(int i=0;i<f;i++){cerr<<"("; int g=x[i].size(); for(auto j:x[i]){cerr<<j; (--g!=0)?cerr<<",":cerr<<"";}cerr<<")\n";}cerr<<"}";}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void __print(vector<bool> x){cerr<<"{";int f=x.size();for(auto i:x){cerr<<i;(--f !=0)?cerr<<",":cerr<<"";}cerr<<"}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
 
#ifndef ONLINE_JUDGE
#define trace(x...) cerr << "[" << #x << "] = [", _print(x)
#else
#define trace(x...)
#endif
 
template<typename... T>
void read(T&... args) {
    ((cin >> args), ...);
}
 
template<typename... T>
void writ(T&&... args) {
    ((cout << args << " "), ...);
}
#define ull unsigned long long
#define ll long long
#define MOD 1000000007
#define INF (ll)1e18
#define IINF (int)1e9
#define EPS 1e-9
/*
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    In the blazing sun I saw you
    In the shadows hiding from yourself
    When the lights are on I know you
    See you're grey from all the lies you tell 
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,d; cin>>n>>d;
    ll ans=0;
    for(ll i=0;i<n;i++){
        ll x,y; cin>>x>>y;
        if((x*x)+(y*y)<=(d*d)) ans++;
    }
    cout<<ans<<endl;
    return 0;
}



Measured memory_bytes: 3624
import sys

S = input()

if len(S) == 1:
    print(0)
    sys.exit()


def f(i):
    ans = 0
    for s in range(len(S)):
        if int(S[s]) != i % 2:
            ans += 1
        i += 1
    return ans


print(min(f(0), f(1)))

Measured memory_bytes: 3188
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long i64;

int main()
{
	i64 n, x;
	i64 ans = 0;

	scanf("%lld%lld", &n, &x);

	ans += n;

	i64 a = x, b = n - x;

	while(a != 0 && b != 0) {

		if(a > b) swap(a, b);

		i64 t = b / a;
		i64 na = b - t * a;
		i64 nb = a;
		i64 d = a * 2 * t;

		ans += d;
		a = na;
		b = nb;
	}

	ans -= (a + b);

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

	return 0;
}


Measured memory_bytes: 128