akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, l, r) for (int i = l; i <= (int)(r); i++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define MOD (int) (1e9+7)
#define INF (int) 1e9
#define LLINF (ll) 1e18

int main(){
    int n;
    cin >> n;
    int l[n];
    rep(i, n) cin >> l[i];

    // sort(l, l+n);

    int ans = 0;
    rep(i, n) rep(j, n)rep(k, n){
        if(!(i<j && j < k)) continue;
        if(l[i]==l[j] || l[k]==l[j] || l[i]==l[k]) continue;
        if(l[i] +l[j] > l[k] && abs(l[i]-l[j]) < l[k]) ans++;
        // if(l[i] +l[j] > l[k]) ans++;
    }
    cout << ans << endl;
    // printf("%d\n", N);
    return 0;
}

Measured memory_bytes: 3632
#include<bits/stdc++.h>
#include<string>
 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  
  int A,B;
  cin >> A >>B;
  if(A+B<10){
    cout << A+B << endl;
  }else{
    cout <<"error"<< endl;
  }
    
  return 0;
} 


Measured memory_bytes: 256
fn main() {
    let mut s = String::new();
    let mut t = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    std::io::stdin().read_line(&mut t).unwrap();
    let s = s.trim();
    let t = t.trim();
    let mut d = [-1; 26];
    let mut e = [-1; 26];
    let mut result = true;
    for (a, b) in s.bytes().zip(t.bytes()) {
        let (a, b) = (a as isize - 97, b as isize - 97);
        let (i, j) = (a as usize, b as usize);
        if d[i] >= 0 && d[i] != b || e[j] >= 0 && e[j] != a {
            result = false;
            break;
        }
        d[i] = b;
        e[j] = a;
    }
    println!("{}", if result { "Yes" } else { "No" });
}


Measured memory_bytes: 4352
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> l(3*n);
    for (auto &c:l) {cin >> c;}
    sort(l.begin(),l.end());

    long long count=0;
    for (int i=0;i<n;i++) {count += l[3*n-2-2*i];}
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 1408
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    const ll inf = LONG_LONG_MAX / 2 - 1;
    ll n, m;
    cin >> n >> m;
    vector<ll> c(m);
    rep(i, m) cin >> c[i];
    vector<vector<ll>> dp(m + 1, vector<ll>(n + 1, inf));
    dp[0][0] = 0;
    rep(i, m) {
        rep(j, n + 1) {
            dp[i + 1][j] = dp[i][j];
            if (j >= c[i]) {
                if (dp[i][j - c[i]] != inf) {
                    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - c[i]] + 1);
                }
                if (dp[i + 1][j - c[i]] != inf) {
                    dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][j - c[i]] + 1);
                }
            }
        }
    }
    cout << dp[m][n] << endl;
    return 0;
}



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

int main() {
    int a, b;
    cin >> a >> b;

    int ans = (a + b) % 24;

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


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

int main() {
  ll n, a, b;
  cin >> n >> a >> b;
  ll x = n / (a + b) * a;
  ll y = min(n - (n / (a + b) * (a + b)), a);
  cout << x + y << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<numeric>
#include<math.h>
#include<algorithm>
#include<iomanip>
#include<bitset>
using namespace std;

int main() {

	string S, T;
	cin >> S >> T;

	string str;

	S = S + S;

	bool jdg = false;

	for (int i = 0; i < T.length(); i++) {
		str = S.substr(i, T.length());
		if (str == T) {
			jdg = true;
		}
	}

	if (jdg == true) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}

	return 0;
}



Measured memory_bytes: 256
a, b, c, x, y = map(int, input().split())

def f(x,y):
    return x-y if x>y else 0

if (a+b) > 2*c:
    ans = min(2*c*x + f(y,x)*b, 2*c*y + f(x,y)*a)
else:
    ans = a*x + b*y

print(ans)    

Measured memory_bytes: 3060
#include<stdio.h>
#include<string.h>

int main(){
  int i,j,len;
  int cnt=0;
  char a[105];

  while(scanf("%s",a)!=EOF){
    len=strlen(a)-1;
    j=len;
    for(i=0;i<=len;i++){
      if(i>=j){
	cnt++;
	break;
      }
      if(a[i]!=a[j])
	break;
      j--;
    }
  }
  printf("%d\n",cnt);
  return 0;
}

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

# 偶数番目(0-origin)を正とする場合
minA = 0
sumA = 0
for i in range(n):
	sumA += a[i]
	if i % 2 == 0:
		if sumA <= 0:
			minA += abs(sumA) + 1
			sumA += abs(sumA) + 1
	else:
		if sumA >= 0:
			minA += abs(sumA) + 1
			sumA -= abs(sumA) + 1

# 奇数番目(0-origin)を正とする場合
minB = 0
sumB = 0
for i in range(n):
	sumB += a[i]
	if i % 2 != 0:
		if sumB <= 0:
			minB += abs(sumB) + 1
			sumB += abs(sumB) + 1
	else:
		if sumB >= 0:
			minB += abs(sumB) + 1
			sumB -= abs(sumB) + 1

print(min(minA, minB))

Measured memory_bytes: 63216
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL<<60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i)
#define REP(i,n)  FOR(i,0,(n))
#define RREP(i,n) RFOR(i,0,(n))
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x)  cout << #x << " = " << (x) << endl;
#define SP << " " << 
typedef pair<int,int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

signed main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);

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

  
  if(x >= 0.5 * a * a * b){
    cout << atan2(2 * (a * a * b - x), a * a * a) * 180 / 3.14159265358979323846264338327950288 << endl;
  }
  else{
    cout << atan2(a * b * b, 2 * x) * 180 / 3.14159265358979323846264338327950288;
  }
  return 0;
}


Measured memory_bytes: 640
/*input
6
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll = unsigned long long;
using ld = long double;
using pii = pair<ll,ll>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=1e18;
const int INF=1e6+1;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll mult(ll a,ll b){
    return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=mult(res,a);
        a=mult(a,a);
        b>>=1;
    }
    return res;
}
const ll maxn=2e5+5;
const ll maxlg=__lg(maxn)+2;
int arr[]={1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int k;
    cin>>k;
    --k;
    cout<<arr[k];
}

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

struct Dice
{
	Dice(int d[6]){
		for(int i = 0; i < 6; ++i)
			n[i] = d[i];
	}
	int getIndex( int a ){
		for(int i = 0; i < 6; ++i){
			if( n[i] == a )
				return i;
		}
		return -1;
	}
	void toN(){ shift( n[0], n[1], n[5], n[4] ); }
	void toS(){ shift( n[0], n[4], n[5], n[1] ); }
	void toW(){ shift( n[0], n[2], n[5], n[3] ); }
	void toE(){ shift( n[0], n[3], n[5], n[2] ); }
	void shift( int& a, int& b, int& c, int& d ){
		int t = a;
		a = b;
		b = c;
		c = d;
		d = t;
	}
	void toTop( int t ){
		switch( t )
		{
		case 0: return;
		case 1: toN(); return;
		case 2: toW(); return;
		case 3: toE(); return;
		case 4: toS(); return;
		case 5: toN(); toN(); return;
		}
	}
	void shift( int t ){
		if( 0 == t ) 
			return;

		int i, j, a;
		for( i = 0; i < t; ++i ){
			a = n[0];
			for( j = 0; j < 5; ++j)
				n[j] = n[j+1];
			n[5] = a;
		}
	}
	void twist(){
		int a = n[1];
		n[1] = n[2];
		n[2] = n[4];
		n[4] = n[3];
		n[3] = a;
	}
	bool isSameNumber( const Dice& d ){
		for(int i = 0; i < 6; ++i){
			if(n[i] != d.n[i])
				return false;
		}
		return true;
	}
	bool isTwistSame( const Dice& d ){
		if( n[0] != d.n[0] || n[5] != d.n[5] )
			return false;

		for(int j = 0; j < 4; ++j){
			twist();
			if( this->isSameNumber( d ))
				return true;
		}
		return false;
	}
	bool isSame( const Dice& d ){
		if( this->isTwistSame(d) ) return true;
		this->toN();
		if( this->isTwistSame(d) ) return true;
		this->toN();
		if( this->isTwistSame(d) ) return true;
		this->toN();
		if( this->isTwistSame(d) ) return true;
		this->toE();
		if( this->isTwistSame(d) ) return true;
		this->toN();
		this->toN();
		if( this->isTwistSame(d) ) return true;

		return false;
	}
	
	int n[6];
};

int main()
{
	int n, i, t, f, 
		d1[6] = {},
		d2[6] = {};
	for(i = 0; i < 6; ++i) cin >> d1[i];
	for(i = 0; i < 6; ++i) cin >> d2[i];

	Dice dice1(d1);
	Dice dice2(d2);
	
	cout << (dice1.isSame( dice2 ) ? "Yes" : "No") << "\n";

	return 0;
}

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

using namespace std;

int n, a[100005], b[100005], c[100005], dpa, dpb, dpc;

int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i] >> b[i] >> c[i];
		int pa = max(dpb, dpc) + a[i];
		int pb = max(dpa, dpc) + b[i];
		int pc = max(dpb, dpa) + c[i];
		dpa = pa;
		dpb = pb;
		dpc = pc;
	}
	cout << max(max(dpa, dpb) , dpc);
}

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

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

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

    int ans=0;

    for(int i=1;i<2000;i++)
    {
        if(i*8/100==a&&i*10/100==b)
        {
            ans=i;
            break;
        }
    }

    if(ans)cout<<ans<<"\n";
    else cout<<-1<<"\n";

    return 0;
}


Measured memory_bytes: 3616
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    if(a+b+c>=22){
        printf("bust\n");
    }else{
        printf("win\n");
    }

    return 0;
}

Measured memory_bytes: 128
class UnionFind:
    # par: 親の番号(はじめ par[i]==i つまりすべての頂点が根)
    def __init__(self, size:int):
        self.par = [-1]*size
        self.rank = [0]*size
        for i in range(size):
            self.par[i] = i
    
    def root(self, x:int)->int:
        """根を求める"""
        if self.par[x] == x: # if root
            return x
        else:
            self.par[x] = self.root(self.par[x]) # 経路圧縮
            return self.par[x]
        
    def isSame(self, x:int, y:int)->bool:
        """x と y が同じ集合に属するか否か"""
        return self.root(x)==self.root(y)
    
    def unite(self, x:int, y:int):
        """x と y の属する集合を併合"""
        root_x = self.root(x)
        root_y = self.root(y)
        if root_x == root_y: return
        
        # 短いほうを長いほうにくっつける
        if self.rank[root_x] < self.rank[root_y]:
            self.par[root_x] = root_y
        else:
            self.par[root_y] = root_x
            if self.rank[root_x] == self.rank[root_y]:
                self.rank[root_x] += 1
                

##### main #####
N,M = map(int, input().split())
a,b = [0]*M, [0]*M
for i in range(M):
    ai,bi = map(int, input().split())
    a[i],b[i] = ai-1, bi-1
    
ans = 0
for i in range(M):
    uf = UnionFind(N)
    for j in range(M):
        if i==j: continue # この辺はパス
        uf.unite(a[j], b[j])
    bridge = False
    for i in range(N):
        if not uf.isSame(0,i):
            bridge = True
    if bridge:
        ans += 1
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
#define let auto const

template<typename... Types>
struct dynarr: std::vector<Types...> {
  using std::vector<Types...>::vector;
  using size_type = typename std::vector<Types...>::size_type;
  auto&& operator[](size_type i) { return this->at(i); }
  auto&& operator[](size_type i) const { return this->at(i); }
};

int main() {
  i64 N;
  cin >> N;
  string s;
  cin >> s;
  i64 x = 0;
  i64 y = 0;
  for(auto c: s) {
    if('A' <= c && c <= 'M') x++;
    if('N' <= c && c <= 'Z') x--;
    if('a' <= c && c <= 'm') y++;
    if('n' <= c && c <= 'z') y--;
  }
  cout << abs(x) + abs(y) << endl;
  for(int i = 0;i < abs(x);i++) {
    if(x < 0) cout << "N";
    else cout << "A";
  }
  for(int i = 0;i < abs(y);i++) {
    if(y < 0) cout << "n";
    else cout << "a";
  }
  cout << endl;
}



Measured memory_bytes: 3336
from operator import mul  # mul:引数を2つ持つ関数。例) mul(4, 5)をすると掛け算をして`20`を返す
from functools import reduce  # reduce：引数を2つ持つ関数。(実行したい関数(引数が2つあるもの), リスト)

n, m = map(int, input().split(' '))


def p(_n, _r):
    # nPrをreturnする
    return reduce(mul, list(range(_n, _n - _r, -1)))  # _nから(_n - _r)まで-1ずつ。p(5, 4)の場合はmul(mul(mul(5, 4), 3), 2)


def c(_n, _r):
    # nCrをreturnする
    over = p(_n, _r)
    under = p(_r, _r)
    return over / under


print(int((c(n, 2)) + c(m, 2)))


Measured memory_bytes: 3572
#include<iostream>
using namespace std;
int main()
{
	int a;
	while(scanf("%d",&a)!=EOF)
	{
		int t=a;
		int n[4];
		n[3]=t%10;
		t/=10;
		n[2]=t%10;
		t/=10;
		n[1]=t%10;
		t/=10;
		n[0]=t;
		if((n[0]+n[1]+n[2]+n[3])==7){
		cout<<n[0]<<"+"<<n[1]<<"+"<<n[2]<<"+"<<n[3]<<"=7";return 0;}
		if((n[0]-n[1]+n[2]+n[3])==7){
		cout<<n[0]<<"-"<<n[1]<<"+"<<n[2]<<"+"<<n[3]<<"=7";return 0;}
		if((n[0]+n[1]-n[2]+n[3])==7){
		cout<<n[0]<<"+"<<n[1]<<"-"<<n[2]<<"+"<<n[3]<<"=7";return 0;}
		if((n[0]+n[1]+n[2]-n[3])==7){
		cout<<n[0]<<"+"<<n[1]<<"+"<<n[2]<<"-"<<n[3]<<"=7";return 0;}
		if((n[0]-n[1]-n[2]+n[3])==7){
		cout<<n[0]<<"-"<<n[1]<<"-"<<n[2]<<"+"<<n[3]<<"=7";return 0;}
		if((n[0]-n[1]+n[2]-n[3])==7){
		cout<<n[0]<<"-"<<n[1]<<"+"<<n[2]<<"-"<<n[3]<<"=7";return 0;}
		if((n[0]+n[1]-n[2]-n[3])==7){
		cout<<n[0]<<"+"<<n[1]<<"-"<<n[2]<<"-"<<n[3]<<"=7";return 0;}
		if((n[0]-n[1]-n[2]-n[3])==7){
		cout<<n[0]<<"-"<<n[1]<<"-"<<n[2]<<"-"<<n[3]<<"=7";return 0;}
}

}

Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <queue>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

using P = std::pair<int, int>;

int N, M;
std::vector<int> G[300010];
int S, T;

int vmap( int v, int m )
{ return N*m+v; }

bool used[300010];
std::queue<P> que;
int dist[300010];

int main()
{
  scanf( "%d%d", &N, &M );
  rep( i, M )
  {
    int u, v;
    scanf( "%d%d", &u, &v );
    --u; --v;

    rep( j, 3 )
      G[vmap(u,j)].push_back(vmap(v,(j+1)%3));
  }

  scanf( "%d%d", &S, &T );
  --S; --T;

  que.push( P( 0, vmap(S,0) ) );

  while( !que.empty() )
  {
    P p = que.front(); que.pop();
    int d = p.first, v = p.second;

    used[v] = true;

    if( v == vmap(T,0) )
    {
      printf( "%d\n", d/3 );

      return 0;
    }

    for( auto u : G[v] ) if( !used[u] )
      que.push( P( d+1, u ) ), used[u] = true;
  }

  puts("-1");

  return 0;
}

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


def main():
    H, W, K = map(int, input().split())
    cake = [[0]*W for _ in range(H)]
    sb = []
    for y in range(H):
        s = input().strip()
        for x, c in enumerate(s):
            if c == "#":
                sb.append((y, x))
    for i, (y, x) in enumerate(sb):
        cake[y][x] = i + 1

    for i, s in enumerate(sb):
        i += 1
        y = s[0]
        x0 = x1 = s[1]
        for x in range(s[1]-1, -1, -1):
            if cake[y][x] != 0:
                break
            cake[y][x] = i
            x0 = x
        for x in range(s[1]+1, W):
            if cake[y][x] != 0:
                break
            cake[y][x] = i
            x1 = x
        for y in range(s[0]-1, -1, -1):
            if cake[y][x0:x1+1].count(0) != x1-x0+1:
                break
            for x in range(x0, x1+1):
                cake[y][x] = i
        for y in range(s[0]+1, H):
            if cake[y][x0:x1+1].count(0) != x1-x0+1:
                break
            for x in range(x0, x1+1):
                cake[y][x] = i
    for c in cake:
        print(*c)


if __name__ == '__main__':
    main()


Measured memory_bytes: 52188
# -*- coding:utf-8 -*-

n = int(input())
print(str(n)+':',end='')
array = []
count = 0
while True:
    if n == 999993031:
        break
    for i in range(2,n+1):
        if n%i == 0 and i != n:
            array.append(i)
            n = int(n/i)
            break
        if i == n:
            count += 1
            break
    if count == 24:
        break

array.append(n)
for i in range(len(array)):
    print(' ',array[i],sep = '',end='')
print('')

Measured memory_bytes: 7696
from collections import Counter

h, w, n = map(int, input().split())
dots = [list(map(int, input().split())) for _ in range(n)]

c = Counter()

for dot in dots:
	for i in range(-2, 1):
		for j in range(-2, 1):
			if 1 <= dot[0] + i < h - 1 and 1 <= dot[1] + j < w - 1:
				c[(dot[0] + i) * w + dot[1] + j] += 1

ans = [0] * 10

for v in c.values():
	ans[v] += 1

ans[0] = (h - 2) * (w - 2) - sum(ans[1:])

for a in ans:
	print(a)

Measured memory_bytes: 147576
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    D=int(readline())
    c=list(map(int,readline().split()))
    s=[list(map(int,readline().split())) for _ in range(D)]

    kaisai=[0]*26
    for i in range(D):
        res=-float("inf")
        res_i=-1
        for j in range(26):
            henka=s[i][j]
            kaisai_kari=kaisai[:]
            kaisai_kari[j]=i+1
            for k in range(26):
                henka-=c[k]*(i+1-kaisai_kari[k])
            if res<henka:
                res=henka
                res_i=j
        kaisai[res_i]=i+1
        print(res_i+1)

if __name__=="__main__":
    main()

Measured memory_bytes: 9388
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int n,q,k;
int m,head[300020],next[300020],to[300020];
long long len[300020],dis[300020];
bool visited[300020];

void dfs(int x,int y, long long d){
    dis[x]=d;
    visited[x]=true;
    for (int e=head[x]; e!=-1; e=next[e]){
        if (visited[to[e]]==false){
           if (to[e]==y) {
                continue;
            }
            dfs(to[e],x,len[e]+d);
            visited[to[e]]=false;
        }
    }
}

int main(void){
    scanf("%d",&n);
    for (int i=0; i<n; i++){
        head[i]=-1;
    }
    for (int i=0; i<n-1; i++){
        int a,b;
        long long c;
        scanf("%d%d%lld",&a,&b,&c);
        a--;
        b--;
        next[m]=head[a];
        head[a]=m;
        to[m]=b;
        len[m]=c;
        m++;
        next[m]=head[b];
        head[b]=m;
        to[m]=a;
        len[m]=c;
        m++;
    }
    scanf("%d%d",&q,&k);
    dfs(k-1,-1,0);
    for (int i=0; i<q; i++){
        int x,y;
        scanf("%d%d",&x,&y);
        printf("%lld\n",dis[x-1]+dis[y-1]);
    }
	return 0 ;
}

Measured memory_bytes: 14080
import math
from itertools import product
N,D = map(int,input().split())
X = [list(map(float, input().split())) for i in range(N)]
counter = 0
for x0,x1 in product(X, repeat=2):
    if x0 == x1:
      continue
    if math.sqrt(sum([(a-b)**2 for a, b in zip(x0,x1)])).is_integer():
            counter+=1
print(counter//2)


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

int main(){
	int N;cin>>N;
  	int count=0;
  	int a[100001];
  	bool b[100001];
  	for(int i=1;i<=N;++i){cin>>a[i];b[i]=false;}
  	int v=a[1];b[1]=true;++count;if(v==2){cout<<'1'<<endl;return 0;}
	while(1){
      	if(b[v]==true)break;
    	b[v]=true;v=a[v];++count;
      	if(v==2){
        	cout<<count<<endl;
          	return 0;
        }
    }
	cout<<"-1"<<endl;
  	return 0;
}

Measured memory_bytes: 768
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> ppiii;

int scale[10]{1,2,4,7,12,20,29,38,52,73};

ll dp[10][10];

void dfs(int curr, int mask, int v, ll currW, ll& ret) {
  if(__builtin_popcount(mask) == v) {
    ret = max(ret, currW);
    return;
  }
  for(int i = 0; i < v; i++) {
    if((mask&(1<<i)) == 0) {
      dfs(i, mask | (1<<i), v, currW + dp[curr][i], ret);
    }
  }
}

void solve() {
  int n;
  cin >> n;
  dp[0][1] = 1;
  dp[1][0] = 1;
  for(int v = 2; v < n; v++) {
    ll ret = 0;
    for(int i = 0; i < v; i++) {
      dfs(i, 1<<i, v, 0, ret);
    }
    for(int k = 0; k < v; k++) {
      dp[v][k] = (ret+1) * scale[k];
      dp[k][v] = (ret+1) * scale[k];
    }
  }
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      cout << dp[i][j];
      if(j == n-1) cout << "\n";
      else cout << " ";
    }
  }
}

void casesolve() {
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
  // casesolve();
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstring>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define fix(n) cout<<fixed<<setprecision(n);
#define rep(i,n)   for (int i = 0; i < (n); ++i)
#define sort(a)    sort((a).begin(), (a).end());
#define uniq(a)    SORT(a);(a).erase(unique((a).begin(), (a).end()), (a).end());
#define reverse(a) reverse((a).begin(), (a).end());
#define out(d) cout << (d);
#define outl(d) std::cout<<(d)<<"\n";
#define Yes() printf("Yes\n");
#define No() printf("No\n");
#define YES() printf("YES\n");
#define NO() printf("NO\n");
#define ceil(x, y) ((x + y - 1) / (y))

using namespace std;
using ll = long long;
using P = pair<ll,ll>;

const ll MOD = 1000000007; // 10^9 + 7

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  srand((unsigned)time(NULL));
  fix(12)

  ll n, a, b; cin >> n >> a >> b;
  outl(n / (a + b) * a + min(a, n % (a + b)))
}


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

using namespace std;
using ll = long long;

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

int main() {
  int a, b, t;
  cin >> a >> b >> t;
  int sum = 0, s = 0, mul = 1;
  while (s <= t) {
    s += mul * a;
    if (s > t) break;
    sum += b;
  }
  cout << sum << endl;
  return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n;
  int64_t a,b;
  cin >> n;
  if(n == 0) {
    cout << 2;
    return 0;
  }
  if(n == 1) {
    cout<<1;
    return 0;
  }
  a = 2;
  b = 1;
  for(int i = 0; i<n-1;i++){
    a += b;
    swap(a,b);
  }
  cout<<b;
  
  
}

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

#define int         long long
#define uint        unsigned int
#define ld          long double
#define showoff     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb          push_back
#define pii         pair<int,int>
#define FOR(i,a,b)  for(int i=a;i<b;++i)
#define RFOR(i,a,b) for(int i=a;i>b;--i)
#define f           first
#define se          second
#define maxn        200005
#define all(v)      v.begin(),v.end()
#define sz(x)       (int)x.size()
#define mod         1000000007
#define pqueue      priority_queue<int>
#define pdqueue     priority_queue< pii,vector<pii> ,greater< pii >>

using namespace std;


//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*int power(int a,int n)
{
    if(n == 1)return a%mod;
    if(n == 0)return 1;
    if(n&1)return ((a%mod)*(power((a*a)%mod,n/2))%mod)%mod;
    return power((a*a)%mod,n/2)%mod;
}*/
//const int md = 998244353;

/*int inverse(int x){

    return power(x,mod-2)%mod;//little fermat....
}*/
const int inf = (int) 1e18;
const int md = (int) 1e9+21;


signed main()
{
    showoff;
    int r,d,x;
    cin >> r >> d >> x;
    FOR(i,0,10){
        x = x*r-d;
        cout << x << "\n";
    }
    return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...

Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
 


public class Main {
	static int a1[]= {0,-1,0,1};
	static int b1[]= {-1,0,1,0};
	private static         Scanner sc = new Scanner(System.in);
	//result = Math.pow(num1, num3)
	//StringBuffer str = new StringBuffer(hoge1);
	//String hoge2 = str.reverse().toString();
	//map.containsKey(A)

	//Map<String, Integer> map = new HashMap<String, Integer>(n);
	/*for ( キーのデータ型 key : マップの名前.keySet() ) {
		データのデータ型 data = マップの名前.get( key );
		
		// keyやdataを使った処理;
	}*/
	//int i = Integer.parseInt(s);
	//Queue<String> qq=new ArrayDeque<>(); //add,poll,peek
	//Deque<String> qq=new ArrayDeque<>();//push,pop,peek
	//ArrayList<String> cc = new ArrayList<>(n);
	//Collections.sort(list);
	//Array.sort(list);   cc.contains(tmp)
	//Arrays.asList(c).contains("a")
	//list.set(1,"walk");//1 1 2 3 5
	static int K,N;
	static long mod =1000000007;
	//static 	ArrayList<Integer> cc = new ArrayList<>(100);

	public static void main(String[] args) {
			int N=sc.nextInt();
			ArrayList<Integer> cc = new ArrayList<>(N);
			for(int i=0;i<N;i++) {
				cc.add(sc.nextInt());
			}
			long ans=0;
			int t=0;
			while(true){
				//p(t);
				t=cc.get(t)-1;
				ans++;
				if(t==1) {
					p(ans);
					break;
				}
				if(ans>N) {
					p(-1);
					break;
				}
			}
			
			
	}
	

	
	//N==S
	//E==W
//83 S 
	//69E
	//78N
	//87W
	
	//static ArrayList<Integer> cc = new ArrayList<>(10001);
	
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p() {System.out.println();};
	static void p(long ans) {System.out.println(ans);};
	static void p(double ans) {System.out.println(ans);};
	public static int gcd(int a, int b) {
        return b == 0 ? a: gcd(b, a % b);
	}
	static String nextPermutation(String s) {
		ArrayList<Character> list=new ArrayList<>();
		for(int i=0;i<s.length();i++) {
			list.add(s.charAt(i));
		}
		int pivotPos=-1;
		char pivot=0;
		for(int i=list.size()-2;i>=0;i--) {
			if(list.get(i)<list.get(i+1)) {
				pivotPos=i;
				pivot=list.get(i);
				break;
			}
		}
		if(pivotPos==-1&&pivot==0) {
			return "Final";
		}
		int L=pivotPos+1,R=list.size()-1;
		
		
		
		int minPos=-1;
		char min =Character.MAX_VALUE;
		
		for(int i=R;i>=L;i--) {
			if(pivot<list.get(i)) {
				if(list.get(i)<min) {
					min=list.get(i);
					minPos=i;
				}
			}
		}
		
		Collections.swap(list, pivotPos, minPos);
		Collections.sort(list.subList(L, R+1));
		
		
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<list.size();i++) {
			sb.append(list.get(i));
		}
		return sb.toString();
	}
}
	


Measured memory_bytes: 63360
def main():
    H, W = (int(i) for i in input().split())
    C = [[int(i) for i in input().split()] for j in range(10)]
    A = [[abs(int(i)) for i in input().split()] for j in range(H)]
    determined = set()
    from collections import deque
    notdetermined = deque()
    r = [0] * 10
    for k in range(10):
        r[k] = C[k][1]
    
    from itertools import permutations
    for sel in range(1,10):
        for p in permutations([0,2,3,4,5,6,7,8,9,],sel):
            cur = 0
            k = p[0]
            for i in p[1:]:
                cur += C[k][i]
                k = i
            else:
                cur += C[k][1]
            if cur < r[p[0]]:
                r[p[0]] = cur
    ans = 0
    for row in A:
        for a in row:
            ans += r[a]
    print(ans)

if __name__ == '__main__':
    main()

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

int main(){
	ios::sync_with_stdio(0);
	cout << setprecision(10) << fixed;
	int k;
	cin >> k;
	long long ans = 0;
	for (int i = 1; i <= k; ++i){
		for (int j = 1; j <= k; ++j){
			for (int l = 1; l <= k; ++l){
				int g = __gcd(i, j);
				g = __gcd(g, l);
				ans += (long long) g;
			}
		}
	}
	cout << ans;
	return 0;
}



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

#define N 10000


//Class of stck
class Stack{
private:
  int num[N];
  int head;
public:
  Stack(){
    head=0;
  }

  //add to head position
  void push(int x){
    head++;
    num[head]=x;
  }
  //delete head position
  void pop(void){
    head--;
  }
  //return head of stck
  int top(void){
    return num[head];
  }

};


int main(){
  Stack stack;
  string str;
  int x,y;
  //load  and caluculation
  while(cin >> str){

   
    if(str=="+"){

      x=stack.top();
      stack.pop();
      y=stack.top();
      stack.pop();
      stack.push(x+y);
    }
    else if(str=="-"){
      x=stack.top();
      stack.pop();
      y=stack.top();
      stack.pop();
      stack.push(y-x); 
    }
    else if(str=="*"){
      x=stack.top();
      stack.pop();
      y=stack.top();
      stack.pop();
      stack.push(x*y);
    }
    else{
      x = atoi(str.c_str());
      stack.push(x);
    }
  }
  //print
  cout <<stack.top()<<endl;
  
  return 0;
  
}

Measured memory_bytes: 0
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <math.h>
#include <array>
#include <vector>
#include <queue>
#include <stack>
#include <numeric>
#include <list>
#include <deque>
#include <map>
#include <set>

using namespace std;

typedef long long LL;
typedef pair<int, int>PII;

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), rend()
#define PB pushback
#define SORT(c) sort((c).begin(),(c).end())

const LL INF = 1LL << 60;

//priority_queue<int> p;


string s;

//scanf("%d",&N);

//printf("%d", ans);

//cout <<  << endl;

//買った人に足していくようにする
LL limit = 0;//勝ち負けが決まるライン

LL i, j, k, tmp, ans, cnt, N, K, Q;

int main(void) {
	vector<LL> A;
	vector<LL> p;
	scanf("%lld %lld %lld", &N, &K, &Q);

	REP(i, N) {
		p.push_back(K);
	}

	REP(i, Q) {
		scanf("%lld", &tmp);
		p.at(tmp-1)++;
	}

	limit += Q;
	REP(i, N) {
		if (p.at(i) > limit) {
			printf("Yes\n");
		}
		else {
			printf("No\n");
		}
	}


	return 0;
}

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

ans = sum(a)
b = a[:]
c = [0]*n

for k in range(1, n):
	now = k * x
	for i in range(n):
		tmp = min(b[i-1], a[i])
		now += tmp
		c[i] = tmp
	ans = min(ans, now)
	b = c[:]
	
print(ans)

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

int main(){
  int H1,M1,H2,M2,T;
  cin >> H1 >> M1 >> H2 >> M2 >> T;

  int ans= (H2*60+ M2)- (H1*60+ M1)- T;
  
  cout << ans << endl;
}

Measured memory_bytes: 3596
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()) {
		int b=sc.nextInt();
		int []a=new int[b];
		for(int i=0;i<b;i++) {
			a[i]=i+1;
			//System.out.print(a[i]);
		}//
		int s=sc.nextInt();
		for(int i=0;i<s;i++) {
			   String str = sc.next();
			   String Str[] = str.split(",");
				 int p=Integer.parseInt(Str[0])-1;
				 int q=Integer.parseInt(Str[1])-1;	
				int temp;
				temp=a[p];
				a[p]=a[q];
				a[q]=temp;
		}
			for(int j=0;j<b;j++) {
				System.out.println(a[j]);
			}
	}
	}
	}


Measured memory_bytes: 25820
#include<iostream>
    using namespace std;
     
    int main(){
      long long A, B;
      cin >> A >> B;
      for(int i = 1; i <= B; i++){
        if((A * i) % B == 0){
          cout << A * i << endl;
          break;
        }
      }
      return 0;
    }

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

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    int res = 0;
    res =  (b-a + d-c) - (max(b, d) - min(a, c));
    cout << (res >= 0? res: 0) << endl;
}

Measured memory_bytes: 3588
N = int(input())
S = list(input())

c = [0,0]
for s in S:
    if s == '(':
        c[0] += 1
    else:
        if c[0] > 0:
            c[0] -= 1
        else:
            c[1] += 1

pre = ['('] * c[1]
rear = [')'] * c[0]
ans = pre+S+rear
print(''.join(ans))


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep(i, n) for(ll i = 0;i < n;i++)
#define all(i) i.begin(), i.end()
template<class T, class U> bool cmax(T& a, U b) { if (a<b) {a = b; return true;} else return false; }
template<class T, class U> bool cmin(T& a, U b) { if (a>b) {a = b; return true;} else return false; }

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

  ll a, b;
  cin >> a >> b;

  string s, t;
  rep(i, b) {
    s.push_back('0'+a);
  }
  rep(i, a) {
    t.push_back('0'+b);
  }
  if (s < t) {
    cout << s << endl;
  } else {
    cout << t << endl;
  }
}


Measured memory_bytes: 256
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		String str = "";
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		if((str=bf.readLine())==null)return;
		bf.close();
		int n=Integer.parseInt(str);
		int c=100000;
		for(int i=0;i<n;i++){
			c*=1.05;
			if(c%1000!=0)c+=1000;
			c=(c/1000)*1000;
		}
		System.out.println(c);
	}
}

Measured memory_bytes: 9084
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vll vector<ll>
#define vi vector<int>
#define vvll vector<vll>
#define wh(t) while(t--)
#define fi(n) for(ll i = 0; i < n; i++)
#define fj(n) for(ll j = 0; j < n; j++)
#define fu(i,s,e) for(ll i = s; i <=e; i++)
#define fd(i,s,e) for(ll i = s; i >=e; i--)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define fr first
#define se second
#define sll stack<ll>
#define qll queue<ll>
using namespace std;
const ll M = 1e9+7;
//const ll M = 998244353;

ll pmp(ll x,ll n){ll a = 1;while(n > 0){if(n & 1) a = (a * x)%M;x = (x * x)%M;n = n>>1;}return a;}

void solve(){
	ll n,k;
	cin >> n >> k;
	vll a(n);
	fi(n) cin >> a[i];
	ll ans=0;
	sort(a.begin(), a.end());
	fi(k) ans+=a[i];
	cout << ans << endl;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// #ifndef ONLINE_JUDGE
	// freopen("input.txt", "r", stdin);
	// #endif
	ll t;
	t = 1;
	// cin >> t;
	wh(t){
		solve();
	}
	cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
	return 0;
}

Measured memory_bytes: 3848
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    A, B, K = map(int, input().split())
    
    a = b = 0
    if A>=K:
        a = A - K
        b = B
    elif A+B>=K:
        a = 0
        b = A + B - K
    
    print(str(a) + " " + str(b))


if __name__ == '__main__':
    main()

Measured memory_bytes: 5636
def gcd(m, n)
    if ((0 == m) || (0 == n))
        return 0;
    end
    while m != n
        if m > n
            m = m - n
        else
            n = n - m
        end
    end
    return m
end

def lcm(m, n)
    if (0 == m) || (0 == n)
        return 0
    end
    return ((m / gcd(m, n)) * n)
end

while line = gets
    a,b = line.split.map(&:to_i)
    printf("%d %d\n", gcd(a, b), lcm(a, b))
end

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



int main() {

	int n,d; cin >> n>>d;
	vector<int>v; int count = 0; double sum=0;
	for (int i = 0; i < n; i++) {
		int input1, input2; cin >> input1 >> input2;
		v.push_back(input1); v.push_back(input2);
		sum = sqrt(pow(input1, 2) + pow(input2, 2));
		if(sum <= d)
			count++;
		sum = 0;
		v.clear();
	}
	cout << count;



}

Measured memory_bytes: 3144
use std::str::FromStr;
use std::fmt::Debug;
use std::cmp;
#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

#[allow(dead_code)]
fn read_as_string() -> String{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn main(){
    let line = read_as_vec::<u64>();
    let (_, mut x) = (line[0], line[1]);

    let mut a = read_as_vec::<u64>();
    a.sort();
    for i in 0..a.len(){
        if a[i] > x{
            println!("{}", i);
            return;
        }
        x -= a[i];
    }
    if x > 0 {
        println!("{}", a.len()-1);
    }else{
        println!("{}", a.len());
    }

}


Measured memory_bytes: 4352
package main

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

var sc = bufio.NewScanner(os.Stdin)

func stringLineScan() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	buf := make([]byte, 0) //ここのサイズは何でもよさそう
	sc.Buffer(buf, 1000000009)
	sc.Split(bufio.ScanWords)
	s := stringLineScan()
	t := stringLineScan()
	if check(s, t) && check(t, s) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}
func check(s, t string) bool {
	x := map[byte]byte{}
	for i := 0; i < len(s); i++ {
		si, ti := s[i], t[i]
		sv, sok := x[si]
		if !sok {
			x[si] = ti
		} else if sv != ti {
			return false
		}
	}
	return true
}


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

using ll = long long;
using ull = unsigned long long;

#define MOD 1000000007

int main(void) {
    string O, E;
    cin >> O >> E;
    for(ll i = 0;i < max(O.size(), E.size());i++) {
        if(i < O.size()) cout << O[i];
        if(i < E.size()) cout << E[i];
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 256
// AOJ 1040 Chocolate with Heart Marks
// 2018.2.6 bal4u
 
#include <stdio.h>
#include <string.h>

#define N 144		// 12x12
#define INF 0x03030303
int s[10];
int g[N][N];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0;
	int c = getchar_unlocked();
	while (c < '0') c = getchar_unlocked();
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

int b[128][N];
int minimumSteinerTree(int *s, int sz, int g[N][N], int gsz)
{
	int i, j, k, t, lim, ans;

    if (sz <= 1) return 0;

	for (k = 0; k < gsz; k++) for (i = 0; i < gsz; i++) for (j = 0; j < gsz; j++) {
		t = g[i][k] + g[k][j];
		if (t < g[i][j]) g[i][j] = t;
	}

	memset(b, INF, sizeof(b));
	for (t = 1, i = 0; i < sz; i++, t <<= 1) {
		for (j = 0; j < gsz; j++) b[t][j] = g[s[i]][j];
	}

	lim = (1<<sz) - 1;
	for (i = 1; i <= lim; i++) {
		if (!(i & (i-1))) continue;
		for (j = 0; j < gsz; j++) for (k = 0; k < i; k++) {
			if ((i | k) == i) {
				t = b[k][j] + b[i-k][j];
				if (t < b[i][j]) b[i][j] = t;
			}
		}
		for (j = 0; j < gsz; j++) for (k = 0; k < gsz; k++) {
			t = b[i][k] + g[j][k];
			if (t < b[i][j]) b[i][j] = t;
		}
	}

	ans = INF;
    for (i = 0; i <= lim; i++) for (j = 0; j < gsz; j++) {
		t = b[i][j] + b[lim-i][j];
		if (ans > t) ans = t;
	}
    return ans;
}

int main()
{
    int H, W, i, r, c, t, nr, nc, nt, sz;

    while (H = in()) {
		W = in();
		memset(g, INF, sizeof(g));
		sz = 0;
		for (r = 0; r < H; r++) for (c = 0; c < W; c++) {
			t = r*W + c;
			if (in()) s[sz++] = t;

			for (i = 0; i < 4; i++) {
				nr = r + mv[i][0], nc = c + mv[i][1];
				if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
				nt = nr*W + nc;
				g[t][nt] = 1;
			}
			g[t][t] = 0;
		}

		printf("%d\n", H*W - minimumSteinerTree(s, sz, g, H*W) - 1);
	}
	return 0;
}



Measured memory_bytes: 2048
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

const LLONG ComputeGCM(const LLONG a, const LLONG b)
{
    if (b == 0)
    {
        return a;
    }
    return ComputeGCM(b, a % b);
}

int main()
{
    LLONG A, B;
    cin >> A >> B;
    LLONG gcm = ComputeGCM(A, B);
    LLONG count = 1;
    for (LLONG i = 2, max = static_cast<LLONG>(sqrt(
        static_cast<double>(gcm))) + 1; i <= max; ++i)
    {
        if (gcm % i == 0)
        {
            ++count;
            for (;;)
            {
                gcm /= i;
                if (gcm % i != 0) break;
            }
        }
    }
    if (gcm > 1) ++count;
    cout << count << endl;
}


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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll MAXN=102,MAXV=1013;
ll N,W,pw[MAXN],pv[MAXN],v=0,dp[MAXN][MAXN*MAXV];

/*ll BT(ll k,ll b)
{
    if(b<0)
        return INT_MAX-1;
   if(k==0 && b==0)
       return 0;
   if(k==0)
       return INT_MAX-1;
   if(dp[k][b]!=-1)
      return dp[k][b];

    dp[k][b]=min(BT(k-1,b),BT(k-1,b-pv[k])+pw[k]);
    return dp[k][b];
}*/

int main(){
#ifdef Javier
    freopen(".in", "r", stdin);
#endif // Javier

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin>>N>>W;
    for(int i=1;i<=N;i++)
    {
        cin>>pw[i]>>pv[i];
        v+=pv[i];
    }
    //cout<<v<<endl;
  /*  for(int i=1;i<=N;i++)
    {
        for(int j=0;j<=v;j++)
            dp[i][j]=-1;
    }
*/
    ll ans=INT_MIN;
  /*  for(ll u=v;u>0;u--)
    {
        int aux=BT(N,u);
        if(aux<=W && u>ans)
        {
            ans=u;
            break;
        }
    }*/
   // cout<<dp[N][v]<<endl<<LONG_LONG_MAX<<endl<<BT(3,v);

    for(ll i=0;i<=N;i++)
    {
        for(ll j=0;j<=v;j++)
        {
            if(i==0)
                dp[i][j]=((j==0) ? 0:INT_MAX);
            else
            {

                    dp[i][j]=min(dp[i-1][j],(j-pv[i]>=0) ? dp[i-1][j-pv[i]]+pw[i]:INT_MAX);
                    if(dp[i][j]<=W)
                        ans=max(ans,j);


            }

        }
    }



    cout<<ans;

    return 0;
}



Measured memory_bytes: 82176
#include "bits/stdc++.h"
using namespace std;
int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int d;
	while (cin >> d) {
		int area = 0;
		for (int x = d; x <= 600 - d;x += d) {
			area += x*x*d;
		}
		cout << area << endl;
	}
}

Measured memory_bytes: 3100
// OURL: https://atcoder.jp/contests/abc103/tasks/abc103_d
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define RREP(i,a,n) for(int i=(a),i##_len=(n);i<i##_len;++i)
#define MAT1D(V,T,a) vector<T> V (a)
#define MAT2D(V,T,a,b) vector<vector<T>> V (a, vector<T>(b))
#define MAT3D(V,T,a,b,c) vector<vector<vector<T>>> V (a, vector<vector<T>>(b, vector<T>(c)))

// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){ return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); }

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}
template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){ for(auto &e:t) fill_v(e,v); }

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool>::reference b) { return to_string(bool(b)); }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; }
template <typename A>
string to_string(vector<vector<A>> v) { string res; for(const auto &x : v) {res+="\n ";res += to_string(x);} return res;}

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }

#ifdef LOCAL
#define debug(...) cerr << "[L" << to_string(__LINE__) << ":" <<#__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N,M;
  cin >> N >> M;
  auto ab = make_v<pair<int,int>>(M);
  REP(i,M) {
    cin >> ab[i].first >> ab[i].second;
  }
  sort(ab.begin(), ab.end(), [](const auto &x, const auto &y) {
         return x.second < y.second;
      });
  // debug(ab);
  int ans = 0, pos = 0;
  for (auto &p : ab) {
    if (p.first >= pos) {
      pos = p.second;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 1152
import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		long H = stdIn.nextInt();
		long W = stdIn.nextInt();
		long min = H*W;
		
		for(int i = 1; i < H; i++) {
			long S0 = i*W;
			long S1 = ((H-i)/2)*W;
			long S2 = (H-i-((H-i)/2))*W;
			long S3 = (H-i)*(W/2);
			long S4 = (H-i)*(W-(W/2));
			
			long max1 = Math.max(Math.max(S0,S1),S2);
			long min1 = Math.min(Math.min(S0,S1),S2);
			long max2 = Math.max(Math.max(S0,S3),S4);
			long min2 = Math.min(Math.min(S0,S3),S4);
			
			if(Math.min(max1-min1,max2-min2) < min) min = Math.min(max1-min1,max2-min2);
		}
		for(int i = 1; i < W; i++) {
			long S0 = i*H;
			long S1 = ((W-i)/2)*H;
			long S2 = (W-i-((W-i)/2))*H;
			long S3 = (W-i)*(H/2);
			long S4 = (W-i)*(H-(H/2));
			
			long max1 = Math.max(Math.max(S0,S1),S2);
			long min1 = Math.min(Math.min(S0,S1),S2);
			long max2 = Math.max(Math.max(S0,S3),S4);
			long min2 = Math.min(Math.min(S0,S3),S4);
			
			if(Math.min(max1-min1,max2-min2) < min) min = Math.min(max1-min1,max2-min2);
		}
		
		System.out.println(min);

	}

}

Measured memory_bytes: 24148