akhauriyash/Code-Regression huggingface dataset:
import sys
from collections import Counter as cc
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())
class UnionFind():
    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.height = [0]*n

    def get_root(self, i):
        if self.parent[i] == i:
            return i
        else:
            self.parent[i] = self.get_root(self.parent[i])
            return self.parent[i]
        
    def unite(self, i, j):
        root_i = self.get_root(i)
        root_j = self.get_root(j)
        if root_i != root_j:
            if self.height[root_i] < self.height[root_j]:
                self.parent[root_i] = root_j
            else:
                self.parent[root_j] = root_i
                if self.height[root_i] == self.height[root_j]:
                    self.height[root_i] += 1
    
    def is_in_group(self, i, j):
        if self.get_root(i) == self.get_root(j):
            return True
        else:
            return False

def main():
    n, m = mi()
    uf = UnionFind(n)
    for i in range(m):
        a, b = mi()
        a -= 1
        b -= 1
        uf.unite(a, b)
    for i in range(n):
        uf.get_root(i)
    c = cc(uf.parent)
    ans = max(c.values())
    print(ans)




if __name__ == '__main__':
    main()

Measured memory_bytes: 33808
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}



signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int T1, T2;
	int A1, A2;
	int B1, B2;
	int diff, sb, sa;
	
	cin>>T1>>T2;
	cin>>A1>>A2;
	cin>>B1>>B2;
	
	// cout<<A1+A2<<" "<<B1+B2<<endl;
	
	if(A1*T1 + A2*T2 == B1*T1 + B2*T2){
		cout<<"infinity"<<endl;
		return 0;
	}
	
	if(A1*T1 > B1*T1 && A2*T2 + A1*T1 > B2*T2 + B1*T1){
		cout<<0<<endl;
		return 0;
	} 
	
	if(A1*T1 < B1*T1 && A2*T2 + A1*T1 < B2*T2 + B1*T1){
		cout<<0<<endl;
		return 0;
	}
	
	if(A1 < B1){
		swap(A1, B1);
		swap(A2, B2);
	}
	
	sa = A2*T2 + A1*T1;
	sb = B2*T2 + B1*T1;
	
	diff = sb - sa;
	
	
	int ans = (A1*T1 - B1*T1) / diff;
	
	ans *= 2;
	
	ans++;
	
	if((A1*T1 - B1*T1)%diff == 0) ans--;
	
	cout<<ans<<endl;
	
	
	return 0;
}

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

int main(){
 int n,k;
 cin >> n >> k;
 int a[n];
 for(int i=0;i<n;i++)
      cin >> a[i];
 int t=0;
 for(int i=0;i<n;i++){
   t+=min(abs(a[i]-0)*2,abs(a[i]-k)*2);
 }
 cout << t;
 return 0;
}


Measured memory_bytes: 3608
N, K = map(int, input().split())

count = 0
for i in range(K + 1, N + 1):
    if K == 0:
        count += N
    else:
        a = (i - K) * (N // i)
        b = 0
        if N % i >= K:
            b = N % i - K + 1
        count += a + b
print(count)


Measured memory_bytes: 2940
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){

  int r,n,x1,x2,h,i,j;
  
  while(cin>>r>>n,r){
  int build[41][2]={0};
  for(i=0;i<n;i++){
    cin>>x1>>x2>>h;
    x1+=20; x2+=20;
    for(j=x1+1;j<=x2-1;j++) for(int k=0;k<2;k++) build[j][k]=max(build[j][k],h);
    build[x1][0]=max(build[x1][0],h);
    build[x2][1]=max(build[x2][1],h);
  }
  int bu[41]={0};
  for(i=0;i<41;i++) bu[i]=min(build[i][0],build[i][1]);
  double c[41],result=100;
  for(i=-r+1;i<=r-1;i++) c[i+20]=sqrt(r*r-i*i)-r;
  for(i=20-r+1;i<=20+r-1;i++){
    //cout<<bu[i]-c[i]<<" ";
    //cout<<endl;
    result=min(result,bu[i]-c[i]);
  }
  cout<<result<<endl;
  }
  return 0;

}

Measured memory_bytes: 3180
import java.io.*;
import java.util.*;


public class Main {

	
	public static class FastReader {
		BufferedReader br;
		StringTokenizer root;
		
 
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
 
		String next() {
			while (root == null || !root.hasMoreTokens()) {
				try {
					root = new StringTokenizer(br.readLine());
				} catch (Exception addd) {
					addd.printStackTrace();
				}
			}
			return root.nextToken();
		}
 
		int nextInt() {
			return Integer.parseInt(next());
		}
 
		double nextDouble() {
			return Double.parseDouble(next());
		}
 
		long nextLong() {
			return Long.parseLong(next());
		}
 
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception addd) {
				addd.printStackTrace();
			}
			return str;
		}
	}
	

	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	
	public static FastReader sc = new FastReader();
 
	static int mod = (int) (1e9+7),MAX=(int) (1e4);
	static List<Integer>[] edges;
    static Long[] dp;
    static int[][] a;
    static int n;
	
	public static void main(String[] args) throws IOException {
		
	n = sc.nextInt();
	a = new int[n][n];

	for(int i=0;i<n;++i) {
		for(int j=0;j<n;++j)
			a[i][j] = sc.nextInt();
	}
	
    dp = new Long[1<<n];
    out.println(f(0));    

out.close();
}
private static long f(int mask) {
	int row = Integer.bitCount(mask);
	if(row == n) return 1;
	
	if(dp[mask] != null) return dp[mask];
  
    long ans = 0;
	for(int col=0;col<n;++col)
         if(((mask>>col)&1) == 0 && a[row][col] == 1)
        	 ans=(ans + f(mask|1<<col))%mod;
	
	return dp[mask] = ans;
}
	
	}




Measured memory_bytes: 115912
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <numeric>
#include <cmath>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;

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

const ll MOD = 1000000007;
const int INF = 1<<30;
//const ll INF = (ll)1e18 + 1;

int main(){
  int N; cin >> N;
  vector<int> x(N), y(N), h(N);
  rep(i, N){
    cin >> x[i] >> y[i] >> h[i];
  }
  for (int Cx = 0; Cx <= 100; ++Cx){
    for (int Cy = 0; Cy <= 100; ++Cy){
      int H = 0;
      for (int n = 0; n < N; ++n){
        if (h[n] > 0) {
          H = h[n] + abs(x[n] - Cx) + abs(y[n] - Cy);
          break;
        }
      }
      bool flag = true;
      for (int n = 0; n < N; ++n){
        int h_check = max(H - abs(x[n] - Cx) - abs(y[n] - Cy), 0);
        if (h_check != h[n]) flag = false;
      }
      if (flag){
        cout << Cx << " " << Cy << " " << H << endl;
        return 0;
      }
    }
  }
}


Measured memory_bytes: 256
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
#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 speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cotu cout
#define itn int
const int INF = 1e9 + 7;

int main(){
    int n; cin>>n;
    vi v(n);
    for(int i = 0; i < n; i++) cin>>v[i];
    vi dp(n+1,INF);
    dp[0]=0;
    for(int i = 0; i < n-1; i++){
        dp[i+1] = min(dp[i+1],dp[i]+abs(v[i]-v[i+1]));
        dp[i+2] = min(dp[i+2],dp[i]+abs(v[i]-v[i+2]));
    }
    cout<<dp[n-1];
}

Measured memory_bytes: 1024
//#include<iostream>
//#include<cstdio>
//using namespace std;
//int main(){
  // double a,b,r;
  //  const double PAI=3.14159265;
  // cin >> r ;
  //  a=r*r*PAI;
  // b=2*r*PAI;
  // printf ("%.7f %.7f\n",a,b); 
  //}
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
  const double pi=3.141592653589793;
  double r,a,b;
  cin >> r ;
  a=pi*r*r;
  b=2*pi*r;
  printf("%.7f %.7f\n",a,b);
}



Measured memory_bytes: 3216
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,fma")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 3022, mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n;
	cin >> n;
	n--;
	ll f = 0;
	for(int i = 1; i <= n; i++) f += n/i;
	cout << f << '\n';
}


Measured memory_bytes: 3660
def main(input = STDIN)
  array = [[2, 380], [3, 550], [5, 850], [10, 380 * 5 * 80 / 100], [12, 2200 * 85 / 100], [15, 2550 * 88 / 100]]
  cost = Array.new(51, 1 / 0.0)
  cost[0] = 0
  array.each{|i|
    for j in 0..50 - i[0]
      cost[i[0] + j] = cost[j] + i[1] if cost[i[0] + j] > cost[j] + i[1]
    end
  }
  while (m = input.gets.to_i) != 0
    puts cost[m / 100]
  end
end
main

Measured memory_bytes: 6120
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
	int a, b, c;
	scanf("%d %d %d",&a,&b,&c);
	if(a == 5 && b == 5 && c == 7) printf("YES\n");
	else if(a == 5 && b == 7 && c == 5) printf("YES\n");
	else if(a == 7 && b == 5 && c == 5) printf("YES\n");
	else printf("NO\n");
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1<<30)-1;
const long long LINF = (1LL<<62)-1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
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; }

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  while (true) {
    bool flag = true;
    rep(i, n) {
      if (a[i] > n-1) {
        rep(j, n) {
          if (j == i)
            continue;
          a[j] += a[i]/n;
        }
        ans += a[i]/n;
        a[i] %= n;
        flag = false;
      }
    }
    if (flag)
      break;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3660
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
graph=[]
h,w=map(int,input().split())
for i in range(10):
    graph.append(list(map(int,input().split())))
graph = csr_matrix(graph)

d, p = floyd_warshall(csgraph=graph, directed=True, return_predecessors=True)
#print(d)
dic={-1:0}
for i in range(10):
    dic[i]=d[i][1]
ans=0
for i in range(h):
    for j in map(int,input().split()):
        ans+=dic[j]
print(int(ans))

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

int main(){
  int N;
  cin >> N;
  
  int count = 0;
  if(N % 10 == N  / 10 % 10 && N / 10 % 10 == N / 100 % 10
    || N / 1000 == N / 100 % 10 && N / 100 % 10 == N / 10 % 10)
    cout << "Yes" << endl;
  else cout << "No" << endl;
    
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{

    class Program
    {

        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            string[] str = Console.ReadLine().Split(' ');
            long a = 0;
            long u = 0;
            int ans = int.MaxValue;
            int k = 0;

            for(int i = 0; i < n; i++)
            {
                a = long.Parse(str[i]);

                if(a % 2 != 0)
                {
                    Console.WriteLine("0");
                    return;
                }
                u = a;
                k = 0;

                while(u % 2 == 0)
                {
                    u /= 2;
                    k++;
                    
                }

                if (k < ans)
                {
                    ans = k;
                }
            }

            Console.WriteLine(ans);

        }

    }

}
    


Measured memory_bytes: 13140
k,n = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)

max_l = 0

def max(a,b)
  a > b ? a : b
end


n.times do |i|
  if i == 0
    max_l = max(a[i], max_l)
  else
    max_l = max(a[i] - a[i-1], max_l)
  end
end

last = k - a.last + a[0]

max_l = max(last, max_l)

puts k - max_l


Measured memory_bytes: 16644
#include<bits/stdc++.h>
using namespace std;
int main(){
int a;scanf("%d",&a);
int L=a/100;
int R=a%100;
if(1<=L&&L<=12){
if(1<=R&&R<=12)printf("AMBIGUOUS\n");
else printf("MMYY\n");
}else{
if(1<=R&&R<=12)printf("YYMM\n");
else printf("NA\n");
}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int mod = 1e9+7, A = 1e3+5;
vector<ll> v;
void solve(ll x)
{
	if (x > 1e10)
		return;
	v.pb(x);
	solve(x * 10 + x % 10);
	if (x % 10 != 0)
		solve(x * 10 + x % 10 - 1);
	if (x % 10 != 9)
		solve(x * 10 + x % 10 + 1);
}
int main()
{
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	for (int i = 1; i < 10; i++)
		solve(i);
	sort(v.begin(), v.end());
	int k;
	cin >> k;
	cout << v[k - 1];
	return 0;
}


Measured memory_bytes: 3444
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n) ;i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll=long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD  1000000007
const ll INF=1e18;
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; }



int main(int argc, char const *argv[]) {
  ll h,w;
  cin >> h >> w;
  ll yoko=w/2+w%2;
  ll tate=h/2+h%2;
  ll ans=tate*yoko+(h/2)*(w/2);
  if(h==1 || w==1) ans=1;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int N;
    cin >> N;
    vector<int> h(N);
    for(int i = 0; i < N; i++) cin >> h[i];
    int S = h[N-1];
    for(int i = 1; i < N; i++){
        if(h[i-1] > h[i]) S += h[i-1] - h[i];
    }
    cout << S << endl;
}


Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<math.h>

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

int N;
double X[300], Y[300];
P d[300];

P add(P p1, P p2) {
  return P(p1.first + p2.first, p1.second + p2.second);
}

P minas(P p1, P p2) {
  return P(p1.first - p2.first, p1.second - p2.second);
}

P mul(P p, double c) {
  return P(p.first * c, p.second * c);
}

P ave(P p1, P p2) {
  return P((p1.first + p2.first) / 2, (p1.second + p2.second) / 2);
}

void printP(P p) {
  printf("P:(%f, %f)\n", p.first, p.second);
}

double square(P p) {
  return p.first * p.first + p.second * p.second;
}

double size(P p) {
  return sqrt(square(p));
}

int count(P p) {
  // printP(p);
  int counter = 0;
  for (int i = 0; i < N; i++) {
    if (square(minas(p, d[i])) <= 1.0001) counter++;
  }
  // printf("%d\n",counter);
  return counter;
}

int check(int i, int j) {
  if (square(minas(d[i], d[j])) > 4.0) return 0;
  int ans = 0;
  P average = ave(d[i], d[j]);
  P dir = mul(minas(d[i], d[j]), 0.5);
  // printP(average);
  double s = sqrt(1.0 - square(dir));
  // printf("!!!!!!%f\n",s);
  P dir1 = P((Y[i] - Y[j]), - (X[i] - X[j]));
  //printf("!!!!!!%f\n",size(dir1));
  double r = s / size(dir1);
  P d1 = add(average, mul(dir1, r));
  P d2 = add(average, mul(dir1, -r));
  return max(count(d1), count(d2));
}

int main() {
  while (1) {
    scanf("%d", &N);
    if (N == 0) break;
    for (int i = 0; i < N; i++) scanf("%lf%lf", &X[i], &Y[i]), d[i] = P(X[i], Y[i]);
    int ans = 1;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < i; j++)
        ans = max(ans, check(i, j));
    printf("%d\n", ans);
  }
}


Measured memory_bytes: 2688
#include <iostream>
#include <vector>
#include <cstdlib>
#include <iomanip>

using namespace std;
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; }

const long long INF = 1LL << 60;

// 入力
int N;
long long h[100010];

// DP テーブル
long long dp[100100];

long gcd(long a, long b)
{
    if (a < b) {
        a ^= b;
        b ^= a;
        a ^= b;
    }

    return b ? gcd(b, a % b) : a;
}

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

int main() 
{
    long A, B; 
    cin >> A >> B;
    cout << lcm(A, B) << endl;
}


Measured memory_bytes: 256
from decimal import Decimal,getcontext
from math import sqrt
# getcontext().prec = 10000 # 標準は28,精度を調節
a,b,c=map(Decimal,input().split())
print("Yes" if a.sqrt()+b.sqrt()<c.sqrt() else "No") # sqrt(a)とかだとfloatになるよ

Measured memory_bytes: 5076
from collections import Counter

N = int(input())
lst = list(map(int, input().split()))

C = Counter(lst)
odd = 0
even = 0
for c in C:
    if C[c] % 2 == 0:
        even += 1
    else:
        odd += 1

if even % 2 == 0:
    print (len(C))
else:
    print (len(C) - 1)


Measured memory_bytes: 18648
#include <stdio.h>
    
int main(void){
    int a,b;
    scanf("%d%d",&a,&b);
    int ans;
    if (a > b){
        ans += a;
        a -= 1;
    } else {
        ans += b;
        b -= 1;
    }
    
    if (a > b){
        ans += a;
        a -= 1;
    } else {
        ans += b;
        b -= 1;
    }
    printf("%d",ans);
}

Measured memory_bytes: 128
#include <iostream>
#include <iomanip>
#include <cassert>

#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

#define rep_init(i, init, n) for(int i = (init); i < (n); ++i)
#define rep(i, n) rep_init(i, 0, n)


void Main()
{
	int X, Y, Z;
	cin >> X >> Y >> Z;

	int c = X / (Y+Z);
	if (c * (Y+Z) + Z > X) --c;

	cout << c << endl;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios_base::sync_with_stdio(false);
	std::cout << std::fixed << std::setprecision(15);

	Main();
	return 0;
}

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

using namespace std;

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)

lli data[26];

signed main(){

	string s;
	cin>>s;
	REP(i,0,s.size())data[s.at(i)-'a']++;

	REP(i,0,26){
		if(data[i]==0){
			cout<<s<<(char)('a'+i)<<endl;
			return 0;
		}
	}
	if(s=="zyxwvutsrqponmlkjihgfedcba")cout<<-1<<endl;
	else
	{
		string t=s;
		next_permutation(s.begin(),s.end());
		REP(i,0,s.size()){
			cout<<s[i];
			if(t[i]!=s[i]){
				cout<<endl;
				return 0;
			}
		}
	}

	return 0;
}

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

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define PB push_back
#define MP make_pair
#define ALL(v) v.begin(), v.end()

ll L, R;
int solve() {
    if (R-L >= 2019) return 0;
    ll ans = 2019;
    FOR (l, L, R) FOR (r, l+1, R+1) {
        ans = min(ans, l*r%2019);
    }
    return ans;
}
int main() {
    cin >> L >> R;
    cout << solve() << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  vector<long long> a(n*3);
  for(int i=0;i<n*3;i++) cin >> a[i];
  priority_queue<long long> right;//右は大きいものが抜けてほしい
  priority_queue<long long,vector<long long>,greater<long long>> left;//左は小さいものが抜けてほしい
  vector<long long> leftdp(n+1,0),rightdp(n+1,0);
  for(int i=0;i<n;i++){
    left.push(a[i]);
    leftdp[0]+=a[i];
    right.push(a[i+2*n]);
    rightdp[0]+=a[i+2*n];
  }
  for(int i=0;i<n;i++){
    left.push(a[i+n]);
    leftdp[i+1]=leftdp[i]+a[i+n]-left.top();
    left.pop();
    right.push(a[2*n-1-i]);
    rightdp[i+1]=rightdp[i]+a[2*n-1-i]-right.top();
    right.pop();
  }
  long long ans = -1e18;
  for(int i=0;i<n+1;i++){
    ans = max(ans,leftdp[i]-rightdp[n-i]);
  }
  cout << ans << endl;
}

Measured memory_bytes: 5996
#include <stdio.h>
#include <vector>
#include <iostream>
#include <string>
using namespace std;

int main(void) {
	vector<string> r;
	while (true)
	{
		int gem=0,gemgot=0;
		int map[21][21]={{0}};
		cin >> gem;
		if (gem==0){break;}
		for(int i=0;i<gem;i++)
		{
			int x=0,y=0;
			scanf("%d %d",&x,&y);
			map[x][y] = 1;
		}
		int rx=10,ry=10;
		int n;
		cin >> n;
		for(int i=0;i<n;i++)
		{
			int d=0;
			int dx=0,dy=0;
			char dest[8];
			scanf("%s %d",dest,&d);
			if (dest[0] == 'N'){
				dy = 1;
			} else if (dest[0] == 'S'){
				dy = -1;
			} else if (dest[0] == 'W'){
				dx = -1;
			} else if (dest[0] == 'E'){
				dx = 1;
			}
			for (int j=0;j<d;j++)
			{
				rx += dx;
				ry += dy;
				if (map[rx][ry] == 1)
				{
					map[rx][ry] = 0;
					gemgot++;
				}
			}
		}
		if (gem == gemgot){
			r.push_back("Yes");
		} else {
			r.push_back("No");
		}
	}
	for(int i=0; i<r.size(); i++)
	{
		cout << r[i] << endl;
	}
	return 0;
}

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

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
long long N, M, a[100009][3], maxn = -(1LL << 10);
 
int main() {
	cin >> N >> M;
	for (int i = 1; i <= N; i++) {
		for (int j = 0; j < 3; j++) cin >> a[i][j];
	}
	for (int i = 0; i < 8; i++) {
		vector<long long>vec;
		for (int j = 1; j <= N; j++) {
			long long S = 0;
			for (int k = 0; k < 3; k++) { 
                if ((i / (1 << k)) % 2 == 0) 
                    S += a[j][k]; else S -= a[j][k]; }
			vec.push_back(S);
		}
		sort(vec.begin(), vec.end()); reverse(vec.begin(), vec.end());
		long long ans = 0;
		for (int j = 0; j < M; j++) ans += vec[j];
		maxn = max(maxn, ans);
	}
	cout << maxn << endl;
	return 0;
}

Measured memory_bytes: 3668
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

struct query{
    int y,x,s;
};

int n;
int a[6010];
int dp[2010][2010];
int ma[2010],M;
int cnt[2010][2010];
vector<query> kousin;

void add(int y,int x,int s){
    query q;q.y=y;q.x=x;q.s=s;
    kousin.push_back(q);
}

void flush(){
    while(!kousin.empty()){
        int y=kousin.back().y,x=kousin.back().x,s=kousin.back().s;
        dp[y][x]=max(dp[y][x],s);
        dp[x][y]=dp[y][x];
        ma[y]=max(ma[y],dp[y][x]);
        ma[x]=max(ma[x],dp[y][x]);
        M=max(M,dp[y][x]);
        kousin.pop_back();
    }
}

void solve(){
    cin >> n;
    rep(i,3*n){
        cin >> a[i];a[i]--;
    }
    a[3*n]=n;
    a[3*n+1]=n;
    rep(i,n+1){
        rep(j,n+1){
            dp[i][j]=-mod;
        }
        ma[i]=-mod;
    }
    dp[a[0]][a[1]]=0;
    dp[a[1]][a[0]]=0;
    ma[a[0]]=0;
    ma[a[1]]=0;
    int c=0;
    rep(i,n){
        int x=a[3*i+2],y=a[3*i+3],z=a[3*i+4];
        if(x==y && y==z) {
            c+=1;
            continue;
        }
        rep(_,3){
            int tmp=x;x=y;y=z;z=tmp;
            if(x==y){
                rep(k,n+1){
                    add(k,z,dp[k][x]+1);
                }
            }
            rep(k,n+1){
                add(k,x,ma[k]);
            }
            add(x,y,M);
            add(x,y,dp[z][z]+1);
        }
        flush();
        // rep(i,n+1){
        //     rep(j,n+1){
        //         cout << i << " " << j << " " << dp[i][j] << endl;
        //     }
        // }
    }
    int ans=0;
    rep(i,n+1){
        rep(j,n+1){
            ans=max(ans,c+dp[i][j]);
        }
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}

Measured memory_bytes: 19464
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

int N,M,k;
int card[210];

void riffle(){
  int i,a=0,b=0;
  int A[210],B[210];

  rep(i,N+1) if(i != 0) A[a++] = card[i];
  for(; i < 2*N+1; i++) B[b++] = card[i];

  a = 0; b = 0;
  rep(i,2*N+1) if(i != 0){
    if(i%2 == 1) card[i] = A[a++];
    if(i%2 == 0) card[i] = B[b++];
  }
}

void cut(int k){
  int i,a=0,b=0,j;
  int A[210],B[210];

  rep(i,k+1) if(i != 0) A[a++] = card[i];
  for(; i < 2*N+1; i++) B[b++] = card[i];

  i = 1;
  rep(j,b) card[i++] = B[j];
  rep(j,a) card[i++] = A[j];
}

int main(){
  int i;

  rep(i,210) card[i] = i;

  scanf("%d",&N);
  scanf("%d",&M);

  while(M--){
    scanf("%d",&k);
    if(k == 0) riffle(); else cut(k);
  }

  rep(i,2*N+1) if(i != 0) printf("%d\n",card[i]);

  return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <cstdio>
#include <vector>
#include <set>
#include <cassert>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <functional>
using namespace std;
/**==Info==
*Program:C
*Problem:Splitting Pile
*Date:2017-7-15
*Algorithm:AtCoder*/
long long a[200001];
long long sum[200001];
int main(){
	int n;
	cin>>n;
	
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(i>0){
			sum[i]=sum[i-1]+a[i];
		}else{
			sum[i]=a[i];
		}
	}
	
	long long mn=1e18;
	for(int i=0;i<n-1;i++){
		if(abs(sum[i]-(sum[n-1]-sum[i]))<mn) mn=abs(sum[i]-(sum[n-1]-sum[i]));
	}
	
	cout<<mn;
	return 0;
}


Measured memory_bytes: 3328
fun main(args : Array<String>) {
    var (N,M,K)= ReadInts()
    var tree = UFtree(N)
    var frendscount = (1..N).map{0}.toMutableList()
    for(i in 1..M){
        var (X,Y)= ReadInts()
        tree.unite(X-1, Y-1)
        frendscount[X-1]+=1
        frendscount[Y-1]+=1
    }
    
    for(i in 0..(N-1)){
        tree.find(i)
        frendscount[i]+=1
    }
    for(i in 1..K){
        var (X,Y)=ReadInts()
        var XX=tree.find(X-1)
        var YY=tree.find(Y-1)
        if(XX==YY){
            frendscount[X-1]+=1
            frendscount[Y-1]+=1
        }
    }
    var dic = mutableMapOf<Int,Int>()
    for(i in 0..(N-1)){
        var root= tree.find(i)
        if(!dic.containsKey(root)){
            dic[root]=0
        }
        dic[root] = dic[root]!! + 1
    }
    println( (0..(N-1)).map{dic[tree.find(it)]!!-frendscount[it]}.joinToString(separator=" "))




}


class UFtree(size : Int){
    var root = (1..size).map{-1}.toMutableList()
    var rank = (1..size).map{0}.toMutableList()
    fun find(X: Int):Int{
        if(root[X]<0){
            return X
        }
        var newX=find(root[X])
        root[X]=newX
        return newX
    }
    fun unite(X:Int,Y:Int){
        var XX=find(X)
        var YY=find(Y)

        if(XX==YY){
            return
        }

        if(rank[XX]>rank[YY]){
            root[XX]+=root[YY]
            root[YY]=XX
        }else{
            if(rank[YY]==rank[XX]){
                rank[YY]+=1
            }
            root[YY]+=root[XX]
            root[XX]=YY
        }
    }
}



fun ReadInts():MutableList<Int>{
    return readLine()!!.split(" ").map{it.toInt()}.toMutableList()
}

fun ReadLongs():MutableList<Long>{
    return readLine()!!.split(" ").map{it.toLong()}.toMutableList()
}
fun ReadDoubles():MutableList<Double>{
    return readLine()!!.split(" ").map{it.toDouble()}.toMutableList()
}
fun input() : String{
    return readLine()!!

}

Measured memory_bytes: 91620
#include<bits/stdc++.h>
using namespace std;
int n;int a[100005];
long long sum[100005];
int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
    sort(a + 1 , a + n + 1);
    for(int i = 1;i <= n;i++) sum[i] = sum[i - 1] + a[i];
    long long ans = 0;
    for(int i = 1;i <= n;i++){
        if(i*2 < n - 1) continue;
        if((n-i)*2 < n - 1) continue;
        long long cl = sum[n] - sum[i];
        cl += (sum[n] - sum[n-(n-1)+(n-i)]);
        //printf("%d %d %d %d\n",i,n,n-(n-1)+(n-i)+1,n);
        cl -= sum[i] - sum[0];
        cl -= sum[(n-1)-i];ans = max(ans , cl);
    }
    cout<<ans;
    return 0;
}


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

int main() {
  
  int n;
  cin >> n;
  
  int ans = 0;
  if (n == 1) ans = 1;
  else{ 
    REP(i,n) {
      int j = 2;
      while(1) {
        if (pow(i+2,j) <= n) ans = max(ans,int(pow(i+2,j)));
        else break;
        j++;
      }
    }
  }
  
  cout << ans << endl;
  
}


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

int main(){
    int H, W;
    
    while(1){
        cin >> H >> W;
        if (H == 0 && W == 0) break;
        for (int i=0; i<H; i++){
            for (int j=0; j<W; j++){
                cout << "#";
            }
            cout << endl;
        }
        cout << endl;
    }
}




Measured memory_bytes: 3136
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <stdio.h>

using namespace std;

//typedefリスト
typedef long long llint;
typedef vector<long long int> vint;
typedef vector< vector<int> > vvint;
typedef vector<string> vstr;


int main(void) {
	int A, B;
	cin >> A >> B;
	if (A - 1 > B)cout << A+(A-1) << endl;
	else if (B - 1 > A)cout << 2 * B-1 << endl;
	else cout << A + B << endl;
}


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

long long N;
pair<long long, long long> AB[100005];
long long color[100005];
vector<long long> tree[100005];
set<long long> visit;
set<long long> allcolor;

int main(){
	cin >> N;
	long long maxi = 0;
	long long maxinode = 0;
	for(long long i = 0; i < N-1; i++){
		cin >> AB[i].first >> AB[i].second;
		AB[i].first--;AB[i].second--;
		tree[AB[i].first].push_back(i);
		tree[AB[i].second].push_back(i);
	}

	queue<long long> Q;
	Q.push(0);

	while(!Q.empty()){
		long long cur = Q.front();
		//cout << "cur = " << cur << endl;
		Q.pop();
		if(visit.count(cur) != 0){
			continue;
		}
		visit.insert(cur);
		set<long long> used;
		for(auto idx: tree[cur]){
			if(color[idx] > 0){
				//cout << "used insert " << color[idx] << endl;
				used.insert(color[idx]);
			}
		}

		long long cur_color = 1;
		for(auto idx: tree[cur]){
			//cout << "tree[" << cur << "] idx =" << idx << endl;
			long long to = AB[idx].first;
			if(to == cur){
				to = AB[idx].second;
			}
			//cout << " to " << to << endl;
			//cout << "color[" << idx << "]=" << color[idx] << endl;
			if(color[idx] == 0){
				while(used.count(cur_color) > 0){
					cur_color++;
				}
				color[idx] = cur_color;
				allcolor.insert(cur_color);
				used.insert(cur_color);
				Q.push(to);
			}
		}
	}

	cout << allcolor.size() << endl;
	for(long long i = 0; i < N-1; i++){
		cout << color[i] << endl;
	}
}

Measured memory_bytes: 13568
#include<stdio.h>

void Island1(int, int, int, int);
void Island2(int, int, int, int);

int data[50][50];

int main(){
  int i, j, w, h, x, count;

  while(1){
    scanf("%d %d", &w, &h);
    if(w == 0 && h == 0) break;

    for(i=0; i<h; i++){
      for(j=0; j<w; j++){
	scanf("%d", &data[i][j]);
      }
    }

    for(i=0, count = 0; i<h; i++){
      for(j=0; j<w; j++){
	if(data[i][j] == 1){
	  Island1(h, w, i, j);
	  count++;
	}
      }
    }

    printf("%d\n", count);
  }

  return 0;
}

void Island1(int h, int w, int i, int j){
  data[i][j] = 2;
  Island2(h, w, i-1, j-1);
  Island2(h, w, i-1, j);
  Island2(h, w, i-1, j+1);
  Island2(h, w, i, j-1);
  Island2(h, w, i, j+1);
  Island2(h, w, i+1, j-1);
  Island2(h, w, i+1, j);
  Island2(h, w, i+1, j+1);
}

void Island2(int h, int w, int i, int j){
  if(i > -1 && i < h && j > -1 && j < w && data[i][j] == 1)
    Island1(h, w, i, j);
}

Measured memory_bytes: 0
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define int long long
int n, h[123456], dp[123456];
signed main() {
	cin >> n;
	for (int i = 0; i < n; i++)cin >> h[i];
	dp[0] = 0;
	dp[1] = abs(h[1] - h[0]);
	for (int i = 2; i < n; i++)dp[i] = min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1]));
	cout << dp[n - 1] << endl;
}

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

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for(int (i) = (n); (i) < (m); (i)++)
#define repn(i, n) for(int (i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for(int (i) = (n-1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define ii(x) int x; cin >> (x)
#define ii2(x, y) int x, y; cin >> (x) >> (y)
#define ii3(x, y, z) int x, y, z; cin >> (x) >> (y) >> (z)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define yn(x) cout << ((x)?("Yes"):("No")) << endl
#define YN(x) cout << ((x)?("YES"):("NO")) << endl
#define bit_c(x) __builtin_popcountll(x)

inline void logger(){ std::cout << " [LOGGER] " << endl; }
template<typename A, typename... B>
void logger(const A& a, const B&... b){
    cout << a << " , "; logger(b...);
}

typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 100005;

/* ...o(^-^)o... */


int main(){
    int x, y; cin >> x >> y;
    for(int a=0; a<=x; a++){
        int b = x-a;
        if(2*a+4*b==y){
            out("Yes"); return 0;
        }
    }
    out("No");
}

Measured memory_bytes: 3636
object Main extends App {
  
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val t = Array.fill(n)(sc.nextInt())
  val m = sc.nextInt()
  val px = Array.fill(m)((sc.nextInt(), sc.nextInt()))
  px.foreach{ i =>
    var array = t.clone
    array(i._1 - 1) = i._2
    println(array.sum)
  }
}

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

int L;

bool canput(int k, string &S){
	int i = k - 1, j = k + 1;
	while(i >= 0 && S[i] == 'W'){
		i--;
	}
	while(j < L && S[j] == 'W'){
		j++;
	}
	if(i < 0 || j >= L){
		return true;
	}
	if(S[i] == 'B' && S[j] == 'B'){
		return false;
	}
	else{
		return true;
	}
}

int put(int k, string &S){
	int ans = 0;
	int i = k - 1;
	while(i >= 0 && S[i] == 'B'){
		i--;
	}
	if(i >= 0 && S[i] == 'W'){
		ans = max(ans, k - i - 1);
	}
	int j = k + 1;
	while(j < L && S[j] == 'B'){
		j++;
	}
	if(j < L && S[j] == 'W'){
		ans = max(ans, j - k - 1);
	}
	return ans;
}

int main(){
	int i, ans = 0;
	string S;
	scanf("%d", &L);
	cin >> S;
	for(i = 0; i < L; i++){
		if(S[i] == '.'){
			if(canput(i, S)){
				ans = max(ans, put(i, S));
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}


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

int main(void) {
  int N;
  cin >> N;
  int count[4] = {0, 0, 0, 0};
  string judge[4] = {"AC", "WA", "TLE", "RE"};
  
  string tmp;
  for(int i=0; i<N; i++) {
    cin >> tmp;
    if(tmp=="AC") count[0]++;
    else if(tmp=="WA") count[1]++;
    else if(tmp=="TLE") count[2]++;
    else count[3]++;
  }  
  
  for(int i=0; i<4; i++) {
    cout << judge[i] << " x " << count[i] << endl;
  }
  
  return 0;
}

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

typedef long long ll;

int n,T;
ll dp[50];
vector<int> grd;
vector<pair<int,int> > v;

int main(){

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

    cin>>n>>T;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        b+=a+1;
        if(a)v.emplace_back(a,b);
        else grd.push_back(b);
    }
    sort(v.begin(),v.end(),[](pair<int,int> a,pair<int,int> b){
        return a.first*b.second>b.first*a.second;
    });
    sort(grd.begin(),grd.end());
    for(int i=1;i<32;i++)dp[i]=T+1;
    for(int i=0;i<v.size();i++){
        for(int j=30;j>=0;j--){
            dp[j+1]=min(dp[j+1],(v[i].first+1)*dp[j]+v[i].second);
        }
    }
    int ans=0;
    for(int i=0;i<32&&dp[i]<=T;i++){
        ll lft=T-dp[i];
        int pos=0;
        while(pos<grd.size()&&lft>=grd[pos]){
            lft-=grd[pos];
            pos++;
        }
        ans=max(ans,pos+i);
    }
    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 2420
get_number(Number) :-
    get_string(String),
    number_string(Number, String).

get_string(String) :-
    current_input(Input),
    read_string(Input, ' \n', '', _, String).

/* memo
    code(int)
    string_code(1, S, C), (C: code)
    char_code(X, Y) (X: char, Y: code)
    string_chars(S, S2) (S: string, S2: [char])
    to_lower(C, C2) (C: char? C2: code?)

    f(0, [], []).
    f(Z, [X|Xs], [Y|Ys]) :- X = Y, f(Z1, Xs, Ys), Z is Z1 + 1.
    f(Z, [_|Xs], [_|Ys]) :- f(Z, Xs, Ys).

    match: =, \=

    numeric: <,=<, >, >= (eval)
    lexicographically: @<. @=<, @>, @>=
    value?: =:=, =\= (eval)
    identical: ==, \==
*/

main :-
    get_number(A),
    get_number(B),
    X is A / B,
    writeln(X).


Measured memory_bytes: 8128
"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    const N = parseInt(arg[0]);
    const S = arg.slice(1, N + 1);
    
    const alphabets = "abcdefghijklmnopqrstuvwxyz".split("");
    
    let answer = [];
    
    for(let i in alphabets) {
        let contains = Infinity;
        
        for(let j in S) {
            let cnt = S[j].split("").filter(n=>n===alphabets[i]).length;
            
            if(cnt > 0) {
                contains = Math.min(cnt, contains);
            } else {
                contains = 0;
                break;
            }
        }
        
        for(let j=0; j<contains; j++) {
            answer.push(alphabets[i]);   
        }
    }
    
    console.log(answer.join(""));
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 20936
use std::io;
use std::str::FromStr;

fn input_tuple() -> (i64, i64, i64) {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    let vec: Vec<i64> = line.split_whitespace().map(|s| s.parse().unwrap()).collect();
    (vec[0], vec[1], vec[2])
}

fn main() {
    let (n, a, b) = input_tuple();
    if (a - b).abs() % 2 == 0 {
        println!("{}", (a - b).abs() / 2);
        return;
    }
    if a - 1 < n - b {
        println!("{}", a - 1 + 1 + (b - a - 1).abs() /2);        
    } else {
        println!("{}", n - b + 1 + (b - a - 1).abs() / 2);
    }
}


Measured memory_bytes: 4352
import java.util.Scanner;

public class Main {

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        int n  = sc.nextInt();
        int[] a  = new int[n];

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

        int res = 0;
        for(int i=0; i<n-2; i++) {
            if (a[i] < a[i + 1] && a[i + 1] < a[i + 2])
                res++;
            if (a[i] > a[i + 1] && a[i + 1] > a[i + 2])
                res++;
        }

        System.out.println(res);

    }


}


Measured memory_bytes: 23380
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		if( N != S.Length ){ Console.WriteLine(0); return; }
		
		var RH0 = new RollingHash(S,2525,(long)1e9+7);
		var RH1 = new RollingHash(S,5252,(long)1e9+9);
		int l = 0, r = 0;
		HashSet<ulong> H = new HashSet<ulong>();
		for(int q=0;q<M;q++){
			var qry = Query[q];
			switch(qry){
				case "L++": l++;break;
				case "L--": l--;break;
				case "R++": r++;break;
				case "R--": r--;break;
				default: break;
			}
			H.Add((ulong)RH0.GetHash(l,(r-l+1))*(ulong)RH1.GetHash(l,(r-l+1)));
		}
		
		Console.WriteLine(H.Count);
		
	}
	
	int N,M;
	String S;
	String[] Query;
	public Sol(){
		var d = ria();
		N = d[0]; M = d[1];
		S = rs();
		Query = new String[M];
		for(int i=0;i<M;i++) Query[i] = rs();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class RollingHash{
	
	long[] Hash;
	long[] Base;
	String S;
	long mod;
	int N;
	
	public RollingHash(String s_,long base_=2525,long mod_=(long)1e9+7){
		mod=mod_;
		S=s_;
		N=S.Length;
		Hash=new long[N+1];
		
		Base=new long[N+1];
		Base[0]=1;
		Base[1]=base_;
		
	
		for(int i=1;i<N;i++){
			Base[i+1]=Base[i]*Base[1];
			if(Base[i+1]>=mod)Base[i+1]%=mod;
		}
		
		Hash[0]=0;
		Hash[1]=(long)S[0];
		for(int i=1;i<N;i++){
			Hash[i+1]=Hash[i]*Base[1]+(long)S[i];
			if(Hash[i+1]>=mod)Hash[i+1]%=mod;
		}
	}
	
	public long GetHash(int strt,int len){
		//return hash of S.Substring(strt,len);
		if(len==0)return 0;
		if(strt+len>=N)len=N-strt;
		long ret=Hash[strt+len]+mod-(Hash[strt]*Base[len]>=mod?Hash[strt]*Base[len]%mod:Hash[strt]*Base[len]);
		return ret>=mod?ret%mod:ret;
	}

}

Measured memory_bytes: 45104
#include <stdio.h>

int main()
{
	int a[3] = { 0 };
	scanf("%d", &a[0]);
	scanf("%d", &a[1]);
	scanf("%d", &a[2]);



	int temp = 0;
	if (a[0] > a[1])
	{
		temp = a[0];
		a[0] = a[1];
		a[1] = temp;
	}
	if (a[1] > a[2])
	{
		temp = a[1];
		a[1] = a[2];
		a[2] = temp;
	}
	if (a[0] > a[1])
	{
		temp = a[0];
		a[0] = a[1];
		a[1] = temp;
	}
	
	
	int n = (a[2] - a[1]) + (a[1] - a[0]);

	printf("%d", n);



	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
int main(){
  char s[100000];
  int i,sw=0,sum=0;

  scanf("%s",s);

  for(i=0;i<99999;i++){
    if(s[i+1] == NULL)
      break;
      
    if(s[i] == '1'){
      if(s[i+1] == '1'){
        sum += 1;
        s[i+1] = '0';
      }
    }else if(s[i] == '0'){
      if(s[i+1] == '0'){
        sum += 1;
        s[i+1] = '1';
      }
    }
      
  }

  printf("%d",sum);

  return 0;
}


Measured memory_bytes: 256
s = input()
s0, s1 = int(s[:2]), int(s[2:])
if (1 <= s0 <= 12) and (1 <= s1 <= 12):
    print('AMBIGUOUS')
elif 1 <= s0 <= 12:
    print('MMYY')
elif 1 <= s1 <= 12:
    print('YYMM')
else:
    print('NA')

Measured memory_bytes: 3060
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		String str = sc.next();
		
		int start = str.indexOf("A");
		int end = str.lastIndexOf("Z");
		
		System.out.println(end - start + 1);
	}
}


Measured memory_bytes: 26416
#include<stdio.h>

int main()
{
	int card[52] = {}, num, i, n;
	char mark;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		while (1)
		{
			mark = getchar();
			if (mark != '\n')
				break;
		}
		scanf("%d", &num);
		switch (mark)
		{
		case 'S':
			card[num - 1] = 1;
			break;
		case 'H':
			card[num + 12] = 1;
			break;
		case 'C':
			card[num + 25] = 1;
			break;
		case 'D':
			card[num + 38] = 1;
			break;
		}
	}
	for (i = 0; i < 52; i++)
	{
		if (!card[i])
		{
			switch (i / 13)
			{
			case 0:
				printf("S %d\n", i + 1);
				break;
			case 1:
				printf("H %d\n", i - 12);
				break;
			case 2:
				printf("C %d\n", i - 25);
				break;
			case 3:
				printf("D %d\n", i - 38);
				break;
			}
		}
	}

	return 0;
}


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

using namespace std;

const int M = 20000007;
const int inf = 1000000000;

int Pool = 0;

vector<pair<int, int> > G[M];
int lc[M], rc[M];

class SegmentTree {
	public:
	int N;
	int root;

	SegmentTree(int _N) { 
		N = _N;
		root = -1;
	}

	void build(int l, int r, int &x, const vector<int> &a) {
		if(x == -1) {
			x = Pool++;
		}
		if(l == r) {
			G[x].emplace_back(a[l], 1);
			return;
		}
		int mid = l + r >> 1;
		build(l, mid, lc[x], a);
		build(mid + 1, r, rc[x], a);
		G[x].emplace_back(lc[x], 0);
		G[x].emplace_back(rc[x], 0);
	} 

	void update(int l, int r, int x, int a, int b, int cur) {
		if(l > b || r < a) {
			return;
		}
		if(l >= a && r <= b) {
			G[cur].emplace_back(x, 0);
			return;
		}
		int mid = l + r >> 1;
		update(l, mid, lc[x], a, b, cur);
		update(mid + 1, r, rc[x], a, b, cur);
	}
};

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

	int h, w, k;
	cin >> h >> w >> k;

	vector<string> s(h);

	int sx, sy, tx, ty;
	cin >> sx >> sy >> tx >> ty;
	--sx; --sy; --tx; --ty;
	
	for(int i = 0; i < h; ++i) {
		cin >> s[i];
	}

	memset(lc, -1, sizeof(lc));
	memset(rc, -1, sizeof(rc));

	vector<SegmentTree> row(h, SegmentTree(w));
	vector<SegmentTree> col(w, SegmentTree(h));

	Pool = h * w;

	auto id = [&] (int i, int j) {
		return i * w + j;
	};	

	for(int i = 0; i < h; ++i) {
		vector<int> a;
		for(int j = 0; j < w; ++j) {
			a.emplace_back(id(i, j));
		}
		row[i].build(0, w - 1, row[i].root, a);
	}

	for(int i = 0; i < w; ++i) {
		vector<int> a;
		for(int j = 0; j < h; ++j) {
			a.emplace_back(id(j, i));
		}
		col[i].build(0, h - 1, col[i].root, a);
	}

	for(int i = 0; i < h; ++i) {
		int last = -1;
		for(int j = 0; j < w; ++j) {
			if(s[i][j] != '@') {
				row[i].update(0, w - 1, row[i].root, max(last + 1, j - k), j, id(i, j));
			} else {
				last = j;
			}
		}
		last = w;
		for(int j = w - 1; j >= 0; --j) {
			if(s[i][j] != '@') {
				row[i].update(0, w - 1, row[i].root, j, min(last - 1, j + k), id(i, j));
			} else {
				last = j;
			}
		}
	}

	for(int i = 0; i < w; ++i) {
		int last = -1;
		for(int j = 0; j < h; ++j) {
			if(s[j][i] != '@') {
				col[i].update(0, h - 1, col[i].root, max(last + 1, j - k), j, id(j, i));
			} else {
				last = j;
			}
		}
		last = h;
		for(int j = h - 1; j >= 0; --j) {
			if(s[j][i] != '@') {
				col[i].update(0, h - 1, col[i].root, j, min(last - 1, j + k), id(j, i));
			} else {
				last = j;
			}
		}
	}

	deque<int> q;
	q.push_front(id(sx, sy));

	vector<int> d(M, inf);
	d[id(sx, sy)] = 0;

	while(!q.empty()) {
		int u = q.front();
		q.pop_front();

		for(auto [v, w] : G[u]) {
			if(w == 0) {
				if(d[v] > d[u]) {
					d[v] = d[u];
					q.push_front(v);
				}
			} else {
				if(d[v] > d[u] + 1) {
					d[v] = d[u] + 1;
					q.push_back(v);
				}
			}
		}
	}

	if(d[id(tx, ty)] == inf) {
		cout << -1 << '\n';
	} else {
		cout << d[id(tx, ty)] << '\n';
	}

	return 0;
}

Measured memory_bytes: 983184