akhauriyash/Code-Regression huggingface dataset:
const MOD: i64 = 1000000007;

fn main() {
  let n: usize = read();
  let mut vect: Vec<i64> = read_vec();
  let mut veca: Vec<i64> = read_vec();
  
  if vect[n-1] != veca[0] {
    println!("0");
    return;
  }
  
  vect.insert(0,0);
  veca.push(0);
  let udt = vect.windows(2).map(|w| if w[0]==w[1] {(w[1],false)} else {(w[1],true)});
  let uda = veca.windows(2).map(|w| if w[0]==w[1] {(w[0],false)} else {(w[0],true)});
  let udt2 = vect.windows(2).map(|w| if w[0]==w[1] {(w[1],false)} else {(w[1],true)});
  let uda2 = veca.windows(2).map(|w| if w[0]==w[1] {(w[0],false)} else {(w[0],true)});

  let invalid = |(hp1,hp2)| {
    match (hp1, hp2) {
      ((h1, true), (h2, true)) => h1 != h2,
      ((h1, true), (h2, false)) => h1 > h2,
      ((h1, false), (h2, true)) => h1 < h2,
      _ => false
    }
  };

  if udt2.zip(uda2).any(invalid) {
    println!("0");
    return;
  }

  let ans =
    udt.
    zip(uda).
    filter_map(|((h1,p1),(h2,p2))| if p1 || p2 {None} else {Some(std::cmp::min(h1,h2))}).
    fold(1, |a,x| (a*x) % MOD);
  
  println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
  let mut buf = String::new();
  std::io::stdin().read_line(&mut buf).ok();
  buf.trim().parse::<T>().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
  let mut buf = String::new();
  std::io::stdin().read_line(&mut buf).ok();
  buf.trim().split_whitespace().map(|t| t.parse::<T>().ok().unwrap()).collect()
}

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

#define Int long long int
#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()

int main(){

    int n,m,x;
    cin >> n >> m >> x;

    int a;
    int l=0,r=0;
    REP(i,m){
        cin >> a;
        if(a<x)l++;
        else r++;
    }
    if(l<r)cout << l << endl;
    else cout << r << endl;

}



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

public class Main {

    private static FastReader in = new FastReader(System.in);
    private static PrintWriter out = new PrintWriter(System.out);

    private static final long MOD = (long) 1e9 + 7;

    public static void main(String[] args) {
        solve();
        out.flush();
    }

    private static void solve() {
        String s = in.next();
        int q = in.nextInt();

        StringBuilder sbPrefix = new StringBuilder();
        StringBuilder sbSuffix = new StringBuilder();

        boolean flipped = false;
        for (int i = 0; i < q; i++) {
            int type = in.nextInt();
            if (type == 1) {
                flipped = !flipped;
            } else {
                int f = in.nextInt();
                if (f == 1) {
                    if (!flipped) {
                        sbPrefix.append(in.next());
                    } else {
                        sbSuffix.append(in.next());
                    }
                } else {
                    if (!flipped) {
                        sbSuffix.append(in.next());
                    } else {
                        sbPrefix.append(in.next());
                    }
                }
            }
        }

        String ans;
        if (!flipped) {
            ans = sbPrefix.reverse().append(s).append(sbSuffix).toString();
        } else {
            ans = sbSuffix.reverse().append(new StringBuilder(s).reverse()).append(sbPrefix).toString();
        }

        out.println(ans);
    }

    // https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        FastReader(InputStream inputStream) {
            br = new BufferedReader(new
                    InputStreamReader(inputStream));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        // https://qiita.com/masakinpo/items/5f77f2a879578a85a0bb
        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }
}


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

const ll INF = pow(10, 9) + 7;

int main() {
  int n;
  cin >> n;
  double a=0,b;
  rep(i,n){
    double x;
    cin >>x;
    a += 1/x;
  }
  cout << 1/a << endl;
}

Measured memory_bytes: 3660
//header{{{
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/math/common_factor_rt.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
  
using namespace std;
//using namespace boost;
//using namespace multiprecision;  

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define Fixed fixed << setprecision(12)
#define int int64_t
using pii = pair<int,int>;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7; 
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A,B>;
  
inline int square(int a){ return a * a;}
inline int updiv(int a,int b){ return (a + b - 1) / b; }

template <class A, class B> inline int Max(const A &a, const B &b){ return (a < b ? b : a); }
template <class A, class B> inline int Min(const A &a, const B &b){ return (a > b ? b : a); }

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


signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int a, v, b, w, t;
    cin >> a >> v >> b >> w >> t;

    if(a < b){
        cout << (a + t * v >= b + t * w ? "YES\n" : "NO\n");
    }else{
        cout << (b - t * w >= a - t * v ? "YES\n" : "NO\n");
    }

    return (0);
}


Measured memory_bytes: 3640
N = int(input())
L = [int(num) for num in input().split()]
L.sort()
a = 0
b = 0
c = 0
ans = 0
for i in range(N):
    if N < 3:
        break
    a = L[i]
    for j in range(i+1,N):
        b = L[j]
        for k in range(j+1,N):
            c = L[k]
            if a != b and a != b and b != c:
                if c-b < a and a < b+c:
                    ans = ans + 1

print(ans)

Measured memory_bytes: 9040
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1);
const ll MOD = 1000000007;

int main() {
  string S; cin >> S;
  string SS = S;
  
  reverse(SS.begin(),SS.end());
  vector<string> T = {"maerd","remaerd","esare","resare"};
  
  
  while (SS.size() > 0) {
    bool can = false;
    for (int i = 0; i < 4; i++) {
      if (SS.size() < T[i].size()) continue;
      int n = T[i].size();
      bool can2 = true;
      for (int j = 0; j < n; j++) {
        if (SS[j] != T[i][j]) can2 = false;
      }
      if (can2) {
        can = true;
        SS = SS.substr(n);
        break;
      }
    }
    if (!can) break;
  }
  
  if (SS.size() == 0) cout << "YES" << endl;
  else cout << "NO" << endl;
}

Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
int main(){
  int N;
  cin>>N;
  int A[200000],B[1000001];
  for(int i=0;i<=1000000;i++){
    B[i]=0;
  }
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  sort(A,A+N);
  int k=N;
  for(int i=0;i<N;i++){
    if(A[i]>1000){
      if(i==0) k=0;
      else if(A[i-1]<=1000) k=i;
      for(int j=1;j*A[i]<=1000000;j++){
        B[j*A[i]]++;
      }
    }
    else{
      for(int j=1;j*A[i]<=1000;j++){
        B[j*A[i]]++;
      }
    }
  }
  int answer=0;
  for(int i=0;i<N;i++){
    if(i<=k-1){
      if(B[A[i]]<=1) answer++;
    }
    else{
      if(B[A[i]]<=1){
        int judge=0;
        for(int j=1;j<=1000;j++){
          if(B[j]!=0&&A[i]%j==0) judge++;
        }
        if(judge==0) answer++;
    }
  }
  }
  cout<<answer<<endl;
  return 0;
}

Measured memory_bytes: 7856
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

long long factorical(int n)
{
  long long ans=1,i;
  for(i=n;i>1;i--)
    ans=(ans*i)%MOD;
  return ans;
}

int main(void) {
  int i,j;
  ll n,m;
  cin>>n>>m;
  if(n>m)swap(n,m);
  if(m-n>=2){
    cout<<0<<endl;
  }else  if(n==m){
    ll ans=(2*factorical(n))%MOD;
    cout<<(ans*factorical(m))%MOD<<endl;
  }else{
    cout<<(factorical(n)*factorical(m))%MOD<<endl;
  }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T, class U> void assign(V<T>& v, int n, const U& a) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


int main() {
  cin.tie(nullptr); ios_base::sync_with_stdio(false);
  int n; cin >> n;
  int g = 0;
  while (n--) {
    int a, k; cin >> a >> k;
    auto f = [&](const auto& f, int x) -> int {
      if (x < k) return 0;
      int q = x / k, r = x - k * q;
      if (r == 0) return q;
      if (r < q + 1) return f(f, (lint) (k - 1) * x / k);
      return f(f, k * q + r % (q + 1));
    };
    g ^= f(f, a);
  }
  cout << (g ? "Takahashi" : "Aoki") << '\n';
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define lli	long long int
#define pb push_back
#define M 1000000007
#define pi 3.1415926535897932384626433832795028841971
#define rep(i,a,b)	for(i=a;i<b;i++)
using namespace std;
int main()
{
	char ch1,ch2,ch3;
	string s1,s2,s3,str;
    lli i,j,k,a,m,n,c,x,y,t,z,d,sum=0,ans=0,temp;
    lli v[3][3];
    rep(i,0,3){
    	for(j=0;j<3;j++)	cin >> v[i][j];
	}
    cin >> n;
    lli b[n],arr[3][3];
    for(i=0;i<3;i++){
    	for(j=0;j<3;j++)	arr[i][j]=0;
	}
    rep(i,0,n)	cin >> b[i];
    for(i=0;i<n;i++){
    	a=b[i];
    	for(j=0;j<3;j++){
    		for(k=0;k<3;k++){
    			if(v[j][k]==a){
    				arr[j][k]=1;
				}
			}
		}
	}
	if(arr[0][0]+arr[0][1]+arr[0][2]==3){
		cout << "Yes" << endl;return 0;
	}
	if(arr[1][0]+arr[1][1]+arr[1][2]==3){
		cout << "Yes" << endl;return 0;
	}
	if(arr[2][0]+arr[2][1]+arr[2][2]==3){
		cout << "Yes" << endl;return 0;
	}
	if(arr[0][0]+arr[1][0]+arr[2][0]==3){
		cout << "Yes" << endl;return 0;
	}
	if(arr[0][1]+arr[1][1]+arr[2][1]==3){
		cout << "Yes" << endl;return 0;
	}
	if(arr[0][2]+arr[1][2]+arr[2][2]==3){
		cout << "Yes" << endl;return 0;
	}
	if(arr[0][0]+arr[1][1]+arr[2][2]==3){
		cout << "Yes" << endl;return 0;
	}
	if(arr[0][2]+arr[1][1]+arr[2][0]==3){
		cout << "Yes" << endl;return 0;
	}
	cout << "No" << endl;
    
    
	return 0;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B_014_47
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            new Logic_A().Start();

        }

        public class Logic_A
        {
            public void Start()
            {
                var str = Console.ReadLine().Split(' ');
                var a = Convert.ToInt32(str[0]);
                var b = Convert.ToInt32(str[1]);
                var c = Convert.ToInt32(str[2]);

                Console.WriteLine((a == b + c || b == a + c || c == a + b) ? "Yes" : "No");
            }
        }
    }
}

Measured memory_bytes: 11220
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define OUT(s) cout << s << "\n";
using namespace std;

int S,W;
// https://atcoder.jp/contests/abc163
void Main()
{
    if (W >= S) {
        OUT("unsafe");
    } else {
        OUT("safe");
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    // cout << fixed << setprecision(15);
    cin >> S >> W;
    Main();
    return 0;
}

Measured memory_bytes: 3652
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <queue>
#include <sstream>
#include <cmath>
#include <functional>
#include <climits>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(s,i,n) for(int i=s;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define skip(i,n,p) for(int i=0;i<n;i+=p)
#define all(a)  (a).begin(),(a).end()
#define int64 long long
#define debug cerr << __FILE__ << ":" << __LINE__ << endl;
#define INF 2147483647

struct Init {
    Init() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout.precision(20);
        cout << fixed;
        // setw(6) << setfill('0')
    }
} init;

template <typename _Ty>
ostream& operator << (ostream& ostr, const vector<_Ty>& v) {
    for(int i = 0; i < (int) v.size(); i++) {
        ostr << v[i] << (i + 1 != v.size() ? " " : "");
    }
    return ostr;
}

template <typename _K, typename _V>
ostream& operator << (ostream& ostr, const map<_K, _V>& v) {
    ostr << "{";
    for (auto iter = v.begin(); iter != v.end(); ++iter) {
        ostr << (*iter).first << ": " << (*iter).second << ", ";
    }
    ostr << "}";
    return ostr;
}

template <typename _K>
ostream& operator << (ostream& ostr, const set<_K>& v) {
    for (auto iter = v.begin(); iter != v.end(); ++iter) {
        ostr << *iter << (iter != v.end() ? " " : "");
    }
    return ostr;
}

template <typename _K, typename _V>
ostream& operator << (ostream& ostr, const pair<_K, _V>& v) {
    ostr << "(" << v.first << ", " << v.second << ")";
    return ostr;
}

vector<string> split(const string &s, char delim) {
    vector<string> result;
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
    if (!item.empty()) {
            result.push_back(item);
        }
    }
    return result;
}

int64 powmod(int64 n, int64 count, int64 mod) {
    if (count == 0) return 1;
    if (count % 2 == 0) {
        int64 t = powmod(n, count / 2, mod);
        return t * t % mod;
    }
    return n * powmod(n, count - 1, mod) % mod;
}

int64 ncr(int64 n, int64 r, int64 mod) {
    int64 a;
    if (n < r) a = min(n, r - n);
    else a = min(n - r, r);

    int64 res = 1;
    rep(i, a) {
        res = res * (n - i) % mod;
        res = res * powmod(i + 1, mod - 2, mod) % mod;
    }

    return res;
}

int main() {
    int x, y; cin >> x >> y;
    cout << max(0, x - y) << endl;
}


Measured memory_bytes: 256
#include <stdio.h>

int main()
{
    int n, m, a[100][100], b[100], c[100] = {0};
    scanf("%d %d", &n, &m);
    
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < m; i++)
    {
        scanf("%d", &b[i]);
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            c[i] += a[i][j] * b[j];
        }
    }

    for (int i = 0; i < n; i++)
    {
        printf("%d\n", c[i]);
    }

    return 0;
}


Measured memory_bytes: 2104
#include "bits/stdc++.h"
using namespace std;


/* macro */
#define rep(i,a,b) for(int i=a;i<b;i++)
#define revrep(i,a,b) for(int i = a; i > b; i--)
#define int long long
#define exist(s,e) ((s).find(e)!=(s).end())
#define all(v) (v).begin(), (v).end()
#define each(s,itr) for(auto (itr) = s.begin(); (itr) != s.end(); (itr)++)
#define sum(v) accumulate(all(v), (0LL))
#define sec second
#define fir first
#define pb push_back



/* alias */
template<class T> using vec = vector<T>;
typedef vector<int> vi;
typedef pair<int, int> pi;


/* constant */
//const int inf = pow(2, 62);
//const int mod = 1e9 + 7;
const int mod = 100000;
int dx[8]={1,0,-1,0,-1,1,-1,1};
int dy[8]={0,1,0,-1,-1,-1,1,1};



/* io_method */
int input(){int tmp;cin >> tmp;return tmp;}
string raw_input(){string tmp;cin >> tmp;return tmp;}
string readline(){string s;getline(cin, s);return s;}
template<class T> void printx(T n){cout << n;}
template<class T, class U> void printx(pair<T, U> p){cout << "(" << p.first << "," << p.second << ")";}
template<class T> void printx(vector<T> v){cout << "{";rep(i,0,v.size()){printx(v[i]);if(i != v.size()-1)printx(",");}cout << "}";}
template<class T> void print(T n){printx(n);cout << endl;}
template<class T> void print(set<T> s){cout << "{";each(s, e){if(e != s.begin()) printx(",");printx(*e);}cout << "}" << endl;}
template<class T, class U> void print(map<T, U> mp){cout << "{";each(mp, e){cout << "(" << e -> first << "," << e -> second << ")";}cout << "}" << endl;}


/* general_method */
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 (b<a) { a=b; return 1; } return 0; }




/* main */



signed main(){

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

    
    int a; char op; int b;

    cin >> a >> op >> b;

    if(op == '+'){
        print(a + b);
    }else{
        print(a - b);
    }


    
    



    
    

    







   




    

    

    




    



    



    

 
    
    


    

    
   
    













}






    










    




    










Measured memory_bytes: 256
#include <stdio.h>

int main() {
  int h, a;
  scanf("%d", &h);
  scanf("%d", &a);
  if(h % a == 0) {
      printf("%d\n", (h / a));
  } else {
      printf("%d\n", (h / a) + 1);
  }
  return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main(){
  int A[3][3];
  int N;
  int b;
  for(int i=0;i<3;i++)
    for(int j=0;j<3;j++)
      cin>>A[i][j];
  cin>>N;
  string ans="No";
  for(int k=0;k<N;k++){
    cin>>b;
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        if(A[i][j]==b)
          A[i][j]=0;
    if(A[0][0]+A[0][1]+A[0][2]==0||A[1][0]+A[1][1]+A[1][2]==0||A[2][0]+A[2][1]+A[2][2]==0||A[0][0]+A[1][0]+A[2][0]==0||A[0][1]+A[1][1]+A[2][1]==0||A[0][2]+A[1][2]+A[2][2]==0||A[0][0]+A[1][1]+A[2][2]==0||A[0][2]+A[1][1]+A[2][0]==0)
      ans="Yes";
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s, t;
    cin >> s;
    cin >> t;
    long long l = s.length(), lt = t.length();
    if (!(l + 1 == lt) || l > 10)
    {
        cout << "NO" << endl;
        return 0;
    }
    for (long long x = 0; x < l; x++)
    {
        if (s[x] != t[x] || !(s[x] >= 'a' && s[x] <= 'z'))
        {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 3624
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <climits>
using namespace  std;

struct StCst
{
  int m_nNo;
  int m_nInSeq;
  bool m_bDone;

  static bool AsdNo(const StCst& cnroLeft, const StCst& cnroRight) { return cnroLeft.m_nNo < cnroRight.m_nNo; }
};

void fnInput(int &rnMaxSiz, vector<StCst> &rvoSort, int &rnMinNo)
{
  int nCnt = 0;
  cin >> rnMaxSiz;

  rvoSort.resize(rnMaxSiz);
  for (int i = 0; i < rnMaxSiz; i++)
  {
    cin >> rvoSort[i].m_nNo;
    rnMinNo = min(rnMinNo, rvoSort[i].m_nNo);
    rvoSort[i].m_nInSeq = nCnt++;
  }
}

void fnMinCostSort(vector<StCst> &rvoSort, int nMinNo, int &rnSwpCnt)
{
  sort(begin(rvoSort), end(rvoSort), StCst::AsdNo);

  int nCost;
  for (int i = 0; i < rvoSort.size(); i++)
  {
    if (rvoSort[i].m_bDone)  continue;

    int nTotal = rvoSort[i].m_nNo;
    int nCurSize = 1;
    int nCurMin = rvoSort[i].m_nNo; 
    rvoSort[i].m_bDone = true;

    int j = rvoSort[i].m_nInSeq;
    while  (rvoSort[j].m_nNo != rvoSort[i].m_nNo)
    {
      nTotal += rvoSort[j].m_nNo;
      nCurSize++;
      nCurMin = min(nCurMin, rvoSort[j].m_nNo);
      rvoSort[j].m_bDone = true;
      j = rvoSort[j].m_nInSeq;
    }

    if (nCurSize == 1)
        nCost = 0;
    else
      if (nMinNo == nCurMin)
        nCost = nTotal + (nCurSize - 2) * nCurMin;
      else
        nCost = min(nTotal + (nCurSize - 2) * nCurMin, 
                    nTotal + nCurMin + (nCurSize + 1) * nMinNo);
    rnSwpCnt += nCost;
  }
}

void fnResult(int nSwpCnt)
{
  cout << nSwpCnt << endl;
}

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

  int nMaxSiz;
  vector<StCst> voSort;
  int nMinNo = INT_MAX;

  fnInput(nMaxSiz, voSort, nMinNo);

  int nSwpCnt = 0;
  fnMinCostSort(voSort, nMinNo, nSwpCnt);

  fnResult(nSwpCnt);
  return 0;
}

Measured memory_bytes: 1228
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,k;
    long long ans=0;
    cin>>n;
    for( i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            for(k=1;k<=n;k++)
            ans+=__gcd(i,__gcd(j,k));
    cout<<ans<<endl;
}




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

using namespace std;

bool o(string s){
  string c=s;
  reverse(begin(c),end(c));
  return c==s;
}

main() {
  string s;
  cin>>s;
  string t="",c="";
  for(int i=1; i<=(s.size()-1)/2; ++i) t += s[i-1];
  for(int i=(s.size()+3)/2; i<=s.size();++i) c+=s[i-1];
  if(o(s)&&o(t)&&o(c)) return cout<<"Yes",0;
  else cout<<"No";
}


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

int main(){
    int n;
    cin >> n;
    long long sum=0;
    for(int i=1;i<=n;++i){
        if(i%3!=0 && i%5!=0){
            sum += i;
        }
    }
    cout << sum << endl;

    return 0;
}


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

int main() {
    int n; cin >> n;
    int sum = 0, add = 1;
    while (sum < n) {
        sum += add++;
    }
    int exception = sum - n;
    for (int i = 1; i < add; i++) if (i != exception) {
        cout << i << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

const int MAX_H = 410;
const int MAX_W = 410;

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

bool used[MAX_H][MAX_W];
int h, w;
vector<string> s;

bool isOutOfRange(int h, int w, int H, int W) {
    return h < 0 || h >= H || w < 0 || w >= W;
};


pll calc(int y, int x) {
    used[y][x] = true;
    pll ret = pll(0LL, 0LL);
    if(s[y][x] == '#') ret.first++;
    else ret.second++;
    rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if(isOutOfRange(nx, ny, w, h) || s[y][x] == s[ny][nx] || used[ny][nx]) continue;
        auto p = calc(ny, nx);
        ret.fi += p.fi;
        ret.se += p.se;
    }
    return ret;
}

int main(){
    cin >> h >> w;
    s.resize(h);
    rep(i, h) cin >> s[i];
    ll ans = 0;
    rep(i, h) rep(j, w) {
        if(!used[i][j]) {
            auto p = calc(i, j);
            ans += p.fi * p.se;
            // cout << i << " " << j << " " << p.fi << " " << p.se << endl;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 18176
#include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(int v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define LLI long long int
#define fst first
#define snd second
#ifdef DEBUG
#include <boost/core/demangle.hpp>
#define dump(x) cerr << "L" << __LINE__ << ": in " << __PRETTY_FUNCTION__ << " \e[32;1m" << boost::core::demangle(typeid(x).name()) << "\e[37m" << " " << (#x) << " = " << (x) << "\e[m" << endl;
#else
#define dump(x)
#endif
using namespace std;
template <typename T> using V = vector<T>;
template <typename T, typename U> using P = pair<T,U>;
template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;}

template <typename T> ostream& operator<<(ostream &os, vector<T> &v){os << "{";ITR(i,v){if(i!=v.begin())os << ","; os << *i;}os << "}"; return os;}
template <typename T, typename U> ostream& operator<<(ostream& os, pair<T,U> &p){os << "(" << p.first << "," << p.second << ")"; return os;}

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);}

const LLI mod = 1e9+7;

LLI dp[101000][2];
bool visit[101000][2];

LLI rec(vector<vector<int>> &tree, int col, int cur, int par){
  if(visit[cur][col]) return dp[cur][col];
  visit[cur][col] = true;

  dp[cur][col] = 1;
  if(col){ //black
    for(auto next : tree[cur]){
      if(next == par) continue;
      (dp[cur][1] *= rec(tree, 0, next, cur)) %= mod;
    }
  }else{ //white
    for(auto next : tree[cur]){
      if(next == par) continue;
      (dp[cur][0] *= (rec(tree, 0, next, cur) + rec(tree, 1, next, cur)) % mod) %= mod;
    }
  }
  
  return dp[cur][col];
}

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

  int N; cin >> N;
  vector<vector<int>> tree(N);
  REP(i,N-1){
    int x,y; cin >> x >> y;
    --x, --y;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }

  LLI ans = (rec(tree, 0, 0, -1) + rec(tree, 1, 0, -1)) % mod;

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 15872
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    rep(i,n) cin>>a[i];
    int max= *max_element(a.begin(),a.end());
    int cnt=0;
    rep(i,n) if(a[i]==max) cnt++;
    rep(i,n){
        int b=a[i];
        if (b==max &&cnt==1) {
            a[i]=0;
            cout <<*max_element(a.begin(),a.end())<<endl;
            a[i]=max;
        }else{
            cout << max<<endl;
        }
    }
}

Measured memory_bytes: 2432
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
	
	static int[] values;
	static int N;
	static int[] digit, exp;
	static int max = 0;
	static int base;
	
	
	static boolean can() {
//		System.err.println(base);
//		System.err.println("===");
		digit = new int[N];
		exp = new int[N];
		max = -1;
		for (int i = 1; i < values.length; i++) {
			if (values[i] <= values[i-1]) {
				if (!doIt(values[i] - 1)) return false;
//				System.err.print(i + ": " + values[i] + " =>");
//				for (int j = 0; j <= max; j++) {
//					System.err.print(" " + digit[j] + " (" + exp[j] + ")");
//				}
//				System.err.println();
			}
		}
		return true;
	}
	
	static boolean doIt(int val) {
		int left = -1, right = max + 1;
		while (left + 1 < right) {
			int med = (left + right) / 2;
			if (exp[med] < val) left = med;
			else right = med;
		}
		if (right == max + 1) {
			max++;
			digit[max] = 0;
			exp[max] = val;
		}
		if (exp[right] > val) {
			digit[right] = 0;
			exp[right] = val;
		}
		max = right;
		return increment();
	}
	static boolean increment() {
		digit[max]++;
		if (digit[max] == base) {
//			System.err.println(max + " es " + exp[max]);
			if (exp[max] == 0) return false;
			if (max > 0 && exp[max - 1] == exp[max] - 1) {
				max--;
				return increment();
			} else {
				exp[max]--;
				digit[max] = 1;
			}
		}
		return true;
	}
	
	public static void main(String[] args) throws IOException {
		MyScanner sc = new MyScanner();
		PrintWriter out = new PrintWriter(System.out);
		N = sc.nextInt();
		values = new int[N];
		boolean one = true;
		for (int i = 0; i < N; i++) {
			values[i] = sc.nextInt();
			if (i > 0 && values[i] <= values[i-1]) one = false;
		}
		if (one) {
			out.println(1);
			out.flush();
			return;
		}
		int left = 1, right = N;
		while (left + 1 < right) {
			base = (left + right) / 2;
			if (can()) right = base;
			else left = base;
		}
		out.println(right);
		
		out.flush();
	}
	
	static class MyScanner {
		private BufferedReader br;
		private StringTokenizer tokenizer;
		
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		
		public int nextInt() {
			return Integer.parseInt(next());
		}
		
		public long nextLong() {
			return Long.parseLong(next());
		}
	}
}


Measured memory_bytes: 55076
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

template<class T, class Compare = greater<T> >
using MinHeap = priority_queue<T, vector<T>, Compare>;
template<class T, class Compare = less<T> >
using MaxHeap = priority_queue<T, vector<T>, Compare>;
using llong = long long;

int s, e, n, w;
string str;

int main() {
    cin >> str;

    for (int i = 0; i < str.size(); i++) {
        if (str[i] == 'N')n++;
        if (str[i] == 'S')s++;
        if (str[i] == 'E')e++;
        if (str[i] == 'W')w++;
    }

    if ((n > 0 && s == 0) ||
        (s > 0 && n == 0) ||
        (e > 0 && w == 0) ||
        (w > 0 && e == 0)) {
        cout << "No" << endl;
    }
    else {
        cout << "Yes" << endl;
    }

    return 0;
}


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

int main() {
    string str;

    
    while(1) {
        int sum = 0;
        cin >> str;
        if (str == "0") break;
        
        for (int i = 0; i < str.size(); i++) {
            if (str[i] == '1') sum += 1;
            if (str[i] == '2') sum += 2;
            if (str[i] == '3') sum += 3;
            if (str[i] == '4') sum += 4;
            if (str[i] == '5') sum += 5;
            if (str[i] == '6') sum += 6;
            if (str[i] == '7') sum += 7;
            if (str[i] == '8') sum += 8;
            if (str[i] == '9') sum += 9;
            if (str[i] == '0') sum += 0;
        }
        printf("%d\n", sum);
    }
    return 0;
}


Measured memory_bytes: 3264
def modelAnswer(N:int,A:list) -> int:
   T1 = [A[0]]
   T2 = [sum(A)]
   for i in range(1,N):
      T1.append(T1[-1]+A[i])
      T2.append(T2[0] - T1[i-1])
   # print(T1,T2)
   ans = 10**10
   for i in range(len(T1)-1):
      # print(abs(T1[i]-T2[i+1]))
      ans = min(ans,abs(T1[i]-T2[i+1]))
   return ans


def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(modelAnswer(N,A[:]))
if __name__ == '__main__':
   main()


Measured memory_bytes: 34156
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.size = [1] * n
        self.rank = [0] * n
        self.edge = [0] * n

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def is_saturated(self, x):
        x = self.find(x)
        return self.edge[x] == self.size[x]

    def get_size(self, x):
        return self.size[self.find(x)]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        self.edge[x] += 1
        if x == y:
            return

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
            self.edge[y] += self.edge[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            self.edge[x] += self.edge[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

n, h, w = [int(item) for item in input().split()]

UF = UnionFind(h+w)
arc = []
for i in range(n):
    r, c, a = [int(item) for item in input().split()]
    r -= 1; c -= 1
    c += h
    arc.append([a, r, c])
arc.sort(reverse=True)

ans = 0
for a, r, c in arc:
    if not UF.is_saturated(r) or not UF.is_saturated(c):
        UF.union(r, c)
        ans += a

print(ans)

Measured memory_bytes: 35908
#include <bits/stdc++.h>
 
#define int         long long
#define uint        unsigned int
#define ld          long double
#define showoff     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb          push_back
#define pii         pair<int,int>
#define FOR(i,a,b)  for(int i=a;i<b;++i)
#define RFOR(i,a,b) for(int i=a;i>b;--i)
#define f           first
#define se          second
#define maxn        400005
#define all(v)      v.begin(),v.end()
#define sz(x)       (int)x.size()
#define mod         1000000007
#define pqueue      priority_queue<int>
#define pdqueue     priority_queue< int,vector<int> ,greater< int >>
 
using namespace std;
 
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
 
int power(int a,int n,int p)
{
    a %= p;
    if(n == 1)return a;
    if(n == 0)return 1;
    if(n%2)return (a*(power((a*a)%p,n/2,p)%p))%p;
    return power((a*a)%p,n/2,p)%p;
}
const int inf = (int) 1e18;
 
int inverse(int x){
    return power(x,mod-2,mod)%mod;//little fermat....
}

int fact[maxn];

int ncr(int n,int r){
    int num = fact[n];
    int den = (fact[n-r]*fact[r])%mod;
    num *= inverse(den);
    num %= mod;
    return num;
}

void solve(){
    fact[0] = 1;
    FOR(i,1,maxn)fact[i] = (fact[i-1]*i)%mod;
    int n,k;
    cin >> n >> k;
    int ans = 0;
    FOR(i,0,min(k+1,n)){
        ans += (ncr(n,i)*ncr(n-1,i))%mod;
        ans %= mod;
    }
    cout << ans << "\n";
}

signed main()   
{
    showoff;
    int T = 1;
    //cin >> T;
    FOR(t,1,T+1){
        solve();
    }
    return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...


Measured memory_bytes: 3328
#include <bits/stdc++.h>
typedef long long ll;
#define INF 1000000000
#define MOD 1000000007
int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};
using namespace std;
int main(void){
   int d,n;
   cin>>d>>n;
   int ans;
   int val;
   if(d==0){
      val=1;
   }else if(d==1){
      val=100;
   }else{
      val=10000;
   }
   ans=val*n;
   if(n==100)ans+=val;
   cout<<ans<<endl;
   return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
 
int main (void){
  int X , A ;
  
  scanf("%d %d",&X,&A);
  
  if (X < A){
    X = 0;
    printf("%d",X);
  }
  else{
    X = 10;
    printf("%d",X);
  }
  
  return 0;
}


Measured memory_bytes: 128
#include <stdio.h>
#include <string.h>

#define S_MAX 100001
#define T_MAX 100001

int b_search(int arr[], int num, int left, int right){
  if(left == right) return arr[left];
  int mid = (left+right)/2;
  if(arr[mid] > num) return b_search(arr, num, left, mid);
  else return b_search(arr, num, mid+1, right);
}

int main(void){
  int i, j, k;

  char s[S_MAX], t[T_MAX];
  scanf(" %s", s);
  scanf(" %s", t);

  int s_len = strlen(s);

  int alp[26][S_MAX] = {}, alp_num[26] = {}, c_num;
  for(i = 0; i < s_len; i++){
    c_num = s[i]-'a';
    alp[c_num][alp_num[c_num]] = i+1;
    alp_num[c_num]++;
  }

  for(i = 0; i < 26; i++){
    if(alp[i][0] == 0) continue;
    alp[i][alp_num[i]] = alp[i][0]+s_len;
  }

  long ans = 0;
  int prev_pos = 0, next_pos = 0;
  for(i = 0; i < strlen(t); i++){
    c_num = t[i]-'a';
    if(alp[c_num][0] == 0){
      printf("-1\n");
      return 0;
    }
    next_pos = b_search(alp[c_num], prev_pos, 0, alp_num[c_num]);
    ans += next_pos - prev_pos;
    prev_pos = next_pos%s_len;
  }

  printf("%ld\n", ans);

  return 0;
}


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

int main()
{
	long long N, x, res = 0;
	cin >> N >> x;
	vector<int> a(N);
	for (int i = 0; i < N; i++) {
		cin >> a[i];
		if (i >= 1 && a[i - 1] + a[i] > x) {
			res += a[i - 1] + a[i] - x;
			if (a[i - 1] > x) {
				a[i] = 0;
				a[i - 1] = x;
			}
			else {
				a[i] = x - a[i - 1];
			}
		}
	}
	cout << res << endl;
	return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  string s;
  ll k;
  cin >> s >> k;
  ll n = s.size();
  set<string> st;
  rep(i, n) rep2(j, i + 1, min(n + 1, i + 6)) st.insert(s.substr(i, j - i));
  for(auto v : st) {
    if(k > 1) {
      k--;
      continue;
    }
    cout << v << endl;
    return 0;
  }
}

Measured memory_bytes: 1664
#include <bits/stdc++.h>
#define REP(i, n) for(int i=0; i<(int)(n); ++i)
#define FOR(i, s, n) for(int i=(int)(s); i<(int)(n); ++i)
#define ALL(n) (n).begin(), (n).end()
#define pb push_back

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> mat;

const ll MAX_V = 100000;
const ll INF = 1e9;
const ll MOD = 998244353;

int main(){
    int N, K; cin >> N >> K;
    vl V(N);
    ll ans = 0;
    REP(i, N) cin >> V[i];
    REP(l, min(N, K)+1){
        REP(r, min(N, K)-l+1){
            vl have;
            ll sum = 0;
            REP(i, l) have.pb(V[i]);
            REP(i, r) have.pb(V[V.size()-1-i]);
            sort(ALL(have));
            REP(i, have.size()) sum += have[i];
            ans = max(ans, sum);
            REP(i, min(K-l-r, (int)have.size())){
                if (have[i] < 0){
                    sum -= have[i];
                }
                ans = max(ans, sum);
            }
        }
    }
    cout << ans << endl;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
using P = pair<int,int>;
int N, A[3][3], b[10];
bool apper[3][3];

int main(){
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            cin >> A[i][j];
        }
    }
    cin >> N;
    for(int i = 0; i < N; i++) cin >> b[i];
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            apper[i][j] = false;
            for(int k = 0; k < N; k++){
                if(A[i][j] == b[k]) apper[i][j] = true;
            }
        }
    }

    string ANS = "No";
    for(int i = 0; i < 3; i++){
        if(apper[i][0] && apper[i][1] && apper[i][2]) ANS = "Yes";
    }
    for(int i = 0; i < 3; i++){
        if(apper[0][i] && apper[1][i] && apper[2][i]) ANS = "Yes";
    }
    if(apper[0][0] && apper[1][1] && apper[2][2]) ANS = "Yes";
    if(apper[0][2] && apper[1][1] && apper[2][0]) ANS = "Yes";
    cout << ANS << endl;
    return 0;
}

Measured memory_bytes: 3588
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 998244353;

#define MAX_N 2000005
ll inv[MAX_N], fac[MAX_N], finv[MAX_N];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for(int i=2; i<MAX_N; i++) {
    inv[i] = mod - inv[mod%i] * (mod/i) % mod;
    fac[i] = fac[i-1] * (ll)i % mod;
    finv[i] = finv[i-1] * inv[i] % mod;
  }
}
ll perm(ll n, ll r) {
  if(n < 0)       return 0;
  else if(n < r)  return 0;
  else            return (fac[n] * finv[n-r]) % mod;
}
ll comb(ll n, ll r) {
  ll ans = 1;
  ans = perm(n, r);
  ans = (ans * finv[r]) % mod;
  return ans;
}
ll modpow(ll a, ll N) {
  ll ans = 1;
  ll tmp = a;
  while(N > 0) {
    if(N % 2 == 1) (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    N /= 2;
  }
  return ans;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  make();
  int n, m, k; cin >> n >> m >> k;
  ll ans = 0;
  rep(i, k+1) {
    (ans += comb(n-1, i) * m % mod * modpow(m-1, n-1-i) % mod) %= mod;
  }
  cout << ans << "\n";
  return 0;
}


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

using namespace std;

int data[100], n, k;

main(){
  while(cin >> n >> k && (n|k)){
    for(int i=0;i<n;i++){
      cin >> data[i];
    }
    int ck = 0;
    for(int i=0;i+k<=n;i++){
      int tmp = 1;
      for(int j=0;j<k;j++){
        tmp *= data[i+j];
      }
      ck = max(ck, tmp);
    }
    int ans = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        int tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
        for(int i2=0;i2+k<=n;i2++){
          int t = 1;
          for(int j2=0;j2<k;j2++){
            t *= data[i2+j2];
          }
          ans = max(ans, t);
        }
        data[j] = data[i];
        data[i] = tmp;
      }
    }
    if(ans < ck) cout << "NO GAME" << endl;
    else cout << ans-ck << endl;
  }
}

Measured memory_bytes: 0
#include<map>
#include<cmath>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,a,b; 
int main(){
    scanf("%d%d%d",&n,&a,&b);
    n-=a;
    printf("%d\n",n%b);
	return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        int total = 0;
        if(a > b){
            total += a;
            a--;
        }else{
            total += b;
            b--;
        }
        
        if(a > b){
            total += a;
        }else{
            total += b;
        }
        
        System.out.println(total);
        
    }
}


Measured memory_bytes: 21972
#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>

#define MAXN 100000

using namespace std;

struct {
  int x,y;
} typedef SWAP;

int P[MAXN+1];
bool F[MAXN+1];
int PNO[MAXN+1];
int SNO[MAXN+1];
vector<int> V[MAXN+1];

int N,M;

void bfs ( int cnt , int root )
{
  queue<int> Q;

  Q.push(root);
  SNO[root] = cnt;
  PNO[P[root]] = cnt;
  F[root] = true;

  while ( !Q.empty() ) {
    int now = Q.front(); Q.pop();
    for ( int i = 0; i < V[now].size(); ++i ) {
      if ( !F[V[now][i]] ) {
        Q.push(V[now][i]);
        F[V[now][i]] = true;
        SNO[V[now][i]] = cnt;
        PNO[P[V[now][i]]] = cnt;
      }
    }
  }

}


void func ( void ) {
  int cnt = 0;

  for ( int i = 1; i <= N; ++i ) {
    if (!F[i] ) {
      bfs(cnt, i);
      ++cnt;
    }
  }
}


int main()
{
  int sum = 0;
  cin >> N >> M;

  for ( int i = 1; i <= N; ++i ) scanf("%d", &P[i]);
  for ( int i = 1; i <= M; ++i ) {
    int tmp1, tmp2;
    scanf("%d%d", &tmp1, &tmp2);
    V[tmp1].push_back(tmp2);
    V[tmp2].push_back(tmp1);
  }

  func();

  //for ( int i = 1; i <= N; ++i ) cout << PNO[i] << " "; cout << endl;
  //for ( int i = 1; i <= N; ++i ) cout << SNO[i] << " "; cout << endl;
  for ( int i = 1; i <= N; ++i ) if ( PNO[i] == SNO[i] ) ++sum;
  cout << sum << endl;
}


Measured memory_bytes: 7040
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define uniq(v) v.erase(unique(v.begin(),v.end()),v.end())
#define lb(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define ub(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
using Pair = pair<int,ll>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>> 
const ll mod=1000000007;
//const ll mod=998244353;
const ld pi=acos(-1.0);
const ll INF = 1LL<<61;
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 (b<a) { a=b; return 1; } return 0; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる



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

  int ttt;
  cin>>ttt;

  rep(tt,ttt){
    int n;
    cin>>n;

    vector<pair<int,ll>> mae;
    vector<pair<int,ll>> ush;
    ll ans=0;

    rep(i,n){
      int k;
      ll l,r;
      cin>>k>>l>>r;
      k--;
      if(l>=r) {
        auto z=mp(k,l-r);
        mae.pb(z);
        ans+=r;
      }
      else {
        auto z=mp(n-2-k,r-l);
        ush.pb(z);
        ans+=l;
      }
    }

    sort(all(mae));
    sort(all(ush));

    priority_queue<ll, vector<ll>, greater<ll>> que;
    int now=0;
    rep(i,n){
      while(now<mae.size() && mae[now].fi==i) {
        que.push(mae[now].se);
        now++;
      }
      while(que.size()>i+1){
        que.pop();
      }
    }

    while(!que.empty()){
      ll x=que.top();
      ans+=x;
      que.pop();
    }

    now=0;
    rep(i,n){
      while(now<ush.size() && ush[now].fi==-1) {
        now++;
      }
      while(now<ush.size() && ush[now].fi==i) {
        que.push(ush[now].se);
        now++;
      }
      while(que.size()>i+1){
        que.pop();
      }
    }


    while(!que.empty()){
      ll x=que.top();
      ans+=x;
      que.pop();
    }

    cout << ans << endl;
  }

}

Measured memory_bytes: 8264
n,k = list(map(int,input().split()));
a = list(map(int,input().split()));
i = 0;
start = 0;
count = 0;
finished = [0]*n;
while count < n+2 :
    #print(i,count)
    finished[i] = 1;
    i = a[i]-1;
    count += 1;
    if finished[i] :
        start = i;
        break;
    #print(i,count,1) 

amari = 0;
if count > n :
    ans = i+1;
else :
    if start == 0 :
        tostart = 0;
    else :
        i=0;
        count = 0;
        while count < n+1 :
            i = a[i]-1;
            count += 1;
            if i == start :
                break;
        tostart = count;
    i=start;
    count = 0;
    while count < n+1 :
        i = a[i]-1;
        count += 1;
        if i == start :
            break;
    if k < count+tostart :
        i = 0;
        for j in range(k) :
            i = a[i]-1;
        ans = i+1;
    else :
        #print(tostart,count);
        amari = (k-tostart)%count;
        i = start;
        for j in range(amari) :
            i = a[i]-1;
        ans = i+1;

print(ans);


Measured memory_bytes: 32156