akhauriyash/Code-Regression huggingface dataset:
maxn = 100000

n , q = map(int, input().split())
p = list(map(int, input().split()))

par = [0] * (n+1)
rank = [0] * (n+1)

#初期化
for i in range(n+1):
    par[i] = i
    rank[i] = 0

#判定(一番上野根を求める)
def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
#結合
def unite(x , y):
    rx = find(x)
    ry = find(y)
    
    if rx == ry:
        return
    
    if rank[rx] < rank[ry]:
        par[rx] = ry
    elif rank[rx] > rank[ry]:
        par[ry] = rx
    else:
        par[ry] = x
        rank[rx] += 1

for _ in range(q):
    a,b = map(int, input().split())
    unite(a,b)

ans = 0
for i in range(n):
    if find(p[i]) == find(i+1):
        ans += 1
        
print(ans)

Measured memory_bytes: 98156
import java.io.IOException;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) {
		while (true) {
			int n = nextInt();
			if (n == 0) {
				break;
			}
			int[] points = new int[n];
			for (int i = 0; i < points.length; i++) {
				points[i] = nextInt();
			}
			Arrays.sort(points);
			int sum = 0;
			for (int i = 1; i < points.length - 1; i++) {
				sum += points[i];
			}
			System.out.println(sum / (n - 2));
		}

	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return -1;
	}

}

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

int main(){
  int N;
  cin >> N;
  pll p[N];
  rep(i,N){
    ll a,b;
    cin >> a >> b;
    p[i]=make_pair(b,a);
  }
  sort(p,p+N);
  ll sum=0;
  bool f=true;
  rep(i,N){
    sum+=p[i].second;
    if(sum>p[i].first){
      f=false;
      break;
    }
  }
  if(f) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

Measured memory_bytes: 3456
gets
s = gets.chomp

a = []
before_ch = ""
for ch in s.split("")
  if ch == before_ch
    a[a.size-1] = 2
  else
    a << 1
  end
  before_ch = ch
end

count = 1
before_n = 0
for n in a
  count = count * 3 if before_n == 0 and n == 1
  count = count * 6 if before_n == 0 and n == 2
  count = count * 2 if before_n == 1 and n == 1
  count = count * 2 if before_n == 1 and n == 2
  count = count * 1 if before_n == 2 and n == 1
  count = count * 3 if before_n == 2 and n == 2
  before_n = n
end

puts count % 1000000007


Measured memory_bytes: 1788
n,x = map(int,input().split())
l = list(map(int,input().split()))
d = [0]*(n+1)
for i in range(n):
  d[i+1] = d[i] + l[i]
  if d[i+1] > x:
    print(i+1)
    d[n] = x + 1
    break
if d[n] <= x:
  print(n+1)

Measured memory_bytes: 38384
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
	int a = 0;
	int b = 0;

	scanf("%d", &a);
	scanf("%d", &b);

	if (a == 1 && b != 1) { printf("Alice"); return 0; }
	if (a != 1 && b == 1) { printf("Bob"); return 0; }
	if (a == 1 && b == 1) { printf("Draw"); return 0; }

	if (a > b) { printf("Alice"); return 0; }
	if (a < b) { printf("Bob"); return 0; }
	if (a == b) { printf("Draw"); return 0; }

	return 0;
}

Measured memory_bytes: 128
#include <stdio.h>

typedef struct{
  char name[10];
  int t;
} Process;

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

  int num,times,i;
  Process Q[200000];
  int count = 0, sum = 0;

  scanf("%d%d",&num,&times);

  for( i = 0 ; i < num ; i++ ){
    scanf("%s",Q[i].name);
    scanf("%d",&Q[i].t);
  }

  for( i = 0 ; i < num ; i++ ){

    Q[i].t -= times;

    if( Q[i].t <= 0 ){
      sum += Q[i].t+times;
      printf("%s %d\n",Q[i].name,sum);
    }else{
      Q[num] = Q[i];
      sum += times;
      num++;
    }

  }

  return 0;
}

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

int main(){
  int A[3];
   
  for(int i=0;i<3;i++) cin>>A[i];
  sort(A,A+3);

  if(A[0]==5&&A[1]==5&&A[2]==7) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;

 return 0;
}
  
  
  
  
  
  


Measured memory_bytes: 256
#include<cstdio>
#include<cmath>
int m=99,n,w[12]={1,6,9,36,81,216,729,1296,6561,7776,46656,59049};
void dfs(int p,int s,int c)
{
	if(!(s^n))m=fmin(m,c);
	else if(p--)for(int i=0;i<6&&s+w[p]*i<=n;++i)dfs(p,s+w[p]*i,c+i);
}
int main()
{
	scanf("%d",&n);
	dfs(12,0,0);
	printf("%d",m);
}

Measured memory_bytes: 128
/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/
 
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x
 
using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;
 
const ll MODBASE = 1000000007LL;
const int MAXN = 200010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int cnt[MAXN];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    int n;
    cin >> n;
    FOR(i,2,n) {
        int p;
        cin >> p;
        cnt[p]++;
    }
    FOR(i,1,n) cout << cnt[i] << "\n";
    return 0;
}


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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 100100;
const int maxlg = 17;

int n;
vector<pii> adj[maxn];

ll dist[maxn];

void dfs(int v, int p = -1, ll d = 0) {
    dist[v] = d;

    for (auto i : adj[v])
        if (i.first != p)
            dfs(i.first, v, d + i.second);
}

int main() {
    ios_base::sync_with_stdio(0);

    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].push_back(pii(b, c));
        adj[b].push_back(pii(a, c));
    }

    int q, k;
    cin >> q >> k;
    dfs(k);
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        cout << dist[a] + dist[b] << '\n';
    }
}

Measured memory_bytes: 12800
#include <bits/stdc++.h>
#define boost_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define int long long
#define num(a) a-'0'
#define charr(a) (a - 'A' + 1)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define maxs(a,b) if(b>a)a=b
#define mins(a,b) if(b<a)a=b
#define debug(x) cerr<<"["<<#x<<":"<<x<<"] "
#define debug2(a,b) debug(a);debug(b)
#define debug3(a,b,c) debug2(a,b);debug(c)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define print(arr) for(auto it = arr.begin();it < arr.end();it ++){cout << *it << " ";}cout << ln;
#define all(a) (a).begin(), (a).end()
#define vi vector<long long>
#define v vector
#define p pair
#define pb push_back
#define mk make_pair
#define f first
#define s second
#define ln "\n"
typedef long double ld;
typedef double d;
using namespace std;
int modF=1e9+7;
int INF=1e11;

void solve(){
    string s,t;
    cin>>s>>t;
    int l=s.length();
    int tl=t.length();
    int ans=INF;
    for(int i=0;i<=l-tl;i++){
        int counter=0;
        int h=0;
        rep(j,i,i+tl){
            if(s[j]!=t[h]){
                counter++;
            }
            h++;
        }
        mins(ans,counter);
    }
    cout<<ans<<ln;
}

signed main(){
	boost_io;
	solve();
	return 0;
}


Measured memory_bytes: 3656
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
int main() {
  int N;
  cin >> N;
  int X1, X2, X3, X4, X5, X6, X7, X8, X9, X0;
  X1 = (N/1)%10;
  X2 = (N/10)%10;
  X3 = (N/100)%10;
  X4 = (N/1000)%10;
  X5 = (N/10000)%10;
  X6 = (N/100000)%10;
  X7 = (N/1000000)%10;
  X8 = (N/10000000)%10;
  X9 = (N/100000000)%10;
  X0 = (N/1000000000)%10;
  if(N%(X1+X2+X3+X4+X5+X6+X7+X8+X9+X0)==0) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
}

Measured memory_bytes: 3616
sd = gets.chomp
t = gets.chomp

result = false
count = sd.length - t.length

count.downto(0){|i|
  arr = sd[i, t.length]
  arr.length.times{|ai|
    result = arr[ai] == '?' || arr[ai] == t[ai]
    break if result == false
  }
  if result then
    sd[i, t.length] = t
    break
  end
}

puts result ? sd.gsub('?','a') : "UNRESTORABLE"

Measured memory_bytes: 1788
//omajinai c++
#include <iostream>
#include<cmath>
#include<vector>
#include <algorithm>
#include <limits>
#include <numeric>
#include <type_traits>
#include <map>
#include <string>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
constexpr auto mod = 1000000007;
using namespace std;
int main() {
string s,t;
  cin>>s>>t;
  int a,b;
  cin>>a>>b;
  string u;
  cin>>u;
  if(u==s){
    cout<<a-1<<' '<<b;
  }
  else{
     cout<<a<<' '<<b-1;
  }
}

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

void solve(){
    ll k; cin >> k;
    queue<ll> q;
    for(int i=1; i<=9; i++) q.push(i);
    ll cnt = 0;
    ll res;
    while(true){
        ll x = q.front();
        q.pop();
        //cout << x << endl;
        cnt++;
        if(cnt == k){
            res = x;
            break;
        }
        ll rem = x%10;
        if(rem == 0){
            q.push(10*x + rem);
            q.push(10*x +(rem + 1));
        }
        else if(rem == 9){
            q.push(10*x + (rem - 1));
            q.push(10*x + rem);
        }
        else{
            q.push(10*x + (rem - 1));
            q.push(10*x + rem);
            q.push(10*x + (rem + 1));
        }
    }
    cout << res << endl;
}

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

Measured memory_bytes: 1792
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string>
#include <limits>
#include <algorithm>
#include <map>
#include <sstream>
#include <unordered_map>
#include <set>

using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
static ull tenq = 1000000000;
static ull mod = tenq + 7;

using namespace std;

int main()
{
  ll N;
  cin >> N;
  vector<ll> count(N, 0);
  for (auto i = 0; i < N; i++) {
    ll a;
    cin >> a;
    count[a]++;
  }
  ll num = 0;
  ll res = 1;
  for (ll i = (N % 2) + 1; count[i] == 2; i += 2) {
    num++;
    res = (res * 2) % mod;
  }
  if (num * 2 + (N%2) != N) {
    res = 0;
  }

  cout << res << endl;

  return 0;
}


Measured memory_bytes: 1024
#include <iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#include <vector>
#include <utility>
using P = pair<char,int>;
bool flag = true;
#define SENTINEL 1000000000
void Merge(vector<P>& A,int left,int mid,int right){
    int n1 = mid - left;
    int n2 = right - mid;
    vector<P> L(n1+1),R(n2+1);
    rep(i,n1){
        L[i] = A[left + i];
    }
    rep(i,n2){
        R[i] = A[mid + i];
    }
    L[n1].second = SENTINEL;
    R[n2].second = SENTINEL;
    int i = 0;
    int j = 0;
    for(int k=left;k<right;k++){
        if(L[i].second <= R[j].second){
            A[k] = L[i];
            i = i+1;
        }else{
            A[k] = R[j];
            j = j+1;
        }
    }
}
void MergeSort(vector<P>& A,int left,int right){
    if((left + 1) < right){
        int mid = (left+right)/2;
        MergeSort(A, left, mid);
        MergeSort(A, mid, right);
        Merge(A, left, mid, right);
    }
}
int partition(vector<P>& A,int p,int r){
    int x = A[r].second;
    int i = p-1;
    for(int j=p;j<r;j++){
        if(A[j].second <= x){
            i++;
            auto t = A[i];
            A[i] = A[j];
            A[j] = t;
        }
    }
    auto t = A[i+1];
    A[i+1] = A[r];
    A[r] = t;
    return i+1;
}
void quicksort(vector<P>& A,int p,int r){
    if(p < r){
        int q = partition(A,p,r);
        quicksort(A,p,q-1);
        quicksort(A,q+1,r);
    }
}
int main(){
    int N;
    cin >> N;
    vector<P> A(N);
    rep(i,N){
        cin >> A[i].first >> A[i].second;
    }
    vector<P> v2(A.size());
    rep(i,N){
        v2[i].first = A[i].first;
        v2[i].second = A[i].second;
    }
    MergeSort(v2,0,N);
    quicksort(A,0,N-1);
    rep(i,N){
        if(!(v2[i].first == A[i].first))flag = false;
    }
    if(flag == false){
        cout << "Not stable" << endl;
    }else{
        cout << "Stable" << endl;
    }
    rep(i,N){
        cout << A[i].first << " " <<A[i].second << endl;
    }
    return 0;
}


Measured memory_bytes: 5092
package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	var alice, bob int

	fmt.Scan(&n)
	cards := make([]int, n)
	for i := range cards {
		fmt.Scan(&cards[i])
	}

	sort.Sort(sort.Reverse(sort.IntSlice(cards)))

	for i, num := range cards {
		if i%2 == 0 {
			alice += num
		} else {
			bob += num
		}
	}
	fmt.Println(alice - bob)
}


Measured memory_bytes: 512
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <climits>
#include <string>
#include <bitset>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int,int> > vpii;
typedef vector<pair<string,int>> vpsi;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define fin(ans) cout<<(ans)<<endl
#define STI(s) atoi(s.c_str())
#define mp(p,q) make_pair(p,q)
#define pb(n) push_back(n)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define Sort(a) sort(a.begin(),a.end())
#define Rort(a) sort(a.rbegin(),a.rend())
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;}
const int P = 1000000007;
const int INF = INT_MAX;
const ll LLINF = 1LL<<60;

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

int n;
cin >> n;
vi b(n-1);
int a=0;
rep(i,n-1)cin >> b[i];
a += b[0];
for(int i=1;i<n-1;++i){
    a += min(b[i],b[i-1]);
}
a += b[n-2];
fin(a);

//////////////////////////////////////////////////////
return 0;}

Measured memory_bytes: 256
while(1):
    try:
        s = list(input())
        skip = 0
        o = []
        for i in range(len(s)):
            if skip != 0:
                skip -= 1
                continue
            if s[i] != "@":
                o.append(s[i])
            else:
                skip = 2
                for j in range(int(s[i+1])):
                    o.append(s[i+2])
        print(*o,sep="")

    except EOFError:
        break


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

using namespace std;

int main(){
  string s;
  int acount = 0, bcount = 0, ccount = 0;

  cin >> s;

  for (int i = 0; i < 3; i++){
    if (s.at(i) == 'a'){
      if (acount == 1){
        cout << "No" << endl;
        return 0;
      }
      acount++;
    } else if (s.at(i) == 'b'){
      if (bcount == 1){
        cout << "No" << endl;
        return 0;
      }
      bcount++;
    } else if (s.at(i) == 'c'){
      if (ccount == 1){
        cout << "No" << endl;
        return 0;
      }
      ccount++;
    }
  }
  cout << "Yes" << endl;
  return 0;
}



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

long long lcm (long long a, long long b) {
	return (a * b) / __gcd(a, b);
}

void cklcm (long long &x, long long a) {
	x = lcm(x, a);
}

signed main () {
	long long n, m;
	cin >> n >> m;
	vector < long long > a(n);
	long long cur = 1;
	for (long long &x: a) {
		cin >> x;
		cklcm(cur, x / 2);
		if (cur > m) {
			cout << "0\n";
			return 0;
		}
	}
	for (long long &x: a) {
		if (cur % x != (x / 2)) {
			cout << "0\n";
			return 0;
		}
	}
	long long ans = m / (cur * 2);
	if (m % (cur * 2) >= cur)
		++ans;
	cout << ans << '\n';
}


Measured memory_bytes: 1024
package main

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

var sc = bufio.NewReaderSize(os.Stdin, 1024*1024*1)

func main() {
	a := getStdin()
	if 'a' <= a[0] && a[0] <= 'z' {
		fmt.Printf("a\n")
	} else {
		fmt.Printf("A\n")
	}
}

func getStdin() string {
	return readLine()
}
func getStdinInt() int {
	str := getStdin()
	rtn, _ := strconv.Atoi(str)
	return rtn
}
func getStdinInt64() int64 {
	str := getStdin()
	rtn, _ := strconv.ParseInt(str, 10, 64)
	return rtn
}
func getStdinIntArr() []int {
	str := getStdin()
	list := strings.Split(str, " ")
	rtn := make([]int, len(list))
	for idx, val := range list {
		rtn[idx], _ = strconv.Atoi(val)
	}
	return rtn
}

func getStdinArr() []string {
	str := getStdin()
	list := strings.Split(str, " ")
	return list
}

func getStdinIntArr64() []int64 {
	str := getStdin()
	list := strings.Split(str, " ")
	rtn := make([]int64, len(list))
	for idx, val := range list {
		rtn[idx], _ = strconv.ParseInt(val, 10, 64)
	}
	return rtn
}

func readLine() string {
	buf := make([]byte, 0, 0)
	for {
		l, p, e := sc.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}


Measured memory_bytes: 1808
#include <bits/stdc++.h>
 
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
 
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
 
#define _all(arg) begin(arg),end(arg)
 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
using ll=long long;
 
// Description: DP using Convex hull Trick
// TimeComplexity: $ \mathcal{O}(n)$ 
// Verifyed: AOJ 2725

//Inequality sign min >=  max <=
template<typename T> class CHT{
    public:
        T getval(T x){
            while(deq.size()>=2 && f(deq[0],x)<=f(deq[1],x)) deq.pop_front();
            return f(deq[0],x);
        }

        void push(T ca,T cb){
            const int idx=a.size();
            a.push_back(ca),b.push_back(cb);
            while(deq.size()>=2 && check(idx)) deq.pop_back();
            deq.push_back(idx);
        } 

        int size(){return deq.size();}
    private:
        vector<T> a,b;
        deque<int> deq;

        T f(int idx,T x){return 1LL*a[idx]*x+b[idx];}

        bool check(int idx){
            const int i=deq[size()-2],j=deq[size()-1],k=idx;
            return (b[j]-b[i])*(a[k]-a[j])<=(b[k]-b[j])*(a[j]-a[i]);
        }
};

ll t[4010],p[4010],f[4010];
ll dp[4010][4010];
CHT<ll> cht[4010];
 
inline ll sq(ll n){return n*n;}
 
int main(void){
    ll N,T;
    cin >> N >> T;
 
    vector<tuple<ll,ll,ll>> ary(N);
    rep(i,N){
        ll tt,pp,ff;
        cin >> tt >> pp >> ff;
        ary[i]=make_tuple(ff,tt,pp);
    }
 
    sort(_all(ary));
    rep(i,N) tie(f[i],t[i],p[i])=ary[i];
         
    rep(i,1,N+1)rrep(j,T+1,t[i-1]){
        bool ok=true;
        if(j==t[i-1]){
            chmax(dp[i][j],p[i-1]);
        }else if(j>t[i-1]){
            const ll base=j-t[i-1];
            if(cht[base].size()==0) ok=false;
            if(ok) chmax(dp[i][j],p[i-1]-sq(f[i-1])+cht[base].getval(f[i-1]));
            
            
        }

        if(ok==false) continue;
        const ll base=j;
        cht[base].push(2LL*f[i-1],-1LL*sq(f[i-1])+1LL*dp[i][j]);
    }
    
    ll ans=0LL;
    rep(i,N+1) rep(j,T+1) chmax(ans,dp[i][j]);
    cout << ans << endl;
 
    return 0;
}

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

int n,m,u,v,ans;
vector<int> a[10];
int f[10];

void dfs(int p,int q){
	f[q]=1;
	if (p==n) ans++;
	else{
		for (int i=0;i!=a[q].size();i++){
			if (f[a[q][i]]==0){
				dfs(p+1,a[q][i]);
				f[a[q][i]]=0;
			}
		}
	}
}

int main(){
	cin >> n >> m;
	for (int i=1;i<=m;i++){
		cin >> u >> v;
		a[u].push_back(v);
		a[v].push_back(u);
	}
	for (int i=1;i<=n;i++) f[i]=0;
	ans=0;
	dfs(1,1);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vd vector<ld>
#define vvd vector<vector<ld>>
#define ALL(a) begin(a), end(a)
#define RALL(a) rbegin(a), rend(a)
#define SORT(a) stable_sort(ALL(a))
#define RSORT(a) stable_sort(RALL(a))
#define REV(a) reverse(ALL(a))
#define lambda(i) [](auto& i)
#define compare(i, j) [](auto& i, auto& j)
#define min_of(a) *min_element(ALL(a))
#define max_of(a) *max_element(ALL(a))
#define sum(a) accumulate(ALL(a), 0)
#define pb push_back
#define mp make_pair
#define SZ(a) ll((a).size())
#define REP(i, n) for (decltype(n) i = 0; i < (n); i++)
#define RREP(i, n) for (auto i = n - 1; i != static_cast<decltype(i)>(-1); i--)
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define RFOR(i, a, b) for (auto i = b - 1; i != a; i--)
#define EACH(i, a) for (auto&& i: (a))
using namespace std;

const ll INF = 1e18;
const ll mod = 1e9 + 7;

vi cum(vi V) {
  vi U(SZ(V));
  partial_sum(ALL(V), begin(U));
  return U;
}

void solve() {
  ll n;
  ll ans = 0;

  cin >> n;
  vi v(n), u(n);
  REP(i, n) {
    cin >> v[i];
    u[i] = v[i];
  }
  RSORT(u);
  REP(i, n) {
    cout << u[v[i] == u[0] ? 1 : 0] << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(15);
  solve();
}


Measured memory_bytes: 4720
N = int(input())
A = list(map(int, input().split()))
S = {}
for a in A:
    if a in S:
        S[a] += 1
    else:
        S[a] = 1
ans = 0
for k, v in S.items():
    if k > v:
        ans += v
    elif k < v:
        ans += v - k
print(ans)


Measured memory_bytes: 17780
while 1:
    n = input()
    if n == 0: break
    A = map(int,raw_input().split())
    A.sort()
    c,l = A[0],0
    for i in A:
        if i == c:
            l += 1
            if l > n/2:
                print c
                break
        else:
            c = i
            l = 1
    else:
        print "NO COLOR"

Measured memory_bytes: 103908
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double n;
long long sum,m;
int main()
{
    long long  a[5],b[5];
    double n;
    sum=0;
    for(int i=1;i<=4;i++)
         scanf("%lld",&a[i]);
       b[1]=a[1]*8;
       b[2]=a[2]*4;
       b[3]=a[3]*2;
       b[4]=a[4];
       sort(b+1,b+5);
      scanf("%lf",&n);
      m=(long long)n*100;
      sum+=m/200*b[1];
      m=m%200;
     b[1]=a[1]*4;
     b[2]=a[2]*2;
     b[3]=a[3];
     sort(b+1,b+4);
     sum+=m/100*b[1];
     m=m%100;
     b[1]=a[1]*2;
     b[2]=a[2];
     sum+=m/50*min(b[1],b[2]);
     m=m%50;
     sum+=m/25*a[1];
     printf("%lld\n",sum);
      return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = (int)(n)-1; i >=0; i--)
using ll = long long;

int main(){
  string s,t;
  cin>>s>>t;
  int flag = 0;
  rep1(i,s.size()-t.size()+1){
    string tmp = s;
    rep(j,t.size()){
      if(tmp.at(i+j)!='?'&&tmp.at(i+j)!=t.at(j))break;
      else if(tmp.at(i+j)=='?')tmp.at(i+j)=t.at(j);
      if(j==(int)t.size()-1){
        flag = 1;
        s = tmp;
        break;
      }
    }
    if(flag)break;
  }
  if(!flag)cout<<"UNRESTORABLE"<<endl;
  else{
    rep(i,s.size()){
      if(s.at(i)=='?')s.at(i)='a';
      cout<<s.at(i);
    }
    cout<<endl;
  }
  return 0;
}







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

using namespace std;
using ll = long long;
using ull = unsigned long long;

#ifndef __MACRO_H__
#define __MACRO_H__

#define all(collection) (collection).begin(), (collection).end()  // begin to end
#define each(e, collection) for(auto& e: collection)              // for each
#define rep(i, begin, end) for (ll i = begin; i < end; i++)       // repeat
#define repr(i, begin, end) for (ll i = begin; end < i; i--)      // repeat reverse
#define size(collection) ((ll) (collection).size())               // collection size

#endif

namespace NyaGadget
{
	/**
	@brief 素因数分解ライブラリ
	@note
	 nを素因数分解する。
	 ans[i]がnだったとき、素因数分解して得られた積のなかにn個の素数iが含まれていることを表す。
	 例えば n=12 のとき ans[2] = 2, ans[3] = 1 となるので 12 = 2 * 2 * 3 と分かる。
	**/
	void PrimeFactorization(ll n, map<ll, ll>& ans)
	{
		for (ll i = 2; i * i <= n; i++)
		{
			while (n % i == 0)
			{
				ans[i]++;
				n /= i;
			}
		}
		if (n != 1)
			ans[n]++;
	}
}

namespace NyaGadgetCpp17
{
	/**
	@brief 最大公約数を計算する関数
	@note
	 aとbの最大公約数を返す
	**/
	long long Gcd(long long a, long long b)
	{
		return b ? Gcd(b, a % b) : a;
	}
	/**
	@brief 最小公倍数を計算する関数
	@note
	 aとbの最小公倍数を返す
	**/
	long long Lcm(long long a, long long b)
	{
		return a / Gcd(a, b) * b;
	}
};

using namespace NyaGadgetCpp17;
using namespace NyaGadget;

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

	map<ll, ll> ans;
	PrimeFactorization(NyaGadgetCpp17::Gcd(a, b), ans);

	cout << size(ans) + 1;
	return 0;
}



Measured memory_bytes: 256
import java.util.Scanner;

class Main {
  public static int root(int[] root, int i) {
    int r = root[i];
    if (r == i) {
      return r;
    } else {
      int r2 = root(root, r);
      root[i] = r2;
      return r2;
    }
  }

  public static void main(String[] args) {

    var sc = new Scanner(System.in);

    int n = sc.nextInt();
    int m = sc.nextInt();

    var ps = new int[n];
    for (int i = 0; i < n; i++) {
      ps[i] = sc.nextInt() - 1;
    }

    var ab = new int[2 * m];
    for (int i = 0; i < m; i++) {
      int a = sc.nextInt() - 1;
      int b = sc.nextInt() - 1;
      ab[2 * i    ] = a;
      ab[2 * i + 1] = b;
    }

    var root = new int[n];
    for (int i = 0; i < n; i++) {
      root[i] = i;
    }
    for (int i = 0; i < m; i++) {
      int a = ab[2 * i    ];
      int b = ab[2 * i + 1];
      int ra = root(root, a);
      int rb = root(root, b);
      if (ra != rb) {
        root[rb] = ra;
      }
    }

    int answer = 0;
    for (int i = 0; i < n; i++) {
      int p = ps[i];
      int rp = root(root, p);
      int ri = root(root, i);
      if (ri == rp) {
        answer++;
      }
    }

    System.out.println(answer);

  }
}

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

int main(void){
    int n,l;
    cin >> n >> l;
    int ans = 0;

    for(int i=1;i<=n;i++){
        ans += l+i-1;
    }

    if(l>0 || l+n-1<0){
        if(abs(l)<abs(l+n-1)) ans -= l;
        else ans -= l+n-1;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
import java.util.*;
import java.io.*;

public class Main {
	
	public static void main(String[] args) throws IOException {	
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		int ar[][]=new int[n][3];
		int j=0;
		for(int i=0;i<n;i++) {
			ar[i][0]=s.nextInt();
			ar[i][1]=s.nextInt();
			ar[i][2]=s.nextInt();
		}
		int ans=Integer.MIN_VALUE;
		for(int i=1;i<n;i++) {
			ar[i][0]=ar[i][0]+Math.max(ar[i-1][1], ar[i-1][2]);
			ar[i][1]=ar[i][1]+Math.max(ar[i-1][0], ar[i-1][2]);
			ar[i][2]=ar[i][2]+Math.max(ar[i-1][1], ar[i-1][0]);
		}
		for(int i=0;i<3;i++)
			ans=Math.max(ans, ar[n-1][i]);
		System.out.println(ans);
	}

	
}

Measured memory_bytes: 63568
import math
from typing import List

buggage_num, truck_num = map(int, input().split())
buggage_list = [int(input()) for i in range(buggage_num)]


def able_to_load_buggage(max_weight: int,
                         truck_num: int, buggage_list: List[int]) -> bool:
    i = 0
    truck = [0 for i in range(truck_num + 1)]
    for buggage in buggage_list:
        if truck[i] + buggage <= max_weight:
            truck[i] += buggage
        else:
            i += 1
            truck[i] += buggage
        if i > (truck_num - 1):
            return False
    for t in truck:
        if t > max_weight:
            return False
    else:
        return True


# data = [i for i in range(31)]
# for d in data:
#     result = able_to_load_buggage(d, truck_num, buggage_list)
#     print(f'{d} {result}')

left = 0
right = 100000 * 10000 + 1
i = 0
while left < right:
    i += 1
    mid = math.floor((left + right) / 2)
    result = able_to_load_buggage(mid, truck_num, buggage_list)
    # print(f'{i} {left} {mid} {right} {result}')
    if result:
        right = mid
    else:
        left = mid + 1
# print(f'{left} {mid} {right}')
print(left)



Measured memory_bytes: 11292
H, W, A, B = map(int, input().split())
ans = [['0' for i in range(W)] for j in range(H)]

for i in range(H):
    for j in range(W):
        if j+1 <= A and i+1 <= B:
            ans[i][j] = '1'
        elif j+1 > A and i+1 > B:
            ans[i][j] = '1'
        
for i in range(H):
    print(''.join(ans[i]))

Measured memory_bytes: 13812
$stdout.sync = true

N = gets.to_i
left = 0
right = N-1

puts 0
left_stat = gets.chomp
exit if left_stat == "Vacant"
puts N-1
right_stat = gets.chomp
exit if right_stat == "Vacant"

query = (left + right / 2)

while true
    puts query
    result = gets.chomp
    exit if result == "Vacant"

    if (left_stat == result) ^ (query - left).even?
        right = query
        right_stat = result
        query = (left + right) / 2
    else
        left = query
        left_stat = result
        query = (left + right) / 2
    end
end

Measured memory_bytes: 4424
package main

import (
	"fmt"
	"math"
)

var ans []int

func main() {
	var x, y int
	fmt.Scan(&x, &y)
	ans := math.MaxFloat64

	for i := range make([]struct{}, 4) {
		ans = math.Min(ans, solve(i, x, y))
	}
	fmt.Println(int(ans))
}

func solve(i, x, y int) float64 {
	t := 0

	if i%2 > 0 {
		x *= -1
		t++
	}
	if i/2 > 0 {
		y *= -1
		t++
	}
	if x <= y {
		t += y - x
		return float64(t)
	}
	return math.MaxFloat64
}


Measured memory_bytes: 512
#include<stdio.h>

struct Card {
	char suit;
	int number;
};

int main() {
	int i, j, n, flag = 0, minj;
	char s[5];
	struct Card c1[36], c2[36], v;
	scanf("%d", &n);
	for (i = 0; i < n; i++) { 
		scanf("%s", s);
		c1[i].suit = s[0];
		c1[i].number = atoi(s + 1);
		c2[i] = c1[i];
	}

	for (i = 0; i < n; i++) {
		for (j = n - 1; j >= i; j--) {
			if (c1[j].number < c1[j - 1].number) {
				v = c1[j];
				c1[j] = c1[j - 1];
				c1[j - 1] = v;
			}
		}
	}

	for (i = 0; i < n; i++) {
		minj = i;
		for (j = i; j < n; j++) {
			if (c2[j].number < c2[minj].number)minj = j;
		}
		v = c2[i];
		c2[i] = c2[minj];
		c2[minj] = v;
	}

	for (i = 0; i < n; i++) {
		printf("%c%d", c1[i].suit, c1[i].number);
		if (i != n - 1)printf(" ");
	}
	printf("\nStable\n");
	for (i = 0; i < n; i++) {
		printf("%c%d", c2[i].suit, c2[i].number);
		if (i != n - 1)printf(" ");
	}
	for (i = 0; i < n; i++) {
		if ((c1[i].suit != c2[i].suit) || (c1[i].number != c2[i].number)) { 
			flag = 1;
			break;
		}
	}
	if (flag == 1)printf("\nNot stable\n");
	else printf("\nStable\n");
	return 0;
}

Measured memory_bytes: 612
#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define int long long
#define pb push_back
#define inf 1e9+10
#define mod 1000000007

struct item
{
    int wt;
    int val;
};

int fun(vector<item> &arr, int n, int w)
{
    int maxval = n * 1000; // max value of all the bags
    int dp[n + 1][maxval + 1];

    for (int i = 0; i <= maxval; i++)
    {
        dp[1][i] = inf;
    }
    dp[1][0] = 0;
    dp[1][arr[1].val] = arr[1].wt;

    for (int i = 2; i <= n; i++)
    {
        for (int j = 0; j <= maxval; j++)
        {
            dp[i][j] = dp[i - 1][j]; // by not choosing the item
            if (arr[i].val <= j)
            {
                dp[i][j] = min(dp[i - 1][j], arr[i].wt + dp[i - 1][j - arr[i].val]);
            }
        }
    }
    int ans = 0;
    for (int i = 0; i <= maxval; i++)
    {
        if (dp[n][i] <= w)
        {
            ans = i;
        }
    }
    return ans;
}

void solve()
{
    int n, w;
    cin >> n >> w;
    vector<item> arr(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> arr[i].wt >> arr[i].val;
    }
    cout << fun(arr, n, w);
}

int32_t main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    solve();
    return 0;
}


Measured memory_bytes: 78336
#include<bits/stdc++.h>
using namespace std;
vector<int> a[100000];

int main() {
	int n, m;
	cin >> n >> m;
	int l, r;
	for (int i = 0; i < m; i++) {
		cin >> l >> r;
		a[l].push_back(r);
		a[r].push_back(l);
	}
	vector<int> res(n + 1, 0);
	queue<int> p;
	p.push(1);
	while (!p.empty()) {
		int cur = p.front();
		p.pop();
		for (int x : a[cur]) {
			if (res[x] == 0) {
				res[x] = cur;
				p.push(x);
			}
		}
	}
	for (int i = 2; i < n + 1; i++) {
		if (res[i] == 0) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	for (int i = 2; i < n + 1; i++) {
		cout << res[i] << endl;
	}
	return 0;
}

Measured memory_bytes: 10160
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,k;
  cin >> n >> k;
  string s;
  cin >> s;
  int i;
  vector<int>ass;
  int mode = 1;
  long tmp = 0;
  for(i=0;i<n;i++){
    if((int)(s[i]-'0')==mode)tmp++;
    else{
      ass.push_back(tmp);
      tmp=1;
      mode = (mode+1)%2;
    }
  }
  long ans = 0;
  ass.push_back(tmp);
  if(mode==0)ass.push_back(0);
  if(ass.size()<=2*k+1)cout<<n<<endl;
  else{
    for(i=0;i<=2*k;i++){
      ans += ass[i];
    }
    tmp = ans;
    for(i=0;2*i+2*k+2<ass.size();i++){
      tmp = tmp-ass[2*i]-ass[2*i+1]+ass[2*i+2*k+1]+ass[2*i+2*k+2];
      if(tmp>ans)ans=tmp;
    }
    cout << ans << endl;
  }
}


Measured memory_bytes: 1020
# include <iostream>
# include <queue>
# include <vector>
# include <algorithm>
# include <functional>

const int MAX_N = 100;
const int INF = 100000000;

typedef std::pair<int, int> P;

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

int n, k;
std::vector<edge> G[MAX_N];
int d[MAX_N];

int dijkstra(int s, int g) {
	std::priority_queue<P, std::vector<P>, std::greater<P>> que;
	std::fill(d, d + n, INF);
	d[s] = 0;
	que.push(P(0, s));
	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second;
		if (v == g)
			break;
		if (d[v] < p.first)
			continue;
		for (int i = 0; i < G[v].size(); ++i) {
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
	return d[g];
}

bool check(int a, int b) {
	if (G[a].empty())
		return false;
	else {
		for (int i = 0; i < G[a].size(); ++i) {
			if (G[a][i].to == b)
				return true;
		}
	}
	return false;
}



int main() {
	while (1) {
		int f;
		int c, d, e;
		std::cin >> n >> k;
		if (n == 0 && k == 0)
			break;
		for (int i = 0; i < n; ++i)
			G[i].clear();
		for (int i = 0; i < k; ++i) {
			std::cin >> f;
			if (f == 0) {
				std::cin >> c >> d;
				--c;
				--d;
				int res = dijkstra(c, d);
				if (res == INF)
					res = -1;
				std::cout << res << std::endl;
			}
			else {
				std::cin >> c >> d >> e;
				--c;
				--d;
				if (check(c, d)) {
					for (int i = 0; i < G[c].size(); ++i) {
						if (G[c][i].to == d&&G[c][i].cost>e) {
							G[c][i].cost = e;
							for (int j = 0; j < G[d].size(); ++j) {
								if (G[d][j].to == c)
									G[d][j].cost = e;
							}
						}
					}
				}
				else {
					G[c].push_back(edge(d, e));
					G[d].push_back(edge(c, e));
				}
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3112
#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(), 


signed main(){
    FASTER;
    int n,k;
    cin>>n>>k;
    vi v(n);
    for(int &i:v){
        cin>>i;
        i=(i-1+k)%k;
    }
    for(int i=1;i<n;i++)v[i]=(v[i]+v[i-1])%k;
    
    int ans=0;
    map<int,int> mp;
    mp[0]=1;
    for(int i=0;i<n;i++){
        if(i>=k-1){
            if(i==k-1)mp[0]--;
            else mp[v[i-k]]--;
        }
        ans+=mp[v[i]];
        mp[v[i]]++;
    }
    
    cout<<ans;
}


Measured memory_bytes: 17136
import java.util.Random;
import java.util.Scanner;

public class Main {
    static int d = 365;
    static int[] c = new int[26];
    static int[][] s = new int[d][26];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random random = new Random();
        sc.nextInt();
        for (int i=0;i<26;i++){
            c[i]=sc.nextInt();
        }
        for (int i=0;i<d;i++){
            for (int j=0;j<26;j++){
                s[i][j]=sc.nextInt();
            }
        }
        int[] contest = new int[d];
        int count = 100000;
        for (int i=0;i<d;i++)contest[i]=random.nextInt(26);
        for (int i=0;i<count;i++){
            int[] instance = contest.clone();
            int times = random.nextInt(50)+15;
            int[] choose = new int[times];
            for (int j=0;j<times;j++)choose[j]=random.nextInt(d);
            for (int j=0;j<times;j++)instance[choose[j]]=random.nextInt(26);
            if (function(contest)<function(instance))contest=instance;
        }
        for (int i=0;i<d;i++)System.out.println(contest[i]+1);
    }
    static long function(int[] contest){
        long satisfaction = 0;
        int[] interval = new int[26];
        for (int i=0;i<d;i++){
            satisfaction+=s[i][contest[i]];
            for (int j=0;j<26;j++)interval[j]++;
            interval[contest[i]]=0;
            for (int j=0;j<26;j++)satisfaction-=c[j]*interval[j];
        }
        return  satisfaction;
    }
}


Measured memory_bytes: 57624
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <string>
#include <map>
using namespace std;
typedef long long int lli;
#define urept(soeji, start, n) for (int soeji = start; soeji < n; soeji++)
#define drept(soeji, start, n) for (int soeji = start; soeji > n; soeji--)
#define max 1000000000
int main(void)
{
    lli a, b;
    cin >> a >> b;
    if (a <= 0 && b >= 0)
    {
        cout << "Zero" << endl;
    }
    else if (a > 0 && b > 0)
    {
        cout << "Positive" << endl;
    }
    else if (a < 0 && b < 0)
    {
        if ((b - a) % 2 == 0)
        {
            cout << "Negative" << endl;
        }
        else
        {
            cout << "Positive" << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 256
# ABC150 C
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: tuple(map(int, input().split()))
import itertools
n=ni()
p=nl()
q=nl()

if p==q:
    print(0)
    exit()
L=[]
for i in range(n):
    L.append(i+1)
a=0
b=0
idx=1
for l in list(itertools.permutations(L)):
    idx+=1
    if l==p:
        a=idx
    elif l==q:
        b=idx

print(abs(a-b))


Measured memory_bytes: 8052
#include<iostream>
#include<cstring>
using namespace std;
char s[1000000];
int a[1000000]={1,0},n,b[1000000];
int main()
{
	cin>>s;
	int lens=strlen(s);
	for(int i=0;i<=lens-1;i++)
	{
		if(s[i]=='g') b[i]=0;
		else b[i]=1;
	}
	for(int i=2;i<=lens;i++)
	{
		if(i%2==0) a[i]=0;
		else a[i]=1;
	}
	for(int i=0;i<=lens-1;i++)
	{
		if(a[i]<b[i]) n=n-1;
		if(a[i]>b[i]) n=n+1;
	}
	cout<<n;
	return 0;
} 

Measured memory_bytes: 4864
#include<iostream>
using namespace std;
int n,A[100];

void trace(){
    for ( int i = 0; i < n; i++ ){
        if ( i ) cout << " ";
        cout << A[i];
    }
     cout << endl;
}
int main(){
    int i,j,flag,cnt;
    cnt =0;
    cin >> n;
    for ( int i = 0; i < n; i++ ) cin >> A[i];
    
    flag = 1;
    while ( flag ){
        flag = 0;
        for ( j = n-1; j >= 1; j-- ){
            if ( A[j] < A[j-1] ){
              swap ( A[j],A[j-1] ); 
              cnt = cnt+1;
              flag = 1;
            }
        }
    }
     trace();
     cout << cnt << endl;
}

                

Measured memory_bytes: 1160
module Main where

import Debug.Trace
import Data.List
import qualified Data.Sequence as S
import qualified Data.ByteString.Char8 as B
import Data.Int
import Data.Char
import Data.Array
import Data.Maybe
import qualified Data.Map as M
import Data.Ord
import Data.Functor
import Control.Applicative
import Control.Monad
import Text.Printf

solve :: Int -> [Int] -> [(Int, Int, Maybe Int, Maybe Int, Maybe Int)]
solve n l = iter 1 where
    iter i
        | i <= n = (i,
                    heap!i,
                    if parentIdx > 0      then Just (heap!parentIdx)     else Nothing,
                    if leftChildIdx <= n  then Just (heap!leftChildIdx)  else Nothing,
                    if rightChildIdx <= n then Just (heap!rightChildIdx) else Nothing):iter (i+1)
        | i >  n =  [] where
            parentIdx = div i 2
            leftChildIdx  = 2*i
            rightChildIdx = leftChildIdx + 1
    heap = listArray (1, n) l

readIntLn:: IO Int
readIntLn = (fst . fromJust . B.readInt) <$> B.getLine

readIntList:: IO [Int]
readIntList = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

print':: (Int, Int, Maybe Int, Maybe Int, Maybe Int) -> IO ()
print' (id, k, Nothing, Nothing, Nothing) = printf "node %d: key = %d, \n" id k
print' (id, k, Nothing, Just lk, Nothing) = printf "node %d: key = %d, left key = %d,  \n" id k lk
print' (id, k, Nothing, Just lk, Just rk) = printf "node %d: key = %d, left key = %d, right key = %d, \n" id k lk rk
print' (id, k, Just pk, Nothing, Nothing) = printf "node %d: key = %d, parent key = %d, \n" id k pk
print' (id, k, Just pk, Just lk, Nothing) = printf "node %d: key = %d, parent key = %d, left key = %d, \n" id k pk lk
print' (id, k, Just pk, Just lk, Just rk) = printf "node %d: key = %d, parent key = %d, left key = %d, right key = %d, \n" id k pk lk rk
    
main :: IO() 
main = do
    n <- readIntLn
    l <- readIntList
    mapM_ print' $ solve n l



Measured memory_bytes: 4244
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

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

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

int func(string s)
{
    for(int i=0;; ++i){
        if(s == "6174")
            return i;
        
        string L = s;
        sort(L.rbegin(), L.rend());
        sort(s.begin(), s.end());
        
        char t[16];
        sprintf(t, "%04d", stoi(L) - stoi(s));
        s = t;
    }
}

int main()
{
    string s;
    while(cin >> s, s != "0000"){
        if(stoi(s)%1111 == 0){
            cout << "NA" << endl;
        }
        else{
            cout << func(s) << endl;
        }
    }
}

Measured memory_bytes: 1260
a,b,k=gets.chomp.split.map(&:to_i)
k.times do |i|
if i%2==0 
if a%2==1 
a-=1
end
a,b=a/2,b+a/2
else
if b%2==1 
b-=1
end
b,a=b/2,a+b/2
end
end
print "#{a} #{b}"



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

int main(){
    double r,a,b;
    cin >> r;
    a = pow(r,2) * M_PI;
    b = 2 * r * M_PI;

    cout << fixed;
    cout <<setprecision(6) << a << " "<< b  << endl;
}


Measured memory_bytes: 3292
<?php
  fscanf(STDIN, "%i", $n);
  for ($i = 1; $i < $n; $i++)
  {
      fscanf(STDIN, "%i %i", $a, $b);
      $tree[$a][] = $b;
      $tree[$b][] = $a;
  }

  for ($i = 1; $i <= $n; $i++)
  {
      $uni[$i]   = 0;
      $multi[$i] = 0;
  }
  
  $v = 1;
  $crumbs = [];
  while (!is_null($v))
  {
      $c = array_pop($tree[$v]);
      if (!is_null($c))
      {
          array_remove($tree[$c], $v);
          array_push($crumbs, $v);
          $v = $c;
      }
      else
      {
          $c = $v;
          $v = array_pop($crumbs);

          $bits = 1 << msb($multi[$c]);
          $uni[$c] &= ~($bits - 1);
          $uni[$c] += $bits;
          $multi[$v] |= $uni[$v] & $uni[$c];
          $uni[$v]   |= $uni[$c];
      }
  }
  echo (msb($uni[1]) - 1).PHP_EOL;

  function array_remove(&$array, $elem)
  {
      $rests = [];
      while (!is_null($peek = array_pop($array)) && $peek !== $elem)
          array_push($rests, $peek);
      while (!is_null($peek = array_pop($rests)))
          array_push($array, $peek);
  }

  function msb($x)
  {
      $p = 0;
      while ($x !== 0)
      {
          $x >>= 1;
          $p++;
      }
      return $p;
  }
?>


Measured memory_bytes: 63540
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,d,x,a,cnt=0;
  cin>>n>>d>>x;
  for(int i=0;i<n;i++){
    cin>>a;
    for(int j=1;j<=d;j+=a) cnt++;
  }
  cout<<cnt+x<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll a,b,c;cin>>a>>b>>c;
  cout<<((b-a==c-b)?"YES":"NO")<<endl;
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
	int a,b;
	int sum=0;
	cin>>a>>b;
	if(a<=0&&b>=0)
	{
		cout<<"Zero"<<endl;
	}
	else if(a>0&&b>0)
	{
		cout<<"Positive"<<endl;
	}
	else if(a<0&&b<0)
	{
		sum=b-a+1;
		if(sum%2==0) cout<<"Positive"<<endl;
		else cout<<"Negative"<<endl;
	}
	return 0;
}


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

int main(){
    
    int n;
    cin >> n;
    
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    
    int sum = 0;
    rep(i,n){
        sum += a[i] - 1;
    }
    
    cout << sum << endl;
    
}




Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <numeric>
#include <iomanip>
#include <set>
#include <string.h>

using namespace std;

typedef long long ll;

#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define REP3(i,st,en) for(ll (i)=(st);(i)<(en);(i)++)

using P = pair<ll, ll>;
const int MOD = 1e9+7;
const ll INF = 1e+14;
const double PI = 3.14159265349;




int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  
  cout << N*N*N << "\n";
  return 0;
}


Measured memory_bytes: 384
def solve():
    while True:
        n, h = map(int, input().split())
        if n == 0:
            break

        s = set()
        for _ in [0]*h:
            t, *a = input().split()
            i, j = map(lambda x: int(x)-1, a)
            k = 1
            if t == "xy":
                j *= n
                k = n**2
            elif t == "xz":
                j *= n**2
                k = n
            else:
                i *= n
                j *= (n**2)

            s.update(range(i+j, i+j+k*n, k))

        print(n**3 - len(s))


if __name__ == "__main__":
    solve()


Measured memory_bytes: 9776