akhauriyash/Code-Regression huggingface dataset:
def solve():
    W, H, N = tuple(map(int, input().split()))

    R = []
    for _ in range(N):
        R.append(tuple(map(int, input().split())))

    # print("W, H, N", W, H, N)
    # print(R)

    table = [[0 for x in range(W + 1)] for y in range(H + 1)]

    for x, y, a in R:
        # print(x, y, "|", a)
        y = H - y
        # 1: x より小さい左側の領域
        if a == 1:
            for yi in range(H + 1):
                for xi in range(x):
                    table[yi][xi] = "*"
        # 2: x より大きい右側の領域
        elif a == 2:
            for yi in range(H + 1):
                for xi in range(x + 1, W + 1):
                    table[yi][xi] = "*"
        # 3: 長方形の H < y を塗る
        elif a == 3:
            for yi in range(y + 1, H + 1):
                for xi in range(W + 1):
                    table[yi][xi] = "*"
        # 4: 長方形の H > y を塗る
        elif a == 4:
            for yi in range(y - 1, -1, -1):
                for xi in range(W + 1):
                    table[yi][xi] = "*"

    # 白い長方形の左下の座標を探す
    left_bottom = -1, -1
    is_found = False
    for yi in range(H + 1):
        yii = H - yi
        for xi in range(W + 1):
            if table[yii][xi] == 0:
                left_bottom = xi, yi
                is_found = True
                break
        if is_found:
            break

    # 白い長方形の右上の座標を探す
    right_top = -1, -1
    is_found = False
    for yi in range(H + 1):
        for xi in range(W, -1, -1):
            if table[yi][xi] == 0:
                right_top = xi, H - yi
                is_found = True
                break
        if is_found:
            break

    # print(left_bottom, right_top)
    if left_bottom == (-1, -1) and right_top == (-1, -1):
        return 0
    else:
        return (right_top[0] - left_bottom[0]) * (right_top[1] - left_bottom[1])


if __name__ == '__main__':
    res = solve()
    print(res)


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

using namespace std;

#include <vector>
#include <list>
#include <cstring>
#include <cmath>
#define scan(x) scanf("%d", &x)
#define scanl(x) scanf("%lld", &x)

void printarray(int* arr, int n){
	for(int i=0;i<n;i++){
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void printvector(vector <int> A){
	for(auto i=A.begin();i!=A.end();i++){
		cout<<*i<<" ";
	}
	cout<<endl;
}

int main(){

	#ifndef ONLINE_JUDGE
    freopen("D:/Programming/input.txt", "r", stdin);
    freopen("D:/Programming/output.txt", "w", stdout);
	#endif

    int count=0;
    int n;
    scan(n);
    int m;
    scan(m);
    int h[n][2];
    for(int i=0;i<n;i++){
    	scan(h[i][0]);
    	h[i][1]=1;
    }
    int a,b;

    for(int i=0;i<m;i++){
    	scan(a);
    	scan(b);

    	if(h[a-1][0]>h[b-1][0])h[b-1][1]=0;
    	else if(h[a-1][0]<h[b-1][0])h[a-1][1]=0;
    	else {h[b-1][1]=0;h[a-1][1]=0;}
    }

    for(int i=0;i<n;i++){
    	if(h[i][1])count++;
    }
    cout<<count;

	return 0;
}

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

int main()
{
  	int N, M;
	cin >> N >> M;

	int *ms = new int[M];
	for (int i = 0; i < M; ++i)ms[i] = 0;

	for (int i = 0; i < N; ++i)
	{
		int Ki;
		cin >> Ki;
		for (int j = 0; j < Ki; ++j)
		{
			int likeNum;
			cin >> likeNum;
			++ms[likeNum - 1];
		}
	}

	int ret = 0;
	for (int i = 0; i < M; ++i)
		if (ms[i] == N)++ret;

	cout << ret << endl;

	delete[] ms;

  return 0;
}


Measured memory_bytes: 256
N = gets.to_i
@e = {}
(N-1).times do
  a, b = gets.split.map(&:to_i)
  @e[a] ||= []
  @e[b] ||= []
  @e[a] << b
  @e[b] << a
end
@d = Array.new(N+1) { [0, 0] }

def check(n)
  q = [[n, nil, 0]]
  q.each do |(i, prev, d)|
    if n == 1
      @d[i][0] = d
    else
      @d[i][1] = d
    end
    @e[i].each do |j|
      next if j == prev
      q << [j, i, d+1]
    end
  end
end

check(1)
check(N)

fennec = @d[2..-2].count { |(i,j)| i <= j }
sunuke = N - 2 - fennec
if fennec > sunuke
  puts "Fennec"
else
  puts "Snuke"
end


Measured memory_bytes: 28156
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		sc.close();

		int tmp = 7;
		int ans = 1;
		for(int i=1;i<=k;i++) {
			if(tmp%k==0) {
				System.out.println(ans);
				return;
			}
			ans++;
			tmp = (tmp*10+7)%k;

		}
		System.out.println(-1);
	}
}

Measured memory_bytes: 35720
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

  public static void main(String[] args) {
    new Main().run();
  }

  public void run() {

    try(BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {

      int n = Integer.parseInt(in.readLine());
      int[] seq = new int[n+2];
      seq[0] = seq[n+1] = 0;
      int[] d = new int[n+1];

      String line = in.readLine();
      {
        char regex = ' ';
        boolean isContinue = true;
        for(int i=1, offset=0,next=0; isContinue; offset = ++next) {
          if((next = line.indexOf(regex,offset)) == -1) {
            next = line.length();
            isContinue = false;
          }
          seq[i++] = Integer.parseInt(line.substring(offset,next));
        }
      }

      int sum=0;
      StringBuilder buf = new StringBuilder();
      {
        for(int i=0; i<=n; i++) {
          sum += d[i] = Math.abs(seq[i+1] - seq[i]);
        }

        int ans=0;
        for(int i=1; i<=n; i++) {
          ans = sum;
          ans -= d[i-1];
          ans -= d[i];
          ans += Math.abs(seq[i+1] - seq[i-1]);
          buf.append(ans).append('\n');
        }
      }

      System.out.print(buf);
    }
    catch(IOException e) {
      System.err.println(e);
    }
  }
}

Measured memory_bytes: 46360
#include <stdio.h>

int  main (){

  int a;
  int b;
  int j;
  int i;

  while(1){
  scanf( "%d %d", &a,&b);
  if ( a == 0 && b == 0)break;
  for(i = 0; i < a; i++){
  for( j = 1; j< b; j++){
    printf("#");
  }
  printf("#\n");
  }
  printf("\n");
  }
  return 0;
}

Measured memory_bytes: 588
#include<iostream>
#include<stack>

using namespace std;

int A, B;
int main(){
	int cnt = 0;
	cin >> A >> B;
	int now = 1;
	int i;
	for(i = 0; i < 30; i++){
		if(now >= B) break;
		now = now -1 + A;
	}
	cout << i;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
const int dx[] = { -1, 0, 0, 1 }, dy[] = { 0, -1, 1, 0 };
//[const int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy[] = { 0, -1, 1, -1, 1, 0, -1, 1 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
int place(int x, int y, int w) { return w*x + y; }

struct Data
{
	int x, y, kai, sw;
};

int step[50][50][2][1024];

int main()
{
	int h, w;
	cin >> w >> h;
	vs fld(h);
	REP(i, h) cin >> fld[i];
	int s; cin >> s;
	vector<vs> v(s, vs(h));
	REP(i, s)
	{
		REP(j, h) cin >> v[i][j];
	}
	map<pii, int> mp;
	int sx, sy, gx, gy;
	REP(i, h)REP(j, w)
	{
		if (fld[i][j] == '%')
		{
			sx = i, sy = j;
			fld[i][j] = '_';
		}
		if (fld[i][j] == '&')
		{
			gx = i, gy = j;
			fld[i][j] = '_';
		}
		if (islower(fld[i][j]))
		{
			mp[pii(i, j)] = fld[i][j] - 'a';
			fld[i][j] = '_';
		}
		if (isupper(fld[i][j]))
		{
			mp[pii(i, j)] = fld[i][j] - 'A';
			fld[i][j] = '^';
		}
	}
	MS(step, -1);
	step[sx][sy][0][0] = 0;
	queue<Data> que;
	que.push(Data{ sx,sy,0,0 });
	while (!que.empty())
	{
		Data d = que.front();
		que.pop();
		int x = d.x, y = d.y, kai = d.kai, sw = d.sw;
		int st = step[x][y][kai][sw];
		if (fld[x][y] == '|' && step[x][y][1 - kai][sw] == -1)
		{
			step[x][y][1 - kai][sw] = st + 1;
			que.push(Data{ x,y,1 - kai,sw });
		}
		if (mp.count(pii(x, y)))
		{
			int n = mp[pii(x, y)];
			int nsw = sw ^ (1 << n);
			int nkai = kai;
			if (v[n][x][y] == '*') nkai = 1 - kai;
			if (step[x][y][nkai][nsw] == -1)
			{
				step[x][y][nkai][nsw] = st + 1;
				que.push(Data{ x,y,nkai,nsw });
			}
		}
		REP(i, 4)
		{
			int nx = x + dx[i], ny = y + dy[i];
			if (!valid(nx, ny, h, w) || fld[nx][ny] == '#') continue;
			if (fld[nx][ny] == '_' || fld[nx][ny] == '^')
			{
				int nkai = (fld[nx][ny] == '_' ? 0 : 1);
				REP(j, s)
				{
					if ((sw >> j) & 1 && v[j][nx][ny] == '*') nkai = 1 - nkai;
				}
				if (nkai != kai) continue;
			}
			if (step[nx][ny][kai][sw] == -1)
			{
				step[nx][ny][kai][sw] = st + 1;
				que.push(Data{ nx,ny,kai,sw });
			}
		}
	}
	int ans = INF;
	REP(i, 2)REP(j, 1 << s)
	{
		if (step[gx][gy][i][j] != -1) chmin(ans, step[gx][gy][i][j]);
	}
	if (ans == INF) puts("-1");
	else cout << ans << endl;
	return 0;
}

Measured memory_bytes: 23752
#include <stdio.h>
#include <string.h>
void research(){
    
}

int main(void){
    int x[30];
    int n;
    int element;
    int ans = 1;
    char order[500];
    char *token;
    
    fgets(order, 10, stdin);
    sscanf(order, "%d", &n);
    fgets(order, 500, stdin);
    token = strtok(order, " \n");
    sscanf(token, "%d", &x[0]);
    for(int i = 1; i < n; i++){
        token = strtok(NULL, " \n");
        sscanf(token, "%d", &x[i]);
    }
    
    for(int i = 1; i < n; i++){
        element = 0;
        for(int t = 0; t < i; t++){
            if(x[i] >= x[t]){
                element++;
            }
        }
        if(element == i){
            ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp setprecision
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = 1e8;
const ll INF = 1e16;
const string alpha = "abcdefghijklmnopqrstuvwxyz";

int main(){
    int N;
    cin >> N;
    int num[] = {N/3, (N+2)/3, (N+1)/3};
    //c[i]:=最終的にiに置く数字を3で割った余り
    int c[N];
    fill(c, c+N, 0);
    vector<int> es[N];
    rep(i, N-1){
        int u, v;
        cin >> u >> v;
        u--, v--;
        es[u].pb(v), es[v].pb(u);
    }
    queue<int> que;
    que.push(0);
    c[0] = 1;
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(auto &v: es[u]){
            if(c[v]) continue;
            int nc;
            if(c[u] == 1) nc = 2;
            else nc = 1;
            c[v] = nc;
            que.push(v);
        }
    }
    vector<int> vec[3];
    rep(i, N) vec[c[i]].pb(i);
    if(vec[1].size() >= num[1] && vec[2].size() >= num[2]){
        rep(i, num[1]){
            c[vec[1][i]] += 3*i;
        }
        rep(i, vec[1].size()-num[1]){
            c[vec[1][num[1]+i]] = 3*(i+1);
        }
        rep(i, num[2]){
            c[vec[2][i]] += 3*i;
        }
        rep(i, vec[2].size()-num[2]){
            c[vec[2][num[2]+i]] = 3*(num[0]-i);
        }
    }
    elif(vec[1].size() < num[1]){
        rep(i, vec[1].size()){
            c[vec[1][i]] = 3*(i+1);
        }
        rep(i, num[2]){
            c[vec[2][i]] += 3*i;
        }
        rep(i, num[1]){
            c[vec[2][num[2]+i]] = 1+3*i;
        }
        rep(i, vec[2].size()-num[1]-num[2]){
            c[vec[2][num[1]+num[2]+i]] = 3*(num[0]-i);
        }
    }
    else{
        rep(i, vec[2].size()){
            c[vec[2][i]] = 3*(i+1);
        }
        rep(i, num[1]){
            c[vec[1][i]] += 3*i;
        }
        rep(i, num[2]){
            c[vec[1][num[1]+i]] = 2+3*i;
        }
        rep(i, vec[1].size()-num[1]-num[2]){
            c[vec[1][num[1]+num[2]+i]] = 3*(num[0]-i);
        }
    }
    rep(i, N) cout << c[i] << ' ';
    cout << endl;
}

Measured memory_bytes: 15988
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int main(){
int n,k,m=0,p=0;
  cin>>n>>k;
  int X[n];
  rep(i,k)
    cin>>X[i];
  m=-min(X[0],0);
  p=max(X[k-1],0);
  int a=min(m+p*2,m*2+p);
  rep(i,n-k){
     cin>>X[i+k];
     m=-min(X[i+1],0);
     p=max(X[i+k],0);
     a=min({a,m+p*2,m*2+p});
  }
  cout<<a;
  return 0;
}

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

typedef long long ll;

int main()
{
  ll N, K;
  ll a[200000];
  ll sum = 0;
  std::cin >> N;

  for (ll i = 0; i < N; i++) {
    std::cin >> a[i];
    sum += a[i];
  }

  ll left = a[0];
  ll min_num = std::abs(2 * left - sum);
  for (ll i = 1; i < N - 1; i++) {
    min_num = std::min(std::abs(2 * left - sum), min_num);
    left += a[i];
  }

  std::cout << min_num << std::endl;

  return 0;
}


Measured memory_bytes: 1792
// https://atcoder.jp/contests/abc104/tasks/abc104_b
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <set>
#include <unordered_set>
#include <bitset>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n) for(size_t i = 0; i < (n); ++i)

int main()
{
    string S;
    cin >> S;
    int N = S.size();
    if ('A' == S[0])
    {
        int cnt = 0, idx = -1;
        for (int i = 2; i < N - 1; ++i)
        {
            if ('C' == S[i])
            {
                ++cnt;
                if (1 == cnt) idx = i;
            }
        }

        if (1 == cnt)
        {
            for (int i = 1; i < N; ++i)
            {
                if (0 == i || idx == i) continue;
                if ('a' > S[i] || 'z' < S[i])
                {
                    cout << "WA" << endl;
                    return 0;
                }
            }
            cout << "AC" << endl;
            return 0;
        }
        else
        {
            cout << "WA" << endl;
            return 0;
        }
        
    }
    cout << "WA" << endl;
    return 0;
}


Measured memory_bytes: 3616
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}

int main() {
    // メインプログラムここから
    ll n,m;cin>>n>>m;
    vector<ll>a(n),b(n),c(m),d(m);
    for(int i=0; i<n; i++){
        cin>>a[i]>>b[i];
    }
    for(int j=0; j<m; j++){
        cin>>c[j]>>d[j];
    }
    for(int i=0; i<n; i++){
        ll min_dist = 1e10;
        for(int j=0; j<m; j++){
            ll dist = abs(a[i]-c[j])+abs(b[i]-d[j]);
            min_dist=min(min_dist,dist);
            }
        for(int j=0; j<m; j++){
        ll dist = abs(a[i]-c[j])+abs(b[i]-d[j]);
            if (min_dist == dist)
            {cout<<j+1<<endl;
            break;}}
    }        
            
         
           
           
       
    
    // ここまで
}


Measured memory_bytes: 256
def lcs(str1, str2):
    '''文字列str1, str2の最長共通部分列(Lowest Common Subsequence, LCS)を求める
    計算量O(len(str1)*len(str2))  
    '''
    dp = [[0]*(len(str2) + 1) for _ in range(len(str1) + 1)]
    for i in range(len(str1)):
        for j in range(len(str2)):
            if str1[i] == str2[j]:
                dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1)
            else:
                dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

    '''復元を実行
    最長の長さを求めるなら return dp[len(str1)][len(str2)] をするとよい
    '''
    res = ''
    i, j = len(str1), len(str2)
    while i > 0 and j > 0:
        if dp[i][j] == dp[i-1][j]:
            i -= 1
        elif dp[i][j] == dp[i][j-1]:
            j -= 1
        else:
            res = str1[i-1] + res
            i -= 1
            j -= 1
    return res

s = input()
t = input()
print(lcs(s, t))

Measured memory_bytes: 111708
#include <iostream>
 
using namespace std;
 
int main()
{
    int L, A, B, C, D;
    cin >> L >> A >> B >> C >> D;
    const int a = (A / C) + ((A % C) != 0);
    const int b = (B / D) + ((B % D) != 0);
    cout<<L-max(a,b)<<"\n";
    return 0;
}

Measured memory_bytes: 1164
#include <bits/stdc++.h>
#define REP(type, i, a, b) for( type i = a; i < b; i++ )
typedef long long ll;
using namespace std;

int main(){
  int h,w,left,count;
  string s[2001];
  int x[2001][2001],y[2001][2001];
  scanf("%d %d",&h,&w);
  REP(int,i,0,h){
    cin >> s[i];
  }
  

  REP(int,i,0,h){
    REP(int,j,0,w) {x[i][j]=-1;y[i][j]=-1;}
  }

  REP(int,i,0,h){
    left=-1;
    REP(int,j,0,w){
      if(s[i][j]=='.'){
        if(left==-1){
          left=j;
          count=1;
        }else{
          count++;
        }
        if(j==w-1){
          REP(int,k,0,count) x[i][left+k]=count;
        }
      }else if(left!=-1){
        REP(int,k,0,count){ 
          x[i][left+k]=count;
        }
        left=-1;
        x[i][j]=0;
      }else{
        x[i][j]=0;
      }
    }
  }

  REP(int,j,0,w){
    left=-1;
    REP(int,i,0,h){
      if(s[i][j]=='.'){
        if(left==-1){
          left=i;
          count=1;
        }else{
          count++;
        }
        if(i==h-1){
          REP(int,k,0,count) y[left+k][j]=count;
        }
      }else if(left!=-1){
        REP(int,k,0,count){ 
          y[left+k][j]=count;
          y[i][j]=0;
        }
        left=-1;
      }else{
        y[i][j]=0;
      }
    }
  }

  /*REP(int,i,0,h){
    REP(int,j,0,w) printf("%d ",x[i][j]);
    printf("\n");
  }
  printf("\n");
  REP(int,i,0,h){
    REP(int,j,0,w) printf("%d ",y[i][j]);
    printf("\n");
  }*/

  int ans=0;
  REP(int,i,0,h){
    REP(int,j,0,w) if(ans<x[i][j]+y[i][j]-1){ ans=x[i][j]+y[i][j]-1; }
  }
  printf("%d\n", ans);
}

Measured memory_bytes: 35584
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647 >> 1); // 10^9 + 5000
static const ll LLINF = (9223372036854775807 >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;

namespace Printer{
    template<class T>
    void print(T x){
        cout << x << endl;
    }

    template<class T>
    void print(T x, T y){
        cout << x << "," << y << endl;
    }

    template<class T>
    void print(vector<T> v){
        rep(i, v.size()){
            if(i) cout << " ";
            cout<< v[i];
        }
        cout << endl;
    }

    template<class T>
    void print(vector<vector<T> > vv){
        rep(i, vv.size()){
            rep(j, vv[i].size()){
                if(j) cout << " ";
                cout << vv[i][j];
            }
            cout << endl;
        }
    }

    void print(pair<int, int> p){
        cout << "(" << p.first << ", " << p.second << ")" << endl;
    }
};
using namespace Printer;

//--global--//



//----------//

int main(int argc, const char * argv[]) {
    //提出時、消す----//
    //--------------//

    ll n, k; cin >> n >> k;
    vector<ll> h(n);
    rep(i, n) cin >> h[i];
    sort(ALL(h), greater<ll>());
    ll sum = 0;
    rep(i, n){
        if(k > 0){ 
            h[i] = 0;
            k--;
        }
        sum += h[i];
    }

    cout << sum << endl;
    
    return 0;
}


Measured memory_bytes: 1792
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
g = []
for i in range(n+1): g.append([])

for i in range(n-1):
    a, b, c = na()
    g[a].append((b,c))
    g[b].append((a,c))

q,k = na()

def dfs(cur, pre, g, par, dw, w):
    dw[cur] = w
    par[cur] = pre
    for e in g[cur]:
        if e[0] != pre:
            dfs(e[0], cur, g, par, dw, w + e[1])

par = [-1] * (n+2)
dw = [-1] * (n+2)
from queue import Queue
qs = Queue()
qs.put(k)
dw[k] = 0

while not qs.empty():
    cur = qs.get()
    for e in g[cur]:
        if e[0] != par[cur]:
            par[e[0]] = cur
            dw[e[0]] = dw[cur] + e[1]
            qs.put(e[0])


# dfs(k, -1, g, par, dw, 0)

for z in range(q):
    x, y = na()
    print(dw[x] + dw[y])



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

int main() {
   ll i,j,k;
    ll n;
    string s,ans="Yes";
    cin >> n >> s;

    if(n%2!=0)
    {
      ans="No";
    }
    for(i=0; i<n/2; i++)
    {
      if(s[i]!=s[n/2+i])
      {
        ans="No";
      }
    }
  cout <<  ans<< endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstring>
using namespace std;
int main(){
	int T,h,w;
	cin >> T;
	while(T--){
		cin >> h >> w;
		char map[22][22];
		memset(map,'#',sizeof map);
		int t[2];
		int v[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
		int d = 0;
		for(int i = 1; i <= h; ++i){
			for(int j = 1; j <= w; ++j){
				cin >> map[i][j];
				switch(map[i][j]){
					case '>':
						++d;
					case '<':
						++d;
					case 'v':
						++d;
					case '^':
						t[0] = i;
						t[1] = j;
						map[i][j] = '.';
				}
			}
		}
		int n;
		cin >> n;
		char c[101];
		cin >> c;
		char *com = c;
		while(*com){
			int _d = 4;
			switch(*com){
				case 'U':
					--_d;
				case 'D':
					--_d;
				case 'L':
					--_d;
				case 'R':
					--_d;
					d = _d;
					if(map[t[0]+v[d][0]][t[1]+v[d][1]] == '.'){
						t[0] += v[d][0];
						t[1] += v[d][1];
					}
					break;
				default:
					int b[] = {t[0],t[1]};
					bool f = true;
					while(f){
						b[0] += v[d][0];
						b[1] += v[d][1];
						switch(map[b[0]][b[1]]){
							case '*':
								map[b[0]][b[1]] = '.';
							case '#':
								f = false;
						}
					}
			}
			++com;
		}
		char td[] = {'^','v','<','>'};
		map[t[0]][t[1]] = td[d];
		for(int i = 1; i <= h; ++i){
			for(int j = 1; j <= w; ++j){
				cout << map[i][j];
			}
			cout << endl;
		}
		if(T) cout << endl;
	}
}

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

using namespace std;



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

	int N, M;
	cin >> N >> M;

	int sum = 0;

	for(int i = 0, x; i < M; ++i) {
		cin >> x;
		sum += x;

		if (sum > N) {
			cout << -1 << '\n';
			return 0;
		}
	}

	cout << (N - sum) << '\n';

	return 0;	
}

Measured memory_bytes: 3664
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        try (Scanner sc = new Scanner(System.in)) {
            int A = sc.nextInt();
            int B = sc.nextInt();

            int ans = Math.max(Math.max(A + B, A - B), A * B);

            System.out.println(ans);
        }
    }

}


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

using ll = long long;

#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                     \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \
       << endl
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

void solve(ll L, ll R, ll d) {
  ll cnt = 0;
  for (int i = L; i <= R; i++) {
    if (i % d == 0) cnt++;
  }
  cout << cnt << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll L;
  scanf("%lld", &L);
  ll R;
  scanf("%lld", &R);
  ll d;
  scanf("%lld", &d);
  solve(L, R, d);
  return 0;
}


Measured memory_bytes: 3716
# エラトステネスの篩
def make_prime_table(N):
    sieve = [0] * (N + 1)
    sieve[0] = -1
    sieve[1] = -1
    for i in range(2, N + 1):
        if sieve[i] != 0:
            continue
        sieve[i] = i
        for j in range(i * i, N + 1, i):
            if sieve[j] == 0:
                sieve[j] = i
    return sieve

N = int(input())

prime_table = make_prime_table(100)

d = {}
for i in range(2, N + 1):
    while i != 1:
        d.setdefault(prime_table[i], 0)
        d[prime_table[i]] += 1
        i //= prime_table[i]

# 75 = 5 * 5 * 3
#    = 15 * 5
#    = 25 * 3
#    = 75
n74 = 0
n24 = 0
n14 = 0
n4 = 0
n2 = 0
for k in d:
    if d[k] >= 74:
        n74 += 1
    if d[k] >= 24:
        n24 += 1
    if d[k] >= 14:
        n14 += 1
    if d[k] >= 4:
        n4 += 1
    if d[k] >= 2:
        n2 += 1

result = 0
# x ^ 4 * y ^ 4 * z ^ 2 の約数の個数は75個
result += n4 * (n4 - 1) // 2 * (n2 - 2)
# x ^ 14 * y ^ 4 の約数の個数は75個
result += n14 * (n4 - 1)
# x ^ 24 * y ^ 2 の約数の個数は75個
result += n24 * (n2 - 1)
# x ^ 74 の約数の個数は75個
result += n74
print(result)


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

#define ull unsigned long long int
#define ll long long
#define vi vector<int>
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define endl "\n"
#define pb push_back
#define MOD 1e9+7
#define INF 0x3f3f3f3f

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

int main()
{
  int N; cin >> N;
    vi A(N), B(N); for(int i=0 ; i<N;i++) cin >> A[i] >> B[i];
    sort(all(A));
    sort(all(B));
    ll X, Y; 
    if (N % 2 == 1) {
        X = A[N/2];
        Y = B[N/2];
    } else {
        X = A[N/2 - 1] + A[N/2];
        Y = B[N/2 - 1] + B[N/2];
    }
 
    cout << Y-X+1 << endl;
	
	return 0;
}


Measured memory_bytes: 4836
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define DEBUG(x)  cout << #x << " = " << (x) << endl;
#define SORT(x) sort(ALL(x));
#define RSORT(x) sort(RALL(x));
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int inf = 1<<30;
const ll mod = 1000000007;
const double eps = 1e-9;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//cout << fixed << setprecision(10);

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

int main () {
  ll N, K; cin >> N >> K;
  
  if (K%2 != 0) {
    cout << (N/K)*(N/K)*(N/K) << endl;
  }
  else {
    cout << (N/K)*(N/K)*(N/K)
          + ((N+K/2)/K)*((N+K/2)/K)*((N+K/2)/K) << endl;
  }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
int l[110];
int main(){ _;
  int n;cin>>n;
  REP(i,n)cin>>l[i];
  sort(l,l+n);
  int b = accumulate(l,l+n-1,0);
  if(b>l[n-1])
    cout<<"Yes"<<endl;
  else
    cout<<"No"<<endl;
}


Measured memory_bytes: 256
import collections

N=int(input())
A_data=list(map(int,input().split()))
mod=10**9+7

A=collections.Counter(A_data)

f=True
if N%2==1:
    if not A[0]==1:
        ans=0
    else:
        for i in range(2,N,2):
            if not A[i]==2:
                ans=0
                f=False
                break
        if f==True:
            ans=2**((N-1)//2)%mod
elif N%2==0:
    for i in range(1,N,2):
        if not A[i]==2:
            ans=0
            f=False
            break
    if f==True:
        ans=2**(N//2)%mod

print(ans)

Measured memory_bytes: 14820
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>

using namespace std;

#define INF 1 << 29
#define LL long long int

LL const MOD = 1000000007;

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

    LL n;
    cin >> n;

    vector<LL> t(n),a(n);

    for(int i = 0; i < n; i++){
        cin >> t[i];
    }

    for(int i = 0; i < n; i++){
        cin >> a[i];
    }

    set<LL> stt,sta,st;

    stt.insert(0);
    sta.insert(n-1);

    for(int i = 1; i < n; i++){
        if(t[i] > t[i-1]){
            stt.insert(i);
        }
    }

    for(int i = n-2; i >= 0; i--){
        if(a[i] > a[i+1]){
            sta.insert(i);
        }
    }

    for(int i = 0; i < n; i++){
        auto it = stt.find(i);
        if(it != stt.end()){
            if(t[i] > a[i]){
                cout << 0 << endl;
                return 0;
            }else{
                st.insert(i);
            }
        }
    }

    for(int i = 0; i < n; i++){
        auto it = sta.find(i);
        if(it != sta.end()){
            if(t[i] < a[i]){
                cout << 0 << endl;
                return 0;
            }else{
                st.insert(i);
            }
        }
    }

    LL ans = 1;

    for(int i = 0; i < n; i++){
        if(st.find(i) == st.end()){
            ans *= min(t[i],a[i]);
            ans %= MOD;
        }
    }

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

Measured memory_bytes: 11264
#include <vector>
#include <iostream>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, a[100009], cnt; vector<int> g[100009];
int rec(int pos, int pre) {
	int e = 0;
	for (int i : g[pos]) {
		if (i == pre) continue;
		e = max(e, rec(i, pos));
	}
	if (e == m - 1) {
		if(pre != 0) cnt++;
		return 0;
	}
	return e + 1;
}
int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]), a[i]--;
	for (int i = 1; i < n; i++) g[a[i]].push_back(i);
	for (int i : g[0]) rec(i, 0);
	printf("%d\n", cnt + (a[0] != 0 ? 1 : 0));
	return 0;
}

Measured memory_bytes: 10752
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
signed main(){
    IOS;
    int n,x,t;
    cin>>n>>x>>t;
    cout<<((n+x-1)/x)*t;
}

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

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int X,A; cin >> X >>A;
  if(X<A){
    cout << 0 <<endl;
  }else
    cout << 10 << endl;
      
}


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

vector<bool> prime(int n){
    vector<bool> is_prime(n+1, true);
    is_prime.at(0) = false;
    is_prime.at(1) = false;
    for (int i = 4; i < n+1; i += 2){
        is_prime.at(i) = false;
    }
    for (int i = 3; i < sqrt(n)+1; i += 2){
        if (is_prime.at(i) == false){
            continue;
        }
        for (int j = i*2; j < n+1; j += i){
            is_prime.at(j) = false;
        }
    }
    
    return is_prime;
}


int main(){
    int q;
    cin >> q;
    vector<bool> prime_list = prime(pow(10, 5));
    
    vector<int> s(pow(10, 5)+1, 0);
    int n;
    for (int i = 1; i < pow(10, 5)+1; i++){
        s[i] = s[i-1];
        if (prime_list[i] == true && prime_list[(i+1)/2] == true){
            s[i]++;
        }
    }
    int l, r, ans;
    for (int i = 0; i < q; i++){
        cin >> l >> r;
        ans = s[r] - s[l-1];
        cout << ans << endl;
    }
}

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

struct cake{
    int w,h;
    cake(){
        w = 0;
        h = 0;
    }
    cake(int w, int h){
        this->w = w;
        this->h = h;
    }
};

int main() {

    while(true){

        int n,w,d;

        cin >> n >> w >> d;

        if(n == w && w == d && d == 0){
            break;
        }

        cake start(w,d);

        list<cake> lc;

        lc.push_back(start);

        for(int loop=0; loop<n; loop++){

            int p,s;

            cin >> p >> s;

            list<cake>::iterator cut = lc.begin();

            for(int i=0; i<p-1; i++){
                cut++;
            }

            int l = cut->w * 2 + cut->h * 2;

            int sm = s % l;

            cake first,second;

            if((sm < cut->w)){
                int cw1 = sm;
                int cw2 = cut->w-sm;
                first.w = cw1;
                second.w = cw2;
                first.h = second.h = cut->h;
                if(first.w * first.h > second.w * second.h){
                    swap(first,second);
                }
                lc.push_back(first);
                lc.push_back(second);
            }
            else if(sm > (cut->w + cut->h) && sm < (cut->w*2 + cut->h)){ //垂直に切る
                sm -= cut->w + cut->h;
                int cw1 = sm;
                int cw2 = cut->w-sm;
                first.w = cw1;
                second.w = cw2;
                first.h = second.h = cut->h;
                if(first.w * first.h > second.w * second.h){
                    swap(first,second);
                }
                lc.push_back(first);
                lc.push_back(second);
            }
            else if(sm > cut->w && sm < (cut->w + cut->h)){
                sm -= cut->w;
                int ch1 = sm;
                int ch2 = cut->h-sm;
                first.h = ch1;
                second.h = ch2;
                first.w = second.w = cut->w;
                if(first.w * first.h > second.w * second.h){
                    swap(first,second);
                }
                lc.push_back(first);
                lc.push_back(second);
            }
            else{
                sm -= cut->w * 2 + cut->h;
                int ch1 = sm;
                int ch2 = cut->h-sm;
                first.h = ch1;
                second.h = ch2;
                first.w = second.w = cut->w;
                if(first.w * first.h > second.w * second.h){
                    swap(first,second);
                }
                lc.push_back(first);
                lc.push_back(second);
            }

            lc.erase(cut);

        }

        priority_queue<int> q;

        for(list<cake>::iterator itr = lc.begin(); itr != lc.end(); itr++){
            q.push(-(itr->h * itr->w));
        }

        int siz = q.size();

        for(int i=0; i<siz - 1; i++){
            cout << -q.top() << " ";
            q.pop();
        }

        cout << -q.top() << endl;

    }

    return 0;
}


Measured memory_bytes: 3072
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

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

constexpr ll INF = 1ll<<60;

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

template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }

template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }

ll N;
ll L[100010], R[100010];
ll maL, miR = INF;
ll li, ri;
std::vector<P> ps;
ll right[100010];

int main()
{
  std::cin >> N;

  rep( i, N )
  {
    std::cin >> L[i] >> R[i];

    if( chmax( maL, L[i] ) )
      li = i;
    if( chmin( miR, R[i] ) )
      ri = i;

    ps.emplace_back( L[i], R[i] );
  }

  ll ans = 0;

  rep( i, N ) if( i != li && i != ri )
    chmax( ans, R[i]-L[i]+1 );

  if( miR >= maL )
    ans += miR-maL+1;

  if( li == ri )
  {
    std::cout << ans << std::endl;

    return 0;
  }

  std::sort( all(ps) );

  right[N] = INF;

  repd( i, N )
  {
    if( ps[i] == P( L[ri], R[ri] ) )
      right[i] = right[i+1];
    else
      right[i] = std::min( right[i+1], ps[i].second );
  }

  ll l = L[ri];
  ll r = R[li];

  rep( i, N+1 )
  {
    chmax( ans, std::max( 0ll, miR-l+1 )+std::max( 0ll, std::min(right[i],r)-maL+1 ) );

    if( ps[i] != P( L[li], R[li] ) )
      chmax( l, ps[i].first );
  }

  std::cout << ans << std::endl;

  return 0;
}

Measured memory_bytes: 4720
#include <bits/stdc++.h>
using namespace std;
int n;
int a[107],ans=0,cnt=107;
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=2;i<=n;i++){
		if(a[i]==a[i-1]){
			ans++;
			a[i]=++cnt;
		}
	}
	cout<<ans;
}


Measured memory_bytes: 256
class UnionFind:
    def __init__(self, n):
        self.p=[i for i in range(n)]
        self.r=[0 for i in range(n)]
    
    def root(self,x):
        if self.p[x]==x:
            return x
        else:
            self.p[x] = self.root(self.p[x])
            return self.p[x]
    
    def uni(self,x,y):
        x=self.root(x)
        y=self.root(y)
        if(x==y):
            return
        if self.r[x]<self.r[y]:
            self.p[x]=y
        else:
            self.p[y]=x
            if self.r[x]==self.r[y]:
                self.r[x]+=1
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

n,q=map(int,input().split())
uf=UnionFind(n)
for i in range(q):
    c,x,y=map(int,input().split())
    if c==0:
        uf.uni(x,y)
    else:
        print(uf.same(x,y)+0)


Measured memory_bytes: 6040
#include <iostream>
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

#define INF 1000000000
#define MOD 1000000007
#define rep(i,a,b) for(uint32 i = (a); i < (b); ++i)
#define bitget(a,b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(),(x).end()
#define C(x) std::cout << #x << " : " << x << std::endl
#define scanf scanf_s

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;

template<typename T>
class hashmap64 {
	std::vector<std::pair<uint64, T>> map;
	uint64 mask, count, temp;
	uint64 hash64(uint64 key) {
		key = ~key;
		key ^= key << 13;
		key ^= key >> 19;
		return key ^ (key << 17);
	}
public:
	hashmap64() :map(8192, std::make_pair(~static_cast<std::uint_fast64_t>(0), T())) {
		mask = 8191; count = 0;
	}
	hashmap64(size_t s) {
		s |= s << 1;
		s |= s >> 2;
		s |= s >> 4;
		s |= s >> 8;
		s |= s >> 16;
		s |= s >> 32;
		mask = s;
		count = 0;
		map.resize(s + 1, std::make_pair(~static_cast<std::uint_fast64_t>(0), T()));
	}
	T &operator[](uint64 suf) {
		temp = mask&hash64(suf);
		while (map[temp].first != suf) {
			temp = mask&(temp + 1);
		}
		return map[temp].second;
	}
	void insert(uint64 key, const T &value) {
		temp = mask&hash64(key);
		while (~map[temp].first) {
			if (map[temp].first == key) {
				map[temp].second = value;
				return;
			}
			temp = mask&(temp + 1);
		}
		map[temp] = std::make_pair(key, value);
		++count;
		if ((count << 1) > mask) {
			std::pair<uint64, T> mkey;
			mask = mask << 1 | 1;
			map.resize(mask + 1, std::make_pair(~static_cast<std::uint_fast64_t>(0), T()));
			for (uint64 t = 0;t <= mask >> 1;++t) {
				if (~map[t].first) {
					mkey = map[t];
					map[t].first = ~static_cast<std::uint_fast64_t>(0);
					temp = mask&hash64(mkey.first);
					while (~map[temp].first) {
						temp = mask&(temp + 1);
					}
					map[temp] = mkey;
				}
			}
		}
		return;
	}
	bool find(uint64 key) {
		temp = mask&hash64(key);
		while (~map[temp].first) {
			if (map[temp].first == key) {
				return true;
			}
			temp = mask&(temp + 1);
		}
		return false;
	}
	bool empty(void) {
		return !count;
	}
	size_t size(void) {
		return count;
	}
};

int main(void) {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	hashmap64<uint32> map(3);
	uint32 n;
	std::cin >> n;
	int64 a, sum = 1e15;
	uint32 ans = 0;
	map.insert(sum, 0);
	rep(i, 0, n) {
		std::cin >> a;
		sum += a;
		if (map.find(sum)) {
			ans = std::max(ans, i + 1 - map[sum]);
		}
		else {
			map.insert(sum, i + 1);
		}
	}
	std::cout << ans << "\n";
	return 0;
}

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

int main(){
double sol = 0;
double c;
double x,y;
int n;
cin>>n;
vector<pair<int,int>>data;
for(int i=0;i<n;i++){
  int a,b;
  cin>>a>>b;
  data.push_back(make_pair(a,b));
}
    for(int r = 0; r < 100; r++) {
        random_shuffle(data.begin(), data.begin() + n);
        for(int i = 0; i < n; i++) {
            double x = data[i].first, y = data[i].second, c = sqrt(pow(x, 2) + pow(y, 2));
            for(int j = 0; j < n; j++) {
                if(i == j) continue;
                x += data[j].first;
                y += data[j].second;
                double length = sqrt(pow(x, 2) + pow(y, 2));
                if(length > c) c = length;
                else {
                    x -= data[j].first;
                    y -= data[j].second;
                }
            }
            sol = max(c, sol);
        }
    }
        cout<<setprecision(40)<<sol<<endl;
}    



Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}

// 0 false, 1 true 
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

int main() {
    std::cout << std::fixed << std::setprecision(15);
    
    while(true){
        ll h; cin >> h;
        if(h == 0){
            break;
        }else{
            vector<vector<ll>> stone(h, vector<ll>(5));
            rep(i, h){
                rep(j, 5){
                    cin >> stone[i][j];
                }
            }
            ll score = 0;

            while(true){
                ll tmp = score;
                //スコアの計算
                rep(i, h){
                    ll num = 0; ll num_s = -1;
                    rep(j, 5){
                        if(j == 0){
                            num = stone[i][j]; num_s = 0; continue;
                        }

                        if(stone[i][j] == num){
                            if(j == 4){
                                if(j - num_s >= 2){
                                    for(ll k = num_s; k <= j; k++){
                                        score += num; stone[i][k] = 0;
                                    }
                                }
                            }
                        }else{
                            if(j - 1 - num_s >= 2){
                                for(ll k = num_s; k < j; k++){
                                    score += num; stone[i][k] = 0;
                                }
                            }
                            num = stone[i][j]; num_s = j;
                        }
                    }
                }

                //降ろす
                rep(j, 5){
                    for(ll i = h-1; i >= 0; i--){
                        for(ll k = i-1; k >= 0; k--){
                            if(stone[i][j] == 0){
                                swap(stone[i][j], stone[k][j]);
                            }
                        }
                    }
                }

                if(tmp == score){
                    break;
                }
            }
            
            /*
            //テスト用出力
            rep(i, h){
                rep(j, 5){
                    cout << stone[i][j] << " ";
                }
                br;
            }
            */
            cout << score << endl;

        }
    }

}


Measured memory_bytes: 3148
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))


import bisect

cum_a = [0]
for a in A:
    cum_a.append(cum_a[-1] + a)

cum = [0]
for b in B:
    cum.append(cum[-1] + b)

ans = 0
for i in range(n + 1):
    t = cum_a[i]
    if t <= k:
        bnum = i
    else:
        bnum = 0
    if t <= k:
        idx = bisect.bisect_right(cum, k - t)
        bnum += idx - 1
    ans = max(ans, bnum)
print(ans)


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

int main(){
  ll a,b,c,d,ans;
  cin >> a >> b >> c >> d;
  ans = max(max(a*c,a*d),max(b*c,b*d));
  
  cout << ans << endl;
  
  return 0;
}


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

using namespace std ;

long long fpow(long n , long long m){
 	long long res = 1 ;
  while(m>0){
   	if(m&1) res*=n ;
    m/=2 ;
    n*=n ;
  }
  return res ;
}

int main(){
  	long long n ;
  cin >> n ;
  int pow = ceil(log2(n+1)) ;
  cout << fpow(2LL , pow)- 1;
 	return 0 ; 
}

Measured memory_bytes: 512
def dfs(r, c):
    global color
    global g
    s = [r]
    color[r] = c
    while len(s) > 0:
        u = s.pop()
        for u in g[u]:
            v = color[u]
            if v is None:
                color[u] = c
                s.append(u)
                
def assigncolor():
    global g
    global color
    global n
    c = 1
    for i in range(n):
        if color[i] == None:
            dfs(i, c)
            c += 1
            
n, m = list(map(int, input().split()))

g = [[] for i in range(n)]
for i in range(m):
    s, t = list(map(int, input().split()))
    g[s].append(t)
    g[t].append(s)
    
color = [None for i in range(n)]

assigncolor()

q = int(input())
for i in range(q):
    s, t = list(map(int, input().split()))
    if color[s] == color[t]:
        print("yes")
    else:
        print("no")

Measured memory_bytes: 23388
#include <iostream>
#include <climits>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#define MAX 1000000007


using namespace std;
#define ll long long
#define dbg if(0)
#define ISRANGE(val,lo,hi) ((lo<=val)&&(val<hi))



int main(){
    ll n,k;
    double ans=0.0;
    cin >>n>>k;
    for(int i=1;i<=n;i++){
        double a=pow(0.50,ceil(log2((double)k/(double)i)))/(double)n;
        if(i>k)ans+=1/(double)n;
        else ans+=a;
    }
    //ans/=(double)n;

    cout <<setprecision(12)<<ans<<endl;


    return 0;

}

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


int main(void){
    int N;
    scanf("%d",&N);
    int P[N];
    char S[N][11];
    char temp[11];
    int num[N];
    for(int i=0;i<N;i++){
        scanf("%s %d",S[i],&P[i]);
        num[i] = i+1;
    }
    int itemp = 0;
    for(int i=1;i<N;i++){
        for(int j=1;j<N;j++){
        if(strcmp(S[j-1], S[j])>0){
            strcpy(temp, S[j-1]);
            strcpy(S[j-1], S[j]);
            strcpy(S[j], temp);
            itemp = P[j-1];
            P[j-1] = P[j];
            P[j] = itemp;            
            itemp = num[j-1];
            num[j-1] = num[j];
            num[j] = itemp;
            }
        }
    }
    for(int i=1;i<N;i++){
        for(int j=1;j<N;j++){
        if(strcmp(S[j-1], S[j])==0){
            if(P[j-1]<P[j]){
                itemp = P[j-1];
                P[j-1] = P[j];
                P[j] = itemp;            
                itemp = num[j-1];
                num[j-1] = num[j];
                num[j] = itemp;
            }
        }
        }
    }

    for(int i=0;i<N;i++){
        printf("%d\n",num[i]);
    }

    return 0;
}

Measured memory_bytes: 128