akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int et(int a, int b, int c, vector<int>l) {
	int ans = 10000;
	for (int i = 0; i < l.size() - 2; i++) {
		for (int j = i+1; j < l.size() - 1; j++) {
			for (int k = j + 1; k < l.size(); k++) {
				ans = min(ans, abs(l.at(i) - a) + abs(l.at(j) - b) + abs(l.at(k) - c));
			}
		}
	}
	return ans;
}

vector<vector<int>> mopt(int merge_x, vector<int>l) {
	vector<vector<int>>ans(0);
	ans.push_back(l);
	for (int i = 0; i < merge_x; i++) {
		int tmp = ans.size();
		for (int j = 0; j < tmp; j++) {
			for (int k = 0; k < ans.at(j).size() - 1; k++) {
				for (int t = k + 1; t < ans.at(j).size(); t++) {
					vector<int>res;
					for (int u = 0; u < ans.at(j).size(); u++) {
						if (u != k && u != t)res.push_back(ans.at(j).at(u));
					}
					res.push_back(ans.at(j).at(k) + ans.at(j).at(t));
					sort(res.begin(), res.end());
					reverse(res.begin(), res.end());
					ans.push_back(res);
				}
			}
		}
	}
	return ans;
}

int main() {
	int n, a, b, c;
	cin >> n >> a >> b >> c;
	vector<int>l(n);
	for (int i = 0; i < n; i++)cin >> l.at(i);
	sort(l.begin(), l.end());
	reverse(l.begin(), l.end());
	int ans = et(a, b, c, l);
	int merge_x = ans / 10;
	merge_x = min(merge_x, n - 3);
	for (int i = 1; i <= merge_x; i++) {
		vector<vector<int>>merge_opt = mopt(i, l);
		for (int j = 0; j < merge_opt.size(); j++) {
			ans = min(ans, et(a, b, c, merge_opt.at(j))+10*i);
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 84236
#include <bits/stdc++.h>
using namespace std;
const int N = 1414 ;
int n , m , answer = 0 , matrix [N][N] , dp [N][N] ;
int isval (int i , int j , int len )
{
	if ( len == 0 )
	{
		return 1 ;
	}
	int z = len ;
	for (int q = 1 ; q <= len ; q ++)
	{
		if ( matrix [i - q ][j] == 1 || matrix[i][j - q ] == 1)
		{
			z = q - 1 ; 
			break ;
		}
	}
 	return z + 1;
}
int main ()
{
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m ; 
	for (int i = 0 ; i < n ; i ++)
	{
		for (int j = 0 ; j < m ; j ++)
		{
			cin >> matrix[j][i] ;
			if ( matrix[j][i] == 0 )
			{
				dp[j][i] = 1 ;
				answer = 1 ;
			}
		}
	}
	for (int i = 1 ; i < n ; i ++)
	{
		for (int j = 1 ; j < m ; j ++)
		{
			if ( matrix[j][i] == 0)
			{
				dp[j][i] = isval ( j , i , dp[j - 1][i - 1 ] ) ;
			}
			answer = max ( answer , dp[j][i] ) ;
		}
	}
	answer *= answer ;
	cout << answer << endl ; 
	/*cerr << "*********************************" << endl ;
	for (int i = 0 ; i < n ; i ++)
	{
		for (int j = 0 ; j < m ; j ++)
		{
			cerr << dp[j][i] << " " ;
		}
		cerr << endl ;
	}*/
	return  0 ; 
}

Measured memory_bytes: 16496
num_ch = gets.split(' ')[1].to_i
boxes = gets.split(' ').map(&:to_i)

sum = 0
cnt_list = Hash.new(0)
boxes.each do|box|
  sum += box
  sum %= num_ch
  cnt_list[sum] += 1
end

puts(cnt_list.values.inject(0){|s, c| s + c*(c - 1)/2 } + cnt_list[0])


Measured memory_bytes: 12676
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;

map<P,int> mp;
vector<vector<int>> edge(1000000);

void dfs(int x,int p,int color){
    int sz = edge[x].size();
    int nu = 1;
    rep(i,sz){
        int nx = edge[x][i];
        if(nx == p) continue;
        mp[P(x,nx)] = nu;
        if(color == nu) {
            mp[P(x,nx)]++;
            nu++;
        }
        int col = mp[P(x,nx)];
        dfs(nx,x,col);
        nu++;
    }
}

int main(){
    int n; cin >> n;
    vector<P> E;
    rep(i,n-1){
        int a,b; cin >> a >> b;
        a--;b--;
        edge[a].push_back(b);
        edge[b].push_back(a);
        E.push_back(P(a,b));
    }
    int num = 0;
    rep(i,n){
        int x = edge[i].size();
        num = max(num,x);
    }
    cout << num << endl;
    mp[E[0]] = 1;
    int a = E[0].first,b = E[0].second;
    dfs(a,b,1);
    dfs(b,a,1);
    rep(i,n-1){
        int aa = E[i].first,bb = E[i].second;
        cout << mp[P(aa,bb)]+mp[P(bb,aa)] << endl;
    }
}

Measured memory_bytes: 40436
import java.io.*
import java.util.*

var pw = PrintWriter(System.out)
fun solve(A: Long, B: Long, C: Long, D: Long){
    when{
        A+B > C+D -> "Left"
        A+B == C+D -> "Balanced"
        A+B < C+D -> "Right"
        else -> "Invalid"
    }.let{println(it)}
    return
}


fun main(args: Array<String>) {
    fun StringArray(size: Int, init: (Int) -> String = { "\u0000" }): Array<String> {
        return Array<String>(size, init)
    }
    class Scanner(stream: InputStream) {
        private val reader = BufferedInputStream(stream).bufferedReader()
        private var st: StringTokenizer? = null
        fun next(): String {
            while (!(st?.hasMoreTokens() ?: false)) st = StringTokenizer(reader.readLine())
            return st!!.nextToken()
        }
    }
    val sc = Scanner(System.`in`)
    val A = sc.next().toLong()
    val B = sc.next().toLong()
    val C = sc.next().toLong()
    val D = sc.next().toLong()
    solve(A, B, C, D)
}



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


int main(){
    string s;
    string ans;
    while (cin >> s){
	if (s[s.size()-1] == ',' || s[s.size()-1] == '.')
	    s = s.substr(0, s.size()-1);
	if (3 <= s.size() && s.size() <= 6){
	    ans += " " + s;
	}
    }
    cout << ans.substr(1, ans.size()-1) << endl;
}

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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

const int MAX_N = (int)(1e5 + 5);

int n;
ll a[MAX_N];

ll get_high(ll high, ll target) {
  ll high_times = high / target;
  return high_times * target;
}

ll get_low(ll low, ll target) {
  ll low_times = (low + target - 1) / target;
  return low_times * target;
}

bool check_include(ll low, ll high, ll target) {
  if (high < target) {
    return false;
  }

  if (low <= target && target <= high) {
    return true;
  }

  ll low_alpha = (low + target - 1) / target;
  ll high_alpha = high / target;

  if (low_alpha > high_alpha) {
    return false;
  }

  return true;
}

int main(void) {
  // Here your code !
  scanf("%d", &n);

  for (int i = 0; i < n; ++i) {
    scanf("%lld", &(a[i]));
  }

  if (a[n - 1] > 2) {
    printf("%d\n", -1);
    return 0;
  }
  
  ll num_low = 2LL;
  ll num_high = 2LL;

  for (int i = n - 1; i >= 1; --i) {
    ll low = num_low;
    ll high = num_high + a[i] - 1LL;

    if (!check_include(low, high, a[i - 1])) {
      printf("%d\n", -1);
      return 0;
    }

    num_low = get_low(low, a[i - 1]);    
    num_high = get_high(high, a[i - 1]);
  }

  printf("%lld %lld\n", num_low, num_high + a[0] - 1);

  return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint a[200005];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	llint ans = 0;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		ans += a[i], ans %= mod;
	}
	ans *= ans, ans %= mod;
	
	for(int i = 1; i<= n; i++) ans += mod - a[i]*a[i]%mod, ans %= mod;
	ans *= (mod+1)/2, ans %= mod;
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 5200
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORX(i, a, b, x) for (int i = (a); i <= (b); i+=x)
#define debug(x) cerr << #x << " is= " << x << endl; //use for debugging
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 5] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
 
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
 
#define FORD(i, a, b) for(int i = (a); i >= (b); i--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define mp(a,b) make_pair(a,b)
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long int
#define vi         vector<int>
#define vll        vector<ll>
#define vpii       vector<pair<int,int>>
#define vov        vector<vector<int>> 
#define pr         pair<ll,ll>
#define vpll       vector<pair<ll,ll>>
#define all(v)     v.begin(),v.end()
#define rall(v)    v.rbegin(),v.rend()  //reverse of above;
#define sz(container) int((container).size())
#define cut(x) {cout<<x<<"\n"; return;}
#define setprec(x) cout << fixed << showpoint << setprecision(8)<<x<<endl;
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define time cout << clock() / double(CLOCKS_PER_SEC) << endl;
#define endl "\n"
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; 
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
//*s.find_by_order() ->returns an iterator to the k-th largest element (counting from zero);
//s.order_of_key() ->number of elements strictly smaller then our item;
//st.rbegin()->second; last element in set
//st.erase(prev(st.end())); delete last element in set;
//sort(a+1,a+n+1,[](ll x,ll y){return abs(x)>abs(y);});
//sort(v.begin(), v.end(), greater<int>()); 
const int N=2e5+5;
int n,m;
string s;
void solve()
{
 vi ans;
}
int main()
{
  fast_io;
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  readc(a,b,c);
  if(a+b+c>=22) cout<<"bust";
  else cout<<"win";
}

Measured memory_bytes: 3616
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    int a, b, c, d;
    int max, mid, min;
    std::vector<int> array;
    int flag = 0;
    
    std::cin >> a >> b >> c >> d;
    
    //位置関係
    array.push_back(a);
    array.push_back(b);
    array.push_back(c);
    
    sort(array.begin(), array.end());
    
    max = array[0];
    mid = array[1];
    min = array[2];
    
    if (a == max && c == min) {
        if (abs(a - b) <= d && abs(c - b) <= d) flag = 1;
    } else if (c == max && a == min) {
        if (abs(a - b) <= d && abs(c - b) <= d) flag = 1;
    } else if (abs(a - c) <= d) flag = 1;
    
    //tyokusetu
    //std::cout << max << " "<< mid << " " << min << "\n";
    
    if (flag) std::cout << "Yes" << std::endl;
    else if (!flag) std::cout << "No" << std::endl;
    
    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++)
 
int main() {
	int n, m;
	int i, j;
	int a, b;

	cin >> n >> m;

	vector<int> cnt(n);

	for (i = 0; i < m; i++)
	{
		cin >> a >> b;
		cnt.at(a - 1)++;
		cnt.at(b - 1)++;
	}
	for (i = 0; i < n; i++)
		cout << cnt.at(i) << endl;
}



/**/
/*#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define PI 3.141592653589793

int main(void) {
	return 0;
}
*/

Measured memory_bytes: 3584
#include <bits/stdc++.h>
using namespace std;
using LL  = long long;
using VI  = vector<LL>;
using VVI = vector<VI>;
using VB  = vector<bool>;
using VS  = vector<string>;
using PII = pair<LL, LL>;
using VP  = vector<PII>;
#define PB           push_back
#define MP           make_pair
#define SZ(a)        LL((a).size())
#define EACH(x, c)   for (auto x : (c))
#define ALL(c)       (c).begin(), (c).end()
#define REVERSE(c)   reverse(ALL(c))
#define SORT(c)      stable_sort(ALL(c))
#define RSORT(c)     stable_sort((c).rbegin(), (c).rend())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n)    FOR(i, 0, n)
#define dump(x)      cerr << #x << " = " << (x) << endl;


int main() {
    LL N;
    cin >> N;
    vector<pair<string, LL>> X(N);
    unordered_map<string, LL> M;
    REP(i, N) {
        string s;
        LL x;
        cin >> s >> x;
        X[i] = make_pair(s, x);
        string ss = s + to_string(x);
        M[ss] = i + 1;
    }
    stable_sort(ALL(X), [](auto x, auto y) {return x.second > y.second;});
    stable_sort(ALL(X), [](auto x, auto y) {return x.first < y.first;});
    /*REP(i, N) {
        cout << X[i].first << " " << X[i].second << endl;
        }*/
    EACH(x, X) {
        cout << M[x.first + to_string(x.second)] << endl;
    }
    
    return 0;
}


Measured memory_bytes: 256
n = int(input())
s = input()

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

ans = r_cnt * g_cnt * b_cnt

for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1

print(ans)

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

// using mint = modint1000000007;
// using mint = modint998244353;

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
 
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;

bool print_space_enable = false;
void print() { 
    cout << '\n'; 
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) std::cout << " ";
    cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << '\n';
}

void solve() {
    Int a, b, q;
    cin >> a >> b >> q;
    vector<Int> s(a + 1), t(b + 1), x(q);
    for (Int i = 0; i < a; i++) {
        cin >> s[i + 1];
    }
    s[0] = -INF;
    s.push_back(INF);
    for (Int i = 0; i < b; i++) {
        cin >> t[i + 1];
    }
    t[0] = -INF;
    t.push_back(INF);
    for (Int i = 0; i < q; i++) {
        cin >> x[i];
    }
    for (Int i = 0; i < q; i++) {
        Int sr = *lower_bound(all(s), x[i]) - x[i];
        Int tr = *lower_bound(all(t), x[i]) - x[i];
        Int sl = x[i] - *(--lower_bound(all(s), x[i]));
        Int tl = x[i] - *(--lower_bound(all(t), x[i]));
        print(min({max(sr, tr), max(sl, tl), min(sr, tl) * 2 + max(sr, tl), min(sl, tr) * 2 + max(sl, tr)}));
    }
}

int main() {
    solve();
    return 0;
}


Measured memory_bytes: 6332
#include <cstdio>
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		int ans=0;
		for(int i=0;i<=9;i++) for(int j=0;j<=9;j++) for(int k=0;k<=9;k++) for(int l=0;l<=9;l++) {
			ans+=(n==(i+j+k+l));
		}
		printf("%d\n",ans);
	}
}

Measured memory_bytes: 1028
#include<iostream>
using namespace std;
int main(){
    int n,q,a;
    long long int k;
    cin>>n>>k>>q;
    long long int kofn[n];
    for(int i=0;i<n;i++){
        kofn[i]=k;
    }
    for(int i=0;i<q;i++){
        cin>>a;
        kofn[a-1]++;
    }
    for(int i=0;i<n;i++){
        if(kofn[i]-q<=0){
            cout<<"No"<<endl;
            continue;
        }
        cout<<"Yes"<<endl;
    }
    return 0;
}


Measured memory_bytes: 1408
#include <iostream>
using namespace std;
#define MAX 100000
long long n, k, w[MAX];

long long check(long long P) {
  long long v = 0, num_track = k, available_load = P;
  while (1) {
    if (w[v] > P || v == n) return v;
    if (w[v] > available_load) {
      if(!(--num_track)) return v;
      available_load = P;
    }
    available_load -= w[v];
    v++;
  }
  return v;
}

long long solve() {
  long long mid, left = 0, right = 100000 * 10000;
  while (right - left > 1) {
    mid = (left + right) / 2;
    if (check(mid) >= n) right = mid;
    else left = mid;
  }
  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  long long p = 0;
  cout << solve() << endl;
}

Measured memory_bytes: 3840
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#define rep(i,n) for(int i =0;i<(int)(n);i++)
typedef long long ll;
using namespace std;
int main() {
	int n,m;
	cin >> n >> m;
	cout << (n*(n - 1)) / 2 + (m*(m - 1) / 2) << endl;

	return 0;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Text;

namespace AtTest.C_Challenge
{
    class ABC_092
    {
        static void Main(string[] args)
        {
            Method(args);
            Console.ReadLine();
        }

        static void Method(string[] args)
        {
            int n = ReadInt();
            long[] array = ReadLongs();
            long sum = 0;
            long now = 0;
            var eachFee = new long[n];
            var skipFee = new long[n];
            for (int i = 0; i < n; i++)
            {
                sum += Math.Abs(array[i] - now);
                if (i == n - 1)
                {
                    eachFee[i]= Math.Abs(array[i] - now)
                        + Math.Abs(array[i]);
                    skipFee[i]=Math.Abs(now);
                }
                else
                {
                    eachFee[i] = Math.Abs(array[i] - now)
                        + Math.Abs(array[i + 1] - array[i]);
                    skipFee[i] = Math.Abs(array[i + 1] - now);
                }
                now = array[i];
            }
            sum += Math.Abs(array[n - 1]);

            for(int i = 0; i < n; i++)
            {
                Console.WriteLine(sum - eachFee[i] + skipFee[i]);
            }
        }

        private static string Read() { return Console.ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 25948
import sys

def decode(s, n):
    ret = ''
    for c in s:
        if c.isalpha():
            ret += chr((ord(c)+n-ord('a'))%26+ord('a'))
        else:
            ret += c
    return ret

T = ['the', 'this', 'that']

for line in sys.stdin:
    s = line.rstrip()
    for i in xrange(26):
        if any(t in decode(s, i) for t in T):
            print decode(s, i)
            break

Measured memory_bytes: 6316
import sys
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()

cnt = 0
for i in range(N):
  if a[i] %2 == 0:
    if a[i] %3 == 0 or a[i] %5 == 0:
      continue
    else:
      print("DENIED")
      exit()
print("APPROVED")

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

int main() {
  int X;
  cin >> X;
  string res;
  if (X >= 30) {
    res = "Yes";
  } else {
    res = "No";
  }
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 3616
#include<stdio.h>
int main(void){
    int w,n,ai[30],bi[30],_ai[30],_bi[30],i,j,m,output[30];
    scanf("%d%d",&w,&n);
    for(i=0;i<n;i++){
        scanf("%d,%d",&ai[i],&bi[i]);
    }
    for(i=1;i<=w;i++){
        for(j=0;j<n;j++){
            _ai[j]=ai[j];
            _bi[j]=bi[j];
        }
        for(j=0,m=i;j<n;j++){
            if(_ai[j]==m){
                m=_bi[j];
                _ai[j]=0;
                _bi[j]=0;
            }
            if(_bi[j]==m){
                m=_ai[j];
                _ai[j]=0;
                _bi[j]=0;
            }
        }
        output[m-1]=i;
    }
    for(i=0;i<w;i++){
        printf("%d\n",output[i]);
    }
return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> P(N), Q(N);
  for (int &p : P) cin >> p;
  for (int &q : Q) cin >> q;

  if (P > Q) swap(P, Q);

  int ans = 0;
  while (P < Q && ++ans) next_permutation(P.begin(), P.end());
  cout << ans << "\n";
}

Measured memory_bytes: 256
import itertools
from collections import defaultdict
from collections import Counter
H,W,N=map(int, raw_input().split())

d=[-1,0,1]
D=list(itertools.product(d,d))

A=defaultdict(lambda: 0)
for i in range(N):
	a,b=map(int, raw_input().split())
	for dx,dy in D:
		cx=a+dx
		cy=b+dy
		if cx>=2 and cx<=H-1 and cy>=2 and cy<=W-1:
			A[(cx,cy)]+=1

C=Counter(A.values())


for i in range(10):
	if i==0:
		print (H-2)*(W-2)-sum(C.values())
	else:
		print C[i]




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

string s;

int main() {
    cin >> s;
    char temp = s[0];
    int count = 0;
    for (int i = 1; i < s.size(); i++) {
        if (s[i] != temp) {
            temp = s[i];
            count++;
        }
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 512
n,x,y = [int(x) for x in input().split()]
x -= 1
y -= 1
G = [[] for _ in range(n)]
for i in range(n-1):
    G[i].append(i+1)
    G[i+1].append(i)
G[x].append(y)
G[y].append(x)

ans = [0]*n
from collections import deque

def bfs(num):
    q = deque()
    d = [-1]*n
    q.append(num)
    d[num]=0
    while q:
        p = q.popleft()
        for i in G[p]:
            if d[i]==-1:
                d[i] = d[p] + 1
                ans[d[i]] += 1
                q.append(i)

for i in range(n):
    bfs(i)
for i in range(1,n):
    print(ans[i]//2)

Measured memory_bytes: 48092
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
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; }
typedef long long ll;
using namespace std;
int main(void){
    ll n,m,k;cin>>n>>m>>k;
    vector<ll> a(n),b(m);
    rep(i,n) cin>>a[i];
    rep(i,n-1) a[i+1]+=a[i];
    rep(i,m) cin>>b[i];
    rep(i,m-1) b[i+1]+=b[i];
    ll res=0;
    rep(i,n){
        if(a[i]>k)continue;
        auto itr=upper_bound(b.begin(),b.end(),k-a[i]);
        ll K=itr-b.begin();
        chmax(res,(ll)(i+K+1));
    }
    auto itr=upper_bound(b.begin(),b.end(),k);
    ll K=itr-b.begin();
    chmax(res,K);
    cout<<res<<endl;
}

Measured memory_bytes: 6356
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

int main() {
    int c[3][3];
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            cin >> c[i][j];
        }
    }
    
    bool ans = false;
    for (int a1=0; a1<101&&!ans; a1++) {
        int b1=c[0][0]-a1,
            b2=c[0][1]-a1,
            b3=c[0][2]-a1;
        for (int a2=0; a2<101&&!ans; a2++) {
            if (a2+b1==c[1][0]&&
                a2+b2==c[1][1]&&
                a2+b3==c[1][2]) {
                for (int a3=0; a3<101; a3++) {
                    if (a3+b1==c[2][0]&&
                        a3+b2==c[2][1]&&
                        a3+b3==c[2][2]) {
                        ans=true;
                        break;
                    }
                }
            }
        }
    }
    if (ans) {
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}


Measured memory_bytes: 256
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
typedef long long int ll;
using namespace std;
#define P 1000000007
#define rep(i,n) for(i=0;i<n;++i)
#define repp(i,a,n) for(i=a;i<n;++i)
#define repr(i,a,n) for(i=a;i>n;--i)

ll binpowmod(ll a, ll b, ll m) {
    a %= m;
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

ll binpow(ll a, ll b) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return binpowmod(n, p-2, p);
}

int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t,n,k,i,ans;
    cin >> n >> k;
    ans=0;
    while(n>0)
    {
        n/=k;
        ans++;
    }
    cout << ans ;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int X, N;
  vector<int> p;
  vector<pair<int, int>> q;
  cin >> X >> N;
  int y;
  rep(i, N) {
    cin >> y;
    p.push_back(y);
  }
  for (int i = 0; i <= 101; i++) {
    auto result = find(p.begin(), p.end(), i);
    if (result == p.end()) {
      q.push_back(make_pair((X - i) * (X - i), i));
    }
  }
  sort(q.begin(), q.end());
  cout << q[0].second << endl;
  return 0;
}


Measured memory_bytes: 3632
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;

int main(){
  int n;
  cin >> n;
  set<int> st;
  int in;
  for(int i=0; i<n; i++){
    cin >> in;
    st.insert(in);
  }
  cout << st.size() << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
using namespace std;
int a[105];
int main() {
  int n,x;
  cin >> n >> x;
  for(int i = 0; i < n; i++) cin >> a[i];
  sort(a,a+n);
  int ans = 0;
  for(int i = 0; i < n-1; i++) {
    if(x >= a[i]) {
      ans++;
      x -= a[i];
    }
  }

  if(x == a[n-1]) ans++;

  cout << ans << endl;
}


Measured memory_bytes: 764
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

ll fact(ll x){
    ll res = 1;
    for(ll i=1; i<=x; i++) res = res * i % MOD;
    return res;
}

int main(){
    ll n, m;
    cin >> n >> m;
    if(abs(n - m) == 1) cout << fact(n) * fact(m) % MOD << endl;
    else if(abs(n - m) == 0) cout << fact(n) * fact(m) * 2 % MOD << endl;
    else cout << 0 << endl;
    return 0;
}

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

/* -------------------------------- Template -------------------------------- */

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

template<typename T> T inf() { assert(false); }
template<> constexpr int inf<int>() { return 1e9; }
template<> constexpr ll inf<ll>() { return 1e18; }
template<> constexpr ld inf<ld>() { return 1e30; }

struct yes_no : numpunct<char> {
  string_type do_truename()  const { return "Yes"; }
  string_type do_falsename() const { return "No"; }
};

/* -------------------------------- Library -------------------------------- */

/* ---------------------------------- Main ---------------------------------- */

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha;
  cout.imbue(loc);

  int n, k;
  cin >> n >> k;
  vector<int> a(k);
  for (int &i: a) cin >> i;
  sort(ALL(a));
  int sum = accumulate(begin(a), prev(end(a)), 0);
  cout << max(0, a.back() - sum - 1) << endl;
  return 0;
}


Measured memory_bytes: 256
package main

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

func main() {
	N := getStdinInt()
	a := getStdinIntArr()
	cnt := 0
	for i := 0; i < N; i++ {
		if a[i]%2 == 1 && (i+1)%2 == 1 {
			cnt++
		}
	}
	fmt.Printf("%d\n", cnt)
}

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

func getStdin() string {
	return readLine()
}
func getStdinInt() int {
	str := getStdin()
	rtn, _ := strconv.Atoi(str)
	return rtn
}
func getStdinInt2() (int, int) {
	list := getStdinIntArr()
	return list[0], list[1]
}
func getStdinInt3() (int, int, int) {
	list := getStdinIntArr()
	return list[0], list[1], list[2]
}
func getStdinInt4() (int, int, int, int) {
	list := getStdinIntArr()
	return list[0], list[1], list[2], list[3]
}
func getStdinInt64() int64 {
	str := getStdin()
	rtn, _ := strconv.ParseInt(str, 10, 64)
	return rtn
}
func getStdinInt64_2() (int64, int64) {
	list := getStdinIntArr64()
	return list[0], list[1]
}
func getStdinInt64_3() (int64, int64, int64) {
	list := getStdinIntArr64()
	return list[0], list[1], list[2]
}
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 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, _ := sc.ReadLine()
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}


Measured memory_bytes: 2172
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define elif else if
int a,aa[1000],b=0;
signed main()
{
	cin>>a;
	rep(i,a){
		cin>>aa[i];
	}
	sort(aa,aa+a);
	rep(i,a){
		if(aa[i]>aa[i-1]){
			b++;
		}
	}
	cout<<b;
}

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

int main() {
  int N;
  string S;

  cin >> N >> S;

  while (N--) {
    for (int i = 0;i < S.size();i++) {
      if(S[i] == 'Z') {
        S[i] = 'A';
      }
      else {
        S[i] = ++S[i];    
      }
    }
  }

  cout << S;

  return 0;
}

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

const long long MOD = 2;

void solve(long long N, std::vector<long long> A){
  long long ans = 0;
  bool cont = true;
  while(1){
    for(auto&& a : A){
      if(a % 2 == 0){
        a /= 2;
      }else{
        cont = false;
      }
    }
    if(cont == true){
      ans++;
    }else{
      break;
    }
  }
  cout << ans << endl;
}

// Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, std::move(A));
    return 0;
}


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

int main(void){
  int x,n,i,result=0;
  int p[100];
  int z[200]={0};
  
  scanf("%d",&x);
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&p[i]);
    z[p[i]]++;
  }
  for(i=0;i<=101;i++){
    if(z[i]==0&&abs(x-i)<abs(x-result)){
      result=i;
    }
  }
  printf("%d",result);
  
  return 0;
}

Measured memory_bytes: 1736
#include <vector>
#include <string>
#include <iostream>
#define int long long

using namespace std;

signed main(){
    
    string S;
    int K;
    cin >> S >> K;
    vector<int> s;
    
    for(int i = 0; i < S.size(); ++i){
        s.push_back((S[i] - '0'));
    }
    
    int ans = 1;
    
    
    
    for(int i = 0; i < K; ++i){
        if(s[i] != 1){
            ans = s[i];
            break;
        }
    }
    cout << ans << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(){
  char p[101],s[101];
  int i=0,j=0,k=0,l=0,m=0;
  int ans=0;
  for(i=0; i<101; i++){
    scanf("%c",&s[i]);
    if(s[i]=='\n')break;
  }
  for(j=0; j<101; j++){
    scanf("%c",&p[j]);
    if(p[j]=='\n')break;
  }
  for(k=0;k<i;k++){
    if(s[k]==p[0]){
      for(l=0; l<j; l++){
        m=k+l;
        if(m>=i)m-=i;
        if(s[m]!=p[l])break;
        if(l==j-1)ans=1;
      }
    }
  }
  if(ans)printf("Yes\n");
  else printf("No\n");
  return 0;
}


Measured memory_bytes: 2144
//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<ll,ll> P;
const ll MOD=1000000007;
int h,w;
char c[10][10];
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(0);
    cin>>h>>w;
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
            if(c[i][j]=='#') cnt++;
        }
    }
    if(cnt==h+w-1){
        cout<<"Possible"<<endl;
    }else{
        cout<<"Impossible"<<endl;
    }
    
}


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

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

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

    vector<int64_t> q(n, 0L);
    for (auto i : irange(0L, n)) {
        int64_t p;
        cin >> p;
        --p;
        if (p == 0) {
            q[p] = 1;
        } else {
            q[p] = q[p - 1] + 1;
        }
    }

    cout << n - *max_element(q.begin(), q.end()) << endl;
}

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

#define rep(i, n) for(int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> lis(n), cost(n);
    rep(i, n - 1){
        int a, b, w;
        cin >> a >> b >> w;
        a--;
        b--;
        lis[a].push_back(b); cost[a].push_back(w);
        lis[b].push_back(a); cost[b].push_back(w);
    }
    vector<int> ans(n, -1);
    queue<int> q;
    ans[0] = 0;
    q.push(0);
    while(!q.empty()){
        int v = q.front();
        q.pop();
        rep(i, lis[v].size()){
            int u = lis[v][i];
            int w = cost[v][i];
            if(ans[u] != -1) continue;
            ans[u] = (ans[v] + w) % 2;
            q.push(u);
        }
    }
    rep(i, n){
        cout << ans[i] << '\n';
    }
}

Measured memory_bytes: 12416
#include <bits/stdc++.h> 
using namespace std; 
int main() 
{
	int x,count=0;
	cin>>x;
	if(x>=500){
	int k= (x/500)*1000;
	count = count + k;
	int j= x%500;
	j= (j/5)*5;
	count += j;
		cout<<count<<endl;
	}
  else{
  	int j=(x/5)*5;
  	cout<<j<<endl;
  }
    return 0; 
}

Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;


class Program
{
    public static long Read()
    {
        return long.Parse(Console.ReadLine());
    }

    public static long[] Reads()
    {
        return Console.ReadLine().Split().Select(long.Parse).ToArray();
    }

    public static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);

        var input = Reads();
        var a = input[0];
        var b = input[1];
        var x = input[2];
        double s;
        if (a * a * b / 2.0 > x)
        {
            s=(Math.PI/2.0-Math.Atan(2.0*x/(a * b * b)))/Math.PI*180;
        }
        else
        {
            s=Math.Atan(2.0*b/a-2.0*x/(a*a*a)) / Math.PI* 180;
        }
        Console.WriteLine(s);
        Console.Out.Flush();
    }
}


Measured memory_bytes: 13396
def prime_factorization(n):
    """do prime factorization using trial division

    Returns:
        [[prime1,numbers1],[prime2,numbers2],...]
    """
    res = []
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            cnt=0
            while n%i==0:
                cnt+=1
                n//=i
            res.append([i,cnt])
    if n!=1:
        res.append([n,1])
    return res

def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    prime_factors = prime_factorization(N)
    ans = 0
    for p in prime_factors:
        i = 1
        while i <= p[1]:
            ans += 1
            N /= p[0]**i
            p[1] -= i
            i += 1
    print(ans)

if __name__ == '__main__':
    main()


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

using namespace std;

int n, x[200005];
set< int > s;
long long ans;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", x + i);
		if (i == 0) {
			if (x[i]) {
				printf("-1\n");
				return 0;
			}
		} else {
			if (x[i] > x[i - 1] + 1) {
				printf("-1\n");
				return 0;
			}
			if (s.insert(x[i] - i).second)
				ans += x[i];
			else
				ans++;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 10368
#include<bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
signed main(){
	string s,a="CODEFESTIVAL2016";
	cin>>s;
	int cnt=0;
	rep(i,16) {
		if(s[i]!=a[i]) cnt++;
	}
	cout<<cnt<<endl;
}
		
		


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
  int A, B, a[5000], b[5000];
  cin >> A >> B;
  for(int i = 0 ; i < A ; i++ ){
    cin >> a[i];
  }
  for(int i = 0 ; i < B ; i++ ){
    cin >> b[i];
  }
  int ret = 0;
  for(int i = 0 ; i < B ; i++ ){
    int cnt = 0;
    for(int j = 0 ; j < A ; j++ ){
      if(b[i + cnt] == a[j]) cnt++;
    }
    ret = max( ret, cnt);
  }
  cout << ret << endl;
}

Measured memory_bytes: 1200
import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		String s = sc.next();
		boolean flag = true;
		if(s.length() != a + b + 1) {
			System.out.println("No");
			return;
		}
		for(int i = 0; i < a; i++) {
			if(s.charAt(i) < '0' || s.charAt(i) > '9') {
				flag = false;
				break;
			}
		}
		if(s.charAt(a) != '-') {
			flag = false;
		}
		for(int i = a + 1; i < a + b + 1; i++) {
			if(s.charAt(i) < '0' || s.charAt(i) > '9') {
				flag = false;
			}
		}
		if(flag) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
	}
}

Measured memory_bytes: 21972
#include<iostream>

int main(){
    int N;
    std::cin >> N;
    
    long long sum = 0;
    for(int i=1; i<=N; i++){
        if(i % 3 == 0 || i % 5 == 0){
            continue;
        }
        sum += i;
    }
    
    std::cout << sum << std::endl;
    return 0;
}



Measured memory_bytes: 3632
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) throws Exception {
		// TODO 自動生成されたメソッド・スタブ
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String W = in.readLine();

		int X = Integer.parseInt(W);

		int[] F = new int[100004];
		int[] L = new int[100001];
		int lIndex=0;

		Arrays.fill(F, -1);

		for(int i=2;i<F.length;i=i+2){
			if(i==4){
				i--;
			}
			if(F[i]!=-1){
				continue;
			}


			boolean flg = true;
			for(int k=0;k<lIndex;k++){
				if(i%(L[k]*1.0)==0){
					flg=false;
					break;
				}
				if(k*k>i){
					break;
				}
			}
			if(flg){
				//素数
				L[lIndex++]=i;
				F[i]=1;
				for(int k=i+i;k<F.length;k=k+i){
					F[k]=0;
				}
			}
		}
		int ans =-1;
		for(int i=X;i<F.length;i++){
			if(F[i]==1){
				ans=i;
				break;
			}
		}
		System.out.println(ans);
	}
}


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

int n, m;
bool visited[50];
bool graph[50][50];

void dfs(int v) {
    visited[v] = true;
    rep(v2, n) {
        if (!graph[v][v2] || visited[v2]) continue;
        dfs(v2);
    }
}

int main() {
    cin >> n >> m;
    int a[50], b[50];
    rep(i, m) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
        graph[a[i]][b[i]] = graph[b[i]][a[i]] = true;
    }

    int ans = 0;

    rep(i, m) {
        graph[a[i]][b[i]] = graph[b[i]][a[i]] = false;
        {
            rep(j, n) visited[j] = false;
            dfs(0);
            bool bridge = false;
            rep(j, n) if (!visited[j]) bridge = true;
            if (bridge) ans++;
        }
        graph[a[i]][b[i]] = graph[b[i]][a[i]] = true;
    }

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


Measured memory_bytes: 256
str_N = input()
N = int(str_N)

def dfs(n):
    if n > N:
        return 0
    ret = 0
    count = 0
    for i in ['3', '5', '7']:
        if i in str(n):
            count += 1
    if count == 3:
        ret += 1
    n *= 10
    for i in [3, 5, 7]:
        ret += dfs(n+i)
    return ret

print(dfs(0))


Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
#define rep(i,b) for(i=0;i<b;++i)
#define repp(i,a,b) for(i=a;i<b;++i)
#define endl "\n"
//#define LLONG_MAX 99999999999999
typedef long long int lli;
int main(){
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    lli n,i,j,b,a,q,m;
    lli t=1;
    cin>>a>>b;
    if(b>4*a||b<2*a||b%2==1){
			cout<<"No"<<endl;
    }
    else{
			cout<<"Yes"<<endl;
    }


}



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

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

    vector<int> x(m);
    rep (i, m)
        cin >> x[i];

    sort(x.begin(), x.end());

    vector<int> dist(m - 1);
    rep (i, m - 1)
        dist[i] = x[i + 1] - x[i];

    sort(dist.begin(), dist.end(), greater<int>());

    int cnt = min(n - 1, m - 1);
    ll ans = accumulate(dist.begin(), dist.end(), -accumulate(dist.begin(), dist.begin() + cnt, 0));

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

Measured memory_bytes: 4036
#include<cstdio>
#include<vector>
using namespace std;
int n, m;
int depth[100010];
int sz[100010];
int parse[100010];
vector<vector<int>> vt;
void dfs(int here, int de)
{
	depth[here] = de;
	sz[here] = 1;
	for (int i = 0; i < vt[here].size(); i++)
	{
		int next = vt[here][i];
		if (!depth[next])
		{
			parse[next] = here;
			dfs(next, de + 1);
			sz[here] += sz[next];
		}
	}
}
int main()
{
	scanf("%d", &n);
	int sum = n;
	vt.resize(n + 1);
	for (int i = 0; i < n - 1; i++)
	{
		int q, w;
		scanf("%d %d", &q, &w);
		vt[q].push_back(w);
		vt[w].push_back(q);
	}
	dfs(1, 1);
	int ch = depth[n]-depth[1] - 1;
	int ch1 = 0, ch2 = 0;
	ch /= 2;
	while (ch--) {
		n = parse[n];
	}
	ch1 = sz[n];
	ch2 = sum - sz[n];
	if (ch1 >= ch2)
		puts("Snuke");
	else
		puts("Fennec");
}

Measured memory_bytes: 13184
#include <cstdio>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

const int MOD = 1000000007, INF = 1111111111; 
using namespace std;
typedef long long lint;

struct coor {
	int x, y, h;
	coor() {}
	coor(int x, int y, int h = -1) :x(x), y(y), h(h) {}
	const bool operator<(coor l) const { return h > l.h; }
};

//inline bool check(coor data1, coor data2, int X, int Y) {
//	if (data1.h == 0 || data2.h == 0) return true;
//	return data1.h + abs(data1.x - X) + abs(data1.y - Y) == data2.h + abs(data2.x - X) + abs(data2.y - Y);
//}

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;

	vector<coor> data(N);
	for (int i = 0; i < N; i++) cin >> data[i].x >> data[i].y >> data[i].h;
	sort(data.begin(), data.end());

	vector<coor> tmp;
	for (int X = 0; X <= 100; X++) {
		for (int Y = 0; Y <= 100; Y++) {

			int H = data[0].h + abs(data[0].x - X) + abs(data[0].y - Y);
			bool flag = true;
			for (int i = 0; i < N; i++) {

				if (max(H - abs(data[i].x - X) - abs(data[i].y - Y), 0) != data[i].h) {
					flag = false;
					break;
				}

			}

			if (flag) {
				cout << X << " " << Y << " " << H << "\n";
				return 0;
			}

		}
	}

	//vector<int> combi(N);
	//combi[N - 1] = 1;
	//combi[N - 2] = 1;

	//for (const auto &elem : tmp) {
	//	bool flag = true;
	//	do {
	//		auto one = find(combi.begin(), combi.end(), 1);
	//		auto two = find(one + 1, combi.end(), 1);
	//		flag &= check(data[one - combi.begin()], data[two - combi.begin()], elem.x, elem.y);

	//	} while (next_permutation(combi.begin(), combi.end()));

	//	if (flag) {
	//		cout << elem.x << " " << elem.y << " " << data[0].h + abs(data[0].x - elem.x) + abs(data[0].y - elem.y) << "\n";
	//		break;
	//	}
	//}

	return 0;

}

Measured memory_bytes: 256
from scipy.sparse import*
f=csgraph.floyd_warshall
def main():
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  d=d.astype('i')
  print('\n'.join(str(d[i])for i in zip(*[iter(t[m*3+1:])]*2)))
main()

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



int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  ll x, k; cin >> x >> k;
  vector<ll> r(k + 2), diff(k + 2);
  for(int i = 0; i < k; i++) {
    cin >> r[i + 1];
    diff[i + 1] = diff[i] + (i % 2 ? 1 : -1) * (r[i + 1] - r[i]);

    // cerr << r[i + 1] << ", " << diff[i + 1] << endl;
  }
  // sentinel
  r[k + 1] = 1e9 + 19;

  vector<ll> mi(k + 1, x + 1), ma(k + 1, -1);
  mi[0] = 0; ma[0] = x;
  ll rmi = 0, rma = x;
  vector<ll> rvaltop(k + 1), rvalbottom(k + 1);
  bool stop = 0;
  for(int i = 1; i <= k; i++) {
    int df = r[i] - r[i - 1];
    if(mi[i - 1] <= x) mi[i] = mi[i - 1];
    if(ma[i - 1] >= 0) ma[i] = ma[i - 1];
    if(i % 2) { // down
      rmi -= df;
      rma -= df;
    } else { // up
      rmi += df;
      rma += df;
    }
    if(rmi < 0) {
      if(mi[i] <= x) mi[i] += -rmi;
      rmi = 0;
    }
    if(rma < 0) {
      mi[i] = x + 1, ma[i] = -1;
      rma = 0;
    }
    if(rmi > x) {
      mi[i] = x + 1, ma[i] = -1;
      rmi = x;
    }
    if(rma > x) {
      if(ma[i] >= 0) ma[i] -= rma - x;
      rma = x;
    }
    rvalbottom[i] = rmi;
    rvaltop[i] = rma;

    if(mi[i] < 0) mi[i] = x + 1;
    if(ma[i] > x) ma[i] = -1;

    if(mi[i] > ma[i]) mi[i] = x + 1, ma[i] = -1;
    mi[i] = min(mi[i], x + 1);
    ma[i] = max(ma[i], -1ll);
    // cerr << endl << "i : " << i << endl;
    // cerr << "mi: " << mi[i] << " , ma: " << ma[i] << endl;
    // cerr << "rmi: " << rmi << " , rma: " << rma << endl;
  }
  // cerr << endl;

  ll q; cin >> q;
  for(int i = 0; i < q; i++) {
    ll t, a; cin >> t >> a;
    int pmi = upper_bound(begin(mi), end(mi), a) - begin(mi) - 1;
    int pma = upper_bound(begin(ma), end(ma), a, greater<ll>()) - begin(ma) - 1;
    int p = min(pmi, pma);
    int g = upper_bound(begin(r), end(r), t) - begin(r) - 1;
    ll gt = t - r[g];
    int dec = 1 - g % 2;
    int tp = pmi > pma;

    // cerr << endl << "i : " << i << endl;
    // cerr << "g: " << g << " , p: " << p << endl;
    // cerr << "pmi: " << pmi << " , pma: " << pma << endl;
    // cerr << "gt: " << gt << " , dec: " << dec << endl;
    ll val;
    if(p + 1 <= g) {
      // not unique-er
      // no identity
      // HEIBON

      // cerr << "not unque " << endl;
      val = (tp ? rvaltop[g] : rvalbottom[g]) + (dec ? -1 : 1) * gt;
    } else {
      val = a + diff[g] + (dec ? -1 : 1) * gt;
    }
    val = min(val, x);
    val = max(val, 0ll);
    cout << val << endl;
  }
}



Measured memory_bytes: 5888
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const ll mod = 1000000007;

int main() {
    string s,t;
    cin >> s >> t;
    map<char, vector<ll>> mps;
    for (ll i = 0; i < s.size(); i++) mps[s[i]].push_back(i+1),mps[s[i]].push_back(i+1+s.size());
    for (char s = 'a'; s <= 'z'; s++) {
        auto& v = mps[s];
        sort(v.begin(), v.end());
    }
    ll cnt = 0;
    ll freq = 0;
    for (ll i = 0; i < t.size(); i++) {
        if (mps[t[i]].empty()) {
            printf("%d\n",-1);
            return 0;
        } else {
            ll tmp = *upper_bound(mps[t[i]].begin(),mps[t[i]].end(),cnt);
            if (tmp < s.size()) cnt = tmp;
            else freq++, cnt = tmp - s.size(); 
        }
    }
    printf("%lld\n",freq * s.size() + cnt);
}

Measured memory_bytes: 2800