akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using i64 = long long;

int main() {
    int n;
    std::cin >> n;
    std::vector<int> h(n);
    for (int i = 0; i < n; i++) {
        std::cin >> h[i];
    }
    std::vector<int> dp(n);
    dp[1] = abs(h[0] - h[1]);
    for (int i = 2; i < n; i++) {
        dp[i] = std::min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
    }

    std::cout << dp[n - 1] << std::endl;

    return 0;
}

Measured memory_bytes: 1024
#https://atcoder.jp/contests/dp/tasks/dp_c
N = int(input())

p = [list(map(int,input().split())) for l in range(N)]
dp = [[0,0,0]]*N
dp[0] = p[0]

for i in range(1,N,1):
    dp[i] = [0,0,0]#この文がないとバグる、理由はわからん
    #print(dp)
    for j in range(3):
        for k in range(3):
            if k != j:
                dp[i][j] = max(dp[i][j],dp[i-1][k] + p[i][j])
                #print(i,j,dp[i][j])


print(max(max(dp)))

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

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second

using namespace std;

const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 31;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-6;
const double pi = atan2(0, -1);

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("arrayop.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    //int TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int x;
    cin >> x;
    int cnt = 0;
    for (int i = 1;;i++)
    {
        cnt += i;
        if (cnt >= x)
        {
            cout << i << endl;
            return 0;
        }
    }
    return 0;
}


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

const int mod = 1e+9 + 7;

int main() {

    int n;
    cin >> n;

    vector<long long int> num(n);

    for(int i = 0; i < n; ++i) {
        cin >> num[i];
     //   cout << bitset<64>(num[i]) << endl;
    }

    long long int sum = 0;

    for(int bit = 0; bit < 60; ++bit) {
        long long int n0 = 0, n1 = 0;

        for(int i = 0; i < n; ++i) {
            if(num[i] & (1ll << bit))
                n1++;
            else
                n0++; 
        }

   //     cout << bit << " : " << "n1 = " << n1 << ", n0 = " << n0 << endl;

        long long int mul = (n1 * n0) % mod;
        long long int d = (1ll << bit) % mod;
        long long int addVal = (mul * d) % mod;
        sum = (sum + addVal) % mod;

    }
    
    cout << sum << endl;

    return 0;

}

Measured memory_bytes: 2560
import itertools
n,m,q = list(map(int,input().split()))
lis = []
for i in range(q):
    lis.append(list(map(int,input().split())))
l = [x for x in range(1,m+1)]
c_list = list(itertools.combinations_with_replacement(l,n))
ans = 0
for con in c_list:
    tmp = 0
    for item in lis:
        if con[item[1]-1]-con[item[0]-1] == item[2]:
            tmp += item[3]
    ans = max(ans,tmp)
print(ans)


Measured memory_bytes: 21624
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define RREP(i, a, b) for(int i = (a); i >= (b); i--)
#define pq priority_queue
#define P pair<int, int>
#define P2 pair<int, P>
#define P3 pair<int, P2>
typedef long long ll; typedef long double ld;
using namespace std;
const int INF = 1e9, MOD = 1e9+7, around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
const int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
const ld PI = abs(acos(-1));
const int sqrtN = 512;
const int logN = 32;
const ll LINF=1e18;

template<typename T>
struct Dinic{
	struct edge{
		int to;
		T cap;
		int rev;
	};
	
	const T INF;
	vector<vector<edge>> g;
	vector<T> min_cost;
	vector<int> itr;
	
	Dinic(int n) : INF(numeric_limits<T>::max()){
		g.resize(n);
	}
	
	void add_edge(int from, int to, T cap){
		g[from].push_back(edge{to, cap, (int)g[to].size()});
		g[to].push_back(edge{from, 0, (int)g[from].size() - 1});
	}
	
	bool bfs(int s, int t){
		min_cost.assign(g.size(), -1);
		queue<int> qu; qu.push(s); min_cost[s] = 0;
		
		while(!qu.empty()){
			auto p = qu.front(); qu.pop();
			
			for(auto e:g[p]){
				if(e.cap <= 0 or min_cost[e.to] != -1) continue;
				min_cost[e.to] = min_cost[p] + 1;
				qu.push(e.to);
			}
		}
		
		return min_cost[t] != -1;
	}
	
	T dfs(int idx, const int t, T flow){
		if(idx == t) return flow;
		for(int &i = itr[idx]; i < g[idx].size(); i++){
			edge &e = g[idx][i];
			
			if(e.cap <= 0 or min_cost[idx] >= min_cost[e.to]) continue;
			T d = dfs(e.to, t, min(flow, e.cap));
			if(d > 0){
				e.cap -= d;
				g[e.to][e.rev].cap += d;
				return d;
			}
			
		}
		
		return 0;
	}
	
	T max_flow(int s, int t){
		T flow = 0;
		while(bfs(s, t)){
			itr.assign(g.size(), 0);
			T f = 0;
			while((f = dfs(s, t, INF)) > 0) flow += f;
		}

		return flow;
	}
};

int main(){
	int n,m; cin >> n >> m;
	Dinic<int> dinic(n);
	for(int i = 0; i < m; i++){
		int a,b,c; cin >> a >> b >> c;
		dinic.add_edge(a, b, c);
	}
	
	cout << dinic.max_flow(0, n - 1) << endl;
	return 0;
}



Measured memory_bytes: 3216
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  string s;
  cin >> s;
  int n = s.length();
  int cnt = 0;
  for(int i = 0; i < n; i++) {
    if(s[i] == 'x') {
      cnt++;
    }
  }
  if(cnt <= 7) {
    cout << "YES\n";
  } else {
    cout << "NO\n";
  }
  return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    
    int a = s.nextInt();
    int b = s.nextInt();
    if (a != b) {
      System.out.println(Math.max(a, b) * 2 - 1);
    } else {
      System.out.println(2 * a);
      
    }
  }
}


Measured memory_bytes: 23124
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[]a = new int[n];
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
        for(int i=n-1;i>0;i--){
            System.out.printf("%d ",a[i]);
        }
        System.out.println(a[0]);
        sc.close();
    }
}


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

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

int main()
{
    int k;
    int g,ans;

    cin >> k;

    ans = 0;
    for (int a = 1;a <= k;++a) {
        for (int b = 1;b <= k;++b) {
            for (int c = 1;c <= k;++c) {
                g = gcd(a, b);
                g = gcd(g, c);
                ans += g;
            }
        }
    }

    std::cout << ans << endl;

    return(0);
}


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

int c[27];
int s[366][27];
int t[366];
int last[27];

int prev_day(int d,int type){
    d--;
    while(t[d]!=type && t[d]!=0) d--;
    return d;
}

int post_day(int d,int type){
    d++;
    while(t[d]!=type && t[d]!=0) d++;
    return d;
}

int add(int v,int d,int type){
    int post=post_day(d,type);
    int prev=prev_day(d,type);
    v+=s[d][type];
    v+=c[type]*(post-prev)*(post-prev-1)/2;
    v-=c[type]*((d-prev)*(d-prev-1)+(post-d)*(post-d-1))/2;
    t[d]=type;
    return v;
}

int sub(int v,int d,int type){
    int post=post_day(d,type);
    int prev=prev_day(d,type);
    v-=s[d][type];
    v-=c[type]*(post-prev)*(post-prev-1)/2;
    v+=c[type]*((d-prev)*(d-prev-1)+(post-d)*(post-d-1))/2;
    t[d]=-1;
    return v;
}



int main(void){
    int D;cin>>D;
    for(int i=1;i<=26;i++){
        cin>>c[i];
    }
    for(int i=1;i<=D;i++){
        for(int j=1;j<=26;j++){
            cin>>s[i][j];
        }
        
    }
    int v=0;
    for(int i=1;i<=D;i++){
        cin>>t[i];
        last[t[i]]=i;
        v+=s[i][t[i]];
        for(int j=1;j<=26;j++){
            v-=c[j]*(i-last[j]);
        }
    }
    int M;cin>>M;
    int d,q;
    for(int i=0;i<M;i++){
        cin>>d>>q;
        v=sub(v,d,t[d]);
        v=add(v,d,q);
        t[d]=q;
        cout<<v<<endl;
    }
    return 0;
}

Measured memory_bytes: 3672
#include <iostream>


using namespace std;


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

    return 0;
}

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

int main( void )
{
    int a, b, c;
    cin >> a >> b >> c;
    int ans = ( a + b + c ) / 2;
    int mod = ( a + b + c ) % 2;
    if ( mod == 0 )
    {
        if ( (ans == a) || (ans == b) || (ans == c) )
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }
    else
    {
        cout << "No" << endl;
    }

    return 0;
}


Measured memory_bytes: 256
#include<iostream>

using namespace std;

int main(){
  int a,b,c,d;
  cin >> a >> b >> c >> d;
  while(1){
    c -= b;
    if(c <= 0){
      cout << "Yes" << endl;
      break;
    }
    a -= d;
    if(a <= 0){
      cout << "No" << endl;
      break;
    }
  }
  return 0;
}

Measured memory_bytes: 3620
let readIntArray _ =
    stdin.ReadLine().Split(" ") |> Array.map int

()
|> readIntArray
|> fun [| a; _ |] ->
    [| 1 .. a |]
    |> Array.map (fun _ -> stdin.ReadLine())
    |> Array.sort
    |> String.concat ""
    |> printfn "%s"


Measured memory_bytes: 29836
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
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 (b<a) {a=b; return 1;} return 0;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, k;
    cin >> n >> k;
    map<int, ll> mp;
    rep(i, n) {
        int a, b;
        cin >> a >> b;
        mp[a] += b;
    }
    int ans = 0;
    for (auto&& p : mp) {
        k -= p.se;
        if (k <= 0) {
            ans = p.fi;
            break;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 4224
S = input()
if S == "SUN":
    ans = 7
elif S == "MON":
    ans = 6
elif S == "TUE":
    ans = 5
elif S == "WED":
    ans = 4
elif S == "THU":
    ans = 3
elif S == "FRI":
    ans = 2
elif S == "SAT":
    ans = 1
print(ans)


Measured memory_bytes: 9104
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <cstdio>
#include <numeric>

using namespace std;
typedef long long ll;

ll X, Y, A, B, C;
ll p[100005];
ll q[100005];
ll r[100005];
vector<ll> pq;

int main()
{
    cin >> X >> Y >> A >> B >> C;
    for(ll i = 0;i < A;i++)cin >> p[i];
    for(ll i = 0;i < B;i++)cin >> q[i];
    for(ll i = 0;i < C;i++)cin >> r[i];
    sort(p, p+A, std::greater<ll>());
    sort(q, q+B, std::greater<ll>());
    sort(r, r+C, std::greater<ll>());

    ll tans = 0;
    for(ll i = 0;i < X;i++)
    {
        tans += p[i];
        pq.push_back(p[i]);
    }
    for(ll i = 0;i < Y;i++)
    {
        tans += q[i];
        pq.push_back(q[i]);
    }
    sort(pq.begin(), pq.end());
    int pqi = 0;
    for(int i = 0;i < C && pqi < X+Y;i++)
    {
        if(pq[pqi] < r[i])
        {
            tans -= pq[pqi];
            pqi++;
            tans += r[i];
        }
        else
        {
            break;
        }
    }

   cout << tans << endl;
    return 0;
}

Measured memory_bytes: 4724
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(void){
  int N;
  cin >> N;
  
  vector<string>blue(N);
  
  for(int i=0;i<N;i++){
    cin >> blue[i];
  }
  
  int M;
  cin >> M;
  
  vector<string>red(M);
  
  for(int i=0;i<M;i++){
    cin >> red[i];
  }
  
  int point = 0;
  
  for(int i=0;i<N;i++){
    int p = 0;
    for(int j=0;j<N;j++){
      if(blue[i] == blue[j])
        p++;
    }
    for(int j=0;j<M;j++){
      if(blue[i] == red[j])
        p--;
    }
    if(p > point)
      point = p;
  }
  
  cout << point << endl;
  
  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod (ll)(1e9+7)

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct T {
  int x, y, c;
};

signed main() {
  int h, w;
  cin >> h >> w;
  string s[h];
  int sm[51][51] = {};
  rep(i, 0, h) cin >> s[i];
  int wh = 0;
  rep(i, 0, h) rep(j, 0, w) {
    if(s[i][j] == '.') wh++;
    sm[i][j] = 1145141919;
  }
  queue<T> q;
  q.push({0, 0, 0});
  while(!q.empty()) {
    T t = q.front();
    q.pop();
    int x = t.x, y = t.y, c = t.c;
    if(x == h - 1 && y == w - 1) {
      cout << wh - (c + 1) << endl;
      return 0;
    }
    if(sm[x][y] < c) continue;
    c++;
    if(x && sm[x - 1][y] > c && s[x - 1][y] == '.') sm[x - 1][y] = c, q.push({x - 1, y, c});
    if(x < h - 1 && sm[x + 1][y] > c && s[x + 1][y] == '.') sm[x + 1][y] = c, q.push({x + 1, y, c});
    if(y && sm[x][y - 1] > c && s[x][y - 1] == '.') sm[x][y - 1] = c, q.push({x, y - 1, c});
    if(y < w - 1 && sm[x][y + 1] > c && s[x][y + 1] == '.') sm[x][y + 1] = c, q.push({x, y + 1, c});
  }
  cout << -1 << endl;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%lld",&n)
#define pr(n) printf("%d",n)
#define prl(n) printf("%lld",n)
#define nl printf("\n")
#define fr(i,n) for(i=0;i<n;i++)
#define bi(n) __builtin_popcount(n)
#define bil(n) __builtin_popcountll(n)


typedef long long ll;
typedef pair<int,int> pii;
const int N = 100010;
ll mod = 1e9+7;
ll fmod(ll b,ll exp){
    ll res =1;
    while(exp){if(exp&1ll)res=(res*b)%mod;
    b =(b*b)%mod;exp/=2ll;
    }
    return res;
}

ll x[N];

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int t=1,n,i,j;
    int k;
    cin>>n>>k;
    fr(i,n){
        cin>>x[i];
     }
    ll ans = 1e18;

    fr(i,n)
    {
        ll cur = x[i];
        if(i+k-1>=n)break;
        ll nex = x[i+k-1];
        if((cur*nex)<0)ans = min(ans,min(abs(cur)+2*abs(nex), abs(nex)+2*abs(cur)));
        else ans = min(ans,max(abs(cur),abs(nex)));
    }
    cout<<ans<<"\n";

    
    return 0;
}


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

using namespace std;

string A, B, C;

int main(void)
{
	cin >> A >> B >> C;
	
	if(A[A.size()-1] == B[0] && B[B.size()-1] == C[0]) cout << "YES";
	else cout << "NO";
	cout << endl;
	
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline T chmax(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T chmin(T& a, const T b) { return a = (a > b) ? b : a; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT { 
  INIT(){cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;


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

  REP(i, 100000){
    if((int)(i*0.08) == A && (int)(i*0.1) == B){
      cout << i << "\n";
      return 0;
    }
  }

  cout << -1 << "\n";

  return 0;
}

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

int main() {
    string s;
    cin >> s;
    int ans;
    if (s == "SUN") {
        ans = 7;
    }else if (s == "MON"){
        ans = 6;
    }else if (s == "TUE"){
        ans = 5;
    }else if (s == "WED"){
        ans = 4;
    }else if (s == "THU"){
        ans = 3;
    }else if (s == "FRI"){
        ans = 2;
    }else if (s == "SAT"){
        ans = 1;
    }
    
    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 reps(i,x,n) for(int i=x; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define all(X) (X).begin(),(X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

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

template<class A, size_t N, class T> void Fill(A (&a)[N], const T &v){ fill( (T*)a, (T*)(a+N), v ); }

template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"; return os;}
template<class T,size_t n> ostream& operator<<(ostream &os, const array<T,n> &t) {os<<"{"; rep(i,n) {os<<t[i]<<",";} os<<"}"; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const ll INF = 1e9+7;


int main(){
	ios_base::sync_with_stdio(false);
	int N, A, B, ans=0;

	cin >> N >> A >> B;

	cout << min(A, B) << " " << max(0, A + B - N) << endl;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
const int MOD = 1000000007;
int N,A[MAXN],x[MAXN],st[MAXN],ed[MAXN];
int64 cnt[MAXN],V[MAXN],D[MAXN],ID[MAXN],S,tr[MAXN],tr_cnt[MAXN],Inv_2,ans;
int64 fpow(int64 x,int64 c) {
	int64 res = 1,t = x;
	while(c) {
		if(c & 1) res = res * t % MOD;
		t = t * t % MOD;
		c >>= 1;
	}
	return res;
}
int lowbit(int x) {return x & (-x);}
void insert(int x,int64 v) {
	while(x <= N) {
		tr[x] += v;
		tr[x] %= MOD;
		tr_cnt[x]++;
		x += lowbit(x);
	}
}
int64 Query(int x,int64 v) {
	int64 res = 0;
	while(x > 0) {
		res += tr[x];
		x -= lowbit(x);
	}
	res %= MOD;
	return res * v % MOD;
}
int64 getnum(int x) {
	int64 res = 0;
	while(x > 0) {
		res += tr_cnt[x];
		x -= lowbit(x);
	}
	return res;
}
int64 Query_range(int L,int R,int64 v) {
	if(L == 0) ++L;
	if(L > R) return 0;
	return (Query(R,v) + MOD - Query(L - 1,v)) % MOD;
}
void Solve() {
    scanf("%d",&N);
    for(int i = 1 ; i <= N ; ++i) scanf("%d",&A[i]),cnt[A[i]]++;
    for(int i = N - 1; i >= 1 ; --i) cnt[i] += cnt[i + 1];
    S = 1;Inv_2 = (MOD + 1) / 2;
    for(int i = 1 ; i <= N ; ++i) {
    	cnt[i] -= N - i;
    	if(cnt[i] <= 0) {puts("0");return;}
    	S = S * cnt[i] % MOD;
    }
    for(int i = 1 ; i <= N ; ++i) {
    	V[i] = (cnt[i] - 1) * fpow(cnt[i],MOD - 2) % MOD;
    }
    D[0] = 1;
    st[0] = 0;
    for(int i = 1 ; i <= N ; ++i) {
    	x[i] = x[i - 1];D[i] = D[i - 1];
    	if(V[i] == 0) {x[i]++;st[x[i]] = i;ed[x[i] - 1] = i - 1;}
    	else D[i] = D[i] * V[i] % MOD;
    	ID[i] = fpow(D[i],MOD - 2);
    }
    ed[x[N]] = N;
    for(int i = 1 ; i <= N ; ++i) {
    	ans += Query_range(st[x[A[i]]],A[i],D[A[i]] * S % MOD * Inv_2 % MOD);
    	ans %= MOD;
    	insert(A[i],ID[A[i]]);
    }
    memset(tr,0,sizeof(tr));
    memset(tr_cnt,0,sizeof(tr_cnt));
    for(int i = 1 ; i <= N ; ++i) {
    	int64 t = Query_range(A[i] + 1,ed[x[A[i]]],ID[A[i]] * S % MOD * Inv_2 % MOD);
    	ans += ((getnum(N) - getnum(A[i])) * S % MOD + MOD - t) % MOD;
    	ans %= MOD;
    	insert(A[i],D[A[i]]);
    }
    printf("%lld\n",ans);
}

int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

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

int main() {
  string N;cin>>N;
  
  ll sum=0;
  int num = N.size();
  for(int i=0;i<num;i++){
    sum+=N[i]-'0';
  }
  
  if(sum%9==0) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 3688
#include <numeric>
#include <algorithm>
#define REP(i,n) for(int i=0; i<(int)(n); i++)
 
#include <cmath>
#include <queue>
#include <cstdio>
#include <set>
 
inline int getInt(){ int s; scanf("%d", &s); return s; }
inline int getDouble(){ double s; scanf("%lf", &s); return s; }
 
using namespace std;
 
const double EPS = 1e-8;
 
struct point{
  double r;
  double x;
  double y;
  point(){}
  point(double xx, double yy) :
    x(xx), y(yy) { r = sqrt(xx * xx + yy * yy); }
};
 
inline double dist(const point &a, const point &b){
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
 
inline bool operator < (const point &lhs, const point &rhs){
  return lhs.r < rhs.r;
}
 
int main(){
  const int n = getInt();
  const double r = getDouble();
 
  vector<point> p(n);
 
  REP(i,n){
    double x = getDouble();
    double y = getDouble();
    p[i] = point(x, y);
  }
 
  sort(p.begin(), p.end());
 
  vector<int> flag(n, 1);
 
  REP(i,n) if(flag[i] == 1){
    for(int j = i + 1; j < n; j++){
      if(p[j].r -p[i].r > r + r) break;
      if(dist(p[i], p[j]) < r + r){
        flag[j] = 0;
      }
    }
  }
 
  printf("%d\n", accumulate(flag.begin(), flag.end(), 0));
 
  return 0;
}

Measured memory_bytes: 6284
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

long dd(int n){
	return n*n;
}

int main(){
	int n, t[3];
	
	cin >> n;
	for(int i=0; i<n; ++i){
		for(int j=0; j<3; ++j)
		scanf(" %d", &t[j]);
	 		
		sort(t, t+3);
		
		if( dd(t[2]) == dd(t[0])+dd(t[1]) ) printf("YES\n");
		else printf("NO\n");
	}
	 
	return 0;
}

Measured memory_bytes: 1216
#define _USE_NATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <random>
#include <tuple>


using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
#define EPS 1e-9
const int INF = 1050000000;
const long long LINF = 1LL << 60;
const ll MOD = 1e9 + 7;
const int MINF = -1050000000;

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


int main() {
	
	ll n, k;
	cin >> n >> k;

	ll cnt = n/k;
	ll tmp = n - (k*cnt);
	ll res = abs(tmp - k);

	cout << min({ tmp, res, n }) << endl;

	return 0;
}

Measured memory_bytes: 256
# coding: utf-8

import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]

N = input()

flag = "No"

if N[0] == "7":
    flag = "Yes"
if N[1] == "7":
    flag = "Yes"
if N[2] == "7":
    flag = "Yes"

print(flag)


Measured memory_bytes: 76388
function Main(input) {
  input=input.split("\n");
  const T=input[0].trim().split(" ")[1]-0;
  const N=input[0].trim().split(" ")[0]-0;
  var min=Infinity;
  var prices=input[1].split(" ");
  var maxProfit=0;
  var counter=0;
  for(var i=0;i<prices.length;i++){
    prices[i]-=0;
  }
  //console.log(prices);
  for(var i=0;i<prices.length;i++){
    if(min>prices[i]){
      min=prices[i];
      continue;
    }
    if(prices[i]-min>maxProfit){
      maxProfit=prices[i]-min;
      counter=1;
    }else if(prices[i]-min===maxProfit){
      counter++;
    }
    //console.log(counter);
    //console.log(min);
  }
  console.log(counter);
}

//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
  
  


Measured memory_bytes: 39496
input_data= input().split()
N = int(input_data[0])
L = int(input_data[1])

apple=[]

for num in range(1,N+1):
  apple.append(L + num -1)

taberu = min(map(abs,apple))

if taberu > max(apple):
  print(sum(apple)+taberu)
else:
  print(sum(apple)-taberu)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef vector<unsigned int>vec;
typedef vector<vec> mat;
typedef vector<vector<int>> Graph;

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const ll  MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll K, A, B;
    cin >> K >> A >> B;
    ll ans = 1;
    if(B - A <= 2) cout << K + 1 << endl;
    else{
        ans += A - 1;
        K -= A - 1;
        ans += (B - A) * (K / 2);
        if(K % 2 == 1) ans++;
        cout << ans << endl;
    }
}


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

double arr[3000+5];
double dp[3000+5][6000+5];


ll N;
double solve(ll i, ll c){
	double ans = 0;
	
	if (dp[i][3000+c] >=0 ) return dp[i][3000+c];
	
	if (i >= N) {
		if(c>0) ans = 1.0;
	}
	else{
		ans += arr[i]*solve(i+1, c+1);
		ans += (1-arr[i])*solve(i+1, c-1);
	}
	dp[i][3000+c] = ans;
	return dp[i][3000+c];
}

int main(){
	cin>>N;
	for(ll i = 0;i<N;i++)cin>>arr[i];
	
	memset(dp, -1, sizeof dp);
	
	//cout<<
	
	//cout<<solve(0,0)<<endl;
	printf("%.10lf\n", solve(0,0));
}

Measured memory_bytes: 141440
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i,n) for(long long i=0; i<n; i++)
#define repr(i,n,m) for(long long i=m; i<n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) for (auto&& itr : x) { cerr << (itr) << " "; }

template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t;}
template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t;}

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

  int x,y;
  cin >> x >> y;

  for (int i = 0; i <= x; ++i) {
    int a = i;
    int b = x - i;
    int total = a * 2 + b * 4;
    if (total == y) {
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
#define int long long
using namespace std;

int a,b,c,k;

signed main()
{
	cin>>a>>b>>c>>k;
	
	if (k%2==1)  cout<<0-(a-b)<<endl;
	else cout<<(a-b)<<endl;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main() {
  int A, B, C, X;
  cin >> A >> B >> C >> X;
  int res = 0;
  for (int a = 0; a <= A; a++) {
    for (int b = 0; b <= B; b++) {
      for (int c = 0; c <= C; c++) {
        int total = 500 * a + 100 * b + 50 * c;
        if (total == X) res++;
      }
    }
  }
  cout << res << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i,s,n) for(int i = (s); i < (n); i++)
#define INF 1001001
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main(){
    int A, B, m;
    cin >> A >> B >> m;
    int a[A], b[B];
    int min_a = INF, min_b = INF;
    rep(i, A) { 
        cin >> a[i];
        if(a[i]<min_a)
            min_a = a[i];
    }
    rep(i, B) { 
        cin >> b[i];
        if(b[i]<min_b)
            min_b = b[i];
    }
    int min_cost = min_a + min_b;
    rep(i, m) { 
        int x, y, c;
        cin >> x >> y >> c;
        min_cost = min(min_cost, a[x - 1] + b[y - 1] - c);
    }
    cout << min_cost << endl;
    return 0;
}

Measured memory_bytes: 1024
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL<<60)
#define D double
#define fs first
#define sc second
#define int long long
#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 REP(i,n)  FOR(i,0,(n))
#define RREP(i,n) RFOR(i,0,(n))
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x)  cout << #x << " = " << (x) << endl;
#define SP << " " << 
typedef pair<int,int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

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

  int h,w;

  cin >> h >> w;

  int ans = ceil(w / 2.0) * ceil(h / 2.0) + (w / 2) * (h / 2);
  if(h == 1 || w == 1){
    ans = 1;
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int,int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i,n) for(int i=0; i<(int)(n);i++)
#define REP(i,n) for(int i=1;i<=(int)(n);i++)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define ALL(a)  a.begin(),a.end()
#define RALL(a) a.begin(),a.end(),greater<int>()
#define ROT(a) a.begin(),a.begin()+1,a.end()
#define RROT(a) a.begin(),a.end()-1,a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9+7;
const int MAX = 10000000;
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
//cout << 'H' << endl;
/*--------------------------------------------*/

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

    int N;
    cin >> N;

    map<int,ll> mp;

    rep(i,N){
        int a;
        cin >> a;
        if(!mp[a]){
            mp[a] = 1; 
        }else{
            cout << "NO" << endl;
            return 0;
        }      
    }
    cout << "YES" << endl;
}

Measured memory_bytes: 12800
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        String[] place = new String[N];
        int[] value = new int[N];
        for(int i=0; i<N; i++) {
            place[i] = sc.next();
            value[i] = Integer.parseInt(sc.next());
        }
        
        int[] ans = new int[N];
        for(int i=0; i<N; i++) {
            int count = 0;
            for(int j=0; j<N; j++) {
                if(i==j) continue;
                if(place[i].equals(place[j])) {
                    if(value[i] < value[j]) {
                        count++;
                    }
                }else {
                    if(place[i].compareTo(place[j]) > 0) {
                        count++;
                    }
                }
            }
            ans[count] = i;
        }
        
        for(int i : ans) {
            System.out.println(i+1);
        }
    }
}


Measured memory_bytes: 24916
#include <iostream>
#include <set>
#include <stack>

struct state{
  std::set<int> visited;
  int last;
};

bool adjacent[10][10] = {};

int main(void){
  int n, m;
  std::cin >> n >> m;

  for(int i=0; i<m; i++){
    int a, b;
    std::cin >> a >> b;
    adjacent[a][b] = true;
    adjacent[b][a] = true;
  }

  /**
  for(int i=1; i<=m; i++){
    for(int j=1; j<=m; j++){
      std::cout << adjacent[i][j] << " ";
    }
    std::cout << std::endl;
  }
  **/

  int count = 0;
  std::stack<state> st;
  state s;
  s.last=1;
  s.visited.insert(s.last);
  st.push(s);
  while(!st.empty()){
    state s = st.top();
    //std::cout << s.last << " " << s.visited.size() << std::endl;
    st.pop();
    if(s.visited.size() == n){
      count++;
    }

    for(int i=1; i<=n; i++){
      if(adjacent[s.last][i] && s.visited.find(i) == s.visited.end()){
        state new_s;
        new_s.last = i;
        new_s.visited = s.visited;
        new_s.visited.insert(i);
        st.push(new_s);
      }
    }
  }

  std::cout << count << std::endl;

  return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);

		int num = sc.nextInt();
		
		while(num != 0){
			ArrayList<Integer> tri = new ArrayList<Integer>();
			tri.add(sc.nextInt());
			tri.add(sc.nextInt());
			tri.add(sc.nextInt());
			Collections.sort(tri);
			if((Math.pow(tri.get(0),2)+Math.pow(tri.get(1),2))!=Math.pow(tri.get(2),2)){
				System.out.println("NO");
			}else{
				System.out.println("YES");
			}
			num--;
		}
		sc.close();
	}
}

Measured memory_bytes: 33760
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#define LL long long
using namespace std;
LL a,b,ans;
int main()
{
	scanf("%lld%lld",&a,&b);
	if(a==b) {printf("1\n");return 0;}
	int j;
	for(int i=60;i>=0;i--) if((a&(1ll<<i))!=(b&(1ll<<i))) {j=i;break;}
	a%=1ll<<j;
	b%=1ll<<j;
	ans=(1ll<<j)-a;
	LL t=0;
	for(int i=60;i>=0;i--) 
		if(b&(1ll<<i)) 
	{t=(1ll<<(i+1))-1;break;}
	ans+=min(1ll<<j,t+1+(1ll<<j)-a);
	printf("%lld\n",ans);
	return 0;
}

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

const int INF = 1e9+7;

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

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

  sort(h.begin(), h.end());

  int ans = INF;
  for ( int i = 0; i < N-K+1; i++ ) {
    ans = min(ans, h[i+K-1]-h[i]);
  }

  cout << ans << endl;

  return 0;
}


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

int main() {
  long long int v[87];
  int N;
  cin >> N;
  v[0] = 2;
  v[1] = 1;
  for (int i = 2; i <= N; i++) {
    v[i] = v[i - 2] + v[i - 1];
  }
  cout << v[N] << endl;
  return 0;
}

Measured memory_bytes: 256
#include<stdio.h>

int main(void)
{

	int N,i,j;
	char S[101], T[101],A[300];

	scanf("%d", &N);
	scanf("%s %s", S,T);
	j = 0;
	for (i = 0;i<N;i++) {
		A[j] = S[i];
		j++;
		A[j] = T[i];
		j++;
	}
	A[j] = '\0';
	printf("%s", A);
		return 0;
}

Measured memory_bytes: 128
#include <stdio.h>

int main(void){
    int N ,M;
    scanf("%d",&N);
    scanf("%d",&M);
    int favorite[M];
    for (int a;a<M;a++)
    {
        favorite[a]=0;
    }

    for (int i;i<N;++i)
    {
        int K;
        scanf("%d",&K);
        int j = 0;
        for (;j<K;j++)
        {
            int A;
            scanf("%d",&A);
            favorite[A-1]++;
        }
        
    }

    int count = 0;
    for (int k; k<M; ++k)
    {
        if (favorite[k]==N)
        {
            count++;
        }
    }
    printf("%d\n",count);
    return 0;
}

Measured memory_bytes: 128
# code-festival-2016-qualcC - 二人のアルピニスト / Two Alpinists
from bisect import bisect_left as bs


def main():
    """
      /----\     <- xi (= yi)
     /      \--\
    /           \
      xi  yi
    0 ~ xi / xi ~ yi / yi+1 ~ N-1
    """
    N = int(input())
    T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    x, y = T[-1], A[0]  # max of x, y
    xi, yi = T.index(x), N - A[::-1].index(y) - 1  # indices of x, y
    if x != y or xi > yi:
        print(0)
        return
    MOD = 10 ** 9 + 7
    ans, cur = 1, 0
    for i in T[: xi + 1]:
        if i > cur:  # point of new altitude -> no choice (ans * 1)
            cur = i
        else:
            ans = (ans * cur) % MOD  # mulitple choices (1, 2, ..., cur)
    for _ in range(xi + 1, yi):
        ans = (ans * cur) % MOD
    for i in A[yi + 1 :]:
        if i < cur:
            cur = i
        else:
            ans = (ans * cur) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Measured memory_bytes: 19140
#include<bits/stdc++.h>
using ll  = long long;
#define int ll
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define erep(e,v) for(auto && e :v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9:(int)1e18)
#define EPS 0.0000000001
using namespace std;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
using ld  = long double;
using pii = pair<int,int>;
using piii = pair<int,pii>;
int W,H;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
bool valid(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
signed main(){
    vector<int>v(3);erep(e, v) cin >> e;
    sort(all(v));
    rep(i,v.size()){
        if(i) cout <<" ";
        cout << v[i];
    }
    cout<< endl;
    
}





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

#define rep(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define repn(i, m, n) for (int (i) = (m) ; (i) < (int)(n) ; ++(i))
#define rep_rev(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define repn_rev(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= (m) ; --(i))

#define INF 2e9
#define ll long long
#define MOD 1e9+7

typedef pair<int, int> P;

int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    rep(i, n) a[i]--;
    vector<int>idx(n);
    rep(i, n) idx[a[i]] = i;
    set<int> s;
    ll ans = 0;

    for(int x=n-1; x>= 0; --x){
        int i = idx[x];
        ll c = 0;
        //cacl c
        s.insert(i);
        vector<int> l(2, -1);
        vector<int> r(2, n);
        
        /*calc l*/ 
        auto it = s.find(i);
        rep(j, 2){
            if(it == s.begin()) break;
            --it;
            l[j] = *it;
        }

        /*calc r*/
        it = s.find(i);
        rep(j, 2){
            ++it;
            if(it == s.end()) break;
            r[j] = *it;
        }

        vector<int> ls(2);
        ls[0] = i-l[0]; ls[1] = l[0]-l[1];
        vector<int> rs(2);
        rs[0] = r[0]-i; rs[1] = r[1]-r[0];
        c = (ll)ls[0]*rs[1] + (ll)ls[1]*rs[0];

        ans += c*(x+1);
    }

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

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

int     main() {
        string S;
        cin >>S;
        int i=0;
        int R=0;
        int L=0;
        bool PreR=true;
        do {
                // Start from R
                if (PreR) {
                        if ('R'==S.at(i)) {
                                R++;
                        } else {
                                L++;
                                PreR=false;
                        }
                } else {
                        // L loop
                        if ('L'==S.at(i)) {
                                L++;
                        } else {
                                if (R>1) {
                                        for (int i=1; i<R; i++) cout <<"0 ";
                                }
                                cout <<(R+1)/2+L/2 <<' ';
                                cout <<R/2+(L+1)/2 <<' ';
                                if (L>1) {
                                        for (int i=1; i<L; i++) cout <<"0 ";
                                }
                                R=1;
                                L=0;
                                PreR=true;
                        }
                }
                i++;
        } while (i<S.size());
        if (R>1) for (int i=1; i<R; i++) cout <<"0 ";
        cout <<(R+1)/2+L/2 <<' ';
        cout <<R/2+(L+1)/2 <<' ';
        if (L>1) for (int i=1; i<L; i++) cout <<"0 ";
        cout <<endl;
        return 0;
};

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

int main() {
  int a, b, c, d, e, k;
  cin >> a >> b >> c >> d >> e >> k;

  vector<int> n{a, b, c, d, e};
  sort(n.begin(), n.end());

  if(n[4] - n[0] <= k){
    cout << "Yay!" << endl;
  }else {
    cout << ":(" << endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

void solve(vector<int> ls, const vector<pair<int, int>> wc, const int n, const int m) {
    int sum = 0;
    int i=0;
    int cnt=0;
    while (i<n && cnt<m) {
        if (ls[wc[i].second] > 0) {
            sum += wc[i].first;
            ls[wc[i].second]--;
            cnt++;
        }
        i++;
    }
    cout << sum << endl;
}

int main() {
    int n,m,c;         cin >> n >> m >> c;
    vector<int> ls(c); for (auto &l: ls) cin >> l;
    vector<pair<int, int>> wc(n);
    for (auto &x: wc) {
        int c, w;
        cin >> c >> w;
        x.first = w;
        x.second = c - 1;
    }
    sort(wc.begin(), wc.end());
    reverse(wc.begin(), wc.end());
    solve(ls, wc, n, m);    
}



Measured memory_bytes: 5012
H,W=map(int, input().split())
N=int(input())
A=list(map(int, input().split()))
#色N種類　1~N :i
#色iはai個
L=[]
for i,a in enumerate(A):
    L+=([str(i+1)]*a)
#print(L)
import numpy as np
ans=[[0]*W for _ in range(H)]
for h in range(H):
    for w in range(W):
        if h%2==0:
            ans[h][w]=L.pop(0)
        else:
            ans[h][-(w+1)]=L.pop(0)
#print(ans)
for h in range(H):
    print(" ".join(ans[h]))



Measured memory_bytes: 13180
/****************************
/////  Shubham Singhal
/////  LNMIIT
*****************************/
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define eb emplace_back
#define lwr lower_bound
#define upr upper_bound
#define pq priority_queue
#define umap unordered_map
#define all(x) x.begin(),x.end()
#define rep(i,n) for(i=0; i<n;i++)
#define repp(i,a,n) for(i=a; i<n;i++)
#define precise(x) cout<<fixed<<setprecision(x)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define prn(x) cout<<x<<"\n"
#define prn2(x, y) cout<<x<<" "<<y<<"\n"
#define prn3(x, y, z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define prn4(x, y, z, a) cout<<x<<" "<<y<<" "<<z<<" "<<a<<"\n"
#define prnv(x) for(auto i:x) cout<<i<<" "; cout<<"\n"
#define MOD 1000000007
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd; 
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pil> vpil;
typedef vector<pli> vpli;
typedef vector<pll> vpll;
const long long INF64 = 1'000'000'000'000'000'000ll;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}
int main()
{
    fast();
    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    ll z,x,y,haha=lcm(c,d);
    x=b/c-(a-1)/c;
    y=b/d-(a-1)/d;
    z=b/haha-(a-1)/haha;
    cout<<b-a+1-x-y+z;
}

Measured memory_bytes: 256
s = input()
# 文字数の処理があるからlenで長さとる
n = len(s)
# 半分までがleft
sl = s[:n//2]
# 半分+1(中央を飛ばしてから)を始点としてright（奇数だから）
sr = s[n//2+1:]
# 範囲を全指定にしてマイナスステップにすると、要素がリバースする。
# 右半分が回文で左も一緒なら強い回文と言える
if sl == sr and sr ==  sr[::-1]:
    print('Yes')
else:
    print('No')

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

int main()
{
  int N;
  cin >> N;
  vector<long long int> A(N), B(N);
  for (int i = 0; i < N; i++)
  {
    cin >> A[i] >> B[i];
  }
  long long int cnt = 0;
  for (int i = N - 1; i >= 0; i--)
  {
    A[i] += cnt;
    if (A[i] % B[i] == 0)
      continue;
    else
    {
      if (A[i] > B[i])
      {
        long long int rem = A[i] % B[i];
        cnt += B[i] - rem;
      }
      else if (A[i] < B[i])
      {
        cnt += B[i] - A[i];
      }
    }
  }
  cout << cnt << endl;
}


Measured memory_bytes: 1792
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#include <random>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<57;
const double pi=acos(-1);
const double eps=1e-6;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,-0};
const int DX[8]={-1,-1,-1,0,1,1,1,0},DY[8]={1,0,-1,-1,-1,0,1,1};

int n;
vi a,b;

int main(){
	cin>>n;
	a=b=vi(n);
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<n;i++) cin>>b[i];
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	ll res=1;
	for(int i=1;i<n;i++){
		if(a[i]>b[i]){
			auto it=lower_bound(a.begin(),a.end(),b[i]);
			if(it<a.begin()+i) (res*=a.begin()+i-it+1)%=mod;
		}
		else{
			auto it=lower_bound(b.begin(),b.end(),a[i]);
			if(it<b.begin()+i) (res*=b.begin()+i-it+1)%=mod;
		}
	}
	cout<<res<<endl;
}

Measured memory_bytes: 1024