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

using namespace std;

typedef long long ll;
typedef pair< ll, ll > pll;
typedef pair< int, int > pii;

const double pi = 3.14159265358979323846;

// #define fio() cin.tie(0); ios::sync_with_stdio(false);
// template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
// template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define MOD 1000000007LL
#define PS(p) cout << setprecision(15) << fixed << p << endl;
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
#define ALL(c) c.begin(), c.end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define TG0 get< 0 >
#define TG1 get< 1 >
#define TG2 get< 2 >
#define F first
#define S second
#define VL vector< ll >

// to_string(123)
// atoi("123"); atoi(s.c_str());
// "abc".size()
// s.find('+') == string::npos
// 1e18

ll N;
bool dfs(ll now) {
  if (now == N) {
    // cout << now << endl;
    return true;
  }

  if (N < now) {
    return false;
  }

  if (dfs(now + 4)) {
    return true;
  }
  if (dfs(now + 7)) {
    return true;
  }

  return false;
}

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

  cin >> N;

  // if (N % 4 == 0) {
  //   cout << "Yes" << endl;
  // } else if (N % 7 == 0) {
  //   cout << "Yes" << endl;
  // } else if (N % 11 == 0) {
  //   cout << "Yes" << endl;
  // } else {
  //   cout << "No" << endl;
  // }

  if (dfs(0)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}


Measured memory_bytes: 256
// {{{ by shik
#include <bits/stdc++.h>
#include <unistd.h>
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define FOR(it,c) for ( auto it=(c).begin(); it!=(c).end(); it++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

#ifdef SHIK
template<typename T>
void _dump( const char* s, T&& head ) { cerr<<s<<"="<<head<<endl; }

template<typename T, typename... Args>
void _dump( const char* s, T&& head, Args&&... tail ) {
    int c=0;
    while ( *s!=',' || c!=0 ) {
        if ( *s=='(' || *s=='[' || *s=='{' ) c++;
        if ( *s==')' || *s==']' || *s=='}' ) c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1,tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, __VA_ARGS__); \
} while (0)

template<typename Iter>
ostream& _out( ostream &s, Iter b, Iter e ) {
    s<<"[";
    for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
    s<<"]";
    return s;
}

template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.first<<","<<p.second<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
template<typename T, size_t N>
ostream& operator <<( ostream &s, const array<T,N> &c ) { return _out(s,ALL(c)); }
template<typename T>
ostream& operator <<( ostream &s, const set<T> &c ) { return _out(s,ALL(c)); }
template<typename A, typename B>
ostream& operator <<( ostream &s, const map<A,B> &c ) { return _out(s,ALL(c)); }
#else
#define dump(...)
#endif

template<typename T>
void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( long long &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }

void R() {}
template<typename T, typename... U>
void R( T& head, U&... tail ) {
    _R(head);
    R(tail...);
}

template<typename T>
void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
template<typename T>
void _W( const vector<T> &x ) {
    for ( auto i=x.cbegin(); i!=x.cend(); i++ ) {
        if ( i!=x.cbegin() ) putchar(' ');
        _W(*i);
    }
}

void W() {}
template<typename T, typename... U>
void W( const T& head, const U&... tail ) {
    _W(head);
    putchar(sizeof...(tail)?' ':'\n');
    W(tail...);
}

#ifdef SHIK
#define FILEIO(...)
#else
#define FILEIO(name) do {\
    freopen(name ".in","r",stdin); \
    freopen(name ".out","w",stdout); \
} while (0)
#endif

// }}}

const int N=1e5+10;
int n;
LL a[N];
int main() {
    R(n);
    REP(i,n) R(a[i]);
    LL ans=0,t=1;
    REP(i,n) {
        LL x=(a[i]-1)/t;
        if ( x!=0 ) a[i]=1;
        ans+=x;
        if ( a[i]==t ) t++;
    }
    W(ans);
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define lf long double
typedef vector <ll> vi;
typedef pair <ll,ll> ii;
typedef vector <pair <int,int> >vii;
typedef vector <vector <ll> > vv;
typedef vector <string> vs;
ll mod=1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll r;
    cin>>r;
    ll ans=3*r*r;
    cout<<ans<<endl;
}


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

int main(){
    long long a, b, k;
    cin>>a>>b>>k;
    if(a>=k) a-=k;
    else b-=k-a, a=0;
    if(b<0) b=0;
    cout<<a<<' '<<b;
	return 0;
}


Measured memory_bytes: 256
//in the name of god
//if you read this code please search about imam hussain
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int,int>
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define read freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout)

const int maxn=1e5+5;
const int mod=1e9+7;
const int inf=1e9;
const int del=728729;

ll poww(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));}
ll n;
ll m;
ll r[maxn];
ll d[maxn];
ll cnt;
int main(){
	migmig;
	cin >> n >> m ;
	r[1]=1;
	for(int i = 1 ; i <= n;i++){
		d[i]++;
	}
	for(int i = 0 ; i < m ; i ++){
		ll x ;
		ll y ;
		cin >> x >> y;
		d[x]--;
		d[y]++;
		if(r[x]==1){
			r[y]=1;
		}
		if(d[x]==0){
			r[x]=0;
		}
	}
	for(int i = 1 ; i <=n;i++){
		if(r[i]==1){
			cnt++;
		}
	}
	cout<<cnt;
	return 0;
}



Measured memory_bytes: 1792
n = input()
count = 1
max_c = - float('inf')
for i, char in enumerate(n):
    if i == 0:
        continue
    if n[i-1] == char:
        count += 1 
    else:
        count = 1
    max_c = max(max_c, count)
 
ans = 'Yes' if max_c >= 3 else 'No'
print(ans)


Measured memory_bytes: 2940
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << endl;
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

ll dist (ll a, ll b, ll c, ll d) {
    return abs(a - c) + abs(b - d);
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    
    int N, M;
    cin >> N >> M;

    vector<ll> a(N), b(N);
    vector<ll> c(M), d(M);

    REP(i, N) {
        cin >> a[i] >> b[i];
    }

    REP(i, M) {
        cin >> c[i] >> d[i];
    }

    REP(i, N) {
        ll v = INF;
        int m;
        REP(j, M) {
            ll cur = dist(a[i], b[i], c[j], d[j]);
            if (cur < v) v = cur, m = j;
        }

        print(m + 1);
    }

}


Measured memory_bytes: 3660
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {Main instance = new Main();sc = instance.new Scanner();instance.solve();}
    private class Scanner {String[] s = new String[0];String regex = " ";int i = 0;BufferedReader br = new BufferedReader(new InputStreamReader(System.in));public String next() {try {if (i < s.length) { return s[i++];}String st = br.readLine();while ("".equals(st)) { st = br.readLine(); }s = st.split(regex, 0);i = 0;return s[i++];} catch (IOException e) { return null; }}public int nextInt(){return Integer.parseInt(next());}}        
    private static Scanner sc;

    private void solve() {
        int N = sc.nextInt();
        int[] A1 = new int[N];
        int[] A2 = new int[N];
        for (int i = 0; i < N; i++) {
            A1[i] = sc.nextInt();
        }
        for (int i = 0; i < N; i++) {
            A2[i] = sc.nextInt();
        }
        int ans = 0;
        for (int i = 0; i < N; i++) {
            int tmp = 0;
            for (int j = 0; j <= i; j++) {
                tmp += A1[j];
            }
            for (int j = i; j < N; j++) {
                tmp += A2[j];
            }
            if (tmp > ans) {
                ans = tmp;
            }
        }
        System.out.println(ans);
    }
}

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

const long long N = 2e5 + 5;

long long n, m, T, a[N], b[N];

int main()
{
    cin >> n >> m >> T;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    for (long long i = 1; i <= m; i++) cin >> b[i];

    long long res = 0, R = 0, Sum = 0;
    while (R < m && Sum + b[R + 1] <= T)
        Sum += b[++R];
    res = R;
    for (long long i = 1; i <= n; i++) {
        Sum += a[i];
        while (R > 0 && Sum > T) {
            Sum -= b[R];
            R--;
        }
        if (Sum <= T) res = max(res, R + i);
    }

    cout << res;
}


Measured memory_bytes: 6700
#include <cstdio>
#include <cmath>
int n;
double x[1005], y[1005], X, Y, d, e;
double dist(double a, double b) {
	return a*a + b*b;
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%lf%lf", &x[i], &y[i]);
		X += x[i]; Y += y[i];
	}
	X /= n; Y /= n;
	double P = 0.1;
	for (int i = 0; i < 30000; i++) {
		int f = 0;
		d = dist(X - x[0], Y - y[0]);
		for (int j = 1; j < n; j++) {
			e = dist(X - x[j], Y - y[j]);
			if (d < e) { d = e; f = j; }
		}
		X += (x[f] - X)*P;
		Y += (y[f] - Y)*P;
		P *= 0.999;
	}
	printf("%.10lf", sqrt(d));
}


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

int main() {
	int n;
	std::cin >> n;

	std::string str = std::to_string(n);

	for(char &c : str) {
		if(c == '1') {
			c = '9';
		} else if(c == '9') {
			c = '1';
		}
	}

	// 必要？
	int answer = std::stoi(str);

	std::cout << answer << std::endl;

	return 0;
}

Measured memory_bytes: 256
//競技プログラミング用のテンプレート
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <stack>

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define repr(i, n) for(int i = n - 1; i >= 0; i--)

using namespace std;
using ll = long long int;
using p = pair<int, int>;
using pl = pair<ll, ll>;
using v = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vl = vector<ll>;

int main()
{
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> t(m, {0, 0});
  if(n % 2 != 0) {
    int l = 1;
    int r = n - 1;
    for(int i = 0; i < m; i++)
    {
      t[i] = {l, r};
      l++;
      r--;
    }
  }else{
    bool flag = false;
    int l = 1;
    int r = n;
    for(int i = 0; i < m; i++)
    {
      if(!flag && r - l <= n / 2) {
        r--;
        flag = true;
      }
      t[i] = {l, r};
      l++;
      r--;
    }
  }
  for(auto e : t) {
    cout << e.first << " " << e.second << endl;
  }
}


Measured memory_bytes: 3972
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <math.h>

using namespace std;
typedef long long ll;

int main()
{
    string s[7] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
    
    string day;
    cin >> day;

    for (int i = 0; i < 7; i++)
    {
        if(day.compare(s[i]) == 0){
            cout << 7 - i << endl;
        }
    }

    
}


Measured memory_bytes: 256
A,B,C,D = map(int, input().split())

result = 0

if B <= C or D <= A:
    print(result)
    exit()
if A <= C :
    if B >= D:
        result = D-C
    if B < D:
        result = B-C
if A > C:
    if B >=D:
        result = D-A
    if B <D:
        result = B-A

print(result)


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

int main() {
    double ans,p;
    int N,K,a;
    cin >> N >> K;
    ans = 0;
    rep(i,N)
    {
        p = (double)1/N;
        a = 1;
        rep(j,K)
        {
            if((i+1)*a >= K)
            {
                ans += (double)p;
                break;
            }
            else
            {
                a = a*2;
                p = (double)p/2;
            }
            

        }
    }
 

    cout << fixed << setprecision(12)<< (double)ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int i = 1; i <= N; i += 2) {
    int cnt = 0;
    for (int j = 1; j <= i; j++) {
      if (i % j == 0) cnt++;
    }
    if (cnt == 8) ans++;
  }
  cout << ans << endl;
  return 0;
}

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

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

const int mod = 1000*1000*1000+7;

const int N = 100005;

char a[N], b[N];
int sa[N], sb[N];

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

	cin >> (a + 1) >> (b + 1);

	for (int i = 1; a[i]; ++i) {
		sa[i] = sa[i - 1] + (a[i] == 'A' ? +1 : -1);
	}

	for (int i = 1; b[i]; ++i) {
		sb[i] = sb[i - 1] + (b[i] == 'A' ? +1 : -1);
	}

	int q;
	cin >> q;

	REP(i, q) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;

		int r1 = (sa[b] - sa[a - 1]) % 3;
		int r2 = (sb[d] - sb[c - 1]) % 3;

		if (r1 == 2) {
			r1 = -1;
		}
		if (r1 == -2) {
			r1 = +1;
		}

		if (r2 == 2) {
			r2 = -1;
		}

		if (r2 == -2) {
			r2 = +1;
		}

		cout << (r1 == r2 ? "YES" : "NO") << '\n';
	}

	return 0;
}

Measured memory_bytes: 1536
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n,l = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	f = 0
	for i in 1:n-1
		if a[i]+a[i+1] >= l
			f = i
			break
		end
	end
	if f == 0
		println("Impossible")
	else
		println("Possible")
		s = 0
		for i in 1:f-1
			println(i)
		end
		for i in n-1:-1:f+1
			println(i)
		end
		println(f)
	end
end

main()



Measured memory_bytes: 131084
import java.util.*;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			System.out.println(k);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	}
}


Measured memory_bytes: 35704
#include<bits/stdc++.h>
using namespace std;
const long long INF = 1e7+1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector < pair<ll, ll > > vp;
typedef vector <string> vs;
typedef vector <char> vc;
typedef list <ll> lst;

ll n,m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/


string table[10] = {
	"",
	".,!? ",
	"abc",
	"def",
	"ghi",
	"jkl",
	"mno",
	"pqrs",
	"tuv",
	"wxyz"
};

int mod[10] = { 1,5,3,3,3,3,3,4,3,4 };

int main()
{
	cin >> n;
	REP(i, n)
	{
		cin >> s;

		cnt = 0;
		string ans;

		REP(i, s.size())
		{
			if (s[i] != '0') {
				cnt++;
			} else {
				if (cnt != 0) {
					ans += table[s[i - 1] - '0'][(cnt - 1) % mod[s[i - 1] - '0']];
					cnt = 0;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 3152
function Main(input) {
	var data = input.split("\n")
	var n = data[0];
    var dish = data[1].split(" ").map((n) => parseInt(n, 10));
    var point = data[2].split(" ").map((n) => parseInt(n, 10));
    var bonus = data[3].split(" ").map((n) => parseInt(n, 10));
    
    var sum = 0;
    
    for(var i=0; i < n ;i++){
    	sum += point[i];
    	if(dish[i]+1 == dish[i+1]){
    		sum += bonus[dish[i]-1];
    	}
    }
    console.log(sum)
}
 
// Don't edit this line!
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 9420
s_orig = gets.chomp
t_orig = gets.chomp

s = s_orig.codepoints
t = t_orig.codepoints

s_size = s.size
t_size = t.size

dp = Array.new(s_size+1) { Array.new(t_size+1,0) }

0.upto(s_size-1).each do |i|
  dpi = dp[i]
  dpi1 = dp[i+1]
  0.upto(t_size-1).each do |j|
    if s[i] == t[j]
      dpi1[j+1] = dpi[j] + 1
    else
      dpi1[j+1] = dpi1[j] > dpi[j+1] ? dpi1[j] : dpi[j+1]
    end
  end
end

ans = ''
y = s_size
x = t_size

while y>0
  if dp[y-1][x] == dp[y][x]
    y-=1
  elsif dp[y][x-1] == dp[y][x]
    x-=1
  else
    y-=1
    x-=1
    ans << s_orig[y]
  end
end

# dp.each{|l| p l}

puts ans.reverse


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

using namespace std;

const int N = 100;

int aa[N];

int main() {
	int n, m; cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> aa[i];
	int a = 0;
	for (int i = 0; i < n; i++)
		a += aa[i];
	int k = 0;
	for (int i = 0; i < n; i++)
		if (aa[i] * m * 4 >= a)
			k++;
	cout << (k >= m ? "Yes" : "No") << '\n';
	return 0;
}


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

int main(void)
{
    long long X;
    long long Y = 100LL;
    int ans = 0;
    scanf("%lld", &X);
    while (X > Y)
    {
        ++ans;
        Y += (Y / 100);
    }
    printf("%d",ans);
    return 0;
}

Measured memory_bytes: 1736
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repn(i,n) for(int i = 0; i <= (int)(n); i++)
#define srep(i,l,n) for(int i = l; i < (int)(n); i++)
#define srepn(i,l,n) for(int i = l; i <= (int)(n); i++)
#define drep(i,n) for(int i = (int)(n-1); i >= 0; i--)
#define drepn(i,n) for(int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);}
template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;}
template<class T = int> using V = vector<T>;
template<class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    V<> a(2 * n);
    cin >> a;
    sort(all(a));
    int ans = 0;
    for(int i = 0; i < 2 * n; i += 2) ans += a[i];
    cout << ans << endl;
}

Measured memory_bytes: 3656
#include <bits/stdc++.h>
#define maxn 200003
using namespace std;
typedef long long LL;

int n;
LL ans;
int h[maxn];
int a[maxn];
LL dp[maxn];
LL tree[4*maxn];

LL update( int cx , int cy , int q , int id , LL val ) {
	if( q < cx || cy < q )
		return tree[id];
	if( cx == cy )
		return tree[id] = val;
	int mid = (cx + cy) >> 1;
	LL left = update( cx , mid , q , id*2 , val );
	LL right = update( mid+1 , cy , q , id*2+1 , val );
	return tree[id] = max( left , right );
}

LL query( int cx , int cy , int qx , int qy , int id ) {
	if( qy < cx || cy < qx )
		return 0;
	if( qx <= cx && cy <= qy )
		return tree[id];
	int mid = (cx + cy) >> 1;
	LL left = query( cx , mid , qx , qy , id*2 );
	LL right = query( mid+1 , cy , qx , qy , id*2+1 );
	return max( left , right );
}

int main() {
	cin >> n;
	for( int i = 1 ; i <= n ; i++ )
		cin >> h[i];
	for( int i = 1 ; i <= n ; i++ )
		cin >> a[i];
	for( int i = 1 ; i <= n ; i++ ) {
		dp[i] = query( 1 , n , 1 , h[i]-1 , 1 ) + a[i];
		update( 1 , n , h[i] , 1 , dp[i] );
		ans = max( ans , dp[i] );
	}
	cout << ans << endl;
}


Measured memory_bytes: 9600
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define PI 3.1415926535897932384626433832795
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
long long MOD = 1e9+7;
pair<int,int> dx[4] = {{1,0},{-1,0},{0,1},{0,-1}};
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct chash {
    int operator()(int x) const { return x ^ RANDOM; }
};
string toString(long long x)
{
stringstream ss;
ss << x;
string str = ss.str();
return str;
}
long long fastpow(long long x,long long k)
{
    if(!k)
        return 1;
    if(k & 1)
        return ((x * fastpow(x,k-1) % MOD) % MOD) % MOD;
    long long ans = fastpow(x,k/2);
    ans %= MOD;
    ans *= ans;
    ans %= MOD;
    return ans;
}
long long sumF(long long x)
{
    int s = 0;
    while(x)
        s += x%10,x /= 10;
    return s;
}
//a = 1,g = 2,c = 3,t = 4
string toFind = "123";
int n;
bool isBad(int l1,int l2,int l3,int l4,int l5)
{
    string s = toString(l1) + toString(l2) + toString(l3) + toString(l4) + toString(l5);
    for(int i = 0;i < s.length();i++)
    {
        for(int j = 0;j < s.length();j++)
        {
            if(abs(i-j) > 1)
                continue;
            swap(s[i],s[j]);
            if(s.find(toFind) != string::npos)
            {
               // cout << s << endl;
                return true;
            }
            swap(s[i],s[j]);
        }
    }
    return false;
}
long long dp[105][5][5][5][5];
long long solve(int idx,int l1,int l2,int l3,int l4)
{
    if(idx == n)
        return !isBad(l1,l2,l3,l4,0);
    if(dp[idx][l1][l2][l3][l4] != -1)
        return dp[idx][l1][l2][l3][l4];
    long long C = 0;
    for(int i = 1;i <= 4;i++)
    {
        if(isBad(l1,l2,l3,l4,i))
            continue;
        if(l1 == 0 && l2 == 0 && l3 == 0 && l4 == 0)
            C += solve(idx + 1,i,0,0,0) % MOD;
        else if(l2 == 0 && l3 == 0 && l4 == 0)
            C += solve(idx + 1,l1,i,0,0) % MOD;
        else if(l3 == 0 && l4 == 0)
            C += solve(idx + 1,l1,l2,i,0) % MOD;
        else if(l4 == 0)
            C += solve(idx + 1,l1,l2,l3,i) % MOD;
        else
            C += solve(idx + 1,l2,l3,l4,i) % MOD;
    }
    return dp[idx][l1][l2][l3][l4] = C % MOD;
}
int main()
{
    memset(dp,-1,sizeof dp);
    IO;
    cin >> n;
    //cout << isBad(2,1,3,0) << endl;
    cout << solve(0,0,0,0,0);
    return 0;
}


Measured memory_bytes: 768
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


D = I()
C = LMI()
S = [LMI() for _ in range(D)]
T = [I() - 1 for _ in range(D)]

last = defaultdict(int)
ans = 0
for d in range(D):
    ans += S[d][T[d]]
    last[T[d]] = d + 1
    for i in range(26):
        ans -= C[i] * (d + 1 - last[i])
    print(ans)


Measured memory_bytes: 68716
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
    string s;
    cin >> s;

    vector<int> count(26, 0);
    for (int i = 0; i < 4; i++) {
        count[s[i] - 'A']++;
    }

    int cnt2 = 0;
    for (int i = 0; i < 26; i++) {
        if (count[i] == 2) {
            cnt2++;
        }
    }

    cout << (cnt2 == 2 ? "Yes" : "No") << endl;
}


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

// Common file
#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>



#define pb push_back
#define F first
#define S second
#define rep(i,n) for(int i = 0; i < (n); i++)

using ll = long long;
using ld = long double;
using ul = unsigned long long;
using ii = pair<int, int>;
#define Mosta     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ReadFile            freopen("input.in","r",stdin)
#define WriteFile           freopen("output.txt","w",stdout)
const ll mod7 = 1000000007,mod = 1000000000,OO = 0x3f3f3f3f;
const ld EPS = 1e-7;
double Pi=acos(-1);
const ld rad=Pi/180.0;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
            y = y>>1;
            x = (x*x) % p;
    }
    return res;
}
ll nCr (ll n,ll m)
{
    return(m == 0)?1:n*nCr(n-1,m-1)/m;
}

/*****************************************************/

const int N = 105;
int a[N] , n , ans = 0;

int main()
{
    cin >> n;

    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        if(i%2 && a[i]%2)
            ++ans;
    }

    cout << ans << "\n";

    return 0;
}


Measured memory_bytes: 3600
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define f(i,a,n) for(int i=a ; i<n ; i++)
#define F first
#define S second
#define vll vector<ll>
#define fast ios:: sync_with_stdio(false),cin.tie(0);
#define COUT cout<<fixed<<setprecision(12)
#define SET set<ll,greater<ll>>gquiz
#define ITERATOR set<ll,greater<ll>> :: iterator itr
#define V vector<ll>vector
#define sz size()
//#define A vector<pair<ll,ll>>vect
ll N=100000,M=100000;
#define B vect.push_back(make_pair(N,M));
ll MOD=1000000007;
 
bool isprime(ll n){if(n==1 || (n!=2 && n%2==0))return false; if(n==2)return true; 
		for(ll i=3;i<=sqrt(n);i+=2)if(n%i==0)return false; return true; }
ll gcd(ll a,ll b) 
{ 
    if (a == 0) 
        return b; 
    return gcd(b % a, a); 
} 

void solve()
{
	ll n;
	cin>>n;
	ll sum=0;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=n;j++)
		{
			for(ll k=1;k<=n;k++)
			{
				ll h=1;
				ll g=gcd(i,j);
				h=gcd(k,g);
				sum=sum+h;
			}
		}
	}
	cout<<sum;

}
int main()
{
	fast;
	ll t=1;
	//cin>>t;
	while(t--)
	{
		solve();
	}
}

Measured memory_bytes: 3648
import scala.io.StdIn

object Main {
  def main(args:Array[String]){
    val n=StdIn.readLine().split(" ").map(_.toInt)
    val (a,b)=(n(0),n(1))
    println(if(a>b)"a > b" else if(a<b)"a < b" else "a == b")
  }
}

Measured memory_bytes: 43840
#include<iostream>
#include<memory.h>
using namespace std;
int main()
{
	for(int n;cin>>n;)
	{
		if(n==0)
		{
			break;
		}
		int i,a[52]={0};
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		if(n==1)
		{
			cout<<a[1]<<endl;
			continue;
		}
		cout<<a[1];
		for(i=2;i<=n;i++)
		{
			if(a[i+1]==0)
			{
				if((a[i]-a[i-1])==1)
				{
					cout<<"-"<<a[i]<<endl;
				}
				else
				{
					cout<<" "<<a[i]<<endl;
				}
			}
			else if((a[i+1]-a[i])==1&&(a[i]-a[i-1])==1)
			{
				continue;
			}
			else if((a[i]-a[i-1])!=1)
			{
				cout<<" "<<a[i];
			}
			else
			{
				cout<<"-"<<a[i];
			}
		}
	}
	return 0;
}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll> > vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;
typedef pair<ll, ll> P;
const long long INF = numeric_limits<ll>::max();
const long long MOD = 1000000007;
const long double PI = 3.1415926;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
//char c;

#define Endl endl

/*--------------------template--------------------*/

int main() {
    cin >> n;
    s = to_string(n);
    REP(i, s.size()) {
        ans += (s[i] - '0');
    }
    if (n%ans == 0) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

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

using namespace std;

int main(void) {
    string S;
    int K;
    cin >> K >> S;

    int len = S.size();
    if (len <= K) cout << S << endl;
    else cout << S.substr(0,K) << "..." << endl;

    return 0;
}


Measured memory_bytes: 3628
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
class data{
public:
    int pm,pe,pj,rank;
    double me(){return (double)(pm+pe)/2;};
    double ave(){return (double)(pm+pe+pj)/3;}
    int maxi(){return max(max(pm,pe),pj);};
};
int main(){
    int n;
    while(cin>>n){
        if(!n)return 0;
        vector<data>v(n+1);
        v.clear();
        rep(i,n){
            cin>>v[i].pm>>v[i].pe>>v[i].pj;
            if(v[i].me()>=90||v[i].ave()>=80||v[i].maxi()==100){
                v[i].rank=3;
            }else if(v[i].ave()>=70||(v[i].ave()>=50&&(v[i].pe>=80||v[i].pm>=80)))
                v[i].rank=2;
            else
                v[i].rank=1;
            switch (v[i].rank) {
                case 1:
                    cout<<"C"<<endl;
                    break;
                case 2:
                    cout<<"B"<<endl;
                    break;
                default:
                    cout<<"A"<<endl;
                    break;
            }
        }
    }
}

Measured memory_bytes: 3088
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.container;
import std.array;
import std.math;
import std.range;
import std.typecons;
import std.ascii;

void main()
{
  readln;
  int right;
  int left;
  auto s = readln.chomp;
  foreach(c; s) {
    if (c == '(') {
      ++right;
    } else {
      --right;
      if (right < 0) {
        ++left;
        right = 0;
      }
    }
  }
  writeln = '('.repeat.take(left).array ~ s ~ ')'.repeat.take(right).array;
}


Measured memory_bytes: 256
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


#p,n,k,b,*a = [int(i) for i in read().split()]

n = int(input())
s = input()

ans = 0
from itertools import permutations
L = ["R","G","B"]
ans = 0
for p in permutations(L):
    dpr = dpg = dpb = 0
    x = p[0]
    y = p[1]
    for i in s:
        if i == x:
            dpr += 1
        elif i == y:
            dpg += dpr
        else:
            dpb += dpg
    ans += dpb

#print(ans)

for d in range(1,n):
    for i in range(n):
        if i+2*d >= n: break
        if s[i] != s[i+d] and s[i+2*d]!=s[i+d] and s[i]!=s[i+2*d]:
            ans -= 1
        
print(ans)










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

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    int n, t;
    cin >> n >> t;
    vector< pair<int, int> > a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i].first >> a[i].second;
    sort(a.begin(), a.end());
    vector< vector<int> > dp(n, vector<int>(6020, -1));
    dp[0][a[0].first] = a[0].second;
    for (int i = 1; i < n; ++i)
    {
        for (int j = 0; j < 6020; ++j)
        {
            if (j - a[i].first < t && j - a[i].first >= 0 && dp[i - 1][j - a[i].first] != -1)
                dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i].first] + a[i].second);
            if (dp[i - 1][j] != -1)
                dp[i][j] = max(dp[i][j], dp[i - 1][j]);
            if (a[i].first == j)
                dp[i][j] = max(dp[i][j], a[i].second);
        }
    }
    int odp = 0;
    for (int i = 0; i < 6020; ++i)
        odp = max(odp, dp[n - 1][i]);
    cout << odp;
    return 0;
}


Measured memory_bytes: 70912
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<map>
#include<bitset>
using namespace std;

typedef long long ll;
#define rep(i, n) for(int i = 0;i < (n);i++)
#define repr(i, n) for(int i = (n);i >= 0;i--)
#define repf(i, m, n) for(int i = (m);i < (n);i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1;} return 0;}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
//////////////////////////////////////////////////


int main() {
    int k; cin >> k;
    queue<ll> que;
    repf(i,1,10) que.push(i);
    ll ans;
    rep(i,k) {
        ans = que.front();
        que.pop();
        repf(j,max(0,int(ans%10)-1),min(9,int(ans%10)+1)+1) {
            que.push(ans*10+j);
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1792
import sys  
import numpy as np
N1 = list(map(int, input().split()))
N2 = list(map(int, input().split()))
N3 = list(map(int, input().split()))

temp = []
for i in range(3):
    a  = N1[i] + N2[i] - 2*N3[i]
    temp.append(a)
temp2 = []

temp2.append(N1[0] + N1[1] - 2*N1[2])

temp2.append(N2[0] + N2[1] - 2*N2[2])

temp2.append(N3[0] + N3[1] - 2*N3[2])


if len(list(set(temp))) == 1 & len(list(set(temp2))) == 1:
    print("Yes")
else :
    print("No")

Measured memory_bytes: 12504
package main

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

var rdr = bufio.NewReaderSize(os.Stdin, 1024)

func readLine() string {
	buf := make([]byte, 0, 1024)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

func atoi(s string) int {
	i, e := strconv.Atoi(s)
	if e != nil {
		panic(e)
	}
	return i
}

func readStringArray() []string {
	return strings.Split(strings.TrimSpace(readLine()), " ")
}

func readInt() int {
	return atoi(readLine())
}

func readIntArray() []int {
	var a []int
	sa := readStringArray()
	for _, s := range sa {
		a = append(a, atoi(s))
	}
	return a
}

func main() {
	n := readInt()
	var cap []int
	minCap := 1000000000000000
	for i := 0; i < 5; i++ {
		cap = append(cap, readInt())
		if cap[i] < minCap {
			minCap = cap[i]
		}
	}

	var ans int
	ans = 4 + n/minCap
	if n%minCap != 0 {
		ans++
	}
	fmt.Println(ans)
}


Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>
#include <bitset>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

const ll SIZE = 2 * 1e5 + 10, MOD = 1e9 + 7, INF = 1e9;

ll dp[SIZE], lst[SIZE][26];

int main() {
    fastInp;

    string s;
    cin >> s;

    ll pw = (1ll << (s.size() - 1));
    ll sm = 0;
    for (int i = 0; i < pw; i++) {
        ll curS = 0, cr = 0;
        for (int j = 0; j < s.size(); j++) {
            cr *= 10;
            cr += s[j] - '0';
            if ((i >> j) & 1) {
                curS += cr;
                cr = 0;
            }
            
        }
        sm += curS + cr;
    }
    cout << sm;
    return 0;
}


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

using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<int(n); i++)
#define FOR(i,m,n) for(int i=int(m); i<int(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i,a) for(auto &i:a)

typedef pair <int, int> P;
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
const int  INF = 2147483647;
const ll INFL = 9223372036854775807;
const ll mod = 1000000007;


int main() {
	int h, w;
	cin >> h >> w;
	VVC a(h, VC(w));
	REP(i, h)REP(j, w)cin >> a[i][j];
	VVLL dp(h, VLL(w, 0));
	dp[0][0] = 1;
	REP(i, h) {
		REP(j, w) {
			if (i + j == 0)continue;
			if (i == 0) {
				if (a[i][j] == '#')continue;
				dp[i][j] = dp[i][j - 1];
			}
			else {
				if (a[i][j] == '#') {
					dp[i][j] = 0;
				}
				else {
					dp[i][j] = (dp[i - 1][j] + dp[i][max(0, j - 1)])%mod;
				}
			}
		}
	}
	cout << dp[h - 1][w - 1] << endl;
}

Measured memory_bytes: 9088
#include <iostream>
using namespace std;
int main(){
  int a,b,c,n,t[10001]={0},p=0,i,j;
  for(cin>>n>>a>>b,i=0;n--;){
    for(cin>>c,j=c-a;j<=c+a-1&&j<b;)if(!(t[j++]++))p--;
  }
  cout<<b+p<<endl;
}

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

#ifdef DEBUG
#define debug(x) cout << #x << ": " << x << endl
#else
#define debug(x)
#endif

#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(i, n) for(int i = 1; i <= (n); i++)
#define ALL(v) (v).begin(), (v).end()

#define pb push_back
#define mp make_pair
#define mt make_tuple

using namespace std;

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;

const ll INF = 1e18;
const ll MOD = 1e9+7;

template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, const T &b) { if (a>b) { a = b; return true;} return false; }

signed main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    ll X, a = 100; cin >> X;
    ll t = 0;
    while(a < X)
    {
        a += a/100;
        t++;
    }
    cout << t << endl;
}

Measured memory_bytes: 3652
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		// 整数の入力
		int n = sc.nextInt();
		
		int[] a = new int[n];
		int[] b = new int[n+1];

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

		TreeMap<Integer,Integer> treeMap = new TreeMap<>();
		treeMap.put(-1,n+1);
		treeMap.put(n, n+1);
		treeMap.put(b[n], n);


		long sum = 0;

		for(long i=n-1;i>=1;i--) {
			treeMap.put(b[(int) i], (int) i);
			long lu = treeMap.higherKey(b[(int) i]);
			long uu = n;
			if(lu<n) {
				uu = treeMap.higherKey((int) lu);
			}
			long ul = treeMap.lowerKey(b[(int) i]);
			long ll = -1;
			if(ul>-1) {
				ll = treeMap.lowerKey((int) ul);
			}
			sum+=((ul-ll)*(lu-b[(int) i])+(uu-lu)*(b[(int) i]-ul))*i;

		}


		System.out.println(sum);
	}
}

Measured memory_bytes: 66856
#include<bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
#define COUT(v) cout<<(v)<<endl
#define CIN(n)  int(n);cin >> (n)
#define SCIN(n) string(n);cin >> (n)
#define YES(n) cout<<((n)? "YES" : "NO")<<endl
#define Yes(n) cout<<((n)? "Yes" : "No")<<endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) <<endl

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)

#define FOREACH(x,a) for(auto& (x) : (a) )

#define ALL(obj) (obj).begin(),(obj).end()

#define P pair<int,int>
#define I vector<int>
#define S set<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)),a.erase(unique(ALL(a)), a.end())
class Edge{
public:
    int from,to,value;
    Edge(int a,int b,int c){
        from = a;
        to = b;
        value = c;
    }
    Edge(int a,int b){
        from = a;
        to = b;
    }
};
int main(){
    CIN(N);
    I vec;
    REP(a,N){
        CIN(b);
        vec.pb(b);
    }
    int dp[N+10]={0};
    int ans = 0;
    REP(a,N){
        dp[vec.at(a)]=dp[vec.at(a)-1]+1;
        ans = max(ans,dp[vec.at(a)]);
    }
    COUT(N-ans);
    return 0;
}


Measured memory_bytes: 1908
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <numeric>
#include <cmath>

#define ll long long int
#define pb push_back
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
using namespace std;

int mx8[] = {0,0,1,-1,-1,1,-1,1};
int my8[] = {-1,1,0,0,-1,-1,1,1};
int mx4[] = {1,-1,0,0};
int my4[] = {0,0,-1,1};
const int MOD = 1000000007;


int main(){
    double n,x;
    int t;
    cin >> n >> x >> t;
    int c = ceil(n/x);
    cout << c * t << endl;
}

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

int main(){
    string n;
    cin >> n;
    for(int i=0; i<n.size(); i++){
        if(n.at(i) == '1') n.at(i) = '9';
        else n.at(i) = '1';
    }

    cout << n << endl;
}


Measured memory_bytes: 3592
#include <bits/stdc++.h>
using namespace std;
int main(void){
    char c;
    cin >> c;
    if(c == 'a' || c == 'i' || c == 'u' || c == 'e' || c == 'o'){
        cout << "vowel" << endl;
    }else{
        cout << "consonant" << endl;
    }
   
}


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

int main()
{
    string s;
    int n, a, b;

    cin >> n >> a >> b;
    cin >> s;

    int total_student = 0, out_student = 0, sum = a+b;

    for(int i = 0; i < n; i++){
        if(s[i] == 'a' && total_student < sum) {total_student++; cout << "Yes" << "\n";}
        else if(s[i] == 'b' && total_student < sum && out_student < b) {total_student++; out_student++; cout << "Yes" << "\n";}
        else cout << "No" << "\n";
    }

    return 0;
}

Measured memory_bytes: 3720
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//priority_queue<int,vector<int>, greater<int> > q2;



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

	int N, A, B; cin >> N >> A >> B;
	if (A + B > N + 1 || A * B < N) {
		cout << -1 << endl;
		return 0;
	}
	deque<int> ans;
	int a = A;
	int n = 1;
	vector<bool>F(N+1, false);
	while (1) {
		ans.push_front(a);
		F[a--] = 1;
		//cout << B << " " << (int)ans.size() << endl;
		
		if (a%A == 0) {
			n++;
			a = A * n;
			B--;
			//cout << "L" << endl;
			chmin(a, N);
			//if (a > N)break;
		}
		if (B == (N - (int)ans.size()))break;
	}
	n = N;

	while (N != ans.size()) {
		if (!F[n]) {
			cout << n-- << " ";
			N--;
		}
		else n--;
	}
	while (ans.size() != N) {
		if(!F[n])ans.push_front(n--);
		else n--;
	}
	REP(i, N) {
		cout << ans[i] << " ";
	}
	cout << endl;


	return 0;
}



Measured memory_bytes: 4224
#include <iostream>
using namespace std;
int main(){
    // Your code here!
    int N, ans;
    cin >> N;
    
    ans = 0;
    for( int i = 0; i < N; i++ ){
        int  l , r;
        cin >> l >> r;
        ans += r - l + 1;
    }
    
    cout << ans << endl;
}


Measured memory_bytes: 3596
#include <iostream>
#include <string>
#include <set>

using namespace std;

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

	set<string> s;
	string p;
	for (int i = 0; i < N; ++i) {
		string W;
		cin >> W;
		if (!p.empty() && p.back() != W.front()) {
			cout << "No" << endl;
			return 0;
		}
		if (!s.insert(W).second) {
			cout << "No" << endl;
			return 0;
		}
		p = W;
	}
	cout << "Yes" << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
using namespace std;
int main() {

	string S, T;
	cin >> S >> T;

	int ans = 0;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] != T[i]) ans++;
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 3904
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    string s;
    cin >> s;
    
    int n = s.size();
    
    using ll = long long;
    
    const ll mod = 1000000000 + 7;
    
    vector<vector<ll>> dp(n + 1, vector<ll>(4));
    
    dp[0][0] = 1;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= 3; j++) {
            (dp[i + 1][j] += (s[i] == '?' ? 3 : 1) * dp[i][j]) %= mod;
            
            if (j < 3 && (s[i] == '?' || s[i] == "ABC"[j])) {
                (dp[i + 1][j + 1] += dp[i][j]) %= mod;
            }
        }
    }
    
    cout << dp[n][3] << endl;
}
  


Measured memory_bytes: 7360