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

template <class t>
using vc = vector<t>;
template <class t>
using vvc = vector<vector<t>>;
typedef long long ll;
typedef vc<int> vi;
typedef vvc<int> vvi;
typedef pair<int, int> pi;

#define uset unordered_set
#define umap unordered_map

#define endl "\n"

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()

#define all(a) a.bg, a.ed

template <class t, class u>
ostream &operator<<(ostream &os, const pair<t, u> &p) {
    return os << "( " << p.first << ", " << p.second << " )";
}

template <class t>
ostream &operator<<(ostream &os, const vc<t> &v) {
    os << "[ ";
    for (int i = 0; i < v.size(); i++) {
        os << v[i];
        if (i != v.size() - 1) {
            os << ", ";
        }
    }
    return os << " ]";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    // cout << fixed << setprecision(18);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // Solution

    int n;
    cin >> n;
    vi h(n + 1);
    vc<bool> dec(n + 1, false);
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
    }
    int last = h[n];
    for (int i = n - 1; 0 < i; i--) {
        if (h[i] - last > 1) {
            cout << "No";
            return 0;
        } else if (h[i] - last == 1) {
            h[i]--;
        }
        last = h[i];
    }
    cout << "Yes";
}


Measured memory_bytes: 3708
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define inputv(v, n)    \
    vl v;               \
    rep(i, n)           \
    {                   \
        ll x;           \
        cin >> x;       \
        v.push_back(x); \
    }
const ll INF = 999999999999999;
const ll MOD = 998244353;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans = 0;
int main()
{
    string S;
    cin>>n>>S;
    rep(i,n-2){
        if(S[i]=='A'&&S[i+1]=='B'&S[i+2]=='C')ans++;
    }

    cout<<ans<<endl;
}

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

int main(){
  int x;
  cin >>x;
  
  int a=x/100;
  int b=x-a*100;
  
  if (a>12||a==0){
    if(b>12||b==0){
      cout << "NA" <<endl;
    }
    else{
      cout << "YYMM" <<endl;
    }
  }
  else{
    if (b>12||b==0){
      cout << "MMYY" <<endl;
    }
    else{
      cout << "AMBIGUOUS" <<endl;
    }
  }  
}

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

int main() {
  char x, y;
  std::cin >> x >> y;

  if (x < y) {
    std::cout << "<" << std::endl;
  } else if (x == y) {
    std::cout << "=" << std::endl;
  } else {
    std::cout << ">" << std::endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
#define INF (1e9)

const string YES = "Yes";
const string NO = "No";

int main() {
  ll A;
  cin >> A;
  ll B;
  cin >> B;
  ll C;
  cin >> C;

  if ((A == B && B != C) || (A == C && C != B) || (C == B && B != A))
    cout << YES << endl;
  else
    cout << NO << endl;
}


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


def answer(H: int, N: int, A: list) -> str:
    damage = 0                          # damageに0を代入します
    for i in range(0, N):               # 0:Nの間にiがある間は
        damage += int(A[i])             # damageにリスト[i]をint(整数）に変え足します
                                        # 6でdamage=0にforの間Aの値を足していく意味
        i += 1                          # 条件を満たす間はiに1つずつ足していきます。リスト内を進めていく
    if damage < H:
        return 'No'
    else:
        return 'Yes'

print(answer(H, N, A))

Measured memory_bytes: 20520
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
count = B[A[0]-1]
for i in range(1,N):
    count+=B[A[i]-1]
    if A[i] == A[i-1] + 1:
        count+=C[A[i-1]-1]
print(count)

Measured memory_bytes: 3064
//割り当て//2863266を参考
using System;
using System.Linq; //max sum


namespace tst
{
    class Program
    {
        static bool Check(int[]W, int m, int n ,int  k)
        {
            //一つのトラックに何個詰め込むか
            int othlug = 0;
            int car_num = 1;
            for(int i=0; i<n; i++)
            {
                if (othlug + W[i] <= m)
                {
                    //暫定のPの値mが荷物量をオーバーしてないならもう一個積む
                    othlug += W[i];
                    continue;
                }
                else
                {
                    //オーバーしたら次のトラック出動
                    car_num++;
                    othlug = W[i];
                }
            }
            return car_num<=k;
        }
        public static void Main()
        {
            //荷物数C[0]、トラックの数C[1]
            string[] C = Console.ReadLine().Split(' ');
            //荷物数
            int n = int.Parse(C[0]);
            //トラックの数
            int k = int.Parse(C[1]);
            //荷物の重さW
            int[] W = new int[n];
            for(int i=0; i<n; i++)
            {
                W[i] = int.Parse(Console.ReadLine());
            }
            //まずは暫定的なPの値を決め、だんだん下げていく探索
            int biglug = W.Max();
            int sum = W.Sum();
            
            while(biglug < sum)
            {
                //mは暫定のPの値
                int m = (biglug + sum) / 2;
                if (Check(W, m, n , k))//トラックの積載量の変更で台数がオーバーしないなら
                {
                    //上限を下げる
                    sum = m;
                }
                else
                {
                    //下限を上げる
                    biglug = m + 1;
                }
            }
            Console.WriteLine(sum);
        }

    }
}


Measured memory_bytes: 20328
from collections import deque
from typing import List, Optional, Tuple


def cycle_detectable_topological_sort(
    graph: List[List[int]], in_degrees: List[int], first_index: int = 0
) -> Tuple[Optional[List[int]], Optional[List[int]]]:
    """Topological sort that uses Kahn's algorithm and detects a loop (DAG or not).
    Returns:
        if the given graph is DAG, a list of sorted vertices and a list of depths of
        each vertex is returned.
        Otherwise, (None, None) is returned.
    """
    V = len(graph) + first_index
    order = []
    depths = [-1] * V  # depths[i] := the length of the longest path to V_i
    for i in range(first_index, V):
        if not in_degrees[i]:
            order.append(i)
            depths[i] = 0

    queue = deque(order)
    while queue:
        u = queue.popleft()
        cur_depth = depths[u]
        for v in graph[u]:
            in_degrees[v] -= 1
            if not in_degrees[v]:
                depths[v] = cur_depth + 1
                queue.append(v), order.append(v)
    return (order, depths) if len(order) + first_index == V else (None, None)


def abc139_e():
    # https://atcoder.jp/contests/abc139/tasks/abc139_e
    N, *A = map(int, open(0).read().split())

    ids = [[-1] * (N + 1) for _ in range(N + 1)]
    cur_id = 0
    for i in range(1, N + 1):
        for j in range(i + 1, N + 1):
            ids[i][j] = ids[j][i] = cur_id
            cur_id += 1

    graph = [[] for _ in range(N * (N - 1) // 2)]
    in_degrees = [0] * (N * (N - 1) // 2)
    for i, a in enumerate(zip(*[iter(A)] * (N - 1)), 1):
        source = -1
        for j in a:
            target = ids[i][j]
            if source != -1:
                graph[source].append(target)
                in_degrees[target] += 1
            source = target

    _, depths = cycle_detectable_topological_sort(graph, in_degrees)
    print(max(depths) + 1 if depths else -1)


def grl_4_a():
    # https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/4/GRL_4_A
    V, _, *ST = map(int, open(0).read().split())
    graph = [[] for _ in range(V)]
    in_degrees = [0] * V
    for s, t in zip(*[iter(ST)] * 2):
        graph[s].append(t)
        in_degrees[t] += 1

    res, _ = cycle_detectable_topological_sort(graph, in_degrees)
    print(1 if res is None else 0)


if __name__ == "__main__":
    abc139_e()
    # grl_4_a()


Measured memory_bytes: 144084
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<numeric>
#include<queue>
#include<string>
#include<vector>
#include <stdio.h>
#define ll long long
#include<cstring>
#define rep(i,n) for(int i=0;i<n;i++)
ll mod = 1000000007;
using namespace std;
priority_queue<ll int>QUE;//きゅー

int length(char* str) {
	int len = strlen(str);
	return len;
}

int main(void)
{
	char S[100010];
	int count = 0;
	cin >> S;
	int a=length(S);
	for (int i = 0; i < a/2; i++) {
		if (S[i] != S[a - 1 - i]) {
			count++;
		}
	}
	cout << count;
	
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    int h,n,i,j,s=0;
    cin>>h>>n;
    for(i=0;i<n;i++)
    {
        cin>>j;
        s+=j;
    }
    if(s<h) cout<<"No\n";
    else cout<<"Yes\n";
    return 0;
}


Measured memory_bytes: 256
def resolve():
    H, W, K = map(int, input().split())

    c = [list(input()) for _ in range(H)]
    ans = 0

    for i in range(2**H):

        for j in range(2**W):

            now = 0
            for x in range(H):
                for y in range(W):

                    if 2**x & i > 0 and 2**y & j > 0:
                        if c[x][y] == "#":
                            now += 1

            if now == K:
                ans += 1

    print(ans)


resolve()

Measured memory_bytes: 9168
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for(ll i = m, i##_len = (n); i < i##_len; ++i)
#define reprev(i, n) for(ll i = (n-1LL); i >= 0; --i)
#define all(x) (x).begin(), (x).end()
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; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s;
	int n;
	cin >> n >> s;
	bool dp1[10] = {}, dp2[100] = {}, dp3[1000] = {};
	rep (i, n) {
		int x = (int)s[i] - '0';
		if (i > 1) {
			rep (j, 100) {
				if (dp2[j]) dp3[j * 10 + x] = true;
			}
		}
		if (i > 0) {
			rep (j, 10) {
				if (dp1[j]) dp2[j * 10 + x] = true;
			}
		}
		dp1[x] = true;
	}
	int ans = 0;
	for (bool chk : dp3) {
		if (chk) ans++;	
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 384
const main = (input: string) => {
  const N = parseInt(input.trim())
  let isNotFound = true
  let primeNumber = N

  const getPrimeNumber = (n: number) => {
    if (n === 2) return true;
    for (let i = 2; i < n; i++) {
      if (n % i === 0) return false;
    }
    return true;
  }

  let count = 0
  while(isNotFound) {
    if(getPrimeNumber(N + count)) {
      primeNumber = N + count
      isNotFound = false
    }
    count += 1
  }
  console.log(primeNumber)
}

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


Measured memory_bytes: 7372
#pragma warning(disable:4996)

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cstdio>

#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<double, int> pii;

int main(){
    vector<pii> v(24);
    rep(i, 24){
        int a;
        double t;
        scanf("%d %lf", &a, &t);
        v[i] = make_pair(t, a);
    }

    rep(i, 3){
        sort(v.begin() + 8 * i, v.begin() + 8 * i + 8);
    }
    vector<pii> ans;
    rep(i, 3)rep(j, 6){
        ans.push_back(v[i * 8 + j + 2]);
    }
    sort(all(ans));

    rep(i, 3) rep(j, 2){
        printf("%d %.2f\n", v[i * 8 + j].second, v[i * 8 + j].first);
    }
    rep(i, 2){
        printf("%d %.2f\n", ans[i].second, ans[i].first);
    }
}

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

using namespace std;

int main() {
    long k, a, b;
    cin >> k >> a >> b;
    cout << ((k > a - 1 && b - a >= 2)
                 ? a + ((k - (a - 1)) / 2) * (b - a) + (k - (a - 1)) % 2
                 : k + 1)
         << '\n';
    return 0;
}

Measured memory_bytes: 256
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;

template<typename Monoid>
struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;

    int sz;
    vector<Monoid> seg;

    const F f;
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
        sz = 1;
        while(sz < n) sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x) {
        seg[k + sz] = x;
    }

    void build() {
        for(int k = sz - 1; k > 0; k--) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    void update(int k, const Monoid &x) {
        k += sz;
        seg[k] = x;
        while(k >>= 1) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    Monoid query(int a, int b) {
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) L = f(L, seg[a++]);
            if(b & 1) R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const {
        return seg[k + sz];
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    P p[300003];
    for(int i = 0; i < n; i++){
        int l, r;
        cin >> l >> r;
        p[i] = P(r - l + 1, l);
    }
    sort(p, p + n);
    SegmentTree<int> seg(m + 2, [](int a, int b){return a + b;}, 0);
    int j = 0;
    for(int i = 1; i <= m; i++){
        for(; j < n; j++){
            if(p[j].first >= i) break;
            seg.update(p[j].second, seg[p[j].second] + 1);
            seg.update(p[j].second + p[j].first, seg[p[j].second + p[j].first] - 1);
        }
        int ans = n - j;
        for(int r = i; r <= m; r += i) ans += seg.query(0, r + 1);
        cout << ans << endl;
    }
}

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

using namespace std;

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

const double PI = 3.1415926535897932384626433832795;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

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

	int ans = (t / a) * b;
	cout << ans <<  endl;
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

map<int,int>mm;
int val[100005];
int fi[100005];
int seg[800050];
int temp[200020];

void build(int ver,int tl,int tr)
{
  if(tl>tr)
    return ;
  else if(tl==tr)
    seg[ver]=temp[tl];
  else
  {
    int tm=(tl+tr)/2;
    build(2*ver,tl,tm);
    build(2*ver+1,tm+1,tr);
    seg[ver]=seg[2*ver]+seg[2*ver+1];
  }  
}

void upd(int ver,int tl,int tr,int pos,int val)
{
  if(tl>tr)
    return ;
  else if(tl==tr)
    seg[ver]+=val;
  else
  {
    int tm=(tl+tr)/2;
    if(tm>=pos)
      upd(2*ver,tl,tm,pos,val);
    else
      upd(2*ver+1,tm+1,tr,pos,val);
    seg[ver]=seg[2*ver]+seg[2*ver+1];
  }  
}

int get(int ver,int tl,int tr,int l,int r)
{
  if(tl>tr||l>r||tr<l||r<tl)
    return 0;
  else if(tl==l&&tr==r)
    return seg[ver];
  else
  {
    int tm=(tl+tr)/2;
    return get(2*ver,tl,tm,l,min(tm,r))+get(2*ver+1,tm+1,tr,max(tm+1,l),r);
  }  
}

signed main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);  
  cout.tie(0);   

  #ifndef ONLINE_JUDGE
      if(fopen("INPUT.txt","r"))
      {
      freopen ("INPUT.txt" , "r" , stdin);
      freopen ("OUTPUT.txt" , "w" , stdout);
      }
  #endif  

  int n;
  cin>>n;
  int a[n+1];
  set<int>s;
  for(int i=1;i<=n;i++)
  {
    cin>>a[i];
    s.insert(a[i]);
  }
  int cnt=0;
  for(auto i:s)
  {
    mm[i]=++cnt;
    val[cnt]=i;
  }
  for(int i=1;i<=n;i++)
    a[i]=mm[a[i]];    

  vector<int>v;
  for(auto i:s)
    v.push_back(mm[i]);
  int l=0,r=v.size();
  r--;
  int len=(n*(n+1))/2;
  int mx=0;
  while (l<=r)
  {
    int mid=l+(r-l)/2;
    int ind=v[mid];
    int lim=1e5+1;
    for(int i=0;i<=800010;i++)
      seg[i]=0;
    for(int i=0;i<=200005;i++)
      temp[i]=0;
    int x=lim;
    for(int i=1;i<=n;i++)
    {
      if(a[i]<ind)
        x--;
      else
        x++;
      temp[x]++;
    }
    build(1,1,2*lim);
    x=0;
    int tot=0;
    for(int i=1;i<=n;i++)
    {
      int z=x+lim;
      int num=get(1,1,2*lim,z,2*lim);
      if(a[i]<ind)
        x--;
      else
        x++;
      tot+=num;
      upd(1,1,2*lim,x+lim,-1);
    }
    if(tot>=(len+1)/2)
    {
      l=mid+1;
      mx=max(mx,ind);
    }
    else
    {
      r=mid-1;
    }    
  }
  cout<<val[mx];

}


Measured memory_bytes: 21876
#include <bits/stdc++.h>
// atoi : atoi(変数名.c_str())
// to_string(数値)
// %0桁数d とすると表示桁数が「桁数」に満たない場合に0埋めされる
// find(): int i = 変数名.find(“文字列”) 何番目にあるかを得る
// cout << (条件 ? "Yes" : "No") << endl;
// printf("%02d:%02d:%02d",hour,minute,second);
// 降順 sort(a.begin(),a.end(),greater<int>());
// bitset<ビット数> 変数名;  // すべてのビットが0の状態で初期化される
// bitset<ビット数> 変数名("ビット列");  // 指定したビット列で初期化される
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using pii = pair<int, int>;
#define fix10 cout << fixed << setprecision(10);
#define rep(i,n) for(int i=0; i<(int)(n);i++)

int main(){
  int a,b,c;
  cin >> a >> b >> c;
  set<int> st;
  st.insert(a);
  st.insert(b);
  st.insert(c);
  cout << st.size() << endl;
}


Measured memory_bytes: 256
#![allow(unused_imports)]

use proconio::{input, fastout};
use proconio::marker::*;

fn main() {
    input! {
    h: usize,
    w: usize,
    g: [[usize; w]; h]
   }
    let mut ans = vec![];
    let mut is_find = false;
    let mut tmp = vec![];
    for i in 0..w {
        for j in 0..h {
            let (x, y) = if i % 2 == 0 { (i, j) } else { (i, h - j - 1) };
            if g[y][x] % 2 == 1 {
                tmp.push((x, y));
                if is_find {
                    ans.push(tmp.clone());
                    tmp.clear()
                }
                is_find = !is_find;
            } else if is_find {
                tmp.push((x, y));
            }
        }
    }
    let n = ans.iter().map(|x| x.len() - 1).sum::<usize>();
    println!("{}", n);
    for i in 0..ans.len() {
        for j in 0..ans[i].len() - 1 {
            println!("{} {} {} {}", ans[i][j].1 + 1, ans[i][j].0 + 1, ans[i][j + 1].1 + 1, ans[i][j + 1].0 + 1)
        }
    }
}


Measured memory_bytes: 13488
open Batteries
module EnumL = Enum.Labels
module ListL = List.Labels
module ArrayL = Array.Labels

let dbg0 x = Printf.eprintf "[debug]%s\n" @@ dump x
let dbg1 x = dbg0 x; x

let id = identity

let (++) n m = List.range n `To m
let (++-) n m = if n >= m then List.range n `Downto m else []
let (++^) n m = if n < m then List.range n `To (pred m) else []

let scan fmt = Scanf.sscanf (read_line ()) fmt

let scan_lines n fmt f =
  if n = 0 then []
  else List.map (fun _ -> scan fmt f) (1 ++ n)

let scan_list ?sep cnv =
  let line = read_line () in
  (match sep with
   | None -> List.map String.of_char @@ String.to_list line
   | Some sep -> String.split_on_char sep line)
  |> List.map cnv

let scan_array ?sep cnv = Array.of_list @@ scan_list ?sep cnv

let scan_matrix n m e ?sep conv =
  let arr = Array.make_matrix n m e in
  ListL.iter (0 ++^ n)
    ~f:(fun i -> arr.(i) <- Array.of_list @@ scan_list ?sep conv);
  arr

let atoi c = Char.code c - Char.code '0'

let rec powerset = function
  | [] -> [[]]
  | hd::tl ->
    let pws =  powerset tl in
    pws @ ListL.map pws ~f:(fun pw -> hd::pw)

let permutations l =
  let rec interleave x = function
    | [] -> [[x]]
    | (hd::tl) as l ->
      (x::l) :: (interleave x tl |> ListL.map ~f:(fun l -> hd::l)) in
  let rec aux = function
    | [] -> [[]]
    | a::rest ->
      aux rest |> ListL.map ~f:(interleave a) |> List.concat in
  aux l

let intersection l =
  EnumL.filter ~f:(fun x -> EnumL.exists ~f:((=) x) @@ Enum.clone l)

let zip l m =
  let n = min (List.length l) (List.length m) in
  List.combine (List.take n l) (List.take n m)

let lower_bound n f =
  let rec aux l u =
    if u - l > 1 then
      let m = (l + u) / 2 in
      if f m then aux l m
      else aux m u
    else u in aux (-1) n

let between n x m = n <= x && x < m

let (n, m) = scan "%d %d" Tuple2.make
let ls = scan_lines m "%d %d" Tuple2.make

let () =
  let mat = Array.make (succ n) [] in
  ListL.iter ls ~f:(fun (a,b) ->
      mat.(a) <- b::mat.(a);
      mat.(b) <- a::mat.(b));
  let q = Queue.create () in
  Queue.add (1,1) q;
  let parents = Array.make (succ n) 0 in
  let rec wfs q =
    if Queue.is_empty q then ()
    else
      let (p, n) = Queue.pop q in
      if parents.(n) <> 0 then wfs q
      else
        (parents.(n) <- p;
         ListL.iter mat.(n) ~f:(fun m ->
             Queue.add (n, m) q);
         wfs q)
  in
  wfs q;
  if Array.exists ((=) 0)@@ Array.sub parents 2 (Array.length parents - 2)  then
    Printf.printf "No\n"
  else
    (Printf.printf "Yes\n";
     ListL.iter (2++n) ~f:(fun i ->
         Printf.printf "%d\n" parents.(i)))


Measured memory_bytes: 51612
let max_str slst = 
  snd @@ List.fold_left (fun (mx, s) (x, y) -> if mx < x then (x, y) else (mx, s)) (0,"") @@ List.map (fun x -> (String.length x, x)) slst

let mf_str slst = 
  let tbl = Hashtbl.create 1000 in
  List.iter (fun x -> if Hashtbl.mem tbl x then Hashtbl.add tbl x (Hashtbl.find tbl x + 1) else Hashtbl.add tbl x 1) slst;
  fst @@ Hashtbl.fold (fun x y (s, n) -> if y >= n then (x, y) else (s, n)) tbl ("",0)

let _ =
  let slst = Str.split (Str.regexp " ") (read_line ()) in
  Printf.printf "%s %s\n" (mf_str slst) (max_str slst)

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

const int mod = 998244353;
template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< mod >;

int n;

int main(void){
    cin >> n;
    vector<int> d(n);
    int maxv = 0;
    for(int i = 0; i < n; i++){
        cin >> d[i];
        maxv = max(maxv, d[i]);
    }
    vector<int> c(maxv+1);
    for(int i = 0; i < n; i++){
        c[d[i]]++;
    }
    if(d[0] != 0 || c[0] != 1){
        cout << 0 << endl;
        return 0;
    }
    modint res = 1;
    for(int i = 0; i < c.size()-1; i++){
        modint tmp = modint(c[i]).pow(c[i+1]);
        res = res*tmp;
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 3920
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

H, W = map(int, readline().split())
c = [list(map(int, readline().split())) for _ in range(10)]
A = list(map(int, read().split()))


def warshall_floyd(dist):
    for k in range(10):
        for i in range(10):
            for j in range(10):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    return dist


cc = warshall_floyd(c)

ans = 0
for a in A:
    if a == -1:
        continue
    ans += cc[a][1]
print(ans)

Measured memory_bytes: 5284
import java.io.PrintStream;
import java.util.Scanner;

public class Main {

	static void exec(Scanner in, PrintStream out) {
		int[][] c = new int[3][3];
		for (int i = 0; i < 3; i += 1) {
			for (int j = 0; j < 3; j += 1) {
				c[i][j] = in.nextInt();
			}
		}
		for (int i = 0; i <= 100; i += 1) {
			int a1 = i;
			int b1 = c[0][0] - a1;
			int b2 = c[0][1] - a1;
			int b3 = c[0][2] - a1;
			int a2 = c[1][0] - b1;
			if (a2 + b2 != c[1][1] || a2 + b3 != c[1][2]) {
				continue;
			}
			int a3 = c[2][0] - b1;
			if (a3 + b2 != c[2][1] || a3 + b3 != c[2][2]) {
				continue;
			}
			out.println("Yes");
			return;
		}
		out.println("No");
	}

	public static void main(String[] args) {
		exec(new Scanner(System.in), System.out);
	}

}


Measured memory_bytes: 23764
#include <stdio.h>
int main(void){
    // Here your code !
    int a,b,c;
    scanf("%d%d%d", &a, &b, &c);
    int i;
    int j = 0;
    for( i = a ; i <= b ; ++i ){
        if( c%i == 0){
            ++j;
        }
    }
    printf("%d\n", j);
    return 0;
}

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

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
#define clr(a,v) memset(a, v, sizeof(a))

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)
#define reflex ios_base::sync_with_stdio(false)

#define nl '\n'
#define ioi exit(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N= (int)2e5 + 7;
const int inf = (int)1e9 + 7;
//~ const int mod = (int)1e9 + 7;
const int mod = 998244353;
const ll linf = (ll)1e18 + 7;

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

string conv(int a){stringstream ss;ss << a;string str = ss.str();return str;}
string conv(char a){stringstream ss;ss << a;string str = ss.str();return str;}
int conv(string a){stringstream geek(a); int x = 0; geek >> x;   return x;}

bool flag[12];
int cont [12];
int p[12];
int tab[12][12];
int n, m;
int ans = 0;

void solve(int i,int f){
	flag[i] = f;
	if(i >= n){
		
		clr(cont,0);
		rep(k,1,m){
			rep(r,1,tab[k][0]){
				int x = tab[k][r];
				if(flag[x] == 1)
					cont[k]++;
			}
		}
		
		int yes = 0;
		rep(i,1,m){
			if(p[i] == cont[i]%2)
				yes++;
		}
		
		if(yes == m){
			ans++;
		}
		
		return;
	}
	solve(i+1,0);
	solve(i+1,1);
}
int main(void) 
{	
	cin>>n>>m;
	rep(i,1,m){
		int k;
		cin>>k;
		tab[i][0] = k;
		rep(j,1,k){
			cin>>tab[i][j];			
		}
	}
	
	rep(i,1,m)
		cin>>p[i];
	
	solve(1,0);
	
	solve(1,1);
	cout<<ans;
	return 0;
} 



Measured memory_bytes: 256
#include<stdio.h>
int main(void){
	int i;
	int max=0,max1=0,max2=0;
	int a[10];
	
	for(i=0;i<10;i++){
		scanf("%d",&a[i]);
		if(a[i]>max){
			max2=max1;
			max1=max;
			max=a[i];
		}
		else if(a[i]>max1){
			max2=max1;
			max1=a[i];
		}
		else if(a[i]>max2){
			max2=a[i];
		}
	}
	
	
		
	printf("%d\n%d\n%d\n",max,max1,max2);
	return 0;
}

Measured memory_bytes: 600
#include<cstdio>
#include<cstring>

#define MAXN 10
int N;
char str[MAXN+5];

int main(){
    scanf("%s",str+1);
    N=strlen(str+1);
    int Max=(1<<(N-1))-1;
    long long Ans=0;
    for(int i=0;i<=Max;i++){
        long long Num=str[1]-'0';
        for(int j=0;j<N-1;j++){
            if(i&(1<<j)){
                Ans+=Num;
                Num=0;
            }
            Num=Num*10+str[j+2]-'0';
        }
        Ans+=Num;
    }
    printf("%lld",Ans);
}


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

long long mod=1000000007;

long long mypow(long a,long long b) {
  long long c;
  if (b==0) return 1;
  else {
    c=mypow(a,b/2)*mypow(a,b/2)%mod;
    if (b%2==1) c=c*a%mod;
    return c;
  }
}
vector<long long> fac(200001);
vector<long long> minv(200001);
void myfact(long n) {
  long i;
  fac.at(0)=1;
  minv.at(0)=1;
  for(i=1;i<=n;i++) {
    fac.at(i)=i*fac.at(i-1)%mod;
    minv.at(i)=mypow(fac.at(i),mod-2)%mod;
  }
}
long long mycomb(long n,long r) {
  long long c;
  c=fac.at(n)*minv.at(r)%mod*minv.at(n-r)%mod;
  return c;
}
int main() {
  long n,k,m,i;
  long long a;
  cin>>n>>k;
  m=min(n-1,k);
  a=0;
  myfact(n);
  for(i=0;i<=m;i++){
    a=(a+mycomb(n,i)*mycomb(n-1,i)%mod)%mod;
  }
  if (a<0) a+=mod;
  cout<<a<<endl;
}
//3≤n≤2×10^5
//2≤k≤10^9

Measured memory_bytes: 3328
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> ans;
  if (n%2) {
    for (int l = 1, r = n-1; l < r; l++, r--) {
      ans.emplace_back(l, r);
    }
  }
  else {
    bool flag = false;
    for (int l = 1, r = n-1; l < r; l++, r--) {
      if (!flag && r-l<=n/2) {
        r--;
        flag = true;
      }
      ans.emplace_back(l, r);
    }
  }
  rep(i,m) {
    cout << ans[i].fi << ' ' << ans[i].se << endl;
  }
  return(0);
}


Measured memory_bytes: 4224
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;

#[allow(dead_code)]
struct Scanner<'a> {
    cin: StdinLock<'a>,
}
#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn input<T: FromStr>(&mut self) -> T {
        self.read().unwrap()
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);

    let (a, b): (usize, usize) = (sc.input(), sc.input());
    let s: String = sc.input();
    let chs: Vec<char> = s.chars().collect();
    let ans = chs.iter()
        .enumerate()
        .all(|(i, c)| {
            if i == a { *c == '-' } else { c.is_digit(10) }
        });

    println!("{}", if ans { "Yes" } else { "No" });
}


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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
#define mp            make_pair
template<class T, class S> inline bool minup(T& m, const S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, const S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-10;

int n;
string s;

template<typename T> inline void _tostr(const T t, string& s){ostringstream o;o<<t;s=o.str();}
template<typename T> inline string _tostr(const T t){ostringstream o;o<<t;return o.str();}
template<typename T> inline void _toint(const string s, T& n){istringstream i(s);i>>n;}
template<typename T> inline T _toint(const string s){istringstream i(s);T n;i>>n;return n;}


int main()
{
  while(cin >> n, n){
    cin >> s;
    rep(i, n){
      string t = "";
      vrep(v, s){
        int cnt = 1;
        while(v != s.end() && *v == *(v + 1)){
          v += 1;
          cnt += 1;
        }
        t += _tostr(cnt) + *v;
      }
      s = t;
    }

    cout << s << endl;
  }

  return 0;
}

Measured memory_bytes: 1360
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;

ll binpow(ll a, ll b) {
    if (b == 0)
    {
        return 1;
    }
    ll res = binpow(a, b / 2);
    res*=res;
    if (b % 2)
    {
        return res * a;
    }
    return res;
}

ll gcd(ll a,ll b)
{
    if (b==0) return a;
    return gcd(b,a%b);
}

string to_upper(string a)
{
    for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
    return a;
}
 
string to_lower(string a)
{
    for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
    return a;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll a,b,v,w,t;
    cin>>a>>v;
    cin>>b>>w;
    cin>>t;
    if (b<a)
        swap(a,b);
    if (a+v*t>=b+w*t)
        cout<<"YES";
    else
        cout<<"NO";
    return 0;
}

Measured memory_bytes: 3652
N = int(input())

l = 0
r = N
last = 2
c = 0
while True:
    print(c)
    situ = input()
    if situ == "Male":
        situ = 0
    elif situ == "Female":
        situ = 1
    else:
        break
    
    if last == 2:
        last = situ
        c = N // 2
        continue
    elif (last + situ + c) % 2 == 0:
        l = c
    else:
        r = c
    c = (r+l) // 2


        
        

Measured memory_bytes: 3660
#include <iostream>
#include <sstream>
#include <string>


int i=0;
int stack[200];

void push(int n){
    stack[i] = n;
    i++;
}

int pop(){
    --i;
    return stack[i];
}

int main(){
    std::string str;
    std::getline(std::cin,str);
    std::istringstream iss(str);
    std::string a;
    int tmp;
    while(iss>>a){
        if(a=="+"){
            push(pop()+pop());
        }else if(a=="-"){
            tmp = pop();
            push(pop()-tmp);
        }else if(a=="*"){
            push(pop()*pop());
        }else if(a=="/"){
            tmp = pop();
            push(pop()/tmp);
        }else{
            tmp = std::stoi(a);
            push(tmp);
        }
    }
    std::cout << stack[0] << "\n";
    return 0;
}


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

int main()
{
    long M, N;
    cin >> N >> M;
    long A = 0, buf;
    for(int i = 0; i < M; i++){
      cin >> buf;
      A += buf;
    }
    if(N - A < 0)cout << "-1" << endl;
    else cout << N - A << endl;
    return 0;
}


Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<int,int> pi;
typedef pair<lld,lld> pl;
typedef vector<int> vit;
typedef vector<vit> vitt;
typedef vector<lld> vlt;
typedef vector<vlt> vltt;
typedef vector<pi> vpit;
typedef vector<vpit> vpitt;
typedef long double ld;
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mk(a,b) make_pair(a,b)
bool isrange(int y,int x,int n,int m){
	 if(0<=y&&y<n&&0<=x&&x<m) return true;
	 return false;
}
int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1};

int main(void){
	int k;
	scanf("%d",&k);
	lld xx = 1,tot = 0;
	for(int p=1;p<=k+1;p++){
		tot += xx;
		if((tot*7)%k==0) return !printf("%d",p);
		xx = (xx*10)%k;
	}
	printf("-1");
	return 0;
}


Measured memory_bytes: 3824
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int stack[100];
int i = 0;

void push(int x) {
	stack[i++] = x;
}

int pop() {
	return stack[--i];
}

int main(){
  int x;
  char s[100];

  while( scanf("%s", s) != EOF ){
    if ( s[0] == '+' ){
		x = pop();
		push(pop() + x);
    } else if ( s[0] == '-' ){
		x = pop();
		push(pop() - x);
    } else if ( s[0] == '*' ){
		x = pop();
		push(pop() * x);
    } else {
      x = atoi(s);
      push(x);
    }
  }
  
  printf("%d\n", pop());

  return 0;
}

Measured memory_bytes: 548
while True:
    a,op,b=input().split()
    a=int(a)
    b=int(b)
    if op=='+':
        print(a+b)
    elif op=='-':
        print(a-b)
    elif op=='*':
        print(a*b)
    elif op=='/':
        print(a//b)
    else:
        break


Measured memory_bytes: 5596
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,n=0;
    string s;
    cin>>s;
    for (i=0;i<s.length();i++)
    if (s[i]=='A'&&s[i+1]=='C') 
    { cout<<"Yes"<<endl; return 0; }
    cout<<"No"<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
using namespace std;
using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

class UnionFind {
public:
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

int main() {
    ll n;
    cin >> n;
    vector<vector<ll>> z(3, vector<ll>(n));
    vector<vector<ll>> dp(3, vector<ll>(n+1));
    for (int i = 0; i < n; i++) {
        cin >> z[0][i] >> z[1][i] >> z[2][i];
    }
    for (int i = 0; i < n; i++) {
        dp[0][i + 1] = max(dp[1][i] + z[1][i], dp[2][i] + z[2][i]);
        dp[1][i + 1] = max(dp[0][i] + z[0][i], dp[2][i] + z[2][i]);
        dp[2][i + 1] = max(dp[1][i] + z[1][i], dp[0][i] + z[0][i]);
    }
    cout << max(dp[0][n], max(dp[1][n], dp[2][n])) << endl;
}

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

int main(){
  ll N; cin >> N;
  ll i; i = (int)sqrt(N);
  while (N % i != 0) i--;
  ll j = N / i;
  cout << i + j - 2 << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include<stdio.h>
using namespace std;
#define MAX 200000
#define mm  10000000000
typedef long long ll;
int L[MAX],R[MAX];
ll merge(int A[],int n,int left,int mid,int right)
{

    int i,j,k;
    ll cnt=0;
    int n1=mid-left;
    int n2=right-mid;
    for(i=0; i<n1; i++)L[i]=A[left+i];
    for(i=0; i<n2; i++)R[i]=A[mid+i];
    L[n1]=R[n2]=mm;
    i=0;
    j=0;
    for(k=left; k<right; k++)
    {
        if(L[i]<=R[j])
        {
            A[k]=L[i++];
        }
        else
        {
            A[k]=R[j++];
            cnt+=n1-i;//=mid+j-k-i;
        }
    }
    return cnt;

}
ll mergeSort(int A[],int n,int left, int right)
{
    int mid;
    ll v1,v2,v3;
    if(left+1<right)
    {
        mid=(left+right)/2;
        v1=mergeSort(A,n,left,mid);
        v2=mergeSort(A,n,mid,right);
        v3=merge(A,n,left,mid,right);
        return v1+v2+v3;
    }
    return 0;
}
int main()
{
    int A[MAX],n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&A[i]);
    }
    ll ans=mergeSort(A,n,0,n);
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 4716
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [a-1 for a in A]

L = 65
D = [[0]*n for _ in range(L+1)]

for i, a in enumerate(A):
    D[0][i] = a

for i in range(L):
    for j in range(n):
        D[i+1][j] = D[i][D[i][j]]

ans = 0
for i in range(L):
    if k & (1 << i):
        ans = D[i][ans]
print(ans+1)


Measured memory_bytes: 176404
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define fi first
#define se second
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 2005;
const int MOD = 1e9 + 7;
int n, k;
int a[maxn];

void ReadInput()
{
    cin >> n >> k;
    for(int i = 0; i < n; ++i)
        cin >> a[i];
}

void Solve()
{
    int ans = 0;
    for(int i = 0; i < n; ++i)
    {
        int cnt = 0;
        for(int j = 0; j < n; ++j)
            if(a[j] < a[i]) ++cnt;
        ans = ((lli)ans + 1LL * (k - 1) * k / 2 % MOD * cnt) % MOD;
        cnt = 0;
        for(int j = i + 1; j < n; ++j)
            if(a[i] > a[j]) ++cnt;
        ans = ((lli)ans + 1LL * cnt * k) % MOD;
    }
    cout << ans << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}


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

void fnInput(int& rnSwitchSiz, 
             vector<vector<int>>& rvvnLightSw, vector<int>& rvnReslt)
{
  int  nLightSiz;
  cin >> rnSwitchSiz >> nLightSiz;
  rvvnLightSw.resize(nLightSiz);

  for (int i = 0; i < rvvnLightSw.size(); i++)
  {
    int nLightSwSiz;
    cin >> nLightSwSiz;
    rvvnLightSw[i].resize(nLightSwSiz);

    for (int j = 0; j < rvvnLightSw[i].size(); j++)
      cin >> rvvnLightSw[i][j];
  }

  rvnReslt.resize(nLightSiz);
  for (int& rnReslt : rvnReslt)
    cin >> rnReslt;
}

bool fnLightSwChk(const vector<vector<int>>& cnrvvnLightSw,
                  const vector<int>& cnrvnSwitch,
                  const vector<int>& cnrvnReslt)
{
  for (int i = 0; i < cnrvvnLightSw.size(); i++)
  {
    int nLightUpCnt = 0;

    for (int j = 0; j < cnrvvnLightSw[i].size(); j++)
      nLightUpCnt += cnrvnSwitch[ cnrvvnLightSw[i][j] - 1 ];

    if (nLightUpCnt % 2 != cnrvnReslt[i])
      return false;
  }
  return true;
}

int fnNumOfCase(int nSwitchSiz,
                const vector<vector<int>>& cnrvvnLightSw, 
                const vector<int>& cnrvnReslt)
{
  int nNumOfCase = 0;
  vector<int> vnSwitch(nSwitchSiz);

  for (int nCtl = 0; nCtl < ( 1 << nSwitchSiz ); nCtl++)
  {
    for (int nEach = 0; nEach < nSwitchSiz; nEach++)
      if (nCtl & ( 1 << nEach ))
        vnSwitch[nEach] = 1;
      else
        vnSwitch[nEach] = 0;

    if (fnLightSwChk(cnrvvnLightSw, vnSwitch, cnrvnReslt))
      nNumOfCase++;
  }
  return nNumOfCase;
}
  
int main()
{
  int nSwitchSiz;
  vector<vector<int>> vvnLightSw;
  vector<int> vnReslt;

  fnInput(nSwitchSiz, vvnLightSw, vnReslt);
  cout << fnNumOfCase(nSwitchSiz, vvnLightSw, vnReslt) << endl;

  return 0;
}

Measured memory_bytes: 3624
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);
    static StringBuilder sb = new StringBuilder();
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String[] args) {
        int n = nextInt();
        int q = nextInt();
        var a = nextLongs(n);
        var t = new FenwickTree(a);
        while (q --> 0) {
            int query = nextInt();
            if (query == 0) {
                int p = nextInt();
                int x = nextInt();
                t.add(p, x);
            } else {
                int l = nextInt();
                int r = nextInt();
                sb.append(t.sum(l, r)).append('\n');
            }
        }
        pw.write(sb.toString());
        pw.flush();
        pw.close();
        sc.close();
    }

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

    static int[] nextInts(int n) {
        var dat = new int[n];
        Arrays.setAll(dat, i -> nextInt());
        return dat;
    }

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

    static long[] nextLongs(int n) {
        var dat = new long[n];
        Arrays.setAll(dat, i -> nextLong());
        return dat;
    }

    static class FenwickTree{
        private int _n;
        private long[] data;
    
        public FenwickTree(int n){
            this._n = n;
            data = new long[n];
        }
    
        public FenwickTree(long[] data) {
            this(data.length);
            build(data);
        }
    
        public void add(int p, long x){
            assert(0<=p && p<_n);
            p++;
            while(p<=_n){
                data[p-1] += x;
                p += p&-p;
            }
        }
        public long sum(int l, int r){
            assert(0<=l && l<=r && r<=_n);
            return sum(r)-sum(l);
        }
    
        private long sum(int r){
            long s = 0;
            while(r>0){
                s += data[r-1];
                r -= r&-r;
            }
            return s;
        }
    
        private void build(long[] dat) {
            System.arraycopy(dat, 0, data, 0, _n);
            for (int i=1; i<=_n; i++) {
                int p = i+(i&-i);
                if(p<=_n){
                    data[p-1] += data[i-1];
                }
            }
        }
    }
    
}


Measured memory_bytes: 80996