akhauriyash/Code-Regression huggingface dataset:
def resolve():
    from collections import deque
    h, w = map(int, input().split())
    maze = [list(input()) for _ in range(h)]
    visited = [[-1]*w for _ in range(h)]
    nxt = [[0,0]]
    nxt = deque(nxt)
    visited[0][0] = 1
    while nxt:
        x, y = nxt.popleft()
        for i, j in ([1,0], [0,1], [-1,0], [0,-1]):
            nx = x+ i
            ny = y+j
            if 0 <= nx < h and 0 <= ny < w and visited[nx][ny]== -1 and maze[nx][ny] != "#":
                visited[nx][ny] = visited[x][y] + 1
                nxt.append([nx, ny])
    totalsharp = 0
    for i in range(h):
        for j in range(w):
            if maze[i][j] == "#":
                totalsharp += 1
    if visited[h-1][w-1] == -1:
        print(-1)
    else:
        print(h*w-totalsharp-visited[h-1][w-1])
resolve()

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(20) << fixed;
  long long N, X, M;
  cin >> N >> X >> M;
  set<long long> amari;
  long long ans = 0;
  ans += X;
  amari.insert(X);
  vector<long long> kotae(1);
  vector<long long> x;
  x.push_back(X);
  kotae.push_back(X);
  int i = 1;
  for(i; i <= N; i++){  
    X = X * X % M;
    if(amari.count(X)){  
      break; 
    }
    amari.insert(X);
    ans += X;
    kotae.push_back(ans);
    x.push_back(X);
  }
  int j = 0;
  long long tmp2 = 0;
  for(j = 0; j < N; j++){  
    if(x[j] == X){  
      break; 
    } 
    tmp2 += x[j];
  }
  long long tmp = 0;
  for(int k = j; k < x.size(); k++){  
    tmp += x[k];
  }
  int kukan = i - j;
  int saisyo = j;
  long long a = 0;
  for(int t = 0; t < (N - saisyo) % kukan; t++){  
    a += x[j + t];
  }
  cout << tmp2 + (N - saisyo) / kukan * tmp + a <<endl;
}


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

int main(){
  while(1){
    int i, j;
    vector<int> judge;
    string S;
    cin >> S;

    if(S == "#") break;

    for(i = 0; i < (int)S.size(); i++){
      if(S[i] == 'o') judge.push_back(0);
      else if(S[i] == 'e') judge.push_back(1);
    }


    int a;
    int sz = judge.size();
    if(judge[sz - 1] == 0) a = 0;
    else a = 90;
    int n = 1;

    for(j = sz - 2; j >= 0; j--){
      if(judge[j] == 0) a = (a * 2) - 90;
      else a = (a * 2) + 90;
      n *= 2;
    }

    int g;
    g = __gcd(a, n);
    a /= g;
    n /= g;


    if(n == 1) cout << a << endl;
    else cout << a << '/' << n << endl;

  }

  return 0;

}


Measured memory_bytes: 3208
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N, K;
int cnt[MAX_N];

void solve() {
	cin >> N >> K;
	rep(i, 0, N) {
		int a;
		cin >> a; a--;
		cnt[a]++;
	}
	vector<int> vec;
	rep(i, 0, N) {
		if(cnt[i]) vec.pb(cnt[i]);
	}
	if(sz(vec) <= K) {
		cout << 0 << "\n";
		return;
	}
	sort(all(vec), greater<int>());
	cout << accumulate(vec.begin() + K, vec.end(), 0) << "\n";
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
#ifdef LOCAL
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}



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

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = int (1e9) + int (1e5);
const ll INFL = ll(2e18) + ll(1e10);
const ui MOD = 1E9 + 7;
const double EPS = 1e-9;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define ROF(i,x) for(int i = (x) ; i >= 0 ; --i)
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define ODD(x) ( ((x)&1)==0?0:1 )
#define SIGN(x) ( ((x) > 0) - ((x) < 0)  )
#define dbg(x) cerr << #x"= " << x << endl

std::mt19937_64  generator(std::chrono::system_clock::now().time_since_epoch().count());

inline 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;}
ll gcd(ll a, ll b) { return a ? gcd(b%a, a): b; }
ll lcm(ll a, ll b) { return a / gcd(a,b) * b; }


void READ(bool _local){
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _DEBUG
    if (_local)
        freopen ("in.txt", "r", stdin);
#endif
}

int main() {
    READ(0);
    int h,a;cin>>h>>a;
    cout << h/a + (h%a!=0);

    return 0;
}



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

//priority_queue<int, vector<int>, greater<int>> min_heap;
//long long ccw(const Point& A, const Point& B, const Point& C) {
//	return 1LL * (B.x - A.x)*(C.y - A.y) - 1LL * (B.y - A.y)*(C.x - A.x);
//}
vector<int> a[200001];
vector<int> b[200001];
ll Sum[200001];
bool c[200001];
void go(int x) {
	c[x] = true;
	for (int i = 0; i < (int)a[x].size(); i++) {
		int y = a[x][i];
		if (!c[y]) {
			b[x].push_back(y);
			go(y);
		}
	}
}
void go1(int x) {
	for (int i = 0; i < (int)b[x].size(); i++) {
		int y = b[x][i];
		Sum[y] += Sum[x];
		go1(y);
	}
}
int main() { // E 
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n, q; cin >> n >> q;
	for (int i = 0; i < n - 1; i++) {
		int x, y; cin >> x >>  y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	go(1);
	while (q--) {
		int x, y; cin >> x >> y;
		Sum[x] += (ll)y;
	}
	go1(1);
	for (int i = 1; i <= n; i++) {
		cout << Sum[i] << ' ';
	}cout << '\n';

}


Measured memory_bytes: 38528
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
#include <set>
#include<math.h>
#include<vector>
#include<map>
#include<vector>
#include<cstring>

using namespace std;

int main(){
    int N,Q;
    string S;
    cin >>N >> Q >> S;
    vector<int> t(N+1,0);
    for(int i = 0;i < N;i++){
        if(i+1 < N && S[i] == 'A' && S[i+1] == 'C'){
            t[i+1] = t[i] + 1;
        }else{
             t[i+1] = t[i];
        }
    }
    for(int i = 0;i < Q;i++){
        int l,r;
        cin >> l >> r;
        --l,--r;
        cout << t[r] - t[l] << endl;

    }



}

Measured memory_bytes: 1408
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

const int MAX_N = 200000;
const int MAX_K = 10;

int N, K, L;

int par[MAX_N]; //親
int urank[MAX_N]; //木の深さ

ll kgid[MAX_N];
ll lgid[MAX_N];

// n要素で初期化
void init(int n)
{
    for (int i = 0; i < n; i++) {
        par[i] = i;
        urank[i] = 0;
    }
}

// 木の根を求める
int find(int x)
{
    if (par[x] == x) {
        return x;
    } else {
        return par[x] = find(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y)
{
    x = find(x);
    y = find(y);
    if (x == y)
        return;

    if (urank[x] < urank[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (urank[x] == urank[y])
            urank[x]++;
    }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y)
{
    return find(x) == find(y);
}

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

    cin >> N >> K >> L;

    init(N);

    for (int i = 0; i < K; i++) {
        int s, t;
        std::cin >> s >> t;
        unite(t - 1, s - 1);
    }

    for (int i = 0; i < N; i++) {
        kgid[i] = find(i);
    }

    init(N);

    for (int i = 0; i < L; i++) {
        int s, t;
        std::cin >> s >> t;
        unite(t - 1, s - 1);
    }

    for (int i = 0; i < N; i++) {
        lgid[i] = find(i);
    }

    unordered_map<ll, int> mp;

    for (int i = 0; i < N; i++) {
        ll key = kgid[i] * 1E+06 + lgid[i];
        if (!mp.count(key)) {
            mp.insert(make_pair(key, 1));
        } else {
            mp[key]++;
        }
    }

    for (int i = 0; i < N; i++) {
        cout << mp[kgid[i] * 1E+06 + lgid[i]] << "\n";
    }

    return 0;
}

Measured memory_bytes: 15352
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n;
  cin >> n;
  cout << (n % 2 ? 2 * n : n) << endl;
}

Measured memory_bytes: 3588
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, x, n) for(int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
const int ININF = 1000000;
bool is_prime(const unsigned n) {
    switch(n) {
    case 0: // fall-through
    case 1:
        return false;
    case 2:
        return true;
    } // n > 2 が保証された

    if(n % 2 == 0)
        return false;

    // 上で i=2 相当は調べたので、i=3から奇数のみ調べる
    for(unsigned i = 3; i * i <= n; i += 2) {
        if(n % i == 0)
            return false;
    }

    return true;
}
int main() {
    int N;
    cin >> N;
    vector<int> p;
    rep(i, 0, 400) {
        if(is_prime(i * 5 + 1)) {
            p.push_back(i * 5 + 1);
        }
    }
    rep(i, 0, N) {
        cout << p[i];
        if(i != N - 1) {
            cout << " ";
        }
    }
    cout << endl;
}


Measured memory_bytes: 256
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>
// #include <fstream>
#define typeof(x) __typeof__(x)
#define int long long int
#define mod(x) ((x % MOD) + MOD) % MOD
#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 ALL(c) (c).begin(),(c).end()
#define SIZE(c) (int)((c).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define SORT(c) sort(ALL(c))
#define LB(c,x) (int)(lower_bound(ALL(c),x)-(c).begin())
#define UB(c,x) (int)(upper_bound(ALL(c),x)-(c).begin())
#define COUNT(c,x) (int)(UB(c,x)-LB(c,x))
#define UNIQUE(c) SORT(c); (c).erase(unique(ALL(c)),(c).end());

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using namespace std;

typedef pair<int,int> P;
struct edge {
  int to, cost;
};

const int INF = 1e18;
const int MOD = 1e9+7;

template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << " ";
  }
  return s;
}

void dijkstra(const vector< vector<edge> >& G, vector<int>& d, int s)
{
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d.begin(), d.end(), INF);
  d[s] = 0;
  que.push(P(0, s));
  while(!que.empty()) {
    int temp_cost = que.top().first;
    int from = que.top().second;
    que.pop();
    if (d[from] < temp_cost) continue;
    for (int i = 0; i < G[from].size(); i++) {
      edge e = G[from][i];
      if (d[e.to] > d[from] + e.cost) {
        d[e.to] = d[from] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

signed main()
{
  // string filename; cin >> filename;
  // ifstream in(filename);
  // cin.rdbuf(in.rdbuf());

  int N; cin >> N;
  vector< vector<edge> > G(N, vector<edge>());
  rep(i, 0, N-1) {
    int a, b; cin >> a >> b;
    a--; b--;
    G[a].push_back((edge){ b, 1 });
    G[b].push_back((edge){ a, 1 });
  }

  vector< vector<int> > d(2, vector<int>(N));
  dijkstra(G, d[0], 0);
  dijkstra(G, d[1], N-1);

  int score = 0;
  rep(i, 0, N) {
    if (d[0][i] > d[1][i]) {
      score--;
    } else {
      score++;
    }
  }

  if (score > 0) {
    cout << "Fennec" << endl;
  } else {
    cout << "Snuke" << endl;
  }

  return 0;
}


Measured memory_bytes: 9756
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;

inline int getint()
{
	static char c;
	while ((c = getchar()) < '0' || c > '9');

	int res = c - '0';
	while ((c = getchar()) >= '0' && c <= '9')
		res = res * 10 + c - '0';
	return res;
}

const int MaxN = 200000;
const int MaxLogN = 17;
const int INF = 0x3f3f3f3f;

int n;
int a[MaxN + 1];

int val[2][MaxLogN + 1][MaxN + 1];

int log_t[MaxN + 1];

inline int mymin(int i, int j)
{
	return a[i] < a[j] ? i : j;
}

inline void build_table(int val[MaxLogN + 1][MaxN + 1])
{
	for (int k = 0; k < log_t[n]; ++k)
	{
		int up = n - (1 << k + 1) + 1;
		for (int i = 1; i <= up; ++i)
			val[k + 1][i] = mymin(val[k][i], val[k][i + (1 << k)]);
	}
}

inline int query(int d, int l, int r)
{
	int k = log_t[r - l + 1];
	int vl = val[d][k][l];
	int vr = val[d][k][r - (1 << k) + 1];
	return mymin(vl, vr);
}

struct object
{
	int l, r, p, q;
	object() {}
	object(const int &_l, const int &_r)
		: l(_l), r(_r)
	{
		p = query(l & 1, l, r);
		q = query(r & 1, p, r);
	}

	friend inline bool operator<(const object &lhs, const object &rhs)
	{
		return a[lhs.p] > a[rhs.p];
	}
};

priority_queue<object> q;

int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		a[i] = getint();

		int u = i & 1, v = !u;
		val[u][0][i] = i;
		val[v][0][i] = 0;
	}

	a[0] = INF;

	log_t[1] = 0;
	for (int i = 2; i <= n; ++i)
		log_t[i] = log_t[i >> 1] + 1;

	build_table(val[0]);
	build_table(val[1]);

	q.emplace(1, n);
	while (!q.empty())
	{
		object cur = q.top();
		q.pop();

		printf("%d %d ", a[cur.p], a[cur.q]);

		if (cur.l < cur.p)
			q.emplace(cur.l, cur.p - 1);
		if (cur.p < cur.q - 1)
			q.emplace(cur.p + 1, cur.q - 1);
		if (cur.q < cur.r)
			q.emplace(cur.q + 1, cur.r);
	}

	return 0;
}

Measured memory_bytes: 31732
#include <stdio.h>

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

  if((i-t)%2 ==0)
  {  if(i>t){ 
      n=(i-t)/2;
      printf("%d",n+t);
    }
  else if(i<t)
    {if((t-i)%2 ==0)
        n=(t-i)/2;
      printf("%d",n+i);
    }
  }
 else
   printf("IMPOSSIBLE");
}

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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = ReadInt();
            var F = new int[N, 10];
            var P = new int[N, 11];
            for (int i = 0; i < N; i++)
            {
                var input = ReadArrayInt();
                for (int j = 0; j < 10; j++) F[i, j] = input[j];
            }
            for (int i = 0; i < N; i++)
            {
                var input = ReadArrayInt();
                for (int j = 0; j < 11; j++) P[i, j] = input[j];
            }
            var ans = int.MinValue;

            for (int i = 1; i < (1 << 10); i++)
            {
                var tmp = 0;
                for (int j = 0; j < N; j++)
                {
                    var cnt = 0;
                    for (int k = 0; k < 10; k++)
                    {
                        if (F[j, k] == 1 && (i & (1 << k)) == (1 << k)) cnt++;
                    }
                    tmp += P[j, cnt];
                }

                if (tmp > ans) ans = tmp;
            }

            Console.WriteLine(ans);
            Console.ReadLine();
        }

        static string Read() { return Console.ReadLine(); }
        static int ReadInt() { return int.Parse(Read()); }
        static long ReadLong() { return long.Parse(Read()); }
        static int[] ReadArrayInt() { return Read().Split(' ').Select(s => int.Parse(s)).ToArray(); }
        static long[] ReadArrayLong() { return Read().Split(' ').Select(s => long.Parse(s)).ToArray(); }
    }
}

Measured memory_bytes: 13268
#include <bits/stdc++.h>
using namespace std;
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x,y,h,w) x >= 0 && x < h && y >= 0 && y < w

#define int long long
//typedef    long long          ll;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
 
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

class LCA{
	const int MAX_LOG = 20;
	int n;
	bool isDirected;
	vector<vector<int>> G,par;
	vector<int> depth;
	public:
	LCA(int _n,bool b) : n(_n),isDirected(b),par(MAX_LOG,vector<int>(_n,-1)),G(_n),depth(_n,-1){}
	void add_edge(int u,int v){
		G[u].pb(v);
		if(!isDirected) G[v].pb(u);
		else par[0][v] = u;
	}
	void dfs(int v,int p,int d){
		par[0][v] = p;
		depth[v] = d;
		for(int to : G[v]){
			if(to != p) dfs(to,v,d + 1);
		}
	}
	void build(){
		for(int i = 0;i < n;i++){
			if(depth[i] == -1){
				if(!isDirected || par[0][i] == -1) dfs(i,-1,0);
			}
		}
		for(int i = 0;i < MAX_LOG - 1;i++){
			for(int j = 0;j < n;j++){
				if(par[i][j] == -1) par[i + 1][j] = -1;
				else par[i + 1][j] = par[i][par[i][j]];
			}
		}
	}
	int lca(int u,int v){
		if(depth[u] > depth[v]) swap(u,v);
		for(int i = 0;i < MAX_LOG;i++){
			if((depth[v] - depth[u]) >> i & 1) v = par[i][v];
		}
		if(u == v) return u;
		for(int i = MAX_LOG - 1;i >= 0;i--){
			if(par[i][u] != par[i][v]){
				u = par[i][u];
				v = par[i][v];
			}
		}
		return par[0][u];
	}
};

signed main(){
	int n,q;
	cin >> n;
	LCA lca(n,true);
	for(int i = 0;i < n;i++){
		int k;
		cin >> k;
		for(int j = 0;j < k;j++){
			int v;
			cin >> v;
			lca.add_edge(i,v);
		}
	}
	lca.build();
	cin >> q;
	for(int i = 0;i < q;i++){
		int u,v;
		cin >> u >> v;
		cout << lca.lca(u,v) << endl;
	}
	return 0;
}


Measured memory_bytes: 28304
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
    os<<'[';
    rep(i,a.size()) os<<(i?" ":"")<<a[i];
    return os<<']';
}

int G[10][10];

int dp[10][10];
int V;

void warshall_floyd() {
    rep(k, V) rep(i, V) rep(j, V) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}


int main() {
    for(int n; cin >> n, n != 0;) {
        int m = 0;
        rep(i, 10) rep(j, 10) G[i][j] = INF;
        rep(i, 10) G[i][i] = 0;
        rep(i, n) {
            int a, b, c; cin >> a >> b >> c;
            G[a][b] = G[b][a] = c;
            m = max(m, max(a, b));
        }
        m++;
        V = m;
        rep(i, m) rep(j, m) dp[i][j] = G[i][j];
        warshall_floyd();
        int ans = INF;
        int p;
        rep(i, m) {
            int sum = 0;
            rep(j, m) sum += dp[i][j];
            if (ans > sum) {
                p = i;
                ans = sum;
            }
        }
        cout << p << ' ' << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 1160
from random import *
D=int(input())
C=list(map(int,input().split()))
S=[list(map(int,input().split())) for i in range(D)]
X=[0]*26
XX=[0]*26
P=[]
Y,Z=0,0
V,W=0,0
T,U=0,0
for i in range(D):
  for j in range(26):
    X[j]+=C[j]
    XX[j]+=X[j]
  Y,Z=-1,-10**20
  V,W=Y,Z
  T,U=V,W
  for j in range(26):
    if Y==-1:
      Y,Z=0,X[0]+S[i][0]
    if Z<X[j]+S[i][j]:
      T,U=V,W
      V,W=Y,Z
      Y,Z=j,X[j]+S[i][j]
    elif W<X[j]+S[i][j]:
      T,U=V,W
      V,W=j,X[j]+S[i][j]
    elif T<X[j]+S[i][j]:
      T,U=j,X[j]+S[i][j]
  XX[Y]-=X[Y]
  X[Y]=0
  P.append([Y,V,T])
  if randrange(0,25)==0:
    P[i][1]=randrange(0,26)
  if randrange(0,10)==0:
    P[i][2]=randrange(0,26)
  P[i]=tuple(P[i])
SCORE=0
X=[0]*26
for i in range(D):
  SCORE+=S[i][P[i][0]]
  for j in range(26):
    X[j]+=C[j]
  X[P[i][0]]=0
  SCORE-=sum(X)
SCORE2=0
YY=[]
for i in range(D):
  for k in range(2):
    d,q=i,P[i][k+1]
    SCORE2=SCORE
    YY=XX[:]
    SCORE2-=S[d][P[d][0]]
    SCORE2+=S[d][q]
    for j in range(26):
      X[j]=0
    YY[P[d][0]],YY[q]=0,0
    for j in range(D):
      if j==d:
        Z=q
      else:
        Z=P[j][0]
      X[P[d][0]]+=C[P[d][0]]
      X[q]+=C[q]
      X[Z]=0
      YY[P[d][0]]+=X[P[d][0]]
      YY[q]+=X[q]
    if SCORE2-sum(YY)>SCORE-sum(XX):
      z=[P[i][k+1]]
      for l in range(3):
        if l!=k:
          z.append(P[i][l])
      P[i]=tuple(z)
      XX=YY[:]
      SCORE=SCORE2
for i in range(D):
  print(P[i][0]+1)

Measured memory_bytes: 79776
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue<int>
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2 vector<vec>
#define vecll2 vector<vecll>
#define vec3 vector<vec2d>
#define vecll3 vector<vecll2d>
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
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 T> inline void print(vector<T> v){for(T e:v)cout<<e<<" ";cout<<endl;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
void print(vector<vec> v){for(auto e:v){for(auto ee:e)cout<<ee<<" ";cout<<endl;}}
void print(map<int,int> mp){for(auto e:mp)cout<<e.first<<" "<<e.second<<endl;cout<<endl;}

const int N=1e5+10;
const ll INF=1e9+7;
ll a[N<<4];
int sum[11];
main(){
    
    int h,w,n;scanf("%d%d%d",&h,&w,&n);
    int x,y;
    int cnt=0;
    ll m=(ll)(h-2)*(w-2);
    rep(iii,n){
        scanf("%d%d",&x,&y);
        rep(i,3)if( x-i>=1 && x-i<=h-2 ){
            rep(j,3)if(y-j>=1 && y-j<=w-2){
            a[cnt++]=INF*(x-i)+(y-j);
            }
        }
    }
    sort(a,a+cnt);
    ll t=1;
    rep(i,cnt){
        if(a[i]==a[i+1])t++;
        else sum[t]++,t=1,m--;
    }
    printf("%lld %d %d %d %d %d %d %d %d %d",m,sum[1],sum[2],sum[3],sum[4],sum[5],sum[6],sum[7],sum[8],sum[9]);
}



Measured memory_bytes: 8448
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
  long k,n,h,max=0,sum=0;
  vector<long> H;
  cin >> n >> k;
  for(int i=0; i<n; i++)
  {
    cin >> h;
    H.push_back(h);
  }
  sort(H.begin(), H.end());
  n -= k;
  for(auto He : H)
  {
    if(n <= 0)
      break;
    
    sum += He;
    n -= 1;
  }
  cout << sum << endl;
  return 0;
}

Measured memory_bytes: 2420
#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;


template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}
template<typename T>vector<T> & cset(vector<T> &A,T e=T()){for(auto &I:A){I=e;} return A;}


ll gcd(ll a,ll b){return (a%b?gcd(b,a%b):b);}

ll cul(ll A,ll a,ll b){
    if(A==0){return 0;}
    return A-A/a-A/b+A/(a/gcd(a,b)*b);
}

int main(){
    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    cout<<cul(b,c,d)-cul(a-1,c,d)<<endl;
    
    
    
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
int main()
{
  int n,m,a,b;
  cin>>n>>m;
  dsu d(n);
  for(int i=0;i<m;i++)
  {
    cin>>a>>b;
    a--;
    b--;
    d.merge(a,b);
  }
  int count = d.groups().size();
  cout<<count - 1<<endl;
}


Measured memory_bytes: 8868
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
int x,a,ar[10001];
cin>>x>>a;
int s=0;
for(int i=0;i<a;i++){
    cin>>ar[i];
    s=s+ar[i];
}
if(s>x)
    cout<<-1<<'\n';
    else
        cout<<x-s<<'\n';
}


Measured memory_bytes: 3604
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <math.h>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <utility>
#include <set>
#include <map>
using namespace std;
typedef long long int ll;

#define MOD 998244353


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

bool primeNumber(ll n){
    if(n < 2) return false;
    else{
        for(ll i = 2; i * i <= n; i++){
            if(n % i == 0) return false;
        }
        return true;
    }
}
  
  
int countDivisor(int n){
    ll ans = 0;
    for(ll i = 1; i<=n; i++){
        if(n%i == 0)ans++;
    }
    return ans;
}
int main() {
    int n;
    cin>>n;
    int ans = 0;
    for(ll i = 1; i<=n; i+=2){
        if(countDivisor(i) == 8)ans++;
    }
    cout<<ans<<endl;
    return 0;
}
    
    



Measured memory_bytes: 256
def main():
    n = int(input())
    s = input()

    cum = s[1:].count('E')
    ans = cum
    for i in range(1, n):
        if s[i - 1] == 'W':
            cum += 1
        if s[i] == 'E':
            cum -= 1

        ans = min(cum, ans)
    print(ans)

if __name__ == "__main__":
    main()


Measured memory_bytes: 42096
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct number {
  int siz, low, high;
  number(): siz(0), low(0), high(0) {
  }
};
int main() {
  do {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
  } while (false);
  int n, ans = INT_MAX;
  cin >> n;
  string str;
  cin >> str;
  vector<vector<int> > adj(n);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[--u].push_back(--v);
    adj[v].push_back(u);
  }
  auto add_edge = [&] (number a) {
    a.low += a.siz;
    a.high += a.siz;
    return a;
  };
  auto add = [&] (number a, number b) {
    number res;
    res.siz = a.siz + b.siz;
    res.high = a.high + b.high;
    if (a.low > b.low) {
      swap(a, b);
    }
    if (a.high > b.low) {
      res.low = res.high % 2;
    } else {
      res.low = b.low - a.high;
    }
    return res;
  };
  for (int root = 0; root < n; root++) {
    vector<number> value(n);
    function<int (int, int, int)> dfs = [&] (int u, int fa, int dep) {
      int res = 0;
      for (int v : adj[u]) {
        if (v != fa) {
          res += dfs(v, u, dep + 1);
          value[u] = add(value[u], add_edge(value[v]));
        }
      }
      if (str[u] == '1') {
        value[u].siz++;
        res += dep;
      }
      return res;
    };
    int res = dfs(root, -1, 0);
    if (!value[root].low) {
      ans = min(ans, res / 2);
    }
  }
  cout << (ans == INT_MAX ? -1 : ans) << endl;
  return 0;
}


Measured memory_bytes: 640
#include<stdio.h> 
#include<iostream> 
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#define cuttime ios::sync_with_stdio(false)
#define swap(x, y) x ^= y, y ^= x, x^= y
#define INF 0x3f3f3f3f
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define scan1(x) scanf("%d",&x)
#define scan2(x,y) scanf("%d%d",&x,&y)
#define scan3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scanll1(x) scanf("%lld",&x)
#define scanll2(x,y) scanf("%lld%lld",&x,&y)
#define scanll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define lowbit(x) (x&(-x))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=2e5+20;
const int MAX=10000007;
inline int read() {
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0',c=getchar();}
    return x*f;
}

inline void out(int x) {   
	if(x>9) out(x/10);   
	putchar(x%10+'0'); 
} 
char s[20];
int q[N];
std::map<int, int> mp;
const double p=acos(-1);
int main(){
	int n;
	n=read();
	rep(i,1,n-1){
		q[i]=read();
		mp[q[i]]++;
	}
	rep(i,1,n){
		printf("%d\n",mp[i]);
	}
	return 0;
}

Measured memory_bytes: 13908
function Main(input){
  input = input.split(" ").map(Number);
  var n = input[2]-input[0]
  var m = input[3]-input[1];
  console.log((input[2]-m)+" "+(input[3]+n)+" "+(input[0]-m)+" "+(input[1]+n));
}Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());

Measured memory_bytes: 7372
{-# LANGUAGE BangPatterns #-}

main :: IO ()
main = do
  [a, b, k] <- map read . words <$> getLine
  putStrLn $ unwords $ map show $ solve a b k

solve :: Int -> Int -> Int -> [Int]
solve !a !b  0 = [a, b]
solve !a !b  1 = [a1, b + a1]
  where
    a1 = a `div` 2
solve !a !b !k = solve (a1 + b1) b1 (k - 2)
  where
    a1 = a `div` 2
    b1 = (b + a1) `div` 2


Measured memory_bytes: 508
import itertools
import numpy as np

def distance(x2,x1,y2,y1):
    return ((x2-x1)**2 + (y2-y1)**2)**0.5

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

routes = list(itertools.permutations(range(N), N))
ans = []

for route in routes:
    dist = 0
    for i in range(1, N):
        dist += distance(X[route[i]], X[route[i-1]], Y[route[i]], Y[route[i-1]])
    ans.append(dist)

print(np.mean(ans))


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

const int N = 100 * 1000 + 5;

int a[N];
int b[N];
pair <int, int> s[N];
pair < pair <int, int>, int> dif[N];
int main() {
	int n;
	long long ans = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		cin >> b[i];
	//	dif[i] = {{a[i] - b[i], a[i]}, i};
		s[i] = {a[i] + b[i], i};
	}
/*	sort(dif, dif + n);
	int i;
	for (i = 0; i < n / 2; i++) {
		ans -= b[dif[i].second];
	}
	for (i; i < n; i++) {
		ans += a[dif[i].second];
	}
	cout << ans << endl;*/
	sort(s, s + n);
	for (int i = n - 1; i >= 0; i--) {
		if (i % 2 != n % 2) {
			ans += a[s[i].second];
		}
		else {
			ans -= b[s[i].second];
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  
  char c;
  cin >> c;
  
  if (c=='a' || c=='i' || c=='u' || c=='e' || c=='o')
    cout << "vowel" << endl;
  else
    cout << "consonant" << endl;
  
}

Measured memory_bytes: 256
#include <stdio.h>
int main(){

    int W,H,x,y,r;
    
    scanf("%d %d %d %d %d",&W,&H,&x,&y,&r);
    
    if(W<x+r || H<y+r){
        
        printf("No\n");
        
    }else if(x-r<0 || y-r<0){
        
        printf("No\n");
        
    }else{
    
        printf("Yes\n");
        
    }
    
    
    return 0;
}

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

using namespace std;

int main(){
    int N, a;
    cin >> N;
    vector<int> A;
    for(int i = 0; i < N; i++){
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(), A.end());
    for(int i = 1; i < N; i++){
        if(A[i] == A[i-1]){
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
    return 0;
}

Measured memory_bytes: 1400
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <list>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip> // setprecisionを使用するのに必要 cout << fixed << setprecision(15) << p でpを小数点以下15桁表示
using namespace std;
#define REPD(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) REPD(i, 0, n)
typedef long long ll;
#define fin(ans) cout << (ans) << '\n'
/*

*/
int main()
{
    int a,b,h;
    cin >> a >> b >> h;
    fin((a+b)*h/2);
    return 0;
}

Measured memory_bytes: 256
// Shift-JIS（シフトジス）

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

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

    int m = T[0], S = 0;
    for (int i = 0; i < N; i++)
    {
        if (abs(m) > abs(T[i]))
        {
            m = T[i];
        }
        S += T[i];
    }

    cout << S - m << endl;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>

int main()
{
  char s[100001];
  scanf("%s", s);
  char *p;
  int pa = 0;
  for (p = s; *p; ++p)
  {
    if (*p == 'p') pa++;
  }
  printf("%d\n", -pa + (p - s) / 2);
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <iomanip>

typedef long long ll;

using namespace std;

int main() {
    int n;
    cin >> n;
    
    string s;
    cin >> s;
    
    if(n % 2 == 0) {
        string copy1;
        string copy2;
        for(int i = 0; i < n; i++){
            if(i < n/2) {
                copy1.push_back(s[i]);
            }
            else {
                copy2.push_back(s[i]);
            }
        }
        
        if(copy1 == copy2) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    
    cout << "No" << endl;
    return 0;
}



Measured memory_bytes: 256
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools


sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

N, M, C = LI()
B = LI()
A = []

for i in range(N):
    A.append(LI())




res = 0


for i in range(N):
    #  print(i, A[i])
    x = sum([A[i][j] * B[j] for j in range(M)]) + C
    if x > 0: res += 1

print(res)


Measured memory_bytes: 6860
import java.util.Scanner;

public class Main{
public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
String str  = sc.next();
long K = sc.nextLong();
boolean ended = false;
//int count=0;

for(int i=0;i<K;i++){
  char value = str.charAt(i);
  if(value != '1'){
    System.out.println(value);
    ended=true;
    break;
  }
}

if(!ended){
  System.out.println('1');
}

}
}


Measured memory_bytes: 22228
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

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

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

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


fn main() {
    input! {
        abc: [i32; 3]
    }
    let mia = abc[2] - abc[0];
    let mic = abc[2] - abc[1];

    if mia * mic <= 0 {
        println!("Yes");
    } else {
        println!("No");
    }
}


Measured memory_bytes: 4352
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(a) (a).begin(),(a).end()
#define EQ(a,b) (abs((a)-(b)) < EPS)

#define sz size()
using namespace std;
typedef vector<int> vi;
typedef long double D;
typedef complex<D> P;
typedef pair<D,vi> sol;

const D EPS = 1e-8;
const D INF = 1e9;

namespace std{
  bool operator<(const P &a,const P &b){
    return EQ(imag(a),imag(b))?real(a)<real(b):imag(a)<imag(b);
  }
}

sol dp[41][41][41]; //dp[prev][cur][point_num];
vector<P> p,id;
vector<D> area;
vector<vi> vlist;

inline D dot(P x, P y){return real(conj(x)*y);}
inline D cross(P x, P y){return imag(conj(x)*y);}
inline D tri_area(P a, P b, P c){ return cross(b-a,c-a)/2; }

int ccw(P a,P b,P c){
  b -= a;c -= a;
  if (cross(b,c)>EPS) return 1;   //counter clockwise
  if (cross(b,c)<-EPS) return -1; //clockwise
  if (dot(b, c)<-EPS) return 2;   //c--a--b on line
  if (abs(b)<abs(c)) return -2;   //a--b--c on line
  return 0;                       //on segment
}

int main(){
  int n,q,x,y;
  
  cin >> n;
  p.resize(n); id.resize(n);
  rep(i,n){
    cin >> x >> y;
    id[i] = p[i] = P(x,y);
  }
  sort(all(p));

  area = vector<D>(n+1,INF);
  vlist = vector<vi>(n+1,vi());

  rep(init,n){
    rep(i,n)rep(j,n)rep(k,n+1)dp[i][j][k] = sol(INF,vi());
    for(int i=init+1;i<n;i++){
      vi tmp(2); tmp[0] = init; tmp[1] = i;
      dp[init][i][2] = sol(0,tmp);
    }
    for(int num=2;num<n;num++){
      for(int prev=init;prev<n;prev++){
	for(int cur=init+1;cur<n;cur++){
	  for(int nxt=init+1;nxt<n;nxt++){
	    if(ccw(p[init],p[cur],p[nxt])!=1)continue;
	    if(cross(p[cur]-p[prev],p[nxt]-p[cur]) <= EPS)continue;

	    D S = dp[prev][cur][num].first + tri_area(p[init],p[cur],p[nxt]);
	    if(S +EPS < dp[cur][nxt][num+1].first){
	      dp[cur][nxt][num+1].first = S;
	      dp[prev][cur][num].second.push_back(nxt);
	      dp[cur][nxt][num+1].second = dp[prev][cur][num].second;
	      dp[prev][cur][num].second.pop_back();
	    }
	  }
	}
      }
    }

    rep(i,n)rep(j,n)rep(k,n+1){
      if(area[k] > dp[i][j][k].first + EPS){
	area[k] = dp[i][j][k].first;
	vlist[k] = dp[i][j][k].second;
      }
    }
  }

  cin >> q;
  while(q--){
    cin >> x;
    if(EQ(area[x],INF))cout << "NA\n";
    else{
      rep(i,vlist[x].sz)rep(j,n){
	if(EQ(p[vlist[x][i]],id[j])){
	  cout << j+1 << (i+1==(int)vlist[x].sz?"\n":" ");
	  break;
	}
      }
    }
  }
}

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

#ifdef DEBUG
#define deb(...) cerr __VA_ARGS__
#else
#define deb(...)
#endif
using namespace std;
typedef long long ll;

vector<ll> dziel(ll x)
{
	vector<ll> d;
	for(ll i=2; i*i <= x; ++i)
	{
		if(x % i == 0)
			d.push_back(i);
		
		while(x % i == 0)
			x /= i;
	}
	
	if(x != 1)
		d.push_back(x);
		
	return d;
	
}
int main()
{
	ios::sync_with_stdio(0);
	
	ll A, B;
	cin >> A >> B;
	
	auto s = dziel(A);
	
	int ile=1;
	
	for(ll x : s){
		//cout << x << " ";
		if(B % x == 0)
			++ile;
	}
			
	cout << ile;
}


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

std::string eraser = "eraser";
std::string erase = "erase";
std::string dream = "dream";
std::string dreamer = "dreamer";

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

    // eraserの削除
    std::string::size_type npos = S.find(eraser, 0);
    while(npos != std::string::npos){
        S.erase(npos, eraser.size());
        npos = S.find(eraser, npos);
    }

    // eraseの削除
    npos = S.find(erase, 0);
    while(npos != std::string::npos){
        S.erase(npos, erase.size());
        npos = S.find(erase, npos);
    }

    // 残りがdreamとdreamerのみであることを確認
    npos = S.find(dreamer, 0);
    while(npos != std::string::npos){
        S.erase(npos, dreamer.size());
        npos = S.find(dreamer, npos);
    }

    npos = S.find(dream, 0);
    while(npos != std::string::npos){
        S.erase(npos, dream.size());
        npos = S.find(dream, npos);
    }

    if(S.empty()){
        std::cout << "YES" << std::endl;
    }else{
        std::cout << "NO" << std::endl;
    }
    
    return 0;
}


Measured memory_bytes: 512
h, w = gets.split.map(&:to_i)
n = gets.to_i
colors = gets.split.map(&:to_i)
ans = Array.new(h) { Array.new(w) }
tmp = []
colors.each.with_index(1) do |num, color|
  num.times { tmp << color }
end
h.times do |r|
  w.times do |c|
    c = w - c - 1 if r.odd?
    ans[r][c] = tmp.shift
  end
end
ans.each { |a| puts a.join(" ") }


Measured memory_bytes: 2556
# include <algorithm>
# include <cassert>
# include <cctype>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <cmath>
# include <iostream>
# include <map>
# include <queue>
# include <set>
# include <sstream>
# include <stack>
# include <string>
# include <vector>
# include <iomanip>

# define rep(i, n) for (int i = 0; i < (int)(n); i++)
# define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
# define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;
typedef long long ll;


// %% begin matrix %%

typedef vector<int> vec;
typedef vector<vec> mat;

void pretty_print(mat g) {
  rep (i, g.size()) {
    rep (j, g[0].size()-1) {
      cout << g[i][j] << ' ';
    }
    cout << g[i][g[0].size()-1] << endl;
  }
}


mat mul(mat A, mat B) {
  mat C(A.size(), vec(B.size()));
  rep (i, A.size()) {
    rep (k, B.size()) {
      rep (j, B[0].size()) {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return C;
}

// %% end matrix %%

# define MAX_V 100000
# define MAX_E 500000

# define INF 1001001001

struct edge {int from, to, cost;};

int V, E;
vector<int> d(MAX_V);
edge es[MAX_E];


void bellman_ford(int s) {
  
  // NOTE : initialize
  rep (i, V) d[i] = INF;
  d[s] = 0;

  while (true) {
    bool update = false;

    rep (i, E) {
      edge e = es[i];

      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }

    if (!update) break;
  }
}



int main() {

  int s;
  cin >> V >> E >> s;

  rep (i, E) {
    int f, t, c;
    cin >> f >> t >> c;
    es[i].from = f;
    es[i].to = t;
    es[i].cost = c;
  }

  bellman_ford(s);

  rep (i, V) {
    if (d[i] == INF) {
      cout << "INF" << endl;
    }
    else {
      cout << d[i] << endl;
    }
  }
}

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

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

int main(){
  int N;
  cin>>N;
  vector<ll> A(N);
  for(ll i=0;i<N;i++){
    cin>>A.at(i);
    A.at(i)-=i+1;
  }
  sort(A.begin(),A.end());
  int x=A.at(N/2);
  ll sum=0;
  for(int i=0;i<N;i++){
    A.at(i)-=x;
    sum+=abs(A.at(i));
  }
  cout<<sum<<endl;

}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
#define all(x) (x).begin(),(x).end()

int main(){
    ll n, ans = 0;
    cin >> n;
 
    map<string,ll> mp;
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        sort(all(s));
        ans += ++mp[s]-1;
    }
 
    cout << ans << endl;
    return 0;
 
}

Measured memory_bytes: 11136
#include<iostream>
using namespace std;
 
int main(){
  int m,f,b;
  cin>>m>>f>>b;

  if(b<=m+f)

   if(b<=m) cout<< 0 <<endl;
    else
    cout<<b-m<<endl;

  else
   cout<<"NA"<<endl;

  return 0;
}


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

int main(){
  long long n,m; cin >> n >> m;
  long long p[m];
  string s[m];
  for(long long i=0;i<m;i++) cin >> p[i] >> s[i];
  long long correct=0,wrong=0;
  bool flag=true;
  set <int> a;
  map<int,int> tmp_wrong;
  long long focus=p[0];
  for(long long i=0;i<m;i++){
    if(!a.count(p[i])&&s[i]=="WA"){
      tmp_wrong[p[i]]++;
    }else if(!a.count(p[i])&&s[i]=="AC"){
      a.insert(p[i]);
      wrong+=tmp_wrong[p[i]];
      correct++;
    }
  }
  cout << correct << " " << wrong <<endl;
}

Measured memory_bytes: 15872
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

struct UnionFind
{
    vector<int> data;
    UnionFind() {}
 
    // initialize
    UnionFind(int size) : data(size, -1) {}
 
    void unite_set(int x, int y){
        x = root(x);
        y = root(y);
        bool is_unite = (x != y);
        
        if (is_unite){
            if (data[y] < data[x]){
                swap(x, y);
            }
            data[x] += data[y];
            data[y] = x;
        }
    }
 
    bool is_same(int x, int y){
        return root(x) == root(y);
    }
 
    int root(int x){
        return (data[x] < 0) ? x : data[x] = root(data[x]);
    }
 
    int size(int x){
        return -data[root(x)];
    }
};

int main()
{
    int n, q, m;
    int a, b;
    cin >> n;
    struct UnionFind d(n);
    vector<int> data;
    data.resize(n);
    
    for (int i=0; i<n; ++i){
        data[i] = i;
    }
    // unite
    cin >> m;
    for (int i=0; i<m; ++i){
        cin >> a >> b;
        d.unite_set(a, b);
    }
    // find
    cin >> q;
    for (int i=0; i<q; ++i){
        cin >> a >> b;
        if(d.is_same(a, b)){
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}



Measured memory_bytes: 3592