akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
using namespace std;

int main() {
    int N, Y;
    cin >> N >> Y;
    int res10000 = -1, res5000 = -1, res1000 = -1;  // 答えを格納 (見つからなかったら -1)
    for (int a = 0; a <= N; ++a) {  // 10000円の枚数を 0 〜 N で調べる
        for (int b = 0; b + a <= N; ++b) {  // 5000円の枚数を 0 〜 N-a で調べる
            
            // 見つかっていたら break --- 実はこの処理はしなくても大丈夫です
            if (res10000 != -1) break;
            
            int c = N - a - b;  // 1000円の枚数は決まる
            int total = 10000*a + 5000*b + 1000*c;
            if (total == Y) {
                res10000 = a;
                res5000 = b;
                res1000 = c;
            }
        }
    }
    
    // 答えを出力 (見つかっていなくても -1 -1 -1 になるので OK です)
    cout << res10000 << " " << res5000 << " " << res1000 << endl;
}

Measured memory_bytes: 256
#include <iostream>
//#include <stdio.h>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <map>
#include <algorithm>
#include <math.h>
#include <iostream>     // std::cout, std::fixed
#include <iomanip>
#include<queue>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <sstream>
using namespace std;
typedef long long ll;



int main () {
    ios_base::sync_with_stdio(false);
    int n=3;
    int a[n][n];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    for(int a1=0;a1<=1000;a1++)
    {
        int b1=a[0][0]-a1;
        int b2=a[0][1]-a1;
        int b3=a[0][2]-a1;
        int a2=a[1][0]-b1;
        int a3=a[2][0]-b1;
        if(a2+b2==a[1][1] && a3+b3==a[2][2] && a[2][1]==a3+b2 && a[1][2]==a2+b3)
        {
            cout<<"Yes";
            return 0;
        }
    }
    cout<<"No";
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <string>
#include <queue>
#include <numeric>
#include <ctype.h>
#include <limits>
#include <iomanip>

#define rep(i,n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
using namespace std;

typedef long long ll;

int main() {
	/*int n;
	cin >> n;
	double ans = (n+1) / 2;
	ans /= (double)n;
	cout << setprecision(7) << ans;*/


	int n, k;
	cin >> n >> k;
	int h;
	int ans = 0;
	rep(i, n) {
		cin >> h;
		if (h >= k)ans++;
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	int n;
	int dp[100001];
	fill_n(dp, 100001, INF);
	int a[100001];
	cin >> n;
	rep(i, n)
		cin >> a[i];

	rep(i,n){
		*lower_bound(dp, dp + n, a[i]) = a[i];
	}
	cout << lower_bound(dp, dp + n, INF)-dp << endl;

	return 0;
}

Measured memory_bytes: 1940
import java.util.*;

public class Main {
  public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    String N = sc.next();
    if(N.equals("Sunny")){
    	System.out.println("Cloudy");
    }else if(N.equals("Cloudy")){
    	System.out.println("Rainy");
    }else{
    	System.out.println("Sunny");
    }
  }
}

Measured memory_bytes: 22228
#include <stdio.h>
 
int main(void)
{
    int n;
    int ii;
    int i;
    float k1, k2;
    float x[4], y[4];
     
    scanf("%d", &n);
    for (ii = 0; ii < n; ii++){
        for (i = 0; i < 4; i++){
            scanf("%f %f", &x[i], &y[i]);
        }
        k1 = (y[1] - y[0]) / (x[1] - x[0]);
        k2 = (y[3] - y[2]) / (x[3] - x[2]);
         
        if (k1 == k2){
            printf("YES\n");
        }
        else {
            printf("NO\n");
        }
    }
     
    return (0);
}

Measured memory_bytes: 612
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



long long GCD(long long a, long long b) {
    return b ? GCD(b, a%b) : a;
}

int main() {
    int N; cin >> N;
    vector<long long> A(N);
    for (int i = 0; i < N; ++i) cin >> A[i];

    long long res = A[0];
    for (int i = 0; i < N; ++i) {
        res = GCD(res, A[i]);
    }
    cout << res << endl;
}














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

#define int				long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define FORE( i, m, n ) FOR( i, m , n + 1 )
#define REP( i, n )     FOR( i, 0, n )
#define REPE( i, n )	FOR( i, 0, n + 1 )
#define REPR( i, m )	for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )	FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define P				pair<int, int>

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

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

signed main() {
	int n, yen; cin >> n >> yen;

	REPE( x, n ) {
		REPE( y, n ) {
			int z = n - x - y;
			if( 10000 * x + 5000 * y + 1000 * z == yen && z >= 0 ) {
				cout << x << " " << y << " " << z << endl;
				return 0;
			}
		}
	}

	cout << -1 << " " << -1 << " " << -1 << endl;
}

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

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < n; i++)
#define Rep(i, n) for(int i = 1; i < n; i++)
#define In(vec) rep(i, n) cin>>vec[i];
#define ANS(s) cout << s << endl
#define inc(a, b) cin >> a >> b;

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vii;

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

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int round_int(int a, int b) { return (a + (b - 1)) / b; }


// 4,8方向に探索する際に用いる配列
//int dx[4] = { 0,  0, 1, -1 };
//int dy[4] = { 1, -1, 0,  0 };
int dx[8] = { 1, 0, -1,  0, 1, -1, -1,  1 };
int dy[8] = { 0, 1,  0, -1, 1,  1, -1, -1 };

constexpr auto MAX = 100010;

// ────────────────────────────────────────────────────

int main()
{
	ll n;
	cin >> n;

	vl a(n), b(n), c(n);

	In(a);
	In(b);
	In(c);

	sort(ASC(a));
	sort(ASC(b));
	sort(ASC(c));

	ll ans = 0;

	rep(i, n)
	{
		ans += (lower_bound(ASC(a), b[i]) - a.begin()) * (c.end() - upper_bound(ASC(c), b[i]));
	}
	ANS(ans);
}


Measured memory_bytes: 2560
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

struct timer{
	time_t start;
	timer(){start=clock();}
	~timer(){cerr<<1.*(clock()-start)/CLOCKS_PER_SEC<<" secs"<<endl;}
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int M,N,K;
int sum[3][1010][1010];
string s="JOI";

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	
	cin>>M>>N>>K;
	char C;
	rep(i,M)rep(j,N){
		cin>>C;
		rep(k,3){
			sum[k][i+1][j+1]=sum[k][i+1][j]+sum[k][i][j+1]-sum[k][i][j]+(C==s[k]);
		}
	}
	int a,b,c,d;
	rep(i,K){
		cin>>a>>b>>c>>d;
		a--,b--;
		rep(k,3){
			cout<<sum[k][c][d]-sum[k][a][d]-sum[k][c][b]+sum[k][a][b]<<(k==2?"\n":" ");
		}
	}
}

Measured memory_bytes: 13108
n = int(input())
a, b = map(int, input().split())
c = list(map(int, input().split()))
o = 1000
out = 0
for i in range(n):
    t = abs(b-(a-c[i]*0.006))
    if t < o:
        o = t
        out = i+1
print(out)


Measured memory_bytes: 3060
const main = (input) => {
  input = input.split(' ');
  const a = parseInt(input[0], 10);
  
  if (a<1200) {
    console.log("ABC");
  }else if (a<2800) {
    console.log("ARC");
  }else{
    console.log("AGC");
  }
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 7372
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
int n;
char a[10];


int main(){
	scanf("%s", a);
	bool flag = false;
	if(a[0] == a[1] && a[1] == a[2]) flag = true;
	if(a[1] == a[2] && a[2] == a[3]) flag = true;
	if(flag) printf("Yes\n");
	else printf("No\n");
	return 0;
}

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

int main() {
  int K, A, B;
  cin >> K >> A >> B;
  
  int i = 1;
  while ( 1 ) {
    if ( A <= i * K && i * K <= B ) {
      cout << "OK" << endl;
      break;
    }
    if ( i * K > B ) {
      cout << "NG" << endl;
      break;
    }
    i++;
  }
  
}


Measured memory_bytes: 3624
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <climits>
#include <iomanip>
#include <string.h>
#include <algorithm>
#include <functional>

using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define clr(a,b) memset((a),b,sizeof(a))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define sz(a) int((a).size())
#define pb push_back

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;

template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); }
template<typename T> T pow(T a,T b, ll m){T ans=1; while(b>0){ if(b%2==1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans%m; }

const int N = (int)2e5+77;

ll n,m,k,x,y;
ll a[N];

int main() {
    fast;
    cin>>n;
    forn(i,n) cin>>a[i],k+=a[i];
    m = (ll)1e17;
    forn(i,n-1){
        x += a[i];
        k -= a[i];
        m = min(m,abs(x - k));
    }
    cout<<m<<endl;
    return 0;
}

Measured memory_bytes: 1792
#include<cstdio>
#include<cmath>

inline int gcd(int x,int y)
{
	return !y ? x : gcd(y,x%y);
}

int main()
{
	int n,x,y;
	int ans;
	scanf("%d %d",&n,&x);
	scanf("%d",&y);
	ans = fabs(x-y);
	for(int i=2;i<=n;i++)
	{
		int k;
		scanf("%d",&y);
		k = fabs(x-y);
		ans = gcd(ans,k);
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 128
#include <iostream>
using namespace std;
int main(void){
    int n, m, num, ans1 = 0, ans2 = 0;
    string s;
    int a[100001], p[100001];
    for(int i=0; i < 100001; i++) a[i] = p[i] = 0;
    
    cin >> n >> m;
    for(int i=0; i<m; i++){
        cin >> num;
        cin >> s;
        
        if(a[num] == 0){
            if(s == "AC") a[num]++;
            else p[num]++;
        }
    }
    for(int i=0; i <= n; i++){
        ans1 += a[i];
        if(a[i] != 0) ans2 += p[i];
    }
    
    cout << ans1 << " " << ans2 << endl;
}


Measured memory_bytes: 1024
#include <stdio.h>
#define N 2000001

void countingSort(int A[], int k, int n){
  int i, j;
  int B[N],C[N];

  for(i = 0 ; i <= k ; i++){
    C[i] = 0;
  }
  for(j = 1 ; j <= n ; j++){
    C[A[j]]++;
  }
  for(i = 1 ; i <= k ; i++){
    C[i] = C[i] + C[i - 1];
  }
  for(j = n ; j >= 1 ; j--){
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }

  for(i = 1 ; i <= n ; i++){
    printf("%d", B[i]);
    if(i != n){
       printf(" ");
     }
  }
  printf("\n");

}

int main(){
  int i, j, n;
  int k=0;


  scanf("%d", &n);
  int A[n];
  for(i = 1 ; i <= n ; i++){
    scanf("%d", &A[i]);
    if(k < A[i]){
      k = A[i];
    }
  }

  countingSort(A,k,n);
  return 0;
}



Measured memory_bytes: 17396
def LI(): return list(map(int, input().split()))

n = int(input())
pair = []
for _ in range(n):
    _a, _b = LI()
    pair.append((_b, _a))

pair.sort()

acc = 0
last_b = 0
for b, a in pair:
    acc += (b - last_b) - a
    last_b = b

    if acc < 0:
        print('No')
        break
else:
    print('Yes')


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

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

#define MOD 1000000007

void swap(long *xp, long *yp) {
  long temp = *xp; 
  *xp = *yp; 
  *yp = temp; 
} 
 
long modinv(long a) {
  long b = MOD, u = 1, v = 0;
  while (b) {
    long t = a / b;
    a -= t * b; swap(&a, &b);
    u -= t * v; swap(&u, &v);
  }
  u %= MOD;
  if (u < 0) u += MOD;
  return u;
}

//nCmを求める
long combination(int n, int m){
    long ans=1;

    for(int i=n;i>(n-m);i--) ans=(ans*i)%MOD;
    for(int i=2;i<=m;i++) ans=(ans*modinv(i))%MOD;

    return ans;
}

int main(void){
    int X,Y;
    scanf("%d%d",&X,&Y);

    long ans;
    if((X+Y)%3!=0) ans=0;
    else{
        int m=(2*Y-X)/3; //(i+1,j+2)の回数
        int n=(2*X-Y)/3; //(i+2,j+1)の回数

        if(m<0 || n<0) ans=0;
        else ans=combination(m+n,n);
    }

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

    return 0;
}

Measured memory_bytes: 128
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		long X = stdIn.nextLong();
		long num500 = X / 500;
		long num5 = (X % 500) / 5;
		System.out.println(num500 * 1000 + num5 * 5);
	}
}

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

#define TYPE(c)   remove_reference_t<decltype(c)>
#define REP(i, n) for(TYPE(n) i = 0; i < n; i++)
#define FOR(v, c) for(TYPE(c.begin()) v = c.begin(); v != c.end(); v++)
#define ALL(c)    c.begin(), c.end()
#define SORT(c)   sort(ALL(c))
#define RSORT(c)  sort(ALL(c), greater<TYPE(c)::value_type>())
#define UNIQUE(c) c.erase(unique(ALL(c)), c.end())

using namespace std;
using ll = long long;

constexpr int MOD  = (int)1e9 + 7;
constexpr int INF  = (int)1e9 + 1;
constexpr ll  LINF = (ll)1e18 + 1;
template<typename T> constexpr bool chmax(T& a, const T& b)
{ if(a < b) { a = b; return true; } else { return false; } }
template<typename T> constexpr bool chmin(T& a, const T& b)
{ if(b < a) { a = b; return true; } else { return false; } }
template<typename S, typename T> ostream& operator<<(ostream &os, const pair<S, T>& p)
{ return os << "(" << p.first << "," << p.second << ")"; }
template<typename T> auto operator<<(ostream& os, const T& t)
    -> typename conditional<true, ostream&, decltype(*t.begin())>::type
{ os << "["; FOR(v, t) { if(v != t.begin()) os << ","; os << *v; } os << "]"; return os; }
ostream& operator<<(ostream &os, const string& s)
{ FOR(v, s) os << *v; return os; }


/**
 *  @brief SとS[i:|S|-1](i=0,1,...,|S|-1)の
 *         最長共通接頭辞(LCP: Longest Common Prefix)を導出する
 *  @note  Z-Algorithm 計算量O(|S|)
 */
class LCPCulculator {
public:
    /**
     *  @param (S)    対象の文字列
     *  @param (dupl) falseの場合、max_lcp_length_を
     *                S[0:i-1]とS[i:|S|-1](i=1,...,|S|-1)における最長共通接頭辞の最大値とする
     */
    explicit LCPCulculator(const string& S,
                           const bool&   dupl = true) :
        dupl_(dupl), S_(S) { createTable(); }
    ~LCPCulculator() { }

    const string& getString() const { return S_; }
    const std::vector<int>& getTable() const { return table_; }
    const int& getMaxLCPLength() const { return max_lcp_length_; }

    void rerun(const string& S) { S_ = S; createTable(); }

private:
    const bool       dupl_;
    std::string      S_;
    std::vector<int> table_;           // table_[i] : SとS[i:|S|-1]の最長共通接頭辞の長さ
    int              max_lcp_length_;  // 求まる最長共通接頭辞の最大値

    void createTable() {
        table_    = std::vector<int>(S_.size(), 0);
        table_[0] = S_.size();
        max_lcp_length_  = 0;
        size_t i = 1, j = 0, k = 0;
        while(i < S_.size()) {
            while(i + j < S_.size() && S_[j] == S_[i + j]) j++;
            table_[i]       = j;
            max_lcp_length_ = max(max_lcp_length_, (int)(dupl_ ? j : min(j, i)));
            if(j == 0) { i++; continue; }
            k = 1;
            while(i + k < S_.size() && k + table_[k] < j) {
                table_[i + k] = table_[k];
                k++;
            }
            i += k; j -= k;
        }
    }
};

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

    int N;
    std::cin >> N;
    string S;
    std::cin >> S;

    int ans = 0;
    REP(i, N - 1) {
        LCPCulculator lcp(S.substr(i), false);
        chmax(ans, lcp.getMaxLCPLength());
    }

    std::cout << ans << std::endl;

    return 0;
}


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

typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef complex<double> com;

#define MOD 1000000007
#define PI 3.14159265359
#define endl '\n'
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define unique_sort(x) (sort(x.begin(), x.end()), x.resize(distance(x.begin(), unique(x.begin(), x.end()))))
#define fast_io() ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define dbg(x) cout << #x << " = " << x << '\n'
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<endl;

void test()
{
	int n,k,sum=0;
	cin>>n>>k;

	int a[n];

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

	sort(a,a+n);

	for(int i=0;i<k;i++)
	{
		sum+=a[i];
	}

	cout<<sum<<endl;
}

int main()
{   
fast_io();
cout << fixed << setprecision(15);
int t = 1;

//cin>>t;

for(int i = 0; i < t; i++)
{
	test();
}
return 0;
}

Measured memory_bytes: 3656
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;

#define REP(i,n) for(ll (i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(),v.end()
#define Decimal fixed << setprecision(20)
#define SHOWP(x) cerr<<"["<<(x).FI<<", "<<(x).SE<<"]";
#define SHOWX(x) cerr<<#x<<": "<<x<<endl;
#define SHOWVEC(v, e) REP(i, e) cerr << (v[i]) << ' '; cerr << endl;
#define SHOW2D(a, h, w) REP(i, h){REP(j, w)cerr<<setw(3)<<(a[i][j])<<' ';cerr<<endl;}
constexpr int INF = 1100000000;
constexpr long long LLINF = 1LL << 60;
constexpr long long MOD = 1000000007;

typedef long long ll;
typedef pair<ll, ll> P;

int dist[1100][1100];
int dj[] = {-1, 0, 1, 0};
int di[] = {0, -1, 0, 1};
int main()
{
  REP(i, 1100) REP(j, 1100) dist[i][j] = INF;
  int H, W;
  cin >> H >> W;
  queue<pair<int, int>> q;
  REP(i, H) {
    REP(j, W) {
      char c;
      cin >> c;
      if (c == '#') {
        dist[i][j] = 0;
        q.push(MP(i, j));
      }
    }
  }
  while (!q.empty()) {
    auto tmp = q.front(); q.pop();
    int i = tmp.FI, j = tmp.SE;
    REP(k, 4) {
      int ni = i + di[k], nj = j + dj[k];
      if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
      if (dist[i][j] + 1 < dist[ni][nj]) {
        dist[ni][nj] = dist[i][j] + 1;
        q.push(MP(ni, nj));
      }
    }
  }

  int ans = 0;
  REP(i, H) REP(j, W) ans = max(ans, dist[i][j]);
  cout << ans << endl;

  return 0;
}

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

int main(void){
    int N, K;
    string S;
    cin >> N >> K >> S;
    
    int ans = 0, left_idx = 0, right_idx = 0, count = 0;
    for (int i = 0; i < N-1; i++) {
        if (i == 0 && S[i] == '0')
            count++;
        else if (S[i] == '1' && S[i+1] == '0')
            count++;
        
        if (count > K) {
            right_idx = i;
            break;
        }
    }
    if (right_idx == 0)
        right_idx = N-1;

    ans = right_idx-left_idx+1;
    while(right_idx != N-1) {
        while(!(S[left_idx] == '0' && S[left_idx+1] == '1')) {
            left_idx++;
            //cout << S[left_idx] << " "<< S[left_idx+1] << " " << (S[left_idx] == '0' && S[left_idx+1] == '1') << endl;
        }
        left_idx++;
        
        right_idx++;
        while(!(S[right_idx] == '1' && S[right_idx+1] == '0') && right_idx != N-1) {
            right_idx++;
            if (right_idx == N-1)
                break;
        }
        ans = max(ans, right_idx-left_idx+1);
    }
    cout << ans << endl;
}

Measured memory_bytes: 512
#include <iostream>

using namespace std;

int main()
{
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    int left = a + b;
    int right = c + d;
    if (left > right)
    {
        cout << "Left" << endl;
    }
    else if (right > left)
    {
        cout << "Right" << endl;
    }
    else
    {
        cout << "Balanced" << endl;
    }
    
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using P = pair<int,int>;
  
int main()
{
  int N, K;
  cin >> N >> K;

  vector<int> A(N);
  REP(i, N) cin >> A[i];
  
  if((N-1)%(K-1)==0){
    cout << (N-1)/(K-1) << endl;
  }else{
    cout << (N-1)/(K-1)+1 << endl;
  }
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>

using namespace std;
using pii=pair<int,int>;
using ll=long long;

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


    int n;
    cin>>n;
    map<string,int> m;
    for(int i=0; i<n; i++){
        string s;
        cin>>s;
        sort(s.begin(),s.end());
        auto ret=m.insert({s,1});
        if(!ret.second) ret.first->second+=1;
    }

    ll answer=0;
    for(auto it=m.begin(); it!=m.end(); it++){
        answer+=(ll)it->second*(ll)(it->second-1)/(ll)2;
    }
    cout<<answer;

    return 0;
}


Measured memory_bytes: 11264
def main():
    N = int(input())

    ones_place = N % 10

    if ones_place in [2, 4, 7, 5, 9]:
        print('hon')
    elif ones_place in [0, 1, 6, 8]:
        print('pon')
    else:
        print('bon')

if __name__ == '__main__':
    main()

Measured memory_bytes: 80444
/**
*   @Author : MeGaCrazy
*   @InitTime : Sun Dec 30 18:14:51 2018
*   @Idea :
*
*
*
*
*
*  Happy Solving :)
**/
#include <bits/stdc++.h>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
  /// freopen("in","r",stdin);
   //freopen("out","w",stdout);
#endif
    int stx,sty,enx,eny;
    scanf("%d %d %d %d",&stx,&sty,&enx,&eny);
    for(int i=stx;i<enx;i++){
    	printf("R");
    }
    for(int i=sty;i<eny;i++){
    	printf("U");
    }
    for(int i=stx;i<enx;i++){
    	printf("L");
    }
    for(int i=sty;i<eny;i++){
    	printf("D");
    }

    printf("D");
    for(int i=stx;i<=enx;i++){
    	printf("R");
    }
    for(int i=sty;i<=eny;i++){
    	printf("U");
    }
    printf("L");

    printf("U");
    for(int i=stx;i<=enx;i++){
    	printf("L");
    }

    for(int i=sty;i<=eny;i++){
    	printf("D");
    }
    printf("R");





    



}

Measured memory_bytes: 256
#include <queue>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> Pair;

bool compare_pair_by_first(Pair a, Pair b) {
    return a.first < b.first;
}

bool operator<(Pair &a, Pair &b) {
    return a.second < b.second;
}

int main() {
    long long n,m; cin >> n >> m;
    vector<vector<ll>> vs(100001);
    for(ll i = 0; i < n; i++) {
        ll tmpa, tmpb;
        cin >> tmpa >> tmpb;
        vs[tmpa].push_back(tmpb);
    }
    // sort(pairs.begin(), pairs.end(), compare_pair_by_first);
    priority_queue<ll> pq;
    ll current = 0;
    ll ans = 0;
    for(ll i = 1; i <= m; i++) {
        for(auto x : vs[i]) {
            pq.push(x);
        }
        if(!pq.empty()) {
            ans += pq.top();
            pq.pop();
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 5632
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); i++ )
#define UREP( i, m, n ) for ( unsigned int i = (unsigned int)( m ); i < (unsigned int)( n ); i++ )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define RALL( c ) (c).rbegin(), (c).rend()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

int main()
{
	while ( true )
	{
		int w, h;
		cin >> w >> h;

		if ( !( w | h ) )
		{
			break;
		}

		VS board( h );
		for ( auto &line : board )
		{
			cin >> line;
		}

		queue< PII > que;

		REP( i, 0, h )
		{		
			if ( 1 <= que.size() )
			{
				break;
			}

			REP( j, 0, w )
			{
				if ( board[i][j] == '@' )
				{
					board[i][j] = '.';
					que.push( MP( i, j ) );
					break;
				}
			}
		}

		int res = 0;
		const int dir[][2] = {
			0, 1,
			1, 0,
			0, -1,
			-1, 0
		};

		while ( !que.empty() )
		{
			PII curr = que.front();
			que.pop();

			if ( board[ curr.fst ][ curr.snd ] == '#' )
			{
				continue;
			}

			board[ curr.fst ][ curr.snd ] = '#';
			res++;

			REP( d, 0, 4 )
			{
				PII next = curr;

				next.fst += dir[d][0];
				next.snd += dir[d][1];

				if ( 0 <= next.fst && next.fst < h && 0 <= next.snd && next.snd < w )
				{
					que.push( next );
				}
			}
		}
		
		cout << res << endl;
	}

	return 0;
}

Measured memory_bytes: 1236
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
using namespace std;
char mp[12][12];
	   
void dfs(int y,int x){
  if(y<0||x<0||y>=12||x>=12)return ;
  if(mp[y][x]=='1'){
    mp[y][x]='0';
    dfs(y-1,x);
    dfs(y+1,x);
    dfs(y,x-1);
    dfs(y,x+1);
  }else return ;
}


int main(){
  while(cin>>mp[0][0]){
    rep(i,12)rep(j,12){
      if(i==0&&j==0)continue;
      else cin>>mp[i][j];

    }
    int ans=0;
    rep(i,12)rep(j,12){
      if(mp[i][j]=='1'){
	dfs(i,j);
	ans++;
      }
    }
    cout<<ans<<endl;
  }

  return 0;
}

Measured memory_bytes: 3016
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = Integer.parseInt(scanner.next());
        Integer[] s = new Integer[N];
        int sum = 0;
        for (int i=0;i<N;i++) {
            s[i] = Integer.parseInt(scanner.next());
            sum += s[i];
        }
        scanner.close();

        if (sum % 10 != 0) {
            System.out.println(sum);
            return;
        }

        Arrays.sort(s);

        for (int num : s) {
            int tmp = sum - num;

            if (tmp % 10 != 0) {
                System.out.println(tmp);
                return;
            }
        }

        System.out.println(0);
    }
}


Measured memory_bytes: 21588
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <utility>
#include <iostream>
#include <algorithm>
using namespace std;
int a[200005],s=0;
pair<int,int> ans[100005];
struct node {
	int l,r,x;
}c[800005],d[800005];
vector<int> b[100005];
void buildc (int l,int r,int x) {
	c[x].l=l;c[x].r=r;
	if (l==r) {
		if (l%2==1) c[x].x=l; else c[x].x=-1;
		return;
	}
	int m=(l+r)>>1;
	buildc(l,m,x<<1);
	buildc(m+1,r,x<<1|1);
	int t1=c[x<<1].x,t2=c[x<<1|1].x;
	if (t1==-1) c[x].x=t2;
	else if (t2==-1) c[x].x=t1;
	else {
		if (a[t1]<a[t2]) c[x].x=t1; else c[x].x=t2;
	}
}
void buildd (int l,int r,int x) {
	d[x].l=l;d[x].r=r;
	if (l==r) {
		if (l%2==0) d[x].x=l; else d[x].x=-1;
		return;
	}
	int m=(l+r)>>1;
	buildd(l,m,x<<1);
	buildd(m+1,r,x<<1|1);
	int t1=d[x<<1].x,t2=d[x<<1|1].x;
	if (t1==-1) d[x].x=t2;
	else if (t2==-1) d[x].x=t1;
	else {
		if (a[t1]<a[t2]) d[x].x=t1; else d[x].x=t2;
	}
}
int queryc (int l,int r,int x) {
	if (c[x].l>=l && c[x].r<=r) {
		return c[x].x;
	}
	int m=(c[x].l+c[x].r)>>1;
	if (r<=m) return queryc(l,r,x<<1);
	else if (l>m) return queryc(l,r,x<<1|1);
	else {
		int t1=queryc(l,m,x<<1),t2=queryc(m+1,r,x<<1|1);
		if (t1==-1) return t2;
		else if (t2==-1) return t1;
		else {
			if (a[t1]<a[t2]) return t1; else return t2;
		}
	}
}
int queryd (int l,int r,int x) {
	if (d[x].l>=l && d[x].r<=r) {
		return d[x].x;
	}
	int m=(d[x].l+d[x].r)>>1;
	if (r<=m) return queryd(l,r,x<<1);
	else if (l>m) return queryd(l,r,x<<1|1);
	else {
		int t1=queryd(l,m,x<<1),t2=queryd(m+1,r,x<<1|1);
		if (t1==-1) return t2;
		else if (t2==-1) return t1;
		else {
			if (a[t1]<a[t2]) return t1; else return t2;
		}
	}
}
void work (int l,int r) {
	if (l>r) return;
	int x,y;
	if (l%2==1) x=queryc(l,r,1); else x=queryd(l,r,1);
	if (l%2==1) y=queryd(x+1,r,1); else y=queryc(x+1,r,1);
	s++;
	int t=s;
	ans[s].first=a[x];ans[s].second=a[y];
	if (l<=x-1) b[t].push_back(s+1);
	work(l,x-1);
	if (x+1<=y-1) b[t].push_back(s+1);
	work(x+1,y-1);
	if (y+1<=r) b[t].push_back(s+1);
	work(y+1,r);
}
priority_queue<pair<int,int> > q;
int main ( ) {
	int n,i;
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%d",&a[i]);
	buildc(1,n,1);buildd(1,n,1);
	work(1,n);
	q.push(make_pair(-ans[1].first,1));
	while (!q.empty()) {
		pair<int,int> t=q.top();
		q.pop();
		for (i=0;i<(int)b[t.second].size();i++) q.push(make_pair(-ans[b[t.second][i]].first,b[t.second][i]));
		printf("%d %d ",ans[t.second].first,ans[t.second].second);
	}
	puts("");
	return 0;
}
/*
4
3 2 4 1

3 1 2 4

2
1 2

1 2

8
4 6 3 2 8 5 7 1

3 1 2 7 4 6 8 5
*/

Measured memory_bytes: 33024
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;

    if (s[k - 1] == 'A') {
        s[k - 1] = 'a';
    }
    if (s[k - 1] == 'B') {
        s[k - 1] = 'b';
    }
    if (s[k - 1] == 'C') {
        s[k - 1] = 'c';
    }

    cout << s;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main() {
	int A[101], N, i, j, temp;
	cin >> N;
	for (i = 0; i < N; i++)cin >> A[i];
	for (int i = 0; i < N; i++) {
		temp = A[i];
		j = i - 1;
		while (j >= 0 && A[j] > temp) {
			A[j + 1] = A[j];
			j--;
		}
		A[j + 1] = temp;
		for (j = 0; j < N; j++) {
			cout << A[j];
			if (j != N - 1)cout << " ";
		}
		cout << endl;
	}
}

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

using ll = long long int;

ll x, y;

ll ans, cur;

int main(void){
    // Here your code !
    scanf("%lld %lld", &x, &y);
    cur = x;
    while (cur <= y) {
      ans++;
      cur *= 2;
    }

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

Measured memory_bytes: 256
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        var xy = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        var a = new int[7] { 1, 3, 5, 7, 8, 10, 12 };
        var b = new int[4] { 4, 6, 9, 11 };
        Console.WriteLine((a.Contains(xy[0]) && a.Contains(xy[1])) || (b.Contains(xy[0]) && b.Contains(xy[1])) ? "Yes" : "No");
    }
}


Measured memory_bytes: 13268
# import 周り
from fractions import gcd

# 定数関係
mod = 10 ** 9 + 7

# 補助関数
def inv(a):
    b, u, v = mod, 1, 0
    while b != 0:
        t = a // b
        a, b = b, a - t * b
        u, v = v, u - t * v
        if u < 0: u += mod
    return u

if __name__ == '__main__':
    #入力関係
    n = int(input())
    A = list(map(int, input().split()))

    V = 1
    U = 0
    for ele in A:
      V = V * ele // gcd(V, ele) # LCMを計算
      U += inv(ele)
    V %= mod
    print(U * V % mod)


Measured memory_bytes: 6108
#include <iostream>

using namespace std;

long long int func(int year,int month,int day){
	return (year*10000+month*100+day);
}

int main(){
	long long int data[4];
	data[0] = 1868*10000+9*100+8;
	data[1] = 1912*10000+7*100+29;
	data[2] = 1926*10000+12*100+24;
	data[3] = 1989*10000+1*100+7;

	long long int num;
	int year,month,day;
	while(cin>>year){
		cin>>month;
		cin>>day;
		num = func(year,month,day);
		if(num<data[0]){
			cout<<"pre-meiji"<<endl;
		}
		else if(data[0]<=num&&num<=data[1]){
			cout<<"meiji "<<year - 1868 + 1<<" "<<month<<" "<<day<<endl;
		}
		else if(data[1]<=num&&num<=data[2]){
			cout<<"taisho "<<year - 1912 + 1<<" "<<month<<" "<<day<<endl;
		}
		else if(data[2]<=num&&num<=data[3]){
			cout<<"showa "<<year - 1926 + 1<<" "<<month<<" "<<day<<endl;
		}
		else{
			cout<<"heisei "<<year - 1989 + 1<<" "<<month<<" "<<day<<endl;
		}
	}

	return 0;
}

/*
Sample Input
2005 9 3
1868 12 2
1868 9 7
Output for the Sample Input
heisei 17 9 3
meiji 1 12 2
pre-meiji
*/

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

using namespace std;


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string a, b;
  long long int mod = 1e9 + 7;
  while (cin >> n && n) {
    cin >> a >> b;
    long long ans = 1;
    bool ud = true;
    int start = 0;
    if (a[0] == b[0]) {
      ans = (ans * 3) % mod;
      ud = true;
      start = 1;
    }
    else {
      ans = (ans * 6) % mod;
      ud = false;
      start = 2;
    }
    for (int i = start; i < n;) {
      if (ud) {
        if (a[i] == b[i]) {
          ans = (ans * 2) % mod;
          ud = true;
          i++;
        }
        else {
          ans = (ans * 2) % mod;
          ud = false;
          i+=2;
        }
      }
      else {
        if (a[i] == b[i]) {
          ans = (ans * 1) % mod;
          ud = true;
          i++;
        }
        else {
          ans = (ans * 3) % mod;
          ud = false;
          i+=2;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include<algorithm>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000
int MOD=1000000007;
vector<int>s(100001);
int n;
bool arunashi(int b){
  int l=0;
  int r=n-1;
  while(r>=l){
    int t=(r+l)/2;
    if(b>s[t]){l=t+1;}
    else if(b==s[t]){return true;}
    else r=t-1;
  }
  return false;
}
signed main() {
cin>>n;
int ans=0;
rep(i,n){
  cin>>s[i];
}
int q;cin>>q;
vector<int>t(q);
rep(i,q){
  cin>>t[i];
}
rep(i,q){
  int p=t[i];
  if(arunashi(p))ans++;
}
cout<<ans<<endl;
}



Measured memory_bytes: 3816
#include <iostream>
#include <string>
#include <algorithm>
 
int main(void) {
  int a, b, c;
  int k;

  std::string str;
  std::cin >> str;
  a = std::stoi(str);
  std::cin >> str;
  b = std::stoi(str);
  std::cin >> str;
  c = std::stoi(str);
  std::cin >> str;
  k = std::stoi(str);

  int max = std::max({a,b,c});

  int smax = max;

  for (int i = 0; i < k; i++) {
    smax *= 2;
  }

  int total = a + b + c + smax - max;

  std::cout << total << std::endl;

  return 0;
}

Measured memory_bytes: 256
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
# for _ in range(int(input())):
a,b,c,d=map(int,input().split())
while 1:
	if c<=b:
		print('Yes')
		break
	else:
		c=c-b
		if a<=d:
			print('No')
			break
		else:
			a=a-d


Measured memory_bytes: 9172
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

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

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
    t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v){
    for (auto &e : t) fill(e, v);
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }

#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }




using Graph = vector<vector<int> >;


int N;
Graph G;
vector<int> C;

vector<int> res;
void rec(int v, int p, int &iter) {
    res[v] = C[iter];
    for (auto ch : G[v]) {
        if (ch == p) continue;
        rec(ch, v, ++iter);
    }
}

int main() {
    while (cin >> N) {
        G.assign(N, vector<int>());
        for (int i = 0; i < N-1; ++i) {
            int a, b; cin >> a >> b;
            --a, --b;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        C.resize(N);
        long long sum = 0;
        for (int i = 0; i < N; ++i) cin >> C[i], sum += C[i];
        sort(C.begin(), C.end(), greater<long long>());
        sum -= C[0];

        res.resize(N);
        int iter = 0;
        rec(0, -1, iter);
        cout << sum << endl;
        for (auto v : res) cout << v << " ";
        cout << endl;
    }
}

















Measured memory_bytes: 1408
n,k=map(int,input().split())
a=[int(input()) for i in range(n)]

class SegTree:
    # init_valはseg木にしたいlist、ide_eleは単位元
    # """
    # 単位元の説明
    # 最小値のセグ木 → 10**9　(最小値の更新に影響しないため)
    # 　　和のセグ木 → 0　(上の単位元の説明を参照)
    # 　　積のセグ木 → 1　(上の単位元の説明を参照)
    # 　　gcdのセグ木 → 0　(gcdを更新しない値は0)
    # """
    # funcは行いたい操作を代入
    # 区間の最小値を求めたいならmin
    # 区間の最大値を求めたいならmax
    # """
    def __init__(self, init_val, ide_ele, func):
        self.ide_ele=ide_ele
        self.num=2**(len(init_val)-1).bit_length()
        self.seg=[self.ide_ele]*2*self.num
        self.func=func

        #set_val
        for i in range(len(init_val)):
            self.seg[i+self.num-1]=init_val[i]
        #built
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2])

    # 書け
    def segfunc(self, x, y):
        return self.func(x,y)
    
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])

    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res

seg=SegTree([0]*(300000+10),0,max)
ans=0
for ai in a:
    ma=min(300000,ai+k)
    mi=max(0,ai-k)
    tmp=seg.query(mi,ma+1)+1
    seg.update(ai,tmp)
    ans=max(ans,tmp)
print(ans)

Measured memory_bytes: 104104
#include <iostream>

using namespace std;

int main()
{
    int d; cin >> d;
    int t; cin >> t;
    int s; cin >> s;

    if (d%t==0 && d/t<=s) {cout << "Yes" << endl;}
    else if (d/t+1<=s) {cout << "Yes" << endl;}
    else {cout << "No" << endl;}

    cin.sync();cin.get();
    return 0;
}


Measured memory_bytes: 3628
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cmath>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < 10; i++) {
    double tmp = (double)n/i;
    if (tmp == (int)tmp && tmp < 10) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

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

using namespace std;

int main() {
	int h, w;
	cin >> h >> w;
	string c [100];
	for (int i = 0; i < h; i++)
	{
		cin >> c[i];
	}
	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cout << c[i] << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 764
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
using namespace __gnu_pbds;
int main(){
	string s; cin >> s;
	ll ans = 0, w = 0;
	for (ll i = s.length() - 1; i >= 0; i--){
		if (s[i] == 'B'){
			ans += (s.length() - 1 - i - w); w++;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;
const ll INF = 1e+18;

// DIJKSTRA
// cf. arihon p.96
struct edge {int to, cost;};
vector<vector<edge>> graph1(100010), graph2(100010);
vector<ll> dist1(100010, INF), dist2(100010, INF);
typedef pair<int,int> P;
// <kyori, chouten>

void dijkstra1(int s){
    priority_queue<P, vector<P>, greater<P> > Q;
    Q.push(P(0,s));
    dist1[s] = 0;
    while(!Q.empty()){
        P p = Q.top(); Q.pop();
        //      p <kyori, chouten>
        int v = p.second;
        if(dist1[v] < p.first) continue;
        for (edge e : graph1[v]){
            if (dist1[e.to] > dist1[v] + e.cost){
                dist1[e.to] = dist1[v] + e.cost;
                Q.push(P(dist1[e.to], e.to));
            }
        }
    }
}
void dijkstra2(int s){
    priority_queue<P, vector<P>, greater<P> > Q;
    Q.push(P(0,s));
    dist2[s] = 0;
    while(!Q.empty()){
        P p = Q.top(); Q.pop();
        //      p <kyori, chouten>
        int v = p.second;
        if(dist2[v] < p.first) continue;
        for (edge e : graph2[v]){
            if (dist2[e.to] > dist2[v] + e.cost){
                dist2[e.to] = dist2[v] + e.cost;
                Q.push(P(dist2[e.to], e.to));
            }
        }
    }
}


int main() {
    int n, m, s, t;
    cin >> n >> m >> s >> t;
    s--; t--;
    REP(i,m){
        int u, v, a, b;
        cin >> u >> v >> a >> b;
        u--; v--;
        graph1[u].push_back({v,a});
        graph1[v].push_back({u,a});
        graph2[u].push_back({v,b});
        graph2[v].push_back({u,b});
    }
    dijkstra1(s); dijkstra2(t);
    ll p = INF;
    ll q = 1e+15;
    vector<ll> ans(n,0);
    REP(i,n){
        p = min(p, dist1[n-1-i]+dist2[n-1-i]);
        ans[n-1-i] = q-p;
    }
    for(auto p: ans){
        cout << p << endl;
    }
    return 0;
}


Measured memory_bytes: 16316
#include<iostream>
using namespace std;
int main() {
	int x;
	int i = 1;
	
	while (1) {
		cin >> x;
		if (x == 0) {
			break;
		}
		else {
			cout << "Case " << i << ": " << x << "\n";
		}
		i++;
	}
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
    int n,a;
    set<int>s;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a;
        s.insert(a);
    }
    
    cout<<s.size()<<endl;
}

Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int s = sc.nextInt();
		int w = sc.nextInt();
		if(s<=w) {
			System.out.println("unsafe");
		} else {
			System.out.println("safe");
		}
	}

}


Measured memory_bytes: 35816