akhauriyash/Code-Regression huggingface dataset:
from bisect import bisect_left
import sys
sys.setrecursionlimit(10 ** 7)

N = int(input())
A = list(map(int, input().split()))

edges = [[] for _ in range(N)]
for _ in range(N - 1):
    fr, to = map(lambda a: int(a) - 1, input().split())
    edges[fr].append(to)
    edges[to].append(fr)

ans = [0] * N
L = [-10**18]

def search(now, pr):
    a = A[now]
    i = bisect_left(L, a)
    isAppended = False
    prevValue = -1

    if i == len(L):
        L.append(a)
        isAppended = True
    else:
        prevValue = L[i]
        if L[i] > a:
            L[i] = a

    ans[now] = len(L) - 1

    for to in edges[now]:
        if to == pr:
            continue
        search(to, now)

    if isAppended:
        L.pop()
    else:
        L[i] = prevValue

search(0, -1)
print(*ans, sep='\n')


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




int dp[1<<19];
int dpmb[2][1<<19];



int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
	int n, k; cin >> n >> k;
  string s, t; cin >> s >> t;
  for (int i = 0; i < n; ++i) s[i] = s[i] == 'W' ? 1 : 0;
  for (int i = 0; i < n; ++i) t[i] = t[i] == 'W' ? 1 : 0;
  deque<int> dq[2];
  dpmb[1][0] = dpmb[0][0] = 0;
  for (int i : {0, 1}) dq[i].push_back(0);
  for (int i = 0, b[2] = {}, bc[2] = {}; i < n; ++i) {
    for (int j : {0, 1}) if (dq[j].size() and dq[j].front() == i - k) {
      dq[j].pop_front();
    }
    dp[i + 1] = (s[i] != t[i]) + dp[i];
    if (i == 0 or t[i] != t[i - 1]) { ++b[t[i]]; }
    dp[i + 1] = min({dp[i + 1], b[0] + dpmb[0][dq[0].front()] + 1, 
                                b[1] + dpmb[1][dq[1].front()] + 1});
    if (i == n - 1 or t[i + 1] != t[i]) { ++bc[t[i]]; }
    for (int j : {0, 1}) {
      dpmb[j][i + 1] = dp[i + 1] - bc[j];
      while (dq[j].size() and dpmb[j][dq[j].back()] >= dpmb[j][i + 1]) {
        dq[j].pop_back();
      }
      dq[j].push_back(i + 1);
    }
  }
  cout << dp[n] << '\n';
}



Measured memory_bytes: 10296
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
int main()
{
    int n;
    string s, t;
    cin >> n >> s >> t;
    for(int i = 0; i < n; i++)
    {
        cout << s[i] << t[i];
    }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
char s[200005];
int main()
{
	scanf("%s",&s);
	int t=strlen(s);
	long long sum=0;
	for(int i=0;i<t;i++)
	{
		sum=sum+(s[i]-'0');
	}
	if(sum%9==0) printf("Yes");
	else printf("No");
	return 0;	
} 

Measured memory_bytes: 4012
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
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 (b<a) {a=b; return 1;} return 0;}

const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
    long long x;
    mint(long long x=0) : x((x % mod + mod) % mod) {}
    mint operator-() const { return mint(-x);}
    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 { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    ll n, k;
    cin >> n >> k;
    mint ans = 0;
    for (ll i = k; i <= n+1; ++i) {
        ans += i * (n - i + 1) + 1;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3616
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define PI acos(-1)
typedef  long long ll;
ll gcd(ll x,ll y){
   if(y==0) return x;
   else return gcd(y,x%y);
}
ll lcm(ll x,ll y){
   return x/gcd(x,y)*y;
}
using namespace std;

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

   if(s=="ABC") cout<<"ARC"<<endl;
   else cout<<"ABC";
return 0;
}


Measured memory_bytes: 3624
def examA():
    S = SI()
    if S=="SUN":
        ans = 7
    elif S=="MON":
        ans = 6
    elif S=="TUE":
        ans = 5
    elif S=="WED":
        ans = 4
    elif S=="THU":
        ans = 3
    elif S=="FRI":
        ans = 2
    else:
        ans = 1
    print(ans)

    return


def examB():
    return

def examC():
    return

def examD():
    return

def examE():
    return

def examF():
    return

import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examA()


Measured memory_bytes: 38384
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<deque>
#include<string>
using namespace std;

int main()
{
	#ifdef local
	freopen("1.txt","r",stdin);
	#endif
	long long n,m;
	cin>>n>>m;
	if(n==m||n==0&&m==0||m==1&&n==2||m==2&&n==1)printf("Brown");
	else
	{
		if(n<m)swap(n,m);
		if(n-m>=2)printf("Alice");
		else printf("Brown");
	}
}

Measured memory_bytes: 256
import operator
from collections import defaultdict
from functools import reduce

n = int(input())
arr = [input() for _ in range(n)]
l = list(reduce(operator.and_, [set(i) for i in arr]))
d = defaultdict(lambda: 1<<60)
l.sort()

for s in arr:
    for c in l:
        cnt = s.count(c)
        d[c] = min(d[c], cnt)

res = []
for k, v in d.items():
    res.append(k*v)
ans = "".join(res)
print(ans)

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

using namespace std;
const int maxn = 18;

int n;
int niz[maxn][maxn];
long long dp[(1 << maxn)];
long long cost[(1 << maxn)];

long long f(int mask) {
	if (mask == 0) return 0;
	long long &ret = dp[mask];
	if (ret != -1) return ret;
	
	vector<int> koji;
	for (int i = 0; i < n; i++)
		if (mask & (1 << i)) koji.push_back(i);
	
	int siz = koji.size();
	ret = 0;
	for (int i = 0; i < (1 << siz); i++) {
		int ac = 0;
		for (int j = 0; j < siz; j++)
			if (i & (1 << j)) ac |= (1 << koji[j]);
		ret = max(ret, cost[ac] + f(mask & (~ac)));
	}
	return ret;
}

int main() {
	memset(dp, -1, sizeof dp);
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			scanf("%d", &niz[i][j]);
	
	for (int i = 0; i < (1 << n); i++) {
		for (int x = 0; x < n; x++) {
			for (int y = x + 1; y < n; y++) {
				if ((i & (1 << x)) && (i & (1 << y))) cost[i] += niz[x][y];
			}
		}
	}
	
	printf("%lld", f((1 << n) - 1));
	return 0;
}


Measured memory_bytes: 2816
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
n,m=map(int,input().split())
uf1=UnionFind(n)
for i in range(m):
    a,b=map(int, input().split())
    uf1.union(a-1,b-1)

for i in range(n):uf1.find(i)
print(len(set(uf1.par))-1) 

Measured memory_bytes: 98976
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <iostream>
#include <utility>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <limits.h>
#include <cstring>
#include <tuple>
#include <cassert>
#include <numeric>
using namespace std;
// type alias
typedef long long LL;
typedef vector < int > VI;
typedef unordered_map < int, int > MAPII;
typedef unordered_set < int > SETI;
typedef pair< int , int > II;
typedef tuple< int, int, int > III;
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n)  for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// minmax
#define SMAX(a,b) a = max(a,b)
#define SMIN(a,b) a = min(a,b)
// debug cerr
#define TRACE true
#define dump(x) if(TRACE) { cerr << #x << " = " << (x) << endl; }
#define dump2(x,y) if(TRACE) { cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl; }
#define dump3(x,y,z) if(TRACE) { cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z) << endl; }
#define dump4(x,y,z,a) if(TRACE) { cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z) << ", " << #a << " = " << (a) << endl; }
#define dumpAR(ar) if(TRACE) { FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }

/*
 
 8/5/2018
 
 24:30-24:56 analysis
 
 */

// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG x.cpp && ./a.out
const int MAX_N=5e3+1;
int N,C;
LL D[30][30];
int A[MAX_N][MAX_N];
int X[3][30];
void solve() {
  ZERO(X);
  REP(i,N)REP(j,N) X[(i+j)%3][A[i][j]]++;
  LL res=1e9;
  REP(i,C)REP(j,C)if(i!=j)REP(k,C)if(j!=k&&i!=k){
    LL x=0;
    int C2[3]={i,j,k};
    REP(g,3)REP(c,30) x+=1LL*X[g][c]*D[c][C2[g]];
//    dump4(i,j,k,x);
    SMIN(res,x);
  }
  cout<<res<<endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  
  cin>>N>>C;
  REP(i,C)REP(j,C) cin>>D[i][j];
  REP(i,N)REP(j,N) cin>>A[i][j],A[i][j]--;
  solve();
  return 0;
}


Measured memory_bytes: 9088
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

void solve()
{
	string T, P;
	cin >> T >> P;
	int size = T.size() - P.size() + 1;
	for (int i = 0; i < size; ++i)
	{
		i = T.find(P, i);
		if (i != string::npos)
		{
			printf("%d\n", i);
		}
		else
		{
			return;
		}
	}
}

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

Measured memory_bytes: 2048
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <numeric>
#include <math.h>
using namespace std;

#define N_MAX   (100000)
#define LL_MAX_NUM (1LL<<60)

typedef long long ll;
typedef long long int lli;

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

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
#include <chrono>
#include <cassert>
#include <tuple>
#include <utility>
using namespace std;

int dist[100001];
int fix[100001];
vector<int> G[100001];
vector<int> dq[100001];
vector<int> val[100001];

int main(){
  int n;
  //cin >> n;
  scanf("%d", &n);

  //vector<vector<int>> G(n);
  for(int i=0; i<n-1; ++i){
    int a,b;
    //cin >> a,b;
    scanf("%d%d", &a,&b);
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  int k;
  //cin >> k;
  scanf("%d", &k);

  fill(dist, dist+n, 1e8);
  //vector<int> dist(n, 1e8);
  //vector<bool> fix(n, false);

  int mn = 1e6, mx=0;
  while(k--){
    int v,p;
    //cin >> v,p;
    scanf("%d%d", &v, &p);
    v--;

    dist[v] = p;
    fix[v] = true;

    mn = min(mn, p);
    mx = max(mx, p);
  }

  if(mx-mn >= n){
    puts("No");
    return 0;
  }

  for(int i=0; i<n; i++){
    if( dist[i] != 1e8 ){
      dist[i] -= mn;
      val[ dist[i] ].push_back( i );
    }
  }

  for(int i=0; i<n; ++i){
    while( val[i].size() ){
      int p = val[i].back(); val[i].pop_back();

      for(int j=0; j<G[p].size(); ++j){
        int nx = G[p][j];
        if( !fix[nx] &&  dist[nx] > dist[p] + 1 ){
          dist[nx] = dist[p] + 1;
          val[i+1].push_back( nx );
        }
      }
    }
  }

  for(int i=0; i<n; ++i ){
    for(auto j=0; j<G[i].size(); ++j){
      if( abs(dist[i]-dist[ G[i][j] ]) != 1 ){
        puts("No");
        return 0;
      }
    }
  }

  puts("Yes");
  for(int i=0; i<n; ++i){
    printf("%d\n", dist[i]+mn);
  }


  return 0;
}


Measured memory_bytes: 14208
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 200000

bool a[N][20];
bool b[20];
int n, k, x;
long long ans, l, r;

void del(int k){
	f(i, 20){
		if (a[k][i])b[i] = false;
	}
	ans += (r - l);
	return;
}

bool add(int k){
	bool v = true;
	f(i, 20){
		if (a[k][i] && b[i])v = false;
	}
	if (!v)return false;
	f(i, 20){
		if (a[k][i])b[i] = true;
	}
	return true;
}


int main(){
	ans = 0;
	bool z;
	scanf("%d", &n);
	k = 0;
	for (int i = 0; i < n; i++){
		scanf("%d", &x);
		f(j, 20){
			if (x % 2 == 1)a[i][j] = true;
			else a[i][j] = false;
			x = x / 2;
		}
	}
	l = 0;
	r = 0;
	while (l < n){
		if (r < n){
			z=add(r);
			if (z)r++;
			else{
				del(l);
				l++;
			}
		}
		else{
			del(l);
			l++;
		}
	}
	printf("%lld\n",ans);

	return 0;
}

Measured memory_bytes: 4096
n=int(input())
s=str(input())

r=0
g=0
b=0

for i in range(n):
    if s[i]=='R':
        r+=1
    if s[i]=='G':
        g+=1
    if s[i]=='B':
        b+=1
ans=r*g*b
        
for j in range(n):
    for i in range(j):
        k=2*j-i
        if k<n:
            if (s[i]!=s[j]) and (s[i]!=s[k]) and (s[j]!=s[k]):
                ans-=1
print(ans)

Measured memory_bytes: 9204
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(s: str):

    ans = 0
    A = 0
    B = 0
    process = []
    for c in s:
        if c == "A":
            if B == 0:
                A += 1
            else:
                A = 1
                B = 0
        elif A > 0 and c == "B":
            if B == 0:
                B = 1
            else:
                B = 0
                A = 0
        elif A > 0 and B > 0 and c == "C":
            ans += A
            B = 0
        else:
            A = 0
            B = 0
    print(ans)
    # print(process)

    # counter = 0
    # while "ABC" in s:
    #     s = s.replace("ABC", "BCA", 1)
    #     # print(s)
    #     counter += 1
    # print(counter)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    solve(s)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3508
#include<stdio.h>
#include<cmath>
int main(){
    double a,b,C;
    scanf("%lf %lf %lf", &a, &b, &C);
    double s=a*b*sin(C*M_PI/180)*0.5;
    double c=sqrt(a*a+b*b-2*a*b*cos(C*M_PI/180));
    printf("%.8lf\n",s);
    printf("%.8lf\n",a+b+c);
    printf("%.8lf\n",2*s/a);
    return 0;
}


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

typedef struct{
    int p;
    int y;
    int i;
    int n;
} city_t;

int sort1(const void *a, const void *b){
    if (((city_t*)a)->p!=((city_t*)b)->p){
        return ((city_t*)a)->p-((city_t*)b)->p ;
    } else {
        return ((city_t*)a)->y-((city_t*)b)->y ;
    }
}

int sort2(const void *a, const void *b){
    return ((city_t*)a)->i-((city_t*)b)->i ;
}

int main(void){
    int n,m;
    scanf("%d%d",&n,&m);
    city_t city[m];
    for (int i=0; i<m; i++){
        scanf("%d%d",&city[i].p,&city[i].y);
        city[i].i=i;
    }
    qsort (city,m,sizeof(city_t),sort1);
    city[0].n=1;
    for (int i=1; i<m; i++){
        if (city[i].p==city[i-1].p){
            city[i].n=city[i-1].n+1;
        } else {
            city[i].n=1;
        }
    }
    qsort (city,m,sizeof(city_t),sort2);
    for (int i=0; i<m; i++){
        if (city[i].p==100000){
            printf("100000");
        } else if (city[i].p>=10000){
            printf("0%d",city[i].p);
        } else if (city[i].p>=1000){
            printf("00%d",city[i].p);
        } else if (city[i].p>=100){
            printf("000%d",city[i].p);
        } else if (city[i].p>=10){
            printf("0000%d",city[i].p);
        } else {
            printf("00000%d",city[i].p);
        }
        if (city[i].n==100000){
            printf("100000\n");
        } else if (city[i].n>=10000){
            printf("0%d\n",city[i].n);
        } else if (city[i].n>=1000){
            printf("00%d\n",city[i].n);
        } else if (city[i].n>=100){
            printf("000%d\n",city[i].n);
        } else if (city[i].n>=10){
            printf("0000%d\n",city[i].n);
        } else {
            printf("00000%d\n",city[i].n);
        }
    }
	return 0 ;
}


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

int main() {
  vector<int> menu(5);
  int min;
  int index = 0;
  for (int i = 0; i < 5; i++) {
    cin >> menu[i];
    if (i == 0) {
      min = menu[0];
      index = 0;
    } else if (menu[i] % 10 < min % 10 && menu[i] % 10 != 0) {
      min = menu[i];
      index = i;
    }
  }
  
  int ans = min;
  
  for (int i = 0; i < 5; i++) {
    if (i == index) continue;
    for (int j = 1; j < 15; j++) {
      if (menu[i] <= 10 * j) {
        ans += 10 * j;
        break;
      }
    }
  }
  
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for(int i=0; i<n; i++)
#define reps(i, s, e) for(int i=s; i<e; i++)
#define repr(i, n) for(int i=n-1; i>=0; i--)
#define reprs(i, s, e) for(int i=e-1; i>=s; i--)

int main(){

    int n; cin >> n;
    string s; cin >> s;

    ll esum[n+1], wsum[n+1];
    esum[0] = wsum[0] = 0;

    reps(i, 0, n){
        if(s[i] == 'E'){
            esum[i+1] = esum[i] + 1;
            wsum[i+1] = wsum[i]; 
        }
        else{
            wsum[i+1] = wsum[i] + 1;
            esum[i+1] = esum[i];
        }
        // cout << esum[i+1] << " " << wsum[i+1] << endl;
    }

    ll ans = 1e10;
    reps(i, 1, n+1){
        ans = min(ans, wsum[i-1] + esum[n] - esum[i]);
    }

    cout << ans;

    return 0;
}

Measured memory_bytes: 5380
// AOJ 	0634: Ridge
// 2018.1.5 bal4u@uu
// 解答するプログラムのために考案された問題。高さがすべて異なる？
// Kヶ所以内に限定すれば、もっと実用的な問題となろう。

#include <stdio.h>
#include <string.h>

int map[1002][1002];
int memo[1002][1002];
int hi2r[1000002], hi2c[1000002];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0;
	int c = getchar_unlocked();
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

int main()
{
	int h, w, m, i, r, c, r2, c2, f, max, ans;
	
	h = in(), w = in();
	max = 0;
	for (r = 0; r < h; r++) for (c = 0; c < w; c++) {
		m = in(); if (m > max) max = m;
		map[r][c] = m, hi2r[m] = r, hi2c[m] = c;
	}

	ans = 0;
	for (m = 1; m <= max; m++)
	{
		f = 0, r = hi2r[m], c = hi2c[m];
		for (i = 0; i < 4; i++) {
			r2 = r + mv[i][0], c2 = c + mv[i][1];
			if (r2 < 0 || r2 >= h || c2 < 0 || c2 >= w) continue;
			if (map[r2][c2] < m) {
				if (!f) f = memo[r2][c2];			// (r,c)周りの低い区域が１ヶ所だけか
				else if (f != memo[r2][c2]) f = -1;
			}
		}
		if (f == 0) f = m;	// (r,c)の周りに低い区域がないケース
		memo[r][c] = f;		// (r,c)の周りの低い区域の高さか(r,c)の高さが記録される。
		if (f < 0) ans++;	// 複数の低い区域が周りにあるケースでは、尾根とカウントされる。
	}
	printf("%d\n", ans);
	return 0;
}



Measured memory_bytes: 17388
#include <bits/stdc++.h>
//#include<iostream>
//#include<vector>
//#include<map>
//#include<math.h>
//#include<algorithm>

#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n-1); i > -1; i--)
#define rrep2(i,n,a) for(int i = (int)(n); i >= (int)(a); i--)

#define fi first
#define se second

using namespace std;

using ll = long long;
using v_int = vector<int>;
using v2_int = vector<v_int>;
using v_ll = vector<ll>;
using v2_ll = vector<v_ll>;
using v_string = vector<string>;
using v_bool = vector<bool>;
using v2_bool = vector<v_bool>;
using pii = pair<int, int>;
using mii = map<int, int>;

const double PI = 3.1415926535897932;
const int INF = (int)2e9;
const ll LINF = (ll)2e18;
const ll MOD = 1000000007;
const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

template<typename T, typename U> inline
ostream& operator<<(ostream &o, pair<T,U> &p) {
    return o << "{" << p.first << "," << p.second << "}";
}
template<typename T> inline
istream& operator>>(istream &is, vector<T> &vec) {
    for(auto &v : vec) { is >> v; }
    return is;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<T> &vec) {
    for(auto &v : vec) { os << v << ",";}
    return os;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<vector<T> > &mat) {
    for(auto &row : mat) { os << row << endl; }
    return os;
}

template<typename T> inline
void chmin(T &a, T b) { a = min(a, b); }
template<typename T> inline
void chmax(T &a, T b) { a = max(a, b); }

int main(void)
{
    int n;
    cin >> n;
    
    vector<vector<pii> > a(n);

    rep(i,n) {
        int cnt;
        cin >> cnt;
        rep(j,cnt) {
            int x,y;
            cin >> x >> y;
            a[i].push_back({x-1,y});
        }
    }
    
    int ans = -1;
    rep(i,(1<<n)) {
        v_int honest(n, -1);
        queue<int> q;
        rep(j,n) {
            if((1<<j)&i) {
                honest[j] = 1;
                q.push(j);
            }
        }
        bool ok = true;
        while(q.size()) {
            int p = q.front(); q.pop();
            for(auto xy : a[p]) {
                int x = xy.fi, y = xy.se;
                if(honest[x] == -1) {
                    honest[x] = y;
                    if(y==1) { 
                        q.push(x);
                    }
                } else {
                    if(honest[x] != y) {
                        ok = false;
                    }
                }
            }
        }
        if(ok) {
            int cnt = 0;
            rep(j,n) {
                if(honest[j]==1) { cnt++; }
            }
            chmax(ans, cnt);
        }
    }
    cout << ans << endl;
    return 0;
} /*atcoder*/



Measured memory_bytes: 3616
import heapq

N,M = map(int, input().split())
List1=list(map(int,input().split()))
List=[]

for i in range(N):
  a = List1[i]
  List.append(-a)

heapq.heapify(List)

for i in range(M):
  a = (-heapq.heappop(List)) // 2
  heapq.heappush(List, -a)
  
print(int(-sum(List)))


Measured memory_bytes: 14180
#include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a); i>n; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
int main() {
	int n; cin >> n;
	int a = 0, b = 0;
	string s1, s2;
	rep(i, 0, n) {
		cin >> s1 >> s2;
		if (s1 == s2) {++a; ++b;}
		else if (s1 > s2) a += 3;
		else b += 3;
	}
	cout << a << ' ' << b << endl;
	return 0;
}

Measured memory_bytes: 3224
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <tuple>
#include <set>
#include <assert.h>
#include <sstream>
#include <string>
//#include <bits/stdc++.h>
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i,i0,n) for(int (i)=(i0);(i)<(n);(i)++)
#define FORR(i,i0,n) for(int (i)=(n)-1; (i)>=(i0);(i)--)
#define SORT(x) sort(x.begin(),x.end())
#define SORTR(x) sort(x.begin(),x.end(),greater <>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
using namespace std;
using ll = long long;
typedef std::pair<int,int> P;
#include <boost/multiprecision/cpp_int.hpp>
 
using bint = boost::multiprecision::cpp_int;

int main(){
    int ans = 0;
    string s; 
    cin >> s;
    rep(i,4){
        if(s[i] == '+') ans++;
        else ans--;
    }
    cout << ans;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
struct node{
	LL y,next;
}e[500010];LL len=0,last[500010];
void ins(LL x,LL y)
{
	e[++len].y=y;
	e[len].next=last[x];last[x]=len;
}
LL n,m,fa[500010],f[500010],sum[500010];
LL findfa(LL x) {return fa[x]==x?x:fa[x]=findfa(fa[x]);}
LL a[500010],b[500010],pos[500010],vis[500010];
bool cmp(LL x,LL y) {return a[x]<a[y];}
int main()
{
	scanf("%lld %lld",&n,&m);
	for(LL i=1;i<=n;i++)
	{
		scanf("%lld %lld",&a[i],&b[i]);
		a[i]=max(0LL,a[i]-b[i]);
	}
	for(LL i=1;i<=m;i++)
	{
		LL x,y;scanf("%lld %lld",&x,&y);
		ins(x,y);ins(y,x);
	}
	for(LL i=1;i<=n;i++) fa[i]=i,sum[i]=b[i],pos[i]=i,f[i]=a[i];
	sort(pos+1,pos+n+1,cmp);
	memset(vis,false,sizeof(vis));
	for(LL j=1;j<=n;j++)
	{
		LL x=pos[j];vis[x]=1;
		for(LL i=last[x];i;i=e[i].next)
		{
			LL y=e[i].y;
			if(vis[y])
			{
				LL tx=findfa(x),ty=findfa(y);
				if(tx==ty) continue;
				sum[tx]+=sum[ty];fa[ty]=tx;
				f[tx]=min(f[tx],f[ty]+max(0LL,a[x]-f[ty]-sum[ty]));
			}
		}
	}
	printf("%lld\n",sum[pos[n]]+f[pos[n]]);
}

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

int main() {
  int N;
  cin >> N;
  vector<int> ps(N + 1, 0);
  for(int i = 1; i <= N; ++i) {
    cin >> ps[i];
  }
  int dc = 0;
  for(int i = 1; i <= N; ++i) {
    if(i != ps[i]) {
      dc++;
    }
  }
  if(dc <= 2) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define fr(i,l,r) for(int i=l;i<=r;i++)
signed main()
{  // freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int h,w;
    cin>>h>>w;
    char a[h][w];

    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            cin>>a[i][j];
        }
    }

int dp[h][w];

 for(int i=0;i<h;i++)
 {
    for(int j=0;j<w;j++)
    {
        if(i==0 || j==0)
        {

            if(i==0 && j==0)dp[i][j]=1;
            else if(i==0)
            {dp[i][j]=dp[i][j-1];
                if(a[i][j]=='#')dp[i][j]=0;
            }
            else {
                dp[i][j]=dp[i-1][j];
                if(a[i][j]=='#')dp[i][j]=0;
            }
        }
        else
        {
            dp[i][j]=(dp[i-1][j]+dp[i][j-1])%1000000007;
            if(a[i][j]=='#')dp[i][j]=0;

        }
    }
 }
  
 cout<<dp[h-1][w-1]%1000000007<<endl;
    return 0;
}

Measured memory_bytes: 9088
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <algorithm>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL<<62);

LL N;
string C1, C2;
int main(int argc, char* argv[]){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	cin >> C1;

	for(int i=N-1; i>=0; i--){
		if(C1[i] == 'W'){
			C1.pop_back();
		}
		else{
			break;
		}
	}

	int wfound = 0;
	for(int i=0; i<C1.size(); i++){
		if(C1[i] == 'W'){
			wfound = 1;
		}
		if(wfound == 0 && C1[i] == 'R'){
			//
		}
		else{
			C2.push_back(C1[i]);
		}

	}
	LL red = 0, white = 0;
	for(int i=0; i<C2.size(); i++){
		if(C2[i] == 'R'){
			red++;
		}
		else{
			white++;
		}
	}

	LL redswap = 0;
	int rightwhite = 0;
	for(int i=C2.size()-1; i>=0; i--){
		if(C2[i] == 'R'){
			redswap++;
		}
		rightwhite++;
		if(rightwhite == white){
			break;
		}
	}

	LL whiteswap = 0;
	int leftred = 0;
	for(int i=0; i<C2.size(); i++){
		if(C2[i] == 'W'){
			whiteswap++;
		}
		leftred++;
		if(leftred == red){
			break;
		}
	}

	LL ans = min(red, white);
	ans = min(ans, whiteswap);
	ans = min(ans, redswap);
	printf("%lld\n", ans);
	return 0;
}



Measured memory_bytes: 3852
#[allow(unused_macros)]
macro_rules! scan {
    () => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().to_string()
        }
    };
    (;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().split_whitespace().map(|s| s.to_string()).collect::<Vec<String>>()
        }
    };
    (;;$n:expr) => {
        {
            (0..$n).map(|_| scan!()).collect::<Vec<_>>()
        }
    };
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($t:ty;;$n:expr) => {
        (0..$n).map(|_| scan!($t;;)).collect::<Vec<_>>()
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    scan!($t)
        ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    scan!($($t),*)
        ).collect::<Vec<_>>()
    };
}

fn main() {
    let c = scan!(String);
    for c in c.chars() {
        match c {
            'a' | 'i' | 'u' | 'e' | 'o' => println!("vowel"),
            _ => println!("consonant"),
        }
    }
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll,ll> ii;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define RST(i,n) memset(i,n,sizeof i)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(),a.end()
#define X first
#define Y second
#define mkp make_pair
#define pb push_back
#define eb emplace_back
#define pob pop_back
#ifdef cold66
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // cold66
//}
template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

const ll MAXn=1e5+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=0x3f3f3f3f;

int main(){
  IOS();
  ll k,a,b;
  cin >> k >> a >> b;

  if (k < a-1) return cout << k+1 << endl,0;

  k -= a-1;
  ll ans = a;
  if (b-a > 2) {
    ll tmp = (b-a) * (k/2);
    if (k&1) {
      cout << ans + tmp + 1 << endl;
    }
    else {
      cout << ans + tmp << endl;
    }
  }
  else cout << a+k << endl;
}


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

#define ll long long
#define mod 1000000007
#define inf 9223372036854775807
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll,ll>
#define vll vector<ll>
#define umap unordered_map<ll,ll>
#define lpq priority_queue<ll,vector<ll>,greater<ll>>
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define repr(i,a,n) for(ll i=n-1;i>=a;i--)
ll globe=0;

ll solve(ll i, ll cap, vll &v, vll &w, vector<vll> &dp){
    if(i==v.size()) return 0;
    if(dp[i][cap]!=-1) return dp[i][cap];
    if(cap>=w[i]) dp[i][cap] = max(dp[i][cap], v[i]+solve(i+1,cap-w[i],v,w,dp));
    dp[i][cap] = max(dp[i][cap], solve(i+1,cap,v,w,dp));
    return dp[i][cap];
}

void solution(){
    ll n,c;
    cin>>n>>c;
    vll v(n);
    vll w(n);
    for(ll i=0;i<n;i++) cin>>w[i]>>v[i];
    vector<vll> dp(n,vll (c+1,-1));
    ll ans=solve(0,c,v,w,dp);
    cout<<ans<<endl;
}

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

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll T=1;
    //cin>>T;
    while(T--){
        solution();
    }

    return 0;
}

//BitMasking->setBit: n|(1<<i); unsetBit: n&!(1<<i); checkBit: n&(1<<i);
//ll idcharacter=upper_bound(v.begin(),v.end(),val)-v.begin();
//v.erase(unique(v.begin(),v.end()),v.end());
//ll cnt=count(v.begin(),v.end(),val);
//bool f=find(v.begin(),v.end(),val);

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

using ll=long long;
using namespace std;

#define rep_i(i, n) for (int i = 0; i < (n); ++i)
#define rep_ll(i, n) for (long long i = 0; i < (n); ++i)
#define r_rep_i(i, start, end) for (int i = (start); i < (end); ++i)
#define r_rep_ll(i, start, end) for (long long i = (start); i < (end); ++i)
#define debug_vi(v) copy((v).begin(), (v).end(), ostream_iterator<int>(cout, " "));
#define debug_vll(v) copy((v).begin(), (v).end(), ostream_iterator<long long>(cout, " "));
#define debug_vd(v) copy((v).begin(), (v).end(), ostream_iterator<double>(cout, " "));
#define sort_v(v) sort((v).begin(), (v).end()); //昇順
#define d_sort_vi(v) sort((v).begin(), (v).end(), greater<int>()); //降
#define d_sort_vll(v) sort((v).begin(), (v).end(), greater<long long>()); //降
#define d_sort_vd(v) sort((v).begin(), (v).end(), greater<double>()); //昇順
#define say(t) cout << (t) << endl;
#define sum_vi(v) accumulate((v).begin(), (v).end(), 0);
#define sum_vll(v)accumulate((v).begin(), (v).end(), 0LL);

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

const int IINF=(1<<30);
const ll LLINF = 1LL<<60;

char BigAlphaToSmall(char a){
    if (a >= 65 && a <= 90){
        return a + 32;
    }
    return a;
}

char SmallAlphaToBig(char a){
    if (a >= 97 && a <= 122){
        return a - 32;
    }
    return a;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    int q;cin >> q;
    deque<int> d;
    int a, b, c;
    for (int i = 0; i < q; i++){
        cin >> a;
        if(a == 0){
            cin >> b >> c;
            if(b == 0){
                d.push_front(c);
                continue;
            }
            if(b == 1){
                d.push_back(c);
                continue;
            }
        }

        if(a == 1){
            cin >> b;
            say(d[b])
            continue;
        }

        if(a == 2){
            cin >> b;
            if(b == 0){
                d.pop_front();
                continue;
            }
            if(b == 1){
                d.pop_back();
                continue;
            }
        }
    }
    
}


Measured memory_bytes: 4628
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define translow transform(s.begin(),s.end(),s.begin(),::tolower);
#define transup transform(s.begin(),s.end(),s.begin(),::toupper);
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
 
ll check(string s)
 {
    string s2=s;
    reverse(s2.begin(),s2.end());
    if(s==s2)
        return 1;
    else
        return 0;
 }
    
int main() {
				fast
                // #ifndef ONLINE_JUDGE
                // freopen("input.txt","r",stdin);
                // freopen("output.txt","w",stdout);
                // #endif
                string s;
                cin>>s;
                ll x=check(s);
//                cout<<x<<endl;
                ll len=s.size();
                len=(len-1)/2;
                string y=s.substr(0,len);
                x+=check(y);
//                 cout<<x<<endl;
                len=(s.size()+3)/2;
                ll z=s.size()-len;
                y=s.substr(len-1,z+1);
//                cout<<y<<endl;
                x+=check(y);
//                 cout<<x<<endl;
                if(x==3)
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
                
                // ll x=check()

            
                return 0;
            }

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

for i in range(0, n+1):
    for j in range(0, n+1):
        k = n - i - j
        total = 10000*i + 5000*j + 1000*k
        if total == y and k >= 0:
            print(i, j, k)
            exit()

print(-1, -1, -1)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
//#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  int N, odd = 0, even = 0;
  cin >> N;
  REP(i, N) {
    int tmp;
    cin >> tmp;
    if (tmp % 2 == 0)
      even++;
    else
      odd++;
  }
  if (odd % 2 == 1)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}

Measured memory_bytes: 3644
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<stdio.h>
#define Max 25
#define Nil -1
using namespace std;
struct Tree {
	int p, l, r;
};
Tree T[Max];

void Pre(int u)
{
	if (u == Nil) return;
	cout << ' ' << u;
	Pre(T[u].l);
	Pre(T[u].r);
}

void In(int u)
{
	if (u == Nil) return;
	In(T[u].l);
	cout << ' ' << u;
	In(T[u].r);
}

void Post(int u)
{
	if (u == Nil) return;
	Post(T[u].l);
	Post(T[u].r);
	cout << ' ' << u;
}


int main()
{
	int n;
	int p, l, r;
	int root;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		T[i].p = Nil;
	}
	for (int i = 0; i < n; i++)
	{
		cin >> p >> l >> r;
		T[p].l = l;
		T[p].r = r;
		if (l != Nil)
		{
			T[l].p = p;
		}
		if (r != Nil)
		{
			T[r].p = p;
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (T[i].p == Nil)
		{
			root = i;
		}
	}
	cout << "Preorder" << endl;
	Pre(root);
	cout << endl;
	cout << "Inorder" << endl;
	In(root);
	cout << endl;
	cout << "Postorder" << endl;
	Post(root);
	cout << endl;
	return 0;
}


Measured memory_bytes: 3112
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(int (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
typedef long long lint;
const lint MOD =1000000007;
const lint MAX = 10000000;

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

void COMinit() // 二項係数を求める時は前処理としてCOMinit()をする
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (lint 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;
    }
}
 
// 二項係数計算
long long COM(lint n, lint k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

lint primary(lint num)
{
    if (num < 2) return 0;
    else if (num == 2) return 1;
    else if (num % 2 == 0) return 0; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return 0;
        }
    }

    // 素数である
    return 1;
}

   

int main(){
lint N;
cin>>N;
vector <lint>x;
rep(i,sqrt(N)){
    if(i==0)continue;
    if(N%i==0){
        x.push_back(i-1);
         x.push_back(N/i-1);
    }
}
lint ans=0;

rep(i,x.size()){
    if(x[i]==0)continue;
    lint a=N/x[i];
    if(a==(N%x[i]))ans+=x[i];
}
cout<<ans;

}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
 
const double EPS = 1e-10;
 
struct Point{
    double x,y;
    Point(){}
    Point(double x,double y) :x(x),y(y){}
    Point operator+(Point p) {return Point(x+p.x,y+p.y);}
    Point operator-(Point p) {return Point(x-p.x,y-p.y);}
    Point operator*(double k){return Point(x*k,y*k);}
    Point operator/(double k){return Point(x/k,y/k);}
    double norm(){return x*x+y*y;}
    double abs(){return sqrt(norm());}
 
    bool operator < (const Point &p) const{
        return x!=p.x?x<p.x:y<p.y;
    }
 
    bool operator == (const Point &p) const{
        return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
    }
};
 
typedef Point Vector;
 
 
bool EQ(double a, double b) {
    return abs(a - b) < EPS;
}
 
bool sort_x(Point a,Point b){
    return a.x!=b.x?a.x<b.x:a.y<b.y;
}
 
double cross(Vector a,Vector b){
    return a.x*b.y-a.y*b.x;
}
 
vector<Point> convex_hull(vector<Point> ps, int n) {
    sort(ps.begin(), ps.end(), sort_x);
    int k = 0;
    vector<Point> qs(n * 2);
 
    for (int i = 0; i < n; ++i) {
        while (k > 1 && cross((qs[k - 1] - qs[k - 2]), (ps[i] - qs[k - 1])) <= 0) k--;
        qs[k++] = ps[i];
    }
 
    for (int i = n - 2, t = k; i >= 0; --i) {
        while (k > t && cross((qs[k - 1] - qs[k - 2]), (ps[i] - qs[k - 1])) <= 0) k--;
        qs[k++] = ps[i];
    }
 
    qs.resize(k - 1);
 
    return qs;
}
 
 
#define fs first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define pb emplace_back
 
using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;
 
const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};
 
template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; } return a > b; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; } return a < b; }
template<typename T> inline void print(const T &x, string s = "\n") { cout << x << s; }
template<typename T> inline void print(const vector<T> &v, string s = " ") 
{ rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s); }
inline bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }
inline lint in() { lint x; std::cin>>x; return x; }
 
int main() {
    double a, b;
    char c;
    vector<Point> vi;
    while (cin >> a >> c >> b) {
        vi.clear();
        vi.pb(Point(a, b));
        rep(i, 3) {
            cin >> c >> a >> c >> b;
            vi.pb(Point(a, b));
        }
 
        vector<Point> po = convex_hull(vi, vi.size());
        cout << (po.size() == vi.size() ? "YES" : "NO") << endl;
    }
}

Measured memory_bytes: 3192
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define COUTF(x) cout << setprecision(15) << (x) << endl
#define ENDL cout << endl
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(i, v.size()) cerr << " " << v[i]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

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;
}

signed main() {
  init();
  ll N, A, B, C;
  cin >> N >> A >> B >> C;
  vll L(N);
  rep(i, N) {
    cin >> L[i];
  }
  L.pb(-1);
  L.pb(-1);
  L.pb(-1);
  SORT(L);

  ll ans = INF;
  do {
    if (L[0] == -1) continue;
    ll a = 0;
    ll b = 0;
    ll c = 0;
    ll count = 0;
    ll cost = 0;

    rep(i, N + 3) {
      if (L[i] == -1) {
        count++;
        continue;
      }
      if (count == 0) {
        if (a != 0) cost += 10;
        a += L[i];
      }
      if (count == 1) {
        if (b != 0) cost += 10;
        b += L[i];
      }
      if (count == 2) {
        if (c != 0) cost += 10;
        c += L[i];
      }
    }
    if (a == 0 || b == 0 || c == 0) {
      continue;
    }
    cost += abs(A - a);
    cost += abs(B - b);
    cost += abs(C - c);
    ans = min(ans, cost);
  } while (next_permutation(ALL(L)));

  COUT(ans);

  return 0;
}

Measured memory_bytes: 3576