akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
    ll a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<char> &s, char a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//const ll mod = 1e10;
typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;

struct Triangle {
    int parent;
    vector<int> children;
};

class Tree {
public:
    vector<vector<int>> edges;

    Tree(int n) : edges(n) {}

    void edge(int i, int j) {
        edges[i].push_back(j);
        edges[j].push_back(i);
    }

    void root(int r, vector<Triangle> &leafs) {
        queue<int> vs;
        vs.push(r);
        vector<bool> check(edges.size(), false);
        check[r] = true;
        while (!vs.empty()) {
            int k = vs.front();
            Triangle triangle;
            triangle.parent = k;

            for (int i : edges[k]) {
                if (check[i]) continue;
                check[i] = true;
                triangle.children.push_back(i);
                vs.push(i);
            }
            vs.pop();
            leafs.push_back(triangle);
        }
        reverse(leafs.begin(), leafs.end());
    }

    int max_v() {
        int v = 0;
        for (auto &e: edges) {
            cmax(v, (int) e.size());
        }
        return v;
    }

    int get_v(int v) {
        rep(i, edges.size()) {
            auto &e = edges[i];
            if (e.size() == v) return i;
        }
        return -1;
    }

};

int main() {
    int n;

//    ifstream is("/home/riku/Downloads/1_10.txt");

    cin >> n;
//    is >> n;
    Tree tree(n);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
//        is >> a >> b;
        tree.edge(a, b);
    }


    if (tree.max_v() <= 2) {
        cout << 1 << endl;
        ret();
    }

    int root = tree.get_v(tree.max_v());

    vector<Triangle> leafs;
    tree.root(root, leafs);

    vector<int> dp(n, 0);

    for (auto &e : leafs) {

        int child_sum = 0;
        int empty = 0;
        for (int i : e.children) {
            child_sum += dp[i];
            if (dp[i] == 0) empty++;
        }

        int next = child_sum + max(empty - 1, 0);


        dp[e.parent] = next;
    }


    cout << dp[root] << endl;
}

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


int main(){
	auto read_string 	=	[](){ string s; cin >> s; return s; };
	auto read_int       =   [](){ int tmp; cin >> tmp; return tmp; };
	auto read_long_long =   [](){ long long int tmp; cin >> tmp; return tmp; };
	auto read_char      =   [](){ char c; cin >> c; return c; };
	auto str_repeat	 	=	[](string a,int count){	string res = ""; for(size_t i = 0 ; i < count ; i++) res += a; return res;	};
	auto to_lower_case 	= 	[](string s){ string res=""; for(size_t i = 0 ; i < s.size() ; i++) res += tolower(s[i]); return res; };
	auto to_upper_case 	= 	[](string s){ string res=""; for(size_t i = 0 ; i < s.size() ; i++) res += toupper(s[i]); return res; };
	auto read_vec_int   =   [](int count){ vector<int> tmp(count); for(size_t i = 0 ; i < count ; i++) cin >> tmp[i]; return tmp; };
	auto title_case     =   [](string s) { string ret = ""; ret+=toupper(s[0]); ret += s.substr(1); return ret; };
	auto char_to_int    =   [](char c){ return c - '0'; };

	int n = read_int();
	if(n == 2){
		cout <<  "1" << endl;
		exit(0);
	}

	int num_odds=0;int num_evens = 0;
	for(size_t i = 1 ; i <= n  ; i++){
		if(i % 2== 0) num_evens++;
		else num_odds++;
	}

	cout <<  num_odds * num_evens << endl;


	return 0;
}

Measured memory_bytes: 256
n,y = map(int, input().split())

for i in range(n + 1):
    for j in range(n - i + 1):
        if i * 10000 + j * 5000 + (n - i - j) * 1000 == y:
            print(i, j, n - i - j)
            exit()
print("-1 -1 -1")

Measured memory_bytes: 2940
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define DEBUGP(val) cerr << #val << "=" << val << "\n"

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

const int N = 10001;
int pr[N];

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  REP(i, 2, N) pr[i] = 1;
  REP(i, 2, N) {
    if (!pr[i]) continue;
    for (int j = 2 * i; j < N; j += i) pr[j] = 0;
  }
  int n;
  while (cin >> n) {
    int c = 0;
    REP(i, 1, n + 1) c += pr[i] && pr[n + 1 - i];
    cout << c << endl;
  }
}



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

#define ll long long 
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define FOR(i,a,b) for (int i=(a); i<(b); i++)
#define REP(i,n) for (int i=0; i<(n); i++)
#define RREP(i,n) for (int i=(n-1); i>=0; i--)
#define ALL(x) x.begin(),x.end()
#define SZ(x) x.size()
#define MNTO(a,b) a = min(a,(__typeof__(a))(b))
#define MXTO(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define debug(x) cerr<<#x<<" is "<<x<<endl

using namespace std;

const int maxn = 3e5+5;
const int intinf = 1<<29;
const ll inf = 1ll<<60;


void GG(){cout<<"No\n"; exit(0);}

bool p[maxn];
int nb[maxn];

int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
   	ll a, b; cin>>a>>b;
   	if (a<=0&&b>=0) return cout<<"Zero"<<endl, 0;
   	if (b<0) {
   		cout<<((b-a+1)%2==0?"Positive":"Negative")<<endl;
   	}else{
   		cout<<"Positive"<<endl;
   	}

}

Measured memory_bytes: 256
N, K, Q = map(int, input().split())
A = list(map(int, input().split()))
def list_split(arr, x):
    ret = []
    d = []
    for v in arr:
        if v >= x:
            d.append(v)
        elif d:
            ret.append(d)
            d = []
    else:
        if d:
            ret.append(d)
    return ret

ans = 10 ** 9
for x in sorted(A):
    y = []
    for arr in list_split(A, x):
        la = len(arr)
        if la >= K:
            y += sorted(arr)[:min(la - K + 1, Q)]
    if len(y) >= Q:
        ans = min(ans, sorted(y)[Q - 1] - x)

print(ans)

Measured memory_bytes: 46044
def main():
    import collections
    n = int(input())
    mylist = []
    for i in range(n):
        s = input()
        mylist.append(s)
    c = collections.Counter(mylist)
    print('AC x ' + str(c['AC']))
    print('WA x ' + str(c['WA']))
    print('TLE x ' + str(c['TLE']))
    print('RE x ' + str(c['RE']))
if __name__ == "__main__":
    main()

Measured memory_bytes: 16548
/*input
3
20 10
20 20
20 30
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}

//}}}
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=80000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const double PI=acos(-1);
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		
		b>>=1;
	}
	return res;
}

ll a[maxn],b[maxn];
vector<ll> v;
int main()
{
	IOS;
	int n;
	cin>>n;
	REP(i,n) cin>>a[i]>>b[i];
	ll ans=0;
	REP(i,n) ans-=b[i];
	REP(i,n) v.pb(a[i]+b[i]);
	sort(ALL(v),greater<ll>());
	REP(i,n) if(i%2==0) ans+=v[i];

	cout<<ans<<'\n';
	return 0;
}


Measured memory_bytes: 2936
int main()
{
   int a,b,h,sum=0;

   scanf("%d",&a);
   scanf("%d",&b);
   scanf("%d",&h);
   if (1<=a<=100 && 1<=b<=100 && 1<=h<=100 && h%2==0)
    sum=((a+b)*h)/2;
   {
       printf("%d",sum);
   }
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;
vector<pair<int,int> > v[100005];
bool vis[100005];
int col[100005];
void dfs(int x,int c){
    col[x] = c;
    vis[x] = 1;
    for(pair<int,int> p:v[x]){
        int y = p.fi;
        int w = p.se;
        if(vis[y])continue;
        if(w%2)dfs(y,1-c);
        else dfs(y,c);
    }
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int x,y,w;
        cin>>x>>y>>w;
        v[x].pb({y,w});
        v[y].pb({x,w});
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)cout<<col[i]<<endl;
}


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

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

int main()
{   
    int n; cin >> n;
    vector<int> p(n);
    for(int i=0;i<n;i++) cin >> p[i];
    int mi = p[0];
    int res = 1;
    for(int i=1;i<n;i++){
        mi = min(mi,p[i]);
        if(mi == p[i]) res++;
    }
    cout << res << endl;
}
    

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define ll long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<#x<<"="<<x<<endl;
#define endl '\n'
#define M 1000000007
#define int long long
#define INF 1e18
#define max_n 1000000

using namespace std;

void solve()
{
	ll n;
	cin >> n;
	map<string, ll> m;
	while (n--)
	{
		string s;
		cin >> s;
		m[s]++;
	}
	cout << "AC x " << m["AC"] << endl;
	cout << "WA x " << m["WA"] << endl;
	cout << "TLE x " << m["TLE"] << endl;
	cout << "RE x " << m["RE"] << endl;
}

int32_t main()
{
	IOS
	ll T = 1;
	// cin>>T;
	for (ll i = 1; i <= T; ++i)
	{
		// cout<<"Case #"<<i<<": ";
		solve();
	}
	return 0;
}

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

ll dp[10001][2][101];

int main()
{
	memset(dp,0,10001*2*101*sizeof(ll));
	string N;
	cin >> N;
	ll n = N.size();
	ll d;
	cin >> d;
	dp[0][0][0] = 1;
	for(int i = 1;i <= n;++i)
	{
		for(int j=0;j<d;j++)
		{
			for(char c = '0';c<='9' ; c++)
			{
				if(N[i-1]>c)
				{
					dp[i][1][(j+(c-'0'))%d] =(dp[i][1][(j+(c-'0'))%d]+ dp[i-1][0][j])%MINF;
				}
				else if(N[i-1]==c)
				{
					dp[i][0][(j+(c-'0'))%d] = (dp[i][0][(j+(c-'0'))%d] + dp[i-1][0][j])%MINF;
				}
				dp[i][1][(j+(c-'0'))%d] = (dp[i][1][(j+(c-'0'))%d]+dp[i-1][1][j])%MINF;
				
			}
		}
	}

	cout<<((dp[n][1][0]+dp[n][0][0])-1+MINF)%MINF<<endl;

	return 0;
}

Measured memory_bytes: 16000
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;

static int N;

/*
求某一?公室到其他?公室的最短距?。
多??入，n表示n条?系，下面n次?次?入 x y d表示x到y的距?是d。需要注意的是n没有?定，需要根据?入来求。
?出?公室的?号和距?
*/
static int x, y, distances;
static int n;
//static int vis[100];
static const int INF = 1 << 10;
static int dist[100][100];


void floyd()
{
	for (int k = 0; k <= n; k++)
	{
		for (int i = 0; i <= n; i++)
		{
			for (int j = 0; j <= n; j++)
			{
				if (dist[i][k] + dist[k][j] < dist[i][j])
				{
					dist[i][j] = dist[i][k] + dist[k][j];
				}
			}
		}
	}


}




int main()
{
	while (scanf("%d", &N) != EOF)
	{
		if (N == 0) break;
		n = -1;
		fill(dist[0], dist[0] + 100 * 100, INF);
		for (int i = 0; i < N; i++)
		{
			scanf("%d%d%d", &x, &y, &distances);
			n = max(n, x);
			n = max(n, y);
			dist[x][y] = dist[y][x] = min(dist[x][y], distances);
		}
		floyd();
		int u = -1, ans = 1<<30, sum = 0;
		for (int i = 0; i <= n; i++)
		{
			sum = 0;
			for (int j = 0; j <= n; j++)
			{
				if (i != j) sum += dist[i][j];
				//printf("j:%d dist:%d", j, dist[i][j]);
			}
			if (sum < ans)
			{
				u = i;
				ans = sum;
			}
			//printf("\ni:%d sum:%d\n", i, sum);
		}

		printf("%d %d\n", u, ans);
	}
	return 0;
}

Measured memory_bytes: 3168
#include <iostream>
#include <stack>
#include <vector>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
struct point_t { int x, y; };
template <typename T>
bool on(point_t p, T const & xss) {
    return 0 <= p.y and p.y < xss.size()
        and 0 <= p.x and p.x < xss.front().size();
}
const point_t directions[] = { {0,1},{0,-1},{1,0},{-1,0} };
point_t operator + (point_t a, point_t b) { return { a.x + b.x, a.y + b.y }; }
vector<vector<int> > shock(int c, vector<vector<int> > const & p) {
    assert (1 <= c and c <= 6);
    vector<vector<int> > q = p;
    stack<point_t> s;
    s.push({ 0, 0 });
    while (not s.empty()) {
        point_t a = s.top(); s.pop();
        if (q[a.y][a.x] == c) continue;
        q[a.y][a.x] = c;
        for (point_t b : directions) {
            b = a + b;
            if (on(b, p) and q[b.y][b.x] == p[0][0]) {
                s.push(b);
            }
        }
    }
    return q;
}
int count(vector<vector<int> > const & p) {
    vector<vector<bool> > used(p.size(), vector<bool>(p.front().size()));
    int result = 0;
    stack<point_t> s;
    s.push({ 0, 0 });
    while (not s.empty()) {
        point_t a = s.top(); s.pop();
        if (used[a.y][a.x]) continue;
        used[a.y][a.x] = true;
        result += 1;
        for (point_t b : directions) {
            b = a + b;
            if (on(b, p) and not used[b.y][b.x] and p[b.y][b.x] == p[0][0]) {
                s.push(b);
            }
        }
    }
    return result;
}
int solve(int i, vector<vector<int> > const & p, int c) {
    if (i == 4) {
        return count(shock(c, p));
    }
    int result = 0;
    for (int j : { 1,2,3,4,5,6 }) {
        result = max(result, solve(i+1, shock(j,p), c));
    }
    return result;
}
int main() {
    while (true) {
        int h, w, c; cin >> h >> w >> c;
        if (h == 0 and w == 0 and c == 0) break;
        vector<vector<int> > p(h, vector<int>(w));
        repeat (y, h) {
            repeat (x, w) {
                cin >> p[y][x];
            }
        }
        cout << solve(0,p,c) << endl;
    }
    return 0;
}

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

int main()
{
	ll n,a,b;
    cin>>n>>a>>b;
    
    
    if((b-a)%2==0)
    {
		cout<<(b-a)/2<<endl;
    }
    else
    {
      if(a==1 || b==n)
      	cout<<(b-a-1)/2+1<<endl;
      else 
      {
        cout<<min(a-1,n-b)+abs(b-a-1)/2+1<<endl;
      }
      
    }
	return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    static boolean[] bits;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        boolean[][] input=new boolean[m][n];
        bits=new boolean[n];
        int[] p=new int[m];
        int k;
        for(int i=0;i<m;i++){
          k=sc.nextInt();
          for(int j=0;j<k;j++){
            input[i][sc.nextInt()-1]=true;
          }
        }
        for(int i=0;i<m;i++){
          p[i]=sc.nextInt();
        }
        int tmp1;
        boolean tmp2;
        int ans=0;
        for(int i=0;i<Math.pow(2,n);i++){
          tmp2=true;
          for(int j=0;j<m;j++){
            tmp1=0;
            for(int l=0;l<n;l++){
              if(input[j][l]&&bits[l])tmp1++;
            }
            tmp2&=tmp1%2==p[j];
          }
          if(tmp2)ans++;
          if(i!=Math.pow(2,n)-1)update(0);
        }
        System.out.println(ans);

    }
    static void update(int pointer){
        if(!bits[pointer]){
            bits[pointer]=true;
        }else{
            bits[pointer]=false;
            update(pointer+1);
        }
    }
}


Measured memory_bytes: 21972
n,m,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a,b = [0],[0]
for i in range(n):
  a.append(a[i]+A[i])
for i in range(m):
  b.append(b[i]+B[i])
  
ans,bi = 0,m
for i in range(n+1):
  for j in range(bi,-1,-1):
    if a[i]+b[j]<=k:
      ans = max(ans,i+j)
      bi = j
      break
print(ans)

Measured memory_bytes: 133992
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}

int main() {
    int n;
    cin >> n;
    int p[n];
    rep(i,n){
        cin >> p[i];
    }
    int ans = 0;

    srep(i,1,n-1){
        if(min(p[i-1],p[i+1])<p[i]&&p[i]<max(p[i-1],p[i+1]))ans++;
    }

    cout << ans << endl;
    return 0;
}
 
 


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

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if ((a<c&&b>c)||(a>c&&b<c)) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
}

Measured memory_bytes: 256
//author: chinmay0402
//One day your whole life will flash before your eyes, make sure you make it worth watching
#include<bits/stdc++.h>
using namespace std;

//policy based data structures
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

#define ll long long int
#define lld long double
#define fl float
#define vs vector<string>
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll>>
#define fst first
#define scd second
#define sll set<ll>
#define mll map<ll,ll>
#define flash ios_base::sync_with_base(false);cin.tie(NULL);
#define INF (ll)10000000000000000
#define MOD (ll)1000000007
#define pb push_back
#define mll map<ll,ll>
#define test ll t;cin>>t;while(t--)
#define sortv(v) sort(v.begin(),v.end())
#define max3(a,b,c) max(a,max(b,c))

#define forn(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define fornr(i,n) for(ll i=n-1;i>=0;i--)
#define forab(i,a,b,c) for(ll i=a;i<=b;i+=c)

template<typename T>
void read(T &x){
 cin>>x;
}
template<typename T,typename T1>
void read(T &x, T1 &y){
 cin>>x>>y;
}
template<typename T,typename T1, typename T2>
void read(T &x, T1 &y, T2 &z){
 cin>>x>>y>>z;
}
template<typename T,typename T1, typename T2, typename T3>
void read(T &x, T1 &y, T2 &z , T3 &a){
 cin>>x>>y>>z>>a;
}
template<typename T>
void reada(T oneD,ll n){
 for(ll i=0;i<n;i++)
  cin>>oneD[i];
}
template <typename T>
void readv(vector< T > &oneD,ll n){
 forn(i,n){
  ll x;
  read(x);
  oneD.push_back(x);
 }
}
ll gcd(ll a, ll b){
 if(a==0)
  return b;
 else
  return gcd(b%a,a);
}

ll gcdExt(ll a,ll b, ll &x, ll &y){
 if(a==0){
  x=0;
  y=1;
  return b;
 }
 ll x1,y1;
 ll gcd=gcdExt(b%a,a,x1,y1);
 x=y1-(b/a)*x1;
 y=x1;
 return gcd;
}

ll pwr(ll a, ll b){
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a;
  a*=a;
  b>>=1;
 }
 return res;
}

ll pwrm(ll a, ll b, ll m){
 a%=m;
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a%m;
  a=a*a%m;
  b>>=1;
 }
 return res;
}

vector<bool>sieve(1000000,true);
void Sieve(){
 sieve[0]=false;
 sieve[1]=false;
 for(ll i=2;i*i<=1000000;i++){
  if(sieve[i]==true){
   for(ll j=i*i;j<1000000;j+=i)
    sieve[j]=false;
  }
 }
}

vll sieve_spf(1000001);
void Sieve_spf(){
 ll n=1000001;
 
 forn(i,n)
  sieve[i]=i;
 
 sieve[0]=-1;
 sieve[1]=-1;
 
 forab(i,2,n,2)
  sieve[i]=2;
 
 forab(i,3,n,2)
  if(sieve[i]==i)
   forab(j,i*i,n,i)
    if(sieve[j]==j)
     sieve[j]=i;
}


int main(){
 ll n;
 read(n);
 ll a[n-1];
 reada(a,n-1);
 mll mp;
 for1(i,n)
  mp[i]=0;
 forn(i,n-1){
  mp[a[i]]++;
 }
 for(auto x :mp){
  cout<<x.scd<<"\n";
 }
 return 0;
}

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

using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long int

int main(){
  int a,b;
  cin>>a>>b;
  int count=0;
  for(int i=1;i<b-a;i++)
    count+=i;
  cout<<count-a<<endl;


  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdint>
using namespace std;
int main() {
	int N, A, B;
	cin >> N >> A >> B;
	if (A > B) cout << 0;
	else if (N == 1 && A < B) cout << 0;
	else cout << (int64_t)
	(B - A) * (N - 2) + 1;
	return 0;
}
/*
A~BまではB-A+1通りの数字がある
N個において、A*(N-1)+BからA+B*(N-1)までの通りがあるので、
A+B*(N-1)-(A*(N-1)+B)+1=B*(N-2)-A*(N-2)+1=(B-A)*(N-2)+1
ただしA>Bなら0
*/

Measured memory_bytes: 256
# ABC136C - Build Stairs
def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))
    cur = A[0]
    for i in A[1:]:
        if i == cur or i - 1 == cur:
            continue
        elif i > cur:
            cur = i - 1
        else:
            print("No")
            break
    else:
        print("Yes")


if __name__ == "__main__":
    main()

Measured memory_bytes: 63984
#include <bits/stdc++.h>
using namespace std;
 
#define file freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define PI 3.141592653589793238462643383279502884
#define MAX 1000100
#define mod 1000000007
#define EPS 1E-9
#define int long long
#define endl "\n"
 
int fix(int cur, int m)
{
    return ((cur%m) + m) % m;
}
 
int fast_power(int x, int y,int m)
{
    if (y == 0)
        return 1;
    int temp = fast_power(x, y / 2, m) ;
    temp = (temp*temp)% m;
    if (y % 2 != 0)
        temp = (temp*x)% m;
    return temp;
 
}
 
 
int32_t main()
{
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.tie(0);
 
    int t=1;
    //cin >> t;
    while(t--)
    {
        string str;
        cin >> str;
        string ans="";
        for(int i=0;i<str.size();i++)
        {
            if(str[i]=='B')
            {
                if(ans.size())
                    ans.pop_back();
            }
            else
                ans+=str[i];
        }
        cout << ans << endl;
 
    }
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};


const int MAX = 510000;
const int mod = 1000000007;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
ll COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//a^n (mod m)
ll modpow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

//mod mでの a の逆元を求める
ll modinv(ll a) {
    ll b = mod, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= mod; 
    if (u < 0) u += mod;
    return u;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    ll n;
    cin>>n;
    ll ans = modpow(10,n);
    ll d = modpow(9,n);
    ans += mod - d;
    ans %= mod;
    ans += mod - d;
    ans %= mod;
    d = modpow(8,n);
    ans += d;
    ans %= mod;
    cout << ans << endl;

}

Measured memory_bytes: 3668
#include <bits/stdc++.h>
using namespace std;
 
#define FASTIO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vl(p) vector<p>
#define W while
#define For(i,s,x) for(i=s;i<x;i++)
#define Fore(i,s,x) for(i=s;i<=x;i++)
#define FOR(i,x) for(i=0;i<x;i++)
#define Rof(i,s,x) for(i=s;i>=x;i--)
#define all(v) v.begin(),v.end()
#define it(r,v) for(auto r=v.begin();r!=v.end();r++)
#define pb push_back
#define in insert
#define sz size()
#define F first
#define S second
#define nl cout<<"\n"
#define pr(a) cout<<a<<" "
#define pr2(a,b) cout<<a<<" "<<b<<"\n"
#define pr3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n"
#define mod 1000000007

  int main()
  {
     ll n,i;
     cin>>n;
     ll a,b[1000000]={0};
     for(i=0;i<n-1;i++)
     { cin>>a;
      // a=a-1;
       b[a-1]++;
     }
     for(i=0;i<n;i++)
     cout<<b[i]<<"\n";
  }

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

int main() {
    int w, a, b;
    cin >> w >> a >> b;

    int ans;
    if (a + w < b) {
        ans = b - (a + w);
    }
    else if (b + w < a) {
        ans = a - (b + w);
    }
    else {
        ans = 0;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <math.h>   /** for sqrt() **/

int prime(int num);

int main() {
    int n, num, cou = 0;
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        scanf("%d", &num);
        cou += prime(num);
    }
    printf("%d\n", cou);
}

int prime(int num) {
    if (num == 2) {return 1;}
    
    else if (num < 2 || num % 2 == 0) {return 0;}
    
    int i = 3;
    while (i <= sqrt(num)) {
        if (num % i == 0) {return 0;}
        i += 2;
    }
    return 1;
}


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

typedef long long LL;

struct Frac {
    LL a, b;
    Frac() : a(0), b(1) {}
    Frac(LL _a, LL _b) : a(_a), b(_b) {
        fix();
    }
    void fix() {
        LL g = __gcd(abs(a), abs(b));
        a /= g; b /= g;
        if (b < 0) a = -a, b = -b;
        assert(b > 0);
    }
    bool operator == (const Frac &rhs) const {
        return a == rhs.a && b == rhs.b;
    }
    bool operator < (const Frac &rhs) const {
        return a * rhs.b < b * rhs.a;
    }
    bool operator >= (const Frac &rhs) const {
        return a * rhs.b >= b * rhs.a;
    }
    Frac operator + (const Frac &rhs) const {
        return Frac(a * rhs.b + b * rhs.a, b * rhs.b);
    }
    Frac operator + (const int x) const {
        return *this + Frac(x, 1);
    }
    Frac operator - (const Frac &rhs) const {
        return Frac(a * rhs.b - b * rhs.a, b * rhs.b);
    }
    Frac operator - (const int x) const {
        return *this - Frac(x, 1);
    }
    Frac operator * (const Frac &rhs) const {
        return Frac(a * rhs.a, b * rhs.b);
    }
    Frac operator * (const int x) const {
        return *this * Frac(x, 1);
    }
    Frac operator / (const Frac &rhs) const {
        return Frac(a * rhs.b, b * rhs.a);
    }
    Frac operator / (const int x) const {
        return *this / Frac(x, 1);
    }
    Frac operator % (const int M) const {
        LL x = a / b;
        return *this - x + (x % M);
    }
};

int main() {
    int H, h, m, s;
    while (scanf("%d%d%d%d", &H, &h, &m, &s) == 4 && H) {
        LL S = h * 3600 + m * 60 + s;
        Frac vHour(1, H * 10), vMin(1, 10), vSec(6, 1), vMid = (vHour + vMin) / 2;
        Frac pMid = (vMid * S) % 360;
        Frac pSec = (vSec * S) % 360;
        Frac dis = min((pMid + 360 - pSec) % 360, (pMid + 540 - pSec) % 360);
        Frac need = (dis / (vSec - vMid) + S) % (H * 3600);
        if (vSec * need % 360 == vMin * need % 360 && vMin * need % 360 == vHour * need % 360) {
            need = need + Frac(180, 1) / (vSec - vMid);
        }
        need = need % (H * 3600);
        long long hour = 0, minu = 0;
        while (need >= Frac(3600 ,1)) ++ hour, need = need - 3600;
        while (need >= Frac(60, 1)) ++ minu, need = need - 60;
        printf("%lld %lld %lld %lld\n", hour, minu, need.a, need.b);
    }
    return 0;
}

Measured memory_bytes: 1176
#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

int a, b, c, tot;

int main()
{
    scanf("%d%d%d", &a, &b, &c);
    int mx = max(max(a, b), c);
    tot = 3*mx-a-b-c;
    if (tot%2)
        tot += 3;
    printf("%d\n", tot/2);
    return 0;
}


Measured memory_bytes: 256
a, b, c = map(int, input().split())
five = 0
seven = 0
check = [a, b, c]
for s in check:
    if s == 5:
        five += 1
    if s == 7:
        seven += 1
if five == 2 and seven == 1:
    print('YES')
else:
    print('NO')

Measured memory_bytes: 2940
import java.util.Scanner

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val q = scanner.nextInt()

    val p = Array(100001, { i -> 0 } )
    p[0] = 0
    for (i in 1..100000) {
        if (isLike2017(i)) {
            p[i] = p[i - 1]+ 1
        } else {
            p[i] = p[i - 1]
        }
    }

    for (i in 1..q) {
        var l = scanner.nextInt()
        var r = scanner.nextInt()
        println(p[r] - p[l - 1])
    }
}

fun isPrime(n: Int): Boolean {
    if (n <= 1) return false
    if (n <= 3) return true
    for (i in 2..(n - 1)) {
        if (n % i == 0) return false
        if (i * i > n) return true
    }
    return true
}

fun isLike2017(n: Int): Boolean {
    return n % 2 == 1 && isPrime(n) && isPrime((n + 1) / 2)
}

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

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

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> p(n);
    rep(i, n) cin >> p[i];
    rep(i, n) p[i]++;
    int mx = 0;
    int s = 0;
    queue<int> q;
    rep(i, n)
    {
        s += p[i];
        q.push(p[i]);
        if (q.size() > k)
        {
            s -= q.front();
            q.pop();
        }
        if (q.size() == k)
        {
            mx = max(mx, s);
        }
  }
  double ans = mx;
  ans /= 2;
  printf("%.10f\n", ans);
  return 0;
}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define eps 1e-9
#define maxp 1000400
#define mod 1000000007
#define pi 3.141592653589793238
#define sin(s) scanf("%lld",&s);
#define sout(s) printf("%lld\n",s);
#define in(s) cin>>s
#define in2(a,b) cin>>a>>b
#define in3(a,b,c) cin>>a>>b>>c
#define vll vector<ll>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl '\n'
#define sp ' '
#define gear_change ios_base::sync_with_stdio(false),cin.tie(0)
#define nfl(i,n) for(i=0;i<n;i++)
#define sfl(i,s,e) for(i=s;i<e;i++)
#define ufl(i,n) for(i=n-1;i>=0;i--)
ll power(ll x,ll y){
    ll ret=1;
    while(y>0){
        if(y&1)
            ret=(ret*x);
        y/=2;
        x=(x*x);
    }
    return ret;
}
ll get(ll n){
	ll ret=0;
	while(n%2==0){
		ret++;
		n/=2;
	}
	return ret;
}
void solve(){
	ll n,i;
	cin>>n;
	vll vec(n);
	nfl(i,n){
		cin>>vec[i];
	}
	ll ans=0;
	nfl(i,n){
		ans+=get(vec[i]);
	}
	cout<<ans<<endl;
}	
int main(){
	gear_change;
	ll test=1;
	// in(test);
	while(test--){
		solve();
	}
}	


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

using namespace std;
using ll=long long;
using vi=vector<int>;
using vl=vector<ll>;
using vs=vector<string>;

#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, n) for (int i = (a); i < (n); ++i)
#define repr(i, a, n) for (int i = (n); i >= (a); --i)

template<typename T,typename U>
void Inparray(T &f,U n){
  for(int i=0;i<n;i++) cin>>f[i];
}

template<typename T,typename U>
void Printarray(T f,U n){
  for(int i=0;i<n;i++) i!=n ? cout<<f[i]<<" " : cout<<f[i];
}

void flagfunc(bool flag,string A,string B){
  flag ? cout<<A<<endl : cout<<B<<endl;
}

void flag2func(bool flag){
  flag ? cout<<"Yes"<<endl : cout<<"No"<<endl;
}

void flag3func(bool flag){
  flag ? cout<<"YES"<<endl : cout<<"NO"<<endl;
}

int main(){
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int n,m;cin>>n>>m;
  int a[n],b[n],c[m],d[m];
  rep(i,n)cin>>a[i]>>b[i];
  rep(i,m)cin>>c[i]>>d[i];
  int res,ans;
  rep(i,n){
    ans=1;
    res=2*1e9;
    rep(j,m){
      if(res>abs(c[j]-a[i])+abs(d[j]-b[i])){
        res=abs(c[j]-a[i])+abs(d[j]-b[i]);
        ans=j+1;
      }
    }
    cout<<ans<<endl;
  }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

class combination{
private:
    vector<mint> fact,inv,finv;
public:
    combination(int N){
        fact = inv = finv = vector<mint>(N+1);
        fact[0] = fact[1] = 1;
        inv[0] = inv[1] = 1;
        finv[0] = finv[1] = 1;
        for(ll i=2;i<=N;i++){
            fact[i] = fact[i-1]*i;
            inv[i] = (mint) mod - inv[mod%i]*(mod/i);
            finv[i] = finv[i-1]*inv[i];
        }
    }
    mint f(int i){
        return fact[i];
    }
    mint comb(int n,int k){
        if(n<k) return 0;
        if(n<0 || k<0) return 0;
        return fact[n]*finv[k]*finv[n-k];
    }
    mint hcomb(int n,int k){
        if(n==0 && k==0) return 1;
        return comb(n+k-1,k);
    }
    mint perm(int n,int k){
        return comb(n,k)*fact[k];
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M;
    cin >> N >> M;
    mint ans = 0;
    combination C(max(N,M));
    for(int i=0;i<=N;i++){
        mint now = C.comb(N,i);
        now *= C.perm(M,i);
        now *= C.perm(M-i,N-i)*C.perm(M-i,N-i);
        if(i%2) ans -= now;
        else ans += now;
    }
    cout << ans << "\n";
}

Measured memory_bytes: 15072
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())

def win_hand(machine):
  if machine == "s":
    return "r"
  elif machine == "p":
    return "s"
  elif machine == "r":
    return "p"

def judge(machine):
  if machine == "s":
    return R
  elif machine == "p":
    return S
  elif machine == "r":
    return P
  else:
    return 0

t_sorted = [[] for i in range(K)]
for i in range(N):
  t_sorted[i%K].append(T[i])

ans = 0
for i in range(K):
  player = []
  for j in range(len(t_sorted[i])):
    if j == 0:
      player.append(win_hand(t_sorted[i][j]))
      ans += judge(t_sorted[i][j])
    else:
      if win_hand(t_sorted[i][j]) == player[j-1]:
        if j != len(t_sorted[i])-1:
          if "r" != win_hand(t_sorted[i][j]) and "r" != win_hand(t_sorted[i][j+1]):
            player.append("r")
          elif "s" != win_hand(t_sorted[i][j]) and "s" != win_hand(t_sorted[i][j+1]):
            player.append("s")
          elif "p" != win_hand(t_sorted[i][j]) and "p" != win_hand(t_sorted[i][j+1]):
            player.append("p")
        else:
          if "r" != win_hand(t_sorted[i][j]):
            player.append("r")
          elif "s" != win_hand(t_sorted[i][j]):
            player.append("s")
          elif "p" != win_hand(t_sorted[i][j]):
            player.append("p")
      else:
          player.append(win_hand(t_sorted[i][j]))
          ans += judge(t_sorted[i][j])

print(ans)

Measured memory_bytes: 59020
N,M = input().split()

ans_1 = ''
ans_2 = ''
for n in range(int(N)):
  ans_1 += M

for m in range(int(M)):
  ans_2 += N

if ans_1 > ans_2:
  ans = ans_2
else:
  ans = ans_1

print(ans)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define rep(i,f,n) for(ll i=(f); (i) < (n); i++)
#define repe(i,f,n) for(ll i=(f); (i) <= (n); i++)
using namespace std;
using ll = long long;
#define MOD (ll)1000000007
#define PI 3.14159265359
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define P pair<ll, ll>
#define dvec vector<vector<ll>>
//printf("%.10f\n")
//cout << fixed << setprecision(10);
template<class T> inline bool chmax(T& a, T b){if (a < b) { a = b; return true; } return false;}
template<class T> inline bool chmin(T& a, T b){if (a > b) { a = b; return true; } return false;}
ll INF = 1e+18;
int iINF = 1e9;

int main()
{
  int N, K; cin >> N >> K;
  ll ans = 0;

  repe(i, K, N + 1){
    ll mini = i * (i - 1) / 2;
    ll maxi = i * (N + (N - i + 1)) / 2;
    ans +=  (maxi - mini + 1) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;
}



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

int main() {
    int N;
    cin>>N;
    vector<int>v(1<<N);
    for(auto &i:v)cin>>i;
    int ans=0;
    for(int i=0;i<1<<N;i++){
        bool flag=true;
        if(!i)flag=false;
        for(int j=0;j<N;j++){
            if((i>>j)&1){
                if(!v[i])flag=false;
                if(v[i]&&v[i^(1<<j)])flag=false;
                if(!v[i]&&v[i^(1<<j)]){
                    cout<<-1<<endl;
                    return 0;
                }
            }
        }
        if(flag)ans++;
       // cout<<i<<" "<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 7032
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here! 
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = sc.nextInt();
        }
        Arrays.sort(array);
        
        long sum = 0;
        int count = 0;
        for (int i = n - 1; i >= 0; i--) {
            sum += array[i];
            count++;
            if (count == n) break;
            sum += array[i];
            count++;
            if (count == n) break;
        }
        System.out.println(sum - array[n-1]);
    }
}

Measured memory_bytes: 60420
#include<bits/stdc++.h>
//#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template<class T> inline void checkmin(T &a,const T &b){if(b<a) a=b;}
template<class T> inline void checkmax(T &a,const T &b){if(b>a) a=b;}

const int inf=1e9+10;
const int maxn=510;

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

int dp[maxn][maxn],n,a[maxn][maxn];
vector <int> v;
queue <pair<int,int> > que;

bool check(int x,int y){
	if(x<0||y<0||x>=n||y>=n) return false;
	return true;
}

int main(){
	scanf("%d",&n);
	for(int i=0;i<n*n;i++){
		int x;
		scanf("%d",&x);
		x--;
		v.push_back(x);
	}
	for(int i=0;i<n;i++) for(int j=0;j<n;j++) dp[i][j]=inf,a[i][j]=1;
	for(int i=0;i<n;i++){
		dp[0][i]=dp[i][0]=dp[i][n-1]=dp[n-1][i]=0;
		que.push(mp(0,i));   que.push(mp(i,0)); 
		que.push(mp(n-1,i)); que.push(mp(i,n-1)); 
	}
	while(que.size()){
		int x=que.front().fst,y=que.front().snd;
		que.pop();
		for(int d=0;d<4;d++){
			int nx=x+dx[d],ny=y+dy[d];
			if(!check(nx,ny)) continue;
			if(dp[nx][ny]>dp[x][y]+1){
				dp[nx][ny]=dp[x][y]+1;
				que.push(mp(nx,ny));
			}
		}
	}
	ll ans=0;
	for(int i=0;i<v.size();i++){
		int x=v[i]/n,y=v[i]%n;
		ans+=dp[x][y];
		dp[x][y]--;
		a[x][y]=0;
		que.push(mp(x,y));
		while(que.size()){
			int x=que.front().fst,y=que.front().snd;
			que.pop();
			for(int d=0;d<4;d++){
				int nx=x+dx[d],ny=y+dy[d];
				if(!check(nx,ny)) continue;
				if(dp[nx][ny]>dp[x][y]+a[nx][ny]){
					dp[nx][ny]=dp[x][y]+a[nx][ny];
					que.push(mp(nx,ny));
				}
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}


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

int main() {
  int a, b, sum = 0;
  cin >> a >> b;
  for(int i = a; i <= b; i++){
  	int w = i / 10000;
    int x = i / 1000 % 10;
    int y = i / 10 % 10;
    int z = i % 10;
    if(w == z && x == y){
      	sum++;
    }
  }
  cout << sum << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> b;
  while(n!=0){
    if(abs(n)%2==0)
      b.push_back(0);
    else{
      b.push_back(1);
      n--;
    }
    n/=-2;
  }
  if(b.empty())
    cout<<0;
  for(int i=b.size()-1;i>=0;i--)
    cout<<b.at(i);
  cout<<endl;
}

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

void Solve(){
	int n, m;
	cin>>n>>m;
	vector<int> g[n + 1];
	while(m--){
		int u, v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<bool> vst(n + 1);
	queue<int> q;
	vector<int> res(n + 1);
	q.push(1);
	vst[1] = true;
	while(!q.empty()){
		int current = q.front();
		q.pop();
		for(auto x: g[current]){
			if(!vst[x]){
				vst[x] = true;
				q.push(x);
				res[x] = current;
			}
		}
	}
	cout<<"Yes\n";
	for(int i = 2; i <= n; i++) cout<<res[i]<<'\n';
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int T = 1;
	//cin>>T;
	while(T--){
		Solve();
	}
}

Measured memory_bytes: 10308
l, r = map(int, input().split())
mod = 2019
mod_min = 2019


for i in range(l,r):
    for j in range(i+1,r+1):
        m = (i * j) % mod
        if m < mod_min:
            mod_min = m
        if mod_min == 0:
            break
    if mod_min == 0:
        break

print(mod_min)


Measured memory_bytes: 2940
N = int(input())

A = sorted(list(map(int, input().split())))+[10**10]
B = sorted(list(map(int, input().split())))+[10**10]
C = sorted(list(map(int, input().split())))

cnt = [0]*(N+1)

ai = 0
for bi in range(N):
    while A[ai] < B[bi]:
        ai += 1
    cnt[bi] = ai

for i in range(1, N):
    cnt[i] += cnt[i-1]

ans = 0

bi = 0
for ci in range(N):
    while B[bi] < C[ci]:
        bi += 1
    ans += cnt[bi-1]

print(ans)

Measured memory_bytes: 29124
#define _USE_MATH_DEFINES

//#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <vector>
    
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
    
int main(){
    ll a,b,n;
    cin>>a>>b>>n;

/*
    int x,x2;
    if(b>n){
        x=n;
    }else{
        x=b-1;
    }
    */
    //printf("%d, %d", x, int(a*x/b) - a*int(x/b));
    if(b>n){
        printf("%d", int(a*n/b) - a*int(n/b));
    }else{
        printf("%d", max(int(a*n/b) - a*int(n/b), int(a*(b-1)/b) - a*int((b-1)/b)));
    }

} // end of main
    
//実行は、Opt + Ctl + N


//pythonの入力
//s, w = map(int, input().split())


Measured memory_bytes: 3764
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <tuple>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;

bool isValidMonth(const string &s) {

    vector<string> vs = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

    for (const auto &v:vs) {

        if (s == v) {
            return true;
        }

    }
    return false;
}

int main() {


    string s;
    cin >> s;

    string mm1 = s.substr(2);

    bool isValidMM1 = isValidMonth(mm1);

    string mm2 = s.substr(0, 2);

    bool isValidMM2 = isValidMonth(mm2);

    if (isValidMM1 and isValidMM2) {
        cout << "AMBIGUOUS" << "\n";
    } else if (isValidMM1) {
        cout << "YYMM" << "\n";
    } else if (isValidMM2) {
        cout << "MMYY" << "\n";
    } else {
        cout << "NA" << "\n";
    }

    return 0;

}

Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <bitset> 
#include <cstdio> 
#include <vector>
using namespace std;
int n,m;
const int maxn=100000+10;
double a[maxn];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	double middle=a[n]/2.0;
	double ans;
	double now=10000000000;
	for(int i=1;i<=n;i++){
		if(abs(a[i]-middle)<now){
			ans=a[i];
			now=abs(a[i]-middle);
		}
	}
	long long x=a[n],y=ans;
	cout<<x<<' '<<y;
	return 0;
}

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

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

ll dp1[2020];
ll dp2[2020];

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

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N,Z,W; cin>>N>>Z>>W;
    vll a(N);
    rep(i,N) cin>>a[i];
    if (N==1){
        cout<<abs(W-a[0])<<"\n";
        return 0;
    }
    dp1[N-1] = dp2[N-1] = abs(a[N-1]-a[N-2]);
    RFOR(i,N-2,0){
        ll Y = (i>0)?a[i-1]:W;
        dp1[i] = abs(Y-a[N-1]);
        FOR(j,i+1,N-1) chmax(dp1[i],dp2[j]);

        ll X = (i>0)?a[i-1]:Z;
        dp2[i] = abs(X-a[N-1]);
        FOR(j,i+1,N-1) chmin(dp2[i],dp1[j]);
    }
    cout<<dp1[0]<<"\n";
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int64_t MOD=1000000007;
const long long INF = 1LL<<60;

int main() {
  int H,W,M; cin>>H>>W>>M;
  vector<int> LH(H,0),LW(W,0);
  set<pair<int,int>> B;
  rep(i,M){
    int a,b; cin>>a>>b;
    a--;
    b--;
    B.insert(make_pair(a,b));
    LH.at(a)++;
    LW.at(b)++;
  }
  int HM=0,WM=0;
  vector<int> HH,WW;
  rep(i,H){
    if(HM<LH.at(i)){
      HH={};
      HH.push_back(i);
      HM=LH.at(i);
    }
    else if(HM==LH.at(i)){
      HH.push_back(i);
    }
  }
  rep(i,W){
    if(WM<LW.at(i)){
      WW={};
      WW.push_back(i);
      WM=LW.at(i);
    }
    else if(WM==LW.at(i)){
      WW.push_back(i);
    }
  }
  int s=HH.size(),t=WW.size(),ans=WM+HM;
  bool c=false,finish=false;
  rep(i,s){
    rep(j,t){
      pair<int,int> k=make_pair(HH.at(i),WW.at(j));
      if(B.count(k)==false){
        c=true;
        finish=true;
        break;
      }
    }
    if(finish) break;
  }
  
  if(c) cout<<ans;
  else cout<<ans-1; 
}


Measured memory_bytes: 23824