akhauriyash/Code-Regression huggingface dataset:
// eddy1021
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long ll;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<LD,LD> Pt;
typedef tuple<int,int,int> tiii;
typedef tuple<LL,LL,LL> tlll;
#define mod9 1000000009ll
#define mod7 1000000007ll
#define INF  1023456789ll
#define INF16 10000000000000000ll
#define FI first
#define SE second
#define X FI
#define Y SE
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else 
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
int __ = 1 , _cs;
/*********default*********/
#define N 100
void build(){

}
char c[ N ][ N ];
bool got[ N ][ N ];
int n , m;
void init(){
  scanf( "%d%d" , &n , &m );
  for( int i = 0 ; i < n ; i ++ )
    scanf( "%s" , c[ i ] );
}
void solve(){
  if( c[ 0 ][ 0 ] == '.' ){
    puts( "Impossible" );
    exit( 0 );
  }
  int ni = 0 , nj = 0;
  while( ni < n && nj < m ){
    got[ ni ][ nj ] = true;
    if( ni + 1 < n && c[ ni + 1 ][ nj ] == '#' ){
      ni ++;
    }else if( nj + 1 < m && c[ ni ][ nj + 1 ] == '#' ){
      nj ++;
    }else break;
  }
  for( int i = 0 ; i < n ; i ++ )
    for( int j = 0 ; j < m ; j ++ ){
      if( c[ i ][ j ] == '.' && got[ i ][ j ] ){
        puts( "Impossible" );
        exit( 0 );
      }
      if( c[ i ][ j ] == '#' && !got[ i ][ j ] ){
        puts( "Impossible" );
        exit( 0 );
      }
    }
  puts( "Possible" );
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=100000+10;
int n;
vector<int>a[3];
long long cnt[2][maxn],sum[maxn],ans;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int tmp;
		scanf("%d",&tmp);
		a[0].push_back(tmp);
	}
	for(int i=1;i<=n;i++){
		int tmp;
		scanf("%d",&tmp);
		a[1].push_back(tmp);
	}
	for(int i=1;i<=n;i++){
		int tmp;
		scanf("%d",&tmp);
		a[2].push_back(tmp);
	}
	sort(a[0].begin(),a[0].end());
	sort(a[1].begin(),a[1].end());
	sort(a[2].begin(),a[2].end());
	for(int i=0;i<n;i++){
		vector<int>::iterator it=lower_bound(a[0].begin(),a[0].end(),a[1][i]);
		cnt[0][i]=it-a[0].begin();
	}
	sum[0]=cnt[0][0];
	for(int i=1;i<n;i++)
		sum[i]=sum[i-1]+cnt[0][i];
	for(int i=0;i<n;i++){
		vector<int>::iterator it=lower_bound(a[1].begin(),a[1].end(),a[2][i]);
		ans+=sum[it-a[1].begin()-1];
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 3192
#include <bits/stdc++.h>
using namespace std;
int main(){
	long long arr[3];
  cin>>arr[0]>>arr[1]>>arr[2];
  
  sort(arr,arr+3);
  if( (arr[0]&1) && (arr[1]&1) && (arr[2]&1)){
  	cout<<arr[0]*arr[1]<<endl;
  
  }
  else cout<<"0\n";
  return 0;


}


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

int buff[][7] = {
  {0, 1, 2, 4, 6, 7, 9},
  {1, 3, 4, 6, 8, 9, 11},
  {2, 4, 5, 7, 9, 10, 12},
  {4, 6, 7, 9, 11, 12, 14},
  {6, 8, 9, 11, 13, 14, 16},
  {7, 9, 10, 12, 14, 15, 17},
  {9, 11, 12, 14, 16, 17, 18}
};
int main()
{
  string S;
  int arr[7];
  
  while(cin >> S, S != "#") {
    bool f = true;
    for(int i = (1 << 19) - 1; i >= 0; i--) {
      int sum = 0;
      for(int j = 0; j < 7; j++) {
        arr[j] = 0;
        for(int k = 0; k < 7; k++) arr[j] += ((i >> buff[j][k]) & 1) << k;
        sum += (S[arr[j]] - '0') << j;
      }
      if(S[sum] != S[arr[3]]) {
        f = false;
        break;
      }
    }
    cout << (f ? "yes" : "no") << endl;
  }
}

Measured memory_bytes: 1180
using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string[] str = Console.ReadLine().Split();
		long N = long.Parse(str[0]);
		long X = long.Parse(str[1]);
		long T = long.Parse(str[2]);
		long ans = ((N-1)/X+1)*T;
		Console.WriteLine(ans);
	}
}

Measured memory_bytes: 17876
#include "iostream"

using namespace std;

int main()
{
	int w, h, x, y, r;
	cin >> w >> h >> x >> y >> r;
	if (x-r >= 0 && y-r >= 0 && x+r <= w && y+r <= h) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}

    return 0;
}

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

int main(){
    int N;
    cin >> N;
    double a[N];
    int i;
    for(i=0; i<N; i++){
        cin >> a[i];
    }
    
    double den;
    int j;
    den=0;
    for(j=0; j<N; j++){
        den=den+1/a[j];
    }
    
    cout << setprecision(20) << 1/den << endl;
    return 0;
}

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

int main() {
	const int N = 50;
	ll K;
	cin >> K;

	array<ll, N> res;
	ll add = K / N;
	for (int i = 0; i < N; i++) {
		res[i] = i + add;
	}

	for (int i = 0; i < (K % N); i++) {
		for (int j = 0; j < N; j++) {
			if (j == i) res[j] += N;
			else res[j]--;
		}
	}

	cout << N << endl;
	for (int i = 0; i < N; i++) {
		cout << res[i];
		if (i != N - 1) cout << " ";
		else cout << endl;
	}

	return 0;
}


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


const int mod = 1000000007;
//const int mod = 998244353;
const int INF = 1001001001;
const int di[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dj[] = {0, -1, 0, 1, -1, 1, -1, 1};

struct mint {
  ll x;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main()
{
    ll a, b;
    cin >> a >> b;
    if((a == 0 || b == 0) || (a > 0 && b < 0) || (a < 0 && b > 0)) cout << "Zero" << endl;
    else if((a > 0 && b > 0) || (a < 0 && b < 0 && (b - a)%2)) cout << "Positive" << endl;
    else cout << "Negative" << endl;
    return 0;
}


Measured memory_bytes: 3592
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String S = sc.next();
		String T = sc.next();

		int ans=0;
		for (int i=0; i<S.length(); i++){
			if (S.charAt(i) == T.charAt(i)){
				ans++;
			}
		}
		System.out.println(ans);
	}

}

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

int main(){
    int N;
    cin >> N;
    vector<long long> a(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];          
    }
    long long sum = 0;
    long long tmp = a[0];
    for (int i = 0; i < N; i++) {
        if (a[i] < tmp) {
            // 前の人の身長の方が高い
            sum += tmp-a[i];
        }
        else tmp = a[i];          
    }
    cout << sum << endl;
}




Measured memory_bytes: 4764
n, m = map(int, input().split())
l, r = [], []

for _ in range(m):
    s, t = map(int, input().split())
    l.append(int(s))
    r.append(int(t))

ans = min(r) - max(l) + 1 if min(r) >= max(l) else 0
print(ans)


Measured memory_bytes: 16812
N = int(input())
A = list(map(int,input().split()))

"""
ans = 1
for x in range(N):
    ans *= A[x]
"""
A = sorted(A)

ans = 1
for x in A:
    ans *= x
    if ans == 0 or ans > 1.0e+18:
        break


if ans > 1.0e+18:
    print(-1)
else:
    print(ans)


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

int main()
{
  	int req[3]={-1, -1, -1};
	int n,m;
    std::cin >> n >> m;
    for(int i=0;i<m;i++){
    	int pos, val;
        std::cin >> pos >> val;
        if(req[n-pos]==-1){
          	req[n-pos]=val;
        }else if(req[n-pos]!=val){
        	std::cout << "-1\n";
            return 0;
        }
    }
  	if(n==1){
      	if(req[0]==-1) req[0]=0;
    }else{
    for(int i=0;i<(n-1);i++){
        if(req[i]==-1){
			req[i]=0;
        }
    }
  	if(req[n-1]==-1){
		req[n-1]=1;
	}else if(n!=1 && req[n-1]==0){
        std::cout << "-1\n";
    	return 0;
    }
    }
    for(int i=0;i<n;i++){
    	std::cout << req[n-i-1];
    }
  	std::cout << "\n";
  	return 0;
}


Measured memory_bytes: 3592
#include <bits/stdc++.h>
using namespace std;
char s[111];

int main() {
    int len;
    scanf("%d%s", &len, s);
    int R = 0, B = 0;
    for(int i = 0; i < len; ++i) {
        if(s[i] == 'R') R += 1;
        else B += 1;
    }
    puts(R > B ? "Yes" : "No");
    return 0;
}

Measured memory_bytes: 256
import math
n, d = map(int, input().split(' '))

def getDistance(x, y):
    return math.sqrt(x ** 2 + y ** 2)


count = 0

for i in range(n):
    x, y = map(int, input().split(' '))
    if(getDistance(x, y) <= d):
        count += 1

print(count)
    

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

long long n, x, s, mn=1e18, a[2005], m[2005];
int main() {
	int i, j;
	cin >> n >> x;
	for(j=0; j<n; j++) cin >> a[j], m[j] = 1e18;
	for(i=0; i<n; i++) {
		s = 0;
		for(j=0; j<n; j++) {
			m[j] = min(m[j], a[(j-i+n)%n]);
			s += m[j];
		}
		mn = min(mn, s + i*x);
	}
	cout << mn;
	return 0;
}


Measured memory_bytes: 384
pub fn solve() {
    let mut buf = String::new();
    let handle = std::io::stdin();
    handle.read_line(&mut buf).unwrap();
    let n: isize = buf.trim().parse().unwrap();
    buf.clear();
    let mut max = -1;
    let mut max_index = -1;
    let mut prices = Vec::with_capacity(n as usize);

    for i in 0..n {
        handle.read_line(&mut buf).unwrap();
        let p: isize = buf.trim().parse().unwrap();
        prices.push(p); 
        if max < p {
            max = p;
            max_index = i;
        }
        buf.clear();
    }
    prices[max_index as usize] = max / 2;
    let s: isize = prices.into_iter().sum();
    println!("{}", s);
}
fn main() {
    solve();
}


Measured memory_bytes: 4352
S = input()[::-1]

counts = [0] * 2019
counts[0] = 1

a = 1
d = 0

for s in S:
    d = d + int(s)*a
    num = d%2019
    counts[num] += 1
    a *= 10
    a %= 2019

ans = 0

for i in counts:
    if i >=2:
        x = (i*(i-1))//2
        ans += x

print(ans)

Measured memory_bytes: 9392
import sys

n, m = [int(i) for i in input().split(" ")]
e = []
par = [i for i in range(0, n + 1)]
count = [1] * (n + 1)
unu = [0] * m

sys.setrecursionlimit(10**6)

for i in range(0, m):
    e.append([int(j) for j in input().split(" ")])

def root(x):
    if par[x] == x:
        return x
    else:
        r = root(par[x])
        par[x] = r
        return r

def unite(x, y, e):
    x = root(x)
    y = root(y)
    if x == y:
        return
    else:
        unu[e] = count[x] * count[y]
        count[y] += count[x]
        par[x] = y

for i in reversed(range(0, m)):
    unite(e[i][0], e[i][1], i)

s = 0
for i in range(0, m):
    s += unu[i]
    print(s)

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

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

typedef long long ll;

/* --- INFの値が適切か確認する --- */
// const int INF = 1 << 30;
// const ll INF = 1LL << 60;
// const int MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  double ans = 0;
  rep(i, n) {
    double x;
    string s;
    cin >> x >> s;
    if (s == "JPY")
      ans += x;
    else
      ans += x * 380000.0;
  }
  printf("%.10f\n", ans);
}


Measured memory_bytes: 256
'''
https://beta.atcoder.jp/contests/abc077/submissions/2532572
'''
from bisect import bisect_left, bisect_right

def inpl():
    return list(map(int, input().split()))

n = int(input())
a = sorted(inpl())
b = sorted(inpl())
c = sorted(inpl())

ans = 0
for v in b:
    ans += bisect_left(a, v) * (n - bisect_right(c, v))
print(ans)

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

using namespace std;

typedef long long ll;
  
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  vector<pair<int, int> > pairs(N);
  for(int i = 0; i < N; i++){
    cin >> A.at(i) >> B.at(i);
    pairs.at(i) = make_pair(B.at(i), A.at(i));
  }
  sort(pairs.begin(), pairs.end());
  ll time = 0;
  for(int i = 0; i < N; i++){
    time += pairs.at(i).second;
    if(time > pairs.at(i).first){
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}

Measured memory_bytes: 3328
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
using ll = long long;

int main(){
    int n;
    cin>>n;
    vector<pair<ll,ll>> v(n);
    for(int i=0;i<n;i++){
        ll a,b;
        cin>>a>>b;
        v[i]=make_pair(b,a);
    }
    sort(v.begin(),v.end());
    ll t=0;
    for(int i=0;i<n;i++){
        if(v[i].first>=v[i].second+t){
            t+=v[i].second;
        }else{
            cout<<"No"<<endl;
            return 0;
        }
    }
    cout<<"Yes"<<endl;
}

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

int main() {
    int m, f, b;
    cin >> m >> f >> b;
    if(m + f < b)
        cout << "NA" << endl;
    else if(m > b)
        cout << 0 << endl;
    else
        cout << b - m << endl;
}


Measured memory_bytes: 3096
#include <bits/stdc++.h>
using namespace std;
 
int main() {
 
  int A, B;
  cin >> A >> B;

  if (A <= 5) {
    cout << 0 << endl;
  } else if (A <= 12) {
    cout << B / 2 << endl;
  } else {
    cout << B << endl;
  }

}

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

int main(){
  int N,M;
  cin >> N >> M;
  int P[100000];
  int Y[100000];
  vector<int>Y_num[100001];

  for(int i=0;i<M;i++){
	cin >> P[i] >> Y[i];
	Y_num[P[i]].push_back(Y[i]);	
  }

  for(int i=0;i<N;i++){	
	sort(Y_num[i+1].begin(),Y_num[i+1].end());
  }
  
  for(int i=0;i<M;i++){
	cout << setw(6) << setfill('0')<<P[i];
	cout << setw(6) << setfill('0')<<int(lower_bound(Y_num[P[i]].begin(),Y_num[P[i]].end(),Y[i])-Y_num[P[i]].begin())+1;

	cout << endl;
  }
  


  return 0;
}


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

// ----------- define --------------
#define int long long
#define ii pair<int,int>
#define fi first
#define sc second
#define all(x) (x).begin(),(x).end()
// ---------------------------------

void MAIN() {
  int a, b;
  cin >> a >> b;
  cout << a / __gcd(a, b) * b << '\n';
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) MAIN();
}


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

int main()
{
    long long n;

    cin >> n;

    long long nMod = 1;
    long long mMod = 1;
    long long oMod = 1;
    for (int i = 0; i < n; i++)
    {
        nMod *= 10;
        nMod %= 1000000007;

        mMod *= 9;
        mMod %= 1000000007;

        oMod *= 8;
        oMod %= 1000000007;
    }

    long long ans = nMod - (2 * mMod - oMod);
    ans %= 1000000007;
    if (ans <= 0)
    {
        ans += 1000000007;
    }

    if (n <= 1)
    {
        ans = 0;
    }


    cout << ans << endl;
}




Measured memory_bytes: 3576
#include <stdio.h>

int main() {
	char s[4];
	scanf("%s", s);
	
	if(s[0]==s[1] && s[1] != s[2] && s[0] !=s[2]) puts("Yes");
	
	else if (s[0] != s[1] && s[1] != s[2] && s[0] == s[2]) puts("Yes");
	
	else if (s[0] != s[1] && s[1] == s[2] && s[0] != s[2]) puts("Yes");

	else puts("No");
	
	return 0;
	
}

Measured memory_bytes: 2084
/*	MASTER HAS FAILED MORE TIMES
	THAN BEGINNER HAS EVER TRIED
*/

#include <bits/stdc++.h>

using namespace std;

#define endl            '\n'
#define mem(a, n)       memset(a, n, sizeof(a))
#define repeat(n)       for(int i=0; i<n; i++)
#define Unique(n)       (n).erase(unique(all(n)), (n).end())
#define isPowerOfTwo(n) (n && !(n & (n - 1)))
#define NumofDigits(n)  ((int)log10(n)+1)
#define all(a)          a.begin(), a.end()

int sieve(int n);
unsigned long long pow(long long base, long long power);
long long gcd(long long a, long long b){ return a%b ? gcd(b, a%b) : b; }
long long lcm(long long a, long long b){ return a*b/gcd(a, b); }

void quicken(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
void file(){
    #ifdef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif /*ONLINE_JUDGE*/
}

int main(){
    quicken();

    long long n, k; cin >> n >> k;
    long long arr[n], sum = 0;
    repeat(n) cin >> arr[i];
    sort(arr, arr+n);
    reverse(arr, arr+n);

    for(int i=min(n, k); i<n; i++)
        sum += arr[i];
    cout << sum;
    return 0;
}

unsigned long long pow(long long base, long long power){
    if(power==0) return 1;
    if(power==1) return base;

    unsigned long long ans = pow(base, power/2);
    ans *= ans; /// ans = (ans%mod)*(ans%mod);

    if(power&1) return ans*base; /// (ans%mod)*(base%mod);
    return ans;
}

int sieve(int n){
    int c = 0;
    vector<bool> isPrime(n+1, true);
    isPrime[0] = isPrime[1] = 0;

    for(long long i=2; i*i<=n; i++)
        if(isPrime[i])
            for(long long j=i*2; j<=n; j+=i)
                isPrime[j] = 0;
    repeat(n+1)
        if(isPrime[i]) c++;
    return c; /// primeCount [1:n]
}


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

using namespace std;
typedef long long ll;
#define REP(i,n) FOR(i,0,n)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define ALL(a) (a).begin(),(a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll,ll> pii;
struct Edge{
  ll to,id;
};
typedef vector<vector<ll>> Graph;
typedef vector<pii> vpii;
struct X {ll s,t,u; X(ll s,ll t,ll u) : s(s),t(t),u(u){}};

int main() {
  ll n,m; cin>>n>>m;
  vi a(n); REP(i,n) cin>>a[i];

  ll original=0;
  REP(i,n-1) original+=(a[i+1]+m-a[i])%m;

  vector<X> vx;
  REP(i,n-1) {
    if (a[i]<a[i+1]) vx.PB(X(a[i]+1,a[i+1],0));
    else if(a[i]==m) vx.PB(X(1,a[i+1],0));
    else {vx.PB(X(a[i]+1,m,0)); vx.PB(X(1,a[i+1],m-a[i]));}
  }

  UM<ll,vector<X>> f,l;
  for (X x:vx) {
    f[x.s].PB(x);
    l[x.t].PB(x);
  }

  ll sum=0,cnt=0,ma=0;
  REP(i,m+1) {
    sum+=cnt;
    for (X x:f[i]) {cnt++; sum+=x.u;}
    ma=max(ma,sum);
    for (X x:l[i]) {cnt--; sum-=(x.u+x.t-x.s);}
  }
  cout<<original-ma<<endl;
}


Measured memory_bytes: 30060
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
const double EPS = 1e-10;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------

int n;
vector<int> p;

int main()
{
	cin >> n;
	for (int i = 0;i < n;i++) {
		int t;
		cin >> t;
		p.push_back(t);
	}

	sort(p.begin(), p.end());
	p[n - 1] /= 2;

	int ans = 0;
	for (int i = 0;i < n;i++) ans += p[i];
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 256
#!/usr/bin/env python3
import sys


def solve(a: int, b: int):
    ans = 0
    for i in range(1, b - a):
        ans += i
    print(ans - a)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = int(next(tokens))  # type: int
    b = int(next(tokens))  # type: int
    solve(a, b)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
//#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second
#define mp make_pair
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N, M, Dist[55]; ll ans = 0;
Pii Edge[55];
vector <Pii> Graph[55];
bool now[20], visited[55], Check[20][55];

int Prepare(){
  scanf("%d", &N);
  fill(Dist, Dist + N + 1, -1);
  for (int i = 0; i < N - 1; i++){
    int A, B; scanf("%d%d", &A, &B);
    Edge[i] = mp(A, B);
    Graph[A].push_back(mp(B, i));
    Graph[B].push_back(mp(A, i));
  }
  return 0;
}
int Calc_Dist(){
  queue <int> que; que.push(1);
  Dist[1] = 0;
  while(!que.empty()){
    int Q = que.front(); que.pop();
    for (int i = 0; i < Graph[Q].size(); i++){
      int next = Graph[Q][i].fi;
      if (Dist[next] != -1) continue;
      Dist[next] = Dist[Q] + 1;
      que.push(next);
    }
  }
  return 0;
}
int Input(){
  scanf("%d", &M);
  for (int i = 0; i < M; i++){
    int U, V; scanf("%d%d", &U, &V);
    if (Dist[U] > Dist[V]) swap(U, V);

    while (Dist[U] < Dist[V]){
      for (int j = 0; j < Graph[V].size(); j++){
        Pii next = Graph[V][j];
        if (Dist[V] > Dist[next.fi]){
          Check[i][next.se] = true; V = next.fi;
          break;
        }
      }
    }

    while (U != V){
      for (int j = 0; j < Graph[U].size(); j++){
        Pii next = Graph[U][j];
        if (Dist[U] > Dist[next.fi]){
          Check[i][next.se] = true; U = next.fi;
          break;
        }
      }
      for (int j = 0; j < Graph[V].size(); j++){
        Pii next = Graph[V][j];
        if (Dist[V] > Dist[next.fi]){
          Check[i][next.se] = true; V = next.fi;
          break;
        }
      }
    }
  }
  return 0;
}
int Solve(int num){
  int cnt = 0;
  fill(now, now + M, false);
  fill(visited, visited + N + 1, false);
  for (int i = 0; i < M; i++){
    now[i] = num % 2; num /= 2;
    if (now[i] == false) continue;
    cnt++;
    for (int j = 0; j < N - 1; j++){
      if (Check[i][j]) visited[j] = true;
    }
  }

  int count = 0;
  for (int i = 0; i < N - 1; i++){
    if (!visited[i]) count++;
  }

  ans += pow(-1, cnt) * pow(2, count);

  return 0;
}

int main(){
  Prepare();
  Calc_Dist();
  Input();
  for (int i = 0; i < pow(2, M); i++) Solve(i);
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using pdd = pair<double, double>;
using uint = unsigned int;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

    int N, Z, W;
    cin >> N >> Z >> W;
    vector<int> a(N);
    for (int i = 0; i < N; ++i)
        cin >> a[i];

    vector<vector<int>> dp(N + 1, vector<int>(2));
    for (int i = 1; i <= N; ++i) {
        dp[i][0] = abs((N - 1 - i >= 0 ? a[N - 1 - i] : W) - a[N - 1]);
        dp[i][1] = abs((N - 1 - i >= 0 ? a[N - 1 - i] : Z) - a[N - 1]);
        for (int j = 1; j < i; ++j) {
            dp[i][0] = max(dp[j][1], dp[i][0]);
            dp[i][1] = min(dp[j][0], dp[i][1]);
        }
    }

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

    return 0;
}


Measured memory_bytes: 3776
#define _USE_MATH_DEFINES
#include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <tuple>
#include <climits>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<long long>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<vector<double>> VVD;

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

const int MOD = 1000000007;

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

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

void debug(VI &arr){
	int n=arr.size();
	rep(i,n){
		cout<<arr.at(i)<<endl;
	}
}

void debug(VVI &board){
	int h = board.size();
	int w = board.at(0).size();
	rep(i,h){
		rep(j,w){
			cout << board.at(i).at(j) << " ";
		}
		cout<<endl;
	}
}

void debug(VVLL &board){
	int h = board.size();
	int w = board.at(0).size();
	rep(i,h){
		rep(j,w){
			cout<<board.at(i).at(j)<<" ";
		}
		cout<<endl;
	}
}

void debug(VS &board){
	int h = board.size();
	rep(i,h){
		cout<<board.at(i)<<endl;
	}
	return;
}

LL gcd(LL a,LL b){
	return b==0?a:gcd(b,a%b);
}

LL lcm(LL a,LL b){
	return a*b/gcd(a,b);
}

int main(){
	//cout<<fixed;
	//cout<<setprecision(9)<<...
	int l,r,d;
	cin>>l>>r>>d;
	int ans = 0;
	for(int i=l;i<=r;i++){
		if(i%d==0)ans++;
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 3596
#include <iostream>
#include<string.h>
using namespace std;
int main(){
	string str;
	int i;
	cin>>str>>i;
	for (int u=0;u<str.size();u++){
		if (u==0||u%i==0) cout<<str[u];
	}
	cout<<endl;
	return 0;
}

Measured memory_bytes: 256
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		long a=sc.nextLong(),b=sc.nextLong();
		int sum = 1;
		for(;;){
			if(a*2<=b){
				sum++;
				a*=2;
			}else{
				System.out.println(sum);
				break;
			}
		}
	}
}


Measured memory_bytes: 22996
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll,lvector,greater<ll>>
#define P pair<ll,ll>
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define rep(i,n) for(ll i=0; i<n; ++i)
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;cin>>n;
  lvector A(n),B(n);rep(i,n) cin>>A[i]>>B[i];
  sort(ALL(A));sort(ALL(B));
  ll amed,bmed;
  if(n%2==1) {
    amed=A[(n+1)/2-1];
    bmed=B[(n+1)/2-1];
  }
  else {
    amed=(A[n/2-1]+A[n/2]);
    bmed=(B[n/2-1]+B[n/2]);
  }
  ll ans=bmed-amed+1;
  print(ans);
  return 0;
}

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

int main() {
	long long a,b,c,x,y;
	cin>>a>>b>>c>>x>>y;
	long long ans=a*x+b*y;
	
	if(x>=y){
		ans=min(ans, y*c*2+min((x-y)*a,(x-y)*2*c));
	}
	else{
		ans=min(ans, x*c*2+min((y-x)*b,(y-x)*2*c));
	}
	cout<<ans;
	return 0;
}

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

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
// #define DEBUG



unsigned long long getint(int x[], unsigned long long p[], int n, vector<int> mask)
{
    unsigned long long ret = 0;
    for (auto z : mask)
        ret += x[z] * p[z];
    return ret;
}

int bitcount(int x[], int n, vector<int> mask)
{
    int ret = 0;
    for (auto z : mask)
        ret += x[z];
    return ret;
}

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif
    const int b = 64;
    int n, m, q, x, y, bits[b];
    fill(bits, bits + b, 0);
    unsigned long long p[b];
    
    rep(i, b)
        p[i] = 1ll << i;
    cin >> n;
    vector<int> masks[n];
    rep(i, n){
        cin >> m;
        rep(j, m)
        {
            cin >> x;
            masks[i].push_back(x);
        }
    }

    cin >> q;

    rep(i, q)
    {
        cin >> x >> y;
        if (x == 0)
        {
            cout << bits[y] << endl;
        }
        else if (x == 1)
        {
            for (auto z : masks[y])
                bits[z] = 1;
        }
        else if (x == 2)
        {
            for (auto z : masks[y])
                bits[z] = 0;
        }
        else if (x == 3)
        {
            for (auto z : masks[y])
                bits[z] = 1 - bits[z];
        }
        else if (x == 4)
        {
            cout << ((bitcount(bits, b, masks[y]) == masks[y].size()) ? 1 : 0) << endl;
        }
        else if (x == 5)
        {
            cout << ((bitcount(bits, b, masks[y]) > 0) ? 1 : 0) << endl;
        }
        else if (x == 6)
        {
            cout << ((bitcount(bits, b, masks[y]) == 0) ? 1 : 0) << endl;
        }
        else if (x == 7)
        {
            cout << bitcount(bits, b, masks[y]) << endl;
        }
        else if (x == 8)
        {
            printf("%llu\n", getint(bits, p, b, masks[y]));
        }
    }

    // for (auto z : bits)
    //     cout << z;
    // cout << endl;

    return 0;
}



Measured memory_bytes: 3304
n = int(input())
g = [[]for i in range(n)]
def swap(n):
    if n == 1:
        return 0
    else:
        return 1
for i in range(n-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    w = w%2
    g[u].append([v,w])
    g[v].append([u,w])
ans = [-1 for i in range(n)]
from collections import deque
d = deque([[0,0]]) #左は頂点の数字、右は塗った色
while d:
    p,q = d.pop()
    ans[p] = q
    for j,w in g[p]:
        if ans[j] != -1:
            continue
        else:
            if w == 1:
                d.append([j,swap(q)])
            else:
                d.append([j,q])
for i in range(n):
    print(ans[i])

Measured memory_bytes: 84896
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const int mod = 1e9 + 7;

long long com(long long n, long long x)
{
    long long sum = 0;
    for (long long i = 1; i < x / 3 + 1; i++)
    {
        long long a = x;
        if (i > n - 2)
        {
            break;
        }
        a -= i;
        for (long long j = i + 1; j < a / +1; j++)
        {
            long long b = a;
            if (j > n - 1)
            {
                break;
            }
            b -= j;
            if (b > j && b <= n)
            {
                sum++;
            }
            else if (b <= j)
            {
                break;
            }
        }
    }

    return sum;
}

int main()
{
    bool flag = true;
    while (flag)
    {
        long long n, x;
        cin >> n >> x;
        if (n == 0 && x == 0)
        {
            break;
        }
        long long result = com(n, x);
        cout << result << endl;
    }
    return 0;
}


Measured memory_bytes: 3068
n = int(input())
A = list(map(int, input().split()))

CNT = [0] * (n + 1)
for a in A:
    CNT[a] += 1

x = sum(cnt * (cnt - 1) // 2 for cnt in CNT if cnt)

for a in A:
    print(x - (CNT[a] - 1))


Measured memory_bytes: 32232
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int R = sc.nextInt();
		if(R < 1200) {
			System.out.println("ABC");
		}else if(1200 <= R && R < 2800) {
			System.out.println("ARC");
		}else {
			System.out.println("AGC");
		}
	}
}


Measured memory_bytes: 21844
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])
 
def main():
    s,t = inpls()
    a,b = inpm()
    u = str(input())
    if s==u:
        print(a-1)
        print(b)
    else:
        print(a)
        print(b-1)
    
if __name__ == "__main__":
    main()

Measured memory_bytes: 38892
import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;

string my_readln() { return chomp(readln()); }

void main()
{//try{
	auto tokens = split(my_readln());
	auto N = to!ulong(tokens[0]);
	auto X = to!ulong(tokens[1]);

	ulong[2][] memo;
	memo ~= [1, 1];
	foreach (i; 0..N+1)
	{
		memo ~= [memo[i][0] * 2 + 3, memo[i][1] * 2 + 1];
	}

	ulong f(ulong lv, ref ulong remain)
	{
		if (remain == 0) return 0;
		if (lv == 0) return 1;

		ulong r;
		--remain;

		if (remain < memo[lv-1][0])
			r += f(lv-1, remain);
		else
		{
			r      += memo[lv-1][1];
			remain -= memo[lv-1][0];
		}
		if (remain == 0) return r;

		--remain;
		++r;
		if (remain == 0) return r;

		if (remain < memo[lv-1][0])
			r += f(lv-1, remain);
		else
		{
			r      += memo[lv-1][1];
			remain -= memo[lv-1][0];
		}
		if (remain == 0) return r;

		--remain;

		return r;
	}

	auto result = f(N, X);

	writeln(result);
	stdout.flush();
/*}catch (Throwable e)
	{
		writeln(e.toString());
	}
readln();*/
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
#define tpl(...) make_tuple(__VA_ARGS__)
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef long long ll;
typedef pair<int,int> pii;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}
template<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}
template<int N,class Tp>void out(ostream&,const Tp&){}
template<int N,class Tp,class,class ...Ts>void out(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);out<N+1,Tp,Ts...>(o,t);}
template<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';out<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}
template<class T>void output(T *a,int n){REP(i,n){if(i)cout<<',';cout<<a[i];}cout<<endl;}
template<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}
template<class T>void output(T t){if(t<0)t=-t,putchar(45);static int c[20];
int k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;putchar(c[--k]^48));}
template<class S,class ...T>void output(S a,T...t){output(a);putchar(' ');output(t...);}
template<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);
if(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());t=10*t+c-48);t=n?-t:t;return 1;}
template<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}

struct Cmp{
  bool operator()(const string &a, const string &b) {
    return a.size() != b.size() ? a.size() < b.size() : a < b;
  }
};
int main() {
  int n;
  while(cin >> n) {
    map<string,int,Cmp > mp;
    REP(i,n) {
      string s;
      int x;
      cin >> s >> x;
      mp[s] += x;
    }
    FOR(it, mp) {
      cout << it->first << " " << it->second << endl;
    }
  }
}

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

int N;
vector<string> S;

bool is_vowel(char c){
    string s = "aiueo";
    for(char v: s) if(c == v)
        return true;
    return false;
}

int solve(){
    vector<string> codes;
    
    int n = 100;
    for(auto s: S){
        string t = "";
        t += s[0];
        for(int i=0; i<s.size()-1; i++){
            if(is_vowel(s[i]))
                t += s[i+1];
        }
        n = min(n, (int)t.size());
        codes.push_back(t);
    }
    
    for(int k=1; k<=100; k++){
        set<string> set;
        for(auto t: codes){
            set.insert(t.substr(0, k));
        }
        if(set.size() == N)
            return k;
    }
    
    return -1;
}

int main(){
    while(cin >> N, N){
        S.resize(N);
        for(auto &v: S) cin >> v;
        
        cout << solve() << endl;
    }
    
    return 0;
}

Measured memory_bytes: 3160
#include <bits/stdc++.h>
const int INF = 1e9;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;

#define SCIN(n) string(n);cin >> (n)
#define YES(n) cout<<((n)? "YES" : "NO")<<endl
#define Yes(n) cout<<((n)? "Yes" : "No")<<endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) <<endl

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

#define rt return
#define ENDL cout<<endl
#define s second
#define f first

typedef string::const_iterator State;
class PalseError {};
class Edge{
public:
    LL from,to,value;
    Edge(LL a,LL b,LL c){
        from = a;
        to = b;
        value = c;
    }
    Edge(LL a,LL b){
        from = a;
        to = b;
    }
};

int main(){ 
    int A,B,T;cin >> A >>B >> T;
    cout<<T/A*B<<endl;
}


Measured memory_bytes: 764
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
 
int main()
{
  int n;
  cin >> n;
    n = n%10;
  if (n == 3){
       cout << "bon" << endl;
  }else if(n == 0 || n == 1 || n == 6 || n == 8){
       cout << "pon" << endl;
  }else{ 
    cout << "hon" << endl;
  }
  return 0;
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
int t;
long long n,m;
long long a[2000005],f[2000005];
bool cmp(int a,int b)
{
    return a>b;
}
bool check(long long mid)
{
    long long res=m;
        for(int i=1;i<=n;i++)
        {
            if(mid/f[i]<a[i])
            {
                long long x=a[i]-mid/f[i];
                if(x>res)
                    return 0;
                res-=x;
            }
        }
        return 1;
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
        cin>>f[i];
    sort(a+1,a+1+n);
    sort(f+1,f+1+n,cmp);
    long long right=-1;
    for(int i=1;i<=n;i++)
        right= max(right,a[i]*f[i]);
    long long left=0;
    long long ans=0;
    while(left<=right)
    {
        long long mid=(left+right)/2;
        if(check(mid)) right=mid-1,ans=mid;
        else left=mid+1;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 6016
import java.util.Scanner;

public class Main {

	@SuppressWarnings("resource")
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int x = scanner.nextInt();
		int a = scanner.nextInt();
		int b = scanner.nextInt();

		if (Math.abs(a - x) < Math.abs(b - x)) {
			System.out.println("A");
		} else {
			System.out.println("B");
		}
	}
}


Measured memory_bytes: 20564
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using P   = pair<ll, ll>;
using mat = vector<vector<ll>>;

void IOS() { ios::sync_with_stdio(false), cin.tie(0); }

const ll INF = 1e16;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-10;

template <typename T> void dump(T x) { cout << x << endl; }
void dumpf(ld x, int t) { cout << setprecision(t) << fixed << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
	fill((T *)array, (T *)(array + N), val);
}

inline ll mod(ll a, ll b) {
	if (a > 0) return a % b;
	if (a % b == 0) return 0;
	ll x = -a / b + 1;
	a += x * b;
	return a % b;
}

// ll dx[4] = {1, 0, -1, 0};
// ll dy[4] = {0, 1, 0, -1};

int main() {
	IOS();
	string s;
	cin >> s;
	int n	 = s.size();
	ll cnt[2] = {};
	for (int i = 0; i < n; i++) {
		cnt[s[i] - '0']++;
	}
	dump(min(cnt[0], cnt[1]) * 2);
}

Measured memory_bytes: 512
// Why I am so dumb? :c
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define fi first
#define se second

using namespace std;

typedef long long ll;

int arr[200005];

int n, ans;

void solve() {                   
	scanf("%d", &n);

	for (int i = 1, x; i <= n; ++i) {
		scanf("%d", &x);
		arr[x] = i;
	}

	for (int l = 1, r; l <= n; l = r) {
		r = l + 1;

		while (r <= n && arr[r] > arr[r - 1]) {
			++r;
		}

		ans = max(ans, r - l);
	}
    
    printf("%d\n", n - ans);
}

int main() {    
    int tt = 1;

    while (tt--) {
        solve();
    }

    return 0;
}

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

#define ll          long long
#define pb          push_back
#define endl        '\n'
#define mii         map<ll,ll>
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define INF         (1ll<<60)
#define rep(i,a,b)  for(ll i=a;i<=b;i++)
#define rrep(i,a,b)  for(ll i=a;i>=b;i--)
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int N=100005;
void solve(){
    ll s,w;
    cin>>s>>w;
    if(w>=s){
        cout<<"unsafe"<<endl;
    }
    else cout<<"safe"<<endl;
}
signed main()
{
    ios
    int TESTS=1;
    //cin>>TESTS;
    while(TESTS--){
        solve();
    }
    time
    return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>

#define rep_ui(k,n)      for(ui  i = (k); i < (n); ++i)
#define rep_si(k,n)      for(int i = (k); i < (n); ++i)
#define rep_ul(k,n)      for(ul  i = (k); i < (n); ++i)
#define rep_sl(k,n)      for(ll  i = (k); i < (n); ++i)
#define rep_ui_down(k,n) for(ui  i = (k); i > (n); --i)
#define rep_si_down(k,n) for(int i = (k); i > (n); --i)
#define rep_ul_down(k,n) for(ul  i = (k); i > (n); --i)
#define rep_sl_down(k,n) for(ll  i = (k); i > (n); --i)

#define rep_uj(k,n)      for(ui  j = (k); j < (n); ++j)
#define rep_sj(k,n)      for(int j = (k); j < (n); ++j)
#define rep_uj_down(k,n) for(ui  j = (k); j > (n); --j)
#define rep_sj_down(k,n) for(int j = (k); j > (n); --j)

#define rep(k,n)         rep_ui(k,n)

typedef uintmax_t    ull;
typedef intmax_t     ll;
typedef uint64_t     ul;
typedef uint32_t     ui;
typedef uint8_t      uc;

void solve(void)
{
  ui  N{}, M{}; std::cin >> N >> M;
  std::vector<std::vector<ui>> nodes(N, std::vector<ui>{});
  rep(0, M)
  {
    ui u, v; std::cin >> u >> v;
    nodes[u - 1].push_back(v - 1);
  }
  ui S{}, T{}; std::cin >> S >> T;
  --S; --T;
  if (nodes[S].empty())
  {
    std::cout << -1 << "\n";
    return;
  }

  typedef std::pair<ui, ull> Pair;
  std::queue<Pair> mqueue{};
  std::vector<std::map<ui, ull>> res(N);

  // 初期条件
  res[S][0] = 0;
  boost::for_each(nodes[S], [&mqueue](ui p) {
    mqueue.push(Pair{ p, 1 });
    });

  while (!mqueue.empty())
  {
    Pair pos = mqueue.front();
    mqueue.pop();
    
    // 既に通ってたら終わり
    ui mod = pos.second % 3;
    if (res[pos.first].find(mod) != res[pos.first].end()
     && pos.second >= res[pos.first][mod])
      continue;
    res[pos.first][mod] = pos.second;

    // 行き止まり
    if (nodes[pos.first].empty()) continue;

    ull distance = pos.second + 1;
    boost::for_each(
      nodes[pos.first],
      [&mqueue, &distance](ui next)
      {
        mqueue.push(Pair{next, distance});
      });
  }
  
  if (res[T].find(0) != res[T].end())
  {
    std::cout << (res[T][0] / 3);
  }
  else
  {
    std::cout << -1;
  }
  std::cout << "\n";

  return;
}

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

  solve();

  return 0;
}


Measured memory_bytes: 29312
#a=int(input())
#b,c=int(input()),int(input())
# c=[]
#  for i in b:
#     c.append(i)
X,A = map(int,input().split())
# f = list(map(int,input().split()))
#j = [input() for _ in range(a)]
# h = []
# for i in range(e1):
#     h.append(list(map(int,input().split())))
if X>=A:
    print("10")
if X<A:
    print("0")

Measured memory_bytes: 61820
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8931145141919364364,LMOD=998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main(){
    int n; cin>>n;
    int t[n];
    int sum=0;
    rep(i,n){
        cin>>t[i];
        sum+=t[i];
    }
    int m; cin>>m;
    while(m--){
        int p,x; cin>>p>>x;
        cout<<sum-t[--p]+x<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
typedef long long ll;
int n;
int a[3100]; 
ll dp[3100][3100];

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

    for (int len = 1; len <= n; len++) {
        for (int i = 0; i + len <= n; i++) {
            int j = i + len;

            if ((n - len) % 2 == 0) {
                dp[i][j] = max(dp[i+1][j] + a[i], dp[i][j-1] + a[j-1]);
            } else {
                dp[i][j] = min(dp[i+1][j] - a[i], dp[i][j-1] - a[j-1]);
            }
        }
    }
    cout << dp[0][n] << endl;
}

Measured memory_bytes: 73984
#include<bits/stdc++.h>
#define ll long long int 
#define mod 1000000007
#define se second
#define fi first
#define endl "\n"
#define pi 3.1415926535897932384626433832795028841971
#define MAX 9223372036854775807
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
//	cout<<fixed<<setprecision(15);
	ll x,y;
	cin>>x>>y;
	cout<<x*y;
	return 0;
}

Measured memory_bytes: 3640
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
    cin>>n>>s;
    int l=0,r=0;
    for(int i=0;i<n;i++){
        if(s[i]=='(') l++;
        else{
            if(l>0)	l--;
            else	r++;
        }
    }
    for(int i=0;i<l;i++)	s=s+")";
    for(int i=0;i<r;i++)	s="("+s;
    cout<<s<<endl;
    return 0;
}

Measured memory_bytes: 256