akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

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

    int n;
    cin >> n;

    map <int, int> mp;
    vector <int> vec;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        if (mp[a] == 0) vec.push_back(a);
        mp[a]++;
    }

    if (vec.size() == 1) {
        if (mp[0] == n) {
            cout << "Yes\n";
            return 0;
        }
    }

    if (n % 3 != 0) {
        cout << "No\n";
        return 0;
    }

    if (vec.size() == 2) {
        if (mp[0] == n / 3) {
            cout << "Yes\n";
            return 0;
        }
    }

    if (vec.size() == 3) {
        bool f = true;

        if ((vec[0] ^ vec[1] ^ vec[2]) != 0) f = false;
        if (mp[vec[0]] != n / 3) f = false;
        if (mp[vec[1]] != n / 3) f = false;
        if (mp[vec[2]] != n / 3) f = false;

        if (f) {
            cout << "Yes\n";
            return 0;
        }
    }

    cout << "No\n";
    return 0;
}

Measured memory_bytes: 6008
import fractions
N, X = map(int, input().split())
xs = list(map(int, input().split()))
xs.append(X)
xs.sort()
dif = []
for i in range(N):
    dif.append(xs[i+1] -xs[i])
if len(dif) > 1:
    ans = fractions.gcd(dif[0], dif[1])
    for i in range(N):
        ans = fractions.gcd(ans, dif[i])
    print(ans)
else:
    print(dif[0])

Measured memory_bytes: 16240
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main()
 {  ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
	//int t; 
      // / cin>>t;
       // /while(t--){
        int n,flag=1;
        cin>>n;
        int a[n];
        
        unordered_map<int,int>m;
        for(int i=0;i<n;i++){
            cin>>a[i];
            m[a[i]]++;
        }
        for(int i=0;i<n;i++){
            if(m[a[i]]>1) {flag=0;break;}
        }
        cout<<((flag)?"YES\n":"NO\n");
	return 0;
}

Measured memory_bytes: 9288
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <list>
using namespace std;

#define REP(i,a,n) for(i=a; i<n; i++)
#define rep(i,n) REP(i,0,n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define foreach(it,x) for(typeof(x.begin()) it=x.begin(); it!=x.end(); it++)

struct sector{
    int label, s,e; // [s,e]
    sector(int _l, int _s, int _e){
        label = _l, s = _s, e = _e;
    }
};

struct filesystem{
    list<sector> li;

    void clear(){ li.clear(); }

    void write(int l, int s){
        int num = 0;
        foreach(it,li){
            if( s <= 0 ) break;
            if( num < it->s ){
                li.insert(it, sector(l, num, min(s+num-1,it->s-1)));
                s -= (it->s - 1 - num) + 1;
            }
            num = it->e+1;
        }
        if( s > 0 ){
            li.push_back(sector(l,num,s+num-1));
        }
    }

    void del(int l){
        foreach(it,li){
            if( it->label == l ){
                it = li.erase(it);
                it--;
            }
        }
    }

    int read(int p){
        foreach(it,li){
            if( it->s <= p && p <= it->e ) return it->label;
        }
        return -1;
    }

    void print(){
        foreach(it,li){
            printf("label = %d, [%d,%d]\n", it->label, it->s, it->e);
        }
    }
};

int main(){
    int T,i,j,k,l,s,p;
    filesystem fs;

    while( cin >> T, T ){
        fs.clear();

        rep(i,T){
            char ope; cin >> ope;
            if( ope == 'W' ){
                cin >> l >> s;
                fs.write(l,s);
                //printf("W %d %d\n", l,s);
                //fs.print();
            }
            else if( ope == 'D' ){
                cin >> l;
                fs.del(l);
                //printf("D %d\n",l);
                //fs.print();
            }
            else{ // R
                cin >> p;
                cout << fs.read(p) << endl;
            }
        }
        puts("");
    }

    return 0;
}

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

int main() {
	
	string s;
	cin >> s;
	int64_t k;
	cin >> k;
	for(int i=0; i<k; i++){
		if(s.at(i)!='1') {
			cout << s.at(i) << endl;
			return 0;
		} 
	}
	cout << 1 << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
	int l,a,b,c,d,x,y,z;
	scanf("%d %d %d %d %d",&l,&a,&b,&c,&d);
	x=a/c;
	if(a%c>0){
		x+=1;
	}
	y=b/d;
	if(b%d>0){
		y+=1;
	}
	if(x>y){
		z=l-x;
	}
	else{
		z=l-y;
	}
	printf("%d\n",z);
	return 0;
}

Measured memory_bytes: 2708
n = int(input())
c = [0] * (n + 1)
s = [0] * (n + 1)
f = [0] * (n + 1)

for i in range(1, n):
    x, y, z = map(int, input().strip().split( ' '))
    c[i] = x
    s[i] = y
    f[i] = z

def time(i, after=0):
    #print(i, after, c[i], s[i], f[i])
    if i == n:
        return after
    if (after < s[i]):
        start_time = s[i]
    else:
        if (after - s[i]) % f[i] == 0:
            start_time = after
        else:
            start_time = ((after - s[i])  // f[i] + 1) * f[i] + s[i]
    return time(i + 1, after=start_time+c[i])

for i in range(1,n+1):
    print(time(i))


Measured memory_bytes: 3700
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        String S = scanner.next();
        long k = 1;
        String prev = S.substring(0, 1);
        int start = 0;
        for (int i = 1; i < S.length(); i++) {
            String chk = S.substring(i + start, i + 1);
            if (chk.compareTo(prev) == 0) {
                start--;
            } else {
                prev = S.substring(i + start, i + 1);
                start = 0;
                k++;
            }
        }

        System.out.println(k);
    }

}


Measured memory_bytes: 40620
#include <bits/stdc++.h>
#define INF (1<<30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;

int main(){
	int n; cin >> n;
	int l = n/100;
	int r = n%100;
	if( 1 <= l && l <= 12 && 1 <= r && r <= 12 ){
		cout << "AMBIGUOUS" << endl;
	} else if( 1 <= l && l <= 12 && !(1 <= r && r <= 12) ){
		cout << "MMYY" << endl;
	} else if( !(1 <= l && l <= 12) && 1 <= r && r <= 12 ){
		cout << "YYMM" << endl;
	} else {
		cout << "NA" << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int x,y;
	cin >> x >> y ;
	cout << x*y - (x+y) + 1  << endl;
	
	return 0;
}

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

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  int ans = 0;
  rep(i, n) {
    int na = a[i] - 1;
    if(a[na] == i + 1) ans++;
  }
  cout << ans / 2 << endl;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i) = 0; (i) < (n); (i)++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }


int main() {
	int n, m;
	cin >> n >> m;
	vector<int> keys(n, 0);
	rep(i,m){
        int l, r;
        cin >> l >> r;
        l--; r--;
        keys[l]++;
        if(r+1 < n) keys[r+1]--;
	}
	int ans = 0;
	for(int i = 0; i < n-1; i++){
        if(keys[i] == m) ans++;
        keys[i+1] += keys[i];
    }
    if(keys[n-1] == m) ans++;
    cout << ans << endl;
}


Measured memory_bytes: 640
#include <iostream>
#include <cstdio>

using namespace std;
 
int saidai(int a, int b);
int saisho(int a, int b);

int main(){
	int a, b;

	while(cin >> a >> b){
		if(a<b) swap(a,b);
		
		cout << saidai(a, b) << " " << saisho(a,b) << endl;
	}
}

int saidai(int a, int b){
	
	int x=1;
	for(int i=2; i<=a; i++){
		while(1){
		if(a%i == 0 && b%i == 0){
			 x = x * i;
			 a =a/i;
			 b =b/i;
		}
		 else break;
		}
	}

	return x;
}

int saisho(int a, int b){

	int y=1;
	for(int i=2; i<=a; i++){
		while(1){
		if(a%i == 0 && b%i == 0){
			 y = y * i;
			 a =a/i;
			 b =b/i;
		}
		 else break;
		}
	}

	y = y * a * b;

	return y;
}





Measured memory_bytes: 3028
import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int N;
long[] X, Y;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      X = new long[N];
      Y = new long[N];
      foreach (i; 0 .. N) {
        X[i] = readLong();
        Y[i] = readLong();
      }
      
      long x0;
      foreach (i; 0 .. N) {
        chmin(x0, X[i]);
      }
      long l;
      foreach (t; 0 .. 60) {
        const y = 1L << t;
        int sum;
        foreach (i; 0 .. N) {
          const d = X[i] - x0;
          const k = y - Y[i];
          if (0 <= k && k <= d) {
            if ((d & k) == k) {
              sum ^= 1;
            }
          }
        }
        if (sum) {
          l |= 1L << t;
        }
      }
      const ans = x0 + l;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


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

void dfs(vector<vector<int>> &G, vector<int> &D, int v) {
    for (int next : G[v]) {
        if (D[next] != -1) continue;
        D[next] = D[v] + 1;
        dfs(G, D, next);
    }
}

int main() {
    int N, U, V; cin >> N >> U >> V;
    U--; V--;

    vector<vector<int>> G(N);
    REP(i, 0, N - 1) {
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector<int> D_V(N, -1);
    D_V[V] = 0;
    dfs(G, D_V, V);
    
    vector<int> D_U(N, -1);
    D_U[U] = 0;
    dfs(G, D_U, U);

    int dist = 0;
    REP(i, 0, N) {
        if (dist < D_V[i] && D_V[i] > D_U[i]) dist = D_V[i];
    }
    cout << dist - 1 << endl;
    return 0;
}

Measured memory_bytes: 10368
n=int(input())
B=list(map(int,input().split()))
#a,b=map(int,input().split())
#A=[[0 for i in range()] for j in range()]
#data_list = [ list(map(int,input().split(" "))) for i in range(3)]

ans=B[0]

for i in range(n-2):
  ans+=min(B[i],B[i+1])
  
ans+=B[n-2]

print(ans)

Measured memory_bytes: 38384
n = int(input())
a = list(map(int, input().split()))

res = 10**8
for i in range(n):
    t = 0
    while a[i] % 2 != 1:
        a[i] = a[i]//2
        t += 1
    # print(a[i], t)
    res = min(res, t)

print(res)


Measured memory_bytes: 2940
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int l = 0;
        int r = 1_000_000_000;
        while(1 < r-l){
            int m = (l+r)/2;
            int c = 0;
            for (int i = 0; i < n; i++) {
                c += (a[i] + m -1) / m -1;
            }
            if(c <= k) r = m;
            else l = m;
        }
        System.out.println(r);
        sc.close();

    }

}


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

int main(void) {
    long long n;
    long long ae[5];
    long long min = 1000000000000001;
    long long ans;
    cin >> n;
    for(int i = 0; i < 5; i++){
        cin >> ae[i];
        if(min > ae[i]){
            min = ae[i];
        }
    }
    if(n < min){
        ans = 5;
    }else{
        ans = n / min + 5;
        if(n%min == 0){
            ans--;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
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; }
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1<<29);

int main() {

    int n;
    cin >> n;
    ll res = 0;

    vector<vector<ll>> num(10, vector<ll>(10, 0));

    for(int i = 1; i <= n; i++) {
        vector<int> d;
        int ni = i;
        while (ni) {
            d.push_back(ni % 10);
            ni /= 10;
        }
        int a = d[0];
        int b = d.back();
        if (!a || !b) continue;
        num[a][b]++;
    }

    for(int i = 1; i < 10; i++) {
        for(int j = 1; j < 10; j++) {
            res += num[i][j]*num[j][i];
        }
    }

    cout << res << endl;

    return 0;
}

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

long long n, k;
vector<long long> a, mi, pl, zero;

long long solve();
bool chpl(long long now);
bool chmi(long long now);

int main() {
  cin >> n >> k;
  a.resize(n);
  for(int i = 0; i < n; ++i) cin >> a[i];
  sort(a.begin(), a.end());
  cout << solve() << endl;
  return 0;
}

long long solve() {
  k = (n * (n - 1) / 2) - k + 1;
  for(int i = 0; i < n; ++i)
    if(a[i] < 0)
      mi.push_back(-a[i]);
    else if(a[i] > 0)
      pl.push_back(a[i]);
    else
      zero.push_back(0);
  sort(pl.begin(), pl.end());
  sort(mi.begin(), mi.end());
  if(k <= (mi.size() * (mi.size() - 1) / 2) +
              (pl.size() * (pl.size() - 1) / 2)) {
    long long l = 0, r = 1e18 + 1;
    while(r - l > 1) {
      long long mid = (l + r) / 2;
      if(chpl(mid))
        l = mid;
      else
        r = mid;
    }
    return l;
  }
  k -= (mi.size() * (mi.size() - 1) / 2) +
       (pl.size() * (pl.size() - 1) / 2);
  if(k <= zero.size() * (pl.size() + mi.size()) +
              (zero.size() * (zero.size() - 1) / 2))
    return 0;
  k -= zero.size() * (pl.size() + mi.size()) +
       (zero.size() * (zero.size() - 1) / 2);

  long long l = -1e18, r = 0;
  while(r - l > 1) {
    long long mid = (l + r) / 2;
    if(chmi(-mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}

bool chpl(long long now) {
  long long plsize = pl.size(), misize = mi.size(), sum = 0;
  for(int i = 0; i < plsize; ++i) {
    auto it = lower_bound(pl.begin(), pl.end(),
                          (now + pl[i] - 1) / pl[i]);
    sum += pl.end() - it;
    if(it - pl.begin() <= i) --sum;
  }
  for(int i = 0; i < misize; ++i) {
    auto it = lower_bound(mi.begin(), mi.end(),
                          (now + mi[i] - 1) / mi[i]);
    sum += mi.end() - it;
    if(it - mi.begin() <= i) --sum;
  }
  return sum / 2 >= k;
}

bool chmi(long long now) {
  long long plsize = pl.size(), misize = mi.size(), sum = 0;
  for(int i = 0; i < plsize; ++i) {
    auto it =
        upper_bound(mi.begin(), mi.end(), now / pl[i]);
    sum += it - mi.begin();
  }
  for(int i = 0; i < misize; ++i) {
    auto it =
        upper_bound(pl.begin(), pl.end(), now / mi[i]);
    sum += it - pl.begin();
  }
  return sum / 2 >= k;
}

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

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long ll;

ll n, m;
vector<int> graph[100005];
int memo[100005];

int findpath(int node){
	int maxlen = 0;
	for(auto x : graph[node]){
		if(memo[x] != -1)	maxlen = max(maxlen,1 + memo[x]);
		else maxlen = max(maxlen, 1 + findpath(x));
	}
	return memo[node] = maxlen;
}

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

	cin >> n >> m;
	while(m--){
		int a, b;
		cin >> a >> b;
		graph[a].PB(b);
	}

	memset(memo, -1, sizeof(memo));

	int mmax = -1;
	for(int i = 1; i <=n; i++){
		if(memo[i] != -1){
			mmax = max(mmax, memo[i]);
		}
		else{
			mmax = max(mmax, findpath(i));
		}
	}

	cout << mmax << "\n";

	return 0;
}

Measured memory_bytes: 8192
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    #→1,.→0として読み込む
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '#' else 0 for s in read()[:-1]])
        # 内包表記はpypyでは若干遅いことに注意
        # #numpy使うだろうからこれを残しておくけど
    return ret


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from math import gcd


def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a * b // g


N = read_a_int()
ans = 0
for i in range(1, N + 1):
    if i % 3 and i % 5:
        ans += i
print(ans)


Measured memory_bytes: 68060
#include <stdio.h>
#include <stdlib.h>

int min(int a,int b){
  if(a < b){
    return a;
  }
  return b;
}

int main(void){
  int n, k;
  scanf("%d%d", &n, &k);

  int h[n], dp[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
    dp[i] = 1e9;
  }
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      if(i-j < 0) break;
      dp[i] = min(dp[i-j]+abs(h[i]-h[i-j]), dp[i]);
    }
  }

  printf("%d\n", dp[n-1]);

  return 0;
}

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

using namespace std;

int main() {
  int x, a, b;
  cin >> x >> a >> b;

  if (abs(x -a) < abs(x - b)) {
    cout << "A" << endl;
  } else {
    cout << "B" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
import java.util.*;
    import java.io.*;
    public class Main {
        
        static class N{
            int a,b;
            public String toString(){
                return a+" "+b;
            }
        }
        static boolean isp(long g){
            if(g<=3) return true;
            for(int i=2;i<=(int)Math.sqrt(g);i++){
                if(g%i==0) return false;
            }
            return true;
        }
        static long ge(int arr[], long l, long r, int n){
            while(true){
                long m = (l+r)/2;
                long val = getans(arr, m, n);
                long left = getans(arr, m-1, n);
                long right = getans(arr, m+1, n);
                if(left>=val && right>=val) return val;
                if(right>val) r = m-1;
                else l=m+1;
            }
        }
        /*static int  getp(int l, int r, long a, long b){
            while(true){
            int m  =(l+r)/2;
            System.out.println(m);
            long val = m*(a-m)*(b-m);
            long left = ((m-1)*(a-m+1)*(b-m+1));
            long right = ((m+1)*(a-m-1)*(b-m-1));
            if(val>=left && val>= right) return m;
            if(val > right) {
                r=m-1;
                continue;
            }
            l =m+1;
            }
        }*/
        static long getans(int arr[] , long b, int n){
            long ans =0;
            for(int i=0;i<n;i++){
                ans+=Math.abs(arr[i]-(b+i+1));
            }
            return ans;
        }
        public static void main(String args[]) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            int tc = 1;//Integer.valueOf(br.readLine());
            for(int i=0;i<tc;i++){
                int n  = Integer.valueOf(br.readLine());
                int arr[] = new int[n];
                String gf[] = br.readLine().split(" ");
                for(int ii=0;ii<n;ii++) arr[ii] = Integer.valueOf(gf[ii]);
                System.out.println(ge(arr,Integer.MIN_VALUE, Integer.MAX_VALUE, n));
                long mina =Integer.MAX_VALUE;
                /*for(int j=-1222;j<=124124;j++) {
                    mina = Math.min(mina,getans(arr,j,n));
                }
                System.out.println(mina+" mina");*/
                //bw.write(ans+"\n");
            }
            bw.flush();
        }
    }
     

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

typedef long long ll;

const int MOD = (int)(1e+9) + 7;
int S, N;
vector<ll> fac, finv, inv;

void input_init(void) {
	cin >> S;
	fac.resize(S);
	finv.resize(S);
	inv.resize(S);
	N = S / 3;
}

void comb_init() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < S; i++){
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

ll comb(int n, int k){
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll solve(void) {
	ll sum = (ll)0;
	for (int k = 1; k <= N; ++k) {
		int b = k - 1;
		int a = S - 3 * k + b;
		sum += (comb(a, b) % MOD);
		sum %= MOD;
	}
	return sum;
}

int main(void) {
	input_init();
	comb_init();
	ll ans = solve();
	cout << ans << '\n';
	return 0;
}


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

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

int main() {

    int x[6];

    rep(i, 5) cin >> x[i];

    rep(i, 5) {
        if (x[i] == 0 ) {
            cout << i + 1 << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 3584
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
const double pi=acos(-1.0);
const double eps=1e-11;
template<class T> inline void ckmin(T &a,T b){if(b<a) a=b;}
template<class T> inline void ckmax(T &a,T b){if(b>a) a=b;}
template<class T> inline T sqr(T x){return x*x;}
typedef pair<int,int> ipair;
#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,a) for(int i=0;i<(a);++i)
#define ALL(A) A.begin(),A.end()

const int maxnode=2000000+16;

int n,h;
vector<int> g[maxnode];
bool v[maxnode];
int d[maxnode];
int q[maxnode];
bool ww;

void bfs(int p)
{
	if (v[p]) return;
	v[p]=true;
	int sizeq=0;
	q[sizeq++]=p;
	REP(cl,sizeq)
	{
		int x=q[cl];
		for (int y:g[x]) if (!v[y]) v[y]=true,q[sizeq++]=y;
	}
}
void dfs2(int p)
{
	if (v[p]) return;
	v[p]=true;
	if (p>=h && d[p]>0) ww=true;
	for (int x:g[p]) dfs2(x);
}
int main()
{
#ifdef _MSC_VER
	freopen("input.txt","r",stdin);
#endif
	std::ios::sync_with_stdio(false);
	while (cin>>n>>h && n>0)
	{
		h+=2;
		vector<ipair> edges;
		REP(i,n)
		{
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			int s=((c==0)?(a+h):c);
			int t=((d==0)?b:(d+h));
			edges.emplace_back(s,t);
		}
		bool ok=true;
		REP(i,h+h) g[i].clear();
		REP(i,h+h) v[i]=false;
		for (auto e:edges) g[e.first].push_back(e.second);
		FOR(i,h,h+h) bfs(i);
		for (auto e:edges) if (!v[e.first]) ok=false;

		REP(i,h+h) g[i].clear();
		REP(i,h+h) v[i]=false;
		for (auto e:edges) g[e.second].push_back(e.first);
		REP(i,h) bfs(i);
		for (auto e:edges) if (!v[e.second]) ok=false;

		REP(i,h+h) d[i]=0;
		for (auto e:edges) ++d[e.first],--d[e.second];
		REP(i,h) if (d[i]>0) ok=false;
		FOR(i,h,h+h) if (d[i]<0) ok=false;

		REP(i,h+h) g[i].clear();
		REP(i,h+h) v[i]=false;
		for (auto e:edges) g[e.second].push_back(e.first);
		for (auto e:edges) g[e.first].push_back(e.second);
		REP(i,h+h) if (!v[i] && SIZE(g[i])>0)
		{
			ww=false;
			dfs2(i);
			if (!ww) ok=false;
		}

		if (ok)
			printf("YES\n");
		else
			printf("NO\n");
		fflush(stdout);
#ifndef _MSC_VER
		break;
#endif
	}
	return 0;
}



Measured memory_bytes: 54772
#include"bits/stdc++.h"
#include<iostream>
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int main(void) {
	int n,r;

	cin >> n>>r;

	if (n>=10) {
		cout << r << endl;
	}
	else {
		cout << r+100*(10-n) << endl;
	}

	return 0;
}

Measured memory_bytes: 3596
# coding: utf-8
# Your code here!
x1, y1, x2, y2 = map(int,input().split())

b = x2 - x1
a = y1 - y2

x3 = x2 + a
y3 = y2 + b

x4 = x1 + a
y4 = y1 + b

print(x3, y3, x4, y4)

Measured memory_bytes: 38256
#include <bits/stdc++.h>
#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

constexpr auto M = 1000000007L;

int64_t powm(int64_t a, int64_t b) {
  if (b == 0) {
    return 1;
  }
  
  auto p = powm(a, b / 2);
  return p * p % M * ((b % 2) ? a : 1) % M;
}

int64_t invm(int64_t a) {
  return powm(a, M - 2);
}

int main() {
  int64_t r1, c1, r2, c2;
  
  cin >> r1 >> c1 >> r2 >> c2;
  
  vector<int64_t> fact(2000002L, 1);
  vector<int64_t> ifact(fact.size(), 1);
  for (auto i : irange(1L, static_cast<int64_t>(fact.size()))) {
    fact[i] = fact[i - 1] * i % M;
    ifact[i] = ifact[i - 1] * invm(i) % M;
  }

  int64_t ans = 0;
  for (auto r : irange(r1, r2 + 1)) {
    // C(r + c + 1, r + 1)
    ans += fact[r + c2 + 1] * ifact[r + 1] % M * ifact[c2] % M;
    ans %= M;
    ans += fact[r + (c1 - 1) + 1] * ifact[r + 1] % M * ifact[c1 - 1] % M * (M - 1) % M;
    ans %= M;
  }
       
  cout << ans << endl;
}


Measured memory_bytes: 31488
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Main m = new Main();
		m.run();
	}

	Scanner sc = new Scanner(System.in);

	void run() {
		int n = sc.nextInt();
		long c = sc.nextLong();

		long x[] = new long[n];
		long v[] = new long[n];
		for (int i = 0; i < n; i++) {
			x[i] = sc.nextLong();
			v[i] = sc.nextLong();
		}

		long cw[] = new long[n + 1];
		long cwv[] = new long[n + 1];
		for (int i = 0; i < n; i++) {
			cwv[i + 1] = cwv[i] + v[i];
			cw[i + 1] = cwv[i + 1] - x[i];
		}

		long cc[] = new long[n + 1];
		long ccv[] = new long[n + 1];
		for (int i = 0; i < n; i++) {
			int j = n - i - 1;
			ccv[i + 1] = ccv[i] + v[j];
			cc[i + 1] = ccv[i + 1] - c + x[j];
		}
//		System.out.println(Arrays.toString(cw));
//		System.out.println(Arrays.toString(cc));
		long cwMax[] = new long[n+1];
		long ccMax[] = new long[n+1];
		
		ccMax[0] = cc[0];
		cwMax[0] = cw[0];
		for(int i = 1; i < n+1;i++){
			ccMax[i] = Math.max(ccMax[i-1] , cc[i]); 
			cwMax[i] = Math.max(cwMax[i-1] , cw[i]); 
		}
//		System.out.println(Arrays.toString(cwMax));
//		System.out.println(Arrays.toString(ccMax));
		long res = 0;
		
		for(int i = 0; i < n+1;i++){
			int j = n - i;
			
			if (i == 0 || j == 0) {
//				System.out.println(i+" "+j+" : " + (cwMax[i] + ccMax[j]));
				res = Math.max(res, cwMax[i] + ccMax[j] );
			} else {
//				System.out.println(i+" "+j+" : " +x[i-1]+","+ (c - x[n - j ]) + " :" + (cwMax[i] + ccMax[j]-Math.min(x[i-1], c - x[n - j ])));
				res = Math.max(res, cwMax[i] + ccMax[j] - Math.min(x[i-1], c - x[n - j ]));
			}
		
		}
		
		System.out.println(res);
	}
}


Measured memory_bytes: 92632
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    h,w=map(int,readline().split())
    n=int(readline())
    a=list(map(int,readline().split()))

    grid=[[0]*w for _ in range(h)]

    x=0
    y=0
    color=1
    cnt=0
    total=0
    flag=True
    while total<h*w:
        grid[y][x]=color
        cnt+=1
        total+=1
        if flag:
            if x+1<w:
                x+=1
            else:
                y+=1
                flag=False
        else:
            if x-1>=0:
                x-=1
            else:
                y+=1
                flag=True
        if cnt==a[color-1]:
            color+=1
            cnt=0

    for i in range(h):
        print(*grid[i])
        
if __name__=="__main__":
    main()

Measured memory_bytes: 3916
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <array>
#include <set>
#include <climits>
#include <cassert>
#include <bitset>
#include <numeric>
using namespace std;
#define mod 1000000007
long long dp[102][5][2];
int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

#ifdef HELL_JUDGE
    freopen("input","r",stdin);
    freopen("output","w",stdout);
    freopen("error","w",stderr);
#endif 
    string S;cin>>S;
    int K; cin>>K;
    dp[0][0][0]=1;
    for(int i=0;i<(int)S.size();++i)/*size of number we need*/{
        for(int j=0;j<=K;++j)/* non zero element selected*/{
            for(int k=0;k<2;++k)/* are we sure that number is less then N */{
                for(int l=0;l<10;++l){
                    int c =S[i]-'0';
                    if(k==0 && l>c)/* number is out of limit*/{
                        continue;
                    }
                    dp[i+1][j+(l>0)][k|(l<c)]+=dp[i][j][k];
                }
            }
        }
    }
    cout<<dp[(int)S.size()][K][0]+dp[(int)S.size()][K][1]<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

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

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

  if(a < k) {
    sum = a + b - k;
    if(sum < 0) {
      a = 0;
      b = 0;
    } else if(sum > b) {
      a = sum - b;
    } else {
      a = 0;
      b = sum;
    }
  } else {
    a -= k;
  }

  printf("%lu %lu\n", a, b);

  return 0;
}


Measured memory_bytes: 128
n = int(input())
a = list(map(int,input().split()))
d = {}
ans = 0
for i in a:
    if i not in d:
        d[i] = 1
    else:
        d[i] += 1

for i in d.items():
    ans += (i[1]*(i[1]-1)//2)

for i in a:
    print(ans - ((d[i]-1)*(d[i]-d[i]+2))//2)


Measured memory_bytes: 26140
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.*;
import java.util.InputMismatchException;

public class Main {
	public static void main(String[] args) {
		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = in.nextInt();
		int[] m = new int[n];
		int cnt = 0;
		for(int i=0; i<n; i++) {
			m[i] = in.nextInt();
			if(m[i] %2== 1) {
				cnt++;
			}
		}
		if(cnt %2 ==0) {
			out.println("YES");
		}else{
			out.println("NO");
		}
		out.flush();

	}

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

	static class InputReader {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		private int next() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public char nextChar() {
			int c = next();
			while (isSpaceChar(c))
				c = next();
			if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
				return (char) c;
			}
			throw new InputMismatchException();
		}

		public String nextToken() {
			int c = next();
			while (isSpaceChar(c))
				c = next();
			StringBuilder res = new StringBuilder();
			do {
				res.append((char) c);
				c = next();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public int nextInt() {
			int c = next();
			while (isSpaceChar(c))
				c = next();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = next();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c-'0';
				c = next();
			} while (!isSpaceChar(c));
			return res*sgn;
		}

		public long nextLong() {
			int c = next();
			while (isSpaceChar(c))
				c = next();
			long sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = next();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c-'0';
				c = next();
			} while (!isSpaceChar(c));
			return res*sgn;
		}

		public double nextDouble() {
			return Double.valueOf(nextToken());
		}

		public boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		//public interface SpaceCharFilter {
		//	public boolean isSpaceChar(int ch);
		//}
	}
	// template
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r);  }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r);  }

	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t;  }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t;  }
}


Measured memory_bytes: 9300
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <queue>

using namespace std;

void ABC118C()
{
	int N;
	cin >> N;
	vector<long long int> vals(N);
	for (auto& x : vals)
		cin >> x;

	long long int GCD = vals[0];
	for (int i = 1; i < N; ++i)
	{
		auto x0 = max(GCD, vals[i]);
		auto x1 = min(GCD, vals[i]);
		while (x0 % x1 != 0)
		{
			long long int y = x0 - (x0 / x1) * x1;
			x0 = max(y, x1);
			x1 = min(y, x1);
		}
		GCD = x1;
	}
	cout << GCD << endl;
}
int main()
{
	ABC118C();
	return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define INF 1e9
#define endl '\n'
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define FORE(i, a, b) for (int i = (a), e = (b); i < e; ++i)
#define RFORE(i, a, b) for (int i = (b)-1, e = (a); i >= e; --i)
#define ALL(x) (x).begin(), (x).end()
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>())
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, K;
    cin >> N >> K;
    map<int, int> ball;
    for (int i = 0; i < N; i++) {
        int A;
        cin >> A;
        ++ball[A];
    }
    if (ball.size() <= K) {
        cout << 0 << endl;
        exit(0);
    }
    using pair_type = pair<int, int>;
    vector<pair_type> v(ALL(ball));
    sort(ALL(v), [](const pair_type& p1, const pair_type& p2) {
        return p1.second < p2.second;
    });
    int ans = 0;
    int len = v.size();
    FOR(i, 0, len) {
        if (len - i <= K) {
            cout << ans << endl;
            exit(0);
        }
        ans += v.at(i).second;
    }
    cout << ans << endl;
}


Measured memory_bytes: 14524
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

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

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

    cout << MAX(A + B, MAX(A - B, A * B)) << "\n";
    return 0;
}



Measured memory_bytes: 256
// https://atcoder.jp/contests/abc116/tasks/abc116_c

#[allow(unused)]
use std::cmp::{max, min, Ordering};
#[allow(unused)]
use std::collections::{HashSet, VecDeque};
use std::io;
use std::io::{BufRead, Read, Write};
use std::str::FromStr;

#[allow(unused, non_snake_case)]
pub fn solve<R: BufRead, W: Write>(mut io: Io<R, W>) {
    let N: usize = io.next();
    let mut hs: Vec<usize> = io.nexts(N);

    let mut res = 0;

    loop {
        let mut in_has_height_range = false;
        let mut mutated_ct = 0;

        for (i, h) in hs.iter_mut().enumerate() {
            if *h > 0 {
                *h -= 1;
                in_has_height_range = true;
            } else if *h == 0 && in_has_height_range {
                mutated_ct += 1;
                in_has_height_range = false;
            }
        }

        if in_has_height_range {
            mutated_ct += 1;
        }

        if mutated_ct == 0 {
            break;
        } else {
            res += mutated_ct;
        }
    }

    io.println(res);
}

#[allow(dead_code)]
fn main() {
    let stdin = io::stdin();
    let stdin_lock = stdin.lock();
    let stdout = io::stdout();
    let stdout_lock = stdout.lock();

    let io = Io {
        reader: stdin_lock,
        writer: stdout_lock,
    };

    solve(io);
}

/* IO helpers */

pub struct Io<R, W>
where
    R: BufRead,
    W: Write,
{
    pub reader: R,
    pub writer: W,
}

#[allow(dead_code)]
impl<R, W> Io<R, W>
where
    R: BufRead,
    W: Write,
{
    fn println<T: std::fmt::Debug>(&mut self, value: T) {
        writeln!(&mut self.writer, "{:?}", value).expect("Unable to write");
    }

    fn print<T: std::fmt::Debug>(&mut self, value: T) {
        write!(&mut self.writer, "{:?}", value).expect("Unable to write");
    }

    fn next<T: FromStr>(&mut self) -> T {
        let token = self
            .reader
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok().expect("Unable to parse")
    }

    fn nexts<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.next::<T>()).collect::<Vec<T>>()
    }
}

/* Structure helpers */

trait IteratorExt: Iterator + Sized {
    fn vec(self) -> Vec<Self::Item> {
        self.collect()
    }
}

impl<T: Iterator> IteratorExt for T {}


Measured memory_bytes: 4352
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int h = Integer.parseInt(sc.next());
        int n = Integer.parseInt(sc.next());
        
        int a = 0;
        for(int i = 0; i < n; i++){
            a += Integer.parseInt(sc.next());
        }
        
        if(a >= h){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}

Measured memory_bytes: 45608
MOD = 1000000007
n = int(input())
s = input()
t = input()

ans = 3
i = 0
if s[0]==t[0]:
    i += 1
    vert = True
else:
    ans *= 2
    i += 2
    vert = False

while i<n:
    if s[i]==t[i]:
        if vert:
            ans *= 2
            ans %= MOD
        vert = True
        i += 1
    else:
        if vert:
            ans *= 2
        else:
            ans *= 3
        ans %= MOD
        vert = False
        i += 2
print(ans)

Measured memory_bytes: 3064
import java.util.Scanner;
 
class Main{
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for(int i = 0; i < n; i++){
			int K = sc.nextInt();
			int P = sc.nextInt();
			if(K%P == 0)
				System.out.println(P);
			else
				System.out.println(K%P);
		}
	}
}

Measured memory_bytes: 20352
import java.util.*;


public class Main {
	public static void main (String[] args){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] B = new int[N-1];
		for(int i = 0; i < N-1; i++) {
			B[i] = sc.nextInt();
		}
		int[] A = new int[N];
		
			A[0] = B[0];
		
		if(N > 2) {
			for(int i = 1; i < N-1; i++) {
				if(B[i-1] != B[i]) {
					A[i] = Math.min(B[i-1], B[i]);
				}
				else {
					A[i] = B[i];
				}
			A[N-1] = B[N-2];
			}
		}
		else {
			A[1] = A[0];
		}
		int R = 0;
		for(int i = 0; i < N; i++) {
			R += A[i];
			//System.out.println(A[i]);
		}
		System.out.println(R);
	}
}

Measured memory_bytes: 21844
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstdio>
#include <set>
#include <stack>
#include <queue>
#include <cassert>
//#include <bits/stdc++.h>
#define rep(i,n) Rep(i,0,n)
#define Rep(i,k,n) for(int i=k ; i<n ; i++)
#define rep1(i,n) for(int i=1 ; i<=n ; i++)
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())
#define ALL(a)  (a).begin(),(a).end()

typedef long long ll;

//const int MOD = 1000000007;
//const int INF = 1<<30;
using namespace std;

string yn(bool x){
    return x ? "YES" : "NO";
}


char x[50][50];
int c[50][50];

void cnt(int p,int q){
    int dx[8] = {1,0,-1,0,1,-1,-1,1};
    int dy[8] = {1,1,1,-1,-1,0,-1,0};
    
    rep(i,8){
        if(p+dy[i] < 0 or q+dx[i] < 0 or p+dy[i]>=50 or q+dx[i]>=50 or x[p][q] == '#') continue;
        if(x[p+dy[i]][q+dx[i]] == '#') c[p][q]++;
    }
    return;
}



int main(){
    int h,w; cin >> h >> w;
    rep(i,h) rep(j,w) cin >> x[i][j];
    
    rep(i,h) rep(j,w) cnt(i,j);
    
    rep(i,h){
        rep(j,w){
            if(x[i][j] == '#') cout << "#";
            else cout << c[i][j];
        }
        cout << endl;
    }
}






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

using namespace std;

int main() {
    int N, M;
    int fix[3];
    int r[3] = {0};
    fix[0] = -1; fix[1] = -1; fix[2] = -1;
    cin >> N >> M;
    
    for (int i = 0; i < M; i++) {
        int s, c;
        cin >> s >> c;
        s -= 1;
        
        if ((N != 1 && s == 0 && c == 0) || (fix[s] != -1 && fix[s] != c)) {
            cout << -1 << endl;
            return 0;
        }
        
        fix[s] = c;
    }
    
    if (N == 1) {
        if (fix[0] == -1)
            r[2] = 0;
        else
            r[2] = fix[0];
        
        cout << r[2] << endl;
    } else if (N == 2) {
        if (fix[0] == -1) {
            r[1] = 1;
        } else {
            r[1] = fix[0];
        }
        if (fix[1] == -1)
            r[2] = 0;
        else
            r[2] = fix[1];
        
        cout << r[1] * 10 + r[2] << endl;
    } else if (N == 3) {
        if (fix[0] == -1) {
            r[0] = 1;
        } else {
            r[0] = fix[0];
        }
        if (fix[1] == -1) {
            r[1] = 0;
        } else {
            r[1] = fix[1];
        }
        if (fix[2] == -1)
            r[2] = 0;
        else
            r[2] = fix[2];
        
        cout << r[0] * 100 + r[1] * 10 + r[2] << endl;
    }
    
    
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI  = acos(-1.0);
const double EPS = 1e-9;
const int INF = 1e9+10;
const ll mod = 998244353;
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; }
#define print(x) cerr << #x << " is " << x << endl;
//#define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; 


int main()
{
    int N;
    cin >> N;
    vector<ll> A(N);
    multiset<P> s;
    rep(i, N){
        cin >> A[i];
    }
    sort(ALL(A), greater<ll>());
    ll ans = 0;
    
    s.insert(make_pair(-A[0], -A[0]));
    for (int i = 1; i < N; i++){
        ll x = -s.begin()->first;
        ll y = -s.begin()->second;
        //fprintf(stderr, "(%d, %d) %d\n", x, y, s.size());
        s.erase(s.begin());
        //fprintf(stderr, "%d\n", s.size());
        s.insert(make_pair(-A[i], x));    
        s.insert(make_pair(-A[i], y));
        //fprintf(stderr, " add (%d, %d) and (%d, %d) -> %d\n", A[i], x, A[i], y, s.size());
        ans += x;
    }
    cout << ans;
    return 0;
 
}


Measured memory_bytes: 17220
#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)1e9;
const ll  inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;
const double PI = acos(-1);

int N;
string a0;

string ask(int i) {
    out(i);
    fflush(stdout);
    
    string res; 
    cin >> res;
    return res;
}

void f(int l, int r) {
    int md = (l + r) / 2;

    string res = ask(md);
    if (res == "Vacant") return;

    if ((md % 2 == 0 && a0 == res) 
        || (md % 2 == 1 && a0 != res)) {
        f(md + 1, r);
    }
    else {
        f(l, md);
    }
}

int main() {
    cin >> N;

    a0 = ask(0);
    if (a0 == "Vacant") return 0;

    f(1, N);

    return 0;
}

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

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

int main() {
    ll X, Y, Z, K;
    cin >> X >> Y >> Z >> K;
    vector<ll> x(X), y(Y), z(Z);
    rep(i, X) cin >> x[i];
    rep(i, Y) cin >> y[i];
    rep(i, Z) cin >> z[i];


    vector<ll> xy(max(X*Y, K), 0);
    int cur = 0;
    rep(i, X) rep(j, Y) {
        xy[cur++] = x[i] + y[j];
    }

    sort(xy.rbegin(), xy.rend());

    vector<ll> xyz(K * Z);
    cur = 0;
    rep(i, K) rep(j, Z) {
        xyz[cur++] = xy[i] + z[j];
    }

    sort(xyz.rbegin(), xyz.rend());
    rep(i, K) cout << xyz[i] << endl;
}


Measured memory_bytes: 31616