akhauriyash/Code-Regression huggingface dataset:
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main() {
	long long h,w,ans;
    cin>>h>>w;
    if(h==1||w==1){
    	ans=1;
		}
    else if((h*w)%2==0){
    	ans=h*w/2;
    	}
    else{
    	ans=(h*w+1)/2;
    	}
    	cout<<ans<<endl;
    	}	
		


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false) cin.tie(NULL) cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define f first 
#define s second
#define int long long
using namespace std;

const int nax=1e5+5;
vector<int>adj[nax];
const int mod=1e9+7;
const int inf=1e18+7;

signed main(){
	int a,b;
	cin>>a>>b;
	if(a<=9 and a>=1 and b>=1 and b<=9){
		cout<<a*b<<endl;
	}
	else{
		cout<<-1<<endl;
	}
	return 0;
}



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

#define INF ((int)(1e9))
#define MAX 110000

int n, k, a, q, query, x, y;
string key;
unordered_map<string,int> se;

int main(){
	cin>>q;
	
	for(int i = 0; i < q; i++){
		cin>>query>>key;
		if(!query){
			cin>>x;
			se[key] = x;
		} else if(query == 1){
			cout<<se[key]<<endl;
		} 
	}
	return 0;
}


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

int main(){

    vector<string> v;
    v.push_back("Sunny");
    v.push_back("Cloudy");
    v.push_back("Rainy");

    string input;
    cin >> input;
    int index;
    if(input == "Sunny"){
        index = 0;
    }else if(input == "Sunny"){
        index = 1;
    }else if(input == "Rainy"){
        index = 2;
    }

    int next = index + 1;
    cout << v[next%3];

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x) \
	for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T>
inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return 1;
	}
	return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return 1;
	}
	return 0;
}
const long long INFLL = 1LL << 60;
const int INF         = 1 << 30;
const double PI       = acos(-1);
Vi getlist(int terget) {
	Vi res;
	vector<bool> box(terget, true);
	box[0] = false;
	box[1] = false;
	for (int i = 2; i < terget; i++) {
		if (box[i] == false) {
			continue;
		}
		

		if(i%5==1){
		res.push_back(i);
	//	clog << i << endl;
		}
		for (int j = i * 2; j < terget; j += i) {
			box[j] = false;
		}
	}
	clog << "prime" << (res.size()) << endl;
	return res;
}
Vi solve(int n) {
	Vi res(n);
	Vi per = getlist(55556);
	rep(i, n) { res[i] = per[i]; }
	return res;
}
int main() {
	int n;
	cin >> n;
	Vi ans = solve(n);
	repi(itr, ans) {
		cout << (*itr);
		if (itr != ans.end() - 1) {
			cout << " ";
		}
	}
	cout << endl;
}
/*
3 2 6 6 18 30 22
*/

Measured memory_bytes: 3596
#include <iostream>
#include <algorithm>
using namespace std;
//C - Stones
int main(void){
    int n;
    string s;
    
    std::cin >> n >>s;

    int a=0,b=0;
    for (int i = 0; i < n; i++) {
        if(s[i]=='#'){
            a++;
        }else if(s[i]=='.' && a>0){
            b++;
            a--;
        }
    }
    std::cout << b << std::endl;
}


Measured memory_bytes: 640
import sys
def routeSearch(x,y):
	a[y][x]='@'
	if(x<w-1 and a[y][x+1]=='#'):
		routeSearch(x+1,y)
	elif(y<h-1 and a[y+1][x]=='#'):
		routeSearch(x,y+1)
h,w=map(int,input().split())

a=[]
for i in range(h):
	a.append(list(input()))

routeSearch(0,0)
for i in range(h):
	if('#' in a[i]):
		print("Impossible")
		sys.exit()

print("Possible")

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

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

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll n;
ll a[101];
map<ll,ll> cnt;
vector<ll> res;

int main(){
  cin>>n;
  rep(i,n)cin>>a[i];
  if(n==1){
    cout<<a[0]<<endl;
    return 0;
  }
  rep(i,n)cnt[a[i]]++;
  if(cnt[0]==n){
    rep(i,n)cout<<0<<endl;
    return 0;
  }
  ll mn=INF;
  bool flag=false;
  if(cnt[0]%2==1){
    flag=true;
    rep(i,n)if(a[i]>=1)minch(mn,a[i]);
    cnt[mn]--;
    cnt[0]--;
  }
  for(P p : cnt){
    if(p.fi<2)continue;
    rep(i,p.se)res.push_back(p.fi);
  }
  if(res.size()>=2&&res[res.size()-2]==2&&res[res.size()-1]==3){
    swap(res[res.size()-2],res[res.size()-1]);
  }
  rep(i,cnt[0])res.push_back(0);
  if(flag){
    res.push_back(mn); res.push_back(0);
  }
  rep(i,cnt[1])res.push_back(1);
  rep(i,n)cout<<res[i]<<endl;
  return 0;
}



Measured memory_bytes: 3164
#include <bits/stdc++.h>
#define REP(i, n) for (int i=0, i##_limit=(n); i<i##_limit; ++i)
#define FOR(i, a, b) for (int i=(a), i##_limit=(b); i<i##_limit; ++i)
#define pb push_back
#define mp make_pair
using namespace std;
using ll = long long;
using pi = pair<int, int>;

int main()
{
    int N, K;
    cin >> N >> K;
    int H[N];
    REP(i, N) cin >> H[i];

    int ret[N];
    ret[0] = 0;

    FOR(i, 1, N)
    {
        ret[i] = INT_MAX;
        for (int j=1; j<=K && i-j>=0; ++j)
            ret[i] = min(ret[i], abs(H[i]-H[i-j])+ret[i-j]);
    }

    cout << ret[N-1] << endl;
    return 0;
}

Measured memory_bytes: 1024
n,m = map(int,input().split())
bridges = [list(map(int,input().split())) for _ in range(m)]
ans = [0 for _ in range(n)]

for i in range(m):
    for j in range(2):
        ans[bridges[i][j]-1] += 1

for i in range(n):
    print(ans[i])

Measured memory_bytes: 3060
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
s = input()
ans = ""
for i in range(len(s)):
    if s[i] == '0':
        ans += "0"
    elif s[i] == '1':
        ans += "1"
    else:
        ans = ans[:-1]
print(ans)


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

#define C

#ifdef C
int main(void)
{
	int f = 0, b= 0;
	string T, H;
	int rund;
	cin >> rund;
	for (size_t i = 0; i < rund; i++)
	{
		cin >> T >> H;
		if (T == H) {
			f++; b++;
		}
		else
		
			for (size_t i = 0; i <= T.size() && i <= H.size(); i++)
			{
				if (T[i] == H[i])
					continue;
				if (T[i] > H[i])
					f += 3;
				else
					b += 3;
				break;
			}
	
	}

	cout << f << " " << b << endl;
	return 0;
}
#endif

Measured memory_bytes: 3068
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
using namespace std;
int main(void){
    ll n,m;cin>>n>>m;
    ll res=0;
    if(2*n<m){
        m-=2*n;
        res+=n;
        res+=m/4;
    }else{
        res+=m/2;
    }
    cout<<res<<endl;
}

Measured memory_bytes: 3564
import math
import sys

n,k=list(map(int,input().split()))
a=list(map(int,input().split()))

s=sum(a)
s_factors=[]

l=int(math.sqrt(s))

for i in range(1,l+1):
    if s%i==0:
        s_factors.append(i)
        s_factors.append(s//i)

s_factors.sort()

for f in s_factors[::-1]:
    b=[a[i]%f for i in range(n)]
    c=[f-a[i]%f for i in range(n)]
    
    b.sort()
    c.sort()
    c=c[::-1]

    sb=[0 for i in range(n)]
    sc=[0 for i in range(n)]
    
    sb[0]=b[0]
    sc[0]=c[0]

    for i in range(1,n):
        sb[i]=sb[i-1]+b[i]

    for i in range(1,n):
        sc[i]=sc[i-1]+c[i]

    for i in range(n):
        if sb[i]==sc[n-1]-sc[i]:
            if sb[i]<=k:
                print(f)
                sys.exit()

Measured memory_bytes: 43996
# include <bits/stdc++.h>
using namespace std;
int main() {
  string A, B, C;
  cin >> A >> B >> C;
    
  if(A[A.size() - 1] == B[0] && B[B.size() - 1] == C[0]){
    cout << "YES" << endl;
  }else{
    cout << "NO" << endl;
  }
  return 0;
}

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

//typedefs
typedef long long LL;
typedef vector < int > VI;
typedef vector < LL > VL;
typedef vector < VI > VVI;
typedef vector < VL > VVL;
typedef pair < int, int > PII;
typedef pair < double, double > PDD;
typedef pair < LL, LL > PLL;
typedef vector < PII > VII;
typedef vector < PLL > VLL;

//#Defines
#define rep (i, a, b)	for (i = a; i <= b; i++)
#define repR (i, a, b)	for (i = a; i >= b; i--)
#define pb push_back
// #define pb emplace_back
#define F first
#define S second
//#define mp make_pair
#define all(c)		c.begin(), c.end()
#define rev(c)      c.rbegin(), c.rend()
#define endl '\n'
#define pf printf
#define sf scanf
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)
//if harmonic numbers need to be calculated, precalculate upto 1e6, then for any x > 1e6
//call harmonic(x + 0.5)
#define RESET(a, b)	memset(a, b, sizeof(a))
#define sqr(a) ((a) * (a))
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const LL infLL = 9000000000000000000;


/************************************* END OF TEMPLATE *************************************/

int main()
{
    #ifndef ONLINE_JUDGE
        //freopen ("in.txt", "r", stdin);
        //freopen ("out.txt", "w", stdout);
    #endif
    //optimize();

    int a, b;	cin >> a >> b;

    b *= 2;

    if (a > b) {
    	cout << a - b << endl;
    }
    else cout << "0" << endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); cerr << endl << "Time elapsed: " << (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms"; return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;

#define loop(i,a,b) for(ll i=a;i<b;i++)
#define all(v) v.begin(), v.end() 

#define print(stuff) cout << stuff << endl
#define printc(stuff) for(auto x: stuff) cout << x << " "; cout << endl;
#define len length
#define ret0 return 0
#define ret return 

#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound

#define vl vector<ll> 
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll> 
#define pq priority_queue<ll>

// typedef tree<ll,null_type,less<ll>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

#define inf (long long int) 1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN 100001

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

    ll n,x;
    cin>>n>>x;
    ll l[n];
    loop(i,0,n) cin>>l[i];
    ll d[n+1] = {0};
    loop(i,1,n+1){
        d[i]=d[i-1]+l[i-1];
    }
    ll anc = 0;
    loop(i,0,n+1){
        if(d[i] <= x) anc++;
    }
    print(anc);
        
    
}



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

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

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll
#define INF 100000000

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

signed main() {
	ll n;
  cin >> n;
  if (n >= 1000) {
    cout << "ABD" << endl;
    return 0;
  }
  cout << "ABC" << endl;
	return 0;
}

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

typedef pair<int,int> pii;

const int INF = 1e9;

vector<pii> li;
int n;

bool cmp(pii a, pii b){
	return a.second < b.second;
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x,l;
		scanf("%d%d",&x,&l);
		li.push_back(make_pair(x-l,x+l));
	}
	sort(li.begin(),li.end(),cmp);
	int ans = 0;
	int cur = -INF;
	for(int i=0;i<n;i++){
		if(li[i].first >= cur){
			ans++;
			cur = li[i].second;
		}
	}
	printf("%d\n",ans);
	return 0;
}


Measured memory_bytes: 1400
#!/usr/bin/perl

$line = 0;
while (<>) {
    chomp;
    if ($line == 0) {
        $a = $_;
    } elsif ($line == 1) {
        $b = $_;
    } elsif ($line == 2) {
        $h = $_;
    }
    $line++;
}

printf("%d\n", (($a + $b) * ($h)) / 2);


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

int main() {
	string s;
	cin >> s;
	int n = s.size();
	auto no = [](){ cout << -1 << endl; exit(0); };
	if (s.back() != '0') {
		no();
	}
	s = s.substr(0, n - 1);
	string t = s;
	reverse(t.begin(), t.end());
	if (s != t) {
		no();
	}
	if (s[0] != '1') {
		no();
	}
	vector<pair<int, int>> es;
	es.push_back({0, 1});
	int target = 1;
	int cur = 2;
	int num = 2;
	for (int i = 1; i < n; i ++) {
		if (s[i] == '1') {
			int p = i + 1;
			while (num < p) {
				es.push_back({target, cur});
				cur ++;
				num ++;
			}
			es.push_back({target, cur});
			num ++;
			target = cur;
			cur ++;
		}
	}
	for (auto it : es) {
		printf("%d %d\n", it.first + 1, it.second + 1);
	}
        return 0;
}


Measured memory_bytes: 2544
#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i,n) for(ll i = 0; i < ll(n); i++)
#define all(v) v.begin() , v.end()
#define inputv(v,n) rep(i,n){\
    ll x;\
    cin>>x;\
    v.push_back(x);\
    }
const ll INF = 999999999999999;
const ll MOD = 1000000007;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, ans = 0, w, l;
void input() {
	cin >> n;
}
void solve() {
	cout << n * n << endl;
}

int main() {
	input();
	solve();
}

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

using namespace std;

void integrate(vector<vector<int>>& v){
	const int n = v.size(), m = v[0].size();
	for(int i = 0; i < n; ++i){
		for(int j = 0; j + 1 < m; ++j){ v[i][j + 1] += v[i][j]; }
	}
	for(int i = 0; i + 1 < n; ++i){
		for(int j = 0; j < m; ++j){ v[i + 1][j] += v[i][j]; }
	}
}

int query(const vector<vector<int>>& v, int x1, int y1, int x2, int y2){
	return v[y1][x1] - v[y1][x2] - v[y2][x1] + v[y2][x2];
}

int main(){
	ios_base::sync_with_stdio(false);
	int n, m, q;
	cin >> n >> m >> q;
	vector<string> f(n);
	for(int i = 0; i < n; ++i){ cin >> f[i]; }
	vector<vector<int>> c(n + 1, vector<int>(m + 1));
	vector<vector<int>> h(n + 1, vector<int>(m));
	vector<vector<int>> v(n, vector<int>(m + 1));
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < m; ++j){ c[i + 1][j + 1] = f[i][j] - '0'; }
	}
	for(int i = 0; i < n; ++i){
		for(int j = 0; j + 1 < m; ++j){
			if(f[i][j] == '1' && f[i][j + 1] == '1'){ h[i + 1][j + 1] = 1; }
		}
	}
	for(int i = 0; i + 1 < n; ++i){
		for(int j = 0; j < m; ++j){
			if(f[i][j] == '1' && f[i + 1][j] == '1'){ v[i + 1][j + 1] = 1; }
		}
	}
	integrate(c);
	integrate(h);
	integrate(v);
	while(q--){
		int x1, y1, x2, y2;
		cin >> y1 >> x1 >> y2 >> x2;
		--x1; --y1;
		const int cs = query(c, x1, y1, x2, y2);
		const int hs = query(h, x1, y1, x2 - 1, y2);
		const int vs = query(v, x1, y1, x2, y2 - 1);
		cout << cs - hs - vs << "\n";
	}
	return 0;
}



Measured memory_bytes: 53248
import java.util.*;

public class Main {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] arr = new int[n-1][3];
        for(int i = 0; i < arr.length; i++) {
            arr[i][0] = sc.nextInt();
            arr[i][1] = sc.nextInt();
            arr[i][2] = sc.nextInt();
        }
        int time = 0;
        for(int i = 0; i < arr.length; i++) {
            time = 0;
            for(int j = i; j < arr.length; j++) {
                if(time < arr[j][1]) time = arr[j][1];
                else if(time % arr[j][2] != 0) {
                    time += arr[j][2] - time % arr[j][2];
                } 
                time += arr[j][0];
            }
            System.out.println(time);
        }
        System.out.println(0);
    }
}


Measured memory_bytes: 26684
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;

ll n,dp[22][1<<21],cp[22][22];

ll solve(ll i,ll st){
	
//	cout<<i<<" "<<st<<"\n";
	
	if (dp[i][st]!=-1) return dp[i][st];
	
	ll res=0;
	
	for (ll j=0;j<n;++j){
		if (st&(1LL<<j)) res=(res+cp[i-1][j]*solve(i-1,st^(1LL<<j)))%mod;
	}
	
	return dp[i][st]=res;
}

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.out","w",stdout);
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	cin>>n;
	
	for (ll i=0;i<n;++i){
		for (ll j=0;j<n;++j) cin>>cp[i][j];
	}
	
	memset(dp,-1,sizeof(dp));
	
	dp[0][0]=1;
	
/*	for (ll i=0;i<n;++i){
		for (ll st=0;st<(1LL<<n);++st){
			for (ll j=0;j<n;++j){
				if (st&(1LL<<j)) dp[i+1][st]=(dp[i+1][st]+cp[i][j]*dp[i][st^(1LL<<j)])%mod;
			}
			
		}
	}
	
//	for (ll i=1;i<=n;++i)
	
	cout<<dp[n][(1LL<<n)-1]<<"\n";*/
	
	cout<<solve(n,(1LL<<n)-1);
}

Measured memory_bytes: 364104
#include<bits/stdc++.h>
#define vd void
typedef long long ll;
int gi(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
char S[200010];
bool yes[26];
int len[200010];
int nxt[200010][26];
char ans[200010];
int main(){
	scanf("%s",S+1);int n=strlen(S+1);
	int nowlen=0,cnt=0;
	for(int i=n;i;--i){
		S[i]-='a';
		if(!yes[S[i]])yes[S[i]]=1,++cnt;
		if(cnt==26)++nowlen,cnt=0,memset(yes,0,sizeof yes);
		len[i]=nowlen;
	}
	for(int i=n-1;~i;--i){
		for(int j=0;j<26;++j)nxt[i][j]=nxt[i+1][j];
		nxt[i][S[i+1]]=i+1;
	}
	int m=0,p=0;
	for(int i=nowlen;i;--i){
		for(int j=0;j<26;++j)
			if(nxt[p][j]&&len[nxt[p][j]+1]<i){
				p=nxt[p][j],ans[++m]=S[p]+'a';
				break;
			}
	}
	for(int j=0;j<26;++j)
		if(!nxt[p][j]){
			ans[++m]=j+'a';
			break;
		}
	printf("%s",ans+1);
	return 0;
}


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

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()

using namespace std;
using ll = long long;

const double PI = 3.14159265358979;

void solve()
{
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for(int i = 0; i < h; ++i)
  {
    cin >> s[i];
  }

  vector< vector<int> > ans(h, vector<int>(w, 0));
  for(int j = 0; j < h; ++j)
  {
    for(int i = 0; i < w; ++i)
    {
      if (s[j][i] == '#')
      {
        ans[j][i] = -1;
        for(int y = -1; y <= 1; ++y)
        for(int x = -1; x <= 1; ++x)
        {
          if (j + y < 0 || h <= j + y) continue;
          if (i + x < 0 || w <= i + x) continue;
          if (s[j + y][i + x] == '#') continue;
          ans[j + y][i + x]++;
        }
      }
    }
  }


  for(int j = 0; j < h; ++j)
  {
    for(int i = 0; i < w; ++i)
    {
      cout << (char)(ans[j][i] == -1 ? '#' : ans[j][i] + '0');
    }
    cout << endl;
  }
}

int main()
{
  fastio;
  solve();

  return 0;
}

Measured memory_bytes: 3640
#include <stdio.h>

int main(void)
{
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);

	if ((a < b) && (b < c)) {
		printf("Yes\n");
	}
	else {
		printf("No\n");
	}
return 0;
} 


Measured memory_bytes: 2104
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define rep2(i,s,n) for (int i = s; i < n; ++i)
#define all(a) a.begin(),a.end()
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
    int n;
    string s;
    cin >> n >> s;
    int ans = 0;
    rep2(i,1,n) {
        int now = 0;
        string x = s.substr(0,i);
        string y = s.substr(i);
        set<char> cs;
        for(char c : x) cs.insert(c);
        for(char c : y) if(cs.count(c)) {
            now++;
            cs.erase(c);    
        }
        ans = max(ans,now);
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
import sys
from collections import Counter
N = int(input())
D = Counter(list(map(int, input().split())))
M = int(input())
T = list(map(int, input().split()))
for t in T:
    if D[t] <= 0:
        print('NO')
        sys.exit()
    else:
        D[t] -= 1
print('YES')


Measured memory_bytes: 50500
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define All(V) v.begin(), v.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9+7, INF = 1e9;

int main()
{
  int n; cin >> n;
  int ans = 1, mx = 0, cnt = 0;
  for(int i = 1; i <= n; i++){
    int j = i; cnt = 0;
    while(j % 2 == 0){
      cnt++;
      j /= 2;
    }
    if(mx < cnt){
      mx = cnt;
      ans = i;
    }
  }
  cout << ans << endl;
  system("pause");
}

Measured memory_bytes: 504
#include<stdio.h>
int selectionSort(int[], int);
int main(void){
    int a[100], i, n, sw;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        scanf("%d", &a[i]);
    } 
    sw = selectionSort(a, n);
    for(i=0;i<n;i++) {
        if(i){
            printf(" ");
        }
        printf("%d",a[i]);
    }
    printf("\n%d\n",sw);
    return 0;
}
int selectionSort(int a[], int N){
    int i, j, sw = 0, minj, t;
    for(i = 0; i < N-1; i++){
        minj = i;
        for(j=i;j<N;j++){
            if(a[j]<a[minj]){
                minj=j;
            }
        }
        t = a[i];
        a[i] = a[minj];
        a[minj] = t;
        if(i != minj){
            sw++;
        }
    }
    return sw;
}


Measured memory_bytes: 2048
import sys
import math

cx1,cy1,cr1 = (int(x) for x in input().split())
cx2,cy2,cr2 = (int(x) for x in input().split())


distance = math.sqrt((cx1 - cx2)**2 + (cy1 - cy2)**2)

if(distance > cr1+cr2):
    result = 4
elif(distance == cr1+cr2):
    result = 3
elif((distance<cr1 and (distance+cr2 == cr1)) or (distance<cr2 and (distance+cr1 == cr2))):
    result = 1
elif((distance<cr1 and (distance+cr2 < cr1)) or (distance<cr2 and (distance+cr1< cr2))):
    result = 0
elif(distance < cr1+cr2):
    result = 2



print(result)



Measured memory_bytes: 5672
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
using ll=long long;

int main(){
    ll n,x,y,ans,i,j;
    cin>>n>>x>>y;
    vector<ll> k(n-1,0);
    for(i=2;i<=n;i++){
        for(j=1;j<i;j++){
            k[min({i-j,abs(x-i)+abs(y-j)+1,abs(x-j)+abs(y-i)+1})-1]++;
            // cout<<i<<"_"<<j<<"_"<<min({i-j,abs(x-i)+abs(y-j)+1,abs(x-j)+abs(y-i)+1})<<endl;
        }
    }

    for(i=0;i<n-1;i++) cout<<k[i]<<endl;

    return 0;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public class Program
    {
        int N;
        List<int> listV = new List<int>();
        List<int> listC = new List<int>();
        List<int> VC = new List<int>();

        public static void Main(string[] args)
        {
            Program program = new Program();
        }

        Program()
        {
            N = Read(Console.ReadLine())[0];
            listV = Read(Console.ReadLine());
            listC = Read(Console.ReadLine());

            int ans = 0;

            for (int i = 0; i < N; i++)
            {
                int diff = listV[i] - listC[i];
                if (diff > 0) ans += diff;
            }

            Console.WriteLine(ans);
        }

        private static List<int> Read(string input)
        {
            List<string> inputs = input.Split(' ').ToList();
            return inputs.ConvertAll(x => int.Parse(x));
        }
    }
}


Measured memory_bytes: 13268
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    StringBuilder output = new StringBuilder();
    String crlf = System.getProperty("line.separator");
    int row, col, input;
    int sumRow, sumMat;
    int[] sumCol;
    
    row = in.nextInt();
    col = in.nextInt();
    sumCol = new int[col];
    sumMat = 0;
    
    for (int i = 0; i < row; i++) {
      sumRow = 0;
      for (int j = 0; j < col; j++) {
        input =in.nextInt();
        sumRow += input;
        sumCol[j] += input;
        output.append(input).append(" ");
      }
      output.append(sumRow).append(crlf);
    }
    for (int k = 0; k < col; k++) {
      output.append(sumCol[k]).append(" ");
      sumMat += sumCol[k];
    }
    output.append(sumMat);
    System.out.println(output.toString());
  }
}

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

#ifdef _DEBUG
    #include "/home/v-o_o-v/deb.h"
    #define deb(x...)cerr << "[" << #x << "] = [";_print(x);
#else
    #define deb(x...)
#endif

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n = 5;
    vector<int> v = {0, 1, 2, 3, 4};
    vector<int> a(n);
    for(int i = 0; i < 5; i++)cin >> a[i];
    int ans = INT_MAX;
    do{ 
        int time = 0;
        vector<int> b = a;
        for(int i = 0; i < n - 1; i++){
            while(b[v[i]] % 10 != 0){
                b[v[i]]++;
            }
            time += b[v[i]];
        }
        ans = min(ans, b[v[4]] + time);
    }while(next_permutation(all(v)));
    cout << ans << '\n';
    return 0;
}
// Write Here


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

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

int main() {
	
	int n, start_x;
	cin >> n >> start_x;
	vector<int> x(n+1);
	rep(i, n) cin >> x[i];
	x[n] = start_x;
	sort(all(x));
	
	int ans = x[1] - x[0];
	for(int i = 1; i < n; i++){
		ans = gcd(ans, x[i+1] - x[i]);
	}
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 640
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    total_T_in_A = [0] * (N+1)
    total_T_in_A[1] = A[0]
    total_T_in_B = [0] * M
    total_T_in_B[0] = B[0]

    for i in range(1, N+1):
        total_T_in_A[i] = total_T_in_A[i-1] + A[i-1]

    for i in range(1, M):
        total_T_in_B[i] = total_T_in_B[i-1] + B[i]

    result = 0
    for i in range(N+1):
        # A から i 冊読むときにかかる時間
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        # total_T_in_B は累積和を格納した、ソート済の配列
        # B_i <= rem_T < B_i+1 となるような B_i を二分探索によって探す
        first = total_T_in_B[0]
        last = total_T_in_B[M-1]
        if rem_T < first:
            result = max(result, i)
            continue
        if last <= rem_T:
            result = max(result, i + M)
            continue

        # assume that first <= rem_T <= last
        first_i = 0
        last_i = M - 1
        while first_i < last_i:
            if abs(last_i - first_i) == 1:
                break
            mid_i = (first_i + last_i) // 2
            if rem_T < total_T_in_B[mid_i]:
                last_i = mid_i
            else:
                first_i = mid_i
        result = max(result, i + first_i + 1)

    print(result)

main()


Measured memory_bytes: 117936
n,x=map(int,input().split())
p=[1]
q=[1]
for i in range(n):
    p.append(2*p[i]+1)
    q.append(2*q[i]+3)
def cal(n_cal,x_cal):
    if x_cal==0:
        ret=0
    elif n_cal==0 and x_cal==1:
        ret=1
    elif x_cal==1:
        ret=0
    elif 1<x_cal<=q[n_cal-1]+1:
        ret=cal(n_cal-1,x_cal-1)
    elif  q[n_cal-1]+2==x_cal:
        ret=(p[n_cal-1]+1)
    elif x_cal<=2+q[n_cal-1]*2:
        ret=p[n_cal-1]+1+cal(n_cal-1,x_cal-(q[n_cal-1]+2))
    elif x_cal==3+2*q[n_cal-1]:
        ret=2*p[n_cal-1]+1
    return ret
print(cal(n,x))


Measured memory_bytes: 38384
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N, K;
  int A[200000];
    
  cin >> N >> K;
  
  for (int i = 0; i < N; i++)
  {
    cin >> A[i];
  }
  
  for (int i = K; i < N; i++)
  {
    if (A[i] > A[i-K])
    {
      cout << "Yes" << endl;
    }
    else
    {
      cout << "No" << endl;
    }
  }
}

Measured memory_bytes: 4336
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAX=2e3+10;
int A[MAX];
int main (){
    int N,x;cin >> N;
    for(int i=1;i<=N;i++){
        cin >> A[i];
    }
    sort(A+1,A+1+N);
    ll res = 0;
    for(int i=1;i<=N-2;i++){
            for(int j=i+1;j<=N-1;j++){
                bool f=0;
                for(int k=j+1;k<=N;k++){
                    if(A[i]+A[j]>A[k]){
                        res++;
                    }
                    else break;
                }
            }
    }
    cout<<res;
    return 0;
}


Measured memory_bytes: 256
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, read().split())
if (n + 1) // 2 >= k:
    print('YES')
else:
    print('NO')


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

bool is_acgt(char c) {
  if (c == 'A' || c == 'C' || c == 'G' || c == 'T') {
    return true;
  } else {
    return false;
  }
}

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

  int ans = 0;
  int cnt = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    char c = s.at(i);

    if (is_acgt(c)) { cnt++; }
    if (!is_acgt(c) || i == (int)s.size()-1) {
      ans = max(ans, cnt);
      cnt = 0;
    }
  }

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

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

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define each(it,v) for(auto &it : v)
#define mod 1000000007
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vl vector<long>
#define P pair<int,int>
using namespace std;

main()
{
        int d; cin>>d;
        string ans;
        if(d==22)ans="Christmas Eve Eve Eve";
        else if(d==23)ans="Christmas Eve Eve";
        else if(d==24)ans="Christmas Eve";
        else ans="Christmas";
        cout<<ans<<endl;
}

Measured memory_bytes: 256
N = int(input())

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

if A[0]==0:
    B = [1,-1]
    ANS = [1,1]
else:
    B = [A[0],(-1)*(A[0])//abs(A[0])]
    ANS = [0,abs(A[0])+1]

#print(b)
for i in range(len(B)):
    b = B[i]
    for a in A[1:]:
        #print("a+b"+str(a+b))
        if a+b==0:
            ANS[i] += 1
            b = (-1)*(b)//abs(b)
        elif b*(a+b)<0:
            b += a
        else:
            ANS[i] += abs(a+b)+1
            #print("ans"+str(ans))
            b = (-1)*(a+b)//abs(a+b)
        #print(b)
print(min(ANS))

Measured memory_bytes: 14468
N=map(int,input().split())
list=[int(_) for _ in input().split()]


countlist=[1]
for i in range(1,len(list)):
    if list[i]==list[i-1]:
        countlist[-1]+=1
    else:
        countlist.append(1)

sum=0

for num in countlist:
    if num>=2:
        sum+=int(num/2)

print(sum)

Measured memory_bytes: 3064
// -*- coding:utf-8-unix -*-

use proconio::input;

fn main() {
    input! {
        n: usize,
        m: usize,
    }

    // 奇+奇, 偶+偶
    let ans: usize = n * (n - 1) / 2 + m * (m - 1) / 2;
    println!("{}", ans);
}


Measured memory_bytes: 2140
//35
#include<iostream>

using namespace std;

int main(){
  bool w[11][11]={{}};
  for(int i=1;i<=9;i++){
    for(int j=1+i%2;j<10;j+=2){
      char c;
      cin>>c;
      w[i][j]=c-'0';
    }
  }
  int y=0,x=2;
  int d=1;
  int dy[]={-1,0,1,0};
  int dx[]={0,1,0,-1};
  do{
    const char *s="URDL";
    d=(d+1)%4;
    while(w[y+dy[d]][x+dx[d]]){
      cout<<s[(d+3)%4];
      d=(d+3)%4;
    }
    y+=dy[d]*2;
    x+=dx[d]*2;
  }while(x||y);
  cout<<endl;
  return 0;
}

Measured memory_bytes: 0
n,x,y,z=map(int,input().split())
mod=10**9+7
mask=1<<(x+y+z-1)
dp=[[0 for i in range(mask)] for j in range(n+1)]
dp[0][0]=1
ng=(1<<(x+y+z-1))|(1<<(y+z-1))|(1<<(z-1))
for i in range(n):
    for j in range(mask):
        for k in range(1,11):
            nmask=(j<<k)|(1<<(k-1))
            if nmask&ng==ng:
                continue
            nmask&=mask-1
            dp[i+1][nmask]=(dp[i+1][nmask]+dp[i][j])%mod
print((pow(10,n,mod)-sum(dp[n]))%mod)

Measured memory_bytes: 101748
n = gets.to_i

p = gets.split.map(&:to_i)

if p == (1..n).to_a
  puts 'YES'
  exit
end

(0..(n - 1)).each do |i|
  (0..(n - 1)).each do |j|
    p_copy = p.clone
    p_copy[i], p_copy[j] = p_copy[j], p_copy[i]
    
    if p_copy == (1..n).to_a
      puts 'YES'
      exit
    end
  end
end

puts 'NO'

Measured memory_bytes: 2812
s = gets.chomp!

si = s.length - 1

while true
  if s[si-6..si] =~ /dreamer/
    si -= 7
  elsif s[si-5..si] =~ /eraser/
    si -= 6
  elsif s[si-4..si] =~ /dream|erase/
    si -= 5
  else
    if si < 0
      puts "YES"
      break
    else
      puts "NO"
      break
    end
  end
end



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

using namespace std;

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

    long long N;
    string str = "";
    cin >> N;
    while (N > 0) {
        N--;
        str = str + char('a' + N % 26);
        N /= 26;
    }
    reverse(str.begin(), str.end());
    cout << str;
    return 0;
}


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

#define sp <<" "
#define el <<"\n"
#define S second
#define F first
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define obit __builtin_popcount
#define pof pop_front
#define For(i,a,n) for(i=a;i<n;i++)
#define all(ar) ar.begin(),ar.end() 
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned short int us;
typedef short int ss;
typedef long double ld;
const ll mod = 1000000007;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    /*
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    //*/
    ll n,i,m;
    cin>>n>>m;
    bool bar[n+1];
    ll ar[n+1];
    ar[0] = ar[1] = 1;
    memset(bar,true,sizeof(bar));
    For(i,0,m){
        ll x;
        cin>>x;
        bar[x] = false;
        ar[x] = 0;
    }
    ll res = 1;
    For(i,2,n+1){
        if(bar[i]){
            ar[i] = ((ar[i-1] % mod) + (ar[i - 2] % mod)) % mod;
        }
    }
    cout<<ar[n];
}

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

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

using pii = pair<int, int>;

const int MAX_NM = 30;
int N, M, A, C, R = 0;
int B[MAX_NM];

inline void Main() {
  // code
  int A, B, K;
  vector<int> C;
  cin >> A >> B >> K;
  repi(i, 1, min(A, B)+1) {
    if (A % i == 0 && B % i == 0) {
      C.emplace_back(i);
    }
  }
  sort(all(C));
  reverse(all(C));
  cout << C.at(K-1) << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(20);
  Main();
  return 0;
}


Measured memory_bytes: 256
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

/* Macros reg. ends here */

const ll INF = 1LL << 50;

int main() {
  cout << fixed << setprecision(15);

  ll n;
  cin >> n;

  vector<set<ll>> to(n);
  rep(i, n-1){
    ll u, v;
    cin >> u >> v;
    u--, v--;
    if(u > v) swap(u, v);
    to[u].emplace(v);
  }

  ll ans = 0;
  rep(i, n) ans += (i + 1)*(n - i);
  rep(i, n){
    ll pre = i, cnt = 0;
    ll sum = 0;
    for(auto itr = to[i].begin(); itr != to[i].end(); itr++){
      sum += (*itr - pre)*cnt;
      cnt++;
      pre = *itr;
    }
    sum += (n - pre)*cnt;
    sum *= (i + 1);
    ans -= sum;
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 21920
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 2147483647;
const ll LINF = 9223372036854775807;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

ll power(ll x, ll y) {
	ll a=1;
	while(y){if(y&1)a=a*x%MOD;y>>=1;x=x*x%MOD;}
	return a;
}

int main() {
	int n, k; cin >> n >> k;
	VI s(k+1,0);
	ll ans=0;
	for(int i=k;i>=1;i--){
		int p=k/i;
		s[i]=power(p,n);
		for(int j=2;i*j<=k;j++){
			s[i]-=s[i*j];
			s[i]+=MOD;
			s[i]%=MOD;
		}
		ans+=(s[i]*i)%MOD;
		ans%=MOD;
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 4060
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define pb push_back
#define sz(i) (int)(i.size())
#define x first
#define y second
#define ld long double
#define pii pair<int, int>
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
const int mod = 1e9 + 7;
using namespace std;
#ifndef ONLINE_JUDGE
#define tr(...) {__l(__LINE__);__f(#__VA_ARGS__, __VA_ARGS__);}
inline void __l(int line) { cerr << "#" << line << ": "; }template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << std::endl;}template <typename Arg1, typename... Args>void __f(const char* names, Arg1&& arg1, Args&&... args){const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);}
#else
#define tr(...)
#endif
template<class T> void smin(T& a, T val) {if (a > val) a = val;}template<class T> void smax(T& a, T val) {if (a < val) a = val;}template<typename T>inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v){bool first = true;os << "[";for(auto i: v){if(!first)os << ", ";os << i;first = false;}return os << "]";}
const int N = 2*1e5 + 10;
// make sure N size is correctttt!!!!!!!!!

int power(int a, int b, int m = mod){if(b < 0)b += m-1; int r = 1; while(b){if(b&1)r=(r*a)%m; a = (a*a)%m; b>>=1;}return r;}

pii a[N];
int fact[N+10], ifact[N+10];

void pre(int N){
   fact[0] = 1;
   for (int i = 1; i <= N; ++i)
   {
      fact[i] = (i*fact[i-1])%mod;
   }
   ifact[N] = power(fact[N], -1);
   for (int i = N-1; i >= 0; --i)
   {
      ifact[i] = ((i+1)*ifact[i+1])%mod;
   }
}

int nCr(int n, int r){
   if(n < r)return 0;
   return ((fact[n]*ifact[r])%mod * ifact[n-r])%mod;
}

int formula(int x1, int x2, int y1, int y2){
   if(x1 > x2 or y1 > y2)
      return 0;
   return nCr(x2-x1+y2-y1,x2-x1);
}

int h, w, n, total, reach[N];//number of ways to reach from (1,1) to (x, y);

int calculate(){
   int sum = 0;
   for (int i = 1; i <= n; ++i)
   {
      reach[i] = formula(1,a[i].x,1,a[i].y);
      int remove = 0;
      for (int j = 1; j < i; ++j){
         remove += (reach[j] * formula(a[j].x,a[i].x,a[j].y,a[i].y)) % mod;
         remove %= mod;
      }
      reach[i] -= remove;
      reach[i] += mod;
      reach[i] %= mod;
      sum += (reach[i] * formula(a[i].x,h,a[i].y,w))%mod;
      sum %= mod;
   }
   return (total-sum+mod)%mod;
}

int32_t main(){_
   pre(N);

   cin>>h>>w>>n;
   for (int i = 1; i <= n; ++i)
   {
      cin>>a[i].x>>a[i].y;
   }

   sort(a+1, a+n+1);

   total = formula(1,h,1,w);
   cout<<calculate();

   //make sure N size is correct!!!!!!!!!
   return 0;
}

Measured memory_bytes: 4864
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (int i = m; i < (n); ++i)
using namespace std;
using ll = long long;

ll N, M, S, A[500009], B[500009], C[500009];
ll dist[100009];
vector<pair<ll, ll>> G[100009];
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> Q;

void dijkstra(int S){
    // 距離を初期化する
    for (int i = 1; i <= N; i++) dist[i] = 1e18;

    // 優先度付きキューには (距離, 頂点番号) の組を入れる
    Q.push(make_pair(0, S));
    dist[S] = 0;
    while (!Q.empty()) {
        ll pos = Q.top().second; Q.pop();
        for (int i = 0; i < G[pos].size(); i++) {
            ll to = G[pos][i].first, cost = G[pos][i].second;
            if (dist[to] > dist[pos] + cost) {
                dist[to] = dist[pos] + cost;
                Q.push(make_pair(dist[to], to));
            }
        }
    }
    /*
    // 始点からの距離を出力する
    for (int i = 1; i <= N; i++) cout << dist[i] << " ";
    cout << endl;
    */
}

int main() {
    // <無向>グラフを入力する 頂点N 辺M 番号は1~
    cin >> N; M = N-1;
    //scanf("%d%d", &N, &M);
    // 頂点A,B間の距離C
    for (int i = 1; i <= M; i++) {
        cin >> A[i] >> B[i] >> C[i];
        //scanf("%d%d%d", &A[i], &B[i], &C[i]);
        G[A[i]].push_back(make_pair(B[i], C[i]));
        G[B[i]].push_back(make_pair(A[i], C[i]));
    }
    /*
    // 始点をすべて試す
    repp(s,N+1,1){
        dijkstra(s);
    }
    */
    int q, k; cin >> q >> k;
    dijkstra(k);
    vector<vector<int>> ar(q, vector<int>(2));
    rep(i,q) cin >> ar[i][0] >> ar[i][1];
    rep(i,q) cout << dist[ar[i][0]] + dist[ar[i][1]] << endl;
}

Measured memory_bytes: 21232
#include<bits/stdc++.h>
#define FOR0(i,n) for(int i=0;i<n;i++)
#define FOR1(i,n) for(int i=1;i<=n;i++)
#define FORl(i,l,n) for(int i=l;i<n;i++)
using namespace std;
#define pi pair<ll,ll>
#define pb push_back
#define ll long long
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define mst0(x) memset(x,0,sizeof(x));
#define vi vector<ll>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define sp <<" "<<
#define intmax 1e14

ll power(ll num,ll g,ll mod){
  if(g==0)return 1;
  if(g%2==1)return (num*power((num*num)%mod,g/2,mod))%mod;
  return power((num*num)%mod,g/2,mod);
}

const int
	MAXN = 500005,
	MAXLGN = 20;
 
int N, Q;
map<int, int> pos;
int tree[MAXN];
int cant;
struct node {
	int val, L, R, size;
} segtree[2 * MAXN * MAXLGN];
 
int build( int lo, int hi ) {
	if ( lo > hi ) return 0;
	int idx = ++cant;
	int mid = ( lo + hi ) / 2;
	segtree[idx] = (node){ mid, build( lo, mid - 1 ), build( mid + 1, hi ), 0 };
	return idx;
}
 
int update( int x, int val, int amount ) {
	if ( x == 0 ) return 0;
	int idx = ++cant;
	int L = segtree[x].L;
	int R = segtree[x].R;
	if ( val < segtree[x].val ) L = update( L, val, amount );
	if ( val > segtree[x].val ) R = update( R, val, amount );
	segtree[idx] = (node){ segtree[x].val, L, R, segtree[x].size + amount };
	return idx;
}
 
int query( int x, int val ) {
	if (val < segtree[x].val)
		return query( segtree[x].L, val ) + segtree[x].size -	segtree[ segtree[x].L ].size;
 
	if (val > segtree[x].val)
		return query( segtree[x].R, val );
 
	return segtree[x].size - segtree[ segtree[x].L ].size;
}
 
int main() {
	cin >> N;
    cin >> Q;
	tree[0] = build( 1, N );
	FOR1(i, N) {
		int x, posx;
		cin >> x;
        posx = pos[x];
		if (posx != 0) tree[i] = update( update( tree[i - 1], posx, -1 ), i, +1 );
		else tree[i] = update( tree[i - 1], i, +1 );
		pos[x] = i;
	}
 
	while (Q--) {
		int lo, hi;
		cin >> lo >> hi;
		cout << query( tree[hi], lo ) << endl;
	}
 
}

Measured memory_bytes: 294032