akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 105;
typedef long long ll;
int n, m, w[maxn], v[maxn];
int cnt[4];
ll val[4][maxn];
int main() {
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%d %d", w + i, v + i);
		val[w[i] - w[1]][++cnt[w[i] - w[1]]] = v[i];
	}
	for (int i = 0; i < 4; i++) {
		sort(val[i] + 1, val[i] + 1 + cnt[i]);
		reverse(val[i] + 1, val[i] + 1 + cnt[i]);
		for (int j = 1; j <= cnt[i]; j++) {
			val[i][j] += val[i][j - 1];
		}
	}
	ll ans = 0;
	for (int i0 = 0; i0 <= cnt[0]; i0++) {
		for (int i1 = 0; i1 <= cnt[1]; i1++) {
			for (int i2 = 0; i2 <= cnt[2]; i2++) {
				for (int i3 = 0; i3 <= cnt[3]; i3++) {
					if (1ll * i0 * w[1] + 1ll * i1 * (w[1] + 1) + 1ll * i2 * (w[1] + 2) + 1ll * i3 * (w[1] + 3) <= m && val[0][i0] + val[1][i1] + val[2][i2] + val[3][i3] > ans) {
						ans = val[0][i0] + val[1][i1] + val[2][i2] + val[3][i3];
					}
				}
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 128
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <list>
#include <vector>
#include <map>
#include <time.h>
#include <cstdlib>

#pragma warning(disable:4996)

using namespace std;

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

//
//int main(void) {
//	string str;
//	cin >> str;
//	int arg[4];
//	REP(i, 4)arg[i] = 0;
//	for (int i = 0; i < str.length(); i++) {
//		switch (str[i]) {
//		case 'N':
//			arg[0]++;
//			break;
//		case 'S':
//			arg[1]++;
//			break;
//		case 'W':
//			arg[2]++;
//			break;
//		case 'E':
//			arg[3]++;
//			break;
//		}
//	}
//	if (arg[0] == arg[1] && arg[2] == arg[3])
//		cout << "YES" << endl;
//	else
//		cout << "NO" << endl;
//
//
//	return 0;
//
//}



int main(void) {
	string str;
	cin >> str;
	bool arg[4];
	REP(i, 4)arg[i] = false;
	for (int i = 0; i < str.length(); i++) {
		switch (str[i]) {
		case 'N':
			arg[0] = true;
			break;
		case 'S':
			arg[1] = true;
			break;
		case 'W':
			arg[2] = true;
			break;
		case 'E':
			arg[3] = true;
			break;
		}
	}
	if (arg[0] == arg[1] && arg[2] == arg[3])
		cout << "Yes" << endl;
	else
		cout << "No" << endl;


	return 0;
}


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

int main()
{
    ll s, c;
    cin >> s >> c;
    if (c == 1) cout << 0 << "\n";

    else if (s*2 >= c) {
        ll tmp = abs(c - (s*2));
        if (tmp % 2) tmp++;
        tmp /= 2;

        s -= tmp;
        cout << s << "\n";
    }
    else {
        ll tmp = c - (s*2);
        tmp /= 4;

        cout << tmp + s << "\n";
    }
}


Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline void PUT(char c)
{
  static char buf[1<<15],*ptr=buf;
  if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c;
}
static inline int IN(void)
{
  int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
static inline void OUT(int a)
{
  if(a<0)PUT('-'),a=-a;
  int d[40],i=0;do{d[i++]=a%10;}while(a/=10);
  while(i--){PUT(d[i]+48);}PUT('\n');
}
int N=0;
static inline void Add(int *fwt,int i,int v){while(i<N){fwt[i]+=v;i|=i+1;}}
static inline int Sum(int *fwt,int i){int v=0;while(i>=0){v+=fwt[i];i=(i&(i+1))-1;}return v;}
int main()
{
  N=IN();int Q=IN(),after[500001],dat[500000],fenwick[500000]={0},id[500001],left[500001],pos[500001]={0},result[500000];
  rep(i,N){dat[i]=IN();}
  rep(i,Q){left[i+1]=IN()-1;int r=IN()-1;after[i+1]=id[r];id[r]=i+1;}
  for(int r=0;r<N;r++)
  {
    if(pos[dat[r]]){Add(fenwick,pos[dat[r]]-1,-1);}
    Add(fenwick,r,1);
    for(int i=id[r];i;i=after[i]){result[i-1]=Sum(fenwick,r)-Sum(fenwick,left[i]-1);}
    pos[dat[r]]=r+1;
  }
  rep(i,Q){OUT(result[i]);}
}

Measured memory_bytes: 17308
//  __      __       .__           .____________                   __________        __ ________   __                 
// /  \    /  \ ____ |_________  __| _\______   \__ __  ____  _____\______   \__ ___/  |\_____  \ |  | ______  ___.__.
// \   \/\/   _/ __ \|  \_  __ \/ __ | |    |  _|  |  \/ ___\/  ___/|    |  _|  |  \   __/   |   \|  |/ \__  \<   |  |
//  \        /\  ___/|  ||  | \/ /_/ | |    |   |  |  / /_/  \___ \ |    |   |  |  /|  |/    |    |    < / __ \\___  |
//   \__/\  /  \___  |__||__|  \____ | |______  |____/\___  /____  >|______  |____/ |__|\_______  |__|_ (____  / ____|
//        \/       \/               \/        \/     /_____/     \/        \/                   \/     \/    \/\/     

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ff first
#define ss second
#define PI 3.14159265358979323846
#define endl '\n'

using namespace std;

void solve()
{
    int n, k, c;
    cin >> n >> k >> c;
    string s;
    cin >> s;
    int L[k], R[k];
    int p = -1, cp = 0;
    for(int i = 0; i < n, cp < k; i++)
        if(s[i] == 'o')
        {
            if(p == -1)
            {
                L[cp] = i;
                cp++;
                p = i;
            }
            else if(i - p > c)
            {
                L[cp] = i;
                cp++;
                p = i;
            }
        }
    p = n + 1, cp = k - 1;
    for(int i = n - 1; i >= 0, cp >= 0; i--)
        if(s[i] == 'o')
        {
            if(p == n + 1)
            {
                R[cp] = i;
                cp--;
                p = i;
            }
            else if(p - i > c)
            {
                R[cp] = i;
                cp--;
                p = i;
            }
        }
    vector <int> ans;
    for(int i = 0; i < k; i++)
        if(L[i] == R[i])
            ans.push_back(L[i] + 1);
    for(auto i : ans)
        cout << i << endl;
}

int main()
{
	int q = 1;
	//scanf("%d", &q);
	for(int i = 1; i <= q; i++)
	{
		solve();
	}
	return 0;
}

Measured memory_bytes: 5856
#include <bits/stdc++.h>
#define INF (1<<30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;

int n, m;

struct edge{
	int to;
	int cost;
};

vector<vector<edge>> graph(100100);
int val[100100];
bool visited[100100];
bool flag = true;

void dfs( int k ){
	for( auto e : graph[k] ){
		if( visited[e.to] ){
			if( val[e.to] - val[k] != e.cost ) flag = false; 
		}
		else{
			visited[e.to] = true;
			val[e.to] = val[k] + e.cost;
			dfs( e.to );
		}
	}
}


int main(){
	cin >> n >> m;
	for( int i = 0; i < m; ++i ){
		int x, y, d; cin >> x >> y >> d;
		--x; --y;
		edge temp;
		temp.to = y; temp.cost = d;
		graph[x].push_back(temp);
		temp.to = x; temp.cost *= (-1);
		graph[y].push_back(temp);
	}
	for( int i = 0; i < n; ++i ){
		if( !visited[i] ){ 
			val[i] = 0;
			dfs(i);
		}
	}
	cout << ( flag ? "Yes" : "No" ) << endl;
}

Measured memory_bytes: 11136
#include<bits/stdc++.h>
using namespace std;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int s;
    cin >> s;
    vector<int> a;
    a.push_back(s);
    int store = s;
    while(1) {
        if(store%2==0) {
            a.push_back(store/2);
            store /= 2;
        } else {
            a.push_back(3*store+1);
            store = 3 * store + 1;
        }

        for(int i = 0;i < a.size()-1;i++) {
            if(a[i]==store) {
                cout << a.size() << endl;
                return 0;
            }
        }
    }
}

Measured memory_bytes: 256
import math
mod = 1000000007

n,m = map(int,input().split())
if abs(n-m) >= 2:
    num_dm = 0
elif abs(n-m) == 1:
    num_dm = 1
else:
    num_dm = 2
d_comb_num = math.factorial(n) % mod
m_comb_num = math.factorial(m) % mod
ans = num_dm * d_comb_num * m_comb_num
ans %= mod
print(ans)

Measured memory_bytes: 4092
#include<iostream>
using namespace std;
int main(){
long long int n;
long long int waru = 10;
long long int ans=0;
  cin >> n;
  if(n % 2 ==1){
  cout << "0" << endl;
    return 0;
  }
  while(1){
  if(n >= waru){
  ans += n / waru;
  waru *= 5;
  }else{
  break;
  }
  }
  cout << ans << endl;
return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>    //pow(x,y)=x^y 99999とかの場合+1するif文
#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#define rep(init, i, n) for (int i = init; i < (n); i++)
#define ll long long
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end())
//コード自動整形 Shift + Option + F @vscode
using namespace std;

/////main/////
int main()
{
    ll D, T, S;
    cin >> D >> T >> S;
    if(D<=T*S){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
}
/////main/////

/////function/////

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP0(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define RREP0(i,n) for(int i=n-1; i>=0; --i)
#define RREP1(i,n) for(int i=n; i>=1; --i)

#define SORT(c) sort((c).begin(),(c).end())
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define CLR(a) memset((a), 0 ,sizeof(a))

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int INTINF = 2147483647;
const LL LLINF = 9223372036854775807;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int MAX = 100;
int n, M[MAX][MAX];
int d[MAX], color[MAX];

struct init{
    init(){
        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
    };
} init;


void dijkstra(){
  int minv;

  REP0(i,n){
    d[i] = INTINF;
    color[i] = WHITE;
  }

  d[0] = 0;
  color[0] = GRAY;

  while (1) {
    minv = INTINF;
    int u = -1;
    // 未確定頂点のうち最も近いもの := u を選んで確定
    REP0(i,n){
      if ( minv > d[i] && color[i] != BLACK ){
        u = i;
        minv = d[i];
      }
    }
    if ( u == -1 ) break;
    color[u] = BLACK;
    // uから到達可能な未確定頂点を選んで最短距離を更新していく
    REP0(v,n){
      if ( color[v] != BLACK && M[u][v] != INTINF) {
        if ( d[v] > d[u] + M[u][v] ) {
          d[v] = d[u] + M[u][v];
          color[v] = GRAY;
        }
      }
    }
  }
}



void input() {
  std::cin >> n;
  REP0(i,n) REP0(j,n) M[i][j] = INTINF;

  int u, k, v, c;
  REP0(i,n){
    std::cin >> u >> k;
    REP0(j,k){
      std::cin >> v >> c;
      M[u][v] = c;
    }
  }
}


void output(){
  REP0(i,n){
    std::cout << i << " " << ( d[i] == INTINF ? -1 : d[i] ) << '\n';
  }
}


int main(int argc, char const *argv[]) {
  input();
  dijkstra();
  output();
  return 0;
}



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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x,y) x = min(x, y)
#define CHMAX(x,y) x = max(x, y)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl
#define DOUBLE(d) cout << fixed << setprecision(15) << (d) << endl

const int MAX = 2e5+10;

ll N, C[MAX];
vi adj[MAX];

ll sz[MAX];
int parent[MAX];
vi children[MAX];
ll top[MAX];

ll ans[MAX];

void dfs(int v, int p = -1) {
    int tmp = parent[C[v]];
    parent[C[v]] = v;
    FORE (a, adj[v]) if (a != p) {
        dfs(a, v);
        sz[v] += sz[a] + 1;
        ll s = sz[a] + 1;
        FORE (c, children[v]) s -= sz[c] + 1;
        ans[C[v]] -= s * (s+1) / 2;
        children[v] = vi();
    }
    if (tmp >= 0) children[tmp].push_back(v);
    else top[C[v]] -= sz[v] + 1;
    parent[C[v]] = tmp;
}

void solve() {
    REP (i, N) {
        parent[i] = -1;
        top[i] = N;
    }

    REP (i, N) ans[i] = N * (N+1) / 2;
    dfs(0);

    REP (i, N) {
        if (top[i] == N) ans[i] = 0;
        else ans[i] -= top[i] * (top[i] + 1) / 2;
    }
}

int main() {
    cin >> N;
    REP (i, N) { cin >> C[i]; C[i]--; }
    REP (i, N-1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    solve();
    REP (i, N) cout << ans[i] << endl;
}

Measured memory_bytes: 45848
#include<bits/stdc++.h>
typedef long long int ll;
const ll INF = (1LL << 62);

using namespace std;

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

	ll arr[n+1][3];
	ll dp[n+1][3];

	for(int i = 0 ; i <= n ; i++){
		for(int j = 0 ; j < 3 ; j++){
			dp[i][j] = -INF;
		}
	}

	for(ll i = 1 ; i <= n ; i++){
		cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
	}

	dp[1][0] = arr[1][0];
	dp[1][1] = arr[1][1];
	dp[1][2] = arr[1][2];

	for(ll i = 1 ; i < n ; i++){
		for(ll j = 0 ; j < 3 ; j++){
			for (ll k = 0 ; k < 3 ; k++){
				if (j^k){
					dp[i+1][j] = max(dp[i+1][j] , arr[i+1][j] + dp[i][k]);
				}
			}
		}
	}

	ll ans = -INF;
	for(ll i = 0 ; i < 3 ; i++){
		ans = max(ans , dp[n][i]);
	}

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


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

typedef long long ll;

ll count[26];
string s;

int main(){
  cin >> s;
  ll n=s.size();
  ll ans=1+n*(n-1)/2;
  for(ll i=0;i<n;i++){
    count[s[i]-'a']++;
  }
  for(ll i=0;i<26;i++) ans-=count[i]*(count[i]-1)/2;
  cout << ans << endl;
}
    

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


int main() {
	int N,K;
	cin >> N >> K;
	vector<int>A(N);
	int maxsize = 0;
	for (int i = 0;i<N;++i) {
		cin >> A[i];
		maxsize = max(A[i],maxsize);
	}

	int l = 1;
	int r = maxsize;
	for (int n = 0; n < 100; ++n) {
		int x = (l + r) / 2;
		int count = 0;
		for (int i = 0; i < N; ++i) {
			count += A[i] / x;
			if (0 != A[i]%x) {
				count++;
			}
		}
		if (count > N + K) {
			l = x;
		}
		else {
			r = x;
		}
	}	
	cout <<r<<endl;
	return 0;
}

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

// september-9.cpp
// author: Seong Yong-ju ( @sei40kr )
// Copyright (c) 2017 Seong Yong-ju

#define DEBUG(x) cout << #x << ": " << x << endl

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

#define ALL(a) (a).begin(), (a).end()

using namespace std;

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

  int n;
  cin >> n;

  cout << (n % 10 == 9 || n / 10 == 9 ? "Yes" : "No");
}


Measured memory_bytes: 256
def bellman_ford(n, edges, s, distance=None):
    if distance != None:
        d = distance[:]
    else :
        d = [float('inf')] * n
        d[s] = 0
    
    loop_cnt = 0
    while True:
        loop_cnt += 1
        update = False
        for i in range(len(edges)):
            e = edges[i]
            if d[e[0]] != float('inf') and d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                update = True

        if not update or loop_cnt > n: break
    return d

n, m = map(int, input().split())
edges = []
for _ in range(m):
    a, b, c = map(int, input().split())
    edges.append((a-1, b-1, -c))

dist = bellman_ford(n, edges, 0)
test = bellman_ford(n, edges, 0, dist)

if dist[n-1] != test[n-1]:
    print("inf")
else :
    print(-dist[n-1])

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

using namespace std;

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

    int ans = 1, max;
    cin >> max;
    int input;
    for (int i = 1; i < n; i++)
    {
        cin >> input;
        if (input >= max)
        {
            ans++;
            max = input;
        }
    }

    cout << ans;
}


Measured memory_bytes: 256
import numpy as np
N,D=map(int,input().split())
X=[]
for i in range(N):
  X.append(list(map(int,input().split())))
cnt=0
for k in range(N-1):
  for j in range(k+1,N):
    sum=0
    ans=0
    for l in range(D):
      sum+=(X[k][l]-X[j][l])**2
    ans=np.sqrt(sum)
    if ans.is_integer():
      cnt+=1
print(cnt)

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

using namespace std;
using ll = long long;


int main()
{
    map<int,int> mp;
    for(int i = 0; i < 3; i++){
        int a; cin >> a;
        mp[a]++;
    }
    cout << (mp[5] == 2 && mp[7] == 1 ? "YES" : "NO") << endl;
}

Measured memory_bytes: 256
n=int(input())
sx,sy=0,0
f=True
t1=0
for i in range(n):
    t2,gx,gy=map(int,input().split())
    a=abs(sx-gx)+abs(sy-gy)
    t=t2-t1
    if a>t:
        f=False
    elif (a-t)%2==1:
        f=False
    sx,sy=gx,gy
    t1=t2
if f:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 46056
#include <iostream>
#include <set>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>

#define ll long long

using namespace std;

/*   Every goose should go to prison!!!!!
     ██▀▀▄  ██     ██
▄███▀██░░░▌ ██     ██
█████████████████████████████
    ▐██░░░▐ ██     ██
    ▌██░░░░▐██     ██
█████████████████████████████
   ▐░██░▐▒▒▒██▒▒▒▀▀██
   ▐░██░▐▄▒▒██▒▒▒▒▒██▄
█████████████████████████████
     ██▄▄▄▄▄██▄▄▄▄▄██▄▄▄▀▄
     ██    ▌██▌▌   ██
█████████████████████████████
     ██  ▄▄▌██▌▌   ██ */

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n, k;
    cin >> n >> k;
    vector<ll> h(n);
    for(ll i = 0; i < n; ++i) {
        cin >> h[i];
    }
    sort(h.begin(), h.end());
    reverse(h.begin(), h.end());
    ll r = 0;
    for(ll v : h) {
        if(k > 0) {
            --k;
        } else {
            r += v;
        }
    }
    cout << r;
    return 0;
}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template<class T> istream& operator >> (istream& s, vector<T>& v)
{ for (T& x: v) { s >> x; } return s;}
ll a,b;
ll calc(ll x){
    ll c = (x+1) / 2;
    ll ret;
    if((x+1) % 2 == 0) ret = 0;
    else ret = x;
    if(c % 2 == 1) return ret^1;
    else return ret;
}

int main(){
    cin >> a >> b;
    cout << (calc(a-1) ^ calc(b)) << endl;
    return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
using namespace std;
int n,tot,sum,a[2100000],f[2100000],deg[2100000],dp[2100000][2];
bool w[2100000]; char s[2100000];
int edgenum,vet[4100000],Next[4100000],Head[2100000];
void addedge(int u,int v){
    vet[++edgenum]=v; deg[v]^=1;
    Next[edgenum]=Head[u];
	Head[u]=edgenum;
}
void dfs(int u,int fa){
	if (s[u]=='W') w[u]=true;
	for (int e=Head[u];e;e=Next[e])
		if (vet[e]!=fa){
			dfs(vet[e],u);
			if (!w[vet[e]]){
				deg[u]^=1;
				deg[vet[e]]^=1;
			}
			w[u]|=w[vet[e]];
		}
	if (w[u]) tot++;
}
void dfs2(int u,int fa){
    f[u]=(tot-1)*2+sum;
    dp[u][0]=a[u]?-1:1;
	for (int e=Head[u];e;e=Next[e])
		if (vet[e]!=fa&&w[vet[e]]){
	        dfs2(vet[e],u);
	        f[u]=min(f[u],f[vet[e]]);
	        f[u]=min(f[u],(tot-1)*2+sum-dp[u][0]-dp[vet[e]][1]-1);
	        f[u]=min(f[u],(tot-1)*2+sum-dp[u][1]-dp[vet[e]][0]-1);
	        dp[u][0]=max(dp[u][0],dp[vet[e]][0]+1+(a[u]?-1:1));
	        dp[u][1]=max(dp[u][1],dp[vet[e]][1]+1+(a[u]?-1:1));
	    }
}
int main(){
	scanf("%d",&n); int u,v;
	for (int i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		addedge(u,v); addedge(v,u);
	}
	scanf("%s",s+1); int rt=0;
	for (int i=1;i<=n;i++)
		if (s[i]=='W') rt=i;
	if (!rt){
		puts("0");
		return 0;
	}
	dfs(rt,0);
	for (int i=1;i<=n;i++)
		if (w[i]){
			a[i]=(s[i]=='B')^deg[i];
			sum+=!a[i];//翻转后的颜色 
		}
	dfs2(rt,0);
	printf("%d\n",f[rt]);
	return 0;
}


Measured memory_bytes: 25728
import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    }
    
    private void solv() {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        if (a == b) {
            System.out.println(a);
            return;
        }
        if (a < b) {
            int temp = a;
            a = b;
            b = temp;
        }
        System.out.println(gcd(a, b));
    }
    
    private int gcd(int x, int y) {
        if (y == 0) return x;
        if (y <= 1 || x <= y) return 1;
        return gcd(y, x%y);
    }
    private void print(int[] xs) {
        StringBuilder sb = new StringBuilder();
        for (int x : xs) sb.append(x).append(' ');
        System.out.println(sb.toString().trim());
    }
}

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

int main() {
    int N;
    string S;
    cin >> N;
    cin >> S;

    int ans = 1;
    FOR(i, 1, N) {
        if (S[i - 1] != S[i]) {
            ans++;
        }
    }

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

Measured memory_bytes: 512
import java.io.*;
import java.util.NoSuchElementException;

public class Main {

    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        long a = fs.nextLong();
        long b = fs.nextLong();
        long ans = a + b;
        if( ans % 2 == 0 ){
            System.out.println( ans / 2 );
        }
        else {
            System.out.println( "IMPOSSIBLE" );
        }
    }

    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) {
                    return false;
                }
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte()) {
                return buffer[ptr++];
            } else {
                return -1;
            }
        }

        private static boolean isPrintableChar(int c) {
            return 33 <= c && c <= 126;
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
                ptr++;
            }
            return hasNextByte();
        }

        public String next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public long nextLong() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
                throw new NumberFormatException();
            }
            return (int) nl;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}


Measured memory_bytes: 22612
n = int(input())
a = list(map(int, input().split()))

ans = 0

count = 0

is_zero = False

mint = abs(a[0])

for i in range(0, n):
	if(a[i] == 0):
		is_zero = True
	if(a[i] < 0):
		a[i] = a[i] * -1
		count += 1
	ans += a[i]
	mint = min(mint, a[i])

if(count % 2 != 0 and is_zero == False):
	ans -= 2 * mint
print(ans)

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

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int k;
    cin >> k;
    int p[32]={1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};

    cout << p[k-1] <<"\n";

    return 0;
}

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

int cnt;

void insertionSort(int A[], int n, int g)
{
	for (int i = g; i < n; i++) {
		int v = A[i];
		int j = i - g;
		while (j >= 0 && A[j] > v) {
			A[j + g] = A[j];
			j -= g;
			cnt++;
		}
		A[j + g] = v;
	}
}

void shellSort(int A[], int n)
{
	cnt = 0;
	int G[100];		
	int k, m = 1;
	G[0] = 1;

	while ((k = G[m - 1] * 3 + 1) < n) {
		G[m] = k;
		m++;
	}
	for (int i = 1; i < m; i++)
		G[i] = 3 * G[i - 1] + 1;

	cout << m << "\n";
	for (int i = m - 1; i >= 0; i--) {
		cout << G[i];
		if (i != 0) cout << " ";
	}
	cout << "\n";

	for (int i = m - 1; i >= 0; i--)
		insertionSort(A, n, G[i]);
}

int main()
{
	int n;
	scanf("%d", &n);
	int *p = new int[n];

	for (int i = 0; i < n; i++) scanf("%d", &p[i]);

	shellSort(p, n);
	cout << cnt << "\n";
	for (int i = 0; i < n; i++) printf("%d\n", p[i]);
		
	delete[] p;
	return 0;
}

Measured memory_bytes: 4896
// In the name of God

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2*(int)1e5+5, mod = 998244353;
pair<int,int> pts[N];
set<int> xc, yc;
unordered_map<int, int> xm, ym;
int n, sg[4*N];

void updt(int node, int l, int r, int x) {
	if(l > x || r < x) return;
	if(l==r && l==x) {
		sg[node]++;
		return;
	}
	int mid = (l+r)/2;
	updt(2*node, l, mid, x);
	updt(2*node+1, mid+1, r, x);
	sg[node] = sg[2*node]+sg[2*node+1];
}

int quer(int node, int l, int r, int ll, int rr) {
	if(l>rr||r<ll) return 0;
	if(l>=ll&&r<=rr) {
		return sg[node];
	}
	int mid = (l+r)/2;
	int q1 = quer(2*node, l, mid, ll, rr);
	int q2 = quer(2*node+1, mid+1, r, ll, rr);
	return q1 + q2;
}

int bin_pow(int x, int y) {
	int ret = 1;
	while(y > 0) {
		if(y&1) ret = ret * x % mod;
		x = x * x % mod;
		y >>= 1;
	}
	return ret;
}

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for(int i=0; i<n; i++) {
		int x, y;
		cin >> x >> y;
		xc.insert(x);
		yc.insert(y);
		pts[i] = make_pair(x, y);
	}
	int c = 0;
	for(auto &x : xc) {
		xm[x] = c;
		c++;
	}
	c = 0;
	for(auto &y : yc) {
		ym[y] = c;
		c++;
	}
	for(int i=0; i<n; i++) {
		pts[i].first = xm[pts[i].first];
		pts[i].second = ym[pts[i].second];
	}
	sort(pts, pts+n);
	int ans = 0;
	for(int i=0; i<n; i++) {
		ans += bin_pow(2, n);
		int left = pts[i].first;
		int right = n-pts[i].first-1;
		int lower = pts[i].second;
		int upper = n-pts[i].second-1;
		int lower_left = quer(1, 0, n-1, 0, pts[i].second);
		int lower_right = lower - lower_left;
		int upper_left = left - lower_left;
		int upper_right = upper - upper_left;
		ans -= bin_pow(2, left) + bin_pow(2, right) + bin_pow(2, lower) + bin_pow(2, upper);
		ans += bin_pow(2, upper_left) + bin_pow(2, upper_right) + bin_pow(2, lower_left) + bin_pow(2, lower_right);
		ans--;
		while(ans<0)ans+=mod;
		ans %= mod;
		updt(1, 0, n-1, pts[i].second);
	}
	cout << ans;
	return 0;
}


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

using namespace std;
#define ll long long

#define rep(i, a) for (int i = 0; (i) < (int) (a); (i)++)
#define reps(i, a, b) for (int i = (int) (a); (i) < (int) (b); (i)++)
#define rrep(i, a) for (int i = (int) a-1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int) (a)-1; (i) >= (int) (b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#define TRUE__  "Yes"
#define FALSE__ "No"
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}

#ifdef LOCAL
#define lcout(a) cout << a;
#define lcoutln(a) cout << a << endl;
#define lcerr(a) cerr << a;
#define lcerrln(a) cerr << a << endl;
#else
#define lcout(a) 
#define lcoutln(a) 
#define lcerr(a) 
#define lcerrln(a) 
#endif

#define MOD 1000000007

void init();
void solve();

signed main()
{
	init();
	solve();
}

void init()
{
	int X = MOD;
	int t = 2;
	while (X % t) t++;
}

void solve()
{
	int a[3];
	rep(i, 3) cin >> a[i];
	sort(a, a+3);
	cout << a[0] + a[1] + a[2] * 10 << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 999999999;
const double pi = acos(-1);

#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define mod(i) ((i)%(ll)(1e9 + 7))
#define divm(a,b) (mod(a * modpow((ll)b,(ll)(1e9+5))))

ll modpow(ll i,ll j){ ll tmp=1; while(j){ if(j%2)tmp=mod(tmp*i);i=mod(i*i);j/=2;}return tmp;}

///////////////////////////////////////////////////////////////////////////

int main(){
	string a,b,c;
	cin >>a >> b >> c;
    char t = 'a';
    while(1){
        if(t == 'a'){
            if(a.size() == 0){
                cout << "A" << endl;
                return 0;
            }
            t = a[0];
            a = a.substr(1);
        }else if (t == 'b'){
            if(b.size() == 0){
                cout << "B" << endl;
                return 0;
            }
            t = b[0];
            b = b.substr(1);
        }else{
            if(c.size() == 0){
                cout << "C" << endl;
                return 0;
            }
            t = c[0];
            c = c.substr(1);
        }
    }
	
	return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 10
#define INF 0x3FFFFFFF
using namespace std;
int digit[MAXN+5];
int work(int x)
{
	int len=0,ret=0;
	while(x!=0)
	{
		digit[++len]=x%10;
		x/=10;
	}
	for(int i=1;i<=len;i++)
		ret+=digit[i];
	return ret;
}
int main()
{
	int N,ans=INF;
	scanf("%d",&N);
	for(int i=1;i<N;i++)
		ans=min(work(i)+work(N-i),ans);
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
inline void ci(void){
  return;
}
template <typename First, typename... Rest>
void ci(First& first, Rest&... rest){
  cin >> first;
  ci(rest...);
  return;
}
inline void co(void){
  cout << "\n";
  return;
}
template <typename First, typename... Rest>
void co(First first, Rest... rest){
  cout << first << " ";
  co(rest...);
  return;
}
#define ll long long
#define ld long double
#define st string
#define dl double
#define wh while
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define tp tuple
#define ft first
#define sc second
#define ve vector
#define tos to_string
#define len length
#define rt return 0
#define br break
#define con continue
#define lcm(n, m) (n * m / gcd(n, m))
#define np next_permutation
#define repi(i, m, n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i, n) repi(i, 0, n)
#define repill(i, m, n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define repll(i, n) repill(i, 0, n)
#define revi(i, m, n) for(int i=(int)(m);i>(int)(n);i--)
#define rev(i, n) revi(i, 0, n)
#define revill(i, m, n) for(ll i=(ll)(m);i>(ll)(n);i--)
#define revll(i, n) revill(i, 0, n)
#define sz(v) ((int)(v).size()) 
#define each(i, n) for(auto&& i: n)
#define all(n) (n.begin(), n.end())
#define sp(n) cout << fixed << setprecision(n)
// sqrt ルート, int 2*10^9, gcd　最大公約数
// __builtin_popcount bitの個数

int main() {
  int A, B, C, K;
  ci(A, B, C, K);
  if(max(A, max(B, C)) == A){
    rep(i, K){
      A *= 2;
    }
    co(A + B + C);
  }
  else if(max(A, max(B, C)) == B){
    rep(i, K){
      B *= 2;
    }
    co(A + B + C);
  }
  else{
    rep(i, K){
      C *= 2;
    }
    co(A + B + C);
  }
}


Measured memory_bytes: 256
#include<iostream>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int w,h,s,sum;
struct P{
	int y,x;
	inline P(){}
	inline P(int y,int x):y(y),x(x){}
};
P pmax(P a,P b){
	if(a.y!=b.y){
		return a.y>b.y?a:b;
	}else{
		return a.x>b.x?a:b;
	}
}
int u[32][32];
P dp[32][32][32][32];
P ret(P a,P b,int cnt){
	if(dp[a.y][a.x][b.y][b.x].y!=-1)
		return dp[a.y][a.x][b.y][b.x];
	P ans(1,cnt);
	int s1=0,s2=0;
	for(int i=a.y;i<b.y;i++){
		for(int j=a.x;j<=b.x;j++){
			s1+=u[i][j];
		}
		if(sum-(cnt-s1)>s)
			break;
		
		if(sum-s1>s)
			continue;
		P x=ret(a,P(i,b.x),s1),y=ret(P(i+1,a.x),b,cnt-s1);
		ans=pmax(ans,P(x.y+y.y,min(x.x,y.x)));
	}
	for(int i=a.x;i<b.x;i++){
		for(int j=a.y;j<=b.y;j++){
			s2+=u[j][i];
		}
		if(sum-(cnt-s2)>s)
			break;
		if(sum-s2>s)
			continue;
		
		P x=ret(a,P(b.y,i),s2),y=ret(P(a.y,i+1),b,cnt-s2);
		ans=pmax(ans,P(x.y+y.y,min(x.x,y.x)));
	}
	return dp[a.y][a.x][b.y][b.x]=ans;
}
int main(){
	while(cin>>h>>w>>s,h||w||s){
		sum=0;
		rep(i,h)rep(j,w)rep(k,h)rep(l,w)
		dp[i][j][k][l]=P(-1,0);
		rep(i,h)rep(j,w){
			cin>>u[i][j];
			sum+=u[i][j];
		}
		P ans=ret(P(0,0),P(h-1,w-1),sum);
		cout<<ans.y<<" "<<s-(sum-ans.x)<<endl;
	}
	return 0;
}

Measured memory_bytes: 9068
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits>

using namespace std;


int main(int argc, char** argv){
    long long int n;
    vector<long long int> c(5);
    cin >> n;
    for (int i=0; i<5; i++) cin >> c[i];

    long long int temp_min = c[0];
    for (int i=0; i<5; i++) temp_min = min(c[i], temp_min);
    if (temp_min < n){
        if (n%temp_min != 0) cout << n/temp_min + 5 << "\n";
        else cout << n/temp_min + 4 << "\n";
    } else {
        cout << 5 << "\n";
    }

    /*
    long long int t=0;
    vector<long long int> x(6, 0);
    x[0] = n;

    while (1){
        //if (t > 10) break;
        //for (int i=0; i<6; i++) cerr << x[i] << " "; cerr << "\n";
        t++;
        vector<long long int> old_x(6);
        for (int i=0; i<6; i++) old_x[i] = x[i];
        for (int i=0; i<5; i++){
            if (old_x[i] < c[i]){
                x[i+1] += old_x[i];
                x[i] -= old_x[i];
            } else {
                x[i+1] += c[i];
                x[i] -= c[i];
            }
        }
        if (x[5] == n) break;
    }

    cout << t << "\n";
    */

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i=1 ; i<=(n) ; ++i)
#define repr(i,n) for (int i=(n); i>=1 ; --i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

const int mod = 1000000007;
// modint: mod 計算を int を扱うように扱える構造体
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

const int MOD = 1000000007;
using mint = Fp<MOD>;

long long  modPow(long long a, long long n, long long p) {
  if (n == 1) return a % p;
  if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

long long mod_expo(long long a, long long b)
{
	long long now=a;
	long long ans=1;
	while(b)
	{
		if(b&1)
		{
			ans*=now;
		}
		b>>=1;
		now*=now;
		now%=mod;
		ans%=mod;

	}
	return ans;
}

int main(){
  int n;
  int k;
  cin >> n >> k;
  vector<ll> d(k+1);
  for(int i=1; i<=k ; i++){
    ll p = modPow((k/i),n, mod);
    //ll p = (1ll*mod_expo((k/i),n))%mod;
    d[i] = p;
  }
  for(int i=k; i>=1; i--){
    for(int j = 2*i; j<=k; j+=i){
      d[i] -= d[j];
      //if(d[i]<0) d[i] = -d[i];
      d[i] = (d[i]+mod )% mod;
    }
  }
  int ans = 0;
  for(int i=1; i<=k; i++) ans = (ans + (d[i] * i)%mod) % mod;
  //ans %= mod;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 4000
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = readline().strip()

    if N[-1] == "3":
        print("bon")
    elif N[-1] in "0168":
        print("pon")
    else:
        print("hon")


if __name__ == "__main__":
    main()


Measured memory_bytes: 9108
#include <bits/stdc++.h>
using  namespace std;
int main(){
    string s;
    cin >> s;
    int a = 0;
    int b = 0;
    for(auto c: s){
        if(c == 'A') a ++;
        else b ++;
    }
    if(a && b) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int64_t f(int64_t X, int64_t K, int64_t D, int64_t a) {
  return abs((2 * a - K) * D + X);
}

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

  int64_t X, K, D;

  cin >> X >> K >> D;

  int64_t a = K /2 -  X / (2 * D);
  // int64_t a = (K * D - X) / (2 * D);
  int64_t ap = a + 1;
  int64_t am = a - 1;
  vector<int64_t> tmp;
  if (0 <= a and a <= K)
    tmp.push_back(f(X, K, D, a));
  if (0 <= ap and ap <= K)
    tmp.push_back(f(X, K, D, ap));
  if (0 <= am and am <= K)
    tmp.push_back(f(X, K, D, am));
  if (tmp.size() <= 0) {
    int64_t tmp1 = f(X, K, D, 0);
    int64_t tmp2 = f(X, K, D, K);
    if (tmp1 > tmp2) {
      cout << tmp2;
    } else {
      cout << tmp1;
    }
  } else {
    cout << *min_element(tmp.begin(), tmp.end());
  }

  return 0;
}


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

using namespace std;

#define F first
#define S second
#define modulo ll (1e9 + 7)

typedef long long ll;
typedef pair<int,int> pii;

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

const int N = 55, M = 3e3, OO = 0x3f3f3f3f;
const ll llOO = 0x3f3f3f3f3f3f3f3f;

int h, w, n;

bool valid(int i, int j){
    return i >= 1 && i <= h && j >= 1 && j <= w && i + 2 <= h && j + 2 <= w;
}

int main(){
    init();

    cin >> h >> w >> n;
    map<pii, int> mp;
    for(int i = 0;i < n;i++){
        int a, b;   cin >> a >> b;
        for(int i = -2;i <= 0;i++){
            for(int j = -2;j <= 0;j++){
                if(valid(a + i, b + j))
                    mp[{a + i, b + j}]++;
            }
        }
    }

    ll fr[10] = {0};
    for(auto p : mp){
        fr[p.S]++;
    }

    fr[0] = (ll)(h - 2) * (w - 2) - accumulate(fr, fr + 10, 0);

    for(int i = 0;i < 10;i++)
        cout << fr[i] << '\n';
}

Measured memory_bytes: 56576
#include<cstdio> 
#include<cstring> 
#include<algorithm> 
#define rep(p,x,k) for(int p = x;p <= k;++ p) 
#define per(p,x,k) for(int p = x;p >= k;-- p) 
#define gc getchar()
#define pc putchar 
#define LL long long  
#define int long long
inline LL read() {
    LL x = 0,f = 1; 
    char c = gc; 
    while(c < '0' || c > '9') c = gc; 
    while(c <= '9' && c >= '0') x = x * 10 + c -'0',c = gc; 
    return x ; 
} 
void print(LL x) { 
    if(x < 0) { 
        pc('-'); 
        x = -x; 
    } 
    if(x >= 10) print(x / 10); 
    pc(x % 10 + '0'); 
} 
const int maxn = 5007; 
const int mod = 1e9 + 7; 
int a[maxn]; 
int n; 
struct node { 
	int v,next; 
} edge[maxn << 1]; 
int num = 0,head[maxn]; 
inline void add_edge(int u,int v) {
	edge[++ num].v = v; edge[num].next = head[u];head[u] = num; 
} 
int g[maxn]; 
int dp[maxn][maxn]; 
int siz[maxn]; 
void dfs(int x,int fa) { 
	static int t[maxn]; 
	dp[x][1] = 1; siz[x] = 1; 
	for(int i = head[x];i;i = edge[i].next) { 
		int v = edge[i].v;  
		if(v == fa) continue; 
		dfs(v,x); 	
		for(int j = 0,kel = siz[v];j <= siz[x];++ j) {
			for(int k = 0;k <= siz[v];++ k) { 
				(t[j + k] += 1ll * dp[x][j] * dp[v][k] % mod) %= mod; 
			} 
		} 
		siz[x] += siz[v]; 
		for(int j = 0;j <= siz[x];++ j) dp[x][j] = t[j],t[j] = 0; 
	} 
	LL sum = 0; 
	for(int i = 0;i <= siz[x];i += 2) sum += mod - 1ll * dp[x][i] * g[i] % mod;
	dp[x][0] = sum % mod; 
} 
 main() { 
	n = read(); 
	int u,v; 
	rep(i, 1,n - 1) { 
		u = read(),v = read(); 
		add_edge(u,v); 
		add_edge(v,u); 
	} 
	g[0] = 1; 
	for(int i = 2;i <= n;i += 2) (g[i] = 1ll * g[i - 2] * (i - 1)) %= mod;  
	dfs(1,1);  
	print((mod - dp[1][0]) % mod); 
	return 0; 
} 

Measured memory_bytes: 90348
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <functional>
// #include <atcoder/all>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

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

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

struct union_find{
    VI par, sz;

    void init(int n){
        par.resize(n);
        REP(i,n) par[i] = i;
        sz.assign(n, 1);
    }

    int find(int x){
        if (par[x] == x) return x;
        else return par[x] = find(par[x]);
    }

    bool same(int x, int y){
        return find(x) == find(y);
    }

    void merge(int x, int y){
        x = find(x);
        y = find(y);
        if (x == y) return;
        par[x] = y;
        sz[y] += sz[x];
    }

    int size(int x){
        return sz[find(x)];
    }
};


int main(void){
    int n, m;
    cin >> n >> m;
    union_find uf;
    uf.init(n);
    REP(i,m){
        int u = in() - 1, v = in() - 1;
        uf.merge(u, v);
    }
    set<int> st;
    REP(i,n) st.insert(uf.find(i));
    cout << st.size() - 1 << endl;

    return 0;
}


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

int main(){
    int x;
    cin >> x;

    int tmp = x;
    int fx = 0;

    while(tmp > 0){
        fx += tmp%10;
        tmp /= 10;
    }

    if(x%fx == 0)   cout << "Yes" << endl;
    else            cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
/*席:左奥
所要時間:40分
問題:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2104&lang=jp#
Country Road
学んだこと、感想:特殊な状況で実験することの大切さを学びました。
最初は解き方が思い浮かびませんでしたが、一度分かれば後はそこまで難しくなかったです。
vectorを使わずにsortを使ったことがないので、今度挑戦して見たいと思います。
*/
#include<bits/stdc++.h>
using namespace std;

int main(){
  int t;
  cin >> t;
  for(int i=0;i<t;i++){
    int n,k;
    int sum=0;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> b(n-1);
    for(int j=0;j<n;j++){
      cin >>  a.at(j);
    }
    for(int m=0;m<n-1;m++){
      b.at(m)=a.at(m+1)-a.at(m);
    }
    sort(b.begin(),b.end());
    int c=n-k;
      for(int l=0;l<c;l++){
	sum+=b.at(l);
      }	cout << sum << endl;
      }
}



Measured memory_bytes: 3548
#include<bits/stdc++.h>
#include <iomanip>
#include <iostream>
using namespace std;
using ll = unsigned long long;
const long long INF = 1LL << 60;
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
 
ll min(ll a, ll b){
    if(a < b) return a;
    else return b;
}
 
int main(void){
    ll N; cin >> N;
    vector<ll> A(N+1), B(N);
    for(int i = 0; i < N+1; i++) cin >> A[i];
    for(int i = 0; i < N; i++) cin >> B[i];
    ll ans = 0;
    for(int i = 0; i < N; i++){
        ans += min(A[i], B[i]);
        //cout << A[i] <<" " <<  B[i] << "ans" << ans << endl; 
        B[i] -= min(A[i], B[i]);
        ans += min(A[i+1], B[i]);
        //cout << "ans2" << ans << endl;
        A[i+1] -= min(A[i+1], B[i]);
    }
    cout << ans << endl;
}

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

int main (void) {
  int N, M;
  cin >> N >> M;
  int bigger, smaller;
  if (N < M) {bigger = M; smaller = N;}
  else {bigger = N; smaller = M;}
  
  long long i = bigger;
  while (true) {
    if (i % smaller == 0) break;
    i += bigger;
  }
  cout << i << endl;
  
  return 0;
}

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

using namespace std;

int main() {
  int N;
  string S[300];

  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> S[i];
  }
  int ret = 0;
  for(int i = 0; i < N; i++) {
    bool f = true;
    char T[300][300];
    for(int j = 0; j < N; j++) {
      for(int k = 0; k < N; k++) {
        T[j][k] = S[(i + j) % N][k];
      }
    }
    for(int j = 0; j < N; j++) {
      for(int k = 0; k < N; k++) {
        if(T[j][k] != T[k][j]) f = false;
      }
    }
    ret += f;
  }
  cout << ret * N << endl;
}

Measured memory_bytes: 512