akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) ((x)&(-(x)))
#define mid ((l+r)>>1)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
const int maxn = 205;
char mp[maxn][maxn];
int vis[maxn];
int n;
int ok = 1;
void dfs(int u, int c){
    //cout<<"u:"<<u<<" c:"<<c<<endl;
    if(vis[u] == 0) vis[u] = c;
    else if(vis[u] != c){
        ok = 0; return;
    }
    else return;
    for(int i = 1; i <= n; ++i){
        if(mp[u][i] == '1') dfs(i, c^1);
        if(!ok) return;
    }
}
queue<int> q;
int sol(int st){
    memset(vis, 0, sizeof vis);
    vis[st] = 1;
    while(q.size()) q.pop(); q.push(st);
    while(q.size()){
        int u = q.front(); q.pop();
        //cout<<"u:"<<u<<endl;
        for(int i = 1; i <= n; ++i){
            if(mp[u][i] == '0') continue;
            if(vis[i]) continue;
            vis[i] = vis[u] + 1;
            q.push(i);
            //cout<<"i:"<<i<<endl;
        }
    }
    int mx = 0;
    for(int i = 1; i <= n; ++i){
        mx = max(vis[i], mx);
    }
    return mx;
}
int main()
{
    cin>>n;
    for(int i = 1; i <= n; ++i){
        scanf("%s", mp[i]+1);
    }
    dfs(1, 2);
    if(!ok){
        printf("-1\n"); return 0;
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i){
        ans = max(ans, sol(i));
      //  cout<<"i:"<<i<<" ans:"<<ans<<endl;
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll ; 
typedef long double ld ;
typedef pair<ll , ll> pll ;

const ll N = 2e6 + 30 , Mod = 1e9 + 7 ;
const ll SQ = 330 ;

bool ok(ll x) {
	if (x == 1) return 0;
	for (int i = 2;i < x; i ++)  {
		if (x % i == 0) return 0;
	}
	return 1;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0) ;
	ll n ;
	cin >> n;
	while (!ok(n)) n ++;
	cout << n << endl;
    return (0) ;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int mod=1e9+7;
ll f[N];
ll n,k;
ll qpow(ll x,ll y) {
	ll s=1;
	while (y) {
		if (y&1) s=(s*x)%mod;
		x=(x*x)%mod;
		y>>=1;	
	}
	return s;
}
int main() {
	ll i,j,ans=0;
	cin>>n>>k;
	for (i=k;i>=1;i--) {
		f[i]=qpow(k/i,n);
		for (j=i+i;j<=k;j+=i)
			f[i]=(f[i]-f[j]+mod)%mod;
		ans=(ans+f[i]*i%mod)%mod;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 4324
import sys
input = sys.stdin.readline

n = int(input())
a = []
xy = []
for i in range(n):
    a.append(int(input()))
    xy.append([ list(map(int,input().split())) for _ in range(a[i]) ])
# print(a)
# print(xy)

ls = [-1]*n
ans = 0

for i in range(2**n):
    b = str(bin(i))[2:]
    c = b.zfill(n)
    # print(c)
    numbers = sum(list(map(int,list(str(c)))))
    for j in range(n):
        if c[j]=="0":
            pass
        else:
            for k in range(a[j]):
                x = xy[j][k][0]
                y = xy[j][k][1]
                # print(x,y)
                if c[x-1]==str(y):
                    pass
                else:
                    numbers = 0
        # print("--")
    ans = max(ans,numbers)
    # print("------")

print(ans)

Measured memory_bytes: 45532
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
#include<list>
#include <array>
using namespace std;
#define i64  long long
#define int  long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define INF I64_MAX
#define MOD 1000000007



void solve(void)
{ 
  int N;
  cin>>N;
  if(N%2 == 1)
  {
    cout<<0<<endl;
    return;
  }

  int ANS = 0;
  int P = 10;
  while(P <= N)
  {
    ANS += N/P;
    P *= 5;
  }
  
  cout<<ANS;
  
  return;  
}



int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  // std::cout << std::fixed;
  // std::cout << std::setprecision(8);

  solve();

 
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int n,a[102],x,y;
int main()
{
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort (a,a+n);
    for (int i=n-1;i>=0;i--)
    {
        x+=a[i];
        i--;
        if (i<0)
            break;
        y+=a[i];
    }
    cout<<x-y;
    return 0;
}


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

using namespace std;

typedef long long ll;

int n;
vector<map<int, int>> v;

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

	cin >> n;

	v.resize(n);

	rep(i, n){
		int a;
		cin >> a;

		rep(j, a){
			int x, y;
			cin >> x >> y;
			x--;
			v[i][x] = y;
		}
	}
	
	int ans = 0;
	for(int b = 0; b < (1 << n); b++){
		bool ok = true;
		rep(i, n){
			for(auto p : v[i]){
				int x = p.first;
				int y = p.second;

				if(((b >> i) & 1) == 1){
					if(((b >> x) & 1) != y){
						ok = false;
						break;
					}
				} 
			}
			if(!ok) break;
		}
		if(ok) ans = max(ans, __builtin_popcount(b));
	}

	cout << ans << endl;
}


Measured memory_bytes: 256
// By Mohit Gupta
#include <bits/stdc++.h>
using namespace std;
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout," "))
#define printintv(s,e) copy(s,e, ostream_iterator<int>(cout," "))
#define forstl(i,v) for(auto &i: v)
#define forn(i,e) for(int i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(int i = s; i >= 0; i--)
#define rforsn(i,s,e) for(int i = s; i >= e; i--)
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a) __builtin_popcount(a) // count set bits (for ints only diff for ll)
//https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef pair<int,int> p32; 
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int,int> m32;
#pragma GCC optimize ("-O2")
const int LIM = 5e5+5, MOD = 998244353;
int t,n,m,k,x,y;
 
 
int main()
{
	IOS;
	cin>>x>>y>>t;
	cout<<t<<" "<<x<<" "<<y<<ln;
}


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

using ll = long long;

ll gcd(ll x, ll y) {
    if (y == 0) return x;
    return gcd(y, x % y);
}

int main() {
    ll A, B, C, D;
    cin >> A >> B >> C >> D;
    ll ans = B - A + 1;
    ans -= B / C;
    ans += (A - 1) / C;
    ans -= B / D;
    ans += (A - 1) / D;
    ll lcm = C * D / gcd(C, D);
    ans += B / lcm;
    ans -= (A - 1) / lcm;
    cout << ans << endl;
}

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

struct Card { char suit, value; };

void selectionSort(struct Card a[], int N) {
	for (int i = 0; i < N; i++) {
		int minj = i;
		for (int j = i; j < N; j++) {
			if (a[j].value < a[minj].value) {
				minj = j;
			}
		}
		if (a[i].value != a[minj].value) {
			swap(a[i], a[minj]);
		}
	}
}

void pri(struct Card a[], int N) {
	for (int i = 0; i < N - 1; i++) {
		cout << a[i].suit << a[i].value << " ";
	}
	cout << a[N - 1].suit << a[N - 1].value << endl;
}

void BubbleSort(Card c[], int N) {
	for (int i = 0; i < N; i++) {
		for (int j = N - 1; j > i; j--) {
			if (c[j].value < c[j - 1].value) {
				swap(c[j], c[j - 1]);
			}
		}
	}
}

bool com(Card a[], Card b[], int N) {
	for (int i = 0; i < N; i++) {
		if (a[i].suit != b[i].suit)return false;
	}
	return true;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int N; cin >> N;
	Card a[100], b[100];
	for (int i = 0; i < N; i++) {
		cin >> a[i].suit >> a[i].value;
		b[i] = a[i];
	}
	selectionSort(a, N);
	BubbleSort(b, N);
	pri(b, N);
	cout << "Stable" << endl;
	pri(a, N);
	if (com(a, b, N)) {
		cout << "Stable" << endl;
		return 0;
	}
	cout << "Not stable" << endl;
	return 0;
}


Measured memory_bytes: 3224
main(n,a,b,t,i,f){
	scanf("%d",&n);
	while(n--){
		a=b=-100;
		for(i=f=0;i<10;i++){
			scanf("%d",&t);
			if(a<t)			a=t;
			else if(b<t)	b=t;
			else	f=1;
		}
		puts(f?"NO":"YES");
	}
	exit(0);
}

Measured memory_bytes: 0
#include <iostream>
using namespace std;
 
#define TIF(c,a,b) (c ? a : b)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define READ(v) std::cin >> v;
#define WRITE(v) std::cout << v;
#define USORT(a,n) sort(a, a + n);
#define LSORT(a,n) sort(a, a + n, greater<int>());
 
#define cin std::cin
#define cout std::cout
using ll = long long;
using ull = unsigned long long;
 
int main()
{
  int a,b;
  cin >> a >> b;
  if((a > 0 && a < 10) && (b > 0 && b < 10))
  {
  	cout << a*b << endl;
  }
  else
  {
    cout << "-1" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())



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

uf = UnionFind(N + 1)
for i in range(M):
    uf.union(*list(map(int, input().split())))

print(uf.group_count()-2)

Measured memory_bytes: 12968
#include <stdio.h>

int main(void) {
	float max = -1,min = -1, buf;
	
	while(scanf("%f", &buf) != EOF){
		if(min == -1) min = buf;
		if(max < buf) max = buf;
		if(min > buf) min = buf;
	}
	printf("%f\n", max-min);
	return 0;
}

Measured memory_bytes: 644
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end() 
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	string s;
	cin >> s;
	int n = s.size();
	int k;
	cin >> k;
	set<string> pos;
	rep(i, n) repn(j, 1, min(35, n - i + 1)){
		string cur = s.substr(i, j);
		pos.insert(cur);
	}
	int cnt = 0;
	for(string x : pos){
		cnt++;
		if(cnt == k){
			cout << x << endl;
			return 0;
		}
	}
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


Measured memory_bytes: 16896
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <deque>
using namespace std;
int main(){
    int n,t;
    cin >> n >> t;
    long long total=0;
    int now=0;
    vector<long long> tl(n);
    for(int i=0;i<n;i++){
        cin >> tl[i];
    }
    for(int i=0;i<n-1;i++){
        now = tl[i+1]-tl[i];
        total+=min(t,now);
    }
    total+=t;
    cout << total << endl;
}

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

using namespace std;

typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

string s; int n;


int calc(int count, int idx){
	if(count == -1) return -1;
	idx++; //(
	while(s[idx] != ')'){
		if(count == -1) return -1;
		if(isalpha(s[idx])){
			if(count == n){cout << s[idx] << endl; return -1;}
			count++; idx++;
		}
		else {
			int m = 0;
			while(isdigit(s[idx])){m*=10; m += s[idx]-'0'; idx++;}
			rep(i, m) count = calc(count, idx);

			idx++;
			int b = 1;
			while(b != 0){
				if(s[idx] == '(') b++;
				else if(s[idx] == ')') b--;
				idx++;
			}
		}
	}
	return count;
}

int main(){
	while(cin >> s >> n){
		for(int i=0; i < s.size(); i++){
			if(isdigit(s[i]) && isalpha(s[i+1])) s = s.substr(0, i+1) + "(" + s.substr(i+1, 1) + ")" + s.substr(i+2);
		}

		int count = 0;

		if(s == "0" && n == 0) break;
		for(int i=0; i< s.size();){
			if(count == -1) break;
			if(isalpha(s[i])){
				if(count == n){cout << s[i] << endl; count = -1; break;}
				count++; i++;
			}
			else {
				int m = 0;
				while(isdigit(s[i])){m*=10; m += s[i]-'0'; i++;}

				rep(j, m) count = calc(count, i);

				i++;
				int b = 1;
				while(b != 0){
					if(s[i] == '(') b++;
					else if(s[i] == ')') b--;
					i++;
				}
			}
		}
		if(count != -1) cout << 0 << endl;
	}
}

Measured memory_bytes: 1220
//clear adj and visited vector declared globally after each test case
//check for long long overflow
//while adding and subs check if mod becomes -ve
//while using an integer directly in a builtin function add ll
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Dont keep array name as size or any other key word

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
    int g=__gcd(a, b);
    return a/g*b;
}
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }

int32_t main()
{
    IOS;
    char c;
    cin>>c;
    int s=c-'a';
    if(s>=0&&s<=25)
    cout<<'a';
    else
    cout<<'A';
}

Measured memory_bytes: 3608
from queue import PriorityQueue
N = int(input())
A = map(int, input().split())
q = PriorityQueue()
for a in A:
    q.put(-a)
x = -q.get()
while not q.empty():
    y = -q.get()
    if y == 0:
        break
    q.put(-(x % y))
    x = y
print(x)

Measured memory_bytes: 15192
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, self.pow(b, self.m-2))

    def pow(self, x, y):
        if y == 0:
            return 1
        elif y == 1:
            return x % self.m
        elif y % 2 == 0:
            return self.pow(x, y//2)**2 % self.m
        else:
            return self.pow(x, y//2)**2 * x % self.m


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod//i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n-r)
        return self.g1[n] * self.g2[r] * self.g2[n-r] % self.MOD


@mt
def slv(N, M, K):
    m = Mod(10**9+7)
    d = Combination(N*M, 10**9+7)(N*M-2, K-2)

    ans = 0
    for i in range(1, N+1):
        for j in range(1, M+1):
            if i == N and j == M:
                continue
            if i == N or j == M:
                a = 1
            else:
                a = 2
            ans = m.add(ans, m.mul(i*j*a, m.mul(i + j, d)))
    ans = m.div(ans, 2)

    return ans


def main():
    N, M, K = read_int_n()
    print(slv(N, M, K))


if __name__ == '__main__':
    main()


Measured memory_bytes: 29220
#include<bits/stdc++.h>
using namespace std;
int n,ans=0;
bool checks(int x)
{
	int tmp=0;
	for(int i=1;i<=x;i++)
		if(x%i==0)tmp++;
	if(tmp==8)return true;
	return false;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i+=2)
	if(checks(i))ans++;
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 256
def check_count_func():
    count_dict = {}

    def check_count(x):
        if x in count_dict:
            return count_dict[x]
        
        if x == 1:
            return 1
        
        count_dict[x] = 1 + 2 * check_count(x >> 1)
        return count_dict[x]
    
    return check_count


h = int(input())
h_s = bin(h)[2:]
h_s_len = len(h_s)

f = check_count_func()
for i in range(1, h_s_len):
    x = int(h_s[:i], 2)
    f(x)

ans = f(h)
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
using ll = long long;
int main(){
    int a,b; cin>>a>>b;
    if(a<b){
        rep(i,b)cout << a;
        cout << endl;
    }else{
        rep(i,a)cout << b;
        cout << endl;
    }
}


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

using namespace std;

void swap(int &a, int &b) {
	int t = a;
	a = b;
	b = t; 
}

int partition(vector<int> &A, int p, int r) {
	int x = A[r];
	int i = p;
	for (int j = p; j < r; j++) {
		if (A[j] <= x) {
			swap(A[i], A[j]);
			i++;
		}
	}
	swap(A[i], A[r]);
	return i;
}

int main() {
	int n = 0;
	cin >> n;
	vector<int> v;
	v.resize(n);

	{
		vector<int>::iterator i = v.begin();
		copy(istream_iterator<int>(cin), istream_iterator<int>(), i++);
	}
	int p = partition(v, 0, n-1);
	
	for (int i = 0; i < n; i++) {
		if (i == p) {
			cout << "[" << v[i] << "] ";
		}
		else if (i == n - 1) {
			cout << v[i] << "\n";
		}
		else {
			cout << v[i] << " ";
		}
	}
	return 0;
}

Measured memory_bytes: 3096
#include<bits/stdc++.h>
#define ll long long int
#define N (ll)(1e9+7)
#define T int
#define P pair<T,T>
#define umap unordered_map<T,T>
#define uset unordered_set<T>
#define rep(i,n) for(i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define print(v) for(auto x:v) cout<<x<< " "
#define pb push_back
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b,c;
    cin>>a>>b>>c;
    int n=a-b;
    if(n>0)
    {
        if(c-n>=0)
            cout<<c-n;
        else
            cout<<"0";
    }
    else
        cout<<c;

    return 0;
}



Measured memory_bytes: 256
/*
ID: alec3
LANG: C++14
PROG: frac1
CLASSIFICATION: flood fill
*/

#include <bits/stdc++.h>
#define check(x) cout<<(#x)<<": "<<x<<" ";
#define io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ss second
#define ff first
#define pb push_back
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef unsigned long long ull;

int pct(int x) { return __builtin_popcount(x); }

using namespace std;


int pwr(int a,ll b) {
	int ans = 1;
	while(b) {
		if(b & 1) ans = (ans * 1LL * a);
		a = (a * 1LL * a);
		b >>= 1;
	}
    return ans;
}


const int N = 2e5 + 5;
int f[N];
int pw[3][N];

void solve(){
    int n;
    cin >> n;
    // loop through all numbers less than the length of s
    for (int i = 1; i <= n; i++){
        // compute the number mod popcount
        int j = i % pct(i);
        // save f[i] as f[i % pct(i)] + 1
        f[i] = f[j] + 1;
        assert(j < i);
    }
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    int tot = 0;
    for (char ch : s){
        // add one to tot if ch is 1
        tot += (ch == '1');
    }

    vector<int> mods;
    if (tot > 0) mods.pb(tot);
    if (tot > 1) mods.pb(tot -1);
    if (tot < n) mods.pb(tot + 1);
    vector<int> val(mods.size());

    for (int i = 0; i < mods.size(); i++){
        int md = mods[i];
        pw[i][0] = 1 % md;
        for (int j = 1; j <= n; j++){
            pw[i][j] = (pw[i][j-1] << 1) % md;
        }
        for (int j = 0; j < n; j ++){
            if (s[j] == '1')
                val[i] = (val[i] + pw[i][j]) % md;
        }
    }
    vector<int> ans;
    for (int i = 0; i < n; i++){
        int cur = tot + (s[i] == '0') - (s[i] == '1');
        if (cur == 0){
            ans.pb(0);
            continue;
        }
        int j = -1;
        for (int k = 0; k < mods.size(); k++){
            if (mods[k] == cur) j = k;
        }
        assert(j != -1);
        int curval = val[j];
        if (s[i] == '0') {
            curval = (curval + pw[j][i]) % cur;
        }
        else {
            curval = (curval - pw[j][i] + cur) % cur;
        }
        ans.pb(1 + f[curval]);
    }
    reverse(ans.begin(), ans.end());
    for (int x : ans) cout << x << endl;
}

int main()
{
    //ifstream fin("frac1.in");
	//ofstream fout("frac1.out");
	io;
	solve();

	return 0;
}


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

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

    int c[m];
    for(int i=0;i<m;i++)    cin >> c[i];

    int coin[m][n+1];
    for(int i=0;i<m;i++)   coin[i][0] = 0;
    for(int i=1;i<=n;i++)   coin[0][i] = coin[0][i-1]+1;

    for(int i=1;i<m;i++){
        for(int j=0;j<=n;j++){
            if(j<c[i]){
                coin[i][j] = coin[i-1][j];
            }else{
                coin[i][j] = min(coin[i-1][j],coin[i][j-c[i]]+1);
            }
        }
    }

    cout << coin[m-1][n] << endl;

    return 0;
}


Measured memory_bytes: 7012
#include<bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define rep(i, N) for(int (i) = 0; (i) < (N); (i)++)
#define all(v) (v).begin(), (v).end()
#define eb emplace_back

int main(){
    i64 N , K;
    cin >> N >> K;
    vector<i64> A(N);
    rep(i, N) cin >> A[i];
    sort(all(A));
    const i64 INF = i64(1e18) + 1;
    i64 left = -INF, right = INF;
    while(right - left > 1){
        i64 x = (left + right) / 2;
        i64 lt_x = 0;
        rep(i ,N){
            i64 l = -1, r = N;
            if(A[i] < 0) {
                while(r - l > 1){
                    i64 mid = (l + r) / 2;
                    if(A[i] * A[mid] < x) r= mid;
                    else l = mid;
                }
                lt_x += N - r;
            } else {
                while(r - l > 1){
                    i64 mid = (l + r) / 2;
                    if(A[i] * A[mid] < x) l = mid;
                    else r = mid;
                }
                lt_x += (l + 1);
            }
            if(A[i] * A[i] < x) lt_x--;
        }
        lt_x /= 2;
        if(lt_x < K) left = x;
        else right = x;
    }
    cout << left << endl;
}

Measured memory_bytes: 1792
#include <stdio.h>

int main(){
    int N;
    int i;
    long long int sum = 0;
    scanf("%d", &N);
    
    for(int i = 1; i <= N; i++){
        if(!(i % 3 == 0 || i % 5 == 0)){
            sum += i;    
        }
        
    }
     printf("%lld",sum);
    
    return 0;
}

Measured memory_bytes: 1736
import Control.Monad
import Data.Array.IO
import Data.Array.Unboxed
import qualified Data.Vector.Unboxed as V

type Idx = (Int,Int,Int)
type IODp = IOUArray Idx Int

main = do
    [n,a] <- map read.words<$>getLine::IO[Int]
    xs <- map read.words<$>getLine::IO[Int]
    let nx = n * maximum (a:xs)
    let vxs = V.fromList xs
    dp <- newArray ((0,0,0),(n,n,nx)) 0 :: IO IODp
    forM_ [(j,k,s)|s<-[0..nx],k<-[0..n],j<-[0..n]] $ \(j,k,s) -> do
        case (j,k,s) of
            (j,0,0) -> writeArray dp (j,0,0) 1
            (j,k,s) | j>0, s<vxs V.! (j-1) -> writeArray dp (j,k,s) =<< readArray dp (j-1,k,s)
            (j,k,s) | j>0, k>0, s>=vxs V.! (j-1) -> liftM2 (+) (readArray dp (j-1,k,s)) (readArray dp (j-1,k-1,s-vxs V.!(j-1))) >>= writeArray dp (j,k,s)
            otherwise -> return ()
    print.sum =<< mapM (\k -> readArray dp (n,k,k*a)) [1..n]

{-
dp(j,k,s) -> [x1..xj]からk集めた合計がsになる総数

dp(*,0,0) = 1
dp(j,k,s) = dp(j-1,k,s)                    , j>=1, s<xj
dp(j,k,s) = dp(j-1,k,s) + dp(j-1,k-1,s-xj) , k>=1, j>=1, s>=xj
other = 0
-}

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

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

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

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

void Main(){
    int n;
    cin >> n;
    if (n >= 30) {
        cout << "Yes" << en;
    } else { 
        cout << "No" << en;
    }
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}


Measured memory_bytes: 3664
#define REP(i, n) for(int i = 0; i < (int)(n); i++)

#include "bits/stdc++.h"
using namespace std;

int main() {
	long long int W, H, x, y;
	cin >> W >> H >> x >> y;

	double q = (double)(W * H) / 2; 
	bool flag_multi = false;

	if ((x * 2 == W) && (y * 2 == H)) {
		flag_multi = true;
	}

	// 端点の場合
	/*
	if ((x == 0) || (x == W) || (y == 0) || (y == H)) {
		flag_multi = false;
	}
	// 中央の点の場合
	else if ((2 * x == W) && (2 * y != H)) {
		flag_multi = false;
	}
	else if ((2 * y == H) && (2 * x != W)) {
		flag_multi = false;
	}
	*/

	cout << q << " ";
	if (flag_multi) {
		cout << 1 << endl;
	}
	else {
		cout << 0 << endl;
	}

	return 0;
}


Measured memory_bytes: 256
n,m = map(int, input().split(' '))
b = [list(map(int, input().split(' '))) for _ in range(m)]
if n == 1:
    for i in range(10**(n-1)-1,10**n):
        x = list(str(i))
        for s,c in b:
            if x[s-1] != str(c):
                break
        else:
            print(int(''.join(x)))
            break
    else:
        print(-1)
else:
    for i in range(10**(n-1),10**n):
        x = list(str(i))
        for s,c in b:
            if x[s-1] != str(c):
                break
        else:
            print(int(''.join(x)))
            break
    else:
        print(-1)

Measured memory_bytes: 3064
import java.util.*;
public class Main {
  public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int p[]=new int[n];
    for(int i=0;i<n;++i)
      p[i]=sc.nextInt();
    Arrays.sort(p);
    int sum=0;
    for(int i=0;i<k;++i)
      sum+=p[i];
    System.out.println(sum);
  }
}

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

int main() {
  long N,K,ans,dif;
  cin >> N >> K;
  ans = N / K;
  N = N - K*ans;
  dif = N - K;
  if(dif<0) dif = -1*dif;
  while(N >= dif+1){
  N = dif;
  dif = N - K;
  if(dif<0) dif = -1*dif;
  }
  cout << N << endl;
}


Measured memory_bytes: 256
trade = {}
ans = {}
while True:
    s = raw_input()
    if s=='':
        break
    x, y = map(int, s.split(','))
    trade[x] = trade[x] + 1 if x in trade else 1
while True:
    try:
        s = raw_input()
        x, y = map(int, s.split(','))
        if x in trade:
            ans[x] = ans[x] + 1 if x in ans else trade[x] + 1
    except EOFError:
        break
for k, v in sorted(ans.items()):
    print k, v

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin>>n;
    
    vector<int>arr(n);
    
    for(int i=0;i<n;i++)
     cin>>arr[i];
     
    sort(arr.begin(),arr.end());
    
    cout<<arr[n-1]-arr[0]<<"\n";
    
    return 0;
}

Measured memory_bytes: 256
//
//  main.cpp
//  test
//
//  Created by 鈴木大志 on 2018/06/15.
//  Copyright © 2018年 鈴木大志. All rights reserved.
//

#include <iostream>
#include <math.h>

int main(int argc, const char * argv[]) {
    
    int W, H, X, Y, R ;
    
    typedef struct {
        int ox, oy, w, h;
    } Rect ;
    
    std::cin >> W >> H >> X >> Y >> R ;
    
    Rect rect {
        .ox = 0,
        .oy = 0,
        .w = W,
        .h = H
    };
    
    Rect en {
        .ox = X - R,
        .oy = Y - R,
        .w = X + R,
        .h = Y + R
    } ;
    
    
    if (en.ox - rect.ox >= 0 &&
        en.oy - rect.oy >= 0 &&
        rect.w - en.w   >= 0 &&
        rect.h - en.h   >= 0 ) {
        printf("Yes\n") ;
    } else {
        printf("No\n") ;
    }
    
    
    
    return 0;
}



Measured memory_bytes: 3088
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
int dp[10010];
pair<int,int> pii[1010];
set<int> st;
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int h,n;
	cin>>h>>n;
	for(int i=1;i<=n;i++){
		cin>>pii[i].second>>pii[i].first;
	}
	//cout<<pii[6].first<<endl;
	memset(dp,INF,sizeof dp);
	dp[0]=0;
	for(int i=1;i<=n;i++){
		for(int j=pii[i].second;j<=h;j++){
			dp[j]=min(dp[j],dp[j-pii[i].second]+pii[i].first);
		}
	}
	n++;
	pii[n].first=0,pii[n].second=0;
	for(int i=0;i<=h;i++){
		if(dp[i]!=INF){
			for(int j=1;j<=n;j++){
				if(i+pii[j].second>=h){
					//cout<<pii[6].first<<" "<<pii[6].second<<endl;
					//cout<<i<<" "<<j<<" "<<i+pii[j].second<<" "<<dp[i]+pii[j].first<<" "<<pii[j].first<<endl;
				st.insert(dp[i]+pii[j].first);
			}
		}
	}
}
	cout<<*st.begin()<<endl;
    return 0;
}

Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
#define all(x)   (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
//using ll = long long;
using P = pair<int,int>;

ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }

template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }
 
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;

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

struct edge{ int to,num; };

signed main(){
	int n,m,d[100010],c[200010];
	bool s[100010] = {},used[100010] = {},botch[100010] = {};
	vector<edge> G[100010];
	cin >> n >> m;
	for(int i = 0;i < n;i++) cin >> d[i];
	vector<P> vec;
	for(int i = 0;i < n;i++){
		vec.emplace_back(d[i],i);
	}
	for(int i = 0;i < m;i++){
		int u,v;
		cin >> u >> v; u--;v--;
		G[u].push_back({v,i});
		G[v].push_back({u,i});
	}
	sort(all(vec));
	for(P p : vec){
		int ds = p.first,v = p.second;
		botch[v] = true;
		for(edge e : G[v]){
			if(!used[e.to]) continue;
			if(botch[e.to]){
				s[v] = true;
				if(d[e.to] != d[v]){
					cout << -1 << endl;
					return 0;
				}
				c[e.num] = d[v];
				botch[e.to] = false;
			}else if(botch[v]){
				s[v] = !s[e.to];
				c[e.num] = d[v];
			}else c[e.num] = 1000000000;
			botch[v] = false;
		}
		used[v] = true;
	}
	for(int i = 0;i < n;i++) cout << (s[i] ? 'B' : 'W');
	cout << endl;
	for(int i = 0;i < m;i++) cout << c[i] << endl;
}

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

int main()
{
    int n,m;
    cin>>n;
    vector<int> a(n);
    REP(i,n)cin>>a[i];
    cin>>m;
    vector<int> b(m);
    REP(i,m)cin>>b[i];
    cout<<lexicographical_compare(a.begin(),a.end(),b.begin(),b.end())<<endl;
}


Measured memory_bytes: 3104
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <limits.h>
#include <cmath>
using namespace std;
using P=pair<int,int>;
using ll=long long;

int main(){
    double a,b;
    int h,m;
    cin>>a>>b>>h>>m;
    int n=h*60+m;
    double c=360-0.5*n,d=360-6*m;
    double s=abs(a*sin(c*M_PI/180)-b*sin(d*M_PI/180)),t=abs(a*cos(c*M_PI/180)-b*cos(d*M_PI/180));
    cout<<setprecision(16)<<sqrt(s*s+t*t)<<endl;
}


Measured memory_bytes: 4152
/*
ID: rchalam1
LANG: C++14
TASK:
*/
// For train.usaco.org, fill the template above.

// Competitive Coding Template

// GCC OPTIMIAZTION FLAGS
#ifdef ONLINE_JUDGE
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#endif

#include <bits/stdc++.h>

/*
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
*/
// All Includes for Competitive Coding Template

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

using vi = std::vector<int>;
using vll = std::vector<long long>;
using vull = std::vector<unsigned long long>;

#define FOR(i, a, b) for (auto i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (auto i = (a); i >= (b); --i)
#define R0F(i, a) ROF(i, a, 0)
#define ITR(a, x) for (auto& a: x)

#define FSRT(set) sort((set).begin(),(set).end())
#define RSRT(set) sort((set).rbegin(),(set).rend())
#define CSRT(set, function) sort((set).begin(),(set).end(), (function))
#define TRS(set) sort((set).begin(),(set).end(), greater<>())
#define REV(set) reverse((set).begin(),(set).end());

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

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second

// For stdio, call setIO() or setIO("").
void setIO(std::ifstream &readFile, std::ofstream &writeFile, const std::string &name = "", bool stdIO = false)
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);
	if(!name.empty() && !stdIO)
	{
		readFile.open(name + ".in");
		if(readFile.fail()) return;
		writeFile.open(name + ".out");
		std::cin.rdbuf(readFile.rdbuf());
		std::cout.rdbuf(writeFile.rdbuf());
	}
}

// To time program, create object Timer  to start timing, and call Timer.elapsed().
#include <chrono>

class Timer
{
	std::chrono::time_point<std::chrono::high_resolution_clock> m_beg;
public:
	Timer() : m_beg(std::chrono::high_resolution_clock::now())
	{}

	void reset()
	{ m_beg = std::chrono::high_resolution_clock::now(); }

	[[nodiscard]] long double elapsed() const
	{
		return std::chrono::duration_cast<std::chrono::duration<long double, std::ratio<1> >>(
				std::chrono::high_resolution_clock::now() - m_beg).count();
	}
};

// Defines datatype
using dt = int;
// Defines MAXN value
constexpr dt MAXN = 1e5 + 1;
// Defines Modulus
constexpr dt MOD = 1e9 + 7;
// Defines Floating Point Correction
constexpr long double EPS = 1e-9;

//*****
//START CODE BELOW
//*****

template <typename T>
bool isPrime(T n)
{
	for(T i = 2; i * i <= n; i++)
		if(n % i == 0)
			return false;
	return true;
}

int main(int argc, char *argv[])
{
	static std::ifstream readFile;
	static std::ofstream writeFile;
	setIO(readFile, writeFile);
	int x;
	cin >> x;
	for(x; !isPrime(x); ++x);
	cout << x << '\n';
}

Measured memory_bytes: 3648
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main (void) {

  string n;
  char m[3];

  int j[8][3] = { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 },
		  { 0, 1, 1 }, { 1, 1, 0 }, { 1, 0, 1 }, { 1, 1, 1 }}; 

  int l = 0, i;
  
  cin >> n;

  for (i = 0; i < 8; i++) {
    l = 0;
    l += n[0]-'0';
    if (j[i][0] == 0)
      l += n[1]-'0';
    if (j[i][0] == 1)
      l -= n[1]-'0';
    if (j[i][1] == 0)
      l += n[2]-'0';
    if (j[i][1] == 1)
      l -= n[2]-'0';
    if (j[i][2] == 0)
      l += n[3]-'0';
    if (j[i][2] == 1)
      l -= n[3]-'0';
    if (l == 7)
      break;
  }
  for (int k = 0; k < 3; k++) {
    if (j[i][k] == 0)
      m[k] = '+';
    else
      m[k] = '-';
  }

  cout << n[0] << m[0] << n[1] << m[1] << n[2] << m[2] << n[3] << "=7" << endl;

  return 0;
  
}

Measured memory_bytes: 256
from collections import Counter
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    asum = sum(a)
    ac = Counter(a)
    acl = defaultdict(int)
    for ae in ac:
        acl[ae] = ac[ae]

    for _ in range(q):
        b, c = map(int, input().split())
        rmed = acl[b]
        acl[c] += rmed
        acl[b] = 0
        asum -= b * rmed
        asum += c * rmed
        print(asum)

if __name__ == '__main__':
    main()


Measured memory_bytes: 103492
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstring>
#include<math.h>
#include<bitset>
#include<queue>
#include<set>
#include<iomanip>
#include<assert.h>
#include<string>
#include<cstdlib>
#include<cctype>
#include<unordered_map>
#include<sstream>
#include<numeric>
#include<stack>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1LL<<60;
constexpr int inf = 1e9;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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;
};

// intやllをstringに変換
template<typename T> inline string toString(const T &a){
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------
struct edge{
  int from;
  int to;
  int cost;
};

int main(){
  int N,M,R;
  cin >> N >> M >> R;
  vector<int> r(R);
  for(int i=0; i<R; i++){
    cin >> r[i];
    r[i]--;
  }
  vector<edge> e(M);
  for(int i=0; i<M; i++){
    cin >> e[i].from >> e[i].to >> e[i].cost;
    e[i].from--;
    e[i].to--;
  }
  vector<vector<int>> G(N,vector<int> (N));
  for(int i=0; i<N; i++){
    for(int j=0; j<N; j++){
      if(i==j) G[i][j] = 0;
      else G[i][j] = inf;
    }
  }
  for(int i=0; i<M; i++){
    G[e[i].from][e[i].to] = e[i].cost;
    G[e[i].to][e[i].from] = e[i].cost;
  }
  for(int k=0; k<N; k++){
    for(int i=0; i<N; i++){
      for(int j=0; j<N; j++){
        chmin(G[i][j],G[i][k]+G[k][j]);
      }
    }
  }
  sort(r.begin(),r.end());
  int ans = inf;
  do{
    int sum = 0;
    for(int i=0; i<R-1; i++){
      sum += G[r[i]][r[i+1]];
    }
    chmin(ans,sum);
  }while(next_permutation(r.begin(),r.end()));
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> llP;

ll mod(ll a, ll b){
  ll ret=a%b;
  if(ret<0) ret+=b;
  return ret;
}

ll modpow(ll a,ll b,ll c){
  ll res=1;
  while(b>0){
    if(b&1) res=mod(res*a,c);
    a=mod(a*a,c);
    b>>=1;
  }
  return res;
}

int main() {
  double a,b,h,m;
  cin>>a>>b>>h>>m;
  double l_a=30.0*h+m/2.0,s_a=6.0*m;
  double l_x=a*cos(M_PI*l_a/180.0),l_y=a*sin(M_PI*l_a/180.0);
  double s_x=b*cos(M_PI*s_a/180.0),s_y=b*sin(M_PI*s_a/180.0);
  double dis=sqrt((l_x-s_x)*(l_x-s_x)+(l_y-s_y)*(l_y-s_y));
  cout<<fixed<<setprecision(10)<<dis<<endl;
  return 0;
}

Measured memory_bytes: 4084
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev            coyhhhhhhyoc    
#define next          sdNNNmNNNNNNNmds  
#define y0           hNNNNy_yNNNNNN_sNh
#define y1          mNNNNNdtdNNNNNNtsNNm
#define yn          mNNNNNNNNy___smNNNms
#define tm            oooooosyysooooot 
#define read               tyhyt        
#define rank              ytmNmo         
#define index              yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int a, b;
    cin >> a >> b;
    rep(x, 0, 100000) {
        if((int)(x * .08) == a && (int)(x * .1) == b) {
            cout << x << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}


Measured memory_bytes: 256
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using st=string;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
#define pb push_back
#define mp make_pair
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};

int main() {
    ll n;
    cin >> n;
    vl a(n),cnt(100001);
    ll sum=0;
    rep(i,n){
        cin >> a[i];
        cnt[a[i]]++;
        sum+=a[i];
    }
    ll Q;
    cin >> Q;
    rep(i,Q){
        ll b,c;
        cin >> b >> c;
        sum+=(cnt[b]*(c-b));
        cout << sum << endl;
        cnt[c]+=cnt[b];
        cnt[b]=0;
    }
}

Measured memory_bytes: 4840
#include<stdio.h>
#include<cstdlib>

struct node{
    struct node *right;
    struct node *left;
    struct node *parent;
    int key;
};
typedef struct node * Node;
#define NIL NULL

Node root;

Node treeMinimum(Node x){
    
    
}

Node treeMaximum(Node x){
    
    
}

Node treeSearch(Node u, int k){
    if (u == NIL || k == u->key) {
        return u;
    }
    if (k < u->key) {
        return treeSearch(u->left, k);
    }else{
        return treeSearch(u->right, k);
    }
    
}

Node treeSuccessor(Node x){
    
    
}

void treeDelete(Node z){
    Node y; // node to be deleted
    Node x; // child of y
    
    
    
}

void insert(int k){
    Node y = NIL; // xの親
    Node x = root; // 動かすnode
    Node z;
    
    z = (Node)malloc(sizeof(struct node));
    z->key = k;
    z->left = NIL;
    z->right = NIL;
    
    while (x != NIL) {
        y = x;
        if (z->key < x->key) {
            x = x->left;
        }else{
            x = x->right;
        }
    }
    // 今、挿入すべき場所の親がyになっている
    if (y == NIL) { // Tがrootnodeしかない時
        root = z;
    }else if(z->key < y->key){
        y->left = z;
    }else{
        y->right = z;
    }
    
}

void inorder(Node u){
    if (u == NIL) {
        return;
    }
    inorder(u->left);
    printf(" %d", u->key);
    inorder(u->right);
}
void preorder(Node u){
    if (u == NIL) {
        return;
    }
    printf(" %d", u->key);
    preorder(u->left);
    preorder(u->right);
}


int main(){
    int n, i, x;
    char com[20];
    scanf("%d", &n);
    
    for ( i = 0; i < n; i++ ){
        scanf("%s", com);
        if ( com[0] == 'f' ){
            scanf("%d", &x);
            Node t = treeSearch(root, x);
            if ( t != NIL ) printf("yes\n");
            else printf("no\n");
        } else if ( com[0] == 'i' ){
            scanf("%d", &x);
            insert(x);
        } else if ( com[0] == 'p' ){
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        } else if ( com[0] == 'd' ){
            scanf("%d", &x);
            treeDelete(treeSearch(root, x));
        }
    }
    
    return 0;
}




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

int main() {
  int A, B;
  cin >> A >> B;
  
  if (A >= 6 && A <= 12) {
    cout << B / 2 << endl;
  } else if (A <= 5) {
    cout << 0 << endl;
  } else {
    cout << B << endl;
  }
  
  return 0;
}

Measured memory_bytes: 256
//2017-10-18
//miaomiao
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define pb push_back
#define For(i, a, b) for(int i = (a); i <= (int)(b); ++i)

#define N (100000+5)

int fa[N], rk, ans;
vector<int> G[N];

int Dfs(int now){
	int D = 0;
	For(i, 0, G[now].size()-1) D = max(D, Dfs(G[now][i])+1);

	if(D == rk-1){
		if(fa[now] != 1) ++ans;
		return -1;
	}
	return D;
}

int main(){
	int n;

	scanf("%d%d", &n, &rk);
	For(i, 1, n){
		scanf("%d", &fa[i]);
		if(i == 1) ans += fa[i] != 1, fa[i] = 0;
		if(i != 1) G[fa[i]].pb(i);
	}

	For(i, 0, G[1].size()-1) Dfs(G[1][i]);
	printf("%d\n", ans);

	return 0;
}

Measured memory_bytes: 12288