akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
 
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n)  for(int i=0;i<(n);++i)
#define REPr(i, n) for(int i=(n)-1;i>=0; --i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define FORqr(i, m, n) for(int i = (n);i >=(m);--i)
#define PB push_back
#define MP make_pair
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x,0,sizeof(x))
#define FILL(x, y) memset(x,y,sizeof(x))
#define MM(x) memset(x,-1,sizeof(x))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
/* (๑╹◡╹) */
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll MOD = 1000000007;
template<class T>
inline bool chmin(T &a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
 /* (๑╹◡╹)(๑╹◡╹)(๑╹◡╹)*/
template<class T>
inline bool chmax(T &a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
/* (๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹) */
///////////////////////////////////////////////

int main() {
   ll ans = 0;
   int K,N;
   cin >> N >> K;
   vector<ll> vec(N); //木の高さを格納
   rep(i,N)
   {
	   cin >> vec.at(i);
   }
   sort(ALL(vec));

   vector<ll> A;
   rep(i,N-K+1)
   {
	   A.PB(vec.at(K-1+i) - vec.at(i));
   }
   sort(ALL(A));
   ans = A.at(0);
   

   cout << ans << endl;
}

Measured memory_bytes: 2168
#define D(x) cout << #x " = " << x << endl
#define DD(x, y) cout << "(" #x ", " #y ") = (" << x << ", " << y << ")" << endl
#define DDD(x, y, z) cout << "(" #x ", " #y ", " #z ") = (" << x << ", " << y << ", " << z << ")" << endl
#define DV(v) for (auto __macro_vi : v){ cout << __macro_vi << " ";} cout << endl;
#define DA(a, n) for (int __macro_i = 0; __macro_i < n; ++__macro_i) { cout << a[__macro_i] << " "; } cout << endl;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(v) (v).begin(), (v).end()
#include<iostream>
#include<vector>
#include<cassert>
#include<cmath>
#include<algorithm>

using namespace std;

int read() {
    int h, m, s;
    char c;
    cin >> h >> c >> m >> c >> s;
    return h * 3600 + m * 60 + s;
}
using P = pair<int, int>;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1) {
        int n;
        cin >> n;
        if (n == 0) break;
        vector<P> v;
        REP(i, n) {
            v.emplace_back(read(), 1);
            v.emplace_back(read(), 0);
        }
        sort(v.begin(), v.end());
        int ans = 0;
        int cnt = 0;
        for (auto vi : v) {
            if (vi.second == 1) {
                cnt += 1;
            } else {
                cnt -= 1;
            }
            ans = max(ans, cnt);
        }
        cout << ans << endl;
    }
}



Measured memory_bytes: 3292
# 解説を見た。
# https://at274.hatenablog.com/entry/2020/01/24/060000

def main():
    num = int(input())
    counter = [[0] * 10 for _ in range(10)]
    for x in range(1, num + 1):
        x = str(x)
        head = int(x[0])
        tail = int(x[-1])
        counter[head][tail] += 1
    gen = (counter[head][tail] * counter[tail][head]
           for head in range(1, 10)
           for tail in range(1, 10))
    print(sum(gen))


if __name__ == '__main__':
    main()


Measured memory_bytes: 9156
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
long solve(string& k,int D,vector<vector<vector<long>>>& dp,int pos,int d,bool tight){
    if(dp[pos][d][tight]!=-1){
        return dp[pos][d][tight];
    }
     int ub;
    if(tight==1){
        ub=(k[pos]-'0');
    }
    else{
        ub = 9;
    }
    if(pos==(k.length()-1)){
    long long int ans = 0;
    for(int i=0;i<=ub;i++){
      if(i%D==d){
          ans++;
      }
    }
    return ans;
    }
    long long int ans =0 ;
    for(int i=0;i<=ub;i++){
     ans = (ans%mod + solve(k,D,dp,pos+1,(D + d -i%D)%D,(tight&&(i==ub)))%mod)%mod;
    }
    return dp[pos][d][tight] = ans;
    }
int main() {
	string k;
	cin>>k;
	int D;
	cin>>D;
	vector<vector<vector<long>>> dp(100001,vector<vector<long>>(101,vector<long>(2,-1)));
	cout<<(mod+solve(k,D,dp,0,0,1)-1)%mod<<'\n';
	return 0;
}


Measured memory_bytes: 558848
package main

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

var (
	N      int
	S      Names
	cnt    map[string]int
	maxCnt int
)

func readVariables() {
	N = nextInt()
	S = make(Names, 0)
	cnt = make(map[string]int)
}

type Names []string

func (n Names) Len() int {
	return len(n)
}
func (n Names) Less(i, j int) bool {
	return n[i] < n[j]
}
func (n Names) Swap(i, j int) {
	n[i], n[j] = n[j], n[i]
}

func main() {
	readVariables()
	for i := 0; i < N; i++ {
		name := nextStr()
		cnt[name]++
		maxCnt = MaxInt(cnt[name], maxCnt)
	}
	for k, v := range cnt {
		if v == maxCnt {
			S = append(S, k)
		}
	}
	sort.Sort(S)
	for _, v := range S {
		fmt.Println(v)
	}
}

/* 以下、テンプレート*/

var scanner *bufio.Scanner

func init() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 0, 1000000), 1000000)
	scanner.Split(bufio.ScanWords)
}

//nextInt converts next token from stdin and returns integer value.
//nextInt panics when conversion into an integer fails.
func nextInt() int {
	if !scanner.Scan() {
		panic("No more token.")
	}
	num, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic("nextInt(): cannot convert to int: " + scanner.Text())
	}
	return num
}

func nextStr() string {
	if !scanner.Scan() {
		panic("No more token.")
	}
	return scanner.Text()
}

// MinInt は、2つの整数を受け取り、最小値を返します。
func MinInt(x, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}

//MaxInt は、2つの整数を受け取り、最大値を返します。
func MaxInt(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

//AbsInt は、整数の絶対値を返します。
func AbsInt(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

//ModPow は整数の累乗関数で、剰余もサポートします。
//base^exponentの値をmoduleで割った余りを返します。
//moduloが1以下の場合には、剰余演算をしません。
//baseが負の値である場合には、返す値が負になることがあります。
func ModPow(base, exponent, modulo int) (result int) {
	result = 1
	for exponent > 0 {
		if exponent%2 == 1 {
			result *= base
			if modulo > 1 {
				result %= modulo
			}
		}
		base *= base
		if modulo > 1 {
			base %= modulo
		}
		exponent /= 2
	}
	return
}

//Gcd は、引数の整数全ての最大公約数を返します。
func Gcd(vals ...int) (result int) {
	if len(vals) == 0 {
		return
	}
	result = vals[0]
	for i := 1; i < len(vals); i++ {
		result = gcd(result, vals[i])
	}
	return
}

func gcd(x, y int) int {
	x, y = AbsInt(x), AbsInt(y)
	for y > 0 {
		x, y = y, x%y
	}
	return x
}

//Lcm は、与えられた整数の最小公倍数を返します。
func Lcm(vals ...int) (result int) {
	if len(vals) == 0 {
		return
	}
	result = vals[0]
	for i := 1; i < len(vals); i++ {
		result = lcm(result, vals[i])
	}
	return
}

func lcm(x, y int) int {
	return x * y / gcd(x, y)
}


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

#define ll long long int
#define str string

ll MOD = 1000000007,INFL = 1ll << 60,INF = 1 << 30;
ll A[10000000] = {}, X[10000000] = {};
int dy[]={0, 0, 1, -1},dx[]={1, -1, 0, 0};
vector <ll> v;
ll a = 0,b = 0,c = 0,n = 0,d = 0, max = INF;
str S;
int ans = 0;


int main(){
        cin.tie(0);
        ios::sync_with_stdio(false);
        //Code Line
cin >> a >> b >> c >> d;
cout << (a - c) * (b - d) << endl;


  //Code Line
        return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <array>
#include <cmath>
#include <iterator>
#include <utility>
#include <set>
#include <random>
#include <map>
#include <stack>
#include <queue>
#include <list>

using namespace std;


int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 5 && b == 5 && c == 7)
    {
        cout << "YES" << endl;
    }
    else if (a == 5 && b == 7 && c == 5)
    {
        cout << "YES" << endl;
    }
    else if (a == 7 && b == 5 && c == 5)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
// 2014/05/23 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int t;
		cin >> t;

		if(t==0)
			break;

		int n;
		cin >> n;

		int sum = 0;

		for(int i=0; i<n; i++){
			int s, f;
			cin >> s >> f;

			sum += (f-s);
		}

		if(sum>=t){
			cout << "OK" << endl;
		}
		else{
			cout << t-sum << endl;
		}
	}

	return 0;
}

Measured memory_bytes: 1160
const t={input:require('fs').readFileSync('/dev/stdin','utf8').split(/[\s]/),n:(arg:number=0):any=>{if(arg){const a:number[]=[];for(let i=0;i<arg;i++) {a.push(t.n());}return a;}else{const a = t.input[0];t.input.shift();return Number(a);}},s:(arg:number=0):any=>{if(arg){const a:string[]=[];for(let i=0;i<arg;i++){a.push(t.s());}return a;}else{const a=t.input[0];t.input.shift();return String(a);}},c:(arg:number=0):any=>{if(arg) {const a:string[]=[];for(let i=0;i<arg;i++) {a.push(t.c());}return a;}else{let a=t.input[0].split('');const b=a[0];a.shift();if(a.length>0){t.input[0]=a.join('');}else{t.input.shift();}return String(b);}},o: (...arg)=>{console.log(...arg);},abs:(a)=>{return Math.abs(a);},min:(a)=>{return Math.min(a);},max:(a)=>{return Math.max(a);},floor:(a)=>{return Math.floor(a);},asc:(ar:number[])=>{return ar.sort((a,b)=>{return a-b});},desc:(ar:number[])=>{ return ar.sort((a,b)=>{return b-a});},sw:(a,arr:any[],op:'=='|'>='|'>'|'<='|'<'='==')=>{for(let i=0;i<arr.length;i+=2){if(eval(a+op+arr[i]))return arr[i+1]}return arr[arr.length-1];}}
const main = ()=>{
  let [a,b] = t.n(2)
  t.o(2*b-a)
}
main()

Measured memory_bytes: 9420
#include <stdio.h>
#define N 100

int main(){
  int i,j,n,u,v,k;
  int aij[N][N];

  scanf("%d",&n);

  for(i=0; i<N; i++){
    
    for(j=0; j<N; j++) aij[i][j]=0;
    
  }


  for(i=0; i<n; i++){
    scanf("%d",&u);
    scanf("%d",&k);
    
    for(j=0; j<k; j++){
      scanf("%d",&v);
	aij[u-1][v-1]=1;
    }
  }

  for(i=0; i<n; i++){
    for(j=0; j<n-1; j++) printf("%d ",aij[i][j]);
  
      printf("%d",aij[i][n-1]);
    printf("\n");
  }

  return 0;
}

Measured memory_bytes: 644
INF = 2 * 10 ** 5
MOD = 10 ** 9 + 7
n = gets.to_i
colors = []
n.times { colors << gets.to_i }
previous_places = Array.new(INF + 1)
previous_places[colors.shift] = 0
dp = [1]
colors.each.with_index(1) do |color, index|
  previous_place = previous_places[color]
  if previous_place == index - 1 || !previous_place
    dp[index] = dp[index - 1]
  else
    dp[index] = dp[index - 1] + dp[previous_place]
  end
  previous_places[color] = index
end
puts dp.last % MOD


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

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

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

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


int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int a[5];
  rep(i, 5)cin >> a[i];
  int mini = 10;
  int at = -1;
  rep(i, 5){
    if(a[i] % 10 != 0 && mini > a[i] % 10){
      mini = a[i] % 10;
      at = i;
    }
  }
  int res = 0;
  rep(i, 5){
    if(i == at)res += a[i];
    else res += ((a[i] + 9) / 10) * 10;
  }
  cout << res << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
const long long INF = LLONG_MAX / 2;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for(int i=(ll)(r); i<(ll)(n); i++) 
#define REP(i, n) FOR(i, (0), n)
#define ALL(r) r.begin(), r.end()
#define ll long long int
using namespace std;
ll ans = 0, sum = 0, n, k, counter = 0, w, h;
string s;
vector<ll> v;
vector<pair<ll, ll>> vp;
vector<vector<ll>> vv(50, vector<ll>(50, INF));

int main() {
	cin >> n;
	v.resize(n);
	REP(i, n) cin >> v[i];
	cin >> k;
	REP(i,k)
	{
		ll a, b;
		cin >> a >> b;
		vp.emplace_back(a, b);
	}
	sum = accumulate(ALL(v),0);
	REP(i,vp.size())
	{
		cout << sum - v[vp[i].first-1] + vp[i].second << endl;
	}
}

Measured memory_bytes: 256
a=int(input())
b=list(input())
count=0
if(a%2==0):
    a=int(a/2)
    for i in range(a):
        if(b[i]==b[i+a]):
          count+=1
    if(count==a):
        print('Yes')
    else:
        print('No')
else:
    print('No')

Measured memory_bytes: 3060
n = int(input())
 
List = [int(i) for i in input().split()]
lst = sorted(List, reverse=True)
if lst[-1] == 0:
  print(0)
  exit()
total = 1
for i in lst:
  total = total*i
  if total > 1000000000000000000:
    print(-1)
    exit()
print(total)


Measured memory_bytes: 21648
import itertools

N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

memo = [[0 for i in range(C)]for j in range(3)]

for i in range(N):
    for j in range(N):
        cij = c[i][j]
        a = (i + j + 2) % 3
        for k in range(C):
            memo[a][k] += d[cij-1][k]

ans = 250000001

for c1, c2, c3 in itertools.permutations(range(C), 3):
    ans = min(ans, memo[0][c1] + memo[1][c2] + memo[2][c3])

print(ans)

Measured memory_bytes: 44456
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;

vector<long long> divisor(long long n){
    vector<long long> ret;
    for(long long i = 1; i * i <= n; i++){
        if(n % i == 0){
            ret.push_back(i);
            if(i * i != n) ret.push_back(n / i);
        }
    }
    sort(begin(ret), end(ret));
    return (ret);
}

signed main(){
    int n;
    ll m;
    cin >> n >> m;
    vector<ll> vec = divisor(m);
    REP(i,n){
        if(vec[i] >= n){
            cout << m / vec[i] << endl;
            return 0;
        }
    }
}

Measured memory_bytes: 256
fun main(args: Array<String>) {
    val (h, w, k) = readLine()!!.split(' ').map { it.toInt() }
    val s = (0 until h).map { readLine()!! }.toTypedArray()

    val ws = Array(h) { IntArray(w + 1) }

    for (i in 0 until h) {
        for (j in 0 until w) {
            ws[i][j + 1] = ws[i][j] + (s[i][j] - '0')
        }
    }

    var ans = 10000

    for (bits in 0 until (1 shl h - 1)) {

        val hs = run {
            val list = mutableListOf<Int>()
            list.add(0)
            list.addAll((0 until h - 1).filter { (bits shr it) and 1 == 1 }.map { it + 1 })
            list.add(h)
            list.toList()
        }

        var ok = true
        var cnt = 0

        var l = 0
        var r = 1

        while (r <= w) {
            var mx = 0
            for (i in 0 until hs.size - 1) {
                val x = (hs[i] until hs[i + 1]).sumBy { ws[it][r] - ws[it][l] }
                mx = Math.max(mx, x)
            }
            if (mx <= k) {
                r++
                continue
            }
            if (r - l == 1) {
                ok = false
                break
            }
            cnt++
            l = r - 1
        }
        if (ok) {
            ans = Math.min(ans, cnt + hs.size - 2)
        }
    }

    println(ans)
}


Measured memory_bytes: 82460
//的場梨沙★担当
//結城晴★副担当
//日野茜★副担当
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string.h>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>
#include <complex>
#include <regex>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stdio.h>


using namespace std;

int main(void) {
    int A,B;
    cin>>A>>B;

    cout<<A*B<<endl;
}


Measured memory_bytes: 3632
def cnt(s, b):
	ans = []
	for i in s:
		if i in b:
			ans.append(i)
	return ans

def resolve():
	n = int(input())
	h = []
	for i in range(n):
		h.append(input())
	ans = h[0]
	for i in range(1, n):
		sec = list(set(ans) & set(h[i]))
		tmp = ""
		for j in sec:
			tmp += j*min(ans.count(j), h[i].count(j))
		ans = tmp

	print("".join(sorted(ans)))
resolve()

Measured memory_bytes: 40816
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using MinPriorityQueue = priority_queue <T, vector <T>, greater <T>>;
 
#ifdef HOME
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template < typename Arg1 >
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std :: endl;
}
template < typename Arg1, typename... Args >
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    int len = comma - names;
    for (int i = 0; i < len; ++i) {
        cout << names[i];
    }
    cout <<  " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
 
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef complex<double> com;
 
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define unique_sort(x) (sort(x.begin(), x.end()), x.resize(distance(x.begin(), unique(x.begin(), x.end()))))
#define fast_io() ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
 
const ll LINF = LLONG_MAX, SZ = 1e3, MOD = 1e9 + 7, N = 1e5 + 5, M = 1e3 + 5, LG = 21;
const int INF = INT_MAX;
const db PI = acos(-1), EPS = 1e-6;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); 
void test() {
    int n;
    cin >> n;
    map <pair <int, int>, int > mp;
    for(int i = 1; i <= n; i++)
    {
        int x = i;
        int last_digit = x%10;
        int first_digit;
        while(x != 0)
        {
            int rem = x%10;
            x /= 10;
            first_digit = rem;
        }
        mp[{first_digit,last_digit}]++;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        int x = i;
        int last_digit = x%10;
        int first_digit;
        while(x != 0)
        {
            int rem = x%10;
            x /= 10;
            first_digit = rem;
        }
        ans += mp[{last_digit,first_digit}];
    }
    cout << ans << "\n";
}
 
int main() {
    fast_io();
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif
    cout << fixed << setprecision(15);
    int t = 1;
    // cin >> t;
    for (int i = 0; i < t; ++i) {
        test();
    }
    #ifdef HOME
        cout << "Time: " << (int)(clock() * 1000. / CLOCKS_PER_SEC) << "ms";
    #endif
    return 0;
}

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

long long n, k;
long long ans;

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	cin >> n >> k;
	if(k % 2 == 1){
		long long t = n / k;
		ans = t * t * t;
	}else{
		long long l = k / 2;
		long long t = n / l;
		ans = (t/2) * (t/2) * (t/2) + (t-t/2) * (t-t/2) * (t-t/2);
	}
	cout << ans << "\n";
}

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

using namespace std;

int main() {
  
  int N; cin >> N;
  
  int *arr = new int[N];
  for(int i=0; i<N; i++) {
    cin >> arr[i];
  }
  
  int cnt = 0;
  for(int i=0; i<N; i++) {
    for(int j=N-1; j>i; j--) {
      if(arr[j-1] > arr[j]) {
        swap(arr[j-1], arr[j]);
        cnt ++;
      }
    }
  }
  
  for(int i=0; i<N; i++) {
    if(i) cout << " ";
    cout << arr[i];
  }
  cout << endl;
  
  cout << cnt << endl;
  
  return 0;
}

Measured memory_bytes: 1192
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import itertools
n,m,q = iim()

ans = 0
l = []
for i in range(q):
    l.append(iil())

for i in itertools.combinations_with_replacement(list(range(1,m+1)),n):
    sc = 0
    for j in l:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            sc += j[3]
    ans = max(ans,sc)
print(ans)

Measured memory_bytes: 68004
// 縦型探索
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int w, h;
int mx[] = {0, 0, -1, 1};
int my[] = {-1, 1, 0, 0};

int dfs(int x, int y, vector<string> &v) {
    int cnt = 1;
    for ( int i = 0; i < 4; i++ ) {
        int nx = x + mx[i], ny = y + my[i];
        if ( 0 <= nx && nx < w && 0 <= ny && ny < h && v[ny][nx] == '.' ) {
            v[ny][nx] = '#';
            cnt += dfs(nx, ny, v);
        }
    }
    return cnt;
}

int main() {
    while ( true ) {
        int x, y;
        cin >> w >> h;
        if ( w == 0 && h == 0 ) break;

        vector<string> v(h);

        for ( int i = 0; i < h; i++ ) {
            cin >> v[i];

            for ( int j = 0; j < v[i].size(); j++ ) {
                if ( v[i][j] == '@' ) {
                    y = i;
                    x = j;
                }
            }
        }

        cout << dfs(x, y, v) << endl;
    }
    return 0;
}

Measured memory_bytes: 1236
n = int(input())
hh = [int(i) for i in input().split()]

dp = [float("inf")] * (n)
dp[0] = 0

for i in range(n-2):
    dp[i+1] = min(dp[i+1], dp[i] + abs(hh[i+1] - hh[i]))
    dp[i+2] = min(dp[i+2], dp[i] + abs(hh[i+2] - hh[i]))

i = n - 2
dp[i+1] = min(dp[i+1], dp[i] + abs(hh[i+1] - hh[i]))

print(dp[n-1])


Measured memory_bytes: 52424
fun main(arr: Array<String>) {
    val s = readLine()!!.toInt()
    val log = mutableMapOf<Long, Int>(s.toLong() to 1)
    var prev = s.toLong()
    for(i in (2..1000000)) {
        val num = when(prev % 2) {
            0L -> {
                prev / 2
            }
            else -> {
                prev * 3 + 1
            }
        }
        log[num]?.let {
            println(i)
            return
        }
        log[num] = i
        prev = num
    }
}


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

int main(void) {
    int N, K, X, Y;
    cin >> N >> K >> X >> Y;

    int result = 0;

    if (N <= K) {
        result = N * X;
    } else {
        result = K * X + (N - K) * Y;
    }

    cout << result << endl;

    return 0;
}


Measured memory_bytes: 256
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner scan = new Scanner(System.in);

		int N = scan.nextInt();
		int D = scan.nextInt();

		long[] X = new long[N];
		long[] Y = new long[N];

		for (int i = 0; i < N; i++) {
			X[i] = scan.nextInt();
			Y[i] = scan.nextInt();
		}

		int ans = 0;

		for (int i = 0; i < N; i++) {
			double distance = Math.sqrt((X[i] * X[i]) + (Y[i] * Y[i]));

			if (distance <= D) {
				ans++;
			}
		}

		System.out.println(ans);
	}

	//***********************以下共通関数************************************************
	/**
	 * 文字列を反転して返す
	 * @param pStr 文字列
	 * @return 反転した文字列
	 */
	public static String myReverse(String pStr) {
		if (pStr.length() < 1) {
			return "";
		}

		StringBuffer sb = new StringBuffer(pStr);
		return sb.reverse().toString();
	}

	/**
	 * 余りを求める。
	 * @param a
	 * @param b
	 * @return 余り。余りがマイナスの時はプラスで返す。
	 */
	public static long myMod(long a, long b) {
		long ret = 0;

		ret = a % b;

		if (ret < 0) {
			ret += b;
		}

		return ret;
	}

	/**
	 * 乗数を求める ※mod対応
	 * @param kisu 基数
	 * @param josu 乗数
	 * @param primeNumMod_0 法 mod値 使わない場合は「0」
	 * @return
	 */
	public static long myPowWithMod(long kisu, long josu, long primeNumMod_0) {

		long ans = 1;

		//乗数が0の場合→1
		if (josu == 0)
			return 1;

		if (primeNumMod_0 == 0) {
			//modを使わないとき

			while (josu > 0) {

				if (josu % 2 == 0) {
					//累乗が偶数の時

					kisu *= kisu;
					josu = josu / 2;
				} else {
					//累乗が奇数の時
					ans *= kisu;
					josu--;
				}
			}

			return ans;

		} else {
			//modを使うとき

			//初めに桁を落とす
			kisu = myMod(kisu, primeNumMod_0);

			while (josu > 0) {

				if (josu % 2 == 0) {
					//累乗が偶数の時

					kisu *= myMod(kisu, primeNumMod_0);
					josu = josu / 2;

				} else {
					//累乗が奇数の時
					ans *= myMod(kisu, primeNumMod_0);
					josu--;

				}
			}

			return myMod(ans, primeNumMod_0);

		}

	}

}


Measured memory_bytes: 52996
#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string t; cin >> t;
    int n = t.size();
    if (n == 1) {
        if (t == "?") cout << "D\n";
        else cout << t << '\n';
        return 0;
    }
    for (int i = 0; i < n; i++) {
        if (t[i] == '?') {
            if (i > 0 && t[i - 1] == 'P') t[i] = 'D';
            else if (i > 0 && t[i - 1] == '?') t[i - 1] = 'P', t[i] = 'D';
            else if (i < n - 1 && t[i + 1] == 'D') t[i] = 'P';
            else if (i < n - 1 && t[i + 1] == '?') {
                t[i] = 'P', t[i + 1] = 'D';
                i++;
            } else t[i] = 'D';
        }
    }
    cout << t << '\n';
    return 0;
}


Measured memory_bytes: 3784
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define int long long
#define INF 10000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template<typename T>
void readvec(vector<T> &a);
void readindex(vector<int> &a);

#define MAX_V 20000
struct edge { int to, cap, rev; };
vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap){
    G[from].push_back((edge){to, cap, G[to].size()});
    G[to].push_back((edge){from, 0, G[from].size() - 1});
}

int dfs(int v, int t, int f){
    if(v == t) return f;
    used[v] = true;
    REP(i, G[v].size()){
        edge &e = G[v][i];
        if(!used[e.to] && e.cap > 0){
            int d = dfs(e.to, t, min(f, e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t){
    int flow = 0;
    while(true){
        memset(used, 0, sizeof(used));
        int f = dfs(s, t, INF);
        if(f == 0) return flow;
        flow += f;
    }
}

signed main(){

    int H, W; cin >> H >> W;
    int v = 0, s, t, is, js, it, jt;
    char a;
    mat index(H, vec(W, -1));
    REP(i, H){
        REP(j, W){
            cin >> a;
            if(a == '.') continue;
            if(a == 'S'){
                s = v;
                is = i;
                js = j;
            }
            if(a == 'T'){
                t = v;
                it = i;
                jt = j;
            }
            index[i][j] = v;
            add_edge(v, v + 1, 1);
            v += 2;
        }
    }

    if(is == it || js == jt){
        cout << -1;
        return 0;
    }

    REP(i, H){
        REP(j, W){
            if(index[i][j] == -1) continue;
            REP(i0, H) if(i0 != i && index[i0][j] != -1) add_edge(index[i][j] + 1, index[i0][j], INF);
            REP(j0, W) if(j0 != j && index[i][j0] != -1) add_edge(index[i][j] + 1, index[i][j0], INF);
        }
    }

    cout << max_flow(s + 1, t);
    
    return 0;
}


template<typename T>
void readvec(vector<T> &a){
    REP(i, a.size()){
        cin >> a[i];
    }
}
void readindex(vector<int> &a){
    REP(i, a.size()){
        cin >> a[i];
        a[i]--;
    }
}

Measured memory_bytes: 125440
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;

const int MOD = (int)1e9 + 7;

int main() {
	char a[10], b[10], c[10];
	scanf("%5s,%7s,%5s", a, b, c);
	printf("%s %s %s\n", a, b, c);
}


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

int main(){
  string s,t;
  int flg=0;
  cin >> s;
  cin >> t;
  
  for(int i=0;i<s.size();i++){
    if(s.at(i)!=t.at(i)) flg=1;
  }
  if(flg==0) cout << "Yes" << endl;
  else cout << "No" << endl;
}

Measured memory_bytes: 3632
import sys
sys.setrecursionlimit(1000000)

N=int(input())

B=[[]for i in range(N+1)]

F=[10**10 for i in range(N+1)]
F[1]=0
S=[10**10 for i in range(N+1)]
S[N]=0

for i in range(N-1):
    a,b=map(int,input().split())
    B[a].append(b)
    B[b].append(a)
    
#print(B)
def dfsF(f):
    for i in B[f]:
        if F[i]==10**10:
            F[i]=F[f]+1
            dfsF(i)
dfsF(1)

def dfsS(s):
    for i in B[s]:
        if S[i]==10**10:
            S[i]=S[s]+1
            dfsS(i)
dfsS(N)
#print(F)
#print(S)
Fennec=0
Snuke=0
for i in range(1,N+1):
    if F[i]<=S[i]:
        Fennec+=1
    else:
        Snuke+=1
if Fennec>Snuke:
    print("Fennec")
else:
    print("Snuke")


Measured memory_bytes: 209456
void main() {
  problem();
}

void problem() {
  auto N = scan!int;
  auto STONES = scan!string.to!(char[]);

  long solve() {
    long left = 0;
    long right = N-1;
    long ans;

    while(true) {
      if (STONES[right] != 'R') right--;
      if (STONES[left] != 'W') left++;
      if (right <= left) break;

      if (STONES[right] == 'R' && STONES[left] == 'W') {
        right--;
        left++;
        ans++;
      }
    }
    
    return ans;
  }

  solve().writeln;
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");

// -----------------------------------------------


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

using namespace std;

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

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define repr(i,b,a) for(int i=int(b);i>=(a);i--)
#define all(x) x.begin(),x.end()

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

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool valid(int lx,int ux,int ly,int uy,int x,int y){
	return lx<=x&&x<ux&&ly<=y&&y<uy; 
}
ll power(ll x,ll p){
	if(p==0)return 1;
	ll res=power(x*x%mod,p/2);
	if(p%2==1)res=res*x%mod;
	return res;
}

int main()
{
	string s;
	cin>>s;
	int left=0,right=s.size()-1;
	int ans=0;
	while(left<=right){
		if(s[left]!=s[right])ans++;
		left++;
		right--;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
int main(){
    ll n,k,A[200001],MOD=1e9+7,ans=0;
    cin>>n>>k;
    rep(i,n) cin>>A[i];
    ll B[200001]={0},C[200001]={0};
    rep(i,n)rep(j,n) if(i<j&&A[i]>A[j]) B[i]++;
    rep(i,n)rep(j,n) if(A[i]>A[j]) C[i]++;
    ll b=0,c=0;
    rep(i,n) b+=B[i]%MOD,c+=C[i]%MOD;
    b=b%MOD,c=c%MOD;
    ans=(ans+b*k)%MOD;
    ans=(ans+(k*(k-1)/2)%MOD*c)%MOD;
    cout<<ans;
}

Measured memory_bytes: 4992
while True:
	try:
		s1 = raw_input()
		s2 = raw_input()
		a = map(int, s1.split())
		b = map(int, s2.split())
		hashMap = {}
		hit = 0
		blow = 0
		for i in range(len(a)):
			hashMap[a[i]] = i
		for i in range(len(b)):
			if b[i] in hashMap:
				if hashMap[b[i]] == i:
					hit += 1
				else:
					blow += 1
		print '%s %s' % (hit, blow)
	except (EOFError):
		break

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

using namespace std;
#define sqr(x) ((x)*(x))
#define cube(x) ((x)*(x)*(x))
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) ((a)*(b)/GCD(a,b))
#define PI (3.141592653589793238462643383279502884197169399375105820974944)
#define MP make_pair
#define n1 first
#define n2 second
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define SI set<int>
#define SL set<ll>
#define MS multiset
#define MSI multiset<int>
#define MSLL multiset<ll>
#define PB push_back
#define PF push_front
#define VI vector<int>
#define VPI vector<pair<int,int>>
#define VLL vector<ll>
#define VS vector<string>
#define SZ(x) ((int)x.size())

typedef long long ll;

typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const int N = 250555;
const int M = 1155;

VS v[N];
void solve() {
    int n;
    cin >> n;

    map <string, int> m;
    for (int i = 1; i <= n; i++){
        string s;
        cin >> s;
        m[s]++;
    }

    for (auto now : m){
        string x = now.n1;
        int y = now.n2;
        v[y].PB(x);
    }

    for (int i = n; i >= 1; i--){
        if (v[i].empty()) continue;
        sort(v[i].begin(), v[i].end());
        for (auto now : v[i]){
            cout << now << endl;
        }
        return;
    }

}

int main() {
    ios_base::sync_with_stdio(false);
#ifdef dxz05
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else

#endif

    int T = 1;
    //cin >> T;

    while (T--) {
        solve();
    }

    return 0;
}

Measured memory_bytes: 31472
#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

struct spell {
	char t, d;
	spell() : t(' '), d(' ') {}
	spell(char t, char d) : t(t), d(d) {}
};

int N, Q;
string s;
vector<spell> spells;

// マスstのゴーレムが最終的に至る座標
int final_idx(int st) {
	if (st < 0) return -1;
	if (st > N) return N;
	for (int i = 0; i < Q; ++i) {
		if (s[st] == spells[i].t) { // 移動の対象
			if (spells[i].d == 'L') --st;
			else ++st;
		}
		if (st < 0) return -1;
		if (st >= N) return N;
	}
	return st;
}

int main() {
	cin >> N >> Q;
	spells.resize(Q);
	cin >> s;
	for (int i = 0; i < Q; ++i) {
		char t, d; cin >> t >> d;
		spells[i] = spell(t, d);
	}
	// 左端から落下しない最小のindexを求める
	int fall = -1, safe_left = N;
	while (safe_left - fall > 1) {
		int mid = (fall + safe_left) / 2;
		int tmp = final_idx(mid);
		if (tmp == -1) fall = mid;
		else safe_left = mid;
	}
	// 右端から落下しない最大のindexを求める
	fall = N; int safe_right = -1;
	while (fall - safe_right > 1) {
		int mid = (fall + safe_right) / 2;
		int tmp = final_idx(mid);
		if (tmp == N) fall = mid;
		else safe_right = mid;
	}
	// cout << safe_right << " " << safe_left << endl;
	cout << (safe_right - safe_left + 1) << endl;
	return 0;
}

Measured memory_bytes: 1024
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#include <bits/stdc++.h>
#define pb(X) push_back(X)
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
using namespace std;

int n, a, b, c, d, q, ans;

int main() {
	INIT
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	ans = 1;
	b = a[0];
	for (int i = 1; i < n; ++i) {
		if (a[i] >= b) {
			b = a[i];
			++ans;
		}
	}
	cout << ans;
	return 0;
}

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

int main(){
    char c;
    cin >> c;
    char d = c+1;
    cout << d << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<char> Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }


int main()
{
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  string s,t;
  cin >> s >> t;
  rep(i,N)
  {
    if(s.substr(i,N-i)==t.substr(0,N-i))
    {
      cout << 2 * N - (N-i) << endl;
      return 0;
    }
  }
  cout << 2 * N << endl;
  return 0;
}


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

ll n, m, ans;

int main() {
    cin >> n >> m;
    if (n == 1 && m == 1) ans = 1;
    else if (n == 1 || m == 1) ans = max(n, m) - 2;
    else ans = (n - 2) * (m - 2);
    cout << ans << endl;

    return 0;
}

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

int main(){
  int n;
  cin>>n;
  for(int i=1; i<=9; i++){
    if(n%i==0 && 1<=n/i && n/i<=9){
      cout << "Yes" <<endl;
      return 0;
    }
  }
  cout << "No"<<endl;
}

Measured memory_bytes: 256
//自称高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INF 1999999999
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007 

int main() {
    int ans = 0;
	int a, b;
	cin >> a >> b;
    cout << (a-1)*(b-1) << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define MAX 3000000
int n,size;
vector<int> A;
unordered_map<int,int> mp;
vector<ll> bit;
vector<P> tree;

void update(int a,int b,P x,int k,int l,int r){
  if(b<=l || r<=a)return;
  if(a<=l && r<=b){
    tree[k]=max(tree[k],x);
  }else{
    int m=(l+r)/2;
    update(a,b,x,k*2+1,l,m);
    update(a,b,x,k*2+2,m,r);
  }
}

void Upd(int a,int b,P x){
  a+=2;
  b+=2;
  if(a<b){
    update(a,b+1,x,0,0,size);
  }else{
    update(a,size,x,0,0,size);
    update(0,b+1,x,0,0,size);
  }
}

ll Get(int i){
  i+=2;
  i+=size-1;
  P res=tree[i];
  while(i){
    i=(i-1)/2;
    res=max(res,tree[i]);
  }
  return res.second;
}

void Add(int i,ll x){
  i+=2;
  while( i<(int)bit.size() ){
    bit[i]+=x;
    i+=(i&-i);
  }
}

ll Sum(int i){
  i+=2;
  ll res=0;
  while(i>0){
    res+=bit[i];
    i-=(i&-i);
  }
  return res;
}

ll Sum(int a,int b){
  if(a<b){
    return Sum(b)-Sum(a-1);
  }else{
    return Sum(n)-Sum(a-1)+Sum(b);
  }
}


ll solve(){
  vector<int> X;
  for(int i=0;i<(int)A.size();i++)
    X.push_back(A[i]);
  sort(X.begin(),X.end());
  
  
  size=1;
  while( size < n+5 ) size*=2;
  bit.clear();
  bit.resize(size);
  tree.clear();
  tree.resize(size*2);
  mp.clear();
  
  for(int i=0;i<(int)A.size();i++){
    A[i]= lower_bound( X.begin(), X.end(), A[i]) -X.begin() + 1;
    Upd(i,i+1,P(0,i));
    Add(i,1);
    mp[ A[i] ]=i;
  }
  
  int p=-1;
  ll res=0;
  for(int i=1;i+1<=n;i++){

    int num=mp[i];
    int target=Get(num);
    
    Add(num,-1);
    res+=Sum(p,target);
    /*
    for(int j=0;j<n;j++){
      if(A[j]<i)cout<<'-';
      else cout<<A[j];
      cout<<' ';
    }
    cout<<endl;
    for(int j=0;j<n;j++)cout<<Get(j)<<' ';cout<<endl;

        cout<<i<<"  p="<<p<<"  target="<<target<<"  res="<<res<<endl;

    cout<<endl;
    */
    Upd(p,target,P(i,target));
    p=target;
  }
  return res;
}

int main(){
  while(cin>>n&&n){
    A.clear();
    A.resize(n);
    for(int i=0;i<n;i++)cin>>A[i];
    cout<<solve()<<endl;
  }
  return 0;
}

Measured memory_bytes: 22884
// This amazing code is by Eric Sunli Chen.
#include<bits/stdc++.h>
using namespace std;
template<typename T> bool get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar());
	if(t=='-')neg=true,t=getchar();if(t==EOF)return false;
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;return true;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) (get1(a)&&get1(b))
#define get3(a,b,c) (get1(a)&&get2(b,c))
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);
#define kill hehe
int n,m,x[100111],y[100111];
bool kill[411];
bitset<411> a[411];
int main()
{
	get2(n,m);
	for(int i=1;i<=m;i++)get2(x[i],y[i]);
	for(int i=1;i<=n;i++)
	{
		a[i].set(i);
		for(int j=m;j>=1;j--)
		{
			int s=a[i].test(x[j])+a[i].test(y[j]);
			if(s==2)
			{
				kill[i]=1;
				break;
			}
			else if(s==1)
			{
				a[i].set(x[j]);
				a[i].set(y[j]);
			}
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++)if(!kill[i]&&!kill[j]&&(a[i]&a[j]).none())ans++;
	printendl(ans);
	return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
int h,w;
int m[400][400];
set<pair<long long,long long>> used;

pair<int,int> dfs(int x, int y, pair<long long,long long> ret){
    if(used.find(make_pair(x,y)) != used.end()) return ret;
    used.insert(make_pair(x,y));
    if(m[x][y]==0) ret.first++;
    else ret.second++;
    for(int i=-1; i<2; i+=2){
        if(x+i < 0 || x+i >= h) continue;
        if(m[x+i][y] != m[x][y]){
            ret = dfs(x+i,y,ret);
        }
    }
    for(int i=-1; i<2; i+=2){
        if(y+i < 0 || y+i >= w) continue;
        if(m[x][y+i] != m[x][y]){
            ret = dfs(x,y+i,ret);
        }
    }
    return ret;
}

int main(){
    cin >> h >> w;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            char c;
            cin >> c;
            if(c=='.') m[i][j] = 0;
            else m[i][j] = 1;
        }
    }
    long long ans = 0;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            pair<long long,long long> t = dfs(i,j,make_pair(0,0));
            ans += t.first*t.second;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 28416
#pragma GCC optimize("Ofast")
#include<stdio.h>
#include<vector>
#include<utility> 
#include<queue> 
#include<algorithm>
using namespace std;
long long a[1005],b[1005],c[1005],d[1005],e[1005],f[1005],dir[5]={-1,0,1,0,-1};
bool used[3005][3005]={0},up[3005][3005]={0},down[3005][3005]={0},left[3005][3005]={0},right[3005][3005]={0}; 
vector<long long> x,y;
queue<pair<long long,long long>> que;
long long id_x(long long v){
	return lower_bound(x.begin(),x.end(),v)-x.begin();
}
long long id_y(long long v){
	return lower_bound(y.begin(),y.end(),v)-y.begin();
}
int main(){
	x.emplace_back(100000000000000000);
	x.emplace_back(-100000000000000000);
	y.emplace_back(100000000000000000);
	y.emplace_back(-100000000000000000);
	long long i,j,n,m;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++){
		scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);
		x.emplace_back(a[i]);
		x.emplace_back(b[i]);
		y.emplace_back(c[i]);
	}
	for(i=0;i<m;i++){
		scanf("%lld%lld%lld",&d[i],&e[i],&f[i]);	
		x.emplace_back(d[i]);
		y.emplace_back(e[i]);
		y.emplace_back(f[i]);
	}
	sort(x.begin(),x.end());
	sort(y.begin(),y.end());
	x.erase(unique(x.begin(),x.end()),x.end());
	y.erase(unique(y.begin(),y.end()),y.end());
	for(i=0;i<n;i++){
		a[i]=id_x(a[i]);
		b[i]=id_x(b[i]);
		c[i]=id_y(c[i]);
		for(j=a[i];j<b[i];j++){
			up[j][c[i]-1]=1;
			down[j][c[i]]=1;
		}
	}
	for(i=0;i<m;i++){
		d[i]=id_x(d[i]);
		e[i]=id_y(e[i]);
		f[i]=id_y(f[i]);
		for(j=e[i];j<f[i];j++){
			right[d[i]-1][j]=1;
			left[d[i]][j]=1;
		}
	}
	que.push({id_x(0)-1,id_y(0)-1});
	used[id_x(0)-1][id_y(0)-1]=1;
	while(que.size()){
		auto z=que.front();
		que.pop();
		for(i=0;i<4;i++){
			if((!i&&left[z.first][z.second])||(i==1&&up[z.first][z.second])||(i==2&&right[z.first][z.second])||(i==3&&down[z.first][z.second]))
				continue;
			long long r=z.first+dir[i],c=z.second+dir[i+1];
			if(r<0||c<0||r>=(int)x.size()-1||c>=(int)y.size()-1||used[r][c])
				continue;
			used[r][c]=1;
			que.push({r,c});
		} 
	}
	for(i=0;i<(int)x.size();i++){
		if(used[i][0]||used[i][y.size()-1]){
			puts("INF");
			return 0;
		}
	}
	for(i=0;i<(int)y.size();i++){
		if(used[0][i]||used[x.size()-1][i]){
			puts("INF");
			return 0;
		}
	}
	long long ans=0;
	for(i=0;i<(int)x.size()-1;i++)
		for(j=0;j<(int)y.size()-1;j++)
			if(used[i][j])
				ans+=(x[i+1]-x[i])*(y[j+1]-y[j]);
	printf("%lld\n",ans);
}


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

using namespace std;

typedef long long ll;

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

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


void divs(ll &d,ll &v){
	for(ll i = 2; i <= min(d,v); i++){
		if(d%i==0&&v%i==0){
			d/=i;
			v/=i;
			i=1;
		}
	}
}

int main(){

	ll n;
	while(cin>>n&&n!=0){
		ll d,v;
		vector<ll> ds;
		vector<ll> vs;
		bool oo=false;
		for(ll i = 0; i < n; i++){
			cin>>d>>v;

			divs(d,v);


			ds.push_back(d);
			vs.push_back(v);
		}


		ll z=1;
		ll nums[11];
		ll allv=1;

		bool same=true;
		for(ll i = 1; i < n; i++){
			ll n1=ds[i-1]*vs[i];
			ll n2=ds[i]*vs[i-1];
		//	ll ret=lcm(n1,n2);
			ll ret=n1*n2;
			ll r1=ret/n1;
			ll r2=ret/n2;
			if(ds[i-1]==ds[i]&&vs[i-1]==vs[i]){
				z=lcm(z,1);
				same=true;
			}
			else{
			//	if(vs[i-1]!=0)
			//	if(r1*ds[i-1]<vs[i-1]){
				//	z=1;
			//	}
			//	else{
					z=lcm(z,r1*ds[i-1]/vs[i-1]);
			//	}
//				else
//					z=1;
				same=false;
			}
		}
		vector<ll> vll;
		ll m;
		if(!same){
			for(ll i = 0; i < n; i++){
				//if(ds[i]!=0){
					vll.push_back(z*vs[i]/ds[i]);
					if(i==0){
						m=z*vs[i]/ds[i];
					}
					else{
						m=gcd(m,z*vs[i]/ds[i]);
					}
			///	}
			//	else{
				//	vll.push_back(1);
				//	m=1;
			//	}
			}
			for(int i = 0; i < n; i++){
				cout<<vll[i]/m<<endl;
			}
		}
		else{
			for(int i = 0; i < n; i++){
				cout<<1<<endl;
			}
		}
	}

	return 0;
}

Measured memory_bytes: 916
#include <bits/stdc++.h>
using namespace std;
int n;
long long h[100100], dp[100100];
int main(){
   ios::sync_with_stdio(0);
   cin >> n;
   for (int i=2; i<=n+1; i++) cin >> h[i];
   dp[2] = 0;
   dp[3] = abs(h[3] - h[2]);
   for (int i=4; i<=n+1; i++) dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]), dp[i-2]+abs(h[i]-h[i-2]));
//   for (int i=2; i<=n+1; i++) cout << dp[i] << ' ';
  // cout << endl;
   cout << dp[n+1] << endl;
   return 0;
}

Measured memory_bytes: 1792