akhauriyash/Code-Regression huggingface dataset:
// includes
#include <bits/stdc++.h>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define FI first
#define SE second
using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}


int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  vector<int> v(5);
  cin >> v;
  int k;
  cin >> k;
  sort(all(v));
  if(v[4] - v[0] <= k)cout << "Yay!" << endl;
  else cout << ":(" << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
ll dp[400][400][135],c[405][405],f[405],r[405],rui[405][405][135];
int n,a[405],b[405],pos[405],pos2[405],t[405][405];
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
int main(){
	cin>>n;
	repn(i,n){
		cin>>a[i];
		pos[a[i]] = i;
	}
	repn(i,n){
		cin>>b[i];
		pos2[b[i]] = i;
	}
	rep(i,405)rep(j,i+1){
		if(j==0||j==i) c[i][j]=1;
		else c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
	}
	f[0] = 1;
	repn(i,400) f[i]=f[i-1]*1LL*i%mod;
	rep(i,401) r[i] = modpow(f[i],mod-2);
	rep(i,n+1){
		rep(j,n+1){
			repn(k,n){
				if(pos[k]<=i || pos2[k]<=j) t[i][j]++;
			}
		}
	}
	dp[0][0][0] = 1;
	for(int k=1;k<=n/3;k++){
		repn(x,n){
			repn(y,n){
				rui[x][y][k] = rui[x-1][y][k]+rui[x][y-1][k]-rui[x-1][y-1][k];
				rui[x][y][k] %= mod;
				if(x<pos[b[y]] && y<pos2[a[x]] && 2*k<=t[x][y] && t[x][y]<=3*k-1);
				else continue;
				dp[x][y][k] = rui[x-1][y-1][k-1];
				dp[x][y][k] += dp[0][0][k-1]*f[3*k-3-t[0][0]]%mod;
				dp[x][y][k] = dp[x][y][k]%mod*r[3*k-3-t[x-1][y-1]]%mod;
				rui[x][y][k] += dp[x][y][k]*f[3*k-t[x][y]]%mod;
				rui[x][y][k] %= mod;
			}
		}
	}
	ll ans = 0;
	rep(i,n+1)rep(j,n+1){
		ll mul = f[n-t[i][j]];
		ans = (ans+dp[i][j][n/3]*mul%mod)%mod;
	}
	rep(i,n){
		if(i%3 != 0) ans = ans*1LL*i%mod;
	}
	cout<<(ans%mod+mod)%mod<<endl;
}

Measured memory_bytes: 341888
#include <bits/stdc++.h>
#define repz(i, z, n) for(decltype(n) i = (z); i < (n); i++)
#define rep(i, n) for(decltype(n) i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define SIZE(v) ((ll)(v).size())
#define MAX(v) (*max_element(all(v)))
#define MIN(v) (*min_element(all(v)))
#define INF 1000000000000 //1E+12

// 略
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define READ cin >>
#define PRINT cout <<

using namespace std;
void debug_out(){ cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
  cout << H << " ";
  debug_out(T...);
}

#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

typedef long long ll;
typedef unsigned long long ul;
// 1s間で可能なループ回数2E+8回
// intの最大値2147483647~2E+9
// long longの最大値9223372036854775807〜9E+18
// 総和accumulate
// 単純リストvector<> push_back
// 連想配列map<key,value> insert,
// pair キーとバリューを格納 firstとsecondでアクセス
// 小数点指定 cout << fixed << setprecision(15) << endl;
// to_string<->stoi
// stoll(longlong)
// stod(double)
// 大文字65-90(-32)
// 小文字97-122(+32)
// 1E+9 +7
const ll mod = 1000000000+7;
int main(){
  // faster
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n,m; cin >> n >> m;
  cout << n*(n-1)/2+m*(m-1)/2 << endl;
  return 0;
}


Measured memory_bytes: 256
#include<set>  
#include<map> 
#include<stack>
#include<cmath>  
#include<cstdio>  
#include<queue>  
#include<vector>  
#include<climits>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
using namespace std;
#define eps 1
#define MAXN 1000
#define INF 0x3f3f3f3f  
int read(){  
    int f=1,x=0;  
    char c=getchar();  
    while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();}  
    while('0'<=c&&c<='9') x=x*10+c-'0',c=getchar();  
    return f*x;  
}
int main()  
{
	char str[10];
	int a=read(),b=read(),ans=0;
	
	for(int i=a;i<=b;i++)
	{
		bool flag=0;
		sprintf(str,"%d",i);
		for(int j=0;j<int(strlen(str))/2;j++)
			if(str[j]!=str[strlen(str)-j-1]){flag=1;break;}
		if(!flag) ans++;
	}
	printf("%d\n",ans);
    return 0;  
}
/**/

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;

int main() {
    int n;
    cin >> n;
    cout << (1000 - n % 1000) % 1000 << endl;
}


Measured memory_bytes: 3628
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        int X = scanner.nextInt();
        int[][] books = new int[N][M + 1];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M + 1; j++) {
                books[i][j] = scanner.nextInt();
            }
        }
        scanner.close();
        int max = 12345678;

        Loop: for (int i = 0; i < (1 << N); i++) {
            int[] summmary = new int[M + 1];
            for (int j = 0; j < N; j++) {
                if ((1 & i >> j) == 1) {
                    for (int index = 0; index < M + 1; index++) {
                        summmary[index] += books[j][index];
                    }
                }
            }
            for (int j = 1; j < M + 1; j++) {
                if (summmary[j] < X) {
                    continue Loop;
                }
            }
            if (max > summmary[0]) {
                max = summmary[0];
            }
        }

        if (max != 12345678) {
            System.out.println(max);
        } else {
            System.out.println(-1);
        }

    }
}

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

using namespace std;

int main(){
	string s, t;
	cin >> s >> t;
	int len = s.size();
	for (int i = len - 1; i >= 0; i--){
		string k = s.substr(i);
		string m = s.substr(0, i);
		k += m;
		if (k == t){
			puts("Yes");
			return 0;
		}
	}
	puts("No");
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<climits>
#include<vector>
#include<utility>//pair
#include<cmath>//abs,sqrt
#include<cstring>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define prin(arg) cout<<arg<<"\n"
#define prin2(arg1,arg2) cout<<arg1<<" "<<arg2<<"\n"
#define fill(arg,n) memset(arg,n,sizeof(arg))
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
const int INF=1e+9;
const ll INFLL=1e+17;
int n;
ll a[3010];
ll dp[3010][3010];
ll rec(int i,int j){
  if(dp[i][j]!=-INFLL) return dp[i][j];
  if(i==j){
    if((n+1)%2) return -a[i];
    else return a[i];
  }
  ll res;
  if((n-(j-i))%2){
    res=max(rec(i,j-1)+a[j],rec(i+1,j)+a[i]);
  }
  else{
    res=min(rec(i,j-1)-a[j],rec(i+1,j)-a[i]);
  }
  return dp[i][j]=res;
}


int main(){
  cin>>n;
  rep(i,n) cin>>a[i];
  rep(i,n){
    rep(j,n){
      dp[i][j]=-INFLL;
    }
  }
  ll res=rec(0,n-1);
  prin(res);

  


  return 0;
}


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

int main() {
	int n; cin >> n;
	vector<vector<int>> v(9, vector<int>(9, 0));
	for (int i = 1; i <= n; i++) {
		int j = i, x, y;
		x = j % 10;
		int d = 10;
		while (j / d > 0) d *= 10;
		d /= 10;
		y = j / d;
		if (x > 0) v.at(x - 1).at(y - 1)++;
	}
	int ans = 0;
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 9; j++) {
			ans += v.at(i).at(j) * v.at(j).at(i);
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <cstdio>

int main()
{
    // freopen("a.in", "r", stdin);

    char c, pre;
    for(int i = 0; i < 3; i++)
    {
        c = getchar();
        if(i && c != pre) return printf("Yes\n"), 0;
        pre = c;
    }
    printf("No\n");
    return 0;
}

Measured memory_bytes: 128
import java.util.Scanner;

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

	int count = 0;
	int flag = 0;
	int N = scan.nextInt();
	int[] A = new int[N];
	
	for(int i = 0; i < N; i++){
	    A[i] = scan.nextInt();
	}

	while(true){
	    for(int i = 0; i < N; i++){
		if(A[i] % 2 != 0){
		    flag = 1;
		}
	    }
	    if(flag == 1){
		break;
	    }else{
		for(int i = 0; i < N; i++){
		    A[i] /= 2;
		}
		count++;
	    }
	}

	System.out.println(count);
    }
}


Measured memory_bytes: 21972
#include <bits/stdc++.h>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define fio ios::sync_with_stdio(false);cin.tie(0);
#define db double
#define mk make_pair
const double Pi=3.14159265;
const int N = 2e5+10;
const int M = 2e5+10;
const ull base=163;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const db eps=1e-8;
const db pi=acos(-1);
using namespace std;
int a[N];
int main(){
    fio;
    int n;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
    }
    sort(a+1, a+1+n);
    int ans = 0;
    for(int i = 2; i <= n; ++i){
        if(a[i] != a[i-1] && i-1 == n-i+1){
            ans = a[i] - a[i-1];
        }
    }
    cout<<ans;
    return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int x,a,b;
  cin >>x>>a>>b;
  if(abs(x-a)>=abs(x-b))puts("B");
  else puts("A");

  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<math.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
#define N (1000000000+7)
#define INF (ll)1e16
typedef long long ll;

int main(void){
   int n;
   double ans=0.0,num=0.0;
   cin>>n;
   vector<double>a(n);
   for(int i=0;i<n;i++){
       cin>>a[i];
       num+=(double)1/a[i];
   }
   printf("%.9lf\n",(double)1/num);
   return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 

using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

bool eq(pair<ll,ll> a)
{
    return a.ff == a.ss;
}

int main()
{
  FAST/**/
   
   ll n;
   cin>>n;
   
   pii arr[n];
   rep(n)
        cin>>arr[i].ff>>arr[i].ss;
   
   bool pos = 0;
   
   for(ll i=0;i+2<n;i++)
   {
       if(eq(arr[i]) && eq(arr[i+1]) && eq(arr[i+2]))
            pos = 1;
   }
   
   if(pos)
        cout<<"Yes\n";
   else cout<<"No\n";        
   
   
   return 0;        
}  

  
          
   

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define INF 1000000000000000
#define fi first
#define se second
#define all(v) v.begin(),v.end()
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
struct edge{ int to, cost;};
typedef pair<int,int> P;
int a,b,t;
signed main(){
	cin>>a>>b>>t;
	cout<<t/a*b<<endl;
	return 0;
}


Measured memory_bytes: 256
//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define PI 3.14159265358979323846264338327950L
#define IOS            ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ms(a,b)         memset(a, b, sizeof(a))
#define filein      freopen("input.txt", "r", stdin);
#define fileout     freopen("output.txt", "w", stdout);
#define debug       cout<<"Hello, i am in here."<<endl;
#define bit_cnt(mask) __builtin_popcountll(mask)
#define ll long long int
#define dst(u,v,x,y) sqrt((x*1.0-u*1.0)*(x*1.0-u*1.0)+(y*1.0-v*1.0)*(y*1.0-v*1.0))
using namespace std;
const int MOD = (int)1e9+7;
void solve()
{
   int A,B,H,M;
   cin>>A>>B>>H>>M;
   long double theta=(long double)(11*M-60*H)/2.0;
   theta=theta*(PI/180.0);
   long double Result=(long double)(A*A+B*B)-(long double)(2*B*A)*cosl(theta);
   cout<<fixed<<setprecision(20)<<sqrt(Result)<<endl;
}
int  main()
{
    IOS
        solve();
    return 0;
}



Measured memory_bytes: 4108
#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int N=20005;

int a,b,t,cnt,ans;

int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>a>>b>>t;
    cnt=a;
    while (cnt<=t) {
        ans+=b;
        cnt+=a;
    }
    cout<<ans<<'\n';
}

Measured memory_bytes: 256
using System;

namespace atcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC138.RedOrNot();
        }
    }

    class ABC138
    {
        public static void RedOrNot()
        {
            int a = int.Parse(Console.ReadLine());
            string s = Console.ReadLine();

            if(a>=3200)
            {
                Console.WriteLine(s);
            }
            else
            {
                Console.WriteLine("red");
            }
        }
    }
}

Measured memory_bytes: 13140
#include <iostream>
#include <vector>
#include <cstdio>
#include <string>

using namespace std;

string sft[3] = {"lunch", "dinner", "midnight"};

int main()
{
  int n, s, t, u;

  while(cin>>n && n){
    vector<int> vec[3];
    while(n--){
      scanf("%d%*c%d%d", &s, &t, &u);

      if(t > u) u += 60;
      t = u - t;

      if(11 <= s && s <= 14){
	vec[0].push_back(t);
      } else if(18 <= s && s <= 20){
	vec[1].push_back(t);
      } else if(21 <= s || s < 2){
	vec[2].push_back(t);
      }
    }

    for(int i = 0; i < 3; i++){
      if(vec[i].size() == 0){
	cout << sft[i] << " no guest" << endl;
      } else {
	s = 0;
	for(int j = 0; j < vec[i].size(); j++)
	  if(vec[i][j] <= 8) s++;
	  
	cout << sft[i] << " " << (s*100/vec[i].size()) << endl;
      }
    }
  }

  return 0;
}

Measured memory_bytes: 0
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i,n) for(int i=0;i<(lint)(n);i++)
#define REP(i,n) for(int i=1;i<=(lint)(n);i++)
#define all(V) V.begin(),V.end()
#define stackReplaceBegin(size)\
	void* p=malloc(size);\
	esp_new=(lint)p+size-1;\
	void * stack_extend_memory_ = malloc(size);\
	void * stack_extend_origin_memory_;\
	char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);\
	*stack_extend_dummy_memory_ = 0;\
	asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
constexpr int INF = INT_MAX/2;
constexpr lint LINF = LLONG_MAX/2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
lint esp_new;
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
	if (lhs > rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
inline lint gcd(lint a, lint b) {
	while (b) {
		lint c = a;
		a = b; b = c % b;
	}
	return a;
}
inline lint lcm(lint a, lint b) {
	return a / gcd(a, b) * b;
}
bool isprime(lint n) {
	if (n == 1)return false;
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0)return false;
	}
	return true;
}
template<typename T>
T mypow(T a, unsigned int b) {
	T res(1),now(a);
	while(b){
		if(b&1)res*=now;
		b>>=1;
		now*=now;
	}
	return res;
}
template<typename T>
void printArray(std::vector<T>& vec) {
	rep(i, (int)vec.size() - 1)std::cout << vec[i] << " ";
	if(!vec.empty())std::cout << vec.back() << std::endl;
}
template<typename T>
void printArray(T l, T r) {
	T rprev = r;
	rprev--;
	for (T i = l; i != rprev; i++) {
		std::cout << *i << " ";
	}
	std::cout << *rprev << std::endl;
}
lint a,b;
int main() {
	std::cin>>a>>b;
	if(a%2){
		if(b%2){
			std::cout<<(a^((b-a)/2%2))<<std::endl;
		}
		else{
			std::cout<<(a^b^((b-a-1)/2%2))<<std::endl;
		}
	}
	else{
		if(b%2){
			std::cout<<(b-a+1)/2%2<<std::endl;
		}
		else{
			std::cout<<(b^((b-a)/2%2))<<std::endl;
		}
	}
	return 0;
}

Measured memory_bytes: 3620
import sys
from bisect import bisect_right, bisect_left

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    T = [i + 1 - A[i] for i in range(N)]
    idx = sorted(range(N), key=lambda k: T[k])
    T.sort()

    ans = 0
    for i in range(N):
        target = A[i] + i + 1
        left = bisect_left(T, target)
        if left > N - 1:
            continue
        if T[left] != target:
            continue
        else:
            right = bisect_right(T, target)
            ans += right - left
    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 36016
#include<bits/stdc++.h>
using namespace std;
#define il inline
#define ll long long
#define rg register
#define gc getchar()
#define rp(i,x,y) for(rg int i=x;i<=y;++i)

const int N=200000+10;
int n,q,fa[N];
ll mn[N<<1],as;
struct ed{int fr,to;ll wei;};
vector<ed>edge;

il int read()
{
    rg char ch=gc;rg int x=0;rg bool y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}
il ll readl()
{
    rg char ch=gc;rg ll x=0;rg bool y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}
il bool cmp(ed x,ed y){return x.wei<y.wei;}
il int fd(int x){return fa[x]==x?x:fa[x]=fd(fa[x]);}


int main()
{
    n=read();q=read();memset(mn,127/3,sizeof(mn));
    while(q--){int a=read()%n+1,b=read()%n+1;ll c=readl();mn[a]=min(mn[a],c+1);mn[b]=min(mn[b],c+2);edge.push_back((ed){a,b,c});}
    rp(i,1,n<<1)mn[i]=min(mn[i],mn[i-1]+2);rp(i,1,n)edge.push_back((ed){i,i%n+1,min(mn[i],mn[i+n])});sort(edge.begin(),edge.end(),cmp);
    rp(i,1,n)fa[i]=i;int sz=edge.size();rp(i,0,sz-1){int fafr=fd(edge[i].fr),fato=fd(edge[i].to);if(fafr^fato)as+=edge[i].wei,fa[fafr]=fato;}
    printf("%lld\n",as);
    return 0;
}

Measured memory_bytes: 14056
#include <iostream>

using namespace std;
int A, B, C, D;

int main() {
    while (cin >> A >> B >> C >> D) {
        int ans = min(B, D) - max(A, C);
        if (ans < 0)
            ans = 0;
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

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

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

ll N;

void dfs(vector<ll> v) {
    if (v.size() == N) {
        REP(i, N) {
            cout << v[i] + 1;
            if (i < N - 1)
                cout << " ";
        }
        cout << endl;
        return;
    }

    REP(i, N) {
        bool e = false;
        REP(j, v.size()) {
            if (v[j] == i)
                e = true;
        }
        if (e)
            continue;
        v.push_back(i);
        dfs(v);
        v.pop_back();
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    dfs(vector<ll>());
    return 0;
}


Measured memory_bytes: 3164
N = int(input())
Adj = [[0 for i in range(N)] for j in range(N)]

for i in range(N):
    u = list(map(int, input().split()))
    if u[1] > 0:
        for k in u[2: 2+u[1]]:
            Adj[u[0] - 1][k - 1] = 1
            
            
color = [0] * N #1: visited, 0: not visited, 2: completed
s = [0] * N # start
f = [0] * N # finish

time = 0

def DFS(u):
    global time
    color[u-1] = 1
    time += 1
    s[u-1] = time
    
    for i in range(1, N+1):
        if Adj[u-1][i-1] == 1 and color[i-1] == 0:
            DFS(i)
            
    color[u-1] = 2
    time += 1
    f[u-1] = time

for i in range(1, N+1):
    if color[i-1] == 0:
        DFS(i)
        
for i in range(N):
    print('{} {} {}'.format(i+1, s[i], f[i]))

Measured memory_bytes: 7828
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>


#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;


int main()
{ios_base::sync_with_stdio(false);
string s,t,u;
cin>>s>>t;
int a,b;
cin>>a>>b;
cin>>u;
if(s==u)a--;
else b--;
o2(a,b);
return 0;
}



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

using namespace std;

int main(void) {
    int n, k;
    cin >> n >> k;
    vector<long> a(n + 1, 1);
    for(int i = 1; i <= n; i++){
        long tmp;
        cin >> tmp;
        a.at(i) = tmp;
    }

    for(int i = k + 1; i <= n; i++){
        if(a.at(i) > a.at(i - k)){
            cout << "Yes" << endl;
        }
        else{
            cout << "No" << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 4824
//6.12
import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.util.Arrays;

class Main{
    long even[]=new long[1000];
    long gcd(long a,long b){
	return b==0?a:gcd(b,a%b);
    }
    void solve(int n,long oddmin){
	Arrays.sort(even,0,n);
	long ta=even[0];
	for(int i=0;i<n;i++){
	    ta=gcd(ta,even[i]);
	}
	long tx0=even[0]/ta,tx1=even[1]/ta;
	long g2=oddmin/tx0/tx1;
	long g=(long)(Math.sqrt(g2));

	//find A=A'/g
	long evenans=ta/g;
	//find all odd ans
	System.out.println(evenans);
	for(int i=0;i<n;i++){
	    if (i!=0)System.out.print(" ");
	    System.out.print(even[i]/evenans);
	}
	System.out.print("\n");
    }
    void run(){
	Scanner in = new Scanner(System.in);
	//System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
	int n;
	while(true){
	    n=in.nextInt();
	    if(n == 0)break;
	    int m=n*(n+1)/2;
	    int evenp=0;
	    long oddmin=9223372036854775807L;
	    for(int i=0;i<m;i++){
		long tmp;
		tmp=in.nextLong();
		if (tmp%2 == 0)even[evenp++]=tmp;
		else oddmin=(oddmin>tmp)?tmp:oddmin;
	    }
	    solve(n,oddmin);
	}
    }

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

Measured memory_bytes: 13000
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int X = sc.nextInt();
		sc.close();
		int first = (2 * K - 1)/2;
		for(int i = 0;i <= first * 2;i++){
			if(i == 0){
				System.out.print(X - first);
			}else{
				System.out.print(" " + (X - first + i));
			}
		}
	}
}

Measured memory_bytes: 23892
#include <bits/stdc++.h>
#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 all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define bit(n) (1<<(n))
#define ii(x) int x; cin >> (x)
#define ii2(x, y) int x, y; cin >> (x) >> (y)
#define ii3(x, y, z) int x, y, z; cin >> (x) >> (y) >> (z)
#define yn(x, ok, ng) cout << ((x)?(ok):(ng)) << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
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;}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
int const INF = 1001001001;


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

int n;
cin >> n;

vl a(n);
map<int,int> mp;
rep(i,n) {
    cin >> a[i];
    mp[a[i]]++;
}

ll sum = 0;
rep(i,n) {
    sum += a[i];
}

int Q;
cin >> Q;
rep(i,Q) {
    int b,c;
    cin >> b >> c;
    sum += (c-b)*mp[b];
    cout << sum << endl;
    mp[c] += mp[b];
    mp[b] = 0;
}

return 0;
}

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

int main(){
	ll n,k;
	cin >> n >> k;
	vector<ll> p(n);
	rep(i,n) cin >> p[i];
	sort(p.begin(),p.end());
	ll ans = 0;
	rep(i,k) ans += p[i];
	cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3608
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <string>
#define llint long long
#define inf 1e18

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

struct node{
	char c;
	vector<int> vec;
	node(){}
	node(char c){
		this->c = c;
	}
};

string s;
vector<node> tree;
int dp[200005][2];

int parseOR(int &p);

int parseTerm(int &p)
{
	if(s[p] == '('){
		p++;
		int ret = parseOR(p);
		p++;
		return ret;
	}
	else if(s[p] == '?'){
		tree.push_back(node('?'));
		p++;
		return tree.size()-1;
	}
}

int parseAND(int &p)
{
	tree.push_back(node('&'));
	int root = tree.size()-1;
	
	int res = parseTerm(p);
	tree[root].vec.push_back(res);
	
	while(1){
		if(s[p] == '&'){
			p++;
			int res = parseTerm(p);
			tree[root].vec.push_back(res);
		}
		else break;
	}
	return root;
}

int parseOR(int &p)
{
	tree.push_back(node('|'));
	int root = tree.size()-1;
	
	int res = parseAND(p);
	tree[root].vec.push_back(res);
	
	while(1){
		if(s[p] == '|'){
			p++;
			int res = parseAND(p);
			tree[root].vec.push_back(res);
		}
		else break;
	}
	return root;
}

void dfs(int v)
{
	//cout << v << endl;
	for(int i = 0; i < tree[v].vec.size(); i++) dfs(tree[v].vec[i]);
	
	dp[v][0] = dp[v][1] = 1e9;
	if(tree[v].c == '?') dp[v][0] = dp[v][1] = 1;
	if(tree[v].c == '|'){
		int sum = 0;
		for(int i = 0; i < tree[v].vec.size(); i++){
			int u = tree[v].vec[i];
			dp[v][1] = min(dp[v][1], sum + dp[u][1]);
			sum += dp[u][0];
		}
		dp[v][0] = sum;
	}
	if(tree[v].c == '&'){
		int sum = 0;
		for(int i = 0; i < tree[v].vec.size(); i++){
			int u = tree[v].vec[i];
			dp[v][0] = min(dp[v][0], sum + dp[u][0]);
			sum += dp[u][1];
		}
		dp[v][1] = sum;
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> s;
	s += "#";
	
	int p = 0;
	int root = parseOR(p);
	
	/*for(int i = 0; i < tree.size(); i++){
		for(int j = 0; j < tree[i].vec.size(); j++) cout << tree[i].vec[j] << " "; cout << endl;
	}*/
	
	dfs(root);
	cout << dp[root][0] << " " << dp[root][1] << endl;
	
	return 0;
}


Measured memory_bytes: 15656
#include <bits/stdc++.h>
#include <map>
#include <string.h>
#include <math.h>
#include <set>
#include <vector>
#include <algorithm>
#define rep(i, n) for(int i = 0;i<n;i++)
#define All(a) (a).begin(), (a).end()
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> a(n+1), b(n+1);
    rep(i,n){
      cin >> a[i+1];
    }
    for(int i = n; i >= 1; i--)
        for(int k = i; k >=1; k--){
            if(a[k] == k){
                b[i] = a[k];
                a.erase(a.begin() + k);
                break;
            }
            if(k == 1){
                cout << -1 << endl;
	            return 0;
            }
        }
    rep(i,n) cout << b[i+1] << endl;
}

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

bool is_integer( double x ){
  return floor(x)==x;
}

int main(){
  int n,d; cin>>n>>d;
  vector<vector<int>> x(n,vector<int>(d));
  rep(i,n){
    rep(j,d){
      int a; cin>>a;
      x[i][j]=a;
    }
  }
  int c=0;

  rep(i,n-1){
    for(int k=i+1;k<n;k++){
      int sum=0;
      // cout<<"i "<<i<<" k "<<k<<endl;
      rep(j,d){
        sum+=(x[i][j]-x[k][j])*(x[i][j]-x[k][j]);
      }
      double s=sqrt(sum);
      // cout<<s<<endl;
      bool che=is_integer(s);
      if(che)
      c++;
    }
  }
  cout<<c<<endl;
  return 0;
}


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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
using namespace std;
using ull = unsigned long long;
using ll = long long;
constexpr int I_INF = INT_MAX;
constexpr ll L_INF = LLONG_MAX;
const long double PI = (acos(-1));
const ull MOD = 1000000007;


struct __INIT {
    __INIT() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __init;

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i = 0; i < N; i++) cin >> A[i];
    map<int, int> cnt;
    int over = 0;
    for(int a : A) {
        bool exist = false;
        for (int i = 0; i <= 7; i++) {
            if(i * 400 <= a and a < (i + 1) * 400){
                cnt[i]++;
                exist = true;
                break;
            }
        }
        if(!exist) over++;
    }
    cout << max(1, (int)cnt.size()) << ' ';
    cout << (int)cnt.size() + over << endl;
}

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;
  cin >> n >> m;
  vector<int>syukudai(m);
  int sum=0;
  for(int i=0;i<m;i++){
    cin >> syukudai.at(i);
    sum += syukudai.at(i);
  }
  if(sum>n){
    cout << -1 << endl;
    return 0;
  }
  cout << n-sum << endl;
}


Measured memory_bytes: 3672
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  string S;
  cin >> S;
  int A = S.at(0) - '0';
  int B = S.at(1) - '0';
  int C = S.at(2) - '0';
  int D = S.at(3) - '0';
  
  if (A+B+C+D == 7){
    cout << A << '+' << B << '+' << C << '+' << D << '=' << 7 << endl;
  }
  else if (A+B+C-D == 7){
    cout << A << '+' << B << '+' << C << '-' << D << '=' << 7 << endl;
  }
  else if (A+B-C-D == 7){
    cout << A << '+' << B << '-' << C << '-' << D << '=' << 7 << endl;
  }
  else if (A+B-C+D == 7){
    cout << A << '+' << B << '-' << C << '+' << D << '=' << 7 << endl;
  }
  else if (A-B+C+D == 7){
    cout << A << '-' << B << '+' << C << '+' << D << '=' << 7 << endl;
  }
  else if (A-B+C-D == 7){
    cout << A << '-' << B << '+' << C << '-' << D << '=' << 7 << endl;
  }
  else if (A-B-C+D == 7){
    cout << A << '-' << B << '-' << C << '+' << D << '=' << 7 << endl;
  }
  else if (A-B-C-D == 7){
    cout << A << '-' << B << '-' << C << '-' << D << '=' << 7 << endl;
  }
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define RFOR(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define REP(i,n) FOR(i,0,(n))
#define REPS(i,n) FOR(i,1,(n)+1)
#define RREP(i,n) RFOR(i,0,(n))
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define DEBUG(x)  cout << #x << " = " << (x) << endl;
#define SORT(x) sort(ALL(x));
#define RSORT(x) sort(RALL(x));
#define SUM(x) accumulate(ALL(x),0);
#define FI first
#define SE second
#define PB push_back
#define EB emplace_back
#define SZ(x) (int)(x).size()
#define BN(x) ((1<<x)-1)
#define DUP(x,y) (((x)+(y)-1)/(y))
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const ll mod = 1e9+7;
const int INF = (1<<30)-1;
const ll INFLL = (1LL<<62)-1;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//cout << fixed << setprecision(10);

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

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

int main () {
  int N; cin >> N;
  vi a(N); REP(i,N) cin >> a[i];

  map<int,int> cnt;
  REP(i,N) {
    cnt[a[i]]++;
  }

  SORT(a);
  a.erase(unique(ALL(a)),a.end());

  int ans = 0;
  for (int X : a) {
    ans = max(ans, cnt[X-1]+cnt[X] +cnt[X+1]);
  }

  cout << ans << endl;
}

Measured memory_bytes: 5120
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class Main implements Runnable
{
//    static int dx[]={-1,0,1,0};
//    static int dy[]={0,-1,0,1};
    static long mod=1000000007;
    
    public static void main(String[] args) {        
        new Thread(null, new Main(), "", 200 * 1024 * 1024).start();        
    }

    public void run() {
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);

//        int n=sc.nextInt();
        char c[]=sc.getCharSet();
        int d[]=new int[2019];
        int t=1,s=0;
        long r=0;
        d[0]++;
        for (int i = c.length-1,k; -1 < i; i--) {
			k=c[i]-'0';
			s+=t*k;s%=2019;
			r+=d[s];
			d[s]++;
			t*=10;t%=2019;
		}
    
        out.println(r);
     	out.flush();
    }

    static void ret(String ans) {
    	System.out.println(ans);
    	System.exit(0);
    }

    static void ret(long ans) {
    	System.out.println(ans);
    	System.exit(0);
    }
    
    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
}

//class P {
//	int x,y; double d;
//	P(int  x, int y, double d) {
//	  this.x=x;
//	  this.y=y;
//	  this.d=d;
//	}
//}


//class P implements Comparable<P>{
//    int i,x,r,f;
//    P(int i,int x,int r,int f) {
//    	this.i=i;
//    	this.x=x;
//    	this.r=r;
//    	this.f=f;
//    }
//
//    public int compareTo(P p){
//    	if(-this.r+p.r==0) {
//    		return -this.x+p.x;
//    	}
//        return -this.r+p.r;
//    }
//}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString()
    {
        while( st==null || !st.hasMoreTokens() )
			try {
				st = new StringTokenizer(x.readLine());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        return st.nextToken();
    }
    public int nextInt()
    {
        return Integer.parseInt(nextString());
    }
    public int[] nextIntArray(int size) {
        int r[] = new int[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextInt(); 
        }
        return r;
    }
    public long[] nextLongArray(int size) {
        long r[] = new long[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextLong(); 
        }
        return r;
    }
    public char[] getCharSet() {
        return this.nextString().toCharArray();
    }    
    public long nextLong()
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}


Measured memory_bytes: 36700
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		String[] stringList = new String[n];
		for (int i = 0; i < n; i++) {
			stringList[i] = scan.next();
		}

		long count = 0;

		Map<String, Long> map = new HashMap<>();
		for (int i = 0; i < n; i++) {

			char[] c = stringList[i].toCharArray();
			Arrays.sort(c);
			String str = String.valueOf(c);
			if (map.containsKey(str)) {
				long num = map.get(str);
				count += num;
				map.put(str, num + 1);
			} else {
				map.put(str, 1l);
			}


		}

		System.out.println(count);
	}
}

Measured memory_bytes: 53396
def examA():
    S = SI()
    if S[2]==S[3] and S[4]==S[5]:
        print("Yes")
    else:
        print("No")
    return

def examB():
    X = I()
    ans = 0
    ans += (X//500) * 1000
    X %= 500
    ans += (X//5) * 5
    print(ans)
    return

def examC():
    K, N = LI()
    A = LI()
    L = [0]*N
    L[0] = K-A[-1] + A[0]
    for i in range(1,N):
        L[i] = A[i]-A[i-1]
    ans = sum(L) - max(L)
    #print(L)
    print(ans)
    return

def examD():
    def bfs(n, e, fordfs):
        # 点の数、スタートの点、有向グラフ
        W = [-1] * n
        # 各点の状態量、最短距離とか,見たかどうかとか
        W[e] = 0
        que = deque()
        que.append(e)
        while que:
            now = que.popleft()
            nowW = W[now]
            for ne in fordfs[now]:
                if W[ne] == -1:
                    W[ne] = nowW + 1
                    que.append(ne)
        return W
    N, X, Y = LI()
    V = [[]for _ in range(N)]
    for i in range(N-1):
        V[i].append(i+1)
        V[i+1].append(i)
    V[X-1].append(Y-1)
    V[Y-1].append(X-1)
    #print(V)
    ans = [0]*(N-1)
    for i in range(N):
        L = bfs(N,i,V)
        for l in L:
            if l==0:
                continue
            ans[l-1] += 1
    for v in ans:
        print(v//2)
    return

def examE():
    X, Y, A, B, C = LI()
    P = LI()
    Q = LI()
    R = LI()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    cnt = sum(P[:X]) + sum(Q[:Y])
    #print(cnt)
    X -= 1; Y -= 1
    for c in range(C):
        if X>=0 and Y>=0:
            if P[X] <= Q[Y]:
                if P[X] >= R[c]:
                    break
                cnt += (R[c] - P[X])
                X -= 1
            else:
                if Q[Y] >= R[c]:
                    break
                cnt += (R[c] - Q[Y])
                Y -= 1
        elif X>=0:
            if P[X] >= R[c]:
                break
            cnt += (R[c] - P[X])
            X -= 1
        elif Y>=0:
            if Q[Y] >= R[c]:
                break
            cnt += (R[c] - Q[Y])
            Y -= 1
    ans = cnt
    print(ans)
    return

def examF():
    N = I()
    V = [[]for _ in range(N)]

    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examE()

"""

"""

Measured memory_bytes: 98976
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define MOD 1000000007
#define INF (1<<30)
#define LINF (1LL<<60)
#define endl "\n"
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(int (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
typedef unsigned long long int ull;
typedef long long lint;
 
int main(void){
  int N,M,ans=0;
  cin >> N >> M;
  bool check[N][M]={};
  rep(i,N){
    int K;
    cin >> K;
    rep(j,K){
      int A;
      cin >> A;
      A--;
      check[i][A]=1;
    }
  }
  rep(i,M){
    int che=1;
    rep(j,N) if(check[j][i]==0) che=0;
    if(che==1) ans++;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
#if P == Q:
#  print(0)
#  exit()
a = []
for i in range(N):
  a.append(i + 1)
plist = list(itertools.permutations(a))
print(abs(plist.index(P) - plist.index(Q)))

Measured memory_bytes: 8052
# https://atcoder.jp/contests/abc042/tasks/arc058_a
# 最悪でも10**6探索する程度なのでNを一つづつ足して全探索でもよくね？


N, K = list(map(int, input().split()))
D = set(input().split())


def is_inD(n: int):
    tmp = set(str(n))
    return tmp.isdisjoint(D)


for i in range(N, N + 10 ** 6):
    if is_inD(i):
        print(i)
        exit()


Measured memory_bytes: 2940
#include<bits/stdc++.h>
#define up(l,r,i) for(int i=l;i<=r;i++)
#define dn(l,r,i) for(int i=l;i>=r;i--)
using namespace std;

typedef long long LL;
const int INF =2147483647;
int qread(){
    int w=1,c,ret;
    while((c=getchar())> '9'||c< '0')
    w=(c=='-'?-1:1); ret=c-'0';
    while((c=getchar())>='0'&&c<='9')
    ret=ret*10+c-'0';
    return ret*w;
}
int c; bool flgA,flgB;
int main(){
    while((c=getchar())>='A'&&c<='Z'){
        if(c=='C') flgA=true;
        if(c=='F'&&flgA) flgB=true;
    }
    puts(flgB?"Yes":"No");
    return 0;
}

Measured memory_bytes: 256
import java.util.*;

/**
 * A
 */

// bubble sort

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

        int n = sc.nextInt();
        int count=0;
        int array[] = new int[n];

        for(int i=0; i<n; i++) {
            array[i] = sc.nextInt();
        }

        sc.close();

        /**
        * 1 bubbleSort(A, N) // N 個の要素を含む 0-オリジンの配列 A
        * 2   flag = 1 // 逆の隣接要素が存在する
        * 3   while flag
        * 4     flag = 0
        * 5     for j が N-1 から 1 まで
        * 6       if A[j] < A[j-1]
        * 7         A[j] と A[j-1] を交換
        * 8         flag = 1
        */

        boolean flag = true;

        while(flag) {

            flag = false;
            
            for(int j=n-1; j>=1; j--) {

                if(array[j] < array[j-1]) {

                    int tmp = array[j];
                    array[j] = array[j-1];
                    array[j-1] = tmp;

                    count++;
                    flag = true;
                    
                }

            }

        }

        for(int i=0; i<n; i++) {
            
            System.out.print(array[i]);

            if(i != n-1) {
                System.out.print(" ");
            }

        }

        System.out.println();
        System.out.println(count);
    
    }
}


Measured memory_bytes: 26392
#include <bits/stdc++.h>
#define int long long
using namespace std;
int lcm(int num1, int num2) {
  int num3 = __gcd(num1, num2);
  //return num1 * num2 / num3;
  return ((num1 / num3) * (num2 / num3)) * num3;
}
signed main() {
  int n;
  cin >> n;
  int num1;
  cin >> num1;
  int num2;
  for (int i = 1; i < n; i++) {
    cin >> num2;
    num1 = lcm(num1, num2);
  }
  cout << num1 << endl;
  return 0;
}


Measured memory_bytes: 256
#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use itertools::Itertools;
use num::clamp;
use proconio::{input, marker::*, fastout};
use superslice::*;

#[fastout()]
fn main() {
    input! {
        n: usize,
        abc: [(Usize1, Usize1, i64); n-1],
        q: usize, k: Usize1,
        xy: [(Usize1, Usize1); q]
    }

    let mut edges = vec![Vec::new(); n];
    for (a, b, c) in abc {
        edges[a].push((b, c));
        edges[b].push((a, c));
    }

    let mut dists = vec![0; n];
    let mut que = Vec::new();
    que.push((k, k, 0));
    while let Some((now, parent, dist)) = que.pop() {
        for &(next, diff) in &edges[now] {
            if next == parent {
                continue;
            }
            que.push((next, now, dist + diff));
            dists[next] = dist + diff;
        }
    }

    for (x, y) in xy {
        let ans = dists[x] + dists[y];
        println!("{}", ans);
    }
}


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

#ifdef TESTING
  #include "dump.hpp"
#else
  #define DUMP(...)
  #define DEBUG(...)
#endif

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define SZ(a) int((a).size())
#define _REP(_1,_2,_3,_4,name,...) name
#define _REP4(i,b,e,s) for(decltype(e) _b=(b),_e=(e),i=_b+(0<(s)?0:(s));(0<(s)?i<_e:_e<=i);i+=(s))
#define _REP3(i,b,e) for(decltype(e) _b=(b),_e=(e),i=(_b<_e?_b:_b-1);(_b<_e?i<_e:_e<=i);(_b<_e?i++:i--))
#define _REP2(i,n) for(decltype(n) i=0,_n=(n);i<_n;i++)
#define _REP1(n) for(decltype(n) _i=0,_n=(n);_i<_n;_i++)
#define REP(...) _REP(__VA_ARGS__,_REP4,_REP3,_REP2,_REP1)(__VA_ARGS__)
#define FOR(it,c) for(auto&& it=begin(c);it!=end(c);it++)
#define ROF(it,c) for(auto&& it=rbegin(c);it!=rend(c);it++)
#define PB push_back
#define INT(n) int n;assert(0<scanf("%d",&n))
#define MSET(arr,val)  memset(arr,val,sizeof(arr))

int main(){
  map<int, vector<int>> gr;
  INT(n);
  REP(i, 1, n + 1){gr[i] = {};}
  REP(n - 1){INT(a); INT(b); gr[a].PB(b); gr[b].PB(a);}
  vector<ui> fen(n + 1, -1), snu(n + 1, -1); fen[1] = 0; snu[n] = 0;
  queue<int> q;
  q.push(1);
  while(!q.empty()){
    int u = q.front(); q.pop();
    for(int v:gr[u]){
      if (v == n){continue;}
      if (fen[u] + 1 < fen[v]){
        fen[v] = fen[u] + 1;
        if (fen[v] < (ui) (n + 1) / 2){
          q.push(v);
        }
      }
    }
  }
  q.push(n);
  while(!q.empty()){
    int u = q.front(); q.pop();
    for(int v: gr[u]){
      if (v == 1){continue;}
      if (snu[u] + 1 < snu[v]){
        snu[v] = snu[u] + 1;
        if (snu[v] < (ui) n / 2){
          q.push(v);
        }
      }
    }
  }
  int f = 0, s = 0;
  REP(i, 1, n + 1){
    if (fen[i] <= snu[i]){f++;}
    if (snu[i] < fen[i]){s++;}
  }
  if ((n + 1) / 2 <= f && s < n / 2){
    printf("Fennec\n");
  } else if (f < (n + 1) / 2 && n / 2 <= s){
    printf("Snuke\n");
  } else if (n % 2 == 1){
    printf("Fennec\n");
  } else {
    printf("Snuke\n");
  }
  return 0;
}


Measured memory_bytes: 11904