akhauriyash/Code-Regression huggingface dataset:
import itertools


def main():
    N = int(input())
    A = [list(map(int, input().split(' '))) for _ in range(N)]
    score_list = [0 for _ in range(1 << N)]
    for S in range(1, 1 << N):
        group = [r for r in range(N) if (S >> r) & 1 == 1]
        score_list[S] = sum([A[i][j] for i, j in itertools.combinations(group, 2)])
    dp = [0 for _ in range(1 << N)]
    for S in range(1, 1 << N):
        score = score_list[S]
        T = (S - 1) & S
        while T > 0:
            x = dp[S ^ T] + dp[T]
            score = max(score, x)
            T = (T - 1) & S
        dp[S] = score
    print(dp[(1 << N) - 1])


if __name__ == '__main__':
    main()


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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
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;
}

int main() {
  ll N; cin >> N;
  vector<ll> A(N), B(N); REP(i, N) cin >> A[i] >> B[i];
  ll tmp = 0;
  for(ll i = N - 1; i >= 0; --i) {
    A[i] += tmp;
    if(A[i] % B[i] == 0) {
      continue;
    } else {
      ll res = ((A[i]/B[i]) + 1) * B[i] - A[i];
      tmp += res;
    }
  }
  cout << tmp << endl;
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define SIZE 13
int N,M,X;
int POW[SIZE],a[SIZE];
int b[SIZE][SIZE],c[SIZE];
int main(){
	POW[0] = 1;
	for(int i = 1; i <=SIZE; i++){
		POW[i] = POW[i-1]<<1;
	}
	scanf("%d %d %d",&N,&M,&X);
	for(int i = 0; i < N; i++){
		scanf("%d",&a[i]);
		for(int j = 0; j < M; j++){
			scanf("%d",&b[i][j]);
		}
	}	
	int ans = BIG_NUM;
	bool FLG;
	for(int i = 0; i < POW[N]; i++){
		memset(c,0,sizeof(c)); 
		int sum = 0;
		for(int j = 0; j < N; j++){
			if(i & POW[j]){
				sum += a[j];
				for(int k = 0; k < M; k++){
					c[k] += b[j][k];
				}
			}
		} 
		FLG = true;
		for(int j = 0; j < M; j++){
			if(c[j] < X){
				FLG = false;
				break;
			}
		}
		if(FLG) ans = min(ans,sum);
	}
	if(ans == BIG_NUM) printf("-1\n");
	else printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 3776
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=4005;
const ll MOD=998244353;
bitset<MAX> a[6],x,fin;
int n;
ll fin2,p2[MAX];
bitset<MAX> xgcd(bitset<MAX> &p,bitset<MAX> &q){
    bitset<MAX> val=p;
    int pos1=-1,pos2=-1;
    for(int i=MAX-1;i>=0;i--) {
        if(p[i]) pos1=max(pos1,i);
        if(q[i]) pos2=max(pos2,i);
    }
    if(pos2==-1) return val;
    for(int i=pos1;i>=pos2;i--){
        if(val[i]) val^=q<<(i-pos2);
    }
    return xgcd(q,val);
}
void cnt(bitset<MAX> &p,bitset<MAX> &q){
    bitset<MAX> val(0);
    int pos1=-1,pos2=-1;
    for(int i=MAX-1;i>=0;i--) {
        if(p[i]) pos1=max(pos1,i);
        if(q[i]) pos2=max(pos2,i);
    }
    for(int i=pos1;i>=pos2;i--){
        if(p[i]) fin2+=p2[i-pos2],fin2%=MOD;
        if(p[i]!=val[i]) val^=q<<(i-pos2);
    }
    for(int i=MAX-1;i>=0;i--){
        if(p[i]<val[i]){
            return;
        }
        else if(p[i]>val[i]){
            fin2++;return;
        }
    }
    fin2++;
    return;
}
int main(){
    string t;
    cin>>n>>t;
    bitset<MAX> tmp(t);
    x=tmp;
    for(int i=0;i<n;i++) {
        cin>>t;
        bitset<MAX> tmp(t);
        a[i]=tmp;
    }
    fin=a[0];
    for(int i=1;i<n;i++) fin=xgcd(fin,a[i]);
    p2[0]=1;
    for(int i=1;i<MAX;i++) p2[i]=p2[i-1]*2%MOD;
    cnt(x,fin);
    cout<<fin2<<endl;
}


Measured memory_bytes: 3584
#include<iostream>
 
using namespace std;
 
int main(){
  char S[6];
  cin >> S;
  if(S[2] == S[3] & S[4] == S[5]){
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  
  return 0;
}

Measured memory_bytes: 256
N = int(input())
S = str(input())
num = []
for _ in range(10):
    num.append([])

for i in range(N):
    num[int(S[i])].append(i)

ans = 0
for X1 in range(0, 10):
    for X2 in range(0, 10):
        for X3 in range(0, 10):
            X = str(X1) + str(X2) + str(X3)
            if num[X1] != [] and num[X2] != [] and num[X3] != []:
                if num[X1][0] < num[X2][-1]:
                    for i in num[X2]:
                        if num[X1][0] < i < num[X3][-1]:
                            ans += 1
                            break

print(ans)


Measured memory_bytes: 10240
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class Main {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);
	static boolean debug = false;

	static String[] ss = {"maerd", "remaerd", "esare", "resare"};

	static void solve() {
		String s = in.next();
		int n = s.length();
		StringBuilder sb = new StringBuilder(s);
		sb.reverse();
		for (int i=0; i<n; ) {
			boolean f = false;
			for (int j=0; j<4; j++) {
				if (i+ss[j].length() <= n && sb.substring(i, i+ss[j].length()).toString().equals(ss[j])) {
					f = true;
					i += ss[j].length();
					break;
				}
			}

			if (!f) {
				out.println("NO");
				return;
			}
		}

		out.println("YES");
	}

	public static void main(String[] args) {
		debug = args.length > 0;
		long start = System.nanoTime();

		solve();
		out.flush();

		long end = System.nanoTime();
		dump((end - start) / 1000000 + " ms");
		in.close();
		out.close();
	}

	static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); }
}

Measured memory_bytes: 16268
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::{Bytes, Chars};
#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
	input! {
		n: usize,
		mut l: [usize; n],
	}

	if n < 3 {
		println!("0");
		return;
	}

	l.sort();

	let mut ans = 0;
	for i in 0..n-2 {
		for j in i+1..n-1 {
			if l[i] == l[j] {
				continue;
			}
			for k in j+1..n {
				if l[j] == l[k] {
					continue;
				}
				//println!("{:?}", v);
				if l[i] + l[j] > l[k] {
					//println!("OK");
					ans += 1;
				}
			}
		}
	}
	println!("{}", ans);
}



Measured memory_bytes: 2192
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) { 
		Scanner input = new Scanner(System.in);
		int N = input.nextInt();
		double[][] dp = new double[N+1][N+1]; //[number of heads tossed from i coins][ith coin]
		double[] p = new double[N+1];
		double answer = 0.0;
		double bug = 1.0; //keep track of tails' product
		for (int i = 1; i <= N; i++) {
			p[i] = input.nextDouble();
		}
		dp[1][1] = p[1];
		bug=1.0-p[1];
		for (int i = 2; i <= N; i++) { //Filling diagonals of DP table
			dp[i][i] = dp[i-1][i-1]*p[i];
		}
		for (int j = 2; j <= N; j++) { //Filling 1st row of DP table
			dp[1][j] = dp[1][j-1]*(1.0-p[j])+p[j]*(bug);
			bug*=(1.0-p[j]);
		}
		for (int i = 2; i < N; i++) { //dp[N][N] is already filled, last row not necessary
			for (int j = i+1; j <= N; j++) {
				dp[i][j]=dp[i][j-1]*(1.0-p[j])+dp[i-1][j-1]*p[j]; 
			}
		}
		for (int i = N/2+1; i <= N; i++) {
			answer+=dp[i][N];
		}
		System.out.println(answer);
	}
}

Measured memory_bytes: 134220
#include <algorithm>  // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>     // bitset
#include <cctype>     // isupper, islower, isdigit, toupper, tolower
#include <cstdint>    // int64_t, int*_t
#include <cstdio>     // printf
#include <deque>      // deque
#include <iomanip>    // fixed, setprecision
#include <iostream>   // cout, endl, cin
#include <map>        // map
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <stack>      // stack
#include <string>     // string, to_string, stoi
#include <tuple>      // tuple, make_tuple
#include <unordered_map>  // unordered_map
#include <unordered_set>  // unordered_set
#include <utility>        // pair, make_pair
#include <vector>         // vector
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N, L;
  cin >> N >> L;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S.at(i);
  }
  sort(S.begin(), S.end());
  for (auto s : S) {
    cout << s;
  }
  cout << endl;
}


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

ave = 0
for i in range(N):
    ave += A[i]

ave = ave/N

ans = 0
m = 10**8
for i in range(N):
    tmp = A[N-1-i]
    res = abs(tmp-ave)
    if res<=m:
        m = res
        ans = N-1-i
print(ans)

Measured memory_bytes: 61608
N, M = map(int, input().split())
conunt = 0
maxL = 0
minR = 0
maxL, minR = map(int, input().split())
for _ in range(M-1):
    a, b = map(int, input().split())
    if maxL <= a:
      maxL = a
    if minR >= b:
      minR = b
if maxL <= minR:
  print(minR - maxL +1)
else:
  print('0')
    

Measured memory_bytes: 45352
#include<iostream>
using namespace std;
int main(){
  int n;
  scanf("%d",&n);
  int lap[n],dist[n],t[n+1],x[n+1],y[n+1];
  x[0]=y[0]=t[0]=0;
  for(int i=1;i<=n;i++){
    scanf("%d%d%d",&t[i],&x[i],&y[i]);
    lap[i-1]=t[i]-t[i-1];
    dist[i-1]=abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
    }
  for(int i=0;i<n;i++){
    if(dist[i]>lap[i]||(lap[i]-dist[i])%2==1){
      printf("No \n");
      return 0;
      }
    }
  printf("Yes \n");
  return 0;
  }

  
    


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

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

const int N = 300;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 31;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m;
    vector<pair<int, int> > cond[n + 1];
    rep(i, 0, m)
    {
        int l, r, x;
        cin >> l >> r >> x;
        if (r - l + 1 < x)
        {
            cout << 0 << endl;
            return 0;
        }
        cond[r].push_back({l, x});
    }
    int dp[n + 1][n + 1][n + 1];
    rep(i, 0, n + 1)
        rep(g, 0, n + 1)
            rep(b, 0, n + 1)
                dp[i][g][b] = 0;
    dp[0][0][0] = 1;
    rep(i, 0, n + 1)
    {
        rep(g, 0, i + 1)
            rep(b, 0, i + 1)
            {
                for (auto c : cond[i])
                {
                    int cnt = 1;
                    if (g >= c.fi)
                        cnt++;
                    if (b >= c.fi)
                        cnt++;
                    if (cnt != c.se)
                        dp[i][g][b] = 0;
                }
                if (i == n || dp[i][g][b] == 0)
                    continue;
                dp[i + 1][g][b] = (dp[i + 1][g][b] + dp[i][g][b]) % MOD;
                dp[i + 1][b][i] = (dp[i + 1][b][i] + dp[i][g][b]) % MOD;
                dp[i + 1][i][g] = (dp[i + 1][i][g] + dp[i][g][b]) % MOD;
            }
    }
    int ans = 0;
    rep(r, 0, n + 1)
        rep(b, 0, n + 1)
            ans = (ans + dp[n][r][b]) % MOD;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 106752
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;

int main() {
	string S, T;
	cin >> S >> T;
	bool ans = false;
	for (int i = 0; i < S.size(); ++i) {
		if (S == T) {
			ans = true;
			break;
		}
		S = S.back() + S.substr(0, S.size() - 1);
	}

	ans ? cout << "Yes" << endl : cout << "No" << endl;
	return 0;
}

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

int main()
{
    string s;
    cin >> s;
    if (
        (s[0] == s[1] && s[2] == s[3] && s[0] != s[3]) || (s[0] == s[2] && s[1] == s[3] && s[0] != s[1]) || (s[0] == s[3] && s[1] == s[2] && s[0] != s[1]))
    {
        cout << "Yes\n";
    }
    else
    {
        cout << "No\n";
    }
    return 0;
}


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

int d; //日数
const int z=26; //コンテスト数
vector<int> c(z); //コンテストiが開催されないペナルティ
vector<vector<int>> s; //i日目にコンテストjが開催された時の満足度

int score(vector<int> &t)
{
    int sum = 0;
    vector<int> last(z, -1); //コンテストが最後に開催された日
    for (int i = 0; i < t.size(); i++)
    {
        sum += s[i][t[i]];
        last[t[i]] = i;
        for (int j = 0; j < z; j++)
        {
            sum -= c[j] * ((i+1) - (last[j]+1));
        }
        
    }
    return sum;
}

void mix(vector<int> &a, vector<int> &b, int index)
{
    assert(index < d);
    assert(index >= 1);
    for (int i = 0; i < index; i++)
    {
        swap(a[i], b[i]);
    }
}


void Main()
{
    cin >> d;
    s = vector<vector<int>>(d, vector<int>(z));
    for (int i = 0; i < z; i++)
    {
        cin >> c[i];
    }
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < z; j++)
        {
            cin >> s[i][j];
        }
    }

    const int n_change = 100;
    const int best_n = 256;
    const int mix_n = 128;
    const int change_n = 128;
    const int total_n = best_n + mix_n*2 + change_n;

    std::mt19937 mt(rand());
    std::uniform_int_distribution<> rand_z(0, z-1);
    std::uniform_int_distribution<> rand_mix(1, d-1);
    std::uniform_int_distribution<> rand_d(0, d-1);
    std::uniform_int_distribution<> rand_sample(0, best_n-1);

    vector<vector<vector<int>>> ts(2, vector<vector<int>>(total_n, vector<int>(d)));
    for (int i = 0; i < ts[0].size(); i++)
    {
        for (int j = 0; j < d; j++)
        {
            ts[0][i][j] = rand_z(mt);
        }
    }

    vector<pair<int, int>> scores;
    scores.reserve(ts[0].size());
    int w0 = 0, w1 = 1;
    for (int i = 0; i < n_change; i++)
    {
        scores.resize(0);
        for (int j = 0; j < ts[w0].size(); j++)
        {
            scores.push_back(make_pair(score(ts[w0][j]), j));
        }
        sort(scores.rbegin(), scores.rend());
        cerr << scores[0].first << endl;
        
        for (int j = 0; j < best_n; j++)
        {
            for (int k = 0; k < d; k++)
            {
                ts[w1][j][k] = ts[w0][scores[j].second][k];
            }
        }
        for (int j = 0; j < mix_n; j++)
        {
            int rand_a = rand_sample(mt);
            int rand_b = rand_sample(mt);
            for (int k = 0; k < d; k++)
            {
                ts[w1][best_n + 2*j][k] = ts[w1][rand_a][k];
                ts[w1][best_n + 2*j+1][k] = ts[w1][rand_b][k];
            }
            mix(ts[w1][best_n + 2*j], ts[w1][best_n + 2*j+1], rand_mix(mt));
        }
        for (int j = 0; j < change_n; j++)
        {
            int rand_a = rand_sample(mt);
            for (int k = 0; k < d; k++)
            {
                ts[w1][best_n + 2*mix_n + j][k] = ts[w1][rand_a][k];
            }
            ts[w1][best_n + 2*mix_n + j][rand_d(mt)] = rand_z(mt);
        }

        swap(w0, w1);
    }

    for (int i = 0; i < d; i++)
    {
        cout << ts[w0][scores[0].second][i] + 1 << endl;
    }
    
}

int main(int argc, char **argv)
{
    Main();
    return 0;
}



Measured memory_bytes: 6132
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int N = scanner.nextInt();
    boolean allEven = true;
    for (int i = 0; i < N; i++) {
      int a = scanner.nextInt();
      allEven &= a % 2 == 0;
    }
    if (allEven) System.out.println("second");
    else System.out.println("first");
  }
}


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

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

    double ans = 0;
    exrep(j, 1, n) {  // サイコロでjが出たとする
        ll p = 1;
        ll u = j;
        while(u < k) {
            u *= 2;
            p *= 2;
        }   
        ans += 1.0/(double)p;
    }
    ans *= 1.0/(double)n;

    exout(ans);
    re0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
using ll = long long;
int main(){
    int n;
    cin >> n;
    int v[n],c[n];
    for(int i=0;i<n;i++)cin >> v[i];
    for(int i=0;i<n;i++)cin >> c[i];
    int x=0,y=0;
    int max = 0;
    for(int bit=0;bit<(1<<n);bit++){
        x=0;
        y=0;
        for(int i=0;i<n;i++){
            if(bit & 1 << i){
                x+=v[i];
                y+=c[i];
            }
        }
        if(max < x-y)max=x-y;
    }
    cout << max << endl;
    return 0;
}

Measured memory_bytes: 256
#include <string>
#include <algorithm>
#include <vector> 
#include <math.h>
#include <iostream>
using namespace std;
long long mod = 1000000000 + 7;


int main()
{
	
	vector<int> time;
	for (int i = 0; i < 5; i++) {
		int k = 0;
		cin >> k;
		time.push_back(k);
	}
	
	sort(time.begin(),time.end());
	int ans = 0;
	int sh = 11;
	for (int i = 0; i < 5; i++) {
		if (time[i] % 10 == 0) {
			ans = ans + time[i];
		}
		else {
			sh = min(sh, time[i] % 10);
			ans = ans + time[i] - time[i] % 10 + 10;
		}
	}
	if (sh < 11) {
		ans = ans + sh - 10;
	}
	cout << ans << endl;
	return 0;

}


Measured memory_bytes: 256
while True:
    a,b=map(int,input().split())
    if a==0 and b==0:
        break
    if a<b:
        a,b=b,a
    C=0
    while True:
        a=a%b
        a,b=b,a
        C=C+1
        if b==0:
            break
    print(a,C)


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

// @@ !! LIM()

int main(/* int argc, char *argv[] */) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

  ll N; cin >> N;
  vector<ll> A(N);
  ll sum = 0;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  ll acc = 0;
  ll ans = LLONG_MAX;
  for (ll i = 0; i < N; i++) {
    acc += A[i];
    ans = min(ans, abs((sum - acc) - acc));
  }
  cout << ans << endl;
  

  return 0;
}



Measured memory_bytes: 4760
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

int main()
{
    ll n;
    cin>>n;
    vector<ll> a(n),b(n);
    for(ll i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
    }
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    if(n&1) cout<<b[n/2]-a[n/2]+1<<endl;
    else cout<<(b[n/2]+b[(n-1)/2]) - (a[n/2]+a[(n-1)/2]) + 1<<endl;
}

Measured memory_bytes: 6336
N, W = map(int, input().split())
v = []
w = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
dp = [ [0 for i in range(W+1)] for j in range(N+1)]
for i in range(1,N+1):
    for j in range(1,W+1):
        if w[i-1] <= j:
            dp[i][j] = max(v[i-1] + dp[i-1][j-w[i-1]], dp[i-1][j])
        else:
            dp[i][j] = dp[i-1][j]
print(dp[N][W])

Measured memory_bytes: 171528
(let((n(read))(a(read))(b(read))(s 0))
  (loop for i from 1 to n do
		(setq m(apply #'+(mapcar #'(lambda(c)(-(char-int c)48))(concatenate 'list (princ-to-string i)))))
		(if(and(>= m a)(<= m b))(incf s i)))
  (princ s))


Measured memory_bytes: 22112
//#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#define INF 2147483647
#define lli long long int
#define MOD 1000000007
#define pb push_back

using namespace std;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;

bool check(vector<string> A, vector<string> B){
	bool flag = true;
	for (int i = 0; i < A.size() - B.size(); i++) {
		for (int j = 0; j < A.size() - B.size(); j++) {
			flag = true;
			for (int k = 0; k < B.size(); k++) {
				for (int  l = 0; l < B.size(); l++) {
					if(A[i+l][j+k] != B[k][l]) {
						flag = false;
					}
				}
			}
			if (flag) break;
		}
		if (flag) break;
	}

	return flag;
}

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

	vector<string> imgA(n);
	vector<string> imgB(m);
	for (int i = 0; i < n; i++) {
		cin >> imgA[i];
	}
	for (int i = 0; i < m; i++) {
		cin >> imgB[i];
	}

	if (check(imgA,imgB)) {
		cout << "Yes" << endl;
	}else{
		cout << "No" << endl;
	}
	return 0;
}





Measured memory_bytes: 256
parseInt(x::String) = parse(Int, x)
parseInt(x::SubString{String}) = parse(Int, x)
parseInt(x::Char) = parse(Int, x)
parseInt(x::Float64) = trunc(Int, x)
parseFloat(x::String) = parse(Float64, x)
parseFloat(x::Int) = trunc(Float64, x)
parseFloat(x::SubString{String}) = parse(Float64, x)
parseFloat(x::Char) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    a,b,k=parseMap(split(readline()))
    for i in 1:k
        if i%2==1
            a=div(a,2)
            b+=a
        else
            b=div(b,2)
            a+=b
        end
    end
    println("$a $b")
end
main()

Measured memory_bytes: 166952
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;


int main()
{
	int n;cin >> n;
	ll ans = 0;
	vector<int> t;
	REP(i,n)
	{
		int tmp;cin >> tmp;
		t.PB(tmp);
		ans += tmp;
	}
	int m;cin >> m;
	vector<pair<int,int> > p;
	REP(i,m)
	{
		int a,b;cin >> a >> b;
		p.PB(MP(a,b));
	}
	
	REP(i,m)
	{
		cout << ans - (t[p[i].FI-1] - p[i].SE) << endl;
	}
	
	
	
	
	return 0;
}

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


int main() {
  int n;
  cin >> n;
  while (n--) {
    float x[4], y[4];
    float eps = 1e-15;
    for (int i = 0; i < 4; i++) cin >> x[i] >> y[i];
    if (((abs(y[3] - y[2]) < eps && abs(x[3] - x[2]) < eps)) || (abs(x[1] - x[0]) < eps && abs(y[1] - y[0]) < eps)) {
      cout << "NO" << endl;
    } else if (abs((y[3] - y[2]) * (x[1] - x[0]) - (y[1] - y[0]) * (x[3] - x[2])) < eps) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}

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


int main(){
  ll n;cin >> n;
  vector<ll> p(n+1);
  rep(i,n) cin >> p.at(i+1);
  vector<bool> b(n+1);
  for(ll i=1;i<=n;++i){
    if( p.at(i) == i ) b.at(i) = true;
  }
  ll ans = 0;
  for(ll i=1;i<=n;++i){
    if( b.at(i) ){
      ++ans;
      if( i != n ){
        if( b.at(i+1) ) ++i;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 3964
#include <bits/stdc++.h>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define int long long
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 

vector<int> init(int size){
  vector<int> par(size,-1);
  return par;
}

int root(int x, vector<int>& par){
  if(par[x] < 0){
    return x;
  }else{
    return par[x] = root(par[x],par);
  }
}

bool same(int x, int y, vector<int>& par){
  return root(x, par) == root(y, par);
}

void unite(int x, int y, vector<int>& par){
  x = root(x, par), y = root(y, par);
  if(same(x,y, par))return;
  par[x] += par[y];
  par[y] = x;
}

int size(int x, vector<int>& par){
  return -par[root(x, par)];
}

signed main(){
  int a,b;
  cin>>a>>b;
  if(a==b)cout << a+a;
  else if(a>b)cout<<a+a-1;
  else cout<<b+b-1;
}


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

#define ff first
#define ss second
#define endl '\n'

const ll MOD = 1e9 + 7ll;

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

	int n;
	cin >> n;

	ll pow2[61];
	pow2[0] = 1;
	for (int i = 1; i < 61; ++i) {
		pow2[i] = 2ll * pow2[i-1];
	}

	ll x, ones[61] = {0};
	for (int i = 0; i < n; ++i) {
		cin >> x;
		for (int b = 0; b < 61; ++b) {
			ones[b] += ((x & pow2[b]) > 0);
		}
	}

	ll ans = 0;
	for (int b = 0; b < 61; ++b) {
		ans += (((ones[b] * (n - ones[b])) % MOD) * (pow2[b] % MOD)) % MOD;
		ans %= MOD;
	}

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

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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double

# define ull unsigned long long
# define ui unsigned int

# define pb push_back
# define pf push_front

# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >
# define PQ priority_queue

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvs vector < vs >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp

# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >

# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >

# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))

# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";
# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);

string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};

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

const ld pie = 3.1415926535897932384626 ;
const ll mod = 1e9 + 7 ;

/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor ( (a-1)/b ) .

void solve ( int test_case )
{
    int a , b ;
    cin >> a >> b ;

    const int inf = 1e6 ;

    ld ma = 0.08 ;
    ld mb = 0.10 ;

    for ( ld i = 1 ; i < inf ; i ++ )
	{
        int aa = ma * i ;
        int bb = mb * i ;

        if ( aa == a && bb == b )
		{
			cout << i ;
			line ; R ;
		}
	}

	cout << -1 ;
}

int main()
{
    int t = 1;
    // cin >> t;

    for ( int i=1 ; i <= t ; i ++ ) solve ( i ) ;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
using namespace std;

struct item{
	int v;
	int w;
};

int knapsack(int N, int W, vector<struct item> items){
	int C[101][10001];
	for(int i=0;i<=W;i++)
		C[0][i] = 0;
	
	for(int i=1;i<=N;i++)
		C[i][0] = 0;
	
	for(int i=1;i<=N;i++){
		for(int j=1;j<=W;j++){
			if(items[i-1].w <= j)
				C[i][j] = max(C[i-1][j], items[i-1].v + C[i-1][j - items[i-1].w]);
			else
				C[i][j] = C[i-1][j];
		}
	}

	return C[N][W];
}

int main(){
	int N, W, num1, num2;
	vector<struct item> items;
	

	cin>>N>>W;
	for(int i=0;i<N;i++){
		cin>>num1>>num2;
		struct item temp = {num1, num2};
		items.push_back(temp);
	}	
	
	cout<<knapsack(N, W, items)<<endl;
	return 0;
}

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

using namespace std;

//#define int long long
#define FOR(i, j, k) for(int i = j; i < (int)k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)

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

const int MOD = 1e9 + 7;
const int dy[] = {0, 0, 1, -1};
const int dx[] = {1, -1, 0, 0};

template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

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

    int n, a;
    cin >> n >> a;
    cout << n * n - a << endl;
    return 0;
}

Measured memory_bytes: 256
import math
N,K = map(int,input().split())
ans = 0.0
def f(i):
    k=float(K/i)
    x=0
    while(k>1):
        k /=2
        x+=1
    
    return x

for i in range(1,N+1):
    ans += 1.0/float(float(N)*float(2.0**f(i)))
print(ans)

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

int main(){
  long long A,V,B,W,T;
  cin >> A >> V >> B >> W >> T;
  
  int d =abs(A-B);

  if(d<=(V-W)*T || d==0){
    cout << "YES" <<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  
  return 0;
}

Measured memory_bytes: 3636
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int R = sc.nextInt();
		sc.close();
		double s = 2*R*Math.PI;
		System.out.println(s);
	}
}

Measured memory_bytes: 35916
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) { cout << #v << "=" << endl; rep(i_debug, v.size()) { cout << v[i_debug] << ",";}cout << endl; }
#define mdebug(m) { cout << #m << "=" << endl; rep(i_debug, m.size()) { rep(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } cout << endl; } }

int main() {
    int N, M;
    cin >> N >> M;
    vec<vec<int>> s(M, vec<int>());
    rep(i, M) {
        int k;
        cin >> k;
        rep(j, k) {
            int z;
            cin >> z;
            --z;
            s[i].push_back(z);
        }
    }


    vec<int> p(M);
    rep(i, M) cin >> p[i];

    int ans = 0;
    rep(i, 1 << N) {
        bool flag = true;
        rep(j, M) {
            int swich = 0;
            for (auto e : s[j]) {
                if (i & (1 <<e)) ++swich;
            }
            if(swich%2 != p[j]) flag = false;
        }
        if(flag)  ++ans;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
typedef long long ll;

const int MOD = 1e9 + 7;
int N, K;
ll ans = 1, factorial[(int)1e5 + 1];
vector<int> edges[(int)1e5 + 1];

void dfs(int u, int prev, int depth)
{
    int childCnt = 0;
    for (int v : edges[u])
    {
        if (v != prev)
        {
            dfs(v, u, min(depth + 1, 2));
            ans = ans * (K - depth - childCnt) % MOD;
            childCnt++;
        }
    }
}

int main()
{
    cin >> N >> K;
    factorial[0] = 1;
    for (int i = 1; i <= N - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
        factorial[i] = factorial[i - 1] * i % MOD;
    }

    ans *= K;
    dfs(1, 1, 1);

    cout << ans << endl;
    // system("pause");
}


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

using namespace std;

int main(){
 
  int n;
  cin>>n;
  
  int arr[n-1];
  
  for(int i=0;i<n-1;++i)
    cin>>arr[i];
  
  int ans = 0;
  
  for(int i=0;i<n;++i)
  {
    if(i==0||i==n-1)
    {
      ans+= arr[min(i,n-2)];
      
    }
    else{
      ans+= min(arr[i],arr[i-1]);
    }
    
  }
  cout<<ans;
  return 0;
}

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

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define REP(i, n) for(int i=0; i<(int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  int n, m, l;
  while(cin >> n, n) {
    vi score((1<<16), -1);
    vi score_p((1<<16), -1);
    score[0] = 0;
    score_p[0] = 0;
    REP(i, n){
      cin >> m >> l;
      int time = 0;
      REP(j, m){
        int s, e;
        cin >> s >> e;
        FOR(k, s-6, e-6){
          time |= (1 << k);
        }
      }
      REP(j, 1<<16){
        if(score[j] == -1) continue;
        if (j & time) continue;
        int k = j | time;
        if (k >= (1<<16)) continue;
        score_p[k] = max(score_p[k], score[j] + l);
      }
      score = score_p;
    }
    int res = 0;
    REP(j, 1<<16){
      res = max(score[j], res);
    }
    cout << res << endl;
  }
}

Measured memory_bytes: 3336
n = int(input())
a = [input() for i in range(n)]

c0 = str(a.count("AC"))
c1 = str(a.count("WA"))
c2 = str(a.count("TLE"))
c3 = str(a.count("RE"))

print("AC x " + c0)
print("WA x " + c1)
print("TLE x " + c2)
print("RE x " + c3)

Measured memory_bytes: 79164
import math
import numpy as np
from itertools import permutations
n = int(input())
XY = list(tuple(map(int, input().split())) for _ in range(n))

dist = 0
for per in permutations(range(n)):
    for origin, next in zip(per, per[1:]):
        x1, y1 = XY[origin][0], XY[origin][1]
        x2, y2 = XY[next][0], XY[next][1]

        dist += np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
print(dist / math.factorial(n))


Measured memory_bytes: 12504
import itertools
def inpl(): return [int(i) for i in input().split()]

n, m = inpl()
a = [int(i)-1 for i in input().split()]
tans = [0]*2*m
Mi = []
Si = 0
for a0, a1 in zip(a, a[1:]):
    if a1 < a0:
        a1 += m
    tans[a0+2] += 1
    tans[a1+1] -= 1
    Mi.append((a1+1, 1+a0-a1))
    Si += a1 - a0
tans = list(itertools.accumulate(tans))
for x, v in Mi:
    tans[x] += v
tans = list(itertools.accumulate(tans))
ans = [0]*m
for i, v in enumerate(tans):
    ans[i%m] += v
print(Si - max(ans))

Measured memory_bytes: 38024
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v)
    is >> x;
  return is;
}

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

const int N = 16, P = 1000000007;

int n, m;
int a[N + 1], dp[N + 1][1 << N];
int fac[(1 << N) + 5], ifac[(1 << N) + 5];

void prepare(int n) {
  ifac[1] = 1;
  for (int i = 2; i <= n; ++i) ifac[i] = -(P / i) * (ll)ifac[P % i] % P + P;
  ifac[0] = 1;
  for (int i = 1; i <= n; ++i) ifac[i] = ifac[i - 1] * (ll)ifac[i] % P;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * (ll)i % P;
}

int fall(int n, int m) {
  if (m < 0 || m > n) return 0;
  return fac[n] * (ll)ifac[n - m] % P;
}

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> m;
  for (int i = 1; i <= m; ++i)
    cin >> a[i];
  prepare(1 << n);
  dp[m][0] = 1;
  for (int i = m; i; --i) {
    memcpy(dp[i - 1], dp[i], sizeof(dp[i]));
    for (int j = 0; j < 1 << n; ++j)
      for (int k = 0; k < n; ++k)
        if (!((j >> k) & 1))
          dp[i - 1][j | (1 << k)] = (dp[i - 1][j | (1 << k)] + (ll)(P - fall((1 << n) - a[i] - j, (1 << k) - 1)) * dp[i][j] % P * (1 << k)) % P;
  }
  int ans = 0;
  for (int i = 0; i < 1 << n; ++i)
    ans = (ans + dp[0][i] * (ll)fac[(1 << n) - 1 - i]) % P;
  ans = ans * (1LL << n) % P;
  cout << ans << '\n';

#ifdef LBT
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


Measured memory_bytes: 4864
#include <iostream>
using namespace std;
void judge1(int a)
{
    if(100 < a || a <= 0)
    {
        cout << "error" << endl;
    }
}

void judge2(int a)
{
    if(100 <= a || a <= -100)
    {
        cout << "error" << endl;
    }
}
    
void check(int W, int H, int x, int y, int r)
{
    judge1(W);
    judge1(H);
    judge2(x);
    judge2(y);
    judge1(r);
}

int main(int argc, const char * argv[])
{
    int W,H,x,y,r;
    cin >> W;
    cin >> H;
    cin >> x;
    cin >> y;
    cin >> r;
    check(W,H,x,y,r);
    if( x+r > W || y+r > H || x < 0 || y < 0 )
    {
        cout << "No" << endl;
    }
    else
    {
        cout << "Yes" << endl;
    }
    return 0;
}

Measured memory_bytes: 1160
H,W=map(int,input().split())
S=[list(map(int, input().split())) for i in range(10)]
l=[None]*10
for i in range(10):
    l[i]=S[i][1]
for i in range(8):
    for j in range(10):
        for k in range(10):
            if l[j]>l[k]+S[j][k]:
                l[j]=l[k]+S[j][k]
l.append(0)
a=0
for i in range(H):
    L=list(map(int,input().split()))
    for j in L:
        a+=l[j]
print(a)

Measured memory_bytes: 3064
"use strict";

const isUpperCase = str => {
    return str === str.toUpperCase() ? true : false;
}

const main = (input) => {
    const spi = input.split('\n');
    const x = spi[0].split(' ').map(e => parseInt(e));

    for (let i = 0; i < 5; i++) {
        if (x[i] === 0) {
            console.log(i + 1);
            return;
        }
    }
};

main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 29540
#include<bits/stdc++.h>
using namespace std;
#define int long long

int inf = 1LL<<55;

#define MAX_N 100000

struct edge {
  int to, cost;
  edge(){}
  edge(int to, int cost):to(to), cost(cost){}
};

typedef pair<int, int> P;

int N, M, X;
int H[MAX_N];
vector<edge> graph[MAX_N];
vector<int> mincost;
vector<int> mincost2;

signed main(){
  cin >> N >> M >> X;
  for(int i = 0; i < N; i++) cin >> H[i];
  while(M--) {
    int a, b ,t;
    cin >> a >> b >> t;
    --a, --b;
    if(t <= H[a]) graph[a].emplace_back(b, t);
    if(t <= H[b]) graph[b].emplace_back(a, t);
  }
  mincost.resize(N, inf);
  mincost2.resize(N, inf);  
  priority_queue<P, vector<P>, greater<P> > que;
  priority_queue<P, vector<P>, greater<P> > que2;
  que.emplace(0, 0);
  mincost[0] = 0;
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    //cout << v << " " << mincost[v] << endl;
    if(mincost[v] < p.first) continue;
    if(v == N-1) {
      cout << H[v] - (X - mincost[v]) + mincost[v] << endl;
      return 0;
    }
    for(edge& e : graph[v]) {
      //cout << " " << e.to << " "<< mincost[e.to] << endl;
      int cost = mincost[v] + e.cost;
      if(H[e.to] < X-cost) cost = X - H[e.to];
      if(cost > X) {
	//if(H[v] < e.cost) continue;
	cost += cost - X;
	if(cost < mincost2[e.to]) {
	  mincost2[e.to] = cost;
	  que2.emplace(cost, e.to);
	}
      } else if(cost < mincost[e.to]) {
	mincost[e.to] = cost;
	que.emplace(cost, e.to);
      }
    }
  }
 
  while(!que2.empty()) {
    P p = que2.top(); que2.pop();
    int v = p.second;
    //cout << v << " "<< mincost2[v] << endl;
    if(mincost2[v] < p.first) continue;
    if(v == N-1) {
      cout << mincost2[v] + H[v] << endl;
      return 0;
    }
    for(edge& e : graph[v]) {
      if(mincost2[v] + 2*e.cost < mincost2[e.to]) {
	mincost2[e.to] = mincost2[v] + 2*e.cost;
	que2.emplace(mincost2[v] + 2*e.cost, e.to);
      }
    }
  }
  cout << -1 << endl;
  
  return 0;
}

Measured memory_bytes: 22644
#include<bits/stdc++.h>
using namespace std;
#define MN 1005
int n,want[MN][MN],vis[MN],now[MN],ok[MN];
inline int read(){
	int a=0;char c=getchar();
	while(c>'9'||c<'0')c=getchar();
	while('0'<=c&&c<='9'){
		a=a*10+c-48;
		c=getchar();
	}
	return a;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		now[i]=1;
		for(int j=1;j<n;++j)
		want[i][j]=read();
	}
	int CNT=0,r=0,bol=0;
	while(CNT<n){
		r++;
		bol=0;
		for(int i=1;i<=n;++i)
			if(now[i]<n){
			int j=want[i][now[i]];
			if(want[j][now[j]]==i){
				bol=1,ok[i]=ok[j]=max(ok[i],ok[j])+1,now[i]++,now[j]++;
			}
			if(now[i]==n&&!vis[i])vis[i]=1,CNT++;
			if(now[j]==n&&!vis[j])vis[j]=1,CNT++;
		}
		if(!bol&&CNT<n){puts("-1");return 0;}
	}
	for(int i=1;i<=n;++i)r=max(r,ok[i]);
	printf("%d",r);
	return 0;
}


Measured memory_bytes: 4352
use std::io::*;
use std::str::FromStr;
use std::collections::HashMap;

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 s: Vec<char> = read::<String>().chars().collect();
    let t: Vec<char> = read::<String>().chars().collect();

    let mut s_map = HashMap::new();
    let mut t_map = HashMap::new();

    for ch in s.iter() {
        let count = s_map.entry(ch).or_insert(0);
        *count += 1;
    }
    for ch in t.iter() {
        let count = t_map.entry(ch).or_insert(0);
        *count += 1;
    }
 
    let mut s_values: Vec<i32> = s_map.values().map(|v| *v).collect();
    let mut t_values: Vec<i32> = t_map.values().map(|v| *v).collect();
 
    s_values.sort();
    t_values.sort();
 
    if s_values == t_values {
        println!("Yes");
    }else{
        println!("No");
    }
}

 

Measured memory_bytes: 6396
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) for (int i = 0; i <= (int)(n); i++)
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define ANS cout << ans << endl
#define all(a) a.begin(),a.end()
#define SORT(a) sort(all(a))
#define REVERSE(a) reverse(all(a))
#define int long long
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORe(i, m, n) for(int i = m;i <= n;i++)
#define VIin(a) for(int i=0; i<a.size(); i++) cin >> a[i]
#define OUT(a) cout << a << endl
typedef vector<int> VI;
typedef vector<VI> VVI;
const int MOD=1e9+7;
const long long INF = 1LL << 60;
/*最大公約数*/int gcd(int x, int y) { return y ? gcd(y,x%y) : x;}
/*最小公倍数*/int lcm(int a, int b) { int g = gcd(a, b); return a / g * b; }
/*素数判定*/int isPrime(int n){if(n<2){return 0;}for(int i=2; i*i<=n; i++){if(n%i==0){return 0;}}return 1;}
/*約数列挙*/vector<int> getDivisor(int n){vector<int> v; for(int i=1; i*i<=n; i++){if(n%i==0){v.push_back(i);if(i!=n/i){v.push_back(n/i);}}}sort(v.begin(),v.end()); return v;}
//----------------------------------------------
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n; cin >> n;
    int sum = 0;
    VI x(n); VIin(x);
    VI y(n); rep(i,n) y[i] = x[i];
    SORT(y);
    int a,b;
    a = y[n/2-1];
    b = y[n/2];
    rep(i,n){
        if(x[i] <= a) OUT(b);
        else OUT(a);
    }

    return 0;
}

Measured memory_bytes: 5376
//============================================================================
// Name        : template.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <set>
#include <vector>
#include <string.h> // for memset in CF judge.
using namespace std;
#define _CRT_SECURE_NO_DEPRECATE // suppress some compilation warning messages (for VC++ users)
// Shortcuts for "common" data types in contests
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
// To simplify repetitions/loops, Note: define your loop style and stick with it!
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define REP(i, a, b) \
for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define forn(i,n) \
for (int i =0; i<(n); i++)
#define TRvi(c, it) \
for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) \
for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000 // 2 billion
// If you need to recall how to use memset:
#define MEMSET_INF 127 // about 2B
#define MEMSET_HALF_INF 63 // about 1B
//memset(dist, MEMSET_INF, sizeof dist); // useful to initialize shortest path distances
//memset(dp_memo, -1, sizeof dp_memo); // useful to initialize DP memoization table
//memset(arr, 0, sizeof arr); // useful to clear array of integers

int A() {
	int a,b,c;
	cin >> a >> b >> c;
	if(b -a == c-b) cout << "YES" << endl;
	else cout << "NO" << endl;
return 0;
}

int main() {
   string a,b; cin >> a >> b;
   int i = 0; int j = 0;
   string ans = "";
   while(i < a.length() && j < b.length()) {
       ans += a[i++];
       ans += b[j++];
   }

   if(i < a.length()) {
   	while(i < a.length()) {
   		ans += a[i++];
   	}
   } else {
   	while(j < b.length()) {
   		ans += b[j++];
   	}
   }
   cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256