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

int main() {
 int A, B, C, D; 
 cin >> A >> B >> C >> D;
  
  int lower = max(A, C);
  int upper = min(B, D);
    
    if (lower > upper) cout << 0 << endl;
 	else cout << upper - lower << endl;
}

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

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll ;

const ll inf = 1e9 + 17;
const int maxn = 1e5 + 10;
const int maxq = 5e2 + 10;
const int alf = 26;
const ll dlm = 1e9 + 7;
const int del = 998244353 ;
const int eps = 1e-7 ;


int dp[maxn];
int a[maxn];

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    dp[1] = 0;
    for(int i=2;i<=n;i++){
        dp[i] = inf;
        for(int j=1;j<=k;j++){
            if((i-j)<=0)break;
            dp[i] = min(dp[i], ( dp[i-j]+abs(a[i]-a[i-j]) ) );
        }
    }
    cout<<dp[n];
    return 0;
}


Measured memory_bytes: 1024
main = do
--    s <- map read . words <$> getLine ::IO [Int] 
    n <- readLn ::IO Int
    print $ solve n

solve :: Int -> Int
solve n
    | n < 3 = n
    | query n == [] = solve (n-1)
    | sum (query n) == 0 = n
    | otherwise = solve (n-1)


query :: Int -> [Int]
query n = map (`mod` 2) $ filter (\x -> n `mod` x ==0)[2..n-1] 




Measured memory_bytes: 508
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int INF = 1<<30;

int main(){
  int a, b;cin>>a>>b;int ans=0;
  for(int i=a;i<=b;i++){
    int d1, d2, d4, d5;
    d1=i/10000;
    d2=i/1000-d1*10;
    d5=i%10;
    d4=(i%100-d5)/10;
    if(d1==d5 && d2==d4)ans++;
  }
  cout << ans << endl;
}


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

using namespace std;

int main(void){
  int N;
  cin >> N;
  string str;
  cin >> str;

  for(int i = 0; i < str.size(); i++)
    str[i] = 'A' + (((str[i] - 'A') + N) % 26);

  cout << str << endl;
  
  return 0;
}


Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
 
public class Program
{
 
    public static void Main()
    {
        int N = int.Parse(Console.ReadLine());
        int sq = 0;
        int ans = 0;
        while (true)
        {
            sq = Convert.ToInt32(Math.Round(Math.Sqrt(N)));
            if (sq * sq == N) { ans = N; break; }
            N--;
        }
        Console.WriteLine(ans);
    }
 
}

Measured memory_bytes: 13268
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    a = [0] * N

    for i in range(N):
        a[i] = I()

    cur = 0
    for i in range(N):
        cur = a[cur]-1
        if cur == 1:
            print(i+1)
            return

    print(-1)


if __name__ == '__main__':
    main()


Measured memory_bytes: 42856
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SHIFT_LEFT(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
ll dist[333][333];
ll ori[333][333];
ll n;
void warshall_floyd(){
  for(int k=0;k<n;k++){
    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
	dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
      }
    }
  }
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>dist[i][j];
      ori[i][j] = dist[i][j];
    }
  }
  warshall_floyd();
  for(int i = 0;i < n;i++){
    for(int j = i+1;j < n;j++){
      if(ori[i][j] != dist[i][j]){
	cout<<-1<<endl;
	return 0;
      }
    }
  }
  ll ans = 0;
  for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
      bool fl=true;
      for(int k=0;k<n;k++){
	if(i == k || j == k)continue;
	if(dist[i][j] == dist[i][k] + dist[k][j])
	  fl = false;
      }
      if(fl){
	ans += dist[i][j];
      }
    }
  }

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


Measured memory_bytes: 1792
#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define put(a) cout<<a<<"\n"
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }

class UnionFind {
private:
	vector<int> par, height, size;
public:
	UnionFind(int n) {
		par.resize(n);
		REP(i, n) {
			par[i] = i;
		}
		height.resize(n, 0);
		size.resize(n, 1);
	}
	int root(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			par[x] = root(par[x]);
			return par[x];
		}
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y)return;
		if (height[x] < height[y]) {
			par[x] = y;
			size[y] += size[x];
		}
		else {
			par[y] = x;
			size[x] += size[y];
			if (height[x] == height[y])height[x]++;
		}
	}
	int cnt(int x) {
		x = root(x);
		return size[x];
	}
};

int main() {
	int h, w;
	cin >> h >> w;
	vector<vector<char>> s(h, vector<char>(w));
	REP(i, h) {
		REP(j, w) {
			cin >> s[i][j];
		}
	}
	vector<UnionFind> ufh(w, UnionFind(h));
	vector<UnionFind> ufw(h, UnionFind(w));
	REP(i, h) {
		REP(j, w-1) {
			if (s[i][j] == '.' && s[i][j + 1] == '.') {
				ufw[i].unite(j, j + 1);
			}

		}
	}
	REP(j, w) {
		REP(i, h-1) {
			if (s[i][j] == '.' && s[i + 1][j] == '.') {
				ufh[j].unite(i, i + 1);
			}

		}
	}
	int res = 0;
	REP(i, h) {
		REP(j, w) {
			if (s[i][j] == '.') {
				chmax(res, ufw[i].cnt(j) + ufh[j].cnt(i));
			}
		}
	}
	put(res-1);
	/*REP(i, h) {
		s[i][w] = '#';
	}
	REP(i, w) {
		s[h][i] = '#';
	}
	vector<vector<int>> cntv(h, vector<int>(w, 0));
	vector<vector<int>> cnth(h, vector<int>(w, 0));
	bool con = false;
	int temp = 0;
	int prev = 0;
	REP(i, h) {
		REP(j, w+1) {
			if (s[i][j] == '#') {
				FOR(k, prev, j) {
					cnth[i][k] = temp;
				}
				temp = 0;
				prev = j+1;
			}
			else {
				temp++;
			}
		}
	}
	temp = 0;
	REP(j, w) {
		REP(i, h + 1) {
			if (s[i][j] == '#') {
				FOR(k, prev, i) {
					cnth[k][j] = temp;
				}
				temp = 0;
				prev = i+1;
			}
			else {
				temp++;
			}
		}
	}
	int res = 0;
	REP(i, h) {
		REP(j, w) {
			chmax(res, cntv[i][j] + cnth[i][j]);
		}
	}
	put(res);*/
	return 0;
}


Measured memory_bytes: 98432
#include <bits/stdc++.h>
#include <math.h>
#include <cmath>

using namespace std;
using ll =long long;
using vi = vector<int>; 
using vvi = vector<vi>; 
using vl = vector<ll>; 
using vvl = vector<vl>; 
using vs = vector<string>;
using vvs = vector<vs>; 
using vc = vector<char>;
using vvc = vector<vc>; 
using vb = vector<bool>;
using vvb = vector<vb>; 

#define rep(i,n) ;for( int i =0; i < n ; i ++)
#define all(a) a.begin(),a.end()
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
#define pri(a)  printf("%.14lf\n",a);
#define MOD 1000000007
bool is_int_lround(double x){
  return std::lround(x)==x;
}
ll keta(ll x){
  ll n=0;
  while(x>0){
    x /=10 ;
    n ++;
  }
  return n;
}

ll conbi(int n,int m){
  
  cin>>n>>m;
  vector<ll> a(100);
  a[0] =1;
  for(int i=0;i<14;i++){
    a[i+1]=a[i]*(i+1);
  }
  return a[n] /(a[m] *a[n-m]);
}

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;
}
ll kaijo(ll x){
  ll z=1;
  while(x>0){
    z *=x;
    z %=MOD;
    x--;
  }
  return z;
}
ll yakusu_num(ll n){
  vl yakusu(n+1,1);
  for(ll i=2;i*i<=n;i++){ 
    while(n%i==0){
      n /=i;
      yakusu[i]++;
    }
  }
  if(n!=1)yakusu[n]++;
  ll num=1;
  for(ll i=0;i <=n;i++){
    num*=yakusu[i];
  }
  return num;
}


//cout<<""<<endl;
int main(){
  string S;cin>>S;
  if(S[2]==S[3] &&S[4]==S[5])cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;

class Program
{
    static void Main(string[] args)
    {
        var input = Array.ConvertAll(ReadLine().Split(' '), int.Parse);
        var num = input[0];
        var limit = input[1];
        var costs = new List<int>();
        for(var i = 0; i < num; i++)
        {
            var inp = Array.ConvertAll(ReadLine().Split(' '), int.Parse);
            if (inp[1] <= limit) costs.Add(inp[0]);
        }
        if (costs.Count != 0)
            WriteLine(costs.Min());
        else WriteLine("TLE");
    }
}


Measured memory_bytes: 13268
main = do
    n:k:_ <- (map read).words <$> getLine
    s <- getLine
    let u = n - (happy $ ['X'] ++ s ++ ['X'])
    let m = max 1 (u - 2*k)
    putStrLn$show (n-m)
happy (_:'R':a:as)
    | a=='R' = 1+ happy ('R':a:as)
    | otherwise = happy ('R':a:as)
happy (a:'L':as)
    | a=='L' = 1+happy ('L':as)
    | otherwise = happy ('L':as)
happy (_:_:[]) = 0

Measured memory_bytes: 4988
import sys

n = int(sys.stdin.readline())

jpy = 0
btc = 0
for _ in range(n):
    x, y = sys.stdin.readline().split()
    if 'JPY' in y:
        jpy += int(x)
    else:
        btc += float(x) * 380000

print(jpy + btc)


Measured memory_bytes: 2940
#include <stdio.h>

int main(void)
{
  long long n, i;
  long long cnt = 0;
  long long cnt2 = 0;
  char s[200001];
  long long c[200000];
  long long d[200000]; 
  scanf("%lld", &n);
  scanf("%s", s);
  for (i = 0; i < n; i++) {
    if (s[i] == '#') {
      cnt++;
    }
    c[i] = cnt;
  }
  for (i = n-1; i >= 0; i--) {
    if (s[i] == '.') {
      cnt2++;
    }
    d[i] = cnt2;
  }
  long long min = d[0];
  for (i = 0; i < n-1; i++) {
    if (c[i]+d[i+1] < min)
      min = c[i]+d[i+1];
  }
  if (min > c[n-1])
    min = c[n-1];
  printf("%lld\n",min);
}


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

typedef long long ll;

ll N, M;
vector<vector<ll>> Kids;
vector<double> Dp;
typedef pair<ll, ll> Pair;
vector<Pair> Cands;
typedef pair<double, ll> DPair;
double R;

int main() {
  cin >> N >> M;
  Kids.resize(N);
  for (ll i = 0; i < M; ++i) {
    ll s, t;
    cin >> s >> t;
    --s;
    --t;
    Kids[s].push_back(t);
  }
  Dp.resize(N);
  for (ll v = N - 2; v >= 0; --v) {
    Dp[v] = 1;
    for (ll w : Kids[v]) {
      Dp[v] += Dp[w] / Kids[v].size();
    }
  }
  R = Dp[0];
  for (ll v = 0; v < N; ++v) {
    if (Kids[v].size() == 1) continue;
    DPair best = DPair(-1, -1);
    for (ll w : Kids[v]) {
      best = max(best, DPair(Dp[w], w));
    }
    Cands.push_back(Pair(v, best.second));
  }
  for (Pair cand : Cands) {
    for (ll v = N - 2; v >= 0; --v) {
      Dp[v] = 1;
      ll s = Kids[v].size() - (cand.first == v ? 1 : 0);
      for (ll w : Kids[v]) {
        if (Pair(v, w) == cand) continue;
        Dp[v] += Dp[w] / s;
      }
    }
    R = min(R, Dp[0]);
  }
  cout << setprecision(10) << R << endl;
  return 0;
}


Measured memory_bytes: 2560
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip	;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
	int n,w,d;
	while(cin>>n>>w>>d,n+w+d){
		vp out;
		out.pb(pii(w*d,w));
		rep(i,n){
			int a,b;
			cin>>a>>b;
			a--;
			pii p=out[a];
			pii out1,out2;
			out.erase(out.begin()+a);
			while(p.second+p.first/p.second<b)b-=p.second+p.first/p.second;
			if(b<p.second){
				out1=pii(b*p.first/p.second,b);
				out2=pii((p.second-b)*p.first/p.second,p.second-b);
			}else{
				out1=pii(p.second*(b-p.second),p.second);
				out2=pii(p.second*(p.second+p.first/p.second-b),p.second);
			}
			if(out1.first>out2.first)swap(out1,out2);
			out.pb(out1);out.pb(out2);
//		rep(j,out.size())cout<<out[j].first<<(j!=i+1?" ":"\n");
		}
		sort(all(out));
		rep(i,out.size())cout<<out[i].first<<(i!=n?" ":"\n");
	}
}

Measured memory_bytes: 1200
import java.io.*;
class Main{
  public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine().trim());
    int a = 1000;
    while(a<n){
      a = a+1000;
	 // System.out.println("Executed");
	}
    a = a - n;
    System.out.println(a);
  }
}

Measured memory_bytes: 32772
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = [''.join(sorted(list(sr()))) for _ in range(N)]

counter = Counter(S)
answer = sum(x*(x-1)//2 for x in counter.values())
print(answer)


Measured memory_bytes: 19724
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second
#define Nmax 500005
const long double PI = acos(-1);

char mat[1003][1003];
int n,m,mn[1003][1003],ans;
queue<pii>H;
int dx[] = {0,1,-1,0};
int dy[] = {1,0,0,-1};

bool OK(int x, int y){
    if (x <= n && x >= 1 && y <= m && y >= 1) return 1;
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin >> n >> m;
    memset(mn,0xff,sizeof(mn));
    for (int i=1;i<=n;i++){
        cin >> mat[i]+1;
        for (int j=1;j<=m;j++){
            if (mat[i][j] == '#'){
                H.push({i,j});
                mn[i][j] = 0;
            }
        }
    }

    while (!H.empty()){
        pii crt = H.front();
        H.pop();

        for (int i=0;i<4;i++){
                int X = crt.first + dx[i];
                int Y = crt.second + dy[i];
                if (OK(X,Y) && mn[X][Y] == -1){
                    mn[X][Y] = mn[crt.first][crt.second] + 1;
                    ans = max(ans, mn[X][Y]);
                    H.push({X,Y});
                }
        }
    }

    cout << ans << '\n';

    return 0;
}

Measured memory_bytes: 13328
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAX_N = 1e6;
int n, m, k;
int cnt[MAX_N];

void Rec(int idx, int sum)
{
    if (idx >= n) {
        if (sum - k <= 1)
            ++cnt[1];
        else
            ++cnt[sum - k];

        return ;
    }

    for (int i = 1; i <= m; ++i)
        Rec(idx + 1, sum + i);
}

int main()
{
    while (cin >> n >> m >> k, n) {
        int sum = pow(m, n);

        memset(cnt, 0, sizeof(cnt));

        Rec(0, 0);

        double ans = 0.0;
        for (int i = 1; i <= n * m - k; ++i)
            ans += i * (double)cnt[i] / sum;

        printf("%.8f\n", ans);
    }

    return 0;
}

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

namespace ConsoleApp44 {
    class Program {
        static void Main(string[] args) {
            var s = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

            int N = s[0];
            int M = s[1];
            int C = s[2];

            var B = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

            int cnt = 0;

            for (int i = 0; i < N; i++) {
                var A = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                int sum = 0;
                for (int j = 0; j < M; j++) {
                    sum += B[j] * A[j];

                }
                sum += C;

                if (sum > 0) {
                    cnt++;
                }

            }

            Console.WriteLine(cnt);
        }
    }
}


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

int main(){
    int N;
    cin >> N;

    int numbs[110];
    int bucket[110] = {0};
    int count = 0;

    for(int i = 0;i < N;i++){
        cin >> numbs[i];

    }
    for(int i = 0;i < N;i++){
        
        bucket[numbs[i]]++;
        
    }
    for(int i = 1;i < 101;i++){
        if(bucket[i]){
        count++;
    }

    }
    
    cout << count << endl;

}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

n,k = readints()
a = readints()

ans = []
for i in range(n-k):
    if a[i] < a[i+k]:
        ans.append('Yes')
    else:
        ans.append('No')
printrows(ans)




Measured memory_bytes: 120296
#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


struct UnionFind{
  Int num;
  vector<Int> rs,ps;
  UnionFind(){}
  UnionFind(Int n):num(n),rs(n,1),ps(n,0){iota(ps.begin(),ps.end(),0);}
  Int find(Int x){
    return (x==ps[x]?x:ps[x]=find(ps[x]));
  }
  bool same(Int x,Int y){
    return find(x)==find(y);
  }
  void unite(Int x,Int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(rs[x]<rs[y]) swap(x,y);
    rs[x]+=rs[y];
    ps[y]=x;
    num--;
  }
  Int size(Int x){
    return rs[find(x)];
  }
  Int count() const{
    return num;
  }
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin>>n;

  Int m=n*n;
  vector<Int> ps(m);
  for(Int i=0;i<m;i++) cin>>ps[i],ps[i]--;

  vector<Int> rs(m);
  for(Int i=0;i<m;i++) rs[ps[i]]=i;

  vector<Int> ds(m,n);
  for(Int i=0;i<m;i++){
    Int y=i/n,x=i%n;
    chmin(ds[i],y);
    chmin(ds[i],n-(y+1));
    chmin(ds[i],x);
    chmin(ds[i],n-(x+1));
  }

  Int dy[]={0,0,1,-1};
  Int dx[]={1,-1,0,0};
  auto in=[&](Int y,Int x){return 0<=y and y<n and 0<=x and x<n;};

  Int ans=0;
  for(Int p:ps){
    ans+=ds[p];
    using P = pair<Int, Int>;
    priority_queue<P, vector<P>, greater<P> > pq;
    pq.emplace(ds[p],p);

    while(!pq.empty()){
      Int v=pq.top().second;
      if(ds[v]<pq.top().first){
        pq.pop();
        continue;
      }
      pq.pop();
      Int y=v/n,x=v%n;
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)) continue;
        Int u=ny*n+nx;
        Int d=ds[v]+(rs[v]>rs[p]);
        if(ds[u]<=d) continue;
        ds[u]=d;
        pq.emplace(ds[u],u);
      }
    }
  }

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


Measured memory_bytes: 9324
fun main(args: Array<String>) {
    val (n, m) = readLine()!!.split(' ').map(String::toInt)
    val hList = readLine()!!.split(' ').map(String::toLong)
    val abList = (1..m).map {
        val (c, d) = readLine()!!.split(' ').map(String::toInt)
        Pair(c, d)
    }

    val goodArray = IntArray(n + 1) { 1 }
    goodArray[0] = 0
    for ((a, b) in abList) {
        if (hList[a - 1] <= hList[b - 1]) goodArray[a] = 0
        if (hList[a - 1] >= hList[b - 1]) goodArray[b] = 0
    }
    println(goodArray.count { it == 1 })
}


Measured memory_bytes: 68724
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;
    int K;
    cin >> str;
    cin >> K;
    char start1, end;
    start1 = str.at(0);
    end = str.at(str.length() - 1);
    long long count = 0, count1 = 1, count2 = 1;

    bool flag = false, flag_c;
    for (int i = 1; i < str.length(); ++i)
    {
        if (str.at(i - 1) == str.at(i))
        {
            ++count1;
            continue;
        }
        else
        {
            flag = true;
            break;
        }
    }
    if (start1 == end)
    {
        for (int i = str.length() - 1; i > 0; --i)
        {
            if (str.at(i - 1) == str.at(i))
            {
                ++count2;
                continue;
            }
            else
            {
                break;
            }
        }
    }
    flag_c = (start1 == end);

    if (flag)
    {
        for (int i = 1; i < str.length(); ++i)
        {
            if (str.at(i - 1) == str.at(i))
            {
                ++count;
                str.at(i) = 'X';
            }
        }
        count *= K;
        if ((int)str.length() != 1)
        {
            if (str.at(str.length() - 2) != end)
            {
                if (start1 == str.at(str.length() - 1))
                {
                    if (flag_c && ((count1 + count2) % 2 == 1))
                    {
                    }
                    else{
                        count += K - 1;
                    }
                }
            }
        }
    }
    else
    {
        count = ((K + 1) / 2) * (str.length() / 2) + (K / 2) * ((str.length() + 1) / 2);
    }
    if (str.length() == 1)
    {
        count = K / 2;
    }
    cout << count << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, n) for (int i = (s); i < (int)(n); i++)

using namespace std;

int main() {
   int a,b;
   cin >> a >> b;
   if(a%3==0 || b%3==0 || (a+b)%3==0)
   cout << "Possible " << endl;
   else 
   cout << "Impossible" << endl;
}

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = INT_MAX / 2;
const ll  inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const long double PI = acos(-1);

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	/*------------------------------*/

	string s;
	cin >> s;

	if (s == "Sunny") {
		out("Cloudy");
	}
	else if (s == "Cloudy") {
		out("Rainy");
	}
	else {
		out("Sunny");
	}

	return 0;
}

Measured memory_bytes: 256
/*#include<iostream>
#include<stack>
using namespace std;
int source[15];
int main()
{
    int x;
    cin>>x;
    while(x--)
    {
        stack<int>a;
        stack<int>b;
        a.push(0);
        b.push(0);
        for(int k=0;k<10;k++)
            cin>>source[k];
            int i;
            bool flag=0;
        for(i=0;i<10;i++)
        {
            if(source[i]<a.top()&&source[i]<b.top())
               {
                   flag=1;
                    break;
               }
             else if(source[i]>a.top()&&source[i]<b.top())
                    a.push(source[i]);
             else if(source[i]>b.top()&&source[i]<a.top())
                    b.push(source[i]);
                    else
                    {

                    if(a.top()>=b.top())
                        a.push(source[i]);
                    else
                        b.push(source[i]);
                    }
        }
                if(flag)
                cout<<"NO"<<endl;
                else
                    cout<<"YES"<<endl;
    }
    return 0;
}*/
#include<iostream>
using namespace std;
int a[15];
int flag;
void dfs(int x,int l,int r)
{
    if(flag)
        return ;
    if(x==10)
    {
        return ;
    }
    else
    {
        if(a[x]>l)
           dfs(x+1,a[x],r);
        else if(a[x]>r)
            dfs(x+1,l,a[x]);
        else
        {
            flag=1;
            return ;
        }
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<10;i++)
            cin>>a[i];
            flag=0;
            dfs(0,0,0);
            if(flag)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
    }
    return 0;
}



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

#define mk make_pair
#define sc second
#define fr first
#define pb emplace_back
#define all(s) s.begin(), s.end()
#define sz(s) ( (int)s.size() )
#define int long long

using namespace std;

const int inf = (int)1e9 + 7  ;
const long long mod = 1e9 + 7;
const int N = (int)3007;

int n, m;
main(){
      cin >> n >> m;
      cout << ((n % m == 0)?0:1);
}


Measured memory_bytes: 256
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    S = input()
    modsum = 0
    for c in S:
        modsum = (modsum + int(c)) % 9
    if modsum == 0:
        print('Yes')
    else:
        print('No')

    
main()

Measured memory_bytes: 68420
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define INF 1e9+7

int cnt[201];
int main() {
	int n;
	cin >> n;
	for(int i=1;i<201;++i)
		for (int j = 1; i*j < 201; ++j) {
			cnt[i*j]++;
		}
	int ans = 0;
	for (int i = 1; i <= n; i++)
		if (cnt[i] == 8&&i%2==1)
			ans++;
	cout << ans;
	
}


Measured memory_bytes: 256
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
using ll = long long;
constexpr ll INF = 1010000000000000017LL;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

class A {
public:
	void solve(std::istream& in, std::ostream& out) {
        string s;
        in>>s;
        ll n=s.size()+1;
        vector<ll> v(n,0);
        for (ll i = 1; i < n; ++i) {
            if(s.at(i-1)=='<'){
                v.at(i)=max(v.at(i-1)+1,v.at(i));
            }
        }
        for (ll j = n-2; j >= 0; --j) {
            if(s.at(j)=='>'){
                v.at(j)=max(v.at(j+1)+1,v.at(j));
            }
        }
        ll sum=0;
        rep(i,n){
            sum+=v.at(i);
        }
        out<<sum<<endl;



	}
};


int main() {
	A solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


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


int main(int argc, const char * argv[]) {
    
   
    string s; cin >> s;
    bool is9 = false;
    for(int i = 0; i < s.size(); i++) {
        if(s[i]=='9') is9 = true;
    }
    if(is9) cout << "Yes" << endl;
    else cout << "No" << endl;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 1; i < N+1; ++i) {
        int a;
        cin >> a;
        A[a-1] = i;
    }
    for (int i = 0; i < N; ++i) {
        printf("%d%c", A[i], i == N-1 ? '\n' : ' ');
    }
    return 0;
}


Measured memory_bytes: 1280
#include <bits/stdc++.h>
#include <boost/range/irange.hpp>
using namespace std::literals;
using namespace std;

namespace xmaho
{
inline namespace input
{
template<typename T>
T get_value(std::istream& is = std::cin);
template<typename C>
C get_container(std::istream& is = std::cin, typename C::size_type length = std::numeric_limits<typename C::size_type>::max());
}
}
using namespace xmaho;

int main()
{
  unsigned long long n;
  cin >> n;
  vector<unsigned long long> r(n);
  for (auto i = n - 1; i; --i) {
    unsigned long long a;
    cin >> a;
    ++r[a - 1];
  }
  for (auto e : r) {
    cout << e << endl;
  }
  return 0;
}

class initalization
{
  class custom_numpunct: public std::numpunct<char>
  {
  public:
    using numpunct::numpunct;
  protected:
    std::string do_truename() const override { return "Yes"; }
    std::string do_falsename() const override { return "No"; }
  };

public:
  initalization()
  {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.imbue(std::locale(std::cout.getloc(), new custom_numpunct{}));
    std::cout << std::boolalpha << std::fixed << std::setprecision(15);
  }
} initalize;

template<typename T>
T xmaho::input::get_value(std::istream& is)
{ T v {}; is >> v; return v; }

template<typename C>
C xmaho::input::get_container(std::istream& is, typename C::size_type length)
{
  C v {};
  if (length != std::numeric_limits<typename C::size_type>::max())
    v.reserve(length);
  typename C::value_type e {};
  for (auto i {length}; i != 0 && is >> e; --i)
    v.push_back(std::move_if_noexcept(e));
  return v;
}


Measured memory_bytes: 4788
#include <cstdio>

int main(void)
{
	long long a, b, k;

	scanf("%lld %lld %lld" ,&a, &b, &k);

	if (k <= a)
		printf("%lld %lld" ,a - k, b);
	else if (k <= a + b)
		printf("0 %lld" ,b - (k - a));
	else
		printf("0 0");
	return 0;
}

Measured memory_bytes: 128
package main

import "fmt"

func main() {
	var n int
	var s string
	fmt.Scan(&n, &s)

	max := 0
	cnt := 0
	for _, r := range s {
		if r == 'I' {
			cnt++
		} else {
			cnt--
		}

		if cnt > max {
			max = cnt
		}
	}

	fmt.Println(max)
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
//template<typename key, typename val>
//using ordered_tree =
//  tree<key, val, less<>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int64;
typedef complex<double> comp;
const double pi = 3.14159265358979323846;
const int inf = (int)1e+9 + 2;
const int64 inf64 = (int64)1e+18 + 2;
const double dinf = 1e+20;
const int mod = 1'000'000'007;//998244353;
const int base = 2187;
const double eps = 1e-8;
const int N = 100'000;
const int LOGN = 18;

int n, m, k;

void solve(int test)
{
  int d;
  cin >> n >> d;
  vector<vector<int>> a(n, vector<int>(d, 0));
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < d; ++j) {
      cin >> a[i][j];
    }
    for (int j = i - 1; j >= 0; --j) {
      int val = 0;
      for (int q = 0; q < d; ++q) {
        val += (a[i][q] - a[j][q]) * (a[i][q] - a[j][q]);
      }
      int r = sqrt(val);
      ans += r * r == val;
    }
  }
  cout << ans << '\n';
}

void precalc()
{
}

int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  precalc();
  int test = 1;
  //cin >> test;
  //auto start = chrono::high_resolution_clock::now();
  for (int i = 1; i <= test; ++i)
    solve(i);
  //cerr << chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start).count() << '\n';
  return 0;
}


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

int main()
{
  double r;
  const double PI = 3.1415926535897932384626;
  cin >> r;

  cout << fixed << setprecision(10);
  cout << r * r * PI << " " << r * 2 * PI << endl;

  return 0;
}


Measured memory_bytes: 3288
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<math.h>
using namespace std; 
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define pp(...) "\033[94m [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define dg1(x) debug() << pp(x)
#define dg2(x,y) debug() << pp(x) pp(y)
#define dg3(x,y,z) debug() << pp(x) pp(y) pp(z)
#define dg4(w,x,y,z) debug() << pp(w) pp(x) pp(y) pp(z)
#define msg(x) debug() << x
#define int long long
const int xx = 2e5;
const int MOD = 1e9 + 7;
int lcm(int a, int b){
  return a*(b/__gcd(a,b));
}
signed main(){

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

  int a , b;
  cin >> a >> b;
  cout << lcm(a,b) << '\n';

  
}


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

int main(){
  long long n,m; cin>>n >>m;
  if(2*n>m){
    cout<<m/2<<endl;
    return 0;
  }
  long long ans=n;
  m-=2*n;
  ans+=m/4;
  cout<<ans<<endl;
}

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

using namespace std;
typedef long long ll;
typedef long int li;
typedef long double lb;
#define rep(i,j,n) for (ll i = j; i < (n); i++)
#define repr(i,j,n) for(ll i = j; i >= (n); i--)
#define all(x) (x).begin(),(x).end()
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<b)
#define out(ans) cout << ans << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<ll,ll>P;
const ll mod=1e9+7;
const int INF = 1000000;
const double PI=3.14159265359;



int main(){

  ll n;cin>>n;
  ll v[3]={9,6,1};

  ll dp[n+1];rep(i,0,n+1)dp[i]=i;

  rep(i,6,n+1){
    for(int j=6;j<=i;j*=6)chmin(dp[i],dp[i-j]+1);
    for(int j=9;j<=i;j*=9)chmin(dp[i],dp[i-j]+1);
  }
  // rep(i,0,50)cout<<dp[i]<<" ";

  out(dp[n]);


  return 0;
}


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

#include <iostream>
#include <algorithm>

#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <utility>
#include <array>
#include <complex>
#include <valarray>

#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <chrono>
#include <random>
#include <numeric>


using namespace std;
//#define int long long

typedef long long ll;
typedef unsigned long long ull;
//typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef pair<ll,pll> plpll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const int INF = 1<<30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod
#define inside(y, x, h, w) (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false

int n, m, z, w;
int a[2005];
int diff[2005];
int val[2005][2005];

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> z >> w;
    rep(i, n) cin >> a[i];
    if (n == 1) {
        int ans = abs(a[0] - w);
        cout << ans << endl;
        return 0;
    }
    rep(i, n - 1) {
        diff[i] = abs(a[i] - a[n - 1]);
    }
    int ans = max(abs(a[n - 1] - w), diff[n - 2]);
    cout << ans << endl;


}

Measured memory_bytes: 256
s = input()
if s[0] == 'A' and s[2:-1].count('C') == 1 and s[-1].islower():
    ind = s.index('C')
    if s[1:ind].islower() and (ind == len(s) - 2 or s[ind+1:-1].islower()):
        print('AC')
        exit()
print('WA')

Measured memory_bytes: 9060
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#define MP make_pair
using ll = long long;
using namespace std;

int main()
{
  string s;
  cin >> s;
  char first = s[0];
  char last = s[s.size()-1];
  cout << first << s.size()-2 << last << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  
  if( N==1){
    cout << 0 << endl;
  }else{
    unsigned long int sum = 0;
    for(int i = 1; i < N; i++){
      sum = sum + i;
    }
    cout << sum << endl;
  }
  
}



Measured memory_bytes: 384
#include<bits/stdc++.h>
#define me(a,x) memset(a,x,sizeof a)
#define cp(a,x) memcpy(a,x,sizeof a)
using namespace std;
typedef long long LL;
const int N=1e5+2;
char B[1<<14],*S=B,*T=B;
#define gc (S==T&&(T=(S=B)+fread(B,1,1<<14,stdin),S==T)?-1:*S++)
inline int read(){
    int x=0,f=1; char ch=gc;
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1; ch=gc;}
    while(ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=gc;}
    return x*f;
}
struct P{int x,y;}q[N];
int Cmp(P x,P y){return x.x<y.x;}
int a[N]; LL c[N];
int main(){
    int i,n=read(),m=read();
    for(i=1;i<=n;++i) a[i]=q[i].x=read(),q[i].y=i;
    sort(q+1,q+1+n,Cmp);
    LL s=0,mx=0,u=0; int p;
    for(i=2;i<=n;++i){
        if(a[i]>a[i-1]);
        else s+=m-a[i-1],++u;
    }
    q[0].x=0;
    for(i=1;i<=n;++i){
        int x=q[i].y,o=q[i].x;
        s+=(LL)(o-q[i-1].x)*u;
        if(o!=q[i-1].x){
            if(s>mx)mx=s,p=o;
        }
        if(x<n && a[x]<a[x+1]) ++u;
        if(x>1 && a[x-1]<a[x]) --u,s-=a[x]-a[x-1];
        if(x<n && a[x]>a[x+1]) ++u;
        if(x>1 && a[x-1]>a[x]) --u,s-=(m-a[x-1]+a[x]);
    }
    LL ans=0;
    for(i=1;i<n;++i){
        if(a[i]<a[i+1]){
            if(a[i]<p && p<=a[i+1])ans+=1+a[i+1]-p;
            else ans+=a[i+1]-a[i];
        }
        else if(a[i]>a[i+1]){
            if(p>a[i]) ans+=1+m-p+a[i+1];
            else if(p<=a[i+1]) ans+=1+a[i+1]-p;
            else ans+=m-a[i]+a[i+1];
        }
    }
    printf("%lld\n",ans);
    return 0;
}

Measured memory_bytes: 1408
#include <iostream>      // cout, endl, cin
#include <string>        // string, to_string, stoi
#include <vector>        // vector
#include <algorithm>     // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility>       // pair, make_pair
#include <tuple>         // tuple, make_tuple
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <deque>         // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset>        // bitset
#include <cctype>        // isupper, islower, isdigit, toupper, tolower

using namespace std;

int main()
{
    //system("chcp 65001");

    int A,B;
    cin >> A >> B;

    if(A - 2*B >= 0)
    {
        cout << A - 2*B;
    }
    else
    {
        cout << "0";
    }
    

    return 0;
}

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

using namespace std;

void solve()
{
	int H;
	while (cin >> H, H)
	{
		vector< vector<int> > stone(H, vector<int>(5));
		for (int i = 0; i < H; ++i)
		{
			for (int j = 0; j < 5; ++j)
			{
				cin >> stone[i][j];
			}
		}
		int preSum = -1;
		int sum = 0;
		bool update = true;
		while (preSum != sum)
		{
			preSum = sum;
			for (int i = 0; i < H; ++i)
			{
				if (stone[i][0] == stone[i][1] && stone[i][1] == stone[i][2] && stone[i][2] == stone[i][3] && stone[i][3] == stone[i][4])
				{
					sum += stone[i][0] * 5;
					stone[i][0] = stone[i][1] = stone[i][2] = stone[i][3] = stone[i][4] = 0;
				}
				else if (stone[i][0] == stone[i][1] && stone[i][1] == stone[i][2] && stone[i][2] == stone[i][3])
				{
					sum += stone[i][0] * 4;
					stone[i][0] = stone[i][1] = stone[i][2] = stone[i][3] = 0;
				}
				else if (stone[i][1] == stone[i][2] && stone[i][2] == stone[i][3] && stone[i][3] == stone[i][4])
				{
					sum += stone[i][1] * 4;
					stone[i][1] = stone[i][2] = stone[i][3] = stone[i][4] = 0;
				}
				else if (stone[i][0] == stone[i][1] && stone[i][1] == stone[i][2])
				{
					sum += stone[i][0] * 3;
					stone[i][0] = stone[i][1] = stone[i][2] = 0;
				}
				else if (stone[i][1] == stone[i][2] && stone[i][2] == stone[i][3])
				{
					sum += stone[i][1] * 3;
					stone[i][1] = stone[i][2] = stone[i][3] = 0;
				}
				else if (stone[i][2] == stone[i][3] && stone[i][3] == stone[i][4])
				{
					sum += stone[i][2] * 3;
					stone[i][2] = stone[i][3] = stone[i][4] = 0;
				}
			}
			for (int i = H - 1; i >= 0; --i)
			{
				for (int j = 0; j < 5; ++j)
				{
					if (stone[i][j] == 0)
					{
						int pos = i - 1;
						while (pos >= 0 && stone[pos][j] == 0)
						{
							--pos;
						}
						if (pos >= 0)
						{
							swap(stone[i][j], stone[pos][j]);
						}
					}
				}
			}
		}
		cout << sum << endl;
	}
}

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

Measured memory_bytes: 1220
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf 1000000005
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define mod 1000000009
#define sz(a) ((int)(a).size())

template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s);}
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define eps 1e-10
typedef pair<int, int> II;
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define PI 2 * acos(0)

#define maxn 100005

int n, k, a[maxn];

int main(){
	srand(time(NULL));
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("in.txt", "r", stdin);

	cin >> n >> k;
	For(i, 1, n) cin >> a[i];

	cout << (n - 1) / (k - 1) + ((n - 1) % (k - 1) != 0) << endl;

    return 0;
}


Measured memory_bytes: 640
#include<iostream>
#include<vector>
#include<set>
#include<climits>
using namespace std;
#define NONE -1

int prim(int n, vector< vector<int> >v)
{
  set<int>selected;
  set<int>::iterator it;
  int mincost,mini;
  int i;
  int ans=0;

  selected.insert(0);

  while(selected.size()!=n){
    mincost=INT_MAX;
    mini=NONE;

    for(it=selected.begin();it!=selected.end();it++){
      for(i=0;i<n;i++){
	if(selected.find(i)!=selected.end())continue;
	if(v[*it][i]==NONE)continue;
	if(v[*it][i]>=mincost)continue;
	mincost=v[*it][i];
	mini=i;
      }
    }

    if(mini==NONE)break;
    selected.insert(mini);
    ans+=mincost;
  }

  return ans;
}

int main()
{
  int n,m;
  int a,b,d;
  char c;
  while(cin>>n>>m,n){
    vector< vector<int> >v(n,vector<int>(n,NONE));
    while(m-->0){
      cin>>a>>c>>b>>c>>d;
      v[a][b]=v[b][a]=(d/100>0)?d/100-1:0;
    }
    cout<<prim(n,v)<<endl;
  }
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  int a[n];
  vector<pair<int, bool>> x[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < a[i]; j++) {
      int tmp1;
      bool tmp2;
      cin >> tmp1 >> tmp2;
      x[i].push_back(make_pair(tmp1, tmp2));
    }
  }

  for (int tmp = 0; tmp < (1 << n); tmp++) {
    bitset<15> s(tmp);
    int flag = 0;
    for (int i = 0; i < n; i++) {
      if (s[i]) {
        for (int j = 0; j < x[i].size(); j++)
          if (s[x[i][j].first - 1] != x[i][j].second)
            flag++;
      }
    }
    if (flag == 0 && ans < s.count())
      ans = s.count();
  }
  cout << ans;
  return 0;
}

Measured memory_bytes: 3632
#include<stdio.h>

const int MAX_V = 999999;

int main(){
	int prime[MAX_V+1];
	int count[MAX_V+1];
	int i, k, n, sum = 0;
	for(i = 2;i <= MAX_V;i++) prime[i] = 1;
	for(i = 2; i * i <= MAX_V;i++){
		if(prime[i]){
			for(k = 2 * i;k <= MAX_V; k += i){
				prime[k] = 0;
			}
		}
	}
	for(i = 0;i < 2;i++){
		prime[i] = 0;
		count[i] = 0;
	}
	for(i = 2;i <= MAX_V;i++){
		if(prime[i]) sum++;
		count[i] = sum;
	}
	while(scanf("%d", &n) != EOF) printf("%d\n", count[n]);
	return 0;
}

Measured memory_bytes: 9596
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n, k;
  cin>>k>>n;
  
  int a[n+1] {0};
  int max_diff=0;
  cin>>a[0];
  a[n]=a[0]+k;
  for(int i=1;i<=n;i++)
  {
    cin>>a[i];
    if(a[i]-a[i-1] >= max_diff)
      max_diff = a[i]-a[i-1];
  }
  cout<<k-max_diff;
  
  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  
  priority_queue<int64_t> q;
  
  for(int i=0; i < n; i ++){
    int a;
    cin >> a;
    q.push(a);
  }
  
  for(int i=0; i < m; i ++){
    int x;
    x = q.top()/2;
    q.pop();
    q.push(x);
  }
  
  int64_t x = 0;
  
  while( !q.empty()){
    x += q.top();
    q.pop();
  }
  
  cout << x << endl;
  
}


Measured memory_bytes: 1400