akhauriyash/Code-Regression huggingface dataset:
n_a, n_b, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)

ans = a.min + b.min

m.times do
    xyc = gets.split.map(&:to_i)
    t = a[xyc[0]-1] + b[xyc[1]-1] - xyc[2]
    ans = t if ans > t
end

puts ans

Measured memory_bytes: 18588
package main

import (
  "fmt"
)

func main() {
  var n int
  fmt.Scan(&n)
  a := make([]int, n)
  for i:= range a {
    fmt.Scan(&a[i])
  }
  counter := make(map[int]int)
  for _, v := range a {
    counter[v]++
  }
  evenCount := 0
  for _, v := range counter {
    if v % 2 == 0 {
      evenCount++
    }
  }
  answer := len(counter)
  if evenCount % 2 == 1 {
    answer--
  }
  fmt.Println(answer)
}


Measured memory_bytes: 8448
from collections import defaultdict
N, M = map(int, input().split())
A = {i:0 for i in range(N+1)}
for _ in range(M):
  A[int(input())] = 1

mod = 10**9 + 7
dp = [0]*(N+1)
dp[0] = 1
for i in range(N):
  if A[i+1] != 1: 
    dp[i+1] += dp[i]
    dp[i+1] %= mod
  if i+2 >= N+1:
    continue
  if A[i+2] != 1:
    dp[i+2] += dp[i]
    dp[i+2] %= mod

print(dp[N])
  



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


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

    if (a > b) swap(a,b);
    
    rep(i,b)cout << a;
    cout << endl;

	return 0;
}



Measured memory_bytes: 256
def main():
    n = int(input())
    v_list = list(map(int, input().split()))
    v_list.sort()
    ans = 0

    for i in range(n):
        if i == 0 or i == 1:
            ans += v_list[i] / 2 ** (n - 1)
        else:
            ans += v_list[i] / 2 ** (n - i)

    print(ans)


if __name__ == "__main__":
    main()


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

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

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<char> v;

void p() {
    for (char c : v) {
        cout << c;
    }
    cout << endl;
}

void dfs(int digits, int idx, int kinds) {
    if (digits == idx) {
        p();
        return;
    } else {
        for (int i = 0; i < kinds + 1; i++) {
            v[idx] = 'a' + i;
            if (i == kinds)
                dfs(digits, idx + 1, kinds + 1);
            else
                dfs(digits, idx + 1, kinds);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    v.resize(n);
    dfs(n, 0, 0);

    return 0;
}


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

public class Main {
    public static void main(String[] args) {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int n = scanner.nextInt();
        int ans = 1;
        for(int i = 0; i < 3; i++) {
            ans *= n;
        }
        out.println(ans);
        
        out.flush();
    }
    
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        
        public FastScanner() {
            this(new InputStreamReader(System.in));
        }
        
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        
        long nextLong() {
            return Long.parseLong(next());
        }
        
        double nextDouble() {
            return Double.parseDouble(next());
        }
        
        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


Measured memory_bytes: 23124
fun main(args: Array<String>) {
    val n = readInt()
    val a = readIntList()
    val cnt = LongArray(n)

    a.forEach {
        cnt[it - 1]++
    }

    val choose = cnt.map { it.choose2() }

    val sum = choose.sum()

    a.forEach {
        val i = it - 1
        val t = (cnt[i] - 1).choose2()
        println(sum - choose[i] + t)
    }
}

fun Long.choose2() = this * (this - 1) / 2

// Tools
fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)

Measured memory_bytes: 95832
N, T = gets.split.map {|w| w.chomp.to_i}

@ct = []

(1..N).each do
  c, t = gets.split.map {|w| w.chomp.to_i}
  @ct.push({:c => c, :t => t})
end

if m = @ct.select{|w| w[:t] <= T}.min{|a, b| a[:c] <=> b[:c]}
  puts m[:c]
else
  puts "TLE"
end


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

const int N = 606, inF = N;
typedef long double ld;

int n, m;
ld ans = inF;
ld dp[N];
vector<int> nei[N];

ld act(int y) {
	for (int x = y - 1; x >= 0; x--) {
      dp[x] = 0;
		for (int u: nei[x])
			dp[x] += dp[u];
		dp[x] /= nei[x].size();
		dp[x]++;
	}
	return dp[0];
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m;
	while (m--) {
		int u, v;
		cin >> u >> v;
		nei[--u].push_back(--v);
	}
	for (int i = n - 2; ~i; i--) {
		ld sum = 0, maxi = 0;
		for (int u: nei[i]) {
			maxi = max(maxi, dp[u]);
			sum += dp[u];
		}
		if (nei[i].size() > 1) {
			dp[i] = (sum - maxi) / (nei[i].size() - 1) + 1;
			ans = min(ans, act(i));
		}
		dp[i] = sum / nei[i].size() + 1;
	}
	cout << fixed << setprecision(10) << min(ans, dp[0]);
}


Measured memory_bytes: 1408
#include "bits/stdc++.h"
using namespace std;
#define ll long long
int main(int argc, char const *argv[])
{
  ll n, m, r;
  cin >> n >> m >> r;

  ll perm[r];
  for(ll i = 1; i <= r; i++)
    cin >> perm[i-1];

  ll graph[n+1][n+1];

  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      if(i == j){
        graph[i][j] = 0;continue;
      }
      graph[i][j] = 1000000000;}
  }


  for(int i = 1; i <= m; i++){
    ll x,y,z;
    cin >> x >> y >> z;

    graph[x][y] = z;
    graph[y][x] = z;
  }


  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      for(int k = 1; k <= n; k++){
        graph[j][k] = min(graph[j][i]+graph[i][k], graph[j][k]);
      }
    }
  }

  ll ans = 0;
sort(perm, perm+r);
  for(int i = 0; i +1 < r; i++){
    ans += graph[perm[i]][perm[i+1]];
  }

  ll global_min = LLONG_MAX;
  global_min = min(global_min, ans);

  while(next_permutation(perm, perm+r)){
    ans = 0;
    for(int i = 0; i+1 < r; i++){
      ans += graph[perm[i]][perm[i+1]];
    }
    global_min = min(global_min, ans);
  }
ans = 0;
  for(int i = 0; i +1 < r; i++){
    ans += graph[perm[i]][perm[i+1]];
  }
  global_min = min(global_min, ans);

  cout << global_min;
  return 0;
}

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

int main(){
    int S;
    cin >> S;
    int h, m, s;
    h = S / 3600;
    m = (S % 3600) / 60;
    s = (S % 3600) % 60;
    cout << h << ":" << m << ":" << s <<endl;
return 0;
}
    

Measured memory_bytes: 3084
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define RFOR(i, a, b) for(int i = a; i >= b; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
const int INF = 1e9;
typedef long long ll;
using namespace std;
int main() {
    int A, B, C;
    cin >> A >> B >> C;
    if(A + B >= C) puts("Yes");
    else puts("No");
}

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

using namespace std;

#define ll long long

struct Point {
	int x, y;
};

double dist(Point a, Point b) {
	return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}


void solve() {

	int n;
	cin >> n;
	int arr[n];
	Point p[n];
	for(int i = 0; i < n; i++) {
		arr[i] = i;
		cin >> p[i].x >> p[i].y;
	}
	int cnt = 0;
	double total = 0;
	do {
		for(int i = 1; i < n; i++) {
			total +=  dist(p[arr[i]], p[arr[i - 1]]);
		}
		cnt++;
	}while(next_permutation(arr, arr + n));
	cout << setprecision(10);
	cout << total / (double)cnt;




	
}

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	solve();

	return 0;
}

Measured memory_bytes: 3724
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l,r) l+(r-l)/2
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}

void solve(){
	int s,t,d;
	cin>>d>>t>>s;
	if(d>t*s)cout<<"No";
	else cout<<"Yes";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}

Measured memory_bytes: 3656
//{{{
#include<algorithm>
#include<cmath>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; }
//}}}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> ss(1 << n); cin >> ss;
    map<int, int> mp;
    for(int i = 0; i < (1 << n); i++){
        mp[- ss[i]]++;
    }
    // sort(ss.rbegin(), ss.rend());
    map<int, int> sls;
    sls[mp.begin()->first]++;
    mp.begin()->second--;
    if(mp.begin()->second == 0){
        mp.erase(mp.begin());
    }
    printde(mp);
    for(int i = 0; i < n; i++){
        int i1 = 1 << i, i2 = 1 << (i + 1);
        vector<int> nexts;
        auto ite = sls.begin();
        printde(i, i1, i2, sls);
        int num = ite->second;
        for(int j = i1; j < i2; j++){
            if(num == 0){
                ite++;
                num = ite->second;
            }
            printde(ite->first);
            auto ite2 = mp.upper_bound(ite->first);
            if(ite2 == mp.end()){
                cout << "No" << '\n';
                return 0;
            }
            nexts.push_back(ite2->first);
            ite2->second--;
            if(ite2->second == 0){
                mp.erase(ite2);
            }
            num--;
        }
        for(auto& next : nexts){
            sls[next]++;
        }
    }
    cout << "Yes" << '\n';
}


Measured memory_bytes: 14328
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric>
#include <set>
#include <cstdlib>
#include <queue>
#include <map>
#include <cstdio>

using namespace std;

typedef long long llint;
typedef long double ld;
#define inf 1e18
#define mod 1000000007
//priority_queue<llint,vector<llint>,greater<llint>> que;
//priority_queue<llint> q;

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

llint a,b;

void solve(){
  cin >> a >> b;
  if(a>b)cout << "a > b";
  else if(a<b)cout << "a < b";
  else cout << "a == b";
  cout << endl;
}

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



Measured memory_bytes: 3116
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <array>
#include <queue>

using namespace std;

int main () {
    string S, T;
    cin>>S>>T;

    if (S.length() < T.length()) {
        cout<<"UNRESTORABLE"<<endl;
        return 0;
    }

    string ans = "UNRESTORABLE";
    for (int i = S.length()-T.length(); i >= 0; i--) {
        bool flag = true;
        for (int j = 0; j < T.length(); j++) {
            if (!(S[i+j] == '?' || S[i+j] == T[j])) {
                flag = false;
                break;
            }
        }
        if (flag) {
            ans = S;
            for (int j = 0; j < S.length(); j++) {
                if (i <= j && j < i + T.length()) ans[j] = T[j-i];
                else if (ans[j] == '?') ans[j] = 'a';
            }
            break;
        }
    }
    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 256
import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as C8

main :: IO ()
main = do
  [n,m,c] <- readInts
  bs <- readInts
  dss <- replicateM n readInts
  print $ length $ filter (>0) $ map (f c bs) dss
 
f :: Int -> [Int] -> [Int] -> Int
f c bs ds = sum (zipWith (*) bs ds) + c

readInts :: IO [Int]
readInts = map (fst . fromJust . C8.readInt) . C8.words <$> C8.getLine

Measured memory_bytes: 636
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
const int maxn = 500500;
int a[maxn], n;
int main() {
    scanf("%d", &n);
    rep(i, n) scanf("%d", &a[i]);
    rep(i, n) if(a[i] & 1)
        { puts("first"); return 0; }
    puts("second");
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll=long long;
using P = pair<int,int>;
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
const ll MOD=1000000007,MOD2=998244353,INF=1e18;



int main() {
    int N;
    cin>>N;
    vector<int> A(N);
    map<int,int> mp;
    rep(i,N){
        cin>>A.at(i);
        mp[A.at(i)]++;
    }
    sort(A.begin(),A.end());
    vector<int> check(A.at(N-1)+1);
    rep(i,N){
        if(!check.at(A.at(i))){
            for(int j=2;j*A.at(i)<=A.at(N-1);j++){
                check.at(j*A.at(i))=1;
            }
        }
    }
    int ans=0;
    rep(i,N){
        if(mp[A.at(i)]==1){
            if(!check.at(A.at(i))){
                ans++;
            }
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 17140
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#define ll long long
using namespace std;
ll gcd(int a,int b);


int main(){
  int n,m;
  scanf ("%d %d",&n,&m);
  string s,t;
  cin >> s;
  cin >> t;
  ll g=gcd(s.size(),t.size());
  ll sa=s.size()/g;
  ll ta=t.size()/g;
  ll i;
  for(i=0;i<g;i++){
    if(s[sa*i]!=t[ta*i]) break;
  }
  if(i==g){
    cout << g*sa*ta << endl;
  }
  else{
    cout << "-1" << endl;
  }
}


ll gcd(int a,int b){
  if(b==0){
    return a;
  }
  else{
    return gcd(b,a%b);
  }
}


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

int main(void) {
    int N, G, sum=0, count=0;
    cin>>N;
    //vector<int> vec_a(D);


    for(int i=0;i<=(int)(N/4);i++){
      for(int j=0;j<=(int)(N/7);j++){
        sum = 4*i + 7*j;
        if(N==sum){
          cout<<"Yes"<<endl;
          return 0;
        }
        sum = 0;
      }

    }
    cout<<"No"<<endl;
     return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  int64_t k;
  cin >> s >> k;
  for(int i=0;i<k;i++){
    if(s[i]!='1'){
      cout << s[i] << endl;
      return 0;
    }
  }
  cout << 1 << endl;
  return 0;
}

Measured memory_bytes: 3620
#include <sys/time.h>

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <utility>
#include <cctype>

using namespace std;

using ll = long long;
using i_i = pair<int, int>;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)
const ll mod = 1000000007LL;


int main() {
	int h, w, m;

	cin >> h >> w >> m;

	set<i_i> ps;

	map<int, int> xs;
	map<int, int> ys;


	rep(i, m) {
		int y, x;
	
		cin >> y >> x;
		ps.insert(mkp(y,x));

		ys[y]++;
		xs[x]++;
		
	}
	int y_max = 0;
	int x_max = 0; 
	for(auto cur : ys) {
		y_max = max(y_max, cur.second);

	}
	for(auto cur : xs) {
		x_max = max(x_max, cur.second);	
	}

	vector<int> xps;
	vector<int> yps;

	for(auto cur : ys) {
		if (cur.second == y_max) {
			yps.push_back(cur.first);
		}
	}
	for(auto cur : xs) {
		if (cur.second == x_max) {
			xps.push_back(cur.first);
		}
	}

	for(int y: yps) {
		for (int x: xps) {
			if (ps.find(mkp(y, x)) == ps.end()) {
				cout << x_max + y_max << endl;				
				return 0;
			}
		}
	}

	cout << x_max + y_max - 1 << endl;

	return 0;
}


Measured memory_bytes: 49144
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define MOD (1000000007)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()

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 gcd(int a, int b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

const ll INF = 1LL << 60;

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

  rep(bit, (1<<3)){
    sum = s[0] - '0';
    ans = s[0];
    rep(i, 3){
      if(bit & (1<<i)){
        sum += s[i+1] - '0';
        ans += '+';
        ans += s[i+1];
      }
      else{
        sum -= s[i+1] - '0';
        ans += '-';
        ans += s[i+1];
      }
    }
    if(sum == 7){
      ans += "=7";
      break;
    }
  }

  std::cout << ans << '\n';
}


Measured memory_bytes: 256
import sys
N,M = map(int,input().split())
students = [None] * N
checkpoints = [None] * M
for student in range(0,N):
    students[student] = list(map(int,input().split()))
for checkpoint in range(0,M):
    checkpoints[checkpoint] = list(map(int,input().split()))

if N < 0 or N > 50 or M < 0 or M > 50:
    sys.exit()
for student in students:
    if student[0] < -100000000 or student[1] > 100000000:
        sys.exit()
for checkpoint in checkpoints:
    if checkpoint[0] < -100000000 or checkpoint[1] > 100000000:
        sys.exit()

distance = [40000000000000000] * N
result = [0] * N
count = 0
for student in students:
    checkpoint_count = 0
    for checkpoint in checkpoints:
        tmp_distance = int(abs(student[0] - checkpoint[0]) + abs(student[1] - checkpoint[1]))
        if int(distance[count]) > int(tmp_distance):
            distance[count] = tmp_distance
            result[count] = checkpoint_count + 1
        checkpoint_count += 1
    count += 1

for I in result:
    print(I)

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

using namespace std;

long long sum(const unordered_map<int, int> &m) {
  long long sum = 0;
  for (auto p: m) {
    sum += (long long)p.first * (long long)p.second;
  }
  return sum;
}

int main() {
  int N, M;
  cin >> N;
  unordered_map<int, int> m;
  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    m[n]++;
  }
  cin >> M;
  long long s = sum(m);
  for (int i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    if (m.find(b) != m.end()) {
      s += (long long)m[b] * (c - b);
      m[c] += m[b];
      m.erase(b);
    }
    cout << s << endl;
  }
  return 0;
}

Measured memory_bytes: 6628
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <numeric>
#include <regex>
#include <tuple>
#include<iomanip>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9
#define LLINF 1LL<<60

vector<vector<int>> G(200009);
int parent[200009]; // parent[i] : i番目のノードの親(1方向)
bool isused[200009];
int cnt[200009];

int value(int cur) {
	if(!isused[cur]){
		int p = parent[cur];
		cnt[cur] += value(p);
		isused[cur] = true;
	}
	return cnt[cur];
}

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

	int N, Q; cin >> N >> Q;
	for (int i = 0; i < N - 1; i++) {
		int a, b; cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	for (int i = 0; i < Q; i++) {
		int p, x; cin >> p >> x;
		cnt[p] += x;
	}

	parent[1] = 0;
	queue<int> que;
	que.push(1); isused[1] = true;
	while (!que.empty()) {
		int cur = que.front(); que.pop();
		for (int i = 0; i < G[cur].size(); i++) {
			int nxt = G[cur][i];
			if (!isused[nxt]) {
				parent[nxt] = cur;
				isused[nxt] = true;
				que.push(nxt);
			}
		}
	}

	for (int i = 1; i <= N; i++) isused[i] = false;
	isused[1] = true;

	for (int i = 1; i <= N; i++) cout << value(i) << " ";
	cout << endl;

	return 0;
}

Measured memory_bytes: 18048
main = do
  str <- getLine
  let [n, k] = map read (words str) ::[Int]
  print $ ck n k


ck :: Int -> Int -> Int
ck n k 
  | n `mod` k == 0                 = 0
  | k - (n `mod` k) <= (n `mod` k) = k - (n `mod` k)
  | otherwise                      = n `mod` k

Measured memory_bytes: 508
#include <iostream>
#include <string>
#include<algorithm>
#include <cstdlib>
#include <cmath>

using namespace std;
int main()
{
	int n, k, x, y,sum=0;
	cin >> n >> k >> x >> y;
	for (int i = 1; i <= n; i++) {
		if (i <= k) sum += x;
		else sum += y;
	}
	cout << sum << endl;
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
int main()
{

  int x, y, r, tmp;
   
  scanf("%d", &x);
    scanf("%d", &y);

  if(x<y){
    tmp = x;
    x = y;
    y = tmp;
  }

 
  r = x % y;
  while(r!=0){
    x = y;
    y = r;
    r = x % y;
  }

   printf("%d\n", y);

  return 0;
}



Measured memory_bytes: 2108
A=int(input())
B=input().split(' ')
B = [int(s) for s in B]

cnt = 0
dag = 0
for i in range(len(B)):
  dag +=B[i]*B[i]
  for j in range(len(B)):
    cnt += B[i]*B[j]

print(int((cnt-dag)/2))

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
    sum += a.at(i);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (4*a.at(i)*M >= sum) {
      ans++;
    }
  }
  if (ans >= M) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
import sys


def solve(N: int, v: "List[int]"):
    v = sorted(v)
    for i in range(N-1):
        v[i+1] = (v[i] + v[i+1]) / 2.0
    print(v[N-1])
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    v = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, v)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
st = []
for _ in range(N):
    st.append(input().rstrip().split())
X = input().rstrip()
s = []
t = []
for i in st:
    s.append(i[0])
    t.append(int(i[1]))
idx = s.index(X)
print(sum(t[idx+1:]))


Measured memory_bytes: 21916
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecd vector<double>
template <typename T>
T lcm(T a, T b)
{
	return a / __gcd(a, b) * b;
}
void comb(vector<vector<long long int>> &v)
{
	for (int i = 0; i < v.size(); i++)
	{
		v[i][0] = 1;
		v[i][i] = 1;
	}
	for (int k = 1; k < v.size(); k++)
	{
		for (int j = 1; j < k; j++)
		{
			v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
		}
	}
}
bool is_not_out_of_range(size_t now, size_t max_num)
{
	return !(now < 0 || max_num <= now);
}
ll repeat_squaring(ll n, ll p, ll m)
{
	if (p == 0)
		return 1;
	if (p % 2 == 0)
	{
		ll t = repeat_squaring(n, p / 2, m);
		return t * t % m;
	}
	return n * repeat_squaring(n, p - 1, m);
}

int main()
{
	ll ans;
	long double a;
	long double b;
	cin >> a >> b;
	ans = floor(a * b);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3780
#include <iostream>
using namespace std;
#include<string.h>
#include<set>
#include<stdlib.h>
#include<map>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include <cstdio>
#include <cstdlib>
#include<stack>
#include<vector>
#include<cstdlib>
const long long MAX=1e5+10;
long long n,a,b;
long long c[10*MAX];
long long d[10*MAX];
int main()
{
    cin>>n>>a>>b;
    if(a+b-1>n||a*b<n)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    for(long long i=1;i<=b;i++)
        c[i]=a;
    long long s=a*b;
    for(long long i=1;i<b;i++)
    {
        if(s-c[i]>=n)
        {
            s-=c[i]-1;
            c[i]=1;
        }
        else
        {
            c[i]-=s-n;
            break;
        }
    }
    long long len=1;
    s=n;
    for(long long i=1;i<=b;i++)
    {
        if(len>n+1)
        {
            cout<<"-1"<<endl;
            return 0;
        }
        s-=c[i];
        long long t=c[i];
        for(long long j=1;j<=t;j++)
            d[len++]=s+j;
    }
    if(s!=0)
    {
        cout<<"-1"<<endl;
        return 0;
    }
   // cout<<len<<endl;
    for(long long i=1;i<=n;i++)
    {
        if(i==1)
            cout<<d[i];
        else
            cout<<" "<<d[i];
    }
    cout<<endl;
    return 0;
}



Measured memory_bytes: 10368
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll arr[200005];

int n;
ll func(int b){
	ll res = 0;
	for(int i = 0; i < n; i++){
		res+=abs(arr[i]-(b+i));
	}
	return res;
}

int main(){
	
	cin>>n;
	for(int i = 0; i < n; i++) cin>>arr[i];

	vector<ll> B(n);
	for(int i = 0; i < n; i++) B[i] = arr[i] - i;
	sort(B.begin(),B.end());
	
	ll x = B[n/2];
	cout<<func(x);

}

Measured memory_bytes: 3328
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

int main(){
    string s;
    stack<char> sta;
    cin >> s;
    rep(i,s.size()){
        if(i == 0) sta.push(s[i]);
        else if(s[i] != sta.top()){
            sta.push(s[i]);
        }
    }
    cout << sta.size() - 1 << endl;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
#define inp(X) cin >> X
#define out(X) cout << X << endl
#define out16(X) cout << setprecision(16) << X << endl
#define rep(i,n) for(int i=0;i<n;i++ )
#define rep2(i,f,n) for(int i=f;i<n;i++ )
#define MAX(A) *max_element(A.begin(),A.end())
#define MIN(A) *min_element(A.begin(),A.end())
#define SORT(A) sort(A.begin(),A.end())
#define REV(A) reverse(A.begin(),A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vd = std::vector<double>;
using vvd = std::vector<std::vector<double>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using namespace std;
#define MOD 1000000007
#define INF 1000000001
#define LL_INF 1001001001001001001

int main(void) {
	int n,m; cin >> n >> m;
	if(m==n) out("Yes");
	else out("No");
}




Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
class Solve{
public:
    int N;
    ll X;
    vector<int> m;
    void input(){
        cin >> N >> X;
        m.resize(N);
        rep(i,0,N) cin >> m[i];
    }
    void solve(){
        input();
        int ans = N;
        ll rest = X;
        sort(all(m));
        rest -= accumulate(all(m),0ll);

        ans += rest / m[0];
        cout << ans << endl;
    }
};


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

    Solve().solve();


    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
struct edge{
	int y,nxt,v;
}e[2000010];
set<int>c[2000010];
int n,m,head[2000010],dis[2000010],vis[2000010],tot;
void add(int x,int y,int v){
	e[++tot]=edge{y,head[x],v};
	head[x]=tot;
}
void dijkstra(){
	priority_queue<pair<int,int> >q;
	memset(dis,0x3f3f3f3f,sizeof(dis));
	dis[1]=0; q.push(make_pair(0,1));
	while(q.size()){
		int x=q.top().second; q.pop();
		if(vis[x]) continue; vis[x]=1;
//		cout<<x<<':'<<dis[x]<<endl;
		for(int i=head[x];i;i=e[i].nxt){
			int y=e[i].y,v=c[x].count(e[i].v)?0:1;
//			cout<<"y = "<<y<<endl;
			if(dis[x]+v<dis[y]){
				dis[y]=dis[x]+v;
				q.push(make_pair(-dis[y],y));
				c[y].clear(); c[y].insert(e[i].v);
			}
			else if(dis[x]+v==dis[y]) c[y].insert(e[i].v);
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int x,y,v;
		scanf("%d%d%d",&x,&y,&v);
		add(x,y,v); add(y,x,v);
	}
	dijkstra();
//	cout<<0x3f3f3f3f3f<<endl;
	if(dis[n]==0x3f3f3f3f) printf("-1");
	else printf("%d",dis[n]);
	return 0;
}

Measured memory_bytes: 120952
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
#define loop(i, l, r) for(i=l; i<r; i++)/*Instead of function, (for).*/
#define rep(i,r) for(i=0; i<r; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long int lli;
/*Return Combination ,(nCr)*/
int combi(int n, int r)
{
	if(r==0 || r==n) return 1;
	else return combi(n-1,r) + combi(n-1,r-1);
}
/*Return gcd(a, b)*/
int gcd(int a, int b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}
//qsort�֐��Ɏg��compare�֐�,(long long int, int, double�^)
int compare_lli(const void *a, const void *b)
{
    return *(lli*)a - *(lli*)b;
}
int compare_int(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
}
int compare_double(const void *a, const void *b)
{
    return *(double*)a - *(double*)b;
}
/*qsort(data, 10, sizeof(int_lli_double), compare_int);*/
/*qsort(�z��, �v�f��, sizeof(�z��̌^), compare_�z��̌^);*/
int main(void)
{
	lli n, i;
	scanf("%lld", &n);
	lli h[n];

	rep(i,n) scanf("%lld", &h[i]);
	for(i=n-1; i>=1; i--){
		if(h[i-1] - h[i] >= 2){
				puts("No");
				return 0;
		}else if(h[i-1]-h[i] <= 0){
			;
		}else{
			h[i-1]--;
		}
	}
	rep(i,n-1){
		if(h[i]>h[i+1]){
			puts("No");
			return 0;
		}
	}
	puts("Yes");
	return 0;
}

Measured memory_bytes: 896
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main{
  
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    PrintWriter w = new PrintWriter(System.out);
    long n=in.nextLong(),m=in.nextLong();
    long c=0;
    c=Math.min(m/2,n);
    m=m-c*2;
    if (m>0){
      c=c+m/4;
    }
    w.println(c);
    w.close();
    }

  static class InputReader {

    private final InputStream stream;
    private final byte[] buf = new byte[8192];
    private int curChar, snumChars;
    private SpaceCharFilter filter;

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

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

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

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

    public int[] nextIntArray(int n) {
      int a[] = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = nextInt();
      }
      return a;
    }

    public String readString() {
      int c = snext();
      while (isSpaceChar(c)) {
        c = snext();
      }
      StringBuilder res = new StringBuilder();
      do {
        res.appendCodePoint(c);
        c = snext();
      } while (!isSpaceChar(c));
      return res.toString();
    }

    public String nextLine() {
      int c = snext();
      while (isSpaceChar(c))
      c = snext();
      StringBuilder res = new StringBuilder();
      do {
        res.appendCodePoint(c);
        c = snext();
      } while (!isEndOfLine(c));
      return res.toString();
    }

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

    private boolean isEndOfLine(int c) {
      return c == '\n' || c == '\r' || c == -1;
    }

    public interface SpaceCharFilter {
      public boolean isSpaceChar(int ch);
    }
  }
}


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

#define pb push_back
#define MAX 100005
#define ff first
#define ss second
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

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

int n;
ll dp[405][405], a[405];
ll go(int l, int r){
    if(dp[l][r])
        return dp[l][r];
    if(l==r)
        return dp[l][r] = 0;
    ll s = 0;
    for(int i = l; i<=r; ++i)
        s+=a[i];
    ll mn = 1e17+5;
    for(int i = l; i<r; ++i){
        mn = min(mn,go(l,i)+go(i+1,r));
    }
    //error(l,r,mn); 
    return dp[l][r] = mn+s;

}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    for(int i = 0; i<n; ++i ){
        cin>>a[i];
    }
    cout<<go(0,n-1)<<"\n";
}

Measured memory_bytes: 4964
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().solve();
	}

	private void solve() throws IOException {
		try {
			solveA();
			// solveB();
			// solveC();
			// solveD();
			// solveE();
			// solveF();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.flush();
				out.close();
			}
		}

	}

	private void solveA() {
		int numA = nextInt();
		int numB = nextInt();
		int numC = nextInt();

		out.println(numC >= numA && numC <= numB ? "Yes" : "No");

	}

	private void solveB() {
		int numN = nextInt();

		out.println("");
	}

	private void solveC() {
		int numN = nextInt();

		out.println("");
	}

	private void solveD() {
		int numN = nextInt();

		out.println("");
	}

	private void solveE() {
		int numN = nextInt();

		out.println("");
	}

	private void solveF() {
		int numN = nextInt();

		out.println("");
	}

	private final PrintWriter out = new PrintWriter(System.out);
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	private void skipUnprintable() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
	}

	public boolean hasNext() {
		skipUnprintable();
		return hasNextByte();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
}

Measured memory_bytes: 20692
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int max = 0;
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < N; i++) {
			int input = sc.nextInt();
			if (input > max)
				max = input;
			if (input < min)
				min = input;

		}
		System.out.println(max - min);
		sc.close();
	}
}

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

using ll = long long;
constexpr ll inf = static_cast<ll>(1e17);
constexpr ll mod = static_cast<ll>(1e9 + 7);

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

	int one = 0, two = 0;
	int size = s.size();
	for (size_t i = 0, end = s.size(); i < end; ++i)
		if (i % 2 == 0) {
			if (s[i] == '0')
				++one;
		}
		else {
			if (s[i] == '0')
				++two;
		}

	int ans = std::abs((size + 2 - 1) / 2 - one) + std::abs(two);
	ans = std::min(ans, std::abs(one) + std::abs(size / 2 - two));

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


Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9+7)

int main(){
    string s;
    cin >> s;
    if(s.size()==3){
        cout << s[2] << s[1] << s[0] << endl;
    }else{
        cout << s << endl;
    }

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <random>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <sstream>
#include <bitset>
#include <cassert>
#include <fstream>
#include <queue>

#define len(X) ((int)(X).size())

#ifdef __LOCAL
	#define DBG(X) cout << #X << "=" << (X) << '\n';
	#define SAY(X) cout << (X) << '\n';
#else
	#define DBG(X)
	#define SAY(X)
#endif
 
using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld  = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INT_INF = (int)(2e9);
const ll  LL_INF  = (ll)(2e18);

const int NIL = -1;
static mt19937 _g(time(nullptr));

inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); }
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for(auto el: v) cout << el << " "; return os; }
template<typename T> inline T fetch() { T ret; cin >> ret; return ret; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for(auto& elem: ret) cin >> elem; return ret; }

const int MAXN = (int)1e5 + 777;
int a[MAXN], pos[MAXN], n;
set<int> setik;
ll answ = 0;

void solve() {
	cin >> n;

	for(int i = 0; i < n; ++i) {
		int val = fetch<int>() - 1;

		a[i] = val;
		pos[val] = i;
	}

	for(int val = n - 1; val >= 0; --val) {
		int i = pos[val];
		auto iter = setik.upper_bound(i);
		int L = -1, R = n;
		if(iter != setik.end()) {
			R = *iter;
		}

		if(iter != setik.begin()) {
			L = *prev(iter);
		}

		int L2 = -1, R2 = n;
		auto iterR = setik.upper_bound(R);

		if(iterR != setik.end()) {
			R2 = *iterR;
		}

		auto iterL = setik.lower_bound(L);

		if(iterL != setik.begin()) {
			L2 = *(prev(iterL));
		}

		DBG(val + 1); DBG(L); DBG(R); DBG(L2); DBG(R2);
		ll cnt = (R2 - R) * 1LL * (i - L) + (R - i) * 1LL * (L - L2);
		answ += (val + 1) * 1LL * cnt;
		DBG(cnt);
		SAY("-----------");

		setik.insert(i);
	}

	cout << answ << '\n';
}

int main() {
	fast_io();
	solve();

	return 0;
}


Measured memory_bytes: 5760
N = int(input())
A = []
B = []
for _ in range(N):
    a,b = [int(x) for x in input().split()]
    A.append(a)
    B.append(b)

add = 0
for i in range(1,N+1):
    x = (A[-i]+add)%B[-i]
    if(x!=0):
        add += B[-i]-x
print(add)


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

int main() {
	// your code goes here
	int H = 0;
	int W = 0;
	int h = 0;
	int w = 0;
	cin>>H>>W;
	cin>>h>>w;
	int res = (H-h)*(W-w);
	cout<<res;
	return 0;
}


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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String line, str1;
		int m,h;
		for(;;) {
			line = br.readLine();
			if(line.equals("-")) break;
			m = Integer.parseInt(br.readLine());
			for(int i = 0; i < m; i++) {
				h = Integer.parseInt(br.readLine());
				str1 = line.substring(0, h);
				line = line.substring(h, line.length()) + str1;
			}
			sb.append(line).append("\n");
		}
		System.out.print(sb);
	}
}

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

int main()
{
	int x, y;
	int t;

	for (;;) {
		scanf("%d%d", &x, &y);
		if (x == 0 && y == 0) break;
		if (x > y) t = x, x = y, y = t;
		printf("%d %d\n", x, y);
	}
	return 0;
}



Measured memory_bytes: 2032
#include <stdio.h>
#define M 50
int X[M];long long Z[M+1][M*M+1];
int main(){
	int N,A,S=0,i,j,k;
	long long R=0;
	scanf("%d%d",&N,&A);
	for(i=0;i<N;i++)scanf("%d",X+i);
	Z[0][0]=1;
	for(i=0;i<N;i++){
		S+=X[i];
		for(k=i;k>=0;k--)for(j=S;j>=X[i];j--)Z[k+1][j]+=Z[k][j-X[i]];
	}
	for(i=1;i<=N;i++)R+=Z[i][i*A];
	printf("%lld\n",R);
}

Measured memory_bytes: 1152
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007

int main() {
    int n;
    long long int ans;
    string s, s2, S;
    cin >> n >> s >> s2;

    if(n == 1) {
        cout << 3 << '\n';
        return 0;
    }

    for(int i = 0; i < n; ++i) {
        if(s[i] != s[i+1]) {
            S += 'X';
        } else {
            S += 'Y';
            i++;
        }
    }
    if(S[0] == 'X') ans = 3;
    else ans = 6;
    for(int i = 1; i < S.size(); ++i) {
        if(S[i-1] == 'X') {
            if(S[i] == 'X') {
                ans *= 2;
            } else {
                ans *= 2;
            }
        } else {
            if(S[i] == 'X') {
                ans *= 1;
            } else {
                ans *= 3;
            }
        }
        ans %= MOD;
    }

    cout << ans << '\n';

    return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
constexpr ll inf = 1e18;

int main() {
    ll N, M, L; cin >> N >> M >> L;
    vector<vector<ll>> d(N, vector<ll>(N, inf));
    for(int i = 0; i < N; ++i) {
        d[i][i] = 0;
    }
    for(int i = 0; i < M; ++i) {
        ll a, b, c; cin >> a >> b >> c;
        a--, b--;
        d[a][b] = d[b][a] = c;
    }
    for(int k = 0; k < N; ++k) {
        for(int i = 0; i < N; ++i) {
            for(int j = 0; j < N; ++j) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    vector<vector<ll>> ans(N, vector<ll>(N, inf));
    for(int s = 0; s < N; ++s) {
        queue<int> que;
        ans[s][s] = 0;
        que.push(s);
        while(!que.empty()) {
            const int v = que.front(); que.pop();
            for(int to = 0; to < N; ++to) {
                if(ans[s][to] != inf || d[v][to] > L) continue;
                ans[s][to] = ans[s][v] + 1;
                que.push(to);
            }
        }
    }

    int Q; cin >> Q;
    while(Q--) {
        int s, t; cin >> s >> t;
        s--, t--;
        if(ans[s][t] == inf) {
            cout << -1 << endl;
        } else {
            cout << ans[s][t] - 1 << endl;
        }
    }
}


Measured memory_bytes: 1920
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
    int n;
    cin >> n;
    int a[n];
    rep(i, 0, n) cin >> a[i];
    sort(a, a + n);
    int cnt = 0;
    int now = a[0];
    rep(i, 1, n) {
        if (now * 2 >= a[i]) {
            cnt++;
        } else {
            cnt = 0;
        }
        now += a[i];
    }
    cout << ++cnt << "\n";
}

Measured memory_bytes: 4376
N = int(input())
A = list(map(int,input().split()))
i = 0
ret = 1
while i < N-1:
  while i < N-1 and A[i] == A[i+1]:
  	i += 1
  if A[i] < A[i+1]:
    while i < N-1 and A[i] <= A[i+1]:
      i += 1
    if i < N-1:
      ret += 1
      i += 1
  elif A[i] > A[i+1]:
    while i < N-1 and A[i] >= A[i+1]:
      i += 1
    if i < N-1:
      ret += 1
      i += 1
print(ret)

Measured memory_bytes: 14252