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

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
#define int long long
//for set
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;   
// for multiset
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_multiset;


#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
// #define PI 3.1415926535897932384626433832795
#define PI acos(-1.0)
int MOD=1e9+7;
int powmod(int a,int l, int md){int res=1;while(l){if(l&1)res=res*a%md;l/=2;a=a*a%md;}return res;}
typedef long long ll; typedef vector<ll> vi; typedef pair<ll, ll> ii; typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) {return b|(1<<i);} //set ith bit
int __unset(int b, int i) {return b&!(1<<i);}
int __check(int b, int i) {return b&(1<<i);} //returns 0 if ith bit is 0
int mul(int a, int b) {return ((a%MOD)*(b%MOD))%MOD;}
int add(int a, int b) {return (a+b)%MOD;}
int sub(int a, int b) {return (((a-b)%MOD)+MOD)%MOD;}
int __div(int a, int b) {return mul(a, powmod(b, MOD-2, MOD));} //if MOD is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL; //very large number
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq; //for min priority_queue
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t time_p=clock();
void time()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

bool cmp(ii a, ii b) {
	return (atan2((double)a.ss, (double)a.ff)-atan2((double)b.ss, (double)b.ff))<0;
}
int32_t main(void)
{
	IOS;
	int n; cin>>n;
	vii a;
	int ind=0;
	for(int i = 0; i < n; i++) {
		int x, y; cin>>x>>y;
		if(!x && !y) continue;
		a.pb({x,y}); ind++;
	}
	n=a.size();
	if(n==0) {
		cout<<0<<endl;
		return 0;
	}
	sort(all(a), cmp);
	int ans=0;
	for(int i = 0; i < n; i++) {
		ii tmp={0,0};
		for(int j = 1; j <= n; j++) {
			tmp={tmp.ff+a[(i+j-1)%n].ff, tmp.ss+a[(i+j-1)%n].ss};
			ans=max(ans, tmp.ff*tmp.ff+tmp.ss*tmp.ss);
		}
	}
	// ii pre[n];
	// pre[0]={a[0].ff,a[0].ss};
	// for(int i = 1; i < n; i++) {
	// 	pre[i]={pre[i-1].ff+a[i].ff, pre[i-1].ss+a[i].ss};
	// }
	// ii dp[n][n];
	// for(int i = 1; i <= n; i++) {
	// 	for(int j = 0; j <= n-i; j++) {
	// 		if(j==0) {
	// 			dp[j][j+i-1]=pre[j+i-1];
	// 		} else {
	// 			dp[j][j+i-1]={pre[j+i-1].ff-pre[j-1].ff, pre[j+i-1].ss-pre[j-1].ss};
	// 		}
	// 	}
	// }
	// int ans=0;
	// for(int i = 1; i <= n; i++) {
	// 	for(int j = 0; j <= n-i; j++) {
	// 		ans=max(ans,dp[j][j+i-1].ff*dp[j][j+i-1].ff+dp[j][j+i-1].ss*dp[j][j+i-1].ss);
	// 	}
	// }
	cout<<setprecision(40)<<sqrtl(ans)<<endl;
	time();
	return 0;
}

Measured memory_bytes: 256
A, B, M = map(int, input().split())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))
mList = [list(map(int, input().split())) for i in range(M)]
Min = 20001

Min = min(aList) + min(bList)
for i in range(M):
    cost = aList[mList[i][0] -1] + bList[mList[i][1] -1] - mList[i][2]
    Min = min(Min, cost)

print(Min)

Measured memory_bytes: 38444
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cstdint>
#include <cassert>
#include <random>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0) 
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

struct segtree {
    int N;
    vector<i64> dat, sum;
    segtree(int n) {
        N = 1;
        while(N < n) N <<= 1;
        dat.assign(2*N-1,1ll << 60);
        sum.assign(2*N-1,1ll << 60);
    }
    void add(int a, int b, i64 x) { add(a,b,x,0,0,N); }
    i64 add(int a, int b, i64 x, int k, int l, int r) {
        if(b <= l or r <= a) return dat[k];
        if(a <= l and r <= b) {
            sum[k] = min(sum[k], x);
            return dat[k] = min(dat[k], x);
        }
        int m = (l+r)/2;
        return dat[k] = min(min(add(a,b,x,2*k+1,l,m),add(a,b,x,2*k+2,m,r)),sum[k]);
    }
    i64 minimum(int a, int b) { return minimum(a,b,0,0,N); }
    i64 minimum(int a, int b, int k, int l, int r) {
        if(b <= l or r <= a) return 1ll << 60;
        if(a <= l and r <= b) return dat[k];
        int m = (l+r)/2;
        return min(min(minimum(a,b,2*k+1,l,m),minimum(a,b,2*k+2,m,r)),sum[k]);
    }
};


struct S {
  i64 l, r, c;
};

i64 n, m;
vector<S> ss;
vector<i64> dp;
int main() {
  cin >> n >> m;
  ss.resize(m);
  for (int i = 0; i < m; ++i) {
    cin >> ss[i].l >> ss[i].r >> ss[i].c;
  }

  sort(begin(ss), end(ss), [](auto& s, auto& t) {
    if (s.r == t.r) return s.l < t.l;
    return s.r < t.r;
  });

  segtree st(n + 1);
  st.add(1, 2, 0);

  for (auto s : ss) {
    i64 x = st.minimum(s.l, s.r + 1);
    st.add(s.l, s.r + 1, x + s.c);
  }

  i64 ans =  st.minimum(n, n + 1);
  if (ans >= (1ll << 60)) ans = -1;
  cout << ans << endl;
  return 0;
}

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

int main() {
  string S;
  cin >> S;
  
  if (S[0] == S[1] || S[1] == S[2] || S[2] == S[3]) {
    cout << "Bad" << endl;
  } else {
    cout << "Good" << endl;
  }

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define INF 2000000000
#define MOD 1000000007
#define EPS (1e-10)

using namespace std;

int main(int argc, char *argv[]) {
	
	int N; cin >> N;

	vector<int> A(N,0);
	vector<int> B(N,0);
	vector<int> C(N-1,0);

	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> B[i];
	}
	for (int i = 0; i < N-1; i++) {
		cin >> C[i];
	}

	int old = -100;
	int ans = 0;
	for (int i = 0; i < N; i++) {
		ans += B[A[i]-1];
		if (A[i] - old == 1) {
			ans += C[old-1];
		}
		old = A[i];
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 3600
a,b,c,d,e,f = map(int,input().split())
a *= 100
b *= 100

w = []
i = -1
while True:
    i += 1
    if a*i > f:
        break
    j = -1
    while True:
        j += 1
        if a*i+b*j > f:
            break
        w.append(a*i+b*j)
w = list(set(w))

s = []
i = -1
while True:
    i += 1
    if c*i > f:
        break
    j = -1
    while True:
        j += 1
        if c*i+d*j > f:
            break
        s.append(c*i+d*j)
s = list(set(s))

ans = [0, 0, 0]
for y in w:
    if y == 0:
        continue
    for x in s:
        if x+y <= f and e*y//100 >= x:
            ans = max(ans, [x/y, x+y, x])

txt = str(ans[1]) + " " + str(ans[2])
print(txt)


Measured memory_bytes: 12456
cnt = int(input())
ans = 'Yes'
pw = input()
already = set()
already.add(pw)
for i in range(1, cnt):
    nw = input()
    if pw[-1] != nw[0] or nw in already:
        ans = 'No'
        break
    else:
        pw = nw
        already.add(pw)
print(ans)

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

using namespace std;
//DP用
template<class T> inline bool chmin(T& a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//a^n mod を計算する関数
long long modpow(long long a,long long n,long long mod) {
    long long res = 1;
    while(n > 0) {
        if(n % 2 == 1) {
            res = res * a % mod;
        }
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

//a^(-1) mod すなわちaの逆元を返す関数
long long modinv(long long a,long long mod) {
    return modpow(a,mod-2,mod);
}

//最大公約数をを返す関数
long long gcd (long long a,long long b) {
    if(a % b == 0) {
        return b;
    }else {
        return (gcd(b,a%b));
    }
}

const long long INF = 1LL << 60;
const long long P = 1000000007;

//nCkを求める関数
long long conbi (long long n,long long k) {
    long long res = 1;
    long long inv;
    for(long long i = n-k+1; i <= n; i++) {
        res *= i;
        res %= P;
    }
    for(long long i = 1; i <= k; i++) {
        inv = modinv(i,P);
        res *= inv;
        res %= P;
    }
    return res;
}

int main() {
    int n;
    string s;
    cin >> n >> s;
    int sumE[n],sumW[n],sum[n];
    if(s[0] == 'W') {
        sumE[0] = 0;
        sumW[0] = 1;
    }else {
        sumE[0] = 1;
        sumW[0] = 0;
    }
    for(int i = 1; i < n; i++) {
        if(s[i] == 'E') {
            sumE[i] = sumE[i-1] + 1;
        }else {
            sumE[i] = sumE[i-1];
        }
    }
    for(int i = 1; i < n; i++) {
        if(s[i] == 'W') {
            sumW[i] = sumW[i-1] + 1;
        }else {
            sumW[i] = sumW[i-1];
        }
    }
    for(int i = 0; i < n; i++) {
        sum[i] = sumW[i]+(sumE[n-1] - sumE[i]);
    }
    sort(sum,sum+n);
    int possibility = min(sumE[n-1],sum[n-1]);
    cout << min(sum[0],possibility);
    return 0;
}



Measured memory_bytes: 4228
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int n,k,arr[101],mi=10000000;
int dp[100010];
int dfs(int u){
	if(u<mi)return 0;
	int &res=dp[u];
	if(~res)return res;
	for(int i=0;i<n;i++){
		if(u>=arr[i] && !dfs(u-arr[i]))return res=1;
	}
	return res=0;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	memset(dp,-1,sizeof dp);
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>arr[i];
		mi=min(arr[i],mi);
	}

	cout<<(dfs(k)?"First":"Second")<<endl;


	return 0;
}

Measured memory_bytes: 3712
N = int(input())

t1, x1, y1 = 0, 0, 0
flg = True

for i in range(N):
    t2, x2, y2 = map(int, input().split())
    d = abs(x1-x2) + abs(y1-y2)
    dt = t2 - t1
    if d > dt or (dt - d)%2 == 1:
        flg = False
        break
    t1, x1, y1 = t2, x2, y2

if flg:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3064
#include <stdio.h>

int main(){
    int i, n;
    int a[1000];
    scanf("%d", &n);
    for(i=0;i<n;i++){
        scanf(" %d", &a[i]);
    }
    for(i=0;i<n;i++){
        if(i)printf(" ");
        printf("%d", a[n-i-1]);
    }
    printf("\n");
    return 0;
}


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

int main(){
	int n,k;
	string s;
	cin >> n >> k >> s;
	if(s.at(k-1) == 'A') s.at(k-1) = 'a';
	if(s.at(k-1) == 'B') s.at(k-1) = 'b';
	if(s.at(k-1) == 'C') s.at(k-1) = 'c';

	cout << s << endl;
}

Measured memory_bytes: 256
#[allow(unused_imports)]
use std::cmp::{max, min};
use std::io::{stdin, stdout, BufWriter, Write};

fn main() {
    let stdin_obj = stdin();
    let mut scan = Scanner { stdin: stdin_obj.lock() };
    let out = &mut BufWriter::new(stdout());

    let n: usize = scan.read();
    let k: i64 = scan.read();
    let mut a: Vec<i64> = scan.vec(n);
    a.sort();
    let f: Vec<i64> = scan.vec(n);

    let mut l: i64 = 0;
    let mut r: i64 = 1e12 as i64;

    while l < r {
        let mid: i64 = (l + r) >> 1;
        let mut b: Vec<i64> = Vec::new();
        for i in 0..n {
            b.push(mid / f[i]);
        }
        b.sort();
        let mut req: i64 = 0;
        for i in 0..n {
            req += max(a[i] - b[i], 0);
        }
        if req > k {
            l = mid + 1;
        } else {
            r = mid;
        }
    }
    writeln!(out, "{}", l).ok();
}

#[derive(Default)]
pub struct Scanner<R> {
    stdin: R,
}
impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn read_line(&mut self) -> String {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b'\n' || b == b'\r')
            .take_while(|&b| b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}


Measured memory_bytes: 12540
N = int(input())
H = list(map(int, input().split()))

minH_right = [0] * N
min_tmp = 10 ** 18
for i in range(N):
    if min_tmp > H[N - 1 - i]:
        min_tmp = H[N - 1 - i]
    minH_right[N - 1 - i] = min_tmp

ans = 'No'
# print("#", minH_right)
for i in range(N - 1):
    if H[i] > minH_right[i + 1] + 1:
        break
    else:
        continue
else:
    ans = 'Yes'

print(ans)

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

const int MAX_N = 110000;

int dp[MAX_N]; // dp[i] が i についての答え

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

	// 初期化
	for (int i = 0; i < MAX_N; ++i) dp[i] = N; // INF の気持ち
	dp[0] = 0;

	// 集める DP --- dp[n] に遷移を集める
	for (int n = 1; n <= N; ++n) {
		for (int pow6 = 1; pow6 <= n; pow6 *= 6) {
			dp[n] = min(dp[n], dp[n - pow6] + 1);
		}
		for (int pow9 = 1; pow9 <= n; pow9 *= 9) {
			dp[n] = min(dp[n], dp[n - pow9] + 1);
		}
	}

	cout << dp[N] << endl;
}

Measured memory_bytes: 640
def f(n):
    if n % 2 == 0:
        return n // 2
    else:
        return 3 * n + 1

def main(s):
    temp = []
    count = 0
    ans = 0
    for i in range(s, 100000000):
        if i == s:
            a = i
        else:
            a = f(temp[count-1])
        
        temp.append(a)
        count += 1

        if a == 4 or a == 2 or a == 1:
            ans = count + 3
            break
    # print(temp)
    print(ans)


if __name__ == "__main__":
    s = int(input())

    main(s)


Measured memory_bytes: 9052
/******************************\
*  Everyone has a different way of thinking, so God Created us
Hope You Respect My Way..,Thank You ):  *
*                            *
*   Created by : Murad   *
\******************************/
//std::transform(s1.begin(), s1.end(), s1.begin(),::tolower);
#include<bits/stdc++.h>
using namespace std;
#define pi 3.1415926536
#define forn(i,a,b)for(int i=a;i<b;i++)
#define LL long long
#define MP make_pair
#define fi first
#define se second
#define INF 2147483647
#define _INF -2147483647
#define pq priority_queue
#define MM multimap
#define PB push_back
#define ipair pair<int,int>
#define lpair pair<LL,LL>
#define MAXN 100004
#define El3zba ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define modd 1000000007
LL MMIN(LL a, LL b)
{
	if (a <= b)
		return a;
	else
		return b;
}
LL MMAX(LL a, LL b) {
	if (a >= b)
		return a;
	else
		return b;
}
int main()
{
	El3zba;
	int k;
	vector<int>a(5);
	for (int i = 0; i < 5; i++)cin >> a[i];
	sort(a.begin(), a.end());
	cin >> k;
	if (a[4] - a[0] <= k)cout << "Yay!\n"; else cout << ":(\n";
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
	int N,K,a,ans=0;
	cin >> N >> K;
	map<int,int> ma;
	for(int i=0;i<N;i++){
		cin>>a;
		ma[a]++;
	}
	priority_queue<int> Q;
	for(auto x:ma)
		Q.push(-x.second);
	while(Q.size()>K){
		ans -= Q.top();
		Q.pop();
	}
	cout << ans << endl;
}

Measured memory_bytes: 10748
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

int ufp[334334],ufr[334334];

void ufinit(int n){
	int i;
	for(i=0; i<n; ++i){
		ufp[i] = i;
		ufr[i] = 1ll;
	}
}

int ufind(int x){
	if(x == ufp[x]){
		return x;
	}
	ufp[x] = ufind(ufp[x]);
	return ufp[x];
}

bool unionf(int x, int y){
	x = ufind(x);
	y = ufind(y);
	if(x==y){
		return false;
	}
	if(ufr[x] < ufr[y]){
		swap(x,y);
	}
	ufp[y] = x;
	ufr[x] += ufr[y];
	return true;
}

int main(void){
	int n,m,i,u,v;
	ll ans,p,q=0ll,r=0ll;
	cin >> n >> m;
	p = n;
	ans = p*p;
	ufinit(n*3);
	for(i=0; i<m; ++i){
		cin >> u >> v;
		--u; --v;
		unionf(u,v);
		unionf(u+n,v+n*2);
		unionf(u+n*2,v+n);
	}
	for(i=0; i<n; ++i){
		if(ufind(i)!=i){
			continue;
		}
		if(ufr[i] == 1){
			--p;
		}else if(ufind(i+n)==ufind(i+n*2)){
			++q;
		}else{
			++r;
		}
	}
	ans -= p*p;
	ans += (q+r)*(q+r)+r*r;
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 2560
//
/*==========================================================*/
/*    Template ver 2017-0720    |    Created by JollyBee    */
/* DOMINUS pascit me, et nihil mihi deerit (Psalmorum 23:1) */
/*==========================================================*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
const double EPS=(double)1e-9;
const double PI=(double)acos(-1.0);
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define FOR(i,n) for(int i=0;i<n;i++)
#define REPP(i,l,r,c) for(int i=l;i<=r;i+=c)
#define REP(i,l,r) REPP(i,l,r,1)
#define FORD(i,n) for(int i=n-1;i>=0;i--)
#define REVV(i,l,r,c) for(int i=l;i>=r;i-=c)
#define REV(i,l,r) REVV(i,l,r,1)
//random
int irand(int lo, int hi){
    return (((double)rand())/(RAND_MAX+1.0)) * (hi-lo+1) + lo;
}
//ll to string
string toString(ll x) {
    stringstream ss;
    ss << x;
    return ss.str();
}
//string to ll
ll toNumber(string S) {
    ll ret;
    sscanf(S.c_str(),"%lld",&ret);
    return ret;
}
// std::fill(start, end, value);
// for(auto it: DS){}

const int INF=(int)2e9;
const ll MOD=(ll)1e9+7;
/*==========================================================*/
/*                     END OF TEMPLATE                      */
/* DOMINUS pascit me, et nihil mihi deerit (Psalmorum 23:1) */
/*==========================================================*/

int main(){
    char s[105]; scanf("%s", s);
    int res = 1;
    for (int i=0; s[i]; i++) {
        if (s[i] == 'L') res &= (i%2 == 1);
        if (s[i] == 'R') res &= (i%2 == 0);
    }
    puts(res ? "Yes" : "No");
    return 0;
}


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

using namespace std;

const int N = 2e5+7;
const long long INF = 1e18;

int x, d, n;
int a[N];
long long res;
vector<pair<long long, long long>> v;

inline int mod(int i) { return static_cast<int>(1ll*i*x%d); }
inline long long sum(int l, int r) { return l <= r ? 1ll*(l+r)*(r-l+1)/2 : 0; }
inline bool cmp (const int i, const int j) { return mod(i) < mod(j); }

inline void calc()
{
    if (!v.size()) return;
    sort(v.begin(), v.end());
    long long last = -INF;
    for (auto p : v) {
        long long l = p.first, r = p.second;
        res += max(0ll, r-max(l-1, last));
        last = max(last, r);
    }
}

int main()
{
    scanf("%d %d %d", &n, &x, &d);
    if (d == 0) {
        if (x == 0) return puts("1")&0;
        return printf("%d\n", n+1)&0;
    }
    if (d < 0) d = -d, x = -x;
    for (int i = 0; i <= n; ++i) a[i] = i;
    sort(a, a+n+1, cmp);
    long long xd, l, r;
    for (int i = 0; i <= n; ++i) {
        xd = 1ll*a[i]*x/d;
        l = xd+sum(0, a[i]-1);
        r = xd+sum(n-a[i], n-1);
        v.emplace_back(l, r);
        if (i == n || mod(a[i]) != mod(a[i+1])) {
            calc();
            v.clear();
        }
    }
    calc();
    return printf("%lld\n", res)&0;
}

Measured memory_bytes: 6768
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include<string>
#include<cstring>
#include<complex>
#include<cmath>
using namespace std;
#define rep(i, n)   for(int i = 0; i < (int)(n); i++)
#define rrp(i, n)   for(int i = ((int)(n)); i >=0; i--)
#define mpp(i,j,k)  make_pair((int)(i),make_pair((int)(j),(int)(k)))
typedef pair<int,pair<int,int > > ppp;
typedef pair<int,int> pp;
typedef vector<int> vi;
typedef vector<vi>  vvi;

string s;



int main(){
 cin>>s;
 int le=s.size();
 if((s[0]==s[le-1]) ^ le%2)cout<<"First"<<endl;
 else cout<<"Second"<<endl; 
  return 0;}




Measured memory_bytes: 512
#include<iostream>
#include<algorithm>
using namespace std;
int dp[45][405][405], n, p, q, a[100], b[100], c[100];
int main() {
	cin >> n >> p >> q;
	for (int i = 0; i < n; i++)cin >> a[i] >> b[i] >> c[i];
	for (int i = 0; i < 45; i++) { for (int j = 0; j < 405; j++) { for (int k = 0; k < 405; k++)dp[i][j][k] = (1 << 30); } }
	dp[0][0][0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= 400; j++) {
			for (int k = 0; k <= 400; k++) {
				if (dp[i][j][k] == (1 << 30))continue;
				dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
				dp[i + 1][j + a[i]][k + b[i]] = min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
			}
		}
	}
	int minx = (1 << 30);
	for (int i = 1; i <= 400; i++) {
		for (int j = 1; j <= 400; j++) {
			if (i*q - j*p == 0)minx = min(minx, dp[n][i][j]);
		}
	}
	if (minx == (1 << 30))minx = -1;
	cout << minx << endl;
	return 0;
}

Measured memory_bytes: 29184
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int answer = 0;

		if ((A >= 1 && A <= 9) && (B >= 1 && B <= 9)) {
			answer = (A * B);
		} else {
			answer = -1;
		}

		System.out.println(answer);

		sc.close();
	}
}

Measured memory_bytes: 23252
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007ll
#define endl "\n"

int main()
{
    ll n,w;
    cin>>n>>w;
    vector<pair<ll,ll>> ar(n);
    ll sum_value=0;
    for(ll x=0;x<n;x++)
    {
        cin>>ar[x].first>>ar[x].second;
        sum_value+=ar[x].second;
    }
    vector<ll> dp(sum_value+1,INT32_MAX);     //Min.weight for this value
    dp[0]=0;
    for(ll x=0;x<n;x++)
    {
        for(ll y=sum_value;y>=ar[x].second;y--)
        {
            dp[y]=min(dp[y],dp[y-ar[x].second]+ar[x].first);
        }
    }
    ll ans;
    for(ll x=0;x<=sum_value;x++)
    {
        // cout<<dp[x]<<" ";
        if(dp[x]<=w)
        {
            ans=x;
        }
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ll> sl;
typedef pair<int,int> pii;
#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 all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

int main(void) {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    //freopen("in.txt", "r", stdin);
    int n;
    cin >> n;
    vi a(n);
    rep(i,0,n) {
        cin >> a[i];
    }
    sort(all(a));
    if(a[n-1] - a[0] > 1) {
        cout << "No" << '\n';
        return 0;
    }
    int x,y;
    x = 0, y = 0;
    rep(i,0,n) {
        if(a[i] == a[0]) {
            x++;
        } else {
            y++;
        }
    }
    if(x == n) {
        if(a[0] == n-1) {
            cout << "Yes" << '\n';
        } else {
            if(a[0] * 2 <= n) {
                cout << "Yes" << '\n';
            } else {
                cout << "No" << '\n';
            }
        }
    } else {
        if(a[0] >= x && a[n-1] <= x + y/2) {
            cout << "Yes" << '\n';
        } else {
            cout << "No" << '\n';
        }
    }
    return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
typedef long long int ll;

string s[]={"Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday"};

int day(int n){
	if(n==1)return 0;
	else{
		int res;
		n--;
		if(n==1||n==3||n==5||n==7||n==8||n==10||n==12){
			res=31;
		}
		else if(n==2){
			res=29;
		}
		else{
			res=30;
		}
		return res+day(n);
	}
}


int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	while(1){
		int n; cin >> n;
		if(n==0)break;
		int m; cin >> m;
		int sum=m+day(n);
		cout << s[sum%7] << endl;
	}
}


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

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<list<int>> q(n);
    int T;
    cin >> T;
    while (T--) {
        int x;
        cin >> x;
        if (x == 0) {
            int a, b;
            cin >> a >> b;
            q[a].push_back(b);
        }
        if (x == 1) {
            int t;
            cin >> t;
            int size = q[t].size();
            int cnt = 0;
            for (int &it : q[t]) {
                cout << it;
                if (cnt++ != size - 1)cout << " ";
            }
            cout << endl;
        }
        if (x == 2) {
            int t, s;
            cin >> s >> t;
            auto it = q[t].end();
            q[t].splice(it, q[s]);
        }
    }
    return 0;
}


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

using namespace std;


#define PI 3.14159265
#define int long long



main(){


	int l, r; cin >> l >> r;


	int ans = 2018;

	for(int i = l; i < r; i++){
		for(int j = i + 1; j <= r; j++){
			ans = min(ans, (i * j) % 2019);
			if(!ans){
				cout << 0 << endl; return 0;
			}
		}
	}

	cout << ans << endl;

	// for(int i = l; i < r; i++){
	// 	for(int j = i + 1; j <= r; j++){
	// 		if((i * j) % 2019 == ans){
	// 			cout << i << " " << j << endl;
	// 			return 0;
	// 		}
	// 	}
	// }






	return 0;
}

Measured memory_bytes: 256
N = int(input())
A = list(map(int, input().split()))
x = 1
list1 = []

count = 0
for i in range(N):
  count += 1
  if A[i] == x:
    list1.append(i)
    count -= 1   
    x += 1
if list1 == []:
  print(-1)
else:
  print(count) 

Measured memory_bytes: 32324
#include <iostream>

using namespace std;

int main()
{
    int N, X, L[100];
    cin >> N >> X;
    for (int i = 0; i < N; i++)
    {
        cin >> L[i];
    }

    int count = 0, index = 0;
    for (int D = 0; D < X + 1; D+=L[index++])
    {
        count++;
        if(count > N) break;
    }

    cout << count << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T>
void cout_vec(const vector<T> &vec){
  for(auto itr:vec) cout<<itr<<' ';
  cout<<endl;
}

typedef pair<ll,ll> P;
const ll mod=1e9+7;
const ll inf=1e15;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,k,q;
  cin>>n>>k>>q;
  vector<ll> cnt(n+1,0);
  rep(i,q){
    ll a;
    cin>>a;
    cnt[a]++;
  }
  FOR(i,1,n+1){
    if(k-(q-cnt[i])>0) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
  }
}

Measured memory_bytes: 1408
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n-1);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    for(int i = 0; i < n; i++){
        cin >> b[i];
    }
    for(int i = 0; i < n-1; i++){
        cin >> c[i];
    }

    int result = b[a[0]-1];
    for(int i = 1; i < n; i++){
        result += b[a[i]-1];
        if(a[i-1]-1 == a[i]-2) result += c[a[i-1]-1];
    }
    cout << result << endl;
}

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

Measured memory_bytes: 256
var n, m, i, sum, val, cnt : longint;
    a : array[1..1000] of longint;
    
begin
    readln(n, m);
    sum := 0;
    for i := 1 to n do 
    begin
        read(a[i]);
        sum := sum + a[i];
    end;
    val := (sum + 4 * m - 1) div (4 * m);
    cnt := 0;
    for i := 1 to n do
    if (a[i] >= val) then inc(cnt);
    
    if (cnt < m) then writeln('No')
    else writeln('Yes');
end.

Measured memory_bytes: 128
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int n = Integer.parseInt(str[0]);
		int k = Integer.parseInt(str[1]);

		int p[] = new int[n];

		str = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			p[i] = Integer.parseInt(str[i]);
		}

		Arrays.sort(p);

		int min = 0;

		for (int i = 0; i < k; i++) {
			min += p[i];
		}

		System.out.println(min);
	}
}

Measured memory_bytes: 32976
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define VSORT(v) sort(v.begin(), v.end());
#define VREV(v) reverse(v.begin(), v.end());
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
using vll = vector<ll>;
using vdouble = vector<double>;
using vstr = vector<string>;
typedef pair<int,int> P;
int dx[4] = {1, 0, -1, 0},dy[4] = {0, 1, 0, -1};
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;}
template<class T> inline bool chmax(T& a, T b){if(a<b) {a=b; return 1;} return 0; }
template<class T> inline bool chmin(T& a, T b){if(a>b) {a=b; return 1;} return 0; }
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
#include <iostream>
 
using namespace std;
 
int main() {
   int n,k;
   cin>>n;
   string l=to_string(n);
   if(l[0]==l[2])
     cout<<"Yes";
   else
     cout<<"No";
     
 
 
   return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<long long>;
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);
    
    int h, w;
    cin >> h >> w;
    vvc c(h, vc(w));
    rep(i, h) rep(j, w) cin >> c[i][j];
    vvc ans(2*h, vc(w));
    rep(i, 2*h) rep(j, w) ans[i][j] = c[i/2][j];
    rep(i, 2*h) {
        rep(j, w) cout << ans[i][j];
        cout << endl;
    }
}

Measured memory_bytes: 384
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>
#include <random>
#include <cstring>
#define mod  1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back

using namespace std;
using ll = long long;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

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

  ll ans = 0, black = 0;
  for(int i = 0; i < S.size(); i++){
    if(S[i] == 'W') ans += black;
    if(S[i] == 'B') black++;
  }
  
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
const ll INF = 5e15;
const ll MOD = 1e9 + 7;
int main(){
    ll N;
    cin >> N;
    vector<ll> A(N), B(N);
    for(ll &e : A) cin >> e;
    for(ll &e : B) cin >> e;
    ll ans = 0;
    ll low = 0;
    for(ll i = 0; i < N; i++){
        low += (B[i] > A[i]) * (A[i] - B[i]);
        ans += (B[i] > A[i]);
    }
    vector<ll> dic(N);
    for(ll i = 0; i < N; i++) dic[i] = A[i] - B[i];
    sort(dic.begin(), dic.end());
    reverse(dic.begin(), dic.end());
    for(ll i = 0; i < N; i++){
        if(low >= 0) break;
        if(dic[i] <= 0) break;
        low += dic[i];
        ans++;
    }
    if(low >= 0) cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


Measured memory_bytes: 2560
#include <stdio.h>
#define MAX 1000005
#define NIL -1

struct Node{
  int p,l,r;
};
struct Node tree[MAX];
int n,D[MAX],H[MAX];

int getSibling(int u){
  if(tree[u].p==NIL){
    return NIL;
  }
  if(tree[tree[u].p].l!=u && tree[tree[u].p].l!=NIL){
    return tree[tree[u].p].l;
  }
  if(tree[tree[u].p].r!=u && tree[tree[u].p].r!=NIL){
    return tree[tree[u].p].r;
  }
  return NIL;
}
void print(int u){
  int sub,dgree=0;
  printf("node %d: parent = %d, sibling = %d, ",u,tree[u].p,sub = getSibling(u));
  if(tree[u].l!=NIL){
    dgree++;
  }
  if(tree[u].r!=NIL){
    dgree++;
  }
  printf("degree = %d, depth = %d, height = %d, ",dgree,D[u],H[u]);

  if(tree[u].p==NIL){
    printf("root\n");
  }
  else if(tree[u].l==NIL && tree[u].r==NIL){
    printf("leaf\n");
  }
  else{
    printf("internal node\n");
  }
}

void setDepth(int u,int d){
  if(u==NIL){
    return;
  }
  D[u] = d;
  setDepth(tree[u].l,d+1);
  setDepth(tree[u].r,d+1);
}

int setHeight(int u){
  int h1=0,h2=0;
  if(tree[u].l != NIL){
    h1 = setHeight(tree[u].l) + 1;
  }
  if(tree[u].r != NIL){
    h2 = setHeight(tree[u].r) + 1;
  }
  if(h1>=h2){
    H[u] = h1;
    return H[u];
  }
  else{
    H[u] = h2;
    return H[u];
  }
}

int main(){
  int i,v,l,r,root=0;
  scanf("%d",&n);

  for(i=0;i<n;i++){
    tree[i].p = NIL;
  }
  for(i=0;i<n;i++){
    scanf("%d%d%d",&v,&l,&r);
    tree[v].l = l;
    tree[v].r = r;
    if(l != NIL){
      tree[l].p = v;
    }
    if(r != NIL){
      tree[r].p = v;
    }
  }
  for(i=0;i<n;i++){
    if(tree[i].p == NIL){
      root = i;
    }
  }
  setDepth(root,0);
  setHeight(root);

  for(i=0;i<n;i++){
    print(i);
  }
return 0;
}



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

int main() {
  int n, k;
  cin >> n >> k;
  ll ans = k * pow(k-1, n-1);
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> P(N), Q(N), R(N);
    for (auto & p : P) cin >> p;
    for (auto & q : Q) cin >> q;
    for (int i = 0; i < N; ++i) {
        R[i] = i+1;
    }
    int cntP = 0, cntQ = 0, cnt = 0;
    while (next_permutation(R.begin(), R.end())) {
        cnt++;
        if (R == P) cntP = cnt;
        if (R == Q) cntQ = cnt;
    }
    printf("%d\n", abs(cntP-cntQ));
    return 0;
}


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

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef map<ll,ll>::iterator itll;
typedef long double ld;
typedef map<ll,ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define one 1.0
#define inf 1000000000000000000
#define all(vl) vl.begin(),vl.end()
#define m_p make_pair 
#define sz(a) sizeof(a)
#define forn(mp,it) for(it = mp.begin();it!=mp.end();it++)
#define FOR(i,a,n) for(int i=a;i<n;i++) 
#define FORE(i,a,n) FOR(i,a,n+1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl
#define PI 3.14159265358979323846

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,k;
	cin>>n>>k;
	ll cnt = 0;
	for(int i=0;i<n;i++)
	{
		ll a,b;
		cin>>a>>b;
		ll k1 = a*a+b*b;
		if(k1<=k*k)cnt++;
	}
	cout<<cnt<<Endl;
	return 0;
}

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

int main(void) {
    int h, w;
    cin >> h >> w;
    vector<vector<bool>> town(h);
    vector<vector<int>> dist(h);

    int ans = 0;
    rep(i,h) {
        string s;
        cin >> s;
        dist[i].resize(w,-1);
        rep(j,(int)s.size()) {
            if(s[j]=='#') {
                town[i].push_back(false);
            }else{
                town[i].push_back(true);
                ans ++;
            }
        }
    }
    queue<P> que;

    bool flag = false;
    auto push=[&](int x, int y, int d) {
        if(x<0 || y < 0 || x >= w || y >= h)return;
        if(dist[y][x]!=-1)return;
        if(!town[y][x])return;
        que.push(make_pair(x,y));
        dist[y][x] = d;
        if(y==h-1&&x==w-1) {
            ans -= d;   
            flag = true;
        }
    };

    push(0,0,0);

    while(!que.empty()) {
        P tmp = que.front(); que.pop();
        int newx = tmp.first;
        int newy = tmp.second;
        int newd = dist[newy][newx];
        push(newx+1,newy,newd+1);
        push(newx,newy+1,newd+1);
        push(newx-1,newy,newd+1);
        push(newx,newy-1,newd+1);
    }

    if(flag) 
        cout << ans -1<< endl;
    else 
        cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

#define N 50000
#define M 1000 

int h,w,n;
int a[N],col[M][M];

int main()
{
	int now = 1;
	scanf("%d%d%d", &h, &w, &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	for (int i = 1; i <= h; i++)
	{
		if (i % 2 == 1)
		{
			for (int j = 1; j <= w; j++)
			{
				col[i][j] = now;
				a[now]--;
				if (a[now] == 0)
					now++;
			}
		}
		else
		{
			for (int j = w; j >= 1; j--)
			{				
				col[i][j] = now;
				a[now] --;
				if (a[now] == 0)
					now++;
			}
		}
	}
	for (int i = 1; i <= h; i++)
	{
		for (int j = 1; j <= w; j++)
		{
			printf("%d ", col[i][j]);
		}
		printf("\n");
	}
	return 0;
}

Measured memory_bytes: 2688
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#define sqr(x)(x)*(x)
#define fz1(i,n) for (i=1;i<=n;i++)
#define fd1(i,n) for (i=n;i>=1;i--)
#define fz0g(i,n) for (i=0;i<=n;i++)
#define fd0g(i,n) for (i=n;i>=0;i--)
#define fz0k(i,n) for (i=0;i<n;i++)
#define fd0k(i,n) for (i=(long long)(n-1);i>=0;i--)
#define fz(i,x,y) for (i=x;i<=y;i++)
#define fd(i,y,x) for (i=y;i>=x;i--)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define rdst(st,len){char ss[len];scanf("%s",ss);(st)=ss;}
using namespace std;
//struct bit{T_ arr[1000005];int lowbit(int x){return x&(-x);}void add(int x,T_ y){for (int i=x;i<=n;i+=lowbit(i)) arr[i]+=y;}T_ query(int x){T_ s=0;for (int i=x;i>0;i-=lowbit(i)) s+=arr[i];return s;}};
//struct seg{struct treedot{int l,r;T_ sum,add;void update(T_ x){sum+=(r-l+1)*x; add+=x;}}c[800005];void pushdown(int id){if (c[id].add){c[id*2].update(c[id].add);c[id*2+1].update(c[id].add);c[id].add=0;}}void pushup(int id){c[id].sum=c[id*2].sum+c[id*2+1].sum;}void build(int id,int l,int r){if (l>r) return;c[id].l=l;c[id].r=r;c[id].sum=0;c[id].add=0;if (l==r) c[id].sum=0;else{int mid=l+(r-l)/2;build(id*2,l,mid);build(id*2+1,mid+1,r);pushup(id);}}void update(int id,int le,int ri,T_ x){if (le>c[id].r||ri<c[id].l) return;if (le<=c[id].l&&c[id].r<=ri) c[id].update(x);else{pushdown(id);update(id*2,le,ri,x);update(id*2+1,le,ri,x);pushup(id);}}T_ query(int id,int le,int ri){if (ri<c[id].l||c[id].r<le) return 0;if (le<=c[id].l&&c[id].r<=ri) return c[id].sum;T_ ans=0;pushdown(id);ans+=query(id*2,le,ri);ans+=query(id*2+1,le,ri);pushup(id);return ans;}};
long long mod=1e9+7,n,m,i,j,s[200005],fac[200005],fi[200005],inv[200005],a[200005],ans,f[200005];
long long c(long long x,long long y)
{
	return fac[x]*fi[y]%mod*fi[x-y]%mod;
}
long long sum(long long x,long long y)
{
	return ((s[y]-(x==0?0:s[x-1]))%mod+mod)%mod;
}
long long qp(long long x,long long y)
{
	if (y==0) return 1;
	if (y==1) return x;
	long long z=qp(x,y/2);
	z=z*z%mod;
	if (y&1) z=z*x%mod;
	return z;
}
int main()
{
	fac[0]=fac[1]=fi[0]=fi[1]=inv[0]=inv[1]=1;
	for (i=2;i<=200000;i++)
	{
		fac[i]=fac[i-1]*i%mod;
		inv[i]=(mod-mod/i)*inv[mod%i]%mod;
		fi[i]=fi[i-1]*inv[i]%mod;
	}
	scanf("%lld",&n);
	for (i=1;i<=n;i++) scanf("%lld",&a[i]);
	s[0]=fac[n-1];
	for (i=0;i<n;i++) f[i]=fac[i]*fac[n-i-1]%mod;
	for (i=1;i<n;i++)
	{
		s[i]=fac[n]*qp(n-i,mod-2)%mod;
		(s[i]+=s[i-1])%=mod;
	}
	for (i=1;i<n;i++) (ans+=a[i]*sum(i-1,n-2))%=mod;
	reverse(a+1,a+n+1);
	for (i=1;i<=n;i++) (ans+=a[i]*sum(i-1,n-1))%=mod;
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 8064
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
vector<deque<bool> > makeCombinations(int, int);
char btc(bool);

int main() {
  int na, nb;
  cin >> na >> nb;
  vector<deque<bool> > combinations; // true: A, false: B
  if(na <= 4 && nb <= 4) {
    combinations = makeCombinations(na, nb);
    for(auto&& combination : combinations) {
      for(auto&& c : combination) cout << btc(c);
      cout << endl;
    }
  } else {
    vector<deque<bool> > firstCombinations = makeCombinations(min(4, na), min(4, nb));
    vector<deque<bool> > secondCombinations = makeCombinations(na - min(4, na), nb - min(4, nb));
    for(auto&& firstCombination: firstCombinations) for(auto&& secondCombination: secondCombinations) {
      for(auto&& c: firstCombination) cout << btc(c);
      for(auto&& c: secondCombination) cout << btc(c);
      cout << endl;
    }
  }
  return 0;
}

char btc(bool b) {
  return (b ? 'A' : 'B');
}

vector<deque<bool> > makeCombinations(int na, int nb) {
  const bool A = true;
  const bool B = false;
  vector<deque<bool> > combinations;
  vector<deque<bool> > leftCombinations;
  if(na == 1 && nb == 0) return vector<deque<bool> >(1, deque<bool>({A}));
  if(na == 0 && nb == 1) return vector<deque<bool> >(1, deque<bool>({B}));
  if(na != 0) leftCombinations.push_back(deque<bool>({A}));
  if(nb != 0) leftCombinations.push_back(deque<bool>({B}));
  for(auto&& leftCombination: leftCombinations) {
    vector<deque<bool> > rightCombinations = (leftCombination[0] == A ? makeCombinations(na - 1, nb) : makeCombinations(na, nb - 1));
    for(auto&& rightCombination: rightCombinations) {
      deque<bool> combination = leftCombination;
      copy(begin(rightCombination), end(rightCombination), back_inserter(combination));
      combinations.push_back(move(combination));
    }
  }
  return combinations;
}

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

int main(){
	int n;
	cin >> n;
	vector<int>A(n, 0);
	for(int i = 0; i < n; i++){
		cin >> A[i];
	}
	vector< vector<long long int> > dp(n, vector<long long int> (n, 0) );
	for(int i = 0; i < n; i++){
		dp[i][i] = A[i];
	}
	for(int k = 1; k < n; k++){
		for(int i = 0; (i + k) < n; i++){
			dp[i][i+k] = max(A[i] - dp[i+1][i+k], A[i+k] - dp[i][i+k-1]);
		}
	}
	cout << dp[0][n-1] << endl;
}


Measured memory_bytes: 73724
#include <iostream>
using namespace std;
int main(void){
    
    int num;
    int i=1;
    int a, b;
    while(cin >> a >> b){
        if(a == 0 && b == 0){
            break;
        }
        else{
            if(b<a)
                cout << b << " "<< a << endl;
            else
                cout << a << " " << b << endl;
        }
    }
}


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

int main() {
	int a,b,c;
	
	cin>>a>>b>>c;
	
	if(a==1&&b==0&&c==0){
		cout<<"Close"<<endl;
	}else if(a==0&&b==1&&c==0){
		cout<<"Close"<<endl;
	}else if(a==0&&b==0&&c==0){
		cout<<"Close"<<endl;
	}else{
		cout<<"Open"<<endl;
	}
	
	
	return 0;
}

Measured memory_bytes: 3064
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.rmi.server.UnicastRemoteObject;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main{
    static long mod=1000000007;
//    static int dx[]={-1,0,1,0};
//    static int dy[]={0,-1,0,1};

    public static void main(String[] args)   throws Exception, IOException{        
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);

        int n = sc.nextInt();
        int a[] = sc.nextIntArray(n);
        TreeMap<Integer, Integer> tm = new TreeMap<>();
        int s=0;
        for (int i = 0; i < a.length; i++) {
        	Entry<Integer, Integer> x=tm.lowerEntry(a[i]);
        	Integer to=tm.get(a[i]);
			
			if(x==null) {
				s++;
			} else {
				int e=x.getKey(), v=x.getValue()-1;
				if(v==0)tm.remove(e);
				else tm.put(e, v);
			}
			if(to==null)
				tm.put(a[i], 1);
			else tm.put(a[i], to+1);			
		}
        
        
        out.println(s);
        out.flush();
    }



    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
}

class P {
	int x, y;
	P(int  x, int y) {
	  this.x=x;
	  this.y=y;
	}
}


//class P {
//    int id, d;
//    P(int  id, int d) {
//        this.id=id;
//        this.d=d;
//    }
//}

//class P implements Comparable<P>{
//    int a,b;
//    P(int a, int b) {
//    	this.b=b;
//    	this.a=a;
//    }
//
//    public int compareTo(P p){
//        return b-p.b;
//    }
//}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public int[] nextIntArray(int size) throws IOException{
        int r[] = new int[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextInt(); 
        }
        return r;
    }
    public long[] nextLongArray(int size) throws IOException{
        long r[] = new long[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextLong(); 
        }
        return r;
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}


Measured memory_bytes: 46708
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
  int64_t a,b,c,k;
  cin >> a >> b>> c>>k;
  int64_t ans=0;
  if(a>=k){
    ans+=k;
  }else if(a+b>=k){
    ans+=a;
  }else{
    ans+=a;
    k=k-a-b;
    ans=ans-k;
  }
  cout << ans;
}
    

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> a(100005);
int num = 0;

int f(int x) {
    if (a.at(x) == 0) {
        cout << -1 << endl;
        return 0;
    }
    if (a.at(x) == 2) {
        num++;
        cout << num << endl;
        return 0;
    }
    int y = a.at(x);
    a.at(x) = 0;
    num++;
    f(y);
    return 0;
}

int main() {
    int N;
  	cin >> N;
    vector<vector<int>> vec(2, vector<int>(N));
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < N; j++) {
            cin >> vec.at(i).at(j);
        }
    }

    int max = 0;
    for (int i = 0; i < N; i++) {
        int x = 0;
        for (int j = 0; j <= i; j++) {
            x += vec.at(0).at(j);
        }
        for (int j = i; j < N; j++) {
            x += vec.at(1).at(j);
        }
      	//cout << x << endl;
        if (x > max) max = x;
    }
    cout << max << endl;
}

Measured memory_bytes: 3752