akhauriyash/Code-Regression huggingface dataset:
a = int(input())
b = int(input())
c = int(input())
x = int(input())
ans = 0

for i in range(a+1):
    if i*500 <= x:
        for k in range(b+1):
            if k*100 <= (x-i*500):
                if (x-i*500-k*100)/50 <= c:
                    ans += 1
print(ans)	

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

int main() {
	int N, M, b,tmp2, drink=0;
	long long int a, tmp, ans=0;
	cin >> N >> M;
	
	vector<pair<long long int, int> > pairs(N);
	for (int i = 0; i < N; i++) {
		cin >> a >> b;
		pairs[i] = make_pair(a, b);
	}
	sort(pairs.begin(), pairs.end());

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < pairs[i].second; j++) {
			drink++;
			ans += pairs[i].first;
			if (drink == M) {
				cout << ans << endl;
				return 0;
			}
		}
	}

	return 0;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
long long n, totalyears, t2;
double total=100;
int main(){
  scanf("%lld", &n);
  while(total < n){
    totalyears++;
    total *= 1.01;
    t2 = total;
    total = t2;
  }
  printf("%lld", totalyears);
}
    

Measured memory_bytes: 3812
from collections import Counter
n = int(input())
a = [int(x) for x in input().split()]

c = Counter()
for x in a:
    c[x] += 1

m = max(a)
chk = True
if m % 2:
    for i in range(1, m+1):
        if i < m//2+1 and c[i] > 0:
            chk = False
        elif i == m//2+1 and c[i] != 2:
            chk = False
        elif i > m//2+1 and c[i] < 2:
            chk = False
else:
    for i in range(1, m+1):
        if i < m//2 and c[i] > 0:
            chk = False
        elif i == m//2 and c[i] != 1:
            chk = False
        elif i > m//2 and c[i] < 2:
            chk = False

print("Possible" if chk else "Impossible")

Measured memory_bytes: 65160
#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

// using
using namespace std;

// typedef
typedef long long ll;

// define
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REVERSE(c) reverse(ALL(c))
#define UNIQ(c) unique(ALL(c))
#define FOR(i, a, b, c) for (int i = (int)(a); i < (int)(b); i += (int)(c))
#define REP(i, n) FOR(i, 0, n, 1)
#define RFOR(i, a, b, c) for (int i = (int)(a); i >= (int)(b); i -= (int)(c))
#define RREP(i, n) RFOR(i, n, 0, 1)

// functions
template <class T> T ceil(T a, T b) {
    return (a + b - 1) / b;
}
template <class T> T round(T a, T b) {
    return (a + b / 2) / b;
}
template <class T> T gcd(T a, T b) {
    return b ? gcd(b, a % b) : a;
}
template <class T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}
template <class T> bool amax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool amin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

// main
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int X;
    cin >> X;
    int A[4];
    REP(i, 4) {
        A[3 - i] = X % 10;
        X /= 10;
    }
    REP(i, 1 << 3) {
        int val = A[0];
        string res = to_string(A[0]);
        REP(j, 3) {
            int a = A[j + 1];
            if ((i >> j) & 1) {
                val += a;
                res += "+" + to_string(a);
            } else {
                val -= a;
                res += "-" + to_string(a);
            }
        }
        if (val == 7) {
            cout << res + "=7" << endl;
            break;
        }
    }
    return 0;
}


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

const int END = -1;
struct StNod
{
  int m_nDept;
  int m_nParnt;
  int m_nLChd;
  int m_nRSib;

  StNod() :
    m_nDept(END), m_nParnt(END), m_nLChd(END), m_nRSib(END)
  { }
};

void fnInput(vector<StNod> &rvoNod)
{
  StNod oNod;
  int nMaxSiz;

  cin >> nMaxSiz;
  rvoNod.resize(nMaxSiz, oNod);

  for (int i = 0; i < nMaxSiz; i++)
  {
    int nParnt, nCCnt, nChd, nBChd = END;
    cin >> nParnt >> nCCnt;
    for (int j = 0; j < nCCnt; j++)
    {
      cin >> nChd;
      rvoNod[nChd].m_nParnt = nParnt;
      if (nBChd != END) rvoNod[nBChd].m_nRSib = nChd;
      if (!j)          rvoNod[nParnt].m_nLChd = nChd;
      nBChd = nChd;
    }
  }
}

void fnSetDepth(vector<StNod> &rvoNod, int nx, int nDept)
{
  rvoNod[nx].m_nDept = nDept;
  
  if (rvoNod[nx].m_nRSib != END)
    fnSetDepth(rvoNod, rvoNod[nx].m_nRSib, nDept);

  if (rvoNod[nx].m_nLChd != END)
    fnSetDepth(rvoNod, rvoNod[nx].m_nLChd, nDept + 1);
}

void fnRtdTree(vector<StNod> &rvoNod)
{
  int nx = 0, nDept = 0;
  while (rvoNod[nx].m_nParnt != END)
    nx = rvoNod[nx].m_nParnt;

  fnSetDepth(rvoNod, nx, nDept);
}

void fnResult(const vector<StNod> &cnrvoNod)
{
  for (int i = 0; i < cnrvoNod.size(); i++)
  {
    if (cnrvoNod[i].m_nDept == END)  break;
    cout << "node " << i << ": parent = " << cnrvoNod[i].m_nParnt << ", depth = " << cnrvoNod[i].m_nDept;

    if (!cnrvoNod[i].m_nDept)            cout << ", root, ";
    else if (cnrvoNod[i].m_nLChd == END) cout << ", leaf, ";
    else                                 cout << ", internal node, ";

    cout << "[";
    if (cnrvoNod[i].m_nLChd != END)
    {
      int nx = cnrvoNod[i].m_nLChd;
      cout << nx;
      while (cnrvoNod[nx].m_nRSib != END)
      {
        cout << ", " << cnrvoNod[nx].m_nRSib;
        nx = cnrvoNod[nx].m_nRSib;
      }
    }
    cout << "]" << endl;
  }
}

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

  vector<StNod> voNod;

  fnInput(voNod);
  fnRtdTree(voNod);
  fnResult(voNod);
  return 0;
}

Measured memory_bytes: 5744
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>

using namespace std;

int main(int argc, char const *argv[]) {
    int num[3];
    for(int i=0;i<3;i++)cin >> num[i];

    int min = 1000;
    int sum = 0;
    //int diff[2]={0};
    for(int i=0;i<2;i++){
        sum += abs(num[i+1] - num[i]);
    }
    if(sum < min)min = sum;
    swap(num[1],num[2]);
    sum = 0;
    for(int i=0;i<2;i++){
        sum += abs(num[i+1] - num[i]);
    }
    if(sum < min)min = sum;
    swap(num[0],num[1]);
    sum = 0;
    for(int i=0;i<2;i++){
        sum += abs(num[i+1] - num[i]);
    }
    if(sum < min)min = sum;
    swap(num[1],num[2]);
    sum = 0;
    for(int i=0;i<2;i++){
        sum += abs(num[i+1] - num[i]);
    }
    if(sum < min)min = sum;
    swap(num[0],num[1]);
    sum = 0;
    for(int i=0;i<2;i++){
        sum += abs(num[i+1] - num[i]);
    }
    if(sum < min)min = sum;
    swap(num[1],num[2]);
    sum = 0;
    for(int i=0;i<2;i++){
        sum += abs(num[i+1] - num[i]);
    }
    if(sum < min)min = sum;
    cout <<  min << endl;
    // for(int i=0;i<3;i++)cout << num[i] << " ";
    // cout << endl;
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#define MOD 1000000007
long int DP[100001];

long int dp(long int n);

int main(void){
 	int N,M,x;
    scanf("%d",&N);
    scanf("%d",&M);
    for(int i=0;i<100001;i++){
    	DP[i] = -1; 
    }
    for(int i=0;i<M;i++){
      scanf("%d",&x);
      DP[x] = 0;
    } 
    DP[0] =1;
  printf("%ld",dp(N));
}

long int dp(long int n){
 if(DP[n] >= 0 ) return DP[n];
 if(n > 1); return DP[n] = (dp(n-1) + dp(n-2))%MOD;
 if(n == 1) return 1;
};

Measured memory_bytes: 4096
phrases, melodies = [], []
n = gets.to_i
n.times{phrases << gets.chomp.split.map(&:to_i)}
m = gets.to_i
m.times{melodies << gets.to_i}
dp = [0]
phrases.uniq.each{|s, l, p|
    0.upto(392){|i|
        s.upto(l){|x|
            break if i + x > 393
            dp[i + x] = dp[i] + p if !dp[i].nil? && (dp[i + x].nil? || dp[i + x] < dp[i] + p)
        }
    }
}
result = []
melodies.each{|melody| result << dp[melody]}
puts (result.include?(nil) ? -1 : result)


Measured memory_bytes: 6888
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

double distanceAB(double xa, double ya, double xb, double yb);
void trace(int A[], int N);

/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/


#define MAX 10000
#define NIL -1

struct Node {int p, l, r;};
Node T[MAX];
int n;

void preParse(int u)
{
	if (u == NIL) return;
	printf(" %d", u);
	preParse(T[u].l);
	preParse(T[u].r);
}

void inParse(int u)
{
	if (u == NIL) return;
	inParse(T[u].l);
	printf(" %d", u);
	inParse(T[u].r);
}

void postParse(int u)
{
	if (u == NIL) return;
	postParse(T[u].l);
	postParse(T[u].r);
	printf(" %d", u);
}


int main()
{
	int i, v, l, r, root;
	
	cin >> n;
	REP(n) T[i].p = NIL;
	REP(n)
	{
		cin >> v >> l >> r;
		T[v].l = l;
		T[v].r = r;
		if (l != NIL) T[l].p = v;
		if (r != NIL) T[r].p = v;
	}
	
	REP(n) if (T[i].p == NIL) root = i;
	
	printf("Preorder\n");
	preParse(root);
	printf("\n");
	printf("Inorder\n");
	inParse(root);
	printf("\n");
	printf("Postorder\n");
	postParse(root);
	printf("\n");
	
	return 0;
}


void trace(int A[], int N)
{
	REP(N)
	{
		if (i > 0) cout << " ";
		cout << A[i];
	}
	cout << endl;
}


double distanceAB(double xa, double ya, double xb, double yb)
{
	return sqrt((xb-xa)*(xb-xa) + (yb-ya)*(yb-ya));
}

Measured memory_bytes: 1152
import numpy as np
n = int(input())
a_ls = list(map(int, input().split()))

sum_ls = list(np.cumsum(a_ls))
sum_ls.insert(0,0)

ans = float('inf')
for i in range(1,n):
    su = sum_ls[i]
    ara = sum_ls[-1] - sum_ls[i]
    dif = abs(su-ara)
    if dif < ans:
        ans = dif

print(ans)


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

s = sum(a)
for i in range(len(a)):
    a[i]*=n
a_diff = [abs(a[i]-s) for i in range(len(a))]
mi = min(a_diff)
#print(s)
#print(mi)
#print(a_diff)
for i in range(len(a_diff)):
    if a_diff[i] == mi:
        print(i)
        exit()


Measured memory_bytes: 3060
#include <iostream>
#include <queue>
#include <vector>
#include <utility>
using namespace std;

int N,M,R;
vector<pair<int,int>> map[201];
int rs[8];

long long pow(long long b, long long e){
    if(e==1)return b;
    if(e==0)return 1;
    return pow(b,e-1);
}
long long tet2(long long R){
    return pow(R,R);
}

int dijkstra(int a,int b){
    priority_queue<pair<int,int>> Q;
    bool checked[201];
    for(int i=1;i<=200;i++) checked[i]=false;

    int cur=a;
    checked[a]=true;
    for(pair<int,int> x : map[a]){
        Q.push(make_pair(-x.second,x.first));
    }
    while(!Q.empty()){
        pair<int,int> t=Q.top();
        Q.pop();

        if(checked[t.second]) continue;
        checked[t.second]=true;
        if(t.second==b){
            return (-t.first);
        }
        for(pair<int,int> x : map[t.second]){
            Q.push(make_pair(t.first-x.second,x.first));
        }
    }
    return 1000000000;
}

int up1(int i){
    return i<1?1:i;
}

int main(){
    cin >> N >> M >> R;
    for(int i=0;i<R;i++) cin >> rs[i];
    for(int i=0;i<M;i++){
        int a,b,c;
        cin >> a >> b >> c;
        map[a].push_back(make_pair(b,c));
        map[b].push_back(make_pair(a,c));
    }

    long long dist[8][8];
    for(int i=0;i<R;i++){
        for(int j=i+1;j<R;j++){
            long long di=dijkstra(rs[i],rs[j]);
            dist[i][j]=di;
            dist[j][i]=di;
        }
    }
    long long min=1000000000000000;
    int d[8],r[8];
    for(d[0]=0;d[0]<up1(R-0);d[0]++){
    for(d[1]=0;d[1]<up1(R-1);d[1]++){
    for(d[2]=0;d[2]<up1(R-2);d[2]++){
    for(d[3]=0;d[3]<up1(R-3);d[3]++){
    for(d[4]=0;d[4]<up1(R-4);d[4]++){
    for(d[5]=0;d[5]<up1(R-5);d[5]++){
    for(d[6]=0;d[6]<up1(R-6);d[6]++){
    for(d[7]=0;d[7]<up1(R-7);d[7]++){
        int ri[8]={0,1,2,3,4,5,6,7};
        for(int i=0;i<8;i++){
            r[i]=ri[d[i]];
            for(int j=d[i]+1;j<8;j++) ri[j-1]=ri[j];
        }

        long long len=0;
        for(int i=1;i<R;i++) len+=dist[r[i-1]][r[i]];
        if(len<min) min=len;
    }}}}}}}}

    cout << min << endl;

    return 0;
}

Measured memory_bytes: 1560
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <numeric>
#include <cmath>

using namespace std;

typedef long long int lld;

const lld mod = 1e9+7;
const lld INF = 1e9;
//const lld MAXN = 1e9;

int main()
{
	string s;
	int al[26] = {0};

	cin >> s;

	int n = s.size();

	for(int i = 0; i < n; i++)
	{
		al[s[i]-'a']++;
	}

	bool sol = true;
	for(int i = 0; i < 26; i++)
	{
		if (al[i] % 2 != 0)
		{
			sol = !sol;
			break;
		}
	}

	if (sol)
	{
		cout << "Yes" << endl;
	}else
	{
		cout << "No" << endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <bits/stdc++.h>
using namespace std;
int main() {
    double r;
    cin >> r;
    cout << fixed << setprecision(9);
    cout << r*r*M_PI << " " << 2*r*M_PI << endl;
	return 0;
}


Measured memory_bytes: 3292
#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define maxn 200000+66
int a[maxn];
bool cmp(int a,int b)
{
    return a<b;
}
int main()
{
    int h,k;
    while(cin>>h>>k)
    {
        for(int i=1;i<=h;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+h+1);
        ll s=0;
        for(int i=1;i<=h-k;i++)
        {
            s+=a[i];
        }
        cout<<s<<endl;
    }
}


Measured memory_bytes: 1024
// Try AtCoder
// author: Leonardone @ NEETSDKASU

fn main() {
	let mut stdin = String::new();
    std::io::Read::read_to_string(
    	&mut std::io::stdin(),
        &mut stdin).unwrap();
	let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    macro_rules! get {
    	($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(i64));
    }
    let n = get!(usize);
    let mut xs = vec![0; n+1];
    for _ in 0..n {
    	let x = get!(usize);
        xs[x] = xs[x-1] + 1;
    }
    let mut ans = 0;
    for &c in xs.iter() {
    	ans = std::cmp::max(ans, c);
    }
    println!("{}", n - ans);
}

Measured memory_bytes: 8444
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.nextLine());
        int[] h = new int[N];
        int[] dp = new int[N];
        int MAX_H = 10001;
        for (int i = 0; i < N; i++) {
            h[i] = Integer.parseInt(sc.next());
            dp[i] = MAX_H;
        }

        dp[0] = 0;
        dp[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < N; i++) {
            int min1 = dp[i - 1] + Math.abs(h[i - 1] - h[i]);
            int min2 = dp[i - 2] + Math.abs(h[i - 2] - h[i]);
            dp[i] = Math.min(min1, min2);
        }

        System.out.println(dp[N - 1]);
    }
}



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

using namespace std;

typedef vector<int> vi;
typedef long long ll;
#define all(c) begin(c),end(c)

ll f(ll a, ll b) {
    if (a <= b) {
        ll rem = b % a;
        ll d = b / a;
        if (rem == 0)
            return 2 * (d-1) * a + a;
        return 2 * d * a + f(a, rem);
    } else {
        ll rem = a % b;
        ll d = a / b;
        if (rem == 0)
            return 2 * (d-1) * b + b;
        return 2 * d * b + f(rem, b);
    }

}

auto solve() {
    ll N, X;
    cin >> N >> X;

    return N + f(N - X, X);
}

int main() {
    cout << solve() << endl;
    return 0;
}


Measured memory_bytes: 256
def main():
    K,A,B = map(int,input().split())
    if B-A<=2:
        print(1+K)
        exit()
    now = 1
    K -= (A-1)
    now = A
    if K%2==0:
        now += (B-A)*(K//2)
    else:
        now += (B-A)*((K-1)//2)
        now += 1
    print(now)  
if __name__ == '__main__':
    main()

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

int main(){
    int a,b;
    cin >> a >> b;
    if(a>8||b>8){
        cout << ":(";
    }else{
        cout << "Yay!";
    }
}

Measured memory_bytes: 256
#include <stdio.h>

int main()
{
  char c = getchar();
  switch(c){
    case 'a': printf("vowel"); break;
    case 'i': printf("vowel"); break;
    case 'u': printf("vowel"); break;
    case 'e': printf("vowel"); break;
    case 'o': printf("vowel"); break;
  default:
    printf("consonant"); break;
  }
  return 0;
}

Measured memory_bytes: 128
x, y = list(map(str, input().split()))
if x == 'A':
  x = 10
elif x == 'B':
  x = 11
elif x == 'C':
  x = 12
elif x == 'D':
  x = 13
elif x == 'E':
  x = 14
elif x == 'F':
  x = 15
if y == 'A':
  y = 10
elif y == 'B':
  y = 11
elif y == 'C':
  y = 12
elif y == 'D':
  y = 13
elif y == 'E':
  y = 14
elif y == 'F':
  y = 15
if x > y:
  print('>')
elif x == y:
  print('=')
else:
  print('<')

Measured memory_bytes: 9056
N = int(input())
A = [int(i) for i in input().split()]

ans = 0
n = 0

for i in range(N):
    if A[i] == n+1: 
        n += 1
        continue
    else: ans += 1
    
if n == 0:
    print(-1)
else:
    print(ans)    

Measured memory_bytes: 25768
#include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,x[100002],a,cnt,i,p;
	scanf("%d",&n);
	for(i=1;i<=n;i++)	{
		scanf("%d",&p);
		if(i==p)	x[i]=1;
		else x[i]=0;
	}
	x[n+1]=0;
	cnt=0;
	for(i=1;i<=n;i++)	{
		if(x[i]==1)	{
			x[i]--;	x[i+1]--;
			cnt++;
		}
	}
	printf("%d\n",cnt);
    return 0;
}

Measured memory_bytes: 512
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <queue>
#include <bitset>
#define MOD (1000000007l)
#define rep(i, n) for(long i = 0; i < (n); i++)

using namespace std;

void solve() {
  long N;
  cin >> N;
  string S;
  cin >> S;

  long result = 0;

  for (long diff = 1; diff <= S.size() - 1; diff++) {
    long max_tmp = 0;
    long tmp = 0;
    for (long i = 0; i < S.size() - diff; i++) {
      if (S[i] == S[i + diff]) tmp++;
      else {
        max_tmp = max(max_tmp, tmp);
        tmp = 0;
      }
    }
    max_tmp = max(max_tmp, tmp);
    tmp = 0;
    max_tmp = min(max_tmp, diff);

    result = max(result, max_tmp);
  }

  cout << result << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  solve();
  return 0;
}


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

int main(){
	ll n;
	cin >> n;
	if(n % 2 == 1){
		cout << 0 << endl;
		return 0;
	}
	ll k = n/2;
	ll m2 = k + (k/2 + 1) * (k/2) * 2;
	ll m5(0);
	int cnt(1);
	rep(i,100000){
		ll t = pow(5,cnt);
		if(k < t) break;
		cnt++;
		m5 += k/t;
	}
	ll ans = m5;
	if(ans > m2 && m2 > 0) ans = m2;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 384
(stdin => {
  // Define Function

  // Declare Variable
  const inputs = stdin.toString().trim().split('\n')
  const nlist = Array.from(inputs.slice(0, -1), Number)

  const result = nlist.map((v, i) => `Case ${i + 1}: ${v}`)
  console.log(result.join('\n'))
})(require('fs').readFileSync('/dev/stdin', 'utf8'))



Measured memory_bytes: 19988
#include<stdio.h>
int main(void)
{
    int n,i,a,b,c,z,q;
	while(1){
	    scanf("%d",&n);
		if(n==0) break;
		z=0;
		for(i=0;i<n;i++){
		    scanf("%d %d %d",&a,&b,&c);
			if(z<b+c){
			    z=b+c;
				q=a;
			}
		}
		printf("%d %d\n",q,z);
	}
	return 0;
}

Measured memory_bytes: 2680
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long int
#define mod 1000000007
#define vi vector<ll>
#define pb push_back
#define mp make_pair
#define str string
#define sp(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) ll x; cin>>x; while(x--)
#define endl '\n'
void kehsihba() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
int32_t main(){
	//kehsihba();
	ll t=1;
	//cin>>t;
	while(t--){
		ll a,b,c,k;
		cin>>a>>b>>c>>k;
		ll ans=0;
		if(k>a){
			ans+=a;
			k-=a;
		}
		else{
			cout<<k;return 0;
		}

		if(k>b){
			k-=b;
		}
		else{
			cout<<ans;return 0;
		}

		cout<<ans-k;
	}
}

Measured memory_bytes: 3568
// diverta2019_2_c
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

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

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))
#define ALL(x) x.begin(), x.end()

#define INF ((1 << 30)-1)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
template<class T> void print(const T& x){cout << x << endl;}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
//struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    int N;
    scanf("%d", &N); //cin >> N;

    vector<int> A(N);
    REP(i, N) scanf("%d", &A[i]); // cin >> A[i];
    sort(ALL(A));

    int sm = accumulate(ALL(A), 0);

    int ans = -INF;
    int split_idx = -1;
    int tmp_sm = 0;
    REP(i, N-1){
        tmp_sm += A[i];
        if (sm - 2 * tmp_sm >= ans) {
            ans = sm - 2 * tmp_sm;
            split_idx = i;
        }
    }

    print(ans);

    int x = A[0];
    REPN(i, split_idx+1, N-1){
        print(x, A[i]);
        x -= A[i];
    }

    print(A[N-1], x);
    x = A[N-1] - x;

    REPN(i, 1, split_idx+1){
        print(x, A[i]);
        x -= A[i];
    }


    return 0;
}


Measured memory_bytes: 2304
line = input().split(" ")
a = list(map(int, line))
ans = 0
if a[0] == 1 and a[1] != a[2]:
    ans = 0
elif a[0] == 1 and a[1] == a[2]:
    ans = 1
elif a[1] > a[2]:
    ans = 0
else:
    ans = (a[0] - 2) * (a[2] - a[1]) + 1
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;
using P = pair<int,int>;

int main(){
  int n;
  cin >> n;
  vector<string> s(n);
  rep(i,n){
    cin >> s[i];
    sort(s[i].begin(),s[i].end());
  }
  sort(s.begin(),s.end());
  ll count = 1;
  ll ans = 0;
  rep(i,n-1){
    if(s[i] == s[i+1]) ++count;
    else{
      ans += (count)*(count-1)/2;
      count = 1;
    }
    if(i == n-2){
      ans += (count)*(count-1)/2;
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 7296
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def ifn(): return float(input()) #浮動小数点読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def fmn(): return map(float, input().split()) #浮動小数点map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, input().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD

H,W = map(lambda x: int(x)-1, input().split())
s = [input() for _ in range(H+1)]

d = [[-1 for _ in range(W+1)] for _ in range(H+1)]
d[0][0] = 0

queue = deque([(0,0)])

dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]

while len(queue) > 0:
    point = queue.popleft()

    if point[1] == H and point[0] == W: break 
    for i in range(4):
        x = point[0] + dx[i]
        y = point[1] + dy[i]

        if x < 0 or y < 0 or x > W or y > H or s[y][x] == '#': continue
        if d[y][x] != -1: continue
        d[y][x] = d[point[1]][point[0]] + 1
        queue.append((x,y))

if d[H][W] == -1:
    print(-1)
else:
    white_count = 0
    for i in range(H+1):
        for j in range(W+1):
            if s[i][j] == '.':
                white_count += 1
    print(white_count - (d[H][W]+1))

Measured memory_bytes: 5548
#include <stdio.h>
#include <inttypes.h>

int N;
int64_t T;
int Q;

int64_t A[114514];
int D[114514];

int X[1024];

int scNum;
int64_t stopPoints[114514];

int main(void) {
	int i;
	if (scanf("%d%" SCNd64 "%d", &N, &T, &Q) != 3) return 1;
	for (i = 1; i <= N; i++) {
		if (scanf("%" SCNd64 "%d", &A[i], &D[i]) != 2) return 1;
	}
	for (i = 0; i < Q; i++) {
		if (scanf("%d", &X[i]) != 1) return 1;
	}
	for (i = 1; i < N; i++) {
		if (D[i] == 1 && D[i + 1] == 2) {
			stopPoints[scNum++] = A[i] + (A[i + 1] - A[i]) / 2;
		}
	}
	for (i = 0; i < Q; i++) {
		int idx = X[i];
		if (D[idx] == 1) {
			if (stopPoints[scNum - 1] < A[idx]) {
				printf("%" PRId64 "\n", A[idx] + T);
			} else {
				int left = 0, right = scNum - 1;
				int64_t sumipe;
				if (A[idx] < stopPoints[0]) {
					right = 0;
				} else {
					while (left + 1 < right) {
						int mid = left + (right - left) / 2;
						if (A[idx] < stopPoints[mid]) right = mid; else left = mid;
					}
				}
				sumipe = stopPoints[right] - A[idx];
				if (T < sumipe) sumipe = T;
				printf("%" PRId64 "\n", A[idx] + sumipe);
			}
		} else {
			if (A[idx] < stopPoints[0]) {
				printf("%" PRId64 "\n", A[idx] - T);
			} else {
				int left = 0, right = scNum - 1;
				int64_t sumipe;
				if (stopPoints[scNum - 1] < A[idx]) {
					left = scNum - 1;
				} else {
					while (left + 1 < right) {
						int mid = left + (right - left) / 2;
						if (A[idx] < stopPoints[mid]) right = mid; else left = mid;
					}
				}
				sumipe = A[idx] - stopPoints[left];
				if (T < sumipe) sumipe = T;
				printf("%" PRId64 "\n", A[idx] - sumipe);
			}
		}
	}
	return 0;
}



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

def dist(c,d):
    return ((c[0] - d[0])**2 + (c[1] - d[1])**2)**(1/2)

s = 0
for i in range(N):
    for j in range(i+1,N):
        s += dist(A[i],A[j])

print(2*s/N)

Measured memory_bytes: 3060
require "set"

n = gets.to_i
if n == 1
  puts 1
  exit 0
end

set = Set.new
xy = Array.new(n) do
  t = gets.strip.split(" ").map(&:to_i)
  set << "#{t[0]}x#{t[1]}" 
  t
end

diff = []

0.upto(n - 2) do |i|
  (i + 1).upto(n - 1) do |j|
    diff << [xy[j][0] - xy[i][0], xy[j][1] - xy[i][1]]
    diff << [xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]]
  end
end

cost0 = 0

diff.each do |(p, q)|
  c = 0
  xy.each do |(x, y)|
    c += 1 if set.member?("#{x + p}x#{y + q}")
  end
  cost0 = c if c > cost0
end

puts n - cost0


Measured memory_bytes: 2428
import java.util.*;

public class Main {

    public static void main(String[] args) {
        //Compute c1 = new Compute();//calc

        int count = 0;
        boolean sw = false;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        double dMax = Double.MIN_VALUE;
        double dMin = Double.MAX_VALUE;
        //StringBuilder answer=new StringBuilder();

        //input
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int x=sc.nextInt();

        int start = x-k;

        int last = x+k;

        for (int i=x-k+1;i<x+k;++i)
        {
            System.out.println(i);
        }

        //output
        //System.out.println(String.format("%.0f",answer));
    }
}

Measured memory_bytes: 22100
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> istream& operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream& operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream& operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream& operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream& operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream& operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream& operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream& operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> void Max(T &m, const T q) { if (m < q) m = q; }
template<typename T> void Min(T &m, const T q) { if (m > q) m = q; }
template<typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

lint N;
vector<lint> A;

void NO()
{
    puts("NO");
    exit(0);
}

int main()
{
    cin >> N;
    A.resize(N);
    cin >> A;
    lint su = accumulate(A.begin(), A.end(), 0LL);
    lint times = su * 2 / N / (N + 1);
    if (times * N * (N + 1) / 2 != su) NO();

    lint cou = 0;
    REP(i, N)
    {
        lint t = -A[(i + 1) % N] + A[i] + times;
        if (t < 0 || t % N) NO();
        cou += t / N;
    }
    if (cou != times) NO();
    puts("YES");
}


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

using namespace std;

const int inf = 1000000000; // 10^9

int main()
{
	int n, res;
	cin >> n;
	int arr[n];
	int ans = inf;
	for(int i = 0 ;i < n;i++)
	{
		cin >> arr[i];
	}

	for(int i = 1; i <= 100; i++)
	{
		int cost = 0;
		for(int j = 0 ;j < n;j++)
		{
			cost += (arr[j] - i) * (arr[j] - i);

		}
		ans = min(ans, cost);
	}

	cout << ans << endl;


}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
//const int mod = ;

bitset<100000> was, nwas;

int main() {
  std::ios::sync_with_stdio(false); std::cin.tie(0);
  int n;
  cin >> n;
  while (n % 2 == 0) n /= 2;
  while (n % 5 == 0) n /= 5;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  vi r(1, 1);
  int cur = 10 % n;
  while (cur != 1) {
    r.push_back(cur);
    cur = cur * 10 % n;
  }
  for (int i : r) was.set(i);
  for (int i : r) if (was.test(n-i)) {
    cout << 2 << endl;
    return 0;
  }
  for (int tt = 0; tt < 45; ++tt) {
    nwas.reset();
    for (int a : r) {
      nwas |= was << a;
      nwas |= was >> (n - a);
    }
    was = nwas;
    if (was.test(0)) {
      cout << tt + 2 << endl;
      return 0;
    }
  }
  return 0;
}

Measured memory_bytes: 892
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int> 
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fi first
#define se second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
int n,d,cnt;
int x[20][20];
int squ[100001];
int main(){
    cin>>n>>d;
    for(int i=0;i*i<=100000;i++)
        squ[i*i]=1;
    for(int i=0;i<n;i++)
        for(int j=0;j<d;j++)
            cin>>x[i][j];
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++){
            int ans=0;
            for(int k=0;k<d;k++)
                ans+=(x[i][k]-x[j][k])*(x[i][k]-x[j][k]);
            if(squ[ans])
                cnt++;
        }
    cout<<cnt<<endl;
    return 0;
}

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

int main(){
  int N,M;
  int P,X;
  int sum =0;
  cin >> N;
  vector <int> T(N);
  for(int i = 0; i < N; i++){
    cin >> T.at(i);
  }
  cin >> M;
  for(int i = 0; i < M; i++){
    cin >> P >> X;
    P--;
    for(int k = 0; k < N; k++){
    	if(k != P) sum += T.at(k);
    	else	sum += X;
    }
    cout << sum << endl;
    sum = 0;
  }
  

}

Measured memory_bytes: 3560
#include <bits/stdc++.h>
#include <random>
#include <unistd.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector <ll> vi;
typedef vector <ll> vll;
typedef vector <ull> vull;
typedef vector <vll> vvll;
typedef long double ld;
typedef vector <ld> vld;
typedef pair <ll, ll> pii;
typedef pair <ll, ll> pll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef vector <vpii> vvpii;
typedef vector <bool> vbool;

#define forn(i, x, y) for (ll i = x; i < y; ++i)
#define rforn(i, x, y) for (ll i = x; i >= y; --i)
#define add push_back
#define del pop_back
#define ins insert
#define ers erase
#define F first
#define S second
#define int ll
#define _ inline
#define sqr(x) ((x) * (x))
#define pw(x) (1LL << (x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rev(x) reverse(x)
#define sz(x) (int)(x).size()
#define rsz resize
#define cpy(x, T, c) copy(all(x), ostream_iterator <T>(cout, c))
#define mem(x, val) memset(x, val, sizeof(x))
_ ll gcd(ll a, ll b) { while (b) { a %= b, swap(a, b); } return a; }
_ ll lcm(ll a, ll b) { return a * b / max(1LL, gcd(a, b)); }

const ll INF = (ll)1e9 + 2017;
const ll MOD = (ll)1e9 + 7;
const ld EPS = 1e-10;
const ld PI = acos(-1.0);
mt19937 rnd((int)time(0));

const int N = 55;
char f[N][N];
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

void source()
{
    int n, m;
    cin >> n >> m;
    forn(i, 0, n)
    {
        forn(j, 0, m)
        {
            cin >> f[i][j];
        }
    }
    forn(i, 0, n)
    {
        forn(j, 0, m)
        {
            if (f[i][j] == '#')
            {
                continue;
            }
            f[i][j] = '0';
            forn(k, 0, 8)
            {
                if (i + dy[k] >= 0 && i + dy[k] < n && j + dx[k] >= 0 && j + dx[k] < m && f[i + dy[k]][j + dx[k]] == '#')
                {
                    f[i][j]++;
                }
            }
        }
    }
    forn(i, 0, n)
    {
        forn(j, 0, m)
        {
            cout << f[i][j];
        }
        cout << endl;
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout.setf(ios_base::fixed, ios_base::floatfield);
    cout.precision(10);
#ifdef _F1A4X_
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    source();
#ifdef _F1A4X_
    cerr << endl << endl << "\t" << (ld)clock() / CLOCKS_PER_SEC << " sec" << endl;
    sleep(INF);
#endif
}

Measured memory_bytes: 256
l = input()
M = 1000000007
d = len(l)

def modpow2(n):
    if n == 0:
        return 1 
    elif n % 2 == 0:
        return (modpow2(n // 2) ** 2) % M
    else:
        return ((modpow2(n // 2) ** 2) * 2) % M

answer = 3
c = 1
for i in range(1, d):
    if l[i] == '1':
        c += 1
        answer = (answer * 3) % M    
    else:
        answer = ((answer * 3) - modpow2(c + 1)) % M
print(answer)



Measured memory_bytes: 3316
#include<iostream>
#include<vector>
using namespace std;
// GREEDY
int main(){
    int n;
    cin>>n;
    vector<long long>A(n+1);
    vector<long long>B(n);
    //long long B[11000];
    for(int i=0;i<=n;i++){
        cin>>A[i];
    }
    for(int i=0;i<n;i++){
        cin>>B[i];
    }
    long long sum = 0;
    for(int i=0;i<n;i++){
        long long val = min(A[i],B[i]);
        sum += val;
        B[i]-= val;
        long long next = min(A[i+1],B[i]);
        sum += next;
        A[i+1] -= next;
        
    }
    cout<<sum<<endl;
    return 0;
}

Measured memory_bytes: 1792
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <numeric>

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

using namespace std;


int main(){
    string s;
    cin >> s;
    rep(i, s.size()){
        if (i%2 == 0){
            if (s[i]=='L'){
                cout << "No" << endl;
                return 0;
            }
        }else{
            if (s[i]=='R'){
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;

    return 0;
}

Measured memory_bytes: 256
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

a, b, c, d, e = list(map(int, input().split()))

print((c-a)*60 + (d-b) - e)


Measured memory_bytes: 9736
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    n=int(input())
    nn=str(n)
    cnt=0
    for i in nn:
        cnt+=int(i)
    if n%cnt==0:
        print('Yes')
    else:
        print('No')
    
resolve()

Measured memory_bytes: 38256
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<(n);i++)
#define rep2(i,a,n) for(int i=a;i<n;i++)
#define been(ix) (ix).begin(),(ix).end()
using namespace std;
typedef long long ll; 
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001; 
typedef pair<int,int> P;
#define foreach(ix,i) for(auto& (ix) : (i))
typedef long double ld;
const int mod =1000000007;
const int PI = acos(-1);
int main(){
	string s;cin>>s;
	if(s.length()%2){
				cout<<"No"<<endl;
				return 0;
	}
	rep(i,s.length()){
		if(i%2==0){
			if(s[i]!='h'){
				cout<<"No"<<endl;
				return 0;
			}
		}
		else{
			if(s[i]!='i'){
				cout<<"No"<<endl;
				return 0;
			}
		}
	}
	cout<<"Yes"<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<utility>
#include<queue>
#define mod 998244353
#define pb push_back
#define ll long long
using namespace std;
int main(){
	int n,k,dist[2018],ans=10000,a[2018],b[2018];
	vector<int> pat[2018];
	cin>>n>>k;
	for(int i=0;i<n-1;i++){
		cin>>a[i]>>b[i];
		pat[a[i]].pb(b[i]);
		pat[b[i]].pb(a[i]);
	}
	if( k%2 == 0){
		for(int i=1;i<=n;i++){
			int cnt=0;
			for(int j=1;j<=n;j++){
				if(i==j)dist[j]=0;
				else dist[j]=10000;
			}
			queue<int> que;
			que.push(i);
			while(!que.empty()){
				int cur = que.front();
				que.pop();
				for(int j=0;j<pat[cur].size();j++){
					if(dist[pat[cur][j]] > dist[cur]+1){
						dist[pat[cur][j]] = dist[cur]+1;
						que.push(pat[cur][j]);
					}
				}
			}
			for(int j=1;j<=n;j++){
				if(dist[j]>k/2)cnt++;
			}
			if(cnt < ans) ans = cnt;
		}
	 }
	 else{
		for(int i=0;i<n-1;i++){
			int cnt=0;
			for(int j=1;j<=n;j++){
				if(j==a[i] || j==b[i])dist[j]=0;
				else dist[j]=10000;
			}
			queue<int> que;
			que.push(a[i]);
			que.push(b[i]);
			while(!que.empty()){
				int cur = que.front();
				que.pop();
				for(int j=0;j<pat[cur].size();j++){
					if(dist[pat[cur][j]] > dist[cur]+1){
						dist[pat[cur][j]] = dist[cur]+1;
						que.push(pat[cur][j]);
					}
				}
			}
			for(int j=0;j<=n;j++){
				if(dist[j]>k/2)cnt++;
			}
			if(cnt < ans) ans = cnt;
		}
	}
	cout<<ans<<endl;
	return 0;
}

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

int main() {
	int N,c=0,r=0;
	string S;
	cin >> N >> S;

	rep(i,S.size())
		if(S.at(i)=='I')
			r = max(r, ++c);
		else
			--c;
	cout << r << endl;
}

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

using namespace std;

const long long int oo = 987654321987654L;
const int N = 110;
const int MAX = 1010;

int n, tot, cw, cv;
vector<int> w, v;

long long int memo[N][N*MAX];
long long int dp(int at, int sum) {
  	if(sum < 0) return oo;
	if(at == n) return sum == 0 ? 0 : oo;
  	if(memo[at][sum] != -1) return memo[at][sum];
  	long long int choose = w[at] + dp(at + 1, sum - v[at]);
  	long long int skip = dp(at + 1, sum);
  	return memo[at][sum] = min(choose, skip);
}

int main() {
	cin >> n >> tot;
  	for(int i = 0; i < n; ++i) {
		cin >> cw >> cv;
      	w.push_back(cw);
      	v.push_back(cv);
      	fill(memo[i], memo[i] + N * MAX, -1);
    }
  	int ans = 0;
  	for(int i = n * MAX; i >= 0; --i) {
    	long long int minWeight = dp(0, i);
      	if(minWeight <= tot) {
          	ans = i;
          	break;
        }
    }
  	cout << ans << endl;
	return 0; 
}


Measured memory_bytes: 88448
#include <bits/stdc++.h> 
using namespace std; 
const int MAX_N = 505; 
int N, M; 
char a[MAX_N][MAX_N], b[MAX_N][MAX_N], c[MAX_N][MAX_N]; 

int main () { 
	scanf("%d %d", &N, &M); 
	for (int i = 1; i <= N; i++) scanf("%s", a[i] + 1);
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= M; j++) b[i][j] = c[i][j] = a[i][j]; 
	for (int i = 1; i <= N; i += 2) 
		for (int j = 1; j < M; j++) b[i][j] = '#'; 
	for (int i = 1; i <= N; i++) b[i][1] = '#'; 
	for (int i = 2; i <= N; i += 2) 
		for (int j = 2; j <= M; j++) c[i][j] = '#'; 
	for (int i = 1; i <= N; i++) c[i][M] = '#'; 
	for (int i = 1; i <= N; i++, putchar('\n')) 
		for (int j = 1; j <= M; j++) putchar(b[i][j]); 
	putchar('\n'); 
	for (int i = 1; i <= N; i++, putchar('\n')) 
		for (int j = 1; j <= M; j++) putchar(c[i][j]); 
    return 0; 
} 

Measured memory_bytes: 4444
import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = println(solve(new Scanner(System.in)))

  def solve(in: String): String = solve(new Scanner(in))

  def solve(sc: Scanner): String = {
    val H = sc.nextInt
    val W = sc.nextInt
    val S = Array.fill(H)(sc.next.toCharArray)

    for (h <- 0 until H; w <- 0 until W) {
      if (S(h)(w) == '#') {
        if (!Array((-1, 0), (1, 0), (0, -1), (0, 1)).exists { case (dh, dw) =>
          h + dh >= 0 && h + dh < H && w + dw >= 0 && w + dw < W && S(h + dh)(w + dw) == '#'
        }) {
          return "No"
        }
      }
    }
    "Yes"
  }
}


Measured memory_bytes: 25664
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long int ll;

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

void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

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

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

int main() {
  int n;
  cin >> n;
  n = n % 10;
  if (n == 3) {
    P("bon");
  } else if (n == 0 || n == 1 || n == 6 || n == 8) {
    P("pon");
  } else {
    P("hon");
  }
  return 0;
}

Measured memory_bytes: 3600
n = int(input())
taro, hanako = 0, 0

for _ in range(n):
    t, h = input().split()
    
    if t == h:
        taro += 1
        hanako += 1
    elif t < h:
        hanako += 3
    else:
        taro += 3

print(taro, hanako)


Measured memory_bytes: 5600
/*
　　　　　／＞　 フ
　　　　　| 　_　 _|
　 　　　／`ミ _x 彡
　　 　 /　　　 　 |
　　　 /　  ヽ　　 ?
　／￣|　　 |　|　|
　| (￣ヽ＿_ヽ_)_)
　＼二つ
 */

#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define ll long long
#define fi first
#define se second
using namespace std;

template <typename T>
void read(T &x) {
    x = 0; bool f = 0;
    char c = getchar();
    for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
    for (;isdigit(c);c=getchar()) x=x*10+(c^48);
    if (f) x=-x;
}

template<typename F>
inline void write(F x, char ed = '\n') {
	static short st[30];short tp=0;
	if(x<0) putchar('-'),x=-x;
	do st[++tp]=x%10,x/=10; while(x);
	while(tp) putchar('0'|st[tp--]);
	putchar(ed);
}

template <typename T>
inline void Mx(T &x, T y) { x < y && (x = y); }

template <typename T>
inline void Mn(T &x, T y) { x > y && (x = y); }

const int N = 300500;
int h[N], ne[N<<1], to[N<<1], tot;
void add(int x, int y) { ne[++tot] = h[x], to[h[x] = tot] = y; }
inline void adde(int x, int y) { add(x, y), add(y, x); }

int vis[N], siz[N], cnt;
void dfs(int x) {
	siz[vis[x]]++;
	for (int i = h[x]; i; i = ne[i]) {
		int y = to[i]; if (vis[y]) continue;
		vis[y] = vis[x], dfs(y);
	}
}

int x[N], y[N], p[N], st[N], tp, n;
int main() {
	read(n);
	for (int i = 1;i <= n; i++)
		read(x[i]), read(y[i]), p[x[i]] = i;
	for (int i = 1;i <= n; i++) {
		if (!tp || y[p[i]] < y[st[tp]]) { st[++tp] = p[i]; continue; }
		int tmp = st[tp];
		while (tp && y[p[i]] >= y[st[tp]]) adde(p[i], st[tp]), tp--;
		st[++tp] = tmp;
	}
	for (int i = 1;i <= n; i++) {
		if (!vis[i]) vis[i] = ++cnt, dfs(i);
		write(siz[vis[i]]);
	}
	return 0;
}

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

string S,T;
set<char> A[256],B[256];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>S>>T;
	FOR(i,S.size()) {
		A[S[i]].insert(T[i]);
		B[T[i]].insert(S[i]);
	}
	
	FOR(i,256) {
		if(A[i].size()>1) return _P("No\n");
		if(B[i].size()>1) return _P("No\n");
	}
	cout<<"Yes"<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


Measured memory_bytes: 916
#include <stdio.h>

int count_digit(int n) {
	int count = 0;

	while(n != 0) {
		n /= 10;
		count++;
	}

	return count;
}

int main() {
	int a, b;

	while(scanf("%d %d", &a, &b) != EOF) {
		printf("%d\n", count_digit(a + b));
	}

	return 0;
}

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

int main() {
  int n, d;
  cin >> n >> d;
  vector<double> x(n);
  vector<double> y(n);
  int c = 0;
  
  for(int i = 0;i < n;i++) {
    cin >> x.at(i);
    cin >> y.at(i);
    double a = (x.at(i) * x.at(i)) + (y.at(i) * y.at(i));
    double s = sqrt(a);
    if(s <= d) 
      c++;
  }
  cout << c << endl;
}


Measured memory_bytes: 6420