akhauriyash/Code-Regression huggingface dataset:
//第8回シンデレラガール総選挙は是非本田未央ちゃんに投票をお願いします！
//ファンのことも他のアイドルの子たちのことも本当に大事にしてて、今まで周りに色んなものを与えてくれました。
//今度は私たちが未央ちゃんにお返しをする番です。みんなでガラスの靴を履かせてあげましょう！
// #本田未央を一番星に #本田未央を令和のシンデレラガールに #第8回シンデレラガール総選挙
#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
//問題文および制約はちゃんと確認しよう！
//サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
//とりあえずサンプルを読んでから解法を考えよう？
lint dp[310][90010][3];
int a[310];
lint extgcd(lint a, lint b, lint &x, lint &y) {
  lint g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
lint invMod(lint a, lint m) {
  lint x, y;
  if (extgcd(a, m, x, y) == 1) return (x + m) % m;return 0;
}
int main()
{
	int n,sum=0;cin>>n;rep(i,n) cin>>a[i];
	memset(dp,0,sizeof(dp));
	dp[0][0][0]=1;
	lint out=1,mo=998244353;
	rep(i,n) sum+=a[i];
	rep(i,n) rep(j,300*i+5) rep(k,3){
		dp[i][j][k]%=mo;
		dp[i+1][j+a[i]][k]+=dp[i][j][k];
		dp[i+1][j][k]+=dp[i][j][k]*k;
		dp[i+1][j][k+1]+=dp[i][j][k]*(2-k);
	}
	rep(i,n){
		out*=3;out%=mo;
	}
	REP(i,(sum+1)/2,90005) rep(j,3){
		out-=dp[n][i][j]*3;
		out%=mo;
	}
	if(sum%2==0) out+=dp[n][sum/2][1]*3*invMod(2,mo);
	cout<<(out%mo+mo)%mo<<endl;
}


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

#ifdef LOCAL
#define debug(var) cerr << #var << " = " << var << endl;
#else
#define debug(var)
#endif

void init() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
}

const int N = 1e5+234;

vector<int> g[N];
int c[2][N];
int b;

void dfs(int u, int x, int d=1) {
	c[x][u] = d;
	for (int v: g[u]) {
		if (c[x][v]) continue;
		dfs(v, x, d+1);
	}
}

void solve() {
	int n; scanf("%d", &n);
	for (int i = 0; i < n-1; ++i) {
		int a, b; scanf("%d%d", &a, &b);
		g[--a].push_back(--b);
		g[b].push_back(a);
	}
	dfs(0, 0);
	dfs(n-1, 1);
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		if (c[0][i] <= c[1][i]) ++ans;
	}
	debug(b);
	debug(ans);
	printf(ans>n-ans?"Fennec":"Snuke");
}

int main() {
	init();
	int t = 1; //scanf("%d", &t);
	while (t--) {
		solve();
	}
	return 0;
}


Measured memory_bytes: 11264
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE>
void print_vec(const vector<TYPE>& v){
	for(int i=0; i<v.size(); i++){
		cout << v[i] << " ";
	}
	cout << endl;
}

template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   " << endl;
		for(int j=0; j<v[i].size(); j++){
			cout << v[i][j] << "   ";
		}
		cout << endl;
	}
}




int main(){//
	int H, N;
	cin >> H >> N;
	vector<int> A(N), B(N);
	for(int i=0; i<N; i++) cin >> A[i] >> B[i];

	const int H_MAX = 10000;
	const int B_MAX = 10000;
	const int INF = H_MAX*B_MAX+1;
	//dp[n][h]: 0~n-1番目までの魔法を考慮して体力hを減らすのに必要な最小魔力
	vector<vector<int>> dp(N+1, vector<int>(H+H_MAX+1, INF));
	dp[0][0] = 0;//魔法使わないで体力0を減らすのに必要な魔力は0
	for(int n=0; n<=N; n++) dp[n][0] = 0;

	//解1 //dp[n][h]: 魔法0~n-1番目まで考慮した時，体力h以下の敵を倒すのに必要な最小魔力
	//解2に(★)の行を足しただけで，　解2の最後のansを探すforループがいらなくなる
	for(int h=0; h<=H; h++){
		for(int n=0; n<N; n++){
			if(h<A[n]) dp[n+1][h] = min(dp[n+1][h], dp[n][0]+B[n]);//........(★)
			dp[n+1][h] = min(dp[n+1][h], dp[n][h]+0*B[n]);//魔法n番を使わない
			if(h+A[n] < (int)dp[0].size()) dp[n+1][h+A[n]] = min(dp[n+1][h+A[n]], dp[n][h] + B[n]);//魔法nを使う(1回目)
			if(h+A[n] < (int)dp[0].size()) dp[n+1][h+A[n]] = min(dp[n+1][h+A[n]], dp[n+1][h]+B[n]);//魔法nを使う(2回目以降)(1回目の時のh+A[n]が今回のhと等しくなる時に影響)
		}
	}
	cout << dp[N][H] << endl;

	//解2
	// // for(int n=0; n<N; n++){
	// // 	for(int h=0; h<=H+H_MAX; h++){
	// for(int h=0; h<H+H_MAX; h++){
	// 	for(int n=0; n<N; n++){
	// 		dp[n+1][h] = min(dp[n+1][h], dp[n][h]+0*B[n]);//魔法n番を使わない
	// 		if(h+A[n] < (int)dp[0].size()) dp[n+1][h+A[n]] = min(dp[n+1][h+A[n]], dp[n][h] + B[n]);//魔法nを使う(1回目)
	// 		if(h+A[n] < (int)dp[0].size()) dp[n+1][h+A[n]] = min(dp[n+1][h+A[n]], dp[n+1][h]+B[n]);//魔法nを使う(2回目以降)(1回目の時のh+A[n]が今回のhと等しくなる時に影響)
	// 	}
	// }
	// //print_vec2(dp);
	// int ans = INF;
	// for(int h=H; h<=H+H_MAX; h++){//答えは体力H以上削った場合の最小消費体力
	// 	ans = min(ans, dp[N][h]);
	// }
	// cout << ans << endl;

}


Measured memory_bytes: 81664
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
namespace Mylib{
    using namespace std;
    
    
}
int main(void){
    using namespace std;
    string S,T,U;
    int A,B;
    cin >> S >> T;
    cin >> A >> B;
    cin >> U;
    
    if(U==S){
        A--;
    }else{
        B--;
    }
    
    cout << A << " " << B << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
const int inf = 1e9+7;
const int INF = 1e18+7;

int a[100000], dp[2][1<<5];
int put[] = {0, 3, 6, 12, 15};
int cnt[] = {0, 1, 1, 1, 2};
signed main(){
	int h, n;
	scanf("%lld%lld", &h, &n);
	rep(i,n){
		int x, y;
		scanf("%lld%lld", &x, &y);
		a[y] |= (1<<x);
	}
	rep(bit,(1<<4)){
		//if (bit&a[0])dp[0][bit] = -1;
		//else dp[1][bit] = 0;
		if(bit == a[0])dp[0][bit] = 0;
		else dp[0][bit] = -1;
	}
	
	rep(i,h-1){
		fill(dp[1], dp[1]+(1<<5), -1);
		rep(bit,(1<<4)){
			if(dp[0][bit] == -1)continue;
			rep(k,5){
				int check1 = a[i]&put[k];
				int check2 = a[i+1]&put[k];
				if(!check1 and !check2 and !(bit&put[k])){
					int b = a[i+1] | put[k];
					dp[1][b] = max(dp[1][b], dp[0][bit]+cnt[k]);
				}
			}
		}
		swap(dp[0], dp[1]);
	}
	
	int ans = 0;
	rep(i,(1<<4))chmax(ans, dp[0][i]);
	printf("%lld\n", ans);
	return 0;
}





Measured memory_bytes: 3300
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
 
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
 
 
class Prime():
    def __init__(self, m):
        self.M = m + 1
        self.A = a = [True] * self.M
        a[0] = a[1] = 0
        self.T = t = []
        sq = int(math.sqrt(self.M))
        q = 0
        for i in range(2, sq + 1):
            if not a[i]:
                a[i] = q
                continue
            t.append(i)
            a[i] = q
            q += 1
            for j in range(i*i,m,i):
                a[j] = False
 
        for i in range(sq + 1, self.M):
            if a[i]:
                a[i] = q
                q += 1
                t.append(i)
            else:
                a[i] = q
 
    def is_prime(self, n):
        return self.A[n]
 
 
def main():
    rr = []
 
    M = 7368792
    pr = Prime(M)
 
    def f(m, n):
        nn = n
        g = [None] * (m*m)
        for i in range(m, m*m):
            if not g[i]:
                if n == 0:
                    return i
                n -= 1
                for j in range(i, m*m, i):
                    g[j] = 1
        d = pr.A[m*m]
        return pr.T[d+n]
 
    while True:
        m,n = LI()
        if m == 0 and n == 0:
            break
        rr.append(f(m,n))
 
    return '\n'.join(map(str,rr))
 
 
 
 
print(main())


Measured memory_bytes: 102052
h, w = map(int, input().split())
a = [input() for i in range(h)]
MOD = 10**9 + 7

dp = [[0]*w for i in range(h)]
dp[0][0] = 1

for i in range(h):
    for j in range(w):
        if a[i][j] == ".":
            if i+1 < h:
                dp[i+1][j] += dp[i][j]
                dp[i+1][j] %= MOD
            if j+1 < w:
                dp[i][j+1] += dp[i][j]
                dp[i][j+1] %= MOD

print(dp[-1][-1])

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

int waru(int a){
	a=a/2;
  return a;
}

int main(){
 int N;
  cin>>N;
  
  int data[N];
  for(int i=0;i<N;i++){
    cin>>data[i];
  }
int count=0;
int handan=0;
  while(handan==0){
  for(int i=0;i<N;i++){
    if(data[i]%2==0){
     data[i]=data[i]/2; 
    }else{
      handan=1;
    }
  }
    count++;
  }
  cout<<count-1<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll k,house[200005];

ll ABS(ll x){
	if(x >= 0) return x;
	else return -x;
}

int main(){
	scanf("%lld",&k);
	scanf("%d",&n);
	for(int i = 1; i <= n; i++) scanf("%lld",&house[i]);
	ll Min = 1e13;
	Min = min(Min, k - max(k - house[n] + house[1],house[2] - house[1]));
	for(int Start = 2; Start < n; Start++){
		ll L = k - (house[Start] - house[Start - 1]);
		ll R = k - (house[Start + 1] - house[Start]);
		Min = min(Min, min(L,R));
	}
	ll L = k - (house[n] - house[n - 1]);
	ll R = k - (k - house[n] + house[1]);
	Min = min(Min, min(L, R));
	printf("%lld\n", Min);
	return 0;
}

Measured memory_bytes: 1920
import java.util.*;
 class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int index = -1;
        String[] ss = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
        for(int i = 0; i < ss.length; i++){
            if (ss[i].equals(s)){
                index = i;
                
            }
        }
        System.out.println( 7 - index);
    }
}

Measured memory_bytes: 21844
#include<iostream>
#include <list>
#include<stack>
#include<queue>
#include <vector>
#include <set>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<string>
#include <functional>
#include<fstream>

#define FOR(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define REP(i,n) FOR((i),0,(n))
#define LL long long
#define CLR(a) memset((a),0,sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str) int str;std::cin>>str;
#define DEBUGING(str) cout<<str<<endl
using namespace std;

const LL MOD = 1000000007;// 10^9+7
const int INF = (1 << 30);


//変数
int N;
vector<LL> A;






//サブ関数
//入力
void input()
{
	cin >> N;
	A.resize(N);
	REP(i, N) cin >> A[i];
}

LL eval(LL b) {
	LL res = 0;
	REP(i, N) {
		LL tmp = A[i] - (b + i + 1);
		res += abs(tmp);
	}
	return res;
}

//計算
void calc()
{
	LL l = -pow(10, 10);
	LL r = pow(10, 10);

	while (r - l > 2) {
		LL midl = (2 * l + r) / 3;
		LL midr = (l + r * 2) / 3;
		LL scorel = eval(midl);
		LL scorer = eval(midr);

		if (scorel < scorer) {
			r = midr;
		}
		else {
			l = midl;
		}
	}
	LL res = eval(l);
	res = min(res, eval(l + 1));
	res = min(res, eval(l + 2));
	cout << res << endl;
}


//出力
void output()
{

}


//デバッグ
void debug()
{
	int N;
	cin>>N;
}


//メイン関数
int main()
{
	input();
	calc();
	output();
	//debug();
	
	return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define err(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

using namespace std;

typedef long long ll;

const int mod = (int)1e9 + 7;

int dp[123][3][3];

int main() {	
	int n;
	cin >> n;
	
	string s1, s2;
	cin >> s1 >> s2;
	
	for (int c1 = 0; c1 < 3; c1++) {
		for (int c2 = 0; c2 < 3; c2++) {
			if ((s1[0] == s2[0]) ^ (c1 == c2)) continue;
			dp[0][c1][c2] = 1;
		}
	}
	
	for (int i = 0; i + 1 < n; i++) {
		for (int c1 = 0; c1 < 3; c1++) {
			for (int c2 = 0; c2 < 3; c2++) {
				if (dp[i][c1][c2] == 0) continue;
				for (int t1 = 0; t1 < 3; t1++) {
					for (int t2 = 0; t2 < 3; t2++) {
						if ((s1[i] == s1[i + 1]) ^ (c1 == t1)) continue;
						if ((s2[i] == s2[i + 1]) ^ (c2 == t2)) continue;
						if ((s1[i + 1] == s2[i + 1]) ^ (t1 == t2)) continue;
						
						dp[i + 1][t1][t2] += dp[i][c1][c2];
						if (dp[i + 1][t1][t2] >= mod) dp[i + 1][t1][t2] -= mod;
					}
				}
			
			}
		}
	}
	int ans = 0;
	for (int c1 = 0; c1 < 3; c1++) {
		for (int c2 = 0; c2 < 3; c2++) {
			ans += dp[n - 1][c1][c2];
			if (ans >= mod) ans -= mod;
		}
	}
	cout << ans << "\n";
	return 0;
}

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

using namespace std;

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

const int mod = 109;
int cnt[mod];

int main(){
  while(int n = getInt()){
    int ans = -1;
    const int n2 = n / 2;

    for(int i = 0; i < mod; i++){
      cnt[i] = 0;
    }

    for(int i = 0; i < n; i++){
      int a = getInt();
      int m = a % mod;
      if(++cnt[m] > n2){
	ans = a;
	for(++i; i < n; i++)
	  getInt();
      }
    }

    if(ans == -1)
      puts("NO COLOR");
    else
      printf("%d\n", ans);
  }
  return 0;
}

Measured memory_bytes: 840
import java.util.Scanner;

class Main {
	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		StringBuilder str = new StringBuilder();

		while (true) {
			int h = scan.nextInt();
			int w = scan.nextInt();

			if (h == 0 && w == 0)
				break;
          
			// 偶行偶列
			if (h % 2 == 0 && w % 2 == 0) {
				for (int j = 0; j < h / 2; j++) { // 一行目と二行目の繰り返し
					for (int i = 0; i < w / 2; i++) {
						str.append("#.");
					}
					str.append("\n"); // 一行目を並べる

					for (int i = 0; i < w / 2; i++) {
						str.append(".#");
					}
					str.append("\n"); // 二行目を並べる
				}
			}

			// 偶行奇列
			if (h % 2 == 0 && w % 2 != 0) {
				for (int i = 0; i < h / 2; i++) { // 一行目と二行目の繰り返し

					str.append("#"); // 一行目の一文字目
					for (int j = 0; j < (w - 1) / 2; j++) { // 一行目の二文字目以降
						str.append(".#");
					}
					str.append("\n");

					str.append("."); // 二行目の一文字目
					for (int j = 0; j < (w - 1) / 2; j++) {
						str.append("#."); // 二行目の二文字目以降
					}
					str.append("\n");
				}
			}

			// 奇行偶列
			if (h % 2 != 0 && w % 2 == 0) {
				for (int i = 0; i < w/2; i++) {
					str.append("#."); // 一行目
				}
				str.append("\n");

				for (int j = 0; j < (h - 1) / 2; j++) { // 二行目と三行目の繰り返し

					for (int i = 0; i < w/2; i++) {
						str.append(".#"); // 二行目
					}
					str.append("\n");

					for (int i = 0; i < w/2; i++) {
						str.append("#."); // 三行目
					}
					str.append("\n");

				}
			}

			// 奇行奇列
			if (h % 2 != 0 && w % 2 != 0) {
				str.append("#"); // 最初の行の最初の文字
				for (int i = 0; i < (w - 1) / 2; i++) {
					str.append(".#"); // 二文字目からの繰り返し
				}
				str.append("\n");
				for (int j = 0; j < (h - 1) / 2; j++) { // 二行目と三行目の繰り返し

					str.append("."); // 二行目の行の最初の文字
					for (int i = 0; i < (w - 1) / 2; i++) {
						str.append("#."); // 二文字目からの繰り返し　　　　　　　　　　
					}
					str.append("\n");

					str.append("#"); // 三行目の行の最初の文字
					for (int i = 0; i < (w - 1) / 2; i++) {
						str.append(".#"); // 二文字目からの繰り返し
					}
					str.append("\n");
				}
			}
			str.append("\n");
		}
		System.out.print(str);
	}

}

Measured memory_bytes: 19764
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static const int MAX_N = 1e4 + 10;

class Edge{
public:
	int t, w;
	Edge(){}
	Edge(int t, int w): t(t), w(w) {}
};

vector<Edge> G[MAX_N];
int H[MAX_N][MAX_N];
int n, h[MAX_N];

int height(int s, int idx){
	Edge e = G[s][idx];
	if (H[s][e.t]>=0) return H[s][e.t];
	H[s][e.t] = e.w;
	int u = e.t;
	for (int i = 0; i < G[u].size(); ++i)
	{
		if (G[u][i].t == s) continue;
		H[s][u] = max(H[s][u], height(u, i)+e.w);
	}
	return H[s][u];
}



int main(int argc, char const *argv[])
{
	int s, t, w;
	cin >> n;
	for (int i = 0; i < n-1; ++i)
	{
		cin >> s >> t >> w;
		G[s].push_back(Edge(t, w));
		G[t].push_back(Edge(s, w));
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < G[i].size(); ++j) H[i][G[i][j].t] = -1;
	}

    for (int i = 0; i < n; ++i)
    {
    	for (int j = 0; j < G[i].size(); ++j)
    	{
    		Edge e = G[i][j];
    		if (H[i][e.t] < 0) H[i][e.t] = height(i, j);
    	}
    }

    for (int i = 0; i < n; ++i)
    {
    	for (int j = 0; j < G[i].size(); ++j) h[i] = max(h[i], H[i][G[i][j].t]);
    }

	for (int i = 0; i < n; ++i) cout << h[i] << endl;
	return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; i++) {
    int t, s;
    cin >> t >> s;
    a.at(i).first = s;
    a.at(i).second = t;
  }

  sort(a.begin(), a.end());
  int over = 0;
  for (int i = 0; i < n; i++) {
    over += a.at(i).second;
    if (over > a.at(i).first) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes";
  return 0;
}

Measured memory_bytes: 1792
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  
  vector<int> vecA(N);
  for (int i = 0; i < N; i++) {
    cin >> vecA.at(i);
  }
  
  vector<int> vecB(N);
  for (int i = 0; i < N; i++) {
    cin >> vecB.at(i);
  }
  
  vector<int> vecC(N-1);
  for (int i = 0; i < N-1; i++) {
    cin >> vecC.at(i);
  }
  
  int nTh;
  int sum;
  for (int i = 0; i < N; i++) {
    nTh = vecA.at(i) - 1;
    sum += vecB.at(nTh);
    
    if (i== N-1) {continue;}
    if (vecA.at(i+1) - vecA.at(i) == 1) {
      sum += vecC.at(nTh);
    }    
  } 
  
  cout << sum <<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
     
int main() {
  
  int N;
  cin >> N;
  
  vector<int> L(N);
  for (int i = 0 ; i < N ; i++){
    cin >> L.at(i);
  }
  
  sort(L.begin(),L.end());
    
  int summon = 0;
  for (int i = 0 ; i < N - 1 ; i++)
    summon += L.at(i);
    
  if (summon > L.at(N - 1))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
    
}

Measured memory_bytes: 256
h, w, k = [int(i) for i in input().split()]
c = [input() for i in range(h)]

cnt = 0
for i in range(1 << h):
    bit_h = [0] * h
    for j in range(h): bit_h[j] = int(i & (1 << j) > 0)
    for j in range(1 << w):
        bit_w = [0] * w
        for l in range(w):
            bit_w[l] = int(j & (1 << l) > 0)
        cnt_t = 0
        for m in range(h):
            for n in range(w):
                if bit_h[m] == 0 and bit_w[n] == 0 and c[m][n] == "#": cnt_t += 1
        if cnt_t == k:
            cnt += 1
            #print(cnt_t)

print(cnt)

Measured memory_bytes: 9148
#include<bits/stdc++.h>
#define ll long long
#define eps 1e-8
#define INF 0X3f3f3f3f
using namespace std;
const ll maxn = 1e6 + 5;
ll n,num[maxn];
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> num[i];
    for(int i = 0; i < n; i++)
    {
        if(num[i] % 2 == 0)
        {
            if(num[i] % 3 != 0 && num[i] % 5 != 0)
            {
                cout << "DENIED" << endl;
                return 0;
            }
        }
    }
    cout << "APPROVED" << endl;
    return 0;
}


Measured memory_bytes: 256
/*
 * written by Yaqi Zhang
 * University of Wisconsin-Madison
 * 2020
 */

#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stdio.h>
#include<vector>

#define INF 0x3f3f3f3f

using namespace std;

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

int Gcd(int a, int b, int c) {
    return gcd(gcd(a, b), c);
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    int ans = 0;
    for(int a = 1; a <= n; ++a)
        for(int b = 1; b <= n; ++b)
            for(int c = 1; c <= n; ++c) 
                ans += Gcd(a, b, c);
    cout << ans << endl;
    return 0;

}



Measured memory_bytes: 3652
program redistribution
  implicit none
  integer(8), parameter :: modu = 1000000007
  integer :: s, i, j
  integer(8) :: dp(0:700, 2000) = 0
  read(*,*) s
  dp(1, 3) = 1
  do i = 4, s
    do j = 1, (i + 2) / 3
      dp(j, i) = mod(dp(j, i - 1) + dp(j - 1, i - 3), modu)
    end do
  end do
  write(*,'(i0)') mod(sum(dp(:, s)), modu)
end program redistribution

Measured memory_bytes: 13264
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int a,b,c,d;cin >> a >> b >> c >> d;
	if((c<=a && b<=d) || (a<=c && d<=b)){
		cout << min(b,d) - max(a,c) << endl;
	}
	else if(b<=c || d<=a){
		cout << 0 << endl;
	}
	else{
		if(a<=c) cout << b-c << endl;
		else cout << d-a << endl;
	}
}

Measured memory_bytes: 256
N = int(input())
men=list(map(int,input().split()))
for i in range(1,101):
  suu=0
  if i==1:
    for n in range(N):
      suu+=(1-men[n])**2
      ans=suu
  else:
    for h in range(N): 
      suu+=(i-men[h])**2
  if suu<ans:
      ans=suu
print(ans)

Measured memory_bytes: 9208
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int cnt[10008];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<pair<pair<int,int>,int> > a;
        int x,y;
        for(int i=0; i<n; i++)
        {
            cin >> x>> y;
            a.push_back(make_pair(make_pair(x,y),0));
        }
        for(int i=0; i<n; i++)
        {
            cin >> x>> y;
           a.push_back(make_pair(make_pair(x,y),1));
        }
        sort(a.begin(),a.end());
        int ans=0;
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<a.size();i++){
            if(a[i].second==0)
            {
                cnt[a[i].first.second]++;
            }
            else{
                for(int j=a[i].first.second-1;j>=0;j--){
                    if(cnt[j]>0){
                        cnt[j]--;
                        ans++;
                        break;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}


Measured memory_bytes: 256
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 100005
#define maxk 4005

int n,k;
int sum[maxk][maxk];
char s[5];

inline int read(){
	int x=0,f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*f;
}

int calc(int a,int b,int c,int d){
	return sum[c][d]-sum[a-1][d]-sum[c][b-1]+sum[a-1][b-1];
}

int main(){
	n=read(),k=read()<<1;
	for (int i=1;i<=n;i++){
		int x=read()+k,y=read()+k; scanf("%s",s+1);
		if (s[1]=='B') y+=(k>>1);
		x=(x-1)%k+1,y=(y-1)%k+1;
		++sum[x][y],++sum[x][y+k],++sum[x+k][y],++sum[x+k][y+k];
	}
	k<<=1;
	for (int i=1;i<=k;i++)
		for (int j=1;j<=k;j++)
			sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
	int ans=0;
	k>>=1;
	for (int i=1;i<=k;i++)
		for (int j=1;j<=k;j++)
			ans=max(ans,calc(i,j,i+(k>>1)-1,j+(k>>1)-1)+calc(i+(k>>1),j+(k>>1),i+k-1,j+k-1));
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 62976
# ng, ok = 0, 10**9+1

n,k = map(int, input().split())
al = list(map(int, input().split()))
ng, ok = 0, 10**9+1
while abs(ok-ng) > 1:
    mid = (ok+ng)//2
    ok_flag = True
    # ...
    cnt = 0
    for a in al:
        cnt += (a-1)//mid
    
    if cnt <= k:
        ok = mid
    else:
    	ng = mid
print(ok)



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

using namespace std;

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

typedef long long LL;
typedef pair<int, int> Pii;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;

const int maxn = 200000 + 5;

int psz;
struct Node {
  int key, siz;
  LL sum;
  Node *ch[2];
  void update() {
    siz = ch[0]->siz + ch[1]->siz + 1;
    sum = ch[0]->sum + ch[1]->sum + key;
  }
} pool[maxn], *null = new Node();

Node *new_node(int k) {
  Node *i = pool + psz++;
  i->key = i->sum = k, i->siz = 1, i->ch[0] = i->ch[1] = null;
  return i;
}

class SBT {
  Node *root;

  void rotate(Node *&u, int d) {
    Node *v = u->ch[d ^ 1];
    u->ch[d ^ 1] = v->ch[d], u->update();
    v->ch[d] = u, v->update();
    u = v;
  }

  void maintain(Node *&u, int d) {
    if (u == null) return;
    if (u->ch[d]->ch[d]->siz > u->ch[d ^ 1]->siz)
      rotate(u, d ^ 1);
    else if (u->ch[d]->ch[d ^ 1]->siz <= u->ch[d ^ 1]->siz)
      return;
    else
      rotate(u->ch[d], d), rotate(u, d ^ 1);
    maintain(u->ch[0], 0);
    maintain(u->ch[1], 1);
    maintain(u, 0);
    maintain(u, 1);
  }

  void insert(Node *&u, int k) {
    if (u == null) {
      u = new_node(k);
      return;
    }
    int d = (k > u->key);
    insert(u->ch[d], k);
    maintain(u, d), u->update();
  }

 public:
  SBT() : root(null) { null->sum = 0, null->ch[0] = null->ch[1] = null; }
  int size() { return root->siz; }
  void insert(int k) { insert(root, k); }
  int select(int k, LL &s) {
    for (Node *u = root; u != null;) {
      int t = u->ch[0]->siz;
      if (k == t) {
        s += u->ch[0]->sum;
        return u->key;
      }
      if (k > t) s += u->ch[0]->sum + u->key;
      u = u->ch[k > t], k -= (k > t ? t + 1 : 0);
    }
    return inf;
  }
};

int main() {
  int n;
  scanf("%d", &n);
  SBT bst;
  LL sum = 0;
  while (n--) {
    int op, a, b;
    scanf("%d", &op);
    if (op == 1) {
      scanf("%d%d", &a, &b);
      bst.insert(a);
      sum += a + b;
    } else {
      int i = (bst.size() - 1) / 2;
      LL s = 0;
      int x = bst.select(i, s);
      printf("%d %lld\n", x, sum - 2 * s - (LL)x * (bst.size() - 2 * i));
    }
  }

  return 0;
}

Measured memory_bytes: 6912
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i,a,b) for (int i = a; i < b; i++)
static lld r,j,a,m,b,l,x,y,n,s,q,i,z,p,k,c,d,t;string s1,s2,s3;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>m>>s1;k=0;
    rep(i,0,n)
    {
      if(s1[i]>=58||s1[i]<=47)
        k=1;
    }
        if(s1[n]!='-')
      k=1;
    for(i=n+1;i<n+m+1;i++)
      if(s1[i]>=58||s1[i]<=47)
        k=1;
    if(k==1)
      cout<<"No";
    else
      cout<<"Yes";

}

Measured memory_bytes: 256
from sys import stdin


def input():
    return stdin.readline()[:-1]


def intput():
    return int(input())


def sinput():
    return input().split()


def intsput():
    return map(int, sinput())


mod = 10 ** 9 + 7
def ncr(n, r):
    x = 1
    for i in range(n, r, -1):
        x *= i
        x %= mod
    for i in range(n - r, 1, -1):
        x *= pow(i, mod - 2, mod)
        x %= mod
    return x

x , y = intsput()

b = (2 * x - y) // 3
a = x - 2 * b

if a >= 0 and b >= 0 and a + 2 * b == x and 2 * a + b == y:
    print(ncr(a + b, b))
else:
    print(0)


Measured memory_bytes: 40880
# # ####################################################################
# import io
# import sys

# _INPUT = """\
# 1
# 10
# 3
# 10 1
# 1 2
# 3 1
# """
# sys.stdin = io.StringIO(_INPUT)
####################################################################


N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())

#「groupby」を使って、
from itertools import groupby
L = [[k, len(list(g))] for k, g in groupby(A)]
# print( L )

sumA = 0
from collections import defaultdict
d = defaultdict(int)
for k,v in L:
  d[k] += v
  sumA += k*v

# print(d)

for q in range(Q):
  b,c = map(int, input().split())

  sumA += (c-b) * d[b]
  print(sumA)

  d[c] += d[b]
  d[b] = 0






Measured memory_bytes: 31652
#include <bits/stdc++.h>
using namespace std;
int main() {
    string S;
    cin >> S;
    string ans = "No";
    char a = S.at(0), b = S.at(1), c = S.at(2), d = S.at(3);
    if(a==b&&b!=c&&c==d) ans = "Yes";
    if(a!=b&&b==c&&a==d) ans = "Yes";
    if(a==c&&d==b&&a!=b) ans = "Yes";

    cout << ans << endl;
}

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

#define ll                                       long long
#define eb                                     emplace_back
#define pii                                     pair<int,int>
#define pll                                      pair<ll,ll>
#define mp                                     map<int,int>
#define _fastio                              ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define DB(x)                                cerr << __LINE__ << ": " << #x << " = " << (x) << endl
#define Pi                                      acos(-1.0)
#define open                                  freopen("input.txt","r",stdin)         //opening input file
#define close                                  freopen ("output.txt","w",stdout)     //opening output file
#define fori(i,n)                              for(int i=1;i<=n;++i)
#define forn(i,n)                             for(int i=0;i<n;++i)
#define sz(x)                                  int(x.size())
#define error(x)                              cout<<#x<<" "<<x<<endl
#define sorted(v)                            sort(v.begin(),v.end())
int main()
{
    _fastio;
    //open;
    //close;
    string a,b;
    cin>>a>>b;
    int cnt=0;
    for(int i=0; i<3; i++)
    {
        if(a[i]==b[i])
            cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}

/// DO PEN AND PAPERWORK AFTER READING QUESTIONS CAREFULLY
/// Be confident to Solve if others can do.***
/// Common sense: do something instead of nothing
/// Even a single second counts...
/// be super cool, super patient and super focused



Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <string>
#include <set>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <functional>
#include <map>
#include <cstdlib>
#include <stack>
#include <chrono>
#include <random>

using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <class T>
using Vvec = vector<vector<T> >;
using Graph = Vvec<int>;

template <class T>
void debug(T& v)
{
    for(auto& x: v) cout << x << " ";
    cout << endl;
}

ll calc(Vvec<ll>& s, vector<int>& c, int D, vector<int>& ans){
    vector<ll> l(26, -1);
    ll ret = 0;
    for(int i=0; i<D; i++){
        l[ans[i]] = i;
        ret += s[i][ans[i]];
        for(int j=0; j<26; j++) ret -= (ll)c[j] * (i-l[j]);
    }
    return ret;
}

int main()
{
    chrono::system_clock::time_point start, tmp;
    start = std::chrono::system_clock::now();

    int seed = 1;
    mt19937 mt(seed);

    int D;
    cin >> D;
    vector<int> c(26);
    Vvec<ll> s(D, vector<ll>(26));
    for(auto& x: c) cin >> x;
    for(auto& v: s){
        for(auto& x: v) cin >> x;
    }

    vector<int> l(26), ans(D);
    for(int i=0; i<D; i++){
        int m = 0;
        ll t = -1e18;
        for(int j=0; j<26; j++){
            ll t1 = s[i][j] + (ll)(D-i)*c[j]*(i-l[j]+1);
            // ll t1 = s[i][j];
            if(t < t1){
                m = j;
                t = t1;
            }
        }
        ans[i] = m;
        l[m] = i;
    }

    ll S = calc(s, c, D, ans);

    tmp = std::chrono::system_clock::now();
    double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(tmp-start).count();
    uniform_int_distribution<> rnd(0, D-1);

    while(elapsed < 1900){
        int i = rnd(mt);
        int j = rnd(mt);
        if(i != j && abs(i-j) < (int)((1900-elapsed)/40) + 2){
            swap(ans[i], ans[j]);
            ll S1 = calc(s, c, D, ans);
            if(S < S1) S = S1;
            else swap(ans[i], ans[j]);
        }
        tmp = std::chrono::system_clock::now();
        elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(tmp-start).count();
    }

    for(auto& x: ans) cout << x+1 << "\n";
}

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

using namespace std;

using ll = long long;
using ld = long double;

const double PI = 3.1415926535897932384626433832795;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

	// #.
	// ↓
	// .#

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

	vector<int> black(n);
	for (int i = 0; i < n; i++) black[i] = s[i] == '#';
	for (int i = 1; i < n; i++) black[i] += black[i - 1];

	vector<int> white(n);
	for (int i = n - 1; i >= 0; i--) white[i] = s[i] == '.';
	for (int i = n - 2; i >= 0; i--) white[i] += white[i + 1];

	int ans = INT_MAX;
	for (int i = 0; i < n; i++) {
		int bChangeCnt = black[i];
		int wChangeCnt = (i == n - 1) ? 0 : white[i + 1];
		int cost = bChangeCnt + wChangeCnt;

		ans = min(ans, cost);
	}


	int allBlackCnt = 0;
	for (auto v : s) {
		allBlackCnt += v == '.';
	}

	ans = min(ans, allBlackCnt);

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


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

int main() {
  int N = 0, Al = 0, Bo = 0, x = 0;
  cin >> N;
  vector<int> A(N);
  for(int i=0; i < N; i++){
    cin >> A.at(i);
  }
  
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  
  for(int i = 0; i<N; i++){
    if(i%2 == 0){
      Al += A.at(i);
    }
    else{
      Bo += A.at(i);
    }
  }
  cout << Al - Bo << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

typedef unsigned long ul;
typedef long long int ll;
typedef pair<ll, ll> P;
#define pb push_back
#define mp make_pair
#define REP(i,n) REPN(i,0,n)
#define REPN(i,m,n) for(int i=m; i<(n); i++)
#define dump(x) cout << #x << " = " << (x) << endl

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

void search(char s[400][400], bool memo[400][400], P& pair, int m, int n, int h, int w) {
    char now = s[m][n];
    memo[m][n] = true;
    if (now == '#') {
        pair.first++;
    } else {
        pair.second++;
    }

    // dx, dy を使って上下左右の移動をひとまとめに、境界条件もまとめる
    REP(i, 4) {
        int p = m + dx[i];
        int q = n + dy[i];
        if (p >= 0 && p < h && q >= 0 && q < w) {
            if (s[p][q] != s[m][n] && memo[p][q] == false) {
                search(s, memo, pair, p, q, h, w);
            }
        }
    }
}

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

    int h, w;
    cin >> h >> w;
    char s[400][400];
    REP(i, h) {
        REP(j, w) {
            cin >> s[i][j];
        }
    }

    bool memo[400][400];
    REP(i, h) {
        REP(j, w) {
            memo[i][j] = false;
        }
    }

    ll sum = 0;
    REP(i, h) {
        REP(j, w) {
            if (memo[i][j] == false) {
                P pair = mp(0, 0);
                search(s, memo, pair, i, j, h, w);
                sum += (pair.first * pair.second);
            }
        }
    }

    cout << sum << '\n';
}


Measured memory_bytes: 18048
#include <bits/stdc++.h>
using namespace std;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define INF 100000000
typedef long long ll;
 
int main(){
    ll N,M,X,Y;
    cin>>N>>M>>X>>Y;
    VL x(N),y(M);
    ll ma=-INF,mi=INF;
    rep(i,0,N){cin>>x[i];ma=max(ma,x[i]);}
    rep(i,0,M){cin>>y[i];mi=min(mi,y[i]);}
    //cout<<ma<<" "<<mi<<" ";
    ll Z=mi;
    if(Z>ma&&X<Z&&Z<Y){cout<<"No War"<<endl;return 0;}
    cout<<"War"<<endl;
    return 0;
}

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

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

    int s,w;
    cin >> s >> w;

    if(w >= s) cout << "unsafe" << endl;
    else cout << "safe" << endl;
}

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

using namespace std;

int main()
{
	int n, a, b;
	cin >> n >> a >> b;
	int sum = 0;
	for (int i = 1; i <= n; i++)
	{
		int digitsum = 0;
		for (int j = i; j > 0; j /= 10)
			digitsum += j % 10;
		if (digitsum >= a && digitsum <= b) sum += i;
	}
	cout << sum << endl;
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>

#define rep(i, j) for(int i = 0; i < j; i++)
#define rep2(i, j) for(int i = 0; i <= j; i++)
#define all(i) i.begin(), i.end()
#define pint(i) printf("%d\n", i)

using namespace std;

vector<bool> era(1000010, true);
void makePrimeTable(int n)
{
	era[0] = false; era[1] = false;
	for (int i = 2; i <= n; i++)
	{
		if (era[i]) { for (int j = i * 2; j <= n; j += i) { era[j] = 0; } }
	}
}
int main()
{
	makePrimeTable(300000);
	int N, P; while (cin >> N >> P && N != -1)
	{
		vector<int> dat(0);
		vector<int> prm(0);
		while (1)
		{
			N++;
			if (era[N]) { prm.push_back(N);}
			if (prm.size() == P) { break; }
		}
		for (int i = 0; i < P; i++) for (int j = i; j < P; j++) { dat.push_back(prm[i] + prm[j]); }
		sort(all(dat)); 
		printf("%d\n", dat[P - 1]);
	}
	return 0;
}

Measured memory_bytes: 3316
#include <iostream>
#include <vector>

using namespace std;

int main() {
	int N, M;
	cin >> N >> M;
	vector<string> A(N), B(M);
	for(int i = 0; i < N; i++) cin >> A[i];
	for(int i = 0; i < M; i++) cin >> B[i];
	for(int dy = 0; dy <= N - M; dy++) {
		for(int dx = 0; dx <= N - M; dx++) {
			bool flag = true;
			for(int i = 0; i < M; i++) {
				for(int j = 0; j < M; j++) {
					int y = i + dy;
					int x = j + dx;
					if (A[y][x] != B[i][j]) {
						flag = false;
					}
				}
			}
			if (flag) {
				cout << "Yes" << endl;
				return 0;
			}
		}
	}
	cout << "No" << endl;
	return 0;
}


Measured memory_bytes: 256
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

#define mp make_pair
#define pb push_back
#define x first
#define y second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

vpii e[100010];
int size[100010];
bool v[100010];
int n;
ll ans;
bool ok;

void dfs(int k)
{
	size[k]=v[k]=1;
	for (vpii::iterator p=e[k].begin();p!=e[k].end();p++)
		if (!v[p->x])
		{
			dfs(p->x);size[k]+=size[p->x];
			ans=ans+ll(min(size[p->x],n-size[p->x]))*2*p->y;
			if (size[p->x]*2==n)
			{
				ok=1;ans-=p->y;
			}
			if (size[p->x]*2>n&&!ok)
			{
				ok=1;int t=p->y;
				for (vpii::iterator p2=e[p->x].begin();p2!=e[p->x].end();p2++)
					t=min(t,p2->y);
				ans-=t;
			}
		}
}

int main()
{
	#ifndef ONLINE_JUDGE
		//freopen("input.txt","r",stdin);
		//freopen("output.txt","w",stdout);
	#endif
	scanf("%d",&n);
	for (int i=1;i<n;i++)
	{
		int x,y,z;scanf("%d%d%d",&x,&y,&z);
		e[x].pb(mp(y,z));e[y].pb(mp(x,z));
	}
	ans=0;ok=0;
	dfs(1);
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 8832
#include <iostream>
#include <cstdio>
#include <vector>

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

    std::vector<int> a(n);
    for (int i = 0; i < n; ++i){
        scanf("%d", &a[i]);
    }

    bool flg = false;
    int index = 1;
    long count = 0;
    for(int i = 0; i < n; ++i){
        
        ++count;

        index = a.at(index - 1);

        if(2 == index){
            flg = true;
            break;
        }
    }

    if(flg){
        std::cout << count << std::endl;
    }else{
        std::cout << -1 << std::endl;
    }

    return 0;
}





Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pint pair<int, int>
#define pll pair<ll, ll>
#define vint vector<int >
#define vll vector<ll >
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (i = (a); i < (b); ++i)
#define REP(i,n) for (int i = 0; i < (n); ++i)
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main(){
    int n,m,q;
    cin >> n >> m >> q;
    vint a(q),b(q),c(q),d(q);
    REP(i,q){
        cin >> a[i] >> b[i] >> c[i] >> d[i]; 
    }
    vint A(10);
    int ans=0;
    FOR(A[0],1,m+1){
        FOR(A[1],A[0],m+1){
            FOR(A[2],A[1],m+1){
                FOR(A[3],A[2],m+1){
                    FOR(A[4],A[3],m+1){
                        FOR(A[5],A[4],m+1){
                            FOR(A[6],A[5],m+1){
                                FOR(A[7],A[6],m+1){
                                    FOR(A[8],A[7],m+1){
                                        FOR(A[9],A[8],m+1){
                                            int sum=0;
                                            REP(i,q){
                                                if(A[b[i]-1]-A[a[i]-1]==c[i]){
                                                    sum+=d[i];
                                                }
                                            }
                                            ans=max(ans,sum);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3608
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

int get_int(void) {
  int num;
  scanf("%d", &num);
  return num;
}

int get_strint2(char *a1, int *a2) {
  scanf("%s %d", a1, a2);
  return 0;
}

#define NAME_MAX 15
struct store {
    char name[NAME_MAX];
    int score;
    int id;
};

#define NUM_MAX 100

int asc_by_name(const void *a1, const void *a2) {
    struct store *s1 = (struct store*)a1;
    struct store *s2 = (struct store*)a2;

    int cmp = strncmp(s1->name, s2->name, NAME_MAX);
    if(cmp) return cmp;
    return s2->score - s1->score;
}

int main(void) {
    int num = get_int();
    struct store stores[NUM_MAX];
    int i;
    for(i = 0; i < num; i++) {
        get_strint2(stores[i].name, &stores[i].score);
        stores[i].id = i+1;
    }

    qsort(stores, num, sizeof(struct store), asc_by_name);

    for(i = 0; i < num; i++) {
        printf("%d\n", stores[i].id);
    }

    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll h, w;
    cin >> h >> w;
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    vector<vector<ll>> ans(h, vector<ll>(w));
    ll idx = 0, pos = 0, dir = 1;
    rep(i, h) {
        while((pos >= 0) && (pos < w)) {
            ans[i][pos] = idx + 1;
            a[idx]--;
            if (a[idx] == 0) idx++;
            pos += dir;
        }
        if (pos == -1) {
            pos = 0;
            dir = 1;
        }
        else {
            pos = w - 1;
            dir = -1;
        }
    }
    rep(i, h) {
        rep(j, w) {
            printf("%lld%s", ans[i][j], (j == (w - 1)) ? "\n" : " ");
        }
    }
    return 0;
}


Measured memory_bytes: 512
N, K = map(int, input().split())
S = input()
intervals = []

idx = 0
dirs = []
while idx < N:
    start = idx
    dirs.append([0, 1][S[idx] == "R"])
    while idx < N and S[idx] == S[start]:
        idx += 1
    intervals.append(idx - start)

assert(sum(intervals) == N)

def calc_ans(result_dirs):
    ans = N
    if result_dirs[0] == 0:
        ans -= 1
    for i in range(len(result_dirs)-1):
        now_dir, next_dir = result_dirs[i], result_dirs[i+1]
        if now_dir == 1 and next_dir == 0:
            ans -= 2
    if result_dirs[-1] == 1:
        ans -= 1
    return ans


rest = K
result_dirs = [dirs[0]]
for now_dir in dirs[1:]:
    if rest > 0 and now_dir == 1:
        result_dirs.append(0)
        rest -= 1
    else:
        result_dirs.append(now_dir)
if rest > 0 and dirs[0] == 1:
    result_dirs[0] = 0
left_ans = calc_ans(result_dirs)

rest = K
result_dirs = [dirs[0]]
for now_dir in dirs[1:]:
    if rest > 0 and now_dir == 0:
        result_dirs.append(1)
        rest -= 1
    else:
        result_dirs.append(now_dir)
if rest > 0 and dirs[0] == 0:
    result_dirs[0] = 1
right_ans = calc_ans(result_dirs)
print(max(left_ans, right_ans))


Measured memory_bytes: 52464
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	for (int a; cin >> a;) {
		cout << "3C" << setfill('0') << setw(2) << (a - 1) % 39 + 1 << endl;
	}
	return 0;
}

Measured memory_bytes: 3028
#include <cstdio>
#include <functional>
#include <algorithm>
#include <vector>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
#define INF (1ll<<60)
#define MOD 1000000007
#define BOUND 27182818284

typedef long long ll;
typedef long long int lli;

#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)

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

using namespace std;

// gcd
int gcd(int a, int b){
    if(a==0)
        return b;
    return gcd(b%a,a);
}
// gcd array
int findGCD(int arr[], int n){
    int result = arr[0];
    for (int i=1; i < n; i++)
        result = gcd(arr[i], result);

    return result;
}

// sort big to small
// sort(A,A+N, greater<int>());

// v.front() = -BOUND;
// v.back() = BOUND;

// Iterator
// v.begin
// v.end

int main() {
    unsigned int N;
    cin >> N;
    int X[101],Y[101],H[101];
    tuple<long long ,long long ,long long > g;

    rep(i,N){
        cin >> X[i] >> Y[i] >> H[i];
        if(H[i]>=1){
            g = make_tuple(X[i],Y[i],H[i]);
        }
    }

    rep(i,101){
        rep(j,101){
            long long center_hight =
                    get<2>(g)+abs(get<0>(g)-i)+abs(get<1>(g)-j);
            // center_hight = max(center_hight,0LL);
            bool flag = true;
            rep(k,N){
                long long height=
                        center_hight - abs(X[k]-i)-abs(Y[k]-j);
                height = max(height,0LL);
                if(height !=H[k]) flag = false;
            }
            if(flag){
                printf("%d %d %lld\n", i,j,center_hight);
                break;
            }
        }

    }
    return 0;
}



Measured memory_bytes: 256