akhauriyash/Code-Regression huggingface dataset:
def cin
  gets.split.map(&:to_i)
end

n, k = cin
p = cin

s = Array.new(n + 1, 0)
p.each_with_index do |a, i|
  s[i + 1] = s[i] + a
end

ans = s[k]
0.upto(n - k).each do |i|
  sum = s[i + k] - s[i]
  ans = sum if sum > ans
end

puts (ans + k) / 2.0

Measured memory_bytes: 16516
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

ll n, m;
vl visited;

void dfs(const vvl &G, ll v) {
    visited[v] = 1;
    rep(u, n) {
        if(visited[u]) {
            continue;
        }
        if(G[v][u]) {
            dfs(G, u);
        }
    }
}

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

    vvl G(n, vl(n));
    vl a(m), b(m);
    rep(i, m) {
        cin >> a[i] >> b[i];
        a[i]--;  b[i]--;
        G[a[i]][b[i]] = G[b[i]][a[i]] = 1;
    }

    ll ans = 0;
    rep(i, m) {
        G[a[i]][b[i]] = G[b[i]][a[i]] = 0;
        visited.assign(n, 0);
        dfs(G, 0);
        rep(j, n) {
            if(!visited[j]) {
                ans++;
                break;
            }
        }
        G[a[i]][b[i]] = G[b[i]][a[i]] = 1;
    }

    out(ans);
    re0;
}

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

const int MAXN = 6005;

int n, m;
long long C[MAXN][MAXN], dp[MAXN][MAXN];
long long d2[MAXN], d3[MAXN];

int main(){
	scanf("%d %d", &n, &m);
	n *= 3;
	
	for(int i = 0; i <= n; i++)
	for(int j = 0; j <= n; j++){
		if(j == 0)C[i][j] = 1;
		else if(i == 0)C[i][j] = 0;
		else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % m;
	}
	
	for(int i = 0; i <= n; i++){
		if(i == 0){
			d2[i] = 1;
			d3[i] = 1;
		}else{
			d2[i] = d3[i] = 0;
			if(i >= 2){
				d2[i] = (i - 1) * d2[i - 2] % m;
			}
			if(i >= 3){
				d3[i] = 2 * C[i - 1][2] * d3[i - 3] % m;
			}
		}
	}
	
	long long res = 0;
	
	for(int a = 0; a * 3 <= n; a++)
	for(int b = 0; a * 3 + b * 2 <= n; b++){
		if(n - a * 3 - b * 2 < b)continue;
		
		//cout << a << ' ' << b << ' ' <<  C[n][a * 3] * C[n - a * 3][b * 2] % m * d3[a * 3] % m * d2[b * 2] % m << endl;
		res += C[n][a * 3] * C[n - a * 3][b * 2] % m * d3[a * 3] % m * d2[b * 2] % m;
		res %= m;
	}
	
	printf("%lld\n", res);
	
	return 0;
}


Measured memory_bytes: 283136
import sys

N = int(sys.stdin.readline().strip())

def L(i, cache={}):
    if i == 0:
        return 2
    if i == 1:
        return 1
    if i >= 2:
        if i in cache:
            return cache[i]
        r = L(i-2) + L(i-1)
        cache[i] = r
        return r

print(L(N))


Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
const int mod = 1e9 + 7;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
using Point = complex<double>;
double dot(Point a, Point b) {return real(conj(a) * b);}
double cross(Point a, Point b) {return imag(conj(a) * b);}

int main() {
    int n; cin >> n;
    while (n--) {
        double xa, ya, xb, yb, xc, yc, xd, yd;
        cin >> xa >> ya >> xb >> yb >> xc >> yc >> xd >> yd;
        //cout << cross(Point(xa - xb, ya - yb), Point(xc - xd, yc - yd)) << endl;
        if (abs(cross(Point(xa - xb, ya - yb), Point(xc - xd, yc - yd))) < 1e-12) {
            cout << "YES" << endl;
        } else cout << "NO" << endl;
    }
}



Measured memory_bytes: 3180
//
//  main.cpp
//  B - I miss you...
//
//  Created by Hashizo on 2020/02/09.
//  Copyright © 2020 Hashizo. All rights reserved.
//

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <regex>
#include <cmath>

using namespace std;
 
#undef LONG_MAX
#undef LONG_MIN
 
#define LONG_MAX    9223372036854775807
#define LONG_MIN    (-LONG_MAX  -1)
 
#define ll long long
 
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
 
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(v) reverse((v).begin(), (v).end())


int main(int argc, const char * argv[]) {

	string s;
	cin >> s;


	string ans;
	REP(i,s.size()) {
		ans += "x";
	}

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


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

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (int i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 5e5;
int n, ql, qr, q[nsz + 5];
num c, h[nsz + 5], dp[nsz + 5];

num inline nu(int i, int j) {
    return dp[i] + h[i] * h[i] - dp[j] - h[j] * h[j];
}

num inline de(int i, int j) {
    return h[i] - h[j];
}

bool inline cmp(num a, num b, num c, num d) {
    return a * d > b * c;
}

int main() {
    scanf("%d%lld", &n, &c);
    cont (i, n) {
        scanf("%lld", &h[i]);
    }
    ql = qr = 0;
    q[qr++] = 1;
    circ (i, 2, n) {
        num cur = 2 * h[i];
        for (; qr - ql > 1 && !cmp(nu(q[ql + 1], q[ql]), de(q[ql + 1], q[ql]), cur, 1); ++ql);
        int fm = q[ql];
        dp[i] = dp[fm] + (h[i] - h[fm]) * (h[i] - h[fm]) + c;
        for (; qr - ql > 1 && !cmp(nu(i, q[qr - 1]), de(i, q[qr - 1]), nu(q[qr - 1], q[qr - 2]), de(q[qr - 1], q[qr - 2])); --qr);
        q[qr++] = i;
    }
    printf("%lld\n", dp[n]);
}


Measured memory_bytes: 8320
#include <iostream>
#include <utility>

using namespace std;

struct Card
{
    char pic;
    unsigned int val;
    int order;
};

void quick_sort(Card *, int, int);

int partition(Card *cards, int l, int r)
{
    Card pivot = cards[r];
    int i = l - 1;

    for (int j = l; j < r; j++) {
        if (cards[j].val <= pivot.val) {
            i++;
            swap(cards[i], cards[j]);
        }
    }
    swap(cards[++i], cards[r]);
    
    return i;
}

void quick_sort(Card *cards, int l, int r)
{
    if (l < r) {
        int p = partition(cards, l, r);
        quick_sort(cards, l, p - 1);
        quick_sort(cards, p + 1, r);
    }
}

void print_cards(Card *cards, int n)
{
    for (int i = 0; i < n; i++) {
        cout << cards[i].pic << " " << cards[i].val << endl;
    }

}

bool is_stable(Card *cards, int n)
{
    for (int i = 1; i < n; i++) {
        if (cards[i-1].val == cards[i].val && cards[i-1].order > cards[i].order)
            return false;
    }
    return true;
}

int main(void)
{
    int n;
    Card cards[100000];

    cin >> n;
    for (int i = 0; i < n; i++) {
        char pic;
        unsigned int val;
        cin >> pic >> val;
        cards[i] = Card{pic, val, i};
    }

    quick_sort(cards, 0, n-1);
    cout << (is_stable(cards, n) ? "Stable" : "Not stable") << endl;
    print_cards(cards, n);
    
    return 0;
}

Measured memory_bytes: 2332
num = input()
num1 = int(num[0] + num[1])
num2 = int(num[2] + num[3])

if(num1 < 13) and (num1 > 0):
    if(num2 < 13) and (num2 > 0):
        print("AMBIGUOUS")
    else:
        print("MMYY")
else:
    if(num2 < 13) and (num2 > 0):
        print("YYMM")
    else:
        print("NA")


Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
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; }
int main(){
    ios::sync_with_stdio(false);
    int h,w,ch,cw,dh,dw;
    cin >> h >> w>>ch>>cw>>dh>>dw;
    ch--;cw--;dh--;dw--;
    vector<vector<char>> s(h,vector<char>(w));
    rep(i,h) rep(j,w) cin >> s[i][j];
    int INF = INT32_MAX;
    vector<vector<int>> min_warp(h,vector<int>(w, INF));
    using P = pair<int, pair<int,int>>;
    priority_queue<P, vector<P>, greater<P>> pq;//座標、ワープ回数
    pq.emplace(make_pair(0,make_pair(ch,cw)));
    vector<int> walk_dx = {0,0,-1,1};
    vector<int> walk_dy = {1,-1,0,0};
    vector<int> warp_dx, warp_dy;
    for(int x = -2; x <= 2; x++) for(int y = -2; y <= 2; y++) {
        if(x == 0 && y == 0) continue;
        warp_dx.push_back(x);
        warp_dy.push_back(y);
    }
    min_warp[ch][cw] = 0;
    while(!pq.empty()) {
        int y = pq.top().second.first;
        int x = pq.top().second.second;
        int warp = pq.top().first;
        pq.pop();
        if(min_warp[y][x] < warp) continue;
        for(int i = 0; i < 4; i++) {
            int nx = x + walk_dx[i];
            int ny = y + walk_dy[i];
            if(nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
            if(min_warp[ny][nx] <= warp) continue;
            if(s[ny][nx] == '#') continue;
            min_warp[ny][nx] = warp;
            pq.emplace(make_pair(warp,make_pair(ny,nx)));
        }
        for(int i = 0; i < 24; i++) {
            int nx = x + warp_dx[i];
            int ny = y + warp_dy[i];
            if(nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
            if(min_warp[ny][nx] <= warp+1) continue;
            if(s[ny][nx] == '#') continue;
            min_warp[ny][nx] = warp+1;
            pq.emplace(make_pair(warp+1,make_pair(ny,nx)));
        }
    }
    if(min_warp[dh][dw] == INF) cout << -1 << endl;
    else cout <<  min_warp[dh][dw] << endl;
}


Measured memory_bytes: 8480
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,x) for(int i = 0; i < (int)(x); i++)
#define pb push_back
#define mp make_pair
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
const ll MOD = 1e9+7;
const int n_max = 1e5+10;

void print() {
    cout << endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
void print(vector<T> &vec) {
    for (auto& a : vec) {
        cout << a;
        if (&a != &vec.back()) cout << ' ';
    }
    cout << endl;
}

template <class T>
void print(vector<vector<T>> &df) {
    for (auto& vec : df) {
        print(vec);
    }
}




int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll x; cin >> x;
    ll a = x-(x/100)*100;
    ll num = 0;
    ll sum = 0;

    for(int i = 5; 0 < i;i--){
        num += a/i;
        sum += (100+i) * (a/i);
        a -= i*(a/i);
    }
    // print(sum);
    cout << (sum <= x ? 1 : 0) << endl;


}

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

int main(void){
	int N;
	cin>>N;
	vector<pair<ll,int>> a(N+1);
	rep1(i,N){
		cin>>a[i].x;
		a[i].y=i;
	}
	sort(a.begin(),a.end(),greater<pair<ll,int>>());
	vector<ll> cnt(N+1);
	int min_y = N;
	rep(i,N){
		min_y = min(min_y,a[i].y);
		cnt[min_y] += (i+1)*(a[i].x-a[i+1].x);
	}
	rep1(i,N)cout<<cnt[i]<<endl;
	return 0;
}


Measured memory_bytes: 3456
#include <iostream>
#include <algorithm>

using namespace std;
using ll = long long;

int len(ll n)
{
    int d = 0;

    while (n > 0) {
        n /= 10;
        ++d;
    }
    return d;
}

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

    int ans = len(n);

    for (ll i = 1LL; i * i <= n; ++i) {
        if (n % i != 0) continue;
        ll j = n / i;

        int tmp = max(len(i), len(j));

        if (ans > tmp) ans = tmp;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector> 
#include<limits>
#include<numeric>
#include<type_traits>
#include<math.h>
#include<fstream>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7; 

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define out(str) cout << str << endl
#define ALL(a) (a).begin(),(a).end()
#define INF_INT (1<<30)
#define INF_LL (1ll<<62)

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

ll N,K,ans;

int main(){
    cin>>N>>K;
    ll A[N];
    rep(i,N)cin>>A[i];
    sort(A,A+N,greater<ll>());
    rep(i,N){
        if(i>=K)ans+=A[i];
    }
    out(ans);

    
    
}

Measured memory_bytes: 1792
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI=3.1415926535897932384626433832795;
const ll INF = 1LL<<62;
const ll MINF = -1LL<<62;

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)


int main() {
	IO;
	int n;
	cin>>n;
	set<string> s;
	while(n--) {
		string ss;
		cin>>ss;
		s.insert(ss);
	}
	
	cout<<sz(s);

	return 0;
}


Measured memory_bytes: 19200
N, K = map(int, input().split())
A = list(map(int, input().split()))

S = sum(A)

def divisor(n):
    res = []
    rn = int(n**0.5) + 1 
    for i in range(1, rn):
        if n % i == 0:
            res.append(i)
            if i != n / i:
                res.append(int(n / i))
    return sorted(res)

Y = divisor(S)

ans = 1
for i in range(len(Y)-1, 0, -1):
    cnt = []
    for j in range(N):
        cnt.append(A[j] % Y[i])

    cnt = sorted(cnt)

    cnt_s = [0]
    tmp = 0
    for j in range(N):
        tmp += cnt[j]
        cnt_s.append(tmp)
    minc = 10**10
    for j in range(N+1):
        M = cnt_s[j]
        P = (N-j)*Y[i] - (cnt_s[N]-cnt_s[j])
        minc = min(max(M,P),minc)
    if minc <= K:
        ans = Y[i]
        break

print(ans)

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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
using P = pair<ll,ll>;
using ld=long double;

int main(){
   ll x;cin >>x;
   if(x==7||x==5||x==3){
       puts("YES");
   }
   else puts("NO");
   return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    
   ll s;
  cin>>s;
    //cin>>n>>x;

    int mod = 1e9+7;
ll n=s-3+1;
    ll arr[s-3+1];
int k=0;
    for(int i=3;i<=s;i++)
    {
        //cin>>c;
        arr[k]=i;
      k++;
    }

    ll  dp[s+1];
  for(int i=0;i<=s;i++)
    dp[i]=0;
    dp[0]=1;
ll brr[s+1];
  for(int i=0;i<s;i++)
    brr[i]=INT_MAX;
    for(ll i=1;i<=s;i++)
    {
        
        for(ll j=0;j<n;j++)
        {
            if(i-arr[j] >= 0)
            dp[i] += dp[i-arr[j]];
            dp[i] %= mod;
        }
    }

    cout << dp[s] <<"\n";

    return 0;
}

Measured memory_bytes: 3660
import itertools
n,m,x=map(int,input().split())
C,A=[],[]
for i in range(n):
    a=list(map(int,input().split()))
    C.append(a.pop(0))
    A.append(a)
ans=float("inf")
for i in range(n):
    for j in list(itertools.combinations(range(n),i+1)):
        s=0
        R=[0]*m
        for k in j:
            s+=C[k]
            R=[x + y for (x, y) in zip(R, A[k])]
        if all([w>=x for w in R]):
            ans=min(ans,s)
print(-1 if ans == float("inf") else ans)



Measured memory_bytes: 9272
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
inline void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  char f[10];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
int main(){
  int P, Q, R, res;
  rd(P);
  rd(Q);
  rd(R);
  res = P + Q + R -max_L(max_L(P, Q), R);
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20190626-1

// --- original code ---
// {
//   int P, Q, R, res;
//   rd(P,Q,R);
//   res = P + Q + R - max(P, Q, R);
//   wt(res);
// }


Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e5+5;
int n,a,b,c,q,cnt,head[N],deep[N],x[N][20];
ll ans,di[N];
bool f[N];
struct edge{int to,next,w;}e[N*2];
int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
void ins(int u,int v,int w){e[++cnt]=(edge){v,head[u],w};head[u]=cnt;}
void dfs(int k)
{
    f[k]=true;
    for(int i=1;(1<<i)<=deep[k];i++)
        x[k][i]=x[x[k][i-1]][i-1];//递推得出x数组 
    for(int i=head[k];i;i=e[i].next) 
    {
        if(f[e[i].to])continue;
        x[e[i].to][0]=k;//将节点k标记为e[i].to的父亲 
        deep[e[i].to]=deep[k]+1;//层数+1 
        di[e[i].to]=di[k]+e[i].w;
        dfs(e[i].to);
    }
}
int lca(int ri,int rj)
{
    if(deep[ri]<deep[rj])swap(ri,rj);
    int d=deep[ri]-deep[rj]; 
    for(int i=0;(1<<i)<=d;i++)//利用倍增思想，将ri节点跳到与rj节点同层 
        if((1<<i)&d)ri=x[ri][i];
    if(ri==rj)return ri;//找到最近公共祖先 
    for(int i=16;i>=0;i--)
        if((1<<i)<=deep[rj]&&x[ri][i]!=x[rj][i])
            ri=x[ri][i],rj=x[rj][i];//同时往上跳，直到ri与rj为兄弟节点为止 
    return x[ri][0];//返回他们的父亲 
}
int main()
{
	int n;
	n=read();
	for(int i=1;i<n;i++)
	{
		a=read();b=read();c=read();
		ins(a,b,c);ins(b,a,c);
	}
	dfs(1);
	q=read();int k=read();
	while(q--)
	{
		a=read();b=read();
		ans=0;
		ans+=di[a]+di[k]-2*di[lca(a,k)];
		ans+=di[b]+di[k]-2*di[lca(b,k)];
		printf("%lld\n",ans);
	}
	return 0;
}

Measured memory_bytes: 16640
//
#include <vector>
#include <set>
#include <array>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <bitset>
#include <cstdio>
#include <cstdint>
#include <cmath>
//
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);++i)
inline void yes(bool cond) { cond ? puts("Yes") : puts("No"); }
template<typename Type> inline void chmin(Type& a, Type b) { if (a > b) a = b; }
template<typename Type> inline void chmax(Type& a, Type b) { if (a < b) a = b; }
template<typename Type> inline void sort(Type& arr) { std::sort(arr.begin(), arr.end()); }
template<typename Type> inline Type nth(vector<Type>& arr, size_t pos) { std::nth_element(arr.begin(), arr.begin() + pos, arr.end()); return arr[pos]; }
constexpr ll BIGP = 1000000007L;
constexpr ll INF_I = std::numeric_limits<ll>::max();
constexpr ld INF_F = std::numeric_limits<float>::infinity();
constexpr ld PI = 3.14159265358979323846L;
//
int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    ll K;
    cin >> K;
    ll total = 0;
    for(ll i=1;i<=K;++i)
    {
        for (ll j = 1; j <= K; ++j)
        {
            ll tmp = gcd(i, j);
            for (ll k = 1; k <= K; ++k)
            {
                total += gcd(tmp, k);
            }
        }
    }
    cout << total;

    return 0;
}

Measured memory_bytes: 3144
#include<bits/stdc++.h>
using namespace std;
int main(){
	int H;
	while(cin>>H,H){
		int W = 5;
		int fi[20][20];
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				cin>>fi[i][j];
			}
		}
		int sc = 0;
		int D = 10;
		int p = 0;
		while(D--){
			bool er[20][20] = {};
			for(int i = 0;i < H;i++){
				for(int j = 1;j < W - 1;j++){
					if(fi[i][j-1] == fi[i][j] && fi[i][j] == fi[i][j+1]){
						er[i][j] = true;
						er[i][j-1] = true;
						er[i][j+1] = true;
					}
				}
				for(int j = 0;j < W;j++){
					if(er[i][j]){
						p += fi[i][j];
						fi[i][j] = 0;
					}
				}
			}
			int h = H+5;
			while(h--){
				for(int i = H - 1;i > 0;i--){
					for(int j = 0;j < W;j++){
						if(fi[i][j] == 0){
							swap(fi[i][j],fi[i-1][j]);
						}
					}
				}
			}/*
			cout<<"***"<<endl;
			for(int i = 0;i < H;i++){
				for(int j = 0;j < W;j++){
					cout<<fi[i][j];
				}
				cout<<endl;
			}*/
		}
		cout<<p<<endl;
	}
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for(int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (int) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge")))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss

int main() {
	fastio; int x; cin >> x; cout << x*x*x;
}


Measured memory_bytes: 3608
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <random>
#include <chrono>
using namespace std;

int n;
const int maxn = 205;

struct P{
    int x, y;
    P(){}
    P(int _x, int _y):x(_x), y(_y){}
    P operator - (P p){return P(x-p.x, y-p.y);}
    int det(P p){return x * p.y - y * p.x;}
    void read(){
        cin >> x >> y;
    }
}p[maxn], p1[maxn];

const int mod = 998244353;

bool cmp(P a, P b){
    return a.det(b) < 0; 
}

void add(int &a, int b){
    a += b;
    if(a >= mod)
        a -= mod;
}

void sub(int &a, int b){
    a -= b;
    if(a < 0)
        a += mod;
}

int qpow(int a, int n){
    int ret = 1;
    while(n > 0){
        if(n & 1)
            ret = 1LL * ret * a % mod;
        a = 1LL * a * a % mod;
        n >>= 1;
    }
    return ret;
}

int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        p[i].read();
    }
    int ans = qpow(2, n);
    sub(ans, 1);
    sub(ans, n);
    sub(ans, n * (n - 1) / 2);
    for(int i = 1;i <= n;i++){
        for(int j = i + 1;j <= n;j++){
            int num = 0;
            for(int k = j + 1;k <= n;k++){
                if((p[i] - p[j]).det(p[j] - p[k]) == 0){
                    num++;
                }
            }
            int tmp = qpow(2, num);
            sub(tmp, 1);
            sub(ans, tmp);
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#define FOR(i,a,n) for(i=a;i<n;i++)
#define swap(type,a,b) do{type t=a;a=b;b=t;}while(0);
#define MAX(a,b) (((a)>(b))?(a):(b))
#define INF 1000000000
typedef struct LIST{
	int to,cost;
	struct LIST *next;
}Edge;
typedef struct{
	int num,dist;
}Pos;
Edge** init(int v){
	Edge **edge=(Edge**)calloc(v,sizeof(Edge*));
	return edge;
}
void add_edge(Edge **edge,int s,int t,int c){
	Edge *tmp=(Edge*)malloc(sizeof(Edge));
	tmp->to=t,tmp->cost=c;
	tmp->next=edge[s];
	edge[s]=tmp;
	return;
}
void delete_edge(Edge **edge,int v){
	int i;
	Edge *e,*tmp;
	FOR(i,0,v){
		e=edge[i];
		while(e!=NULL){
			tmp=e->next;
			free(e);
			e=tmp;
		}
	}
	return;
}
void push(Pos *heap,int *heap_size,int n,int d){
	int i=(*heap_size)++;
	while(i>0){
		int p=(i-1)/2;
		if(heap[p].dist<=d) break;
		heap[i]=heap[p];
		i=p;
	}
	heap[i].num=n;
	heap[i].dist=d;
	return;
}
Pos pop(Pos *heap,int *heap_size){
	Pos data=heap[0];
	Pos n=heap[--*heap_size];
	int a,b,pos=0;
	while(pos*2+1<*heap_size){
		a=pos*2+1;
		b=pos*2+2;
		if(b<*heap_size&&heap[a].dist>heap[b].dist) a=b;
		if(n.dist<=heap[a].dist) break;
		heap[pos]=heap[a];
		pos=a;
	}
	heap[pos]=n;
	return data;
}
void dijkstra(Edge **edge,int v,int d[],int s){
	int i,heap_size=0;
	Pos p,*heap;
	Edge *e;
	heap=calloc(v,sizeof(Edge));
	FOR(i,0,v) d[i]=INF;
	d[s]=0;
	push(heap,&heap_size,s,0);
	while(heap_size){
		p=pop(heap,&heap_size);
		if(d[p.num]<p.dist) continue;
		for(e=edge[p.num];e!=NULL;e=e->next){
			if(d[e->to]>d[p.num]+e->cost){
				d[e->to]=d[p.num]+e->cost;
				push(heap,&heap_size,e->to,d[e->to]);
			}
		}
	}
	free(heap);
	return;
}
int size=0;
typedef struct{
	int sta,company,v_num;
}Data;
typedef struct node{
	struct node *right;
	struct node *left;
	Data data;
} Node;
Node* add(Node *n,int a,int b,int c){
	int cmp;
	if(n==NULL){
		Node *m=malloc(sizeof(Node));
		m->data.sta=a,m->data.company=b,m->data.v_num=c;
		m->right=m->left=NULL;
		return m;
	}
	else{
		cmp=(n->data.sta==a?n->data.company-b:n->data.sta-a);
		if(cmp==0) return n;
		if(cmp<0) n->right=add(n->right,a,b,c);
		else n->left=add(n->left,a,b,c);
		return n;
	}
}
int find(Node *n,int a,int b){
	int cmp;
	if(n==NULL) return size++;
	cmp=(n->data.sta==a?n->data.company-b:n->data.sta-a);
	if(cmp==0) return n->data.v_num;
	if(cmp<0) return find(n->right,a,b);
	else return find(n->left,a,b);
}
Edge **edge;
int d[500000];
int main(void)
{
	int n,m,p,q,c,v1,v2,i;
	Node *k=NULL;
	scanf("%d%d",&n,&m);
	edge=init(2*m+n);
	FOR(i,0,m){
		scanf("%d%d%d",&p,&q,&c);
		v1=find(k,--p,c);
		v2=find(k,--q,c);
		add_edge(edge,v1,v2,0);
		add_edge(edge,v2,v1,0);
		add_edge(edge,v1,2*m+p,0);
		add_edge(edge,v2,2*m+q,0);
		add_edge(edge,2*m+p,v1,1);
		add_edge(edge,2*m+q,v2,1);
		k=add(k,p,c,v1);
		k=add(k,q,c,v2);
	}
	dijkstra(edge,2*m+n,d,2*m);
	printf("%d\n",d[2*m+n-1]==INF?-1:d[2*m+n-1]);
	return 0;
}
    
    
    


Measured memory_bytes: 65152
n = int(input())

a = []
s = 0
while (-2)**s<10000000000:
    a.append((-2)**s)
    s += 1


def judge(n):
    sum = 0
    count = 0
    dig = 0
    if n >0:
        for num in a:
            count += 1
            if num >0:
                sum += num
            if sum >= n:
                dig = count
                break
    else:
        for num in a:
            count += 1
            if num <0:
                sum += num
            if sum <= n:
                dig = count
                break
            
    return dig
    
ans = ["0" for i in range(judge(n))]


while n!=0:
    ans[judge(n)-1] = "1"
    n -= a[judge(n)-1]

    
answer = ""
for i in range(len(ans)-1,-1,-1):
    answer += ans[i]
    
print(answer)

    
    

Measured memory_bytes: 3064
package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		matrix := make([][]int, 10)
		for i := range matrix {
			matrix[i] = make([]int, 10)
		}
		for i := range matrix {
			for j := range matrix[i] {
				if i == j {
					matrix[i][j] = 0
				} else {
					matrix[i][j] = INF
				}
			}
		}
		num := 0
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			from, _ := strconv.Atoi(buf[0])
			to, _ := strconv.Atoi(buf[1])
			cost, _ := strconv.Atoi(buf[2])
			if num < from {
				num = from
			}
			if num < to {
				num = to
			}
			matrix[from][to] = cost
			matrix[to][from] = cost
		}
		num++
		for i := 0; i < num; i++ {
			for j := 0; j < num; j++ {
				for k := 0; k < num; k++ {
					if matrix[j][i]+matrix[i][k] < matrix[j][k] {
						matrix[j][k] = matrix[j][i]+matrix[i][k]
					}
				}
			}
		}
		ans, sum := 0, INF
		for i := 0; i < num; i++ {
			now := 0
			for j := 0; j < num; j++ {
				now += matrix[i][j]
			}
			if sum > now {
				sum = now
				ans = i
			}
		}
		fmt.Println(ans, sum)
	}
}



Measured memory_bytes: 1116
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
void READ() {
#define R READ();
#define INFI 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define MEMX(x,num) memset(x,num,sizeof(x))
#define prln(x) cout<<#x<<"="<<x<<endl
#define prt(x) cout<<x<<endl
#define prk(x) x<<" "
#define sz(x) int(x.size())
#define PB(x) push_back(x)
#define LINK(x,y) G[x].PB(y),G[y].PB(x)
#ifndef ONLINE_JUDGE
    freopen("A.in", "r", stdin);
#endif
    std::ios::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(3);
}
///////////////////////////
const int N = 5 + (1 << 19);
const int MOD = 1e9 + 7;
LL a,b,c,x,y,ans;
int main() {

    cin>>a>>b>>c>>x>>y;
    if(2*c<a+b)
    {
        ans+=2*c*min(x,y);
        if(x>y)ans+=min(a,2*c)*(x-y);
        else ans+=min(b,2*c)*(y-x);
    }
    else ans=min(a,2*c)*x+min(b,2*c)*y;
    cout<<ans;
    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const int INF=1e9;

int n,k,a[500],sum=0,r[500],rs[501];

bool solve(int d){
    ll m=1e18;
    for(int i=0;i<n;i++){
        r[i]=a[i]%d;
    }
    sort(r,r+n);
    for(int i=0;i<n;i++){
        rs[i+1]=rs[i]+r[i];
    }
    for(int i=1;i<n;i++){
        m=min(m,max((ll)rs[i],(ll)(n-i)*d-rs[n]+rs[i]));
    }
    if(m<=k)return true;
    else return false;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
        sum+=a[i];
    }
    int ans=-1;
    for(int i=1;i*i<=sum;i++){
        if(sum%i==0){
            if(solve(i)){
                ans=max(ans,i);
            }
            if(solve(sum/i)){
                ans=max(ans,sum/i);
            }
        }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 256
/*input
1
8 2 9
1 2
1 6
1 7
1 8
1 9
2 2
2 3
2 4
*/
#include<bits/stdc++.h>
using namespace std;

#define N 200009
#define oo 100000000000007
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define mp make_pair
#define iii pair< int, pair<int, ii> >
#define mod 1000000007
int n, m, l;
int pr1[N], pr2[N], usize[N];
map<ii, int> ma;
int find1(int i) { 
	return (pr1[i] == i) ? i : (pr1[i] = find1(pr1[i])); 
} 
int find2(int i){
	return (pr2[i] == i) ? i : (pr2[i] = find2(pr2[i]));
}

signed main()
{
	//freopen("ROADRAIL.INP", "r", stdin);
	//freopen("ROADRAIL.OUT", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m >> l;
	for(int i = 1; i <= n; ++i) pr1[i] = pr2[i] = i, usize[i] = 1;
	for(int i = 1; i <= m; ++i){
		int x, y; cin >> x >> y;
		if(find1(x) != find1(y)) 
			pr1[find1(x)] = find1(y);
	}
	for(int i = 1; i <= l; ++i){
		int x, y; cin >> x >> y;
		if(find2(x) != find2(y))
			pr2[find2(x)] = find2(y);
	}
	for(int i = 1; i <= n; ++i){
		int x = find1(i), y = find2(i);
		if(ma.find(ii(x, y)) == ma.end()) ma[ii(x, y)] = 1;
		else ma[ii(x, y)] ++ ;
	}
	for(int i = 1; i <= n; ++i){
		int x = find1(i), y = find2(i);
		cout << ma[ii(x, y)] << ' ';
	}
}	

Measured memory_bytes: 14336
#include <iostream>
#include <vector>	// ??????
#include <algorithm>

using namespace std;

int main() {
	vector<int> v(3);
	
	for(int i=0;i<3;i++)
		cin >> v[i];
		
	sort(v.begin(),v.end());
	
	cout << v[0] << " " << v[1] << " " << v[2] << endl;
	
	return 0;
}

Measured memory_bytes: 3104
#include<iostream>
#include<string>
#include<vector>

using namespace std;

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

    int ecount = 0;
    for(int i = 0; i < N/2; i++){
        if(S[i] == 'E')ecount++;
        if(S[N-1-i] == 'E')ecount++;
    }
    if(N%2 != 0 && S[N/2] == 'E')ecount++;

    int wleft = 0;
    int eleft = ecount;
    int wright = N - ecount;
    int eright = 0;
    int ans = N + 1;
    for(int i = 0; i < N/2; i++){
        if(S[i] == 'E')eleft--;
        if(S[N-1-i] == 'W')wright--;
        if(wleft + eleft < ans)ans = wleft + eleft;
        if(wright + eright < ans)ans = wright + eright;

        if(S[i] == 'W')wleft++;
        if(S[N-1-i] == 'E')eright++;
    }
    if(N%2 != 0 && wleft + eleft < ans) ans = wleft + eleft;

    cout << ans;

}

Measured memory_bytes: 900
#include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s[5001],d,f,g,h[5001],i,j;
	f=0;
	scanf("%d",&a);
	for(i=0;i<a;i++){
		scanf("%d",&s[i]);
		h[i]=s[i];
	}
	d=4;
	while(1){
		d--;
		if(d==0) break;
		for(i=1;i<a-1;i++){
			if(s[i]==d){
				if(i==1){
					if(s[i+1]>0){
						s[i]--;
						s[i+1]--;
						f+=2;
					}
					else{
						s[i]--;
						s[i-1]--;
						f+=2;
					}
				}
				else if(i==a-2){
					if(s[i-1]>0){
						s[i]--;
						s[i-1]--;
						f+=2;
					}
					else{
						s[i]--;
						s[i+1]--;
						f+=2;
					}
				}
				else{
					if(s[i-1]>s[i+1]){
						s[i]--;
						s[i-1]--;
						f+=2;
					}
					else{
						s[i]--;
						s[i+1]--;
						f+=2;
					}
				}
			}
		}
	}
	if(s[0]>0) f+=s[0];
	if(s[a-1]>0) f+=s[a-1];
	g=0;
	d=4;
	for(i=0;i<a;i++) s[i]=h[i];
	while(1){
		d--;
		if(d==0) break;
		for(i=a-2;i>0;i--){
			if(s[i]==d){
				if(i==1){
					if(s[i+1]>0){
						s[i]--;
						s[i+1]--;
						g+=2;
					}
					else{
						s[i]--;
						s[i-1]--;
						g+=2;
					}
				}
				else if(i==a-2){
					if(s[i-1]>0){
						s[i]--;
						s[i-1]--;
						g+=2;
					}
					else{
						s[i]--;
						s[i+1]--;
						g+=2;
					}
				}
				else{
					if(s[i-1]>s[i+1]){
						s[i]--;
						s[i-1]--;
						g+=2;
					}
					else{
						s[i]--;
						s[i+1]--;
						g+=2;
					}
				}
			}
		}
	}
	if(s[0]>0) g+=s[0];
	if(s[a-1]>0) g+=s[a-1];
	if(g<f) f=g;
	printf("%d\n",f);
	return 0;
}

Measured memory_bytes: 2764
#include<algorithm>
#include<cstdio>
#include<utility>
#include<queue>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h, w, k, x1, x2, y1, y2;
char s[1000001];
P d[1000000][4];
priority_queue<PP, vector<PP>, greater<PP>> q;

int main() {
    scanf("%d %d %d", &h, &w, &k);
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    x1--; y1--; x2--; y2--;
    for (int i = 0; i < h; i++) scanf("%s", &s[i * w]);

    for (int i = 0; i < h * w; i++) fill(d[i], d[i] + 4, P((int) 1e9, 0));
    
    for (int i = 0; i < 4; i++) {
        int a = x1 + dx[i], b = y1 + dy[i];
        if (0 <= a && a < h && 0 <= b && b < w && s[a * w + b] == '.') {
            d[a * w + b][i] = P(1, 1);
            q.emplace(P(1, 1), 10000000 * i + a * w + b);
        } 
    }

    while(!q.empty()) {
        int z = q.top().second;
        int x = z % 10000000;
        z /= 10000000;
        int y = x % w;
        x /= w;
        P p = q.top().first; q.pop();
        if (d[x * w + y][z] != p) continue;
        for (int i = 0; i < 4; i++) {
            int a = x + dx[i], b = y + dy[i];
            if (0 <= a && a < h && 0 <= b && b < w && s[a * w + b] == '.') {
                P tmp = p;
                if (i != z || tmp.second == k) {
                    tmp.second = 1;
                    tmp.first++;
                }
                else tmp.second++;
                if (tmp >= d[a * w + b][i]) continue;
                d[a * w + b][i] = tmp;
                q.emplace(tmp, 10000000 * i + a * w + b);
            } 
        }
    }
    
    int ans = 1e9; 
    for (int i = 0; i < 4; i++) ans = min(ans, d[x2 * w + y2][i].first);
    printf("%d\n", ans == 1e9 ? -1 : ans);
    return 0;
}


Measured memory_bytes: 38052
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
int main(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int i = 0; i < n; ++i){
        scanf("%d",&arr[i]);
        if(i)
            arr[i] += arr[i-1];
    }
    ll ans = 1e18;
    vector<int> cp = arr;
    for(int start = -1; start <= 1; start += 2){
        arr = cp;
        ll cur = 0, add = 0, sign = start;
        for(int i = 0; i < n; ++i){
            arr[i] += add;
            //cout << arr[i] << ' ' << sign << '\n';
            if(sign == -1 && arr[i] > -1){
                cur += arr[i]+1;
                add -= arr[i]+1;
            }
            if(sign == 1 && arr[i] < 1){
                cur += 1-arr[i];
                add += 1-arr[i];
            }
            sign *= -1;
        }
        ans = min(ans,cur);
    }
    cout << ans << '\n';
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 81;
const ll LINF = 1e18 + 5;
int h, w, a[N][N], b[N][N];
int dp[N][N][N*N*2];
int main()
{
 	ios::sync_with_stdio(false);
 	cin.tie(0);
 	cin >> h >> w;
 	for(int i=1;i<=h;i++)
 		for(int j=1;j<=w;j++)
 			cin >> a[i][j];
 	for(int i=1;i<=h;i++)
 		for(int j=1;j<=w;j++)
 			cin >> b[i][j];
 	dp[1][1][abs(a[1][1]-b[1][1])] = 1;
 	for(int i=1;i<=h;i++)
 	{
 		for(int j=1;j<=w;j++)
 		{
 			int v = abs(a[i][j]-b[i][j]);
 			for(int k=0;k<=(i+j)*80;k++)
 			{	
 				dp[i][j][k+v] |= dp[i-1][j][k];
 				dp[i][j][abs(k-v)] |= dp[i-1][j][k];
 				dp[i][j][k+v] |= dp[i][j-1][k];
 				dp[i][j][abs(k-v)] |= dp[i][j-1][k];
 			}
 		}
 	}
 	for(int i=0;i<=(h+w)*80;i++) if(dp[h][w][i]) return cout << i, 0;
	return 0;
}


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

using namespace std;

int main()
{
  int N;
  bool flag = false;
  while(cin >> N, N){
    if(flag) cout << endl;
    flag = true;
    int coin[4] = {0};
    int value[4] = {10, 50, 100, 500};
    int sum = 0;
    for(int i = 0; i < 4; i++) cin >> coin[i], sum += value[i]*coin[i];
    int use[4] = {0};
    N = sum - N;
    for(int i = 3; i >= 0; i--){
      use[i] = coin[i] - N / value[i];
      N %= value[i];
    }
    for(int i = 0; i < 4; i++){
      if(use[i] > 0) cout << value[i] << " " << use[i] << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 1160
def rec(s):
    """引数末尾に753を付け加えてできる753数
    753以外の数が現れないように文字を加えているので、
    ・すべて一回以上出たら753数に加算
    ・Nを超えたらreturn 0
    させればよい"""
    # base case
    if int(s) > n:
        return 0

    res = 0
    if all(c in s for c in '753'):
        res += 1

    # 再帰ステップ
    for c in '753':
        res += rec(s + c)
    return res


n = int(input())
print(rec('0'))


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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

int main() {

  for (int n; cin >> n, n; ) {

    int left = INF;
    int right = -INF;
    int bottom = -INF;
    int top = INF;

    int xs[201];
    int ys[201];

    xs[0] = 0; ys[0] = 0;

    rep (i, n - 1) {
      int n, d; cin >> n >> d;
      int x = xs[n] + dx[d];
      int y = ys[n] + dy[d];
      xs[i + 1] = x;
      ys[i + 1] = y;

      left = min(left, x);
      right = max(right, x);
      top = min(top, y);
      bottom = max(bottom, y);
    }

    left = min(left, 0);
    right = max(right, 0);
    top = min(top, 0);
    bottom = max(bottom, 0);

    cout << (right - left + 1) << ' '
      << (bottom - top + 1) << endl;

  }

  return 0;
}

Measured memory_bytes: 1160
import numpy as np
def cumsum(x):
    return list(np.cumsum(x))

def cumsum_number(N,K,x):
    ans=[0]*(N-K+1)
    number_cumsum = cumsum(x)
    number_cumsum.insert(0,0)
    for i in range(N-K+1):
        ans[i]=number_cumsum[i+K]-number_cumsum[i]
    return ans
"""
int #整数
float #小数

#for
for name in fruits: #fruitの頭から操作
    print(name)

#リスト
list0 = [] #リスト生成
list0 = [1,2,3,4] #初期化
list0 = [0]*10 #[0,0,0,0,0,0,0,0,0,0]
list1= [i for i in range(10)] #>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list1 = list(map(int, input().split()))　# 複数の入力をリストに入れる
number=[int(input()) for i in range(int(input()))] #複数行の入力をリストに
list1 = list1 + [6, 7, 8] #リストの追加
list2.append(34) #リストの追加　最後に（）内が足される
list.insert(3,10) #リストの追加　任意の位置（追加したい位置,追加したい値）
len(list4) #リストの要素数
max(sa) #list saの最小値
list5.count(2) #リスト(文字列も可）内の2の数
list1.sort() #並び替え小さい順
list1.sort(reverse=True)  #並び替え大きい順
set(list1) #list1内の重複している要素を削除し、小さい順に並べる
sum([2,3,   ,3]) #リストの合計

abs(a) #aの絶対値
max(a,b) min(a,b) #最大値　最小値
text1 = text1.replace("34","test") #text1内の"34"を"test"に書き換える(文字列内の文字を入れ替える)

exit(0) #強制終了

cumsum(リスト) #累積和をリストで返す
cumsum_number(N,K,x) #N個の数が含まれるリストx内のK個からなる部分和をリストで返す
"""
N,M=map(int,input().split())
number=list(map(int,input().split()))
ans = 0
for i in number:
    if i>=sum(number)/(4*M):ans+=1
if ans>=M:print("Yes")
else:print("No")






Measured memory_bytes: 27064
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

void main() {
    auto abx = readints;
    int a = abx[0], b = abx[1], x = abx[2];

    if (a <= x && x <= a + b)
        writeln("YES");
    else
        writeln("NO");
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  int Y;
  cin >> N >> Y;
  
  for(int i=N;i>=0;i--){
    for(int j=N-i;j>=0;j--){
      if(i*10000 + j*5000 + (N-i-j)*1000 == Y){
        cout << i << " " << j << " " << N-i-j;
        return(0);
      }
    }
  }
  
  cout << "-1 -1 -1" << endl;

  return(0);
}


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

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define For(i, a) for (int i=0; i<(a); i++)
#define FORR(i,a,b) for (int i = (b)-1; i >= a; i--)
#define Forr(i,a) for (int i = (a)-1; i >= 0; i--)

#define deb(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__) 
void DBG() { cerr << "]\n";}
template<typename T, typename ...Args> void DBG(T first, Args... args){
cerr << first; if(sizeof...(args))cerr<<", "; DBG(args...); }

#define int ll
#define pb(x) push_back(x)
#define INF (int)1e17
#define PI 3.14159265358979323846
 
int del_x[4] = {-1, 1, 0, 0};
int del_y[4] = {0, 0, 1, -1};
int XX[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int YY[8] = {-1, 0, 1, 1, 1, 0, -1, -1};


void solve(){
	int n; cin >> n;
	int a[n];
	For(i,n)cin >> a[i];
	sort(a,a+n);
	int cnt = 0;
	For(i,n){
		FOR(j,i+1,n){
			FOR(k,j+1,n){
				if(a[i]+a[j]>a[k] && a[i]+a[k]>a[j] && a[k]+a[j]>a[i]){
					if(a[i]!=a[j] && a[i]!=a[k] && a[j]!=a[k])
					cnt++;
				}
			}
		}
	}
	cout << cnt <<"\n";
	

}

int32_t main(){ 
	ios_base:: sync_with_stdio(false); 
	cin.tie(0); 
	int t=1;
	//cin >> t;
	for(int tt=1;tt<=t;tt++){
		//cout << "Case #"<<tt<<": ";
		solve();
	}
}

Measured memory_bytes: 3656
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<algorithm>

#define rep(n) for(int i=0;i<n;i++)
#define repp(j, n) for(int j=0;j<n;j++)
#define reppp(i, m, n) for(int i=m;i<=n;i++)
#define all(c) c.begin(), c.end()
#define MOD 1000000007
#define MAX 1000000001
#define INF 1410065408
 
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
struct edge{int from, to; ll cost;};

signed main(){
    ll N, C, K;
    cin >> N >> C >> K;
    vector<ll> t(N);
    ll ans = 0;
    rep(N){
        cin >> t[i];
    }
    sort(all(t));
    ll s = 0, c = 0;
    rep(N){
        if(s == 0){
            s = t[i];
			c++;
        }else{
            if(t[i] <= s + K){
              c++;
            }else{
			  s = t[i];
			  c = 1;
			  ans++;
			}
        }
		if(c >= C){
		  ans++;
		  s = 0;
		  c = 0;
        }
		//cout << t[i] <<  " " << s  << " " << c << " " << ans << endl;
    }
    if(c > 0) ans++;
    cout << ans;
}


Measured memory_bytes: 1024
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
using ll = long long;
using pi = pair<int,int>;
int main()
{
  ll n,k; cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i,n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i,n) cin >> c[i];

  ll res = -1e+15;
  rep(i,n) {
    vector<ll> dp(1, 0);
    ll maxi = -1e+15;
    ll tmp = 0, cnt = 0;
    int idx = i;
    while(cnt < k) {
      cnt++;
      idx = p[idx];
      tmp += c[idx];
      chmax(maxi, tmp);
      dp.push_back(maxi);
      if(i == idx) break;
    }
    if(cnt < k && tmp > 0) {
      ll m = k / cnt;
      chmax(res, m * tmp + dp[k - m * cnt]);
      m--;
      chmax(res, m * tmp + maxi);	
    }
    else {
      chmax(res, maxi);
    }
  }
  cout << res << "\n";
  
  return 0;
}


Measured memory_bytes: 3740
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 305;
int n , m , l , q , ans[N][N];
ll c[N][N];
int main() {
    abdelrahman010
    cin >> n >> m >> l;
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= n;j++)
            if(i != j)
                c[i][j] = 1e15 , ans[i][j] = 1e9;
    for(int i = 0;i < m;i++) {
        int u , v , w;
        cin >> u >> v >> w;
        c[u][v] = w , c[v][u] = w;
    }
    for(int k = 1;k <= n;k++)
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= n;j++)
                if(c[i][j] > c[i][k] + c[k][j])
                    c[i][j] = c[i][k] + c[k][j];
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= n;j++)
            if(c[i][j] <= l)
                ans[i][j] = 1;
    for(int k = 1;k <= n;k++)
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= n;j++)
                if(ans[i][j] > ans[i][k] + ans[k][j])
                    ans[i][j] = ans[i][k] + ans[k][j];
    cin >> q;
    while(q--) {
        int u , v;
        cin >> u >> v;
        ans[u][v] == 1e9 ? cout << "-1\n" : cout << ans[u][v] - 1 << '\n';
    }
    return 0;
}

Measured memory_bytes: 4740