akhauriyash/Code-Regression huggingface dataset:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])
    
    if temp != 1:
        arr.append([temp, 1])
    if not arr:
        arr.append([n, 1])
    
    return arr


def mod_div(x, y, mod=10 ** 9 + 7):
    return x * pow(y, mod - 2, mod) % mod


def solve():
    _ = int(rl())
    A = list(map(int, rl().split()))
    MOD = 10 ** 9 + 7
    
    max_exp = defaultdict(int)
    for ai in A:
        facts = factorization(ai)
        for fact, m in facts:
            if max_exp[fact] < m:
                max_exp[fact] = m
    
    lcm = 1
    for num, exp in max_exp.items():
        lcm = lcm * pow(num, exp, MOD) % MOD
    
    ans = sum([mod_div(lcm, ai) for ai in A]) % MOD
    print(ans)


if __name__ == '__main__':
    solve()


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

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

  bool ans= true;
  unordered_set<int> data;
  for(int i=0; i<N; i++){
    cin >> buf;
    if(data.count(buf)){
      ans= false;
      break;
    }
    else{
      data.insert(buf);
    }
  }

  if(ans) cout << "YES" << endl;
  else    cout << "NO" << endl;
}

Measured memory_bytes: 8520
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
  ll L,R;
  cin>>L>>R;
  if(R-L>=2019){
    cout<<0;
    return 0;
  }
  ll l=L%2019,r=R%2019;
  if(l>=r){
    r+=2019;
  }
  set<ll> vec;
  for(ll i=l;i<r;i++){
    for(ll j=i+1;j<=r;j++){
      vec.insert((i*j)%2019);
    }
  }
  cout<<*vec.begin();
} 

Measured memory_bytes: 384
#include <iostream>
#include <iomanip>
#include <string>
#include <unordered_map>
#include <cmath>

typedef long long ll;
typedef long double ld;

using namespace std;
int main()
{
  ll n, sum = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++)
  {
    if (i % 3 != 0 && i % 5 != 0)
    {
      sum += i;
    }
  }

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


Measured memory_bytes: 3620
def z_algo(s: str) -> list:
    """LCP(0,i)のlist"""
    N = len(s)
    ret = [-1] * N
    ret[0] = N
    i, j = 1, 0
    while i < N:
        while i + j < N and s[j] == s[i + j]:
            j += 1
        ret[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i + k < N and k + ret[k] < j:
            ret[i + k] = ret[k]
            k += 1
        i += k
        j -= k
    return ret


def main():
    N = int(input())
    S = input()

    ret = 0
    t = ''
    for c in reversed(S):
        t = c + t
        lcp = z_algo(t)
        for diff, len_ in enumerate(lcp):
            ret = max(ret, min(diff, len_))
    print(ret)


if __name__ == '__main__':
    main()


Measured memory_bytes: 49116
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<functional>
#include<queue>
#include<stack>
#include<math.h>
#define INF 1000000001
#define EPS 1.0e-6

using namespace std;

typedef long long ll;

int n, W;
int v[105];
int w[105];


int dp[105][10500];

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

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

	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= W; j++) {
			if (j < w[i])dp[i + 1][j] = dp[i][j];
			else {
				dp[i + 1][j] = max( dp[i][j],dp[i+1][j - w[i]] + v[i] );
			}
		}
	}

	cout << dp[n][W] << endl;

	return 0;
}

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

int main()
{
int N,M,C,ans=0;scanf("%d %d %d",&N,&M,&C);
vector<int> B(M);vector<vector<int>> A(N,vector<int>(M));
for(int i=0;i<M;i++)scanf("%d",&B[i]);
for(int i=0;i<N;i++)for(int j=0;j<M;j++)scanf("%d",&A[i][j]);
for(int i=0;i<N;i++)
{
  int che=C;
  for(int j=0;j<M;j++)che+=A[i][j]*B[j];
  if(che>0)ans++;
}
printf("%d",ans);
}

Measured memory_bytes: 636
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
#include <cstring>
#define LEN 101
using namespace std;

int main(int argc, char const *argv[])
{
    char a[LEN * 2 + 1], b[LEN];
    scanf("%s", a);
    scanf("%s", b);
    int N = strlen(a);
    for(int i = 0; i < N; i ++){
        a[i + N] = a[i];
    }
    a[2*N] = 0;
    if(strstr(a, b) == NULL){
        cout << "No" << endl;
    }else{
        cout << "Yes" << endl;
    }
    return 0;
}


Measured memory_bytes: 3240
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define rep(i,n) for(int i=0; i<n; i++)
#define pb(a) push_back(a)
#define mp make_pair

bool IsPrime(int num);

int main()
{

  int x, ans;

  cin >> x;

  int i = x;

  while(1){

    if(IsPrime(i) == true){
      ans = i;
      break;
    }

    i++;

  }

  cout << ans << endl;

  return 0;

}

bool IsPrime(int num)
{
  if (num < 2) return false;
  else if (num == 2) return true;
  else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
  
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2)
    {
      if (num % i == 0)
        {
	  // 素数ではない
	  return false;
        }
    }
  
  // 素数である
  return true;
}


Measured memory_bytes: 256
#include<stdio.h>

#define N 3

int main(void) {
        int nums[N], sort_buf, min;
        int i, j;

        scanf("%d %d %d", &nums[0], &nums[1], &nums[2]);

        if(1 > nums[0] || nums[1] > 10000 || 1 > nums[1] || nums[1] > 10000 || 1 > nums[2] || nums[2] > 10000) {
                printf("input error\n");
                return 1;
        }

        for(i=0;i<N-1;i++) {
                min = i;
                for(j=i+1;j<N;j++) {
                        if(nums[min] > nums[j]) {
                                min = j;
                        }
                }
                sort_buf = nums[i];
                nums[i] = nums[min];
                nums[min] = sort_buf;
        }

        for(i=0;i<N;i++) {
                printf("%d",nums[i]);
                if(i != N-1) printf(" ");
        }
        printf("\n");

        return 0;
}





Measured memory_bytes: 2120
w,h,n=map(int,input().split())
l = [[int(i) for i in input().split()] for i in range(n)]
right=w
left=0
up=h
down=0
# l [i] [j] は、i個目の箱のインデックスjの要素。
for i in range(n):
    if l[i][2]==1:
        left=max(left,l[i][0])
    elif l[i][2]==2:
        right=min(right,l[i][0])
    elif l[i][2]==3:
        down=max(down,l[i][1])
    elif l[i][2]==4:
        up=min(up,l[i][1])
square_size=(right-left)*(up-down)
if right-left>0 and up-down>0:
    print(square_size)
else:
    print('0')

Measured memory_bytes: 3064
#include <iostream>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <algorithm>
using namespace std;

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

  int cnt=0;
  for(int i=a; i<=b; i++){
    string s = to_string(i), t = to_string(i);
    reverse(t.begin(), t.end());
    if(s==t) cnt++;
  }

  cout << cnt << endl;

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1e18
#define PI 3.14159265358979

using namespace std;

double dist(double a, double b, double c, double d){
  return sqrt(pow(a-c, 2) + pow(b-d, 2));
}

int main(void){
  double a, b, h, m;
  cin >> a >> b >> h >> m;

  // 時間
  double theta1 = 1.0*(h*60+m)/720*2*PI;
  // 分
  double theta2 = 1.0*m / 60*2*PI;

  double x1 = a*sin(theta1);
  double y1 = a*(cos(theta1));
  double x2 = b*sin(theta2);
  double y2 = b*(cos(theta2));

  printf("%.11lf\n", dist(x1, y1, x2, y2));



  return 0;
}


Measured memory_bytes: 4128
# frozen_string_literal: true

n, m, q = gets.split.map(&:to_i)
arr = []
q.times do
  a, b, c, d = gets.split.map(&:to_i)
  arr << [a, b, c, d]
end
sum = 0
[*(1..m)].repeated_combination(n).to_a.each do |ar|
  comp = 0
  all = 0
  arr.each do |a, b, c, d|
    all += d
    comp += if (ar[b - 1] - ar[a - 1]) == c
              d
            else
              0
            end
  end
  sum = comp if sum < comp
end
puts sum

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

int main()
{
    int n;
    cin >> n;
    set<int> s;
    int sum = 0;
    while(n--)
    {
        int a;
        cin >> a;
        if(!s.count(a))
        {
            sum++;
            s.insert(a);
        }
        else
        {
            sum--;
            s.erase(a);
        }
    }
    cout << sum << endl;
    return 0;
}


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

using namespace std;

int main(void)
{
    int x,k,q;
    vector<int> r;
    vector<int> a,t;

    cin>>x>>k;
    
    for(int i = 0;i < k;i++)
    {
	int temp;
	cin>>temp;
	r.push_back(temp);
    }
    cin>>q;

    for(int i = 0;i < q;i++)
    {
	int ta,tt;

	cin>>tt>>ta;
	t.push_back(tt);
	a.push_back(ta);
    }

    int target = 0;
    int position = 0;
    int ctime = 0;
    int sign = -1;
    int dx = 0;
    int minimum = 0,maximum = x;

    while(position < q)
    {
	if(target == k || t[position] < r[target])
	{
	    int f = max(minimum,min(maximum,a[position]+dx)) + sign*(t[position] - ctime);
	    f = max(0,min(x,f));
	    cout<<f<<endl;
	    position++;
	} else {
	    //Calc variaion
	   int diff = sign*(r[target] - ctime);

	   minimum += diff;
	   maximum += diff;
	   dx += diff;

	   minimum = min(x,minimum);
	   if(minimum < 0)
	       minimum = 0;
	   maximum = min(x,maximum);
	   if(maximum < 0)
	       maximum = 0;

	   sign *= -1;
	   ctime = r[target];
	   target++;
	}

    }
    return 0;
}



Measured memory_bytes: 2808
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const ll LINF = 1LL<<60;

const int mxV = 1e5+1;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    if (fopen("input.in", "r")) {
        freopen("input.in", "r", stdin);
        freopen("output.out", "w", stdout);
    }

    int n, w; cin >> n >> w;
    vector<pair<ll, ll>> items(n+1);
    for (int i = 1; i <= n; i++) cin >> items[i].first >> items[i].second;

    // dp[items][value] = min weight to achieve this value
    vector<vector<ll>> dp(n+1, vector<ll>(mxV, LINF));

    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < mxV; j++) {
            // take item
            if (j-items[i].second >= 0) dp[i][j] = min(dp[i][j], dp[i-1][j-items[i].second] + items[i].first);
            // don't take item
            dp[i][j] = min(dp[i][j], dp[i-1][j]);
        }
    }

    ll ans = -LINF;
    for (ll v = 0; v < mxV; v++) {
        if (dp[n][v] <= w) ans = max(ans, v);
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 80256
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
ABC 131 A
https://atcoder.jp/contests/abc131/tasks/abc131_a
*/

int main(void)
{
  int s;
  int a[4] = {0};

  scanf("%d", &s);
  a[0] = s % 10;
  a[1] = (s % 100  - a[0]) / 10;
  a[2] = (s % 1000 - a[1] * 10 - a[0]) / 100;
  a[3] = (s - a[2] * 100 - a[1] * 10 - a[0]) / 1000;

  for (size_t i = 0; i < 3; i++) {
    if (a[i] == a[i+1]) {
      printf("Bad\n");
      return 0;
    }
  }

  printf("Good\n");
  return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstdlib>
#include <stack>
#include <vector>
#define INF 0x3f3f3f3f
typedef long long ll;

using namespace std;

int main()
{
    int a[5];
    for(int i = 0; i < 3; i++)
    cin >> a[i];
    sort(a, a+3);
    if(a[0] == a[1])cout << a[2] << endl;
    if(a[1] == a[2])cout << a[0] << endl;
    return 0;
}


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

using namespace std;

int main(){
	int n;
	int x,y,b,p;
	int res1,res2;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> x >> y >> b >> p;
		res1 = x*b + y*p;
		if(b >= 5 && p >= 2){
			res1 *= 0.8;
		}
		if(b<5) b=5;
		if(p<2) p=2;
		res2 = (x*b + y*p)*0.8;
		cout << min(res1,res2) << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
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; }
using namespace std;
typedef long long ll;

// AtCoder Beginner Contest 139
int main(){

    string s, t;
    cin >> s >> t;

    int ans = 0;
    rep(i, 3)if(s[i] == t[i])ans++;

    cout << ans << endl;
    return 0;

}

Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn for_black(ans: &mut Vec<Vec<String>>, n: usize) {
    let mut cnt = 0;
    for y in 0..20 {
        for _x in 0..25 {
            if cnt == n {
                ans[y * 2 + 0].push("..".to_string());
                ans[y * 2 + 1].push("..".to_string());
            } else {
                ans[y * 2 + 0].push("..".to_string());
                ans[y * 2 + 1].push("#.".to_string());
                cnt += 1;
            }
        }
    }
}

fn for_white(ans: &mut Vec<Vec<String>>, n: usize) {
    let mut cnt = 0;
    for y in 0..20 {
        for _x in 0..25 {
            if cnt == n {
                ans[y * 2 + 0].push("##".to_string());
                ans[y * 2 + 1].push("##".to_string());
            } else {
                ans[y * 2 + 0].push("#.".to_string());
                ans[y * 2 + 1].push("##".to_string());
                cnt += 1;
            }
        }
    }
}

fn main() {
    let a: usize = read();
    let b: usize = read();
    let mut ans = vec![vec![]; 40];
    match (a, b) {
        (1, 1) => { ans = vec![vec![".#".to_string()]]; }
        (1, _) => { for_black(&mut ans, b); }
        (_, 1) => { for_white(&mut ans, a); }
        (_, _) => {
            for_black(&mut ans, b - 1);
            for_white(&mut ans, a - 1);
        }
    }
    println!("{} {}", ans.len(), ans[0].join("").len());
    for line in ans {
        println!("{}", line.join(""));
    }
}

Measured memory_bytes: 4352
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int S;
    cin>>S;
    cout<<S/(60*60);
    cout<<":";
    S=S%(60*60);
    cout<<S/60;
    cout<<":";
    S=S%60;
    cout<<S<<endl;
}


Measured memory_bytes: 3108
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int n,F,x[101],y[101],h[101];
inline bool check(int X,int Y,int H)
{
	for(register int i=1;i<=n;i++)
		if((((H-abs(X-x[i])-abs(Y-y[i]))<0)?0:H-abs(X-x[i])-abs(Y-y[i]))!=h[i])
			return false;
	return true;
}
int main() 
{
	scanf("%d",&n);
	for(register int i=1; i<=n; i++) {
		scanf("%d%d%d",&x[i],&y[i],&h[i]);
		if(h[i]!=0)F=i;
	}
	for(int i=0;i<=100;i++) 
	{
		for(int j=0;j<=100;j++) 
		{
			if(check(i,j,abs(i-x[F])+abs(j-y[F])+h[F]))
			{
				cout<<i<<" "<<j<<" "<<(int)abs(i-x[F])+abs(j-y[F])+h[F]<<endl;
				return 0;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3648
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

const int MAX_N = 5e2 + 10, MOD = 1e9 + 7;
int n, m;
string table[MAX_N];
bool map[2][MAX_N][MAX_N];

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> table[i];

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

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (table[i][j] == '#')
				map[1][i][j] = map[0][i][j] = true;

	for (int j = 0; j < m; j++)
		for (int i = 1; i + 1 < n; i++)
			if (table[i][j] == '.')
				map[j & 1][i][j] = true;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++)
			cout << (map[0][i][j]? '#': '.');

		cout << "\n";
	}
	cout << "\n";

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++)
			cout << (map[1][i][j]? '#': '.');

		cout << "\n";
	}
	return 0;
}

Measured memory_bytes: 1664
sx, sy, tx, ty = gets.chomp.split.map(&:to_i)

xlen = tx - sx
ylen = ty - sy

su_to_tl = 'U' * ylen + 'R' * xlen
td_to_sr = 'D' * ylen + 'L' * xlen
sl_to_tu = 'L' + 'U' * (ylen + 1) + 'R' * (xlen + 1) + 'D'
tr_to_sd = 'R' + 'D' * (ylen + 1) + 'L' * (xlen + 1) + 'U'

puts su_to_tl + td_to_sr + sl_to_tu + tr_to_sd

Measured memory_bytes: 1916
import sys
if __name__ == '__main__':
    N,x = list(map(int,input().split()))
    if 1 < x < 2*N - 1:
        print('Yes')
        for i in range(2*N - 1):
            print(1+ ((x-N+i)%(2*N - 1)))
    else:
        print('No')


Measured memory_bytes: 4560
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
#include<cmath>
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define inf 0x7fffffff
#define llinf 0x7fffffffffffffffll
using namespace std;
int a[1010][1010],b[1010];
int main()
{
	int i,j,k,n,m,l;
	scanf("%d",&n);
	m=1.0*(1.0+sqrt(1+8*n))/2+1e-7;
	if(m*(m-1)!=2*n)
	{
		printf("No");
		return 0;
	}
	printf("Yes\n");
	printf("%d\n",m);
	l=1;
	bool bo;
	k=0;
	for(i=0;i<m;i++)
	{
		bo=1;
		while(b[k]>=m-1)
		{
			k++;
		}
		for(j=0;j<m-1;j++)
		{
			if(k+j<i)
			{
				a[i][j]=a[k+j][b[k+j]];
				b[k+j]++;
			}
			else
			{
				if(bo)
				{
					b[i]=j;
					bo=0;
				}
				a[i][j]=l;
				l++;
			}
		}
	}
	for(i=0;i<m;i++)
	{
		printf("%d ",m-1);
		for(j=0;j<m-1;j++)
		{
			printf("%d ",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

Measured memory_bytes: 3200
/* Author : Aaryan Srivastava ^__^ */ 
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#define pb push_back
#define mp make_pair
#define ff first
#define ss second   
#define rep(i,n) for(int i = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define setval(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define trav(x , a) for(auto &x : a)
#define sz(a) ((int)a.size())
typedef long long ll ; 
#define int ll
using namespace std;

const int N = 3e5 + 5 ;
const int mod = 1e9 + 7 ;
const ll inf = 1e18 ;
const int SZ = 101 ;
const long double eps = 1e-9 ;

using namespace __gnu_pbds;
using ordered_set =  tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ;

typedef pair<int, int> pii; 
typedef pair<int , pii> ipii ;
typedef pair<pii , int> piii ; 
typedef unsigned long long ull ;
typedef long double ld;

ll po(ll x,ll y,ll p = mod) {ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}

const int M = (1 << 9) + 1 ; 
const int L = 1e5 + 1 ;

int dp[M][L];

void solve()
{

    int n ;
    cin >> n ;
    int V[n + 1] , W[n + 1];
    rep(i , n) cin >> V[i + 1] >> W[i + 1];

    for(int i = 1 ; i <= min(M - 2 , n) ; ++i){
        for(int l = 0 ; l < L ; ++l){
            dp[i][l] = max(dp[i][l] , dp[i/2][l]);
            if(l >= W[i]) dp[i][l] = max(dp[i][l] , dp[i/2][l - W[i]] + V[i]);
        }
    }

    int q;
    cin >> q;
    while(q--){
        int v, l;
        cin >> v >> l ;
        if(v <= (M - 2)){
            cout << dp[v][l] << '\n';
        } else {
            int u = v , ans , k;
            vector<pii> s;
            while(u > (M - 2)) s.pb({V[u] , W[u]}) , u /= 2 ;
            k = sz(s);
            ans = dp[u][l] ;
            rep(msk , 1 << k){
                int tw = 0 , tv = 0;
                rep(i , k){
                    if(msk >> i & 1){
                        tw += s[i].ss;
                        tv += s[i].ff;
                    }
                }
                if(tw <= l){
                    ans = max(ans , dp[u][l - tw] + tv);
                }
            }
            cout << ans << '\n';
        }
    }
}
     
int32_t main(int32_t argc, char *argv[])
{
    ios::sync_with_stdio(0);    
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case #" << t << ": " ;
        solve();
        cout << "\n" ;
    }
    return 0;
}


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

int main(){
  int n,a=0,b=0,c=0,d=0;
  cin>>n;
  for(int i=0;i<n;i++){
    string s;
    cin>>s;
    if(s=="AC"){
      a++;
    }
    else if(s=="WA"){
      b++;
    }
    else if(s=="TLE"){
      c++;
    }
    else if(s=="RE"){
      d++;
    }
  }
  cout<<"AC x "<<a<<endl;
  cout<<"WA x "<<b<<endl;
  cout<<"TLE x "<<c<<endl;
  cout<<"RE x "<<d<<endl;
}
  

Measured memory_bytes: 3628
from collections import deque

h, w = [int(i) - 1 for i in input().split()]
ans = 0
maze = []
for i in range(h + 1):
    s = list(input())
    maze.append(s)
    ans += s.count(".")

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

while pos:
    y, x, b = pos.popleft()
    for ny, nx in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
        if y + ny == h and x + nx == w:
            print(ans - b - 2)
            exit()
        if 0 <= y + ny <= h and 0 <= x + nx <= w and maze[y + ny][x + nx] == ".":
            pos.append([y + ny, x + nx, b + 1])
            maze[y + ny][x + nx] = "#"
print(-1)

Measured memory_bytes: 73976
"""bfs"""
import sys
sys.setrecursionlimit(10**6)
n,m = map(int, input().split())

INF = 1001001001

to = [[] for _ in range(n)]
dist = [INF]*n
dist[0] = 0
ans = [-1]*n
ans[0] = [0]

for i in range(m):
    a,b = map(int, input().split())
    a -= 1; b -= 1
    to[a].append(b)
    to[b].append(a)

import queue

q = queue.Queue()
searched = [0]*n
q.put(0)
#searched[0] = 1 # qに突っ込んだ時点でその頂点のsearchedを1にする

while not q.empty():
    v = q.get()
    for i in range(len(to[v])):
        u = to[v][i]
        if dist[u] != INF: continue
#        if dist[v]+1 < dist[u]:
        dist[u] = dist[v]+1
        ans[u] = v
        q.put(u)
#        searched[u] = 1 # qに突っ込んだ時点でその頂点のsearchedを1にする

if -1 not in ans:
    print('Yes')
    for i in ans[1:]: print(i+1)
else:
    print('No')

Measured memory_bytes: 102252
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define int long long
#define mod 1000000007
#define F first
#define S second
#define P pair<long long,long long>
#define all(a) a.begin(),a.end()
using namespace std;
signed main(){
    string s; cin>>s;
    int a=0, b=0;
    rep(i,s.size()){
        if(s.at(i)=='0') a++;
        else b++;
    }
    cout<<min(a,b)*2<<endl;
}


Measured memory_bytes: 512
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let a = read::<i32>();
    let b = read::<i32>();
    println!("{}", [a + b, a - b, a * b].iter().max().unwrap());
}


Measured memory_bytes: 4352
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    String s[] = new String[N];
    for(int i=0;i<N;i++){
      s[i]=sc.next();
    }
    int M = sc.nextInt();
    String t[] = new String[M];
    for(int i=0;i<M;i++){
      t[i]=sc.next();
    }
    int x[] = new int[N];
    for(int i=0;i<N;i++){
      for(int j=0;j<N;j++){
        if(s[i].equals(s[j])){
          x[i]++;
        }
      }

      for(int j=0;j<M;j++){
        if(s[i].equals(t[j])){
          x[i]--;
        }
      }
    }
    Arrays.sort(x);
    System.out.println(Math.max(0,x[N-1]));

  }
  

  
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;

int main() {
	int A, B;
	string S;
	cin >> A >> B >> S;

	bool f = true;
	int len = A + B + 1;

	if (S.size() != len) {
		f = false;
	}
	else {
		for (int i = 0; i < S.size(); i++) {
			if (i == A) {
				if (S.at(i) != '-') {
					f = false;
					break;
				}
			}
			else {
				if (S.at(i) == '-') {
					f = false;
					break;
				}
			}
		}
	}

	cout << (f ? "Yes" : "No") << endl;
}

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

int main() {
	int R;
	cin >> R;
	if (R < 1200) {
		cout << "ABC" << endl;
	}
	else {
		if (R >= 1200 && R < 2800) {
			cout << "ARC" << endl;
		}
		else {
			if (R >= 2800) {
				cout << "AGC" << endl;
			}
		}
	}
}

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


using namespace std;

string str;


int main() {
    //fstream cin("A.in");
    //ofstream cout("A.out");
    int i;
    ios::sync_with_stdio(false);
    cin >> str;
    if(str.size() < 4) {
        cout << "No";
        return 0;
    }
    if(str[0] == 'Y' && str[1] == 'A' && str[2] == 'K' && str[3] == 'I') {
        cout << "Yes";
        return 0;
    }
    cout << "No";
    //cin.close();
    //cout.close();
    return 0;
}


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

int n,x;
int m[105];
int minn = 10005;
int cnt = 0;

int main(){
    scanf("%d %d",&n, &x);
    for(int i = 0; i < n; i++){
        scanf("%d",&m[i]);
        x -= m[i];
        cnt++;
        if(m[i]  < minn){
            minn = m[i];
        }
    }
    printf("%d\n",cnt + x / minn);
return 0;
}


Measured memory_bytes: 256
P, Q, R = [int(i) for i in input().split()]
# P, Q, R = map(int, input().split())
# print(min(P+Q, Q+R, R+P))
min_time = 200
for num in [P+Q, Q+R, R+P]:
    if num < min_time:
        min_time = num
print(min_time)

Measured memory_bytes: 2940
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author verytable
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <vector>

class CCitySavers {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n;
        in >> n;

        std::vector<long long> as;
        as.reserve(n + 1);
        for (int i = 0; i < n + 1; ++i) {
            long long a;
            in >> a;
            as.push_back(a);
        }

        std::vector<long long> bs;
        as.reserve(n);
        for (int i = 0; i < n; ++i) {
            long long b;
            in >> b;
            bs.push_back(b);
        }

        long long s = 0;
        long long prev_b = 0;
        for (int i = 0; i < n + 1; ++i) {
            if (as[i] < prev_b) {
                s += as[i];
                if (i < n) {
                    prev_b = bs[i];
                }
            } else {
                s += prev_b;
                as[i] -= prev_b;
                prev_b = 0;

                if (i < n) {
                    if (bs[i] < as[i]) {
                        s += bs[i];
                    } else {
                        s += as[i];
                        prev_b = bs[i] - as[i];
                    }
                }
            }
        }

        out << s << "\n";
    }
};


int main() {
    CCitySavers solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 2168
from itertools import*
n=int(input())
a=[[s for s in input().split()]for _ in range(n)]
for b in a:
 b[3:5]=b[4],b[3]
t=0
for d,e in combinations(a,2):
 for p in('012345','152043','215304','302541','410352','514320'):
  f=[d[int(i)]for i in p]
  if f[0]==e[0]and f[5]==e[5]:
   f=f[1:5]*2
   for k in range(4):
    if f[k:k+4]==e[1:5]:t=1
print(['Yes','No'][t])


Measured memory_bytes: 5660
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,a,b) for(int i =(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define REPm(i,n) for(int i=(n)-1;i>=0;i--)
#define REP1(i,n) for(int i=1;i<=(n);i++)
typedef long long ll;

int main(){
    int N;
    cin >> N;
    if(N/100 == N%10) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define M 7
using namespace std;
int m,a[M],b[M],g,n,ans;

void dfs(int x,int sum){
  if(x==m){
    if(sum==n)ans++;
    return;
  }
  for(int i=0;i<=b[x];i++)
    dfs(x+1,sum+a[x]*i);
}

int main(){
  while(1){
    cin>>m;
    if(!m)break;
    for(int i=0;i<m;i++)
      cin>>a[i]>>b[i];
    cin>>g;
    while(g--){
      cin>>n;
      ans=0;
      dfs(0,0);
      cout<<ans<<endl;
    }
  }
  return 0;
}

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

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

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

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

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

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

    int N;
    cin >> N;
    int d = N % 10;

    if (d == 2 || d == 4 || d == 5 || d == 7 || d == 9){
        print("hon");
    } else if (d == 0 || d == 1 || d == 6 || d == 8){
        print("pon");
    } else {
        print("bon");
    }

    return 0;
}


Measured memory_bytes: 3636
k = 40 # 多倍長整数内で、一整数を何bitでもつか。
MOD = 200003
list_size = 2 * MOD - 1


def n2l(num, list_size = list_size):
    "number to list"
    s = '{0:0{1}b}'.format(num, k * list_size)
    return [int(s[i-k:i], 2) for i in range(k * list_size, 0, -k)]

def l2n(ls):
    "list to number"
    return int(''.join(('{0:0{1}b}'.format(n, k) for n in reversed(ls))), 2)


def rn2l(num, list_size = list_size):
    "reversed n2l"
    s = '{0:0{1}b}'.format(num, k * list_size)
    return [int(s[i:i+k], 2) for i in range(0, k * list_size, k)]

def rl2n(ls):
    "reversed l2n"
    return int(''.join(('{0:0{1}b}'.format(n, k) for n in ls)), 2)


N = int(input())
As = sorted(map(int, input().split()))

two_pow = [1] * MOD
for i in range(1, MOD):
    two_pow[i] = two_pow[i-1] * 2 % MOD

two_pow_inv = [0] * MOD
for i, e in enumerate(two_pow[:-1]):
    two_pow_inv[e] = i

cnt = [0] * MOD # cnt[i] counts 2 ** i % P
for A in As:
    if A != 0:
        cnt[two_pow_inv[A]] += 1

cnt_conv = n2l(l2n(cnt) ** 2)
ans = 0
for i, cnt in enumerate(cnt_conv):
    if i < MOD:
        ans += two_pow[i] * cnt
    else:
        ans += two_pow[i - MOD + 1] * cnt

ans -= sum((A**2 % MOD for A in As))
ans //= 2
print(ans)

Measured memory_bytes: 263304
//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG 
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s); 
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s); 
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

string s;
int k;
vector<string> risan;

int main()
{
  s = GetString();
  REP(i,SIZE(s))
  {
    string t = "";
    FOR(j,i,min(SIZE(s),i+10))
    {
      t += s[j];
      risan.pb(t);
    }
  }
  scanf("%d",&k);
  SORT(risan);
  risan.erase(unique(ALL(risan)));
  // REP(i,SIZE(risan)) VALUE(risan[i]);
  printf("%s\n",risan[k-1].c_str());
  return 0;
}











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

int main(int argc, char* argv[])
{
	int N,X,Y,i,j,k;
	cin>>N>>X>>Y;
	vector<vector<int>> D(N,vector<int>(N));
	int INF=100000;
	for (i=0;i<N;i++){
		for (j=0;j<N;j++){
			if (i==j){
				D[i][j]=0;
			}else{
				D[i][j]=INF;
			}
		}
	}
	for (i=0;i<N-1;i++){
		D[i][i+1]=1;
		D[i+1][i]=1;
	}
	for (i = 0; i < N; i++){      
		for (j = 0; j < N; j++){  
			if (i==j){
				D[i][j]=0;
			}else{
				if (j>i){
					D[i][j]=j-i;
				}else{
					D[i][j]=i-j;
				}
			}
		}
	}
	D[X-1][Y-1]=1;
	D[Y-1][X-1]=1;
	for (j = 0; j < N; j++){    // 開始頂点
		for (k = 0; k < N; k++){  // 終端
			D[j][k] = min(D[j][k], D[j][X-1] + D[X-1][k]);
		}
	}
	for (j = 0; j < N; j++){    // 開始頂点
		for (k = 0; k < N; k++){  // 終端
			D[j][k] = min(D[j][k], D[j][Y-1] + D[Y-1][k]);
		}
	}
	/*
	for (i = 0; i < N; i++){      // 経由する頂点
		for (j = 0; j < N; j++){    // 開始頂点
			for (k = 0; k < N; k++){  // 終端
				D[j][k] = min(D[j][k], D[j][i] + D[i][k]);
			}
		}
	}*/
	vector<int> Num(N+1,0);
	for (i = 0; i < N; i++){      // 経由する頂点
		for (j = 0; j < N; j++){    // 開始頂点
			Num[D[i][j]]++;
		}
	}
	for (k=1;k<N;k++){
		cout<<Num[k]/2<<endl;
	}
	return 0;
}

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

using namespace std;

#define loop(i, n) for(int i = 0;i < int(n);i++)
#define rloop(i, n) for(int i = int(n);i >= 0;i--)
#define range(i, a, b) for(int i = int(a);i <= int(b);i++)
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1, v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1, v2, v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1, v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1, v2, v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define endl '\n'

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


int main() {

#ifndef ONLINE_JUDGE
//    freopen("in.in", "r", stdin);
    //freopen("out.in", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    vi v(n);
    for (int &x : v)cin >> x;
    multiset<int> ms;
    rloop(i, n - 1) {
        auto it = ms.upper_bound(v[i]);
        if (it == ms.end()) {
            ms.insert(v[i]);
        } else {
            ms.erase(it);
            ms.insert(v[i]);
        }
    }

    cout << SZ(ms) << endl;

    return 0;
}


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

using namespace std;

#define MAX 51

struct Point{
  int x,y;
  double dist(const Point &p){
    return abs(x-p.x)+abs(y-p.y);
  }
};

int N,m[MAX];
vector<Point> lines[MAX];

int getDir(Point &p1,Point &p2){
  if(p1.x == p2.x){
    return (p1.y < p2.y ? 0 : 2);
  }else{
    return (p1.x < p2.x ? 3 : 1);
  }
}

bool equals(vector<Point> &l1,vector<Point> &l2){
  int s1 = l1.size(), s2 = l2.size();
  if(s1 != s2) return false;
  double k;
  bool flg = false;
  int dir1,dir2,diff;
  for(int i = 0 ; i < s1-1 ; i++){
    double d1 = l1[i].dist(l1[i+1]);
    double d2 = l2[i].dist(l2[i+1]);
    dir1 = getDir(l1[i],l1[i+1]);
    dir2 = getDir(l2[i],l2[i+1]);
    if(i > 0){
      if(k != d1/d2 || diff != (dir1-dir2+4)%4){
        flg = true;
        break;
      }
    }else{
      diff = (dir1-dir2+4)%4;
      k = d1/d2;
    }
  }
  if(!flg) return true;
  flg = false;
  for(int i = s1-1 ; i > 0 ; i--){
    double d1 = l1[s1-i-1].dist(l1[s1-i]);
    double d2 = l2[i].dist(l2[i-1]);
    dir1 = getDir(l1[s1-i-1],l1[s1-i]);
    dir2 = getDir(l2[i],l2[i-1]);
    if(i != s1-1){
      if(k != d1/d2 || diff != (dir1-dir2+4)%4){
        flg = true;
        break;
      }
    }else{
      diff = (dir1-dir2+4)%4;
      k = d1/d2;
    }
  }
  return !flg;
}

int main(){
  while(cin >> N,N){
    for(int i = 0 ; i <= N ; i++){
      cin >> m[i]; lines[i].clear();
      for(int j = 0 ; j < m[i] ; j++){
        int x,y;
        cin >> x >> y;
        lines[i].push_back((Point){x,y});
      }
    }
    for(int i = 1 ; i <= N ; i++){
      if(equals(lines[0],lines[i])){
        cout << i << endl;
      }
    }
    cout << "+++++" << endl;
  }
  return 0;
}

Measured memory_bytes: 1224
io = STDIN
n=io.gets.to_i
s=io.gets.chomp.split(//)

left=Array.new(n,0)
right=Array.new(n,0)
(1...n).each do |i|
  left[i]=left[i-1] + (s[i-1]=="W" ? 1 : 0)
end
(n-1).downto(1) do |i|
  right[i-1]=right[i] + (s[i]=="E" ? 1 : 0)
end
ans=n
n.times do |i|
  ans=[ans,left[i]+right[i]].min
end
puts ans


Measured memory_bytes: 32144
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			if(sc.next().equals("Y")) {
				System.out.println("Four");
				System.exit(0);;
			}
		}
		System.out.println("Three");
		sc.close();
	}
}


Measured memory_bytes: 21716
#include <math.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
struct UnionFind//mainより前に置く
{
  vector<int> parent;
  vector<ll> dist;
  
  UnionFind(int x = 1)
  {
    parent.clear();
    parent.resize(x,-1);
    dist.clear();
    dist.resize(x,0);
  }
  
  int find(int x)
  {
    if(parent[x] < 0) return x;//親が確定
    
    int r = find(parent[x]);
    dist[x] += dist[parent[x]];
    return parent[x] = r; 
  }
  
  ll depth(int x)
  {
    find(x);
    return dist[x];
  }
  
  bool same(int x,int y)
  {
    return (find(x) == find(y));
  }
  
  int size(int x)
  {
    return -parent[find(x)];
  }
  
  void unite(int x, int y, ll k = 0)
  {
    k += depth(y);
    k -= depth(x);
    k *= -1;
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(size(x) < size(y))
    {
      swap(x,y);
      k *= -1;
    }
    parent[x] += parent[y];
    parent[y] = x;
    dist[y] = k;
  }
};
 
int main()
{
  int n,q;
  scanf("%d %d",&n,&q);//文字の取得
  UnionFind T(n);
  
  for(int i = 0; i < q; i++)
  {
    int type,x,y;
    scanf("%d %d %d",&type,&x,&y);
    if(type == 0)
    {
      int a;
      scanf("%d",&a);
      T.unite(x,y,a);//距離の追加
    }else
    {
      if(!T.same(x,y))
      {
        printf("?\n");//距離が不明
      }else
      {
        printf("%lld\n",T.depth(x) - T.depth(y));//距離の取得
      }
    }
  }
  
  return 0;
}


Measured memory_bytes: 3852
import java.lang.Math.max

fun main(args: Array<String>){
    val n = readLine()!!.toInt()
    val happiness = Array(n){Array(3){0}}
    for (i in 0 until n){
        val tmp = readLine()!!.split(" ").map { it.toInt() }
        happiness[i][0] = tmp[0]
        happiness[i][1] = tmp[1]
        happiness[i][2] = tmp[2]
    }

    val dp = Array(n){Array(3){0}}
    dp[0][0] = happiness[0][0]
    dp[0][1] = happiness[0][1]
    dp[0][2] = happiness[0][2]

    for(i in 1 until n){
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + happiness[i][0]
        dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + happiness[i][1]
        dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + happiness[i][2]
    }

    println(dp.last().max())
}

Measured memory_bytes: 95092
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define dbg(x) { cerr<<"> "<<#x<<": "<<x<< endl; }
#define dbg2(x,y) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<endl; }
#define dbg3(x,y,z) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<" , "<<#z<<": "<<z<<endl; }
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int inf = INT_MAX;
const double eps = 0.0000001;
const double PI = acos(-1.0);
const int MOD = 1e9+7;
const int N  = 1e5+5;



signed main(){
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #endif  
    IOS;

    int n;
    cin >> n;
    cout << 3*n*n << endl;
    return 0;
}

Measured memory_bytes: 256
gets
x = gets.chomp.split(' ').map(&:to_i).sort
y = gets.chomp.split(' ').map(&:to_i).sort

sumx = 0
n = x.length
(1..n).each do |k|
  sumx += (n - k) * x[k - 1] - (k - 1) * x[k - 1]
end

sumy = 0
n = y.length
(1..n).each do |k|
  sumy += (n - k) * y[k - 1] - (k - 1) * y[k - 1]
end

puts (sumx * sumy) % 1_000_000_007


Measured memory_bytes: 21096
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
ll n,N = 200020,bit[200010];

ll sum(int i){
	int s = 0;
	while(i>=0){
		s += bit[i];
		i = (i&(i+1)) -1;
	}
	return s;
}

void add(int i, int x){
	while(i<=N){
		bit[i] += x;
		i |= i+1;
	}
}

ll a[3][100010],b[2] = {};
ll rev(int x){
	int i;
	ll m;
	if(x&1) m = n/2;
	else m = (n + 1)/2;
	ll ret = m*(m - 1)/2;
	for(i=0;i<n;i++) bit[i] = 0;
	for(i=x;i<n;i+=2){
		ret -= sum(a[1][i]/3);
		add(a[1][i]/3,1);
	}
	return ret;
}

int main(){
	int i,j;
	cin >> n;
	for(j=0;j<3;j++){
		for(i=0;i<n;i++){
			cin >> a[j][i];
		}
	}
	for(i=0;i<n;i++){
		if(a[1][i]%3!=2 || (i + a[1][i])&1){
			cout << "No" << endl;
			return 0;
		}
	}
	for(i=0;i<n;i++){
		if(abs(a[0][i] - a[1][i])!=1 || abs(a[1][i] - a[2][i])!=1){
			cout << "No" << endl;
			return 0;
		}
		if(a[0][i]>a[2][i]){
			b[i&1]++;
		}
	}
	b[1] += rev(0); b[0] += rev(1);
	if(b[0]%2==0 && b[1]%2==0){
		cout << "Yes" << endl;
	}else{
		cout << "No" << endl;
	}
}

Measured memory_bytes: 3456
#include <stdio.h>

int main() {
  int n;
  int i;
  scanf("%d", &n);
  long long int arr[n], max = 1e18, prod = 1, flag = 0, zero = 0;
  
  for ( i = 0; i < n; i++) {
    scanf("%lld", &arr[i]);
    flag = (arr[i] > max) ? 1 : flag;
    prod *= arr[i];
    (arr[i]) ? max /= arr[i] : zero = 1;
  }

  (zero) ? puts("0") : ((flag) ? puts("-1") : printf("%lld\n", prod));
  return 0;
}

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

#define mp make_pair
#define pb push_back
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0)
#define FILE_IO(x) freopen((string(x) + ".in").c_str(), "r", stdin); freopen((string(x) + ".out").c_str(), "w", stdout)
#define sz(x) ((int)((x).size()))
#define len(x) ((int)((x).length()))
#define x first
#define y second
#define foreach(it, v) for (auto it : v)
#define rep(it, n) for (int it = 0; it < n; ++it)
#define forin(it, l, r) for (int it = l; it < r; ++it)
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = (1 << 31) - 1;
const ll LINF = (1ll << 63) - 1;
const double DINF = numeric_limits<double>::infinity();
const int MOD = 1e9 + 7;
const double EPS = 1e-7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
mt19937 mmtw(MOD);
ll rnd(ll x, ll y) { static uniform_int_distribution<ll> d; return d(mmtw) % (y - x + 1) + x; }
template <class T> T arr_max(T a[], int n) { T res = a[0]; for (int i = 1; i < n; ++i) if (a[i] > res) res = a[i]; return res; }
template <class T> T arr_min(T a[], int n) { T res = a[0]; for (int i = 1; i < n; ++i) if (a[i] < res) res = a[i]; return res; }
////////////////////////////////////////////////////////////////////////////////////////////////

const int MAXN = 1e5 + 5;
int n;
int a[MAXN];
bool flag = false;
int res;


int main() {
    FAST_IO;
    cin >> n;
    forin(i, 1, n + 1) {
        cin >> a[i];
    }

    forin(i, 1, n + 1) {
        if (flag) {
            swap(a[i], a[i - 1]);
            res++;
            if (a[i] != i) {
                flag = false;
            }
        } else {
            if (a[i] == i) {
                flag = true;
            }
        }
    }
    cout << (res + flag) << endl;
    return 0;
}

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

int main(){
  string s;
  int ans = 0;

  cin >> s;

  for(int i = 0; i < (s.size() - 1) / 2; i++){
    if(s.at(i) != s.at(s.size() - i - 1)){
      ans++;
    }
  }

  for(int i = 0; i < (s.size() - 1) / 2; i++){
    if(s.at(i) != s.at((s.size() - 1) / 2 - i - 1)){
      ans++;
    }
  }

  for(int i = 0; i < (s.size() - 1) / 2; i++){
    if(s.at((s.size() + 3) / 2 + i - 1) != s.at(s.size() - i - 1)){
      ans++;
    }
  }

  if(ans > 0){
    cout << "No" << endl;
  }else{
    cout << "Yes" << endl;
  }
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define INF (1e12)
#define sz(v) ((int)(v).size())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

int fnd(vector<ll> &v,ll val)
{
	int lo =-1,hi=v.size();
	while(hi-lo>1)
	{
		int m=(hi+lo)/2;
		if(v[m]>val) hi=m;
		else lo=m;
	}
	return lo;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	ll a,b,q; cin>>a>>b>>q;
	vector<ll> s(a),t(b);
	For(i,0,a-1) cin>>s[i];
	For(i,0,b-1) cin>>t[i];
	sort(s.begin(),s.end());
	sort(t.begin(),t.end());
	ll in;
	while(q--)
	{
		cin>>in;
		ll id=fnd(s,in);// cout<<id<<"\n";
		ll ds1=(id>=0?in-s[id]:INF),ds2=(id<sz(s)-1?s[id+1]-in:INF);
		id=fnd(t,in);// cout<<id<<"\n";
		ll dt1=(id>=0?in-t[id]:INF),dt2=(id<sz(t)-1?t[id+1]-in:INF);
		//cout<<ds1<<" "<<ds2<<"  "<<dt1<<" "<<dt2<<"\n";
		ll ans=min(min(max(ds1,dt1),max(ds2,dt2)),min(ds2+dt1+min(ds2,dt1),ds1+dt2+min(ds1,dt2)));
		cout<<ans<<"\n";
	}
	return 0;
}











Measured memory_bytes: 2944
N=int(input())

p = list(map(int,input().split()))
q = [0 for i in range(N)]
for i in range(N):
    q[p[i]-1]=i
    
A = 0
B = 10**9+1
ans_a = []
ans_b = []
for i in range(N):
    A+=q[i]
    if i!=0:
        B-=q[i-1]
    A+=1
    B-=1
    ans_a.append(A)
    ans_b.append(B)
    
print(*ans_a)
print(*ans_b)

Measured memory_bytes: 7264