akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
int n,m;
inline int geti(int x){return (x-1)/m+1;} 
inline int getj(int x){return (x-1)%m+1;}
inline int getx(int i,int j){return (i-1)*m+j;}

const int N=120;
int TL[N],TR[N],vis[N],G[N][N],a[N][N];

int dfs(int x){
	vis[x]=1;
	for(int i=1;i<=n;i++)if(G[x][i]){
		if(!TL[i]){TR[x]=i;TL[i]=x;return 1;}
		else if(!vis[TL[i]]&&dfs(TL[i])){TR[x]=i;TL[i]=x;return 1;}
	}
	return 0;
}

void work(int L){
	memset(G,0,sizeof G);
	memset(TL,0,sizeof TL);
	memset(TR,0,sizeof TR);
	
	for(int i=1;i<=n;++i)
		for(int j=L;j<=m;++j)G[i][geti(a[i][j])]=1;//,fprintf(stderr,"edge %d,%d\n",i,geti(a[i][j]));
	for(int i=1;i<=n;++i){
		memset(vis,0,sizeof vis);
		dfs(i);
	}
	for(int i=1;i<=n;++i){
	//	fprintf(stderr,"TR[%d]=%d\n",i,TR[i]);
		for(int j=L;j<=m;++j)
			if(TR[i]==geti(a[i][j]))swap(a[i][j],a[i][L]);
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)cin>>a[i][j];
	for(int i=1;i<=m;i++)work(i);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cout<<a[i][j]<<(j==m?'\n':' ');//return 0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			while(geti(a[i][j])!=i)swap(a[i][j],a[geti(a[i][j])][j]);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cout<<a[i][j]<<(j==m?'\n':' ');
	return 0;
}



Measured memory_bytes: 512
import sys

readline = sys.stdin.readline

x, n = map(int, readline().split())
if n == 0:
    ans = x
else:
    p = list(map(int, readline().split()))
    for i in range(100):
        if (x - i not in p):
            ans = x-i
            break
        if (x + i not in p):
            ans = x+i
            break
print(ans)

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

int main()
{
    while (true)
    {
        ll h, w;
        cin >> h >> w;
        if (h == 0 && w == 0)
        {
            break;
        }

        for (size_t i = 0; i < h; i++)
        {
            for (size_t j = 0; j < w; j++)
            {
                cout << "#";
            }
            cout << endl;
        }
        cout << endl;
    }
}


Measured memory_bytes: 3076
N,M = map(int, input().split())
A = list(map(int, input().split()))
 
sum = 0
for i in range(N):
    sum = sum + A[i]
    
judge = 0
for i in range(N):
    if A[i] >= sum/(4*M):
        judge = judge +1
        
if judge >= M:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 3064
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
int main()
{
   int N;
   cin >> N;
   
   set<int> d;
   int a = 0;
   int i = 0;
   int n = N;
   while(n--){
       cin >> a;
       d.insert(a);
   }
   
       cout<<d.size()<<endl;
   return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <cstdlib>  // abs() for integer
#include <cmath>

using namespace std;

int main(void) {
    int N;
    cin >> N;
    vector<int> d(N);
    
    for (int i=0;i<N;i++){
        cin >> d[i];
    }
    
    sort(d.begin(),d.end());
   // for (int i=0;i<N;i++){
   // cout << d[i];
   // }
    //cout <<endl;
    int small,small_b,large,large_a;
    small = d[N/2-1];
    small_b = d[N/2-2];
    large = d[N/2];
    large_a = d[N/2+1];
    //cout<<small<<endl;
   // cout<<large <<endl;
    int answer;
    
    if(large ==small){
        answer = 0;
    }
    else {
        answer = large - small;
    }
    cout << answer;
    return 0;
}
    

Measured memory_bytes: 640
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;


// 基本テンプレート
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<double> vd;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<long, long> pil;


// Effective std
template<typename C, typename T>
constexpr int count(C& c, T t) { return count(ALL(c), t); }

template<typename C, typename F>
constexpr int count_if(C& c, F f) { return count_if(ALL(c), f); }

template<typename C, typename T, typename U>
constexpr void replace(C& c, T t, U u) { replace(ALL(c), t, u); }

template<typename C, typename F, typename U>
constexpr void replace_if(C& c, F f, U u) { (ALL(c), f, u); }

template<typename C>
constexpr void sort(C& c) { sort(ALL(c)); }

template<typename C, typename Pred>
constexpr void sort(C& c, Pred p) { sort(ALL(c), p); }


// グラフテンプレート
typedef int Weight;

struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) {}
};

bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight :
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int arr[4][3][10];
int main() {
  int n;
  cin>>n;
  for(int i=0; i<n; i++){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    arr[a-1][b-1][c-1]+=d;
    }
   for(int i=0; i<4; i++){
       for(int j=0; j<3; j++){
           for(int k=0; k<10; k++){
             cout<<" "<<arr[i][j][k];
        }
             cout<<endl;
      }
      if(i==3){
        continue;
      }cout<<"####################"<<endl;
    }
}


Measured memory_bytes: 3104
#include <stdio.h>

bool is_prime(int n)
{
	for (int i = 2; i * i <= n; i++)
		if (n % i == 0)
			return false;
	return true;
}

int main()
{
	int N, n, k = 0;
	scanf("%d", &N);
	while (N--)
	{
		scanf("%d", &n);
		if (is_prime(n))
			k++;
	}
	printf("%d\n", k);

	return 0;
}



Measured memory_bytes: 2728
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int N;
	int cnt=0;
	int A[100];
	cin>>N;
	static int i,Alice,Bob;
	for(i=0;i<N;i++)	cin>>A[i];
	sort(A,A+N);
	for(i=N-1;i>=0;i--){
		if(cnt==0){
			Alice+=A[i];
			cnt=1;
		}
		else{
			cnt=0;	Bob+=A[i];
		}
	}
	cout<<Alice-Bob<<endl;
}

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

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

//container util
//------------------------------------------
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

//repetition
//------------------------------------------
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

double tanri(int m, int y, double r){
    return m * (1 + y * (r/100)) * 1.0;
}

double hukuri(int m, int y, double r){
    return m * pow(1+r/100 , y) * 1.0;
}

int main(){
    int n, y;
    int money = 10000;
    while(cin >> n, n){
        int ans;
        cin >> y;
        double maxRate = 0, rate;
        rep(p, n){
            int b, t;
            double r;

            cin >> b >> r >> t;
            if(t == 1){
                rate = tanri(money, y, r);
            }
            else{
                rate = hukuri(money, y, r);
            }
            if(maxRate < rate){
                maxRate = rate;
                ans = b;
            }
        }
        cout << ans << endl;
    }
}

Measured memory_bytes: 1232
#include <bits/stdc++.h>
using namespace std;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef pair<int, int> Pint;
typedef pair<int64_t, int64_t> Pll;


int main() {
    int64_t N, ans = 0, now;
    cin >> N;
    vector<Pll> SG(N);
    for (int i = 0; i < N; i++){
        int64_t a, b;
        cin >> a >> b;
        SG.at(i).first = a + b;
        SG.at(i).second = a - b;
    }
    sort(ALL(SG));
    ans = 1;
    now = SG.at(0).first;
    for (int i = 1; i < N; i++){
        if (SG.at(i).second < now){
            continue;
        }
        else {
            ans++;
            now = SG.at(i).first;
        }
    }
    cout << ans << endl;

    

}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
int b(int x,int y);
int main()
{
    int K,sum=0;
    scanf("%d",&K);
    for(int i=1;i<=K;i++){
    	for(int j=1;j<=K;j++){
    		for(int z=1;z<=K;z++){
    			int m;
    			m=b(i,j);
    			sum+=b(m,z);
			}
		}
	}
	printf("%d",sum);
	return 0;
} 
int b(int x,int y)
{
	int r;
	while(x%y!=0){
		r=x%y;
		x=y;
		y=r;
	}
	return y;
}



Measured memory_bytes: 3824
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<bitset>
#include<iomanip>
#include<list>
using namespace std;
typedef unsigned long long int ulint;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define RE return 0
//ios::sync_with_stdio(false);
//std::cin.tie(0);
//<< setprecision(20)
const int mod=1e9+7;
const llint big=1e15+100;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
template <class T> void soun(T& ar)
{sort(ar.begin(),ar.end());ar.erase(unique(ar.begin(),ar.end()),ar.end());}
llint gcd(llint a,llint b){if(a%b==0){return b;}else{return gcd(b,a%b);}}
llint lcm(llint a,llint b){return a/gcd(a,b) *b;}
template<class T,class U> auto LB(T& ve,U in){return lower_bound(ve.begin(),ve.end(),in);}
template<class T,class U> auto UB(T& ve,U in){return upper_bound(ve.begin(),ve.end(),in);}
template<class T,class U> auto LBI(T& ve,U in){return LB(ve,in)-ve.begin();}
template<class T,class U> auto UBI(T& ve,U in){return UB(ve,in)-ve.begin();}
//template<class T> void SO(T& ve){sort(T.begin(),T.end());}
int main(void){
	string s,t;cin>>s>>t;
	reverse(t.begin(),t.end());
	cout<<((s==t)?"YES":"NO")<<endl;RE;
}

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

int n;

int main()
{
	cin >> n;
	if (n / 100 == n % 10) printf("Yes\n");
	else printf("No\n");
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
int g[100005];
int main()
{
    int n;
    scanf("%d",&n);
    for (int i=0;i<n;i++) scanf("%d",&g[i]);
    sort(g,g+n);
    int maxn=0;
    for (int i=0;i<n-1;i++)
    {
        if (abs((g[n-1]-g[i])-g[i])<abs((g[n-1]-maxn)-maxn))
            maxn=g[i];
    }
    printf("%d %d\n",g[n-1],maxn);
    return 0;
}


Measured memory_bytes: 1272
import java.util.*;

class Main {
    
	
	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        long H[] = new long[N];
        int A[] = new int[M];
        int B[] = new int[M];
        for(int i=0; i<N; i++) {
        	H[i] = sc.nextLong();
        }
        for(int i=0; i<M; i++) {
        	A[i] = sc.nextInt();
        	B[i] = sc.nextInt();        	
        }
        
        boolean[] good = new boolean[N];
        for(int i=0; i<N; i++) {
        	good[i] = true; 
        }
        int ans = 0;
        
        for(int i=0; i<M; i++) {
        	if(H[A[i]-1] < H[B[i]-1]) {
        		good[A[i]-1] = false;
        	}else if(H[A[i]-1] > H[B[i]-1]) {
        		good[B[i]-1] = false;
        	}else {
        		good[A[i]-1] = false;
        		good[B[i]-1] = false;
        	}
        }
        
        for(int i=0; i<N; i++) {
        	if(good[i] == true) ans++; 
        }
        
        System.out.print(ans);
        
       
	}
}
		
	


Measured memory_bytes: 61232
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 文字列の入力
		String s = sc.next();
		int count = s.length() - s.replaceAll("1", "").length();
		// 出力
		System.out.println(count);
	}
}


Measured memory_bytes: 21844
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())

if (a > b):
    one = a - t * v
    two = b - t * w
    if (one <= two):
        print('YES')
    else:
        print('NO')

else:
    one = a + t * v
    two = b + t * w
    if (one >= two):
        print('YES')
    else:
        print('NO')


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

int main() {
    //
    int num;
    string str;
    int cnt = 0;
    map<string, int> m;

    cin >> num;

    for (int i = 0; i < num; i++) {
        cin >> str;
        m[str]++;
    }

    for (auto itr : m){
        if (itr.second > 0) { 
            cnt++;
        }
    } 

    if(cnt == 3) cout << "Three";
    else cout << "Four";
    //
}


Measured memory_bytes: 3564
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;

string substitute(string src, const string& alpha, const string& beta)
{
  string ret;
  while (!src.empty()) {
    const string::iterator it = search(src.begin(), src.end(), alpha.begin(), alpha.end());
    if (it == src.end()) {
      ret += src;
      break;
    } else {
      ret += string(src.begin(), it) + beta;
      src.assign(it+alpha.size(), src.end());
    }
  }
  return ret;
}

int main()
{
  int n;
  while (cin >> n && n != 0) {
    vector<pair<string,string> > subs(n);
    for (int i = 0; i < n; i++) {
      cin >> subs[i].first >> subs[i].second;
    }
    string gamma, delta;
    cin >> gamma >> delta;
    queue<pair<int,string> > q;
    q.push(make_pair(0, gamma));
    set<string> visited;
    visited.insert(gamma);
    while (!q.empty()) {
      const pair<int,string> p = q.front();
      const int& depth = p.first;
      const string& src = p.second;
      q.pop();

      if (src == delta) {
        cout << depth << endl;
        goto NEXT;
      }

      for (int i = 0; i < n; i++) {
        const string s = substitute(src, subs[i].first, subs[i].second);
        if (s.size() <= delta.size() && visited.insert(s).second) {
          q.push(make_pair(depth+1, s));
        }
      }
    }
    cout << -1 << endl;
NEXT:
    ;
  }
  return 0;
}

Measured memory_bytes: 1044
n=int(input())
N=10**4+1
lst=[0]*N
s=0
for i in range(1,10**2+1):
    for j in range(1,10**2+1):
        for k in range(1,10**2+1):
            s=i**2+j**2+k**2+i*j+j*k+k*i
            if s<10**4+1:
                lst[s]+=1
for i in range(1,n+1):
    print(lst[i])



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

int main() {
  int N;
  cin >> N;
  
  vector<int> B(N-1);
  for (auto &i : B) cin >> i;
  
  int ans = B.front() + B.back();
  for (int i = 0; i < N-2; ++i) ans += min(B[i], B[i+1]);
  cout << ans << endl;
}


Measured memory_bytes: 3648
#include<stdio.h>
int main(void){
	double x1,x2,y1,y2;
	scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
	double y;
	y = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
	printf("%lf\n",y);
}


Measured memory_bytes: 2040
Ss = input()
K = int(input())

nums = []
num = 1
SPrev = Ss[0]
for S in Ss[1:]+'$':
    if S == SPrev:
        num += 1
    else:
        nums.append(num)
        num = 1
    SPrev = S
#print(nums)

if len(nums) == 1:
    ans = nums[0]*K//2
elif Ss[0] != Ss[-1]:
    ans = 0
    for num in nums:
        ans += num//2
    ans *= K
else:
    ans = 0
    for num in nums[1:-1]:
        ans += num//2
    ans *= K
    ans += nums[0]//2 + nums[-1]//2
    ans += (nums[0]+nums[-1])//2 * (K-1)

print(ans)


Measured memory_bytes: 3064
//{{{
#include<algorithm>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ return in >> p.first >> p.second; }
template <class T>
istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }
//}}}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n; cin >> n;
    vector<ll> ae(5); cin >> ae;
    ll mini = *min_element(ae.begin(), ae.end());
    cout << 5 + (n - 1) / mini << '\n';
}


Measured memory_bytes: 888
using System;
class aoj
{
    static void Main()
    {
        for (;;)
        {
            string a = Console.ReadLine();
            char b = ' ';
            bool t = true;
            string[] c = a.Split(b);
            int d, e;
            d = int.Parse(c[0]);
            e = int.Parse(c[1]);
            if (d == 0 && e == 0) break;
            for (int i = 0; i < d; i++)
            {
                for (int j = 0; j < e; j++)
                {
                    if (t)
                    {
                        Console.Write("#");
                        if (e % 2 == 0 && j == e - 1)
                        {
                            t = true;
                        }
                        else
                        {
                            t = false;
                        }
                        
                    }
                    else
                    {
                        Console.Write(".");

                        if(e%2==0&&j==e-1&&t==false)
                        {
                            t = false;
                        }
                        else
                        {
                            t = true;
                        }
                        
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}

Measured memory_bytes: 8820
#include <stdio.h>

int main()
{
    int i = 1;
    int x;
    int n;
    scanf("%d", &n);

    while (i <= n)
    {
        x = i;
        if (x % 3 == 0) {
            printf(" %d", i);
        }
        else
        {
            while (x)
            {
                if (x % 10 == 3)
                {
                    printf(" %d", i);
                    break;
                }
                x /= 10;
            }
        }
        i++;
    }
    printf("\n");

    return 0;
}


Measured memory_bytes: 2104
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

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

    int left = 0;
    int height = 0;

    for (int i = 0; i < s.length(); i++) {
        if (s[i] == '(') {
            height++;
        } else {
            height--; 
        }
        if (height < 0) {
            height = 0;
            left++;
        }
    }
    string l(left, '(');
    string r(height, ')');
    cout << l + s + r << endl; 


    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
	long long int n,m,w,a,z;
	scanf("%lld%lld",&n,&m);
	if(m>n){
		a=m-n*2;
		w=a/4;
		z=w+n;
		printf("%lld\n",z);
	}
	else{
		printf("%lld\n",m/2);
	}	
	return 0;
}

Measured memory_bytes: 128
S = list(input())
Q = int(input())
reverse = False
flont = []
for _ in range(Q):
    tmp = input().split()
    if len(tmp) == 1:
        reverse = not reverse
    else:
        F = int(tmp[1])
        C = tmp[2]
        if F == 1:
            if reverse:
                S.append(C)
            else:
                flont.append(C)
        else:
            if reverse:
                flont.append(C)
            else:
                S.append(C)
flont.reverse()
flont.extend(S)
if reverse:
    flont.reverse()
print(''.join(flont))

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

ll s,cp;
int len,first;
ll read(){
	ll x=0,f=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c<='9'&&c>='0'){x=x*10+c-'0';c=getchar();}
	return x*f;
}

ll cur=0;
int ans=-1;
void dfs(int num,int dep,int tot)
{
	ll power=pow(10,len-dep);
	//printf("(num=%d,dep=%d,tot=%d)\npow=%d cur=%d\n",num,dep,tot,power,cur+power*num);
	if (cur+power*num>s) return ;
	if (tot+9*(len-dep)<=ans) return ;
	cur+=power*num;
	if (dep==len){ans=max(ans,tot);return ;}
	for (int i=9;i>=0;--i)
		dfs(i,dep+1,tot+i);
	cur-=power*num;
}
int main()
{
	s=read();
	cp=s;
	len=log(s)/log(10);
	for (int i=1;i<=len;++i) cp/=10;
	first=cp;
	len++;
	
	//cur+=first*pow(10,len-1);
	for (int i=first;i>=0;--i) dfs(i,1,i);
	cout<<ans;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define clr(a) memset(a, 0, sizeof(a))
#define sz(x) x.size()
#define printarray(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define rep(n) for (ll i = 0; i < n; i++)
#define repc(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b)/gcd(a, b)
#define int long long int
#define ull unsigned long long

using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;

int fastexpo(int b, int exp){
    if(exp == 0) return 1;
    if(exp == 1) return b;
    int ans = (fastexpo(b,exp/2) % MOD);
    ans *= ans;
    ans %= MOD;
    if(exp % 2 == 1){
        ans *= b;
    }
    ans %= MOD;
    return ans;
}

bool checkprime(int n){
	for(int i= 2;i*i<=n;i++){
		if(n % i == 0){
			return false;
		}
	}
	return true;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    while(checkprime(n) == false){
    	n++;
    }
    cout<<n<<endl;
    return 0;
}





Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int N = 210;
char s[N][N];
int stg[N];
int n;
int main()
{
	cin >> n;
	for(int i=1;i<=n;i++){
		scanf("%s",s[i]+1);
	}
	int ret=-1;
	for(int S=1;S<=n;S++){
		queue<int> q;
		q.push(S);
		for(int i=1;i<=n;i++)stg[i]=0;
		stg[S]=1;
		int ans = 1;
		while(q.size()){
			int x=q.front();q.pop();
			ans=max(ans, stg[x]);
			for(int j=1;j<=n;j++)if(s[x][j]=='1'){
				if(!stg[j])stg[j]=stg[x]+1,q.push(j);
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++)if(s[i][j]=='1'){
				if(abs(stg[i]-stg[j])!=1)ans=-1;
			}
		}
		ret=max(ret,ans);
	}
	cout << ret << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
    long n;
    cin >>n;
    vector<long> HP;
    for(long i=0;i<n;i++){
        long tmp;
        cin >> tmp;
        HP.push_back(tmp);
    }

    sort(HP.begin(),HP.end());
    long size = n;
    while(HP.size()!=1){
        
        size = HP.size();
        long n_0=0;
        for(long i=1;i<size;i++){
            HP.at(i) = HP.at(i) % HP.at(0);
            if(HP.at(i) == 0){
                n_0++;
            }
        }
        sort(HP.begin(),HP.end());
        HP.erase(HP.begin(),HP.begin()+n_0);
    }

    printf("%ld",HP.at(0));

}


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

int main() {
  int K,S,c=0;
  cin >> K >> S;
  for(int x=0; x<=K; x++){
    for(int y=0; y<=min(S-x,K); y++){
      if(S-x-y<=K){
        c++;
      }
    }
  }
  cout << c << endl;
}


Measured memory_bytes: 256
(defun calc-gcd (a b)
  (if (zerop (mod a b)) b (calc-gcd b (mod a b))))

(defun calc-divisor (a)
  (loop :for x :from 1 :to a
        :when (zerop (mod a x))
        :collect x))

(defun solve (a b k)
  (let* ((gcd (calc-gcd a b))
         (divisors (calc-divisor gcd)))
    (nth (1- k) (reverse divisors))))


(defun main ()
  (let* ((a (read))
         (b (read))
         (k (read)))
    (format t "~A~%" (solve a b k))))

(main)


Measured memory_bytes: 15976
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	text := stdin.Text()
	strs := strings.Split(text, " ")

	A, _ := strconv.Atoi(strs[0])
	B, _ := strconv.Atoi(strs[1])

	ret := max(max(2*A-1, A+B), 2*B-1)
	fmt.Println(ret)

}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}


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

using namespace std;
////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define LEN(x) ((int)(x).size())
#define ALL(x) x.begin(), x.end()
using ll = long long;
using llu = long long unsigned;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
using vpii = vector<pii>;

const ld EPS = 1e-9;
inline int cmp(ld x, ld y = 0, ld tol = EPS) {
	return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

const int MOD = 1;
inline int mod(ll x, int m = MOD) {
	return (int)(((x%m) + m)%m);
}

// template<typename T, typename M = __gnu_pbds::null_type>
// using ordered_set = __gnu_pbds::tree<T, M, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

////////////////////////// Solution starts below. //////////////////////////////



int main () {
	// ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.precision(10);
    int n;
    cin>>n;
    n>=30? cout<<"Yes"<<endl:cout<<"No"<<endl;

	return 0;
}

Measured memory_bytes: 3604
# encoding:utf-8
import copy
import numpy as np
import random

N,M = map(int, input().split())

submit_time = (N-M)*100+M*1900

expe = 0
pro = (1/2)**M
for i in range(1,1000):
    expe += i*(1-pro)**(i-1)*pro

ans = expe*submit_time
print(round(ans))


Measured memory_bytes: 25192
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	A := nextInt()
	B := nextInt()

	n := 0
	for A*n-(n-1) < B {
		n++
	}

	fmt.Println(n)
}

var stdin = initStdin()

func initStdin() *bufio.Scanner {
	bufsize := 1 * 1024 * 1024 // 1 MB
	var stdin = bufio.NewScanner(os.Stdin)
	stdin.Buffer(make([]byte, bufsize), bufsize)
	stdin.Split(bufio.ScanWords)
	return stdin
}

func nextString() string {
	stdin.Scan()
	return stdin.Text()
}

func nextBytes() []byte {
	stdin.Scan()
	return stdin.Bytes()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextString())
	return i
}

func nextInt64() int64 {
	i, _ := strconv.ParseInt(nextString(), 10, 64)
	return i
}


Measured memory_bytes: 640
import java.util.*;
 
public class Main {
	public static void main (String[] args) {
      Scanner sc = new Scanner(System.in);
      
      int t= sc.nextInt();
      int[][] arr = new int[t][3];
      for(int i =0;i<t;i++){
      	for(int j =0;j<3;j++){
        	arr[i][j]= sc.nextInt();
        }
      }
      
      int[][] result = new int[t][3];
      result[0][0]= arr[0][0];
      result[0][1] = arr[0][1];
      result[0][2]= arr[0][2];
      
      for(int i =1;i<t;i++){
        	result[i][0]= Math.max(result[i-1][1],result[i-1][2])+arr[i][0]; 
            result[i][1]= Math.max(result[i-1][0],result[i-1][2])+arr[i][1];
            result[i][2]= Math.max(result[i-1][1],result[i-1][0])+arr[i][2];
      }

      System.out.println(Math.max(result[t-1][0],Math.max(result[t-1][1],result[t-1][2])));
    }
  
}


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

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

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

  map<ll, ll> m;
  ll n;
  cin >> n;
  ll a[n];
  rep(i, 1, n + 1) {
    cin >> a[i];
    ++m[i - a[i]];
  }
  ll rsl = 0;
  rep(i, 1, n + 1) {
    rsl += m[i + a[i]];
  }
  cout << rsl << endl;
}


Measured memory_bytes: 27912
n, m = map(int, input().split())

if n == 1 and m == 1:
    print(1)
elif n == 1:
    print(m - 2)
elif m == 1:
    print(n - 2)
elif n == 2 or m == 2:
    print(0)
else:
    print((n - 2) * (m - 2))

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

bool isprime(int n) {
  if (n==1)
    return false;
  else {
    bool ok = true;
    for (int i=2; i*i<=n; i++) {
      if (n%i==0) {
        ok = false;
        break;
      }
    }
    return ok;
  }
}

int main() {
  int x;
  cin >> x;
  for (int i=x; i<=200000; i++) {
    if (isprime(i)) {
      cout << i << endl;
      break;
    }
  }
}


Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000
#define SENTINEL 2000000000

vector< pair<string, int> > L(MAX/2+2), R(MAX/2+2);

void merge(vector< pair<string, int> > &A, int left, int mid, int right) {
    int i, j, k;
    int n1 = mid - left;
    int n2 = right - mid;
    for (int i = 0; i < n1; i++) L.at(i) = A.at(left + i);
    for (int i = 0; i < n2; i++) R.at(i) = A.at(mid + i);
    L.at(n1).second = R.at(n2).second = SENTINEL;
    i = j = 0;
    for (k = left; k < right; k++) {
        if (L.at(i).second <= R.at(j).second) {
            A.at(k) = L.at(i++);
        } else {
            A.at(k) = R.at(j++);
        }
    }
}

void mergeSort(vector< pair<string, int> > &A, int left, int right) {
    int mid;
    if (left + 1 < right) {
        mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

int partition(vector< pair<string, int> > &A, int p, int r) {
    int x = A.at(r).second;
    int i = p - 1;
    for (int j = p; j < r; j++) {
        if (A.at(j).second <= x) {
            i = i + 1;
            swap(A.at(i), A.at(j));
        }
    }
    swap(A.at(i+1), A.at(r));
    return i + 1;
}

void quickSort(vector< pair<string, int> > &A, int p, int r) {
    int q;
    if (p < r) {
        q = partition(A, p, r);
        quickSort(A, p, q-1);
        quickSort(A, q+1, r);
    }
}

int main() {
    int n;
    cin >> n;
    vector< pair<string, int> > A(n);
    vector< pair<string, int> > B(n);
    for (int i = 0; i < n; i++) {
        string f;
        int s;
        cin >> f >> s;
        A.at(i) = make_pair(f, s);
        B.at(i) = make_pair(f, s);
    }

    mergeSort(A, 0, n);
    quickSort(B, 0, n-1);

    bool stable = true;
    for (int i =0; i < n; i++) {
        if (A.at(i).first != B.at(i).first) stable = false;
    }
    if (stable) cout << "Stable" << endl;
    else cout << "Not stable" << endl;
    for (int i = 0; i < n; i++) {
        cout << B.at(i).first << " " << B.at(i).second << endl;
    }

    return 0;
}


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

int m;
int re[100]={};
int ra[100]={};

int msort(int i_s,int i_e){
	if(i_s != i_e){
		msort(i_s,(i_s+i_e)/2);
		msort((i_s+i_e)/2+1,i_e);
		int j1=i_s;
		int j2=(i_s+i_e)/2+1;
		int ms[100]={};
		for(int i=i_s;i<=i_e;i++){
			if((re[ra[j2]]>re[ra[j1]] || j1==(i_s+i_e)/2+1) && j2<=i_e){
				ms[i]=ra[j2];
				j2++;
			}else{
				ms[i]=ra[j1];
				j1++;
			}
		}
		for(int i=i_s;i<=i_e;i++){
			ra[i]=ms[i];
		}
	}
	return 0;
}

int main() {
	int n,a;
	while(1){
		cin >> n >> m;
		if(n==0){
			return 0;
		}
		for( int j=0;j<m;j++){
			re[j]=0;
			ra[j]=j;
		}
		for( int i=0;i<n;i++){
			for (int j=0;j<m;j++){
				cin >> a;
				re[j]+=a;
			}
		}
		msort(0,m-1);
		cout << ra[0]+1;
		for(int i=1;i<m;i++){
			cout << ' ' << ra[i]+1;
		}
		cout << endl;
	}
}

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

bool solve() {
	int n; cin >> n;
	int cnt[110] = {};
	int maxi = 0;
	for(int i = 0; i < n; ++i) {
		int a; cin >> a;
		cnt[a]++;
		maxi = max(maxi, a);
	}
	if(cnt[maxi] < 2) {
		return false;
	}
	cnt[maxi] -= 2;
	if(maxi % 2 == 0) {
		if(cnt[maxi / 2] < 0) {
			return false;
		}
		--cnt[maxi / 2];
	}
	for(int i = (maxi + 2) / 2; i < maxi; ++i) {
		if(cnt[i] < 2) {
			return false;
		}
		cnt[i] -= 2;
	}	
	for(int i = (maxi + 1) / 2; i > 0; --i) {
		if(cnt[i] > 0) {
			return false;
		}
	}
	return true;
}

int main() {
	cout << (solve() ? "Possible" : "Impossible") << '\n';
	return 0;
}


Measured memory_bytes: 256
import sys

def dijkstra(m, start, goal, matrix):
  defnode = [False] * (m+1)
  cost = [sys.maxint] * (m+1)
  cost[start] = 0
  node = start
  while True:
    defnode[node] = True
    for i in xrange(1, m+1):
      if defnode[i]:
        continue
      if matrix[node][i] == sys.maxint:
        continue
      cost[i] = min(cost[i], cost[node]+matrix[node][i])
    minnode = sys.maxint
    mincost = sys.maxint
    for i in xrange(1, m+1):
      if defnode[i]:
        continue
      if cost[i] < mincost:
        minnode = i
        mincost = cost[i]
    if minnode == goal:
      print cost[goal]
      return
    node = minnode

while True:
  n, m = map(int, raw_input().split())
  if n == 0 and m == 0:
    break
  cost = [[sys.maxint] * (m+1) for j in xrange(m+1)]
  time = [[sys.maxint] * (m+1) for j in xrange(m+1)]
  for i in xrange(n):
    a, b, c, t = map(int, raw_input().split())
    cost[a][b] = c
    cost[b][a] = c
    time[a][b] = t
    time[b][a] = t
  k = int(raw_input())
  for i in xrange(k):
    p, q, r = map(int, raw_input().split())
    if r == 0:
      dijkstra(m, p, q, cost)
    else:
      dijkstra(m, p, q, time)

Measured memory_bytes: 6748
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);

	public static void main(String[] args) {
		int a = scan.nextInt();
		int b = scan.nextInt();
		if(a + b == 15) {
			System.out.println("+");
		} else if (a * b == 15) {
			System.out.println("*");
		} else {
			System.out.println("x");
		}
    }
}


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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod = int(1e9)+7;
using V = vector<ll>;
using P = pair<ll,ll>;


int main(){
    vector<ll>co(1e5+10,0);
    int n;cin >>n;
    for (int i = 0; i < n; ++i) {
        ll a;cin >>a;
        co[a]++;
        if(a>0)co[a-1]++;
        co[a+1]++;
    }
    ll mx=0;
    for (int j = 0; j < 1e5 + 1; ++j) {
        mx=max(mx,co[j]);
    }
    cout <<mx << endl;
    return 0;
}

Measured memory_bytes: 1024
#include<iostream>
#include<vector>
#include<utility>
#include<bitset>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;

int main(){
	int a, b, count=0;
	cin >> a >> b;
	for(int i=a; i<=b; i++){
		int tmp=i;
		int fif=tmp/10000;
		tmp %= 10000;
		int fou=tmp/1000;
		tmp %= 1000;
		tmp %= 100;
		int sec=tmp/10;
		tmp %= 10;
		if(fif==tmp && fou==sec)
			count++;
	}
	cout << count << '\n';
	return 0;
}

Measured memory_bytes: 764
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>
#include <bitset>
#include <complex>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define all(x) (x).begin(),(x).end()

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 (b<a) { a=b; return 1; } return 0; }

typedef long long ll;

using namespace std;


int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n,k,q;
  cin>>n>>k>>q;
  vector<int>gain(n,k);
  rep(i,q)
  {
    int a;
    cin>>a;
    a--;
    gain[a]++;
  }
  rep(i,n)
  {
    if(gain[i]<=q)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
  }

  return 0;

}


Measured memory_bytes: 1024
# なんか身長順でソートすればいいんじゃない？
# 身長差でありえるのは1〜Nなので，身長差が1になるエージェントの組み合わせをだして，それぞれの身長差について調べる
# でも結局全探索になるか？やっぱり配列を作ると同時に答えが独立してうまく求まるようなのがいいよね
# j-i = Aj+Ai となるのが求めるもの，これを変換してiとjの式のみに分離すると，うまく配列を作るだけでうまくいくようになる
# j-Aj = Ai+i となる．こうすると独立しているので,X=i-Ai,Y=Ai+iを作り，setもしくはdictで探索すればすぐです！
# i==jのときはi-i == Ai+Ai が絶対に成立しないので問題ない

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

X=[i+1-A[i] for i in range(N)]
Y=[i+1+A[i] for i in range(N)]
#Y=set(Y) # set使うと同じ要素が減るのでだめ！！！
dicY=dict()
for y in Y:
    if not y in dicY:
        dicY[y]=1
    else:
        dicY[y]+=1
count=0
for x in X:
    if x in dicY:
        count+=dicY[x]
print(count)


Measured memory_bytes: 50192
# 入力値
a = gets.chomp.to_i
b = gets.chomp.to_i
c = gets.chomp.to_i
x = gets.chomp.to_i

# 制約のチェック
raise "Aは0以上、50以下" unless a.between?(0, 50)
raise "Bは0以上、50以下" unless b.between?(0, 50)
raise "Cは0以上、50以下" unless c.between?(0, 50)
raise "(A+B+C)は1以上" unless (a + b + c) >= 1
raise "Xは50以上、20,000以下" unless x.between?(50, 20000)
raise "Xは50の倍数" unless x % 50 == 0

# 組み合わせ計算
counter = 0
0.upto a do |ai|
  0.upto b do |bi|
    0.upto c do |ci|
      yen = 500 * ai + 100 * bi + 50 * ci
      counter = counter.next if yen == x
    end
  end
end

# 出力
puts counter

Measured memory_bytes: 1788
#include <stdio.h>

    int main(void)
    {
        int n,out;
        scanf("%d",&n);
        if(n>9999)
        {
            out=n-9999+900+9;
            if(n==100000)
                out--;
        }
        else if(n>999)
            out=999-99+9;
        else if(n>99)
            out=n-99+9;
        else if(n>=9)
            out=9;
        else
            out=n;
        printf("%d",out);
        return 0;
    }


Measured memory_bytes: 128
import sys
input = sys.stdin.readline
from collections import deque
from itertools import product
import heapq


def read(INF=10**7):
    H, W = map(int, input().strip().split())
    CH, CW = map(int, input().strip().split())
    DH, DW = map(int, input().strip().split())
    S = [[0 for j in range(W)] for i in range(H)]
    for i in range(H):
        s = list(input().strip())
        for j in range(W):
            if s[j] == "#":
                S[i][j] = -2
            else:
                S[i][j] = INF
    return H, W, CH, CW, DH, DW, S


def solve(H, W, CH, CW, DH, DW, S, INF=10**7):
    DIR0 = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    DIR1 = [
        (-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),
        (-1, -2), (-1, -1), (-1, 1), (-1, 2),
        (0, -2), (0, 2),
        (1, -2), (1, -1), (1, 1), (1, 2),
        (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)
    ]
    S[CH-1][CW-1] = 0
    # Priority queue
    q = []
    heapq.heappush(q, (0, CH-1, CW-1))
    while q:
        # キューの先頭から距離の短い順に候補を取り出す
        d0, y0, x0 = heapq.heappop(q)
        # 距離0で到達可能な場所をキューに入れる
        for dy, dx in DIR0:
            ny, nx = y0 + dy, x0 + dx
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] > d0:
                heapq.heappush(q, (d0, ny, nx))
                S[ny][nx] = d0
        # 距離1で到達可能な場所をキューに入れる
        for dy, dx in DIR1:
            ny, nx = y0 + dy, x0 + dx
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] > d0+1:
                heapq.heappush(q, (d0+1, ny, nx))
                S[ny][nx] = d0+1
    return S[DH-1][DW-1] if S[DH-1][DW-1] != INF else -1


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))


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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

ll n;
vll G[10005];
vll c;

vll ans;
ll pos=1;
void dfs(ll v, ll p){
  for(auto nv: G[v]){
    if(nv==p) continue;
    ans[nv]=c[pos++];
    dfs(nv,v);
  }
}

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  cin>>n;
  rep(i,n-1){
    ll a,b; cin>>a>>b; a--,b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  c.resize(n);
  ll sum=0;
  rep(i,n) cin>>c[i], sum+=c[i];
  sort(c.rbegin(),c.rend());
  sum-=c[0];
  
  ans.resize(n);
  ans[0]=c[0];
  
  dfs(0,-1);
  
  cout<<sum<<endl;
  rep(i,n){
    cout<<ans[i]<<" ";
  }
  cout<<endl;
  
}


Measured memory_bytes: 1536
#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define SZ(x) ((int)(x.size()))
#define FOI(i,a,n) for(int (i)=int(a);(i)<=int(n);++(i))
#define FOD(i,a,n) for(int (i)=int(a);(i)>=int(n);--(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define MSET(tabl,i) memset(tabl, i, sizeof(tabl))
#define PSET(x,y) fixed<<setprecision(y)<<lf(x)
#define DBG(c) cout<<#c<<" = "<<c<<endl;
#define RTIME ((double)clock()/(double)CLOCKS_PER_SEC)

template<typename T,typename S>inline bool REMIN(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool REMAX(T&a,const S&b){return a<b?a=b,1:0;}

typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;

int bit(ll x, int pos){ return (x >> pos) & 1; }
ll power(ll base, ll exp, ll c = 1e9 + 7) { if(!exp) return 1; ll r = power(base, exp >> 1, c); r=(r*r)%c; if(exp&1) r=(r*base)%c; return r; }

ll T,N,M,K;  
int a,b,c;
string s1,s2;

const lf PI = 2*acos(0);
const ll INF = 1e9 + 4;
const int NMAX = 3e5+5;
const ll MOD = 1000000007;

vi eds[NMAX];

int dfs(int cur, int par, int tillnow = 1) {
	if(SZ(eds[cur]) == 1) return tillnow;
	if(SZ(eds[cur]) == 2) {
		for(int v : eds[cur]) if(v != par) return dfs(v, cur, tillnow + 1);
	}
	int ret = 0;
	for(int v : eds[cur]) if(v != par) ret ^= dfs(v, cur, 1);
	return ret + tillnow;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	cin >> N;
	FOI(i, 1, N - 1) {
		cin >> a >> b;
		eds[a].pb(b);
		eds[b].pb(a);
	}

	int ans = 0;
	for(int x : eds[1]) {
		ans ^= dfs(x, 1);
	}

	cout << (ans ? "Alice" : "Bob");
	
	return 0;
}


Measured memory_bytes: 11008
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int DP[85][85][30000]; 

int main() {
  int H, W;
  cin>>H>>W;
  int A[H][W], B[H][W];
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      cin>>A[i][j];
    }
  }
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      cin>>B[i][j];
    }
  }
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      A[i][j] -= B[i][j];
    }
  }
  DP[0][0][15000+A[0][0]] = 1;
  DP[0][0][15000-A[0][0]] = 1;
  for(int i=0;i<H-1;++i) {
    for(int j=0;j<30000;++j) {
      if(DP[i][0][j]>0) {
        DP[i+1][0][j+A[i+1][0]] = 1;
        DP[i+1][0][j-A[i+1][0]] = 1;
      }
    }
  }
  for(int i=0;i<W-1;++i) {
    for(int j=0;j<30000;++j) {
      if(DP[0][i][j]>0) {
        DP[0][i+1][j+A[0][i+1]] = 1;
        DP[0][i+1][j-A[0][i+1]] = 1;
      }
    }
  }
  for(int i=1;i<H;++i) {
    for(int j=1;j<W;++j) {
      for(int k=0;k<30000;++k) {
        if(DP[i-1][j][k]>0) {
          DP[i][j][k+A[i][j]] = 1;
          DP[i][j][k-A[i][j]] = 1;
        }
        if(DP[i][j-1][k]>0) {
          DP[i][j][k+A[i][j]] = 1;
          DP[i][j][k-A[i][j]] = 1;
        }
      }
    }
  }
  int ans = inf;
  for(int i=0;i<30000;++i) {
    if(DP[H-1][W-1][i]>0) ans = min(ans, abs(i-15000));
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 349364
using System;

public class Hello
{
    public static void Main()
    {
        var dset = 1;
        while (true)
        {
            var wmax = int.Parse(Console.ReadLine().Trim());
            if (wmax == 0) break;
            var n = int.Parse(Console.ReadLine().Trim());
            var vaL = new int[n + 1];
            var w = new int[n + 1];
            var dp = new int[n + 1, wmax + 1];
            DpSet(dp, vaL, w);
            for (int i = 0; i < n; i++)
                for (int j = 0; j <= wmax; j++)
                {
                    if (j < w[i]) dp[i + 1, j] = dp[i, j];
                    else dp[i + 1, j] = Math.Max(dp[i, j], dp[i, j - w[i]] + vaL[i]);
                }
            var answ = 0;
            for (int i = 0; i <= wmax; i++)
                if (dp[n, i] == dp[n, wmax]) { answ = i; break; }
            Console.WriteLine("Case {0}:", dset);
            dset++;
            Console.WriteLine(dp[n, wmax]);
            Console.WriteLine(answ);
        }
    }
    public static void DpSet(int[,] dp, int[] vaL, int[] w)
    {
        for (int i = 0; i < vaL.Length - 1; i++)
        {
            string[] line2 = Console.ReadLine().Trim().Split(',');
            vaL[i] = int.Parse(line2[0]);
            w[i] = int.Parse(line2[1]);
        }
    }
}

Measured memory_bytes: 13436
#include <stdio.h>

int main()
{
  int n, q, i, j, ans=0;
  int S[10000], T[500];
  
  scanf("%d", &n);
  for(i=0; i<n; i++)
    scanf("%d", &S[i]);
    
  scanf("%d", &q);
  for(i=0; i<q; i++){
    scanf("%d", &T[i]);
    for(j=0; j<n; j++){
      if(T[i] == S[j]){
        ans++;
        break;
      }
    }
  }
  
  printf("%d\n", ans);
  
  return 0;
}

Measured memory_bytes: 2744