akhauriyash/Code-Regression huggingface dataset:
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()

is_pices_greater_than_points = N >= M
if is_pices_greater_than_points:
    print(0)
    exit()

max_X = max(X)
min_X = min(X)
number_of_moves = max_X - min_X

intervals = [X[i] - X[i - 1] for i in range(1, len(X))]
# print(intervals)
intervals.sort(reverse=True)

for i in range(0, N - 1):
    number_of_moves -= intervals[i]

print(number_of_moves)


Measured memory_bytes: 13968
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define INF (1 << 31) - 1

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  if(K <= A) cout << K << endl;
  else if(K <= A+B) cout << A << endl;
  else cout << 2*A-K+B << endl;
}

Measured memory_bytes: 3632
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;


int main()
{
	int N;
	cin >> N;
	int cnt = 0;
	for (int i = 0; i < N; i++) {
		int p;
		cin >> p;
		if (p != i + 1) cnt++;
	}
	if (cnt > 2) cout << "NO" << endl;
	else cout << "YES" << endl;
}

Measured memory_bytes: 3580
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N, K; cin >> N >> K;
    vector<int> cnt(N);
    for(int i=0; i<K; i++) {
        int M; cin >> M;
        for(int j=0; j<M; j++) {
            int v; cin >> v;
            v--;
            cnt[v]++;
        }
    }

    cout << count(cnt.begin(), cnt.end(), 0) << endl;
    return 0;
}


Measured memory_bytes: 3628
import sys
input = sys.stdin.readline

N = int(input())
A = [int(input()) for _ in range(N)]
cur = 1
ans = A[0] - 1
for a in A[1:]:
    if a == cur + 1:
        cur += 1
    elif a % (cur + 1) == 0:
        ans += a // (cur + 1) - 1
    else:
        ans += a // (cur + 1)
print(ans)

Measured memory_bytes: 77876
#include <bits/stdc++.h>
using namespace std;
int main(void){
    // Your code here!
 int n;
 string s;
 map<string,int> m;
 cin >> n;
 while(n--)
 {
  cin >> s;
  m[s]++;
 }
 cout << "AC x " << m["AC"] << '\n';
 cout << "WA x " << m["WA"] << '\n';
 cout << "TLE x " << m["TLE"] << '\n';
 cout << "RE x " << m["RE"] << '\n';
 return 0;
}

Measured memory_bytes: 3628
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

struct Edge{int to,from,cost;};

#ifdef DEBUG
#define debug cout
#else
SS ss;
#define debug ss
#endif

template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define repn(i,m,n) for(int i=(m);i<=(n);++i)
#define repd(i,n) for(int i=(n)-1;i>=0;--i)
#define repnd(i,m,n) for(int i=(n);i>=(m);--i)
#define rep0(i,n) for(i=0;i<(n);++i)
#define repn0(i,m,n) for(i=(m);i<=(n);++i)
#define repd0(i,n) for(i=(n)-1;i>=0;--i)
#define repnd0(i,m,n) for(i=(n);i>=(m);--i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define IL for(;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define PUTLINE debug<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647/3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[]={1,-1,0,0,1,-1,1,-1,0};
const int dy[]={0,0,1,-1,1,-1,-1,1,0};

int a[110];

int main() {
	int n,k;
	IL{
		cin>>n>>k;
		if(n==0&&k==0)break;
		rep(i,n)cin>>a[i];
		int mx=0;
		rep(i,n-k+1){
			int m=1;
			rep(j,k){
				m*=a[i+j];
			}
//cout<<i<<" "<<m<<endl;
			chmax(mx,m);
		}
		int mm=0;
		rep(i,n)rep(c,n)if(i!=c){
			swap(a[i],a[c]);
			rep(b,n-k+1){
				int m=1;
				rep(j,k){
					m*=a[b+j];
				}
				chmax(mm,m);
			}
			swap(a[i],a[c]);
		}
		if(mm<mx){
			cout<<"NO GAME"<<endl;
		}else{
			cout<<mm-mx<<endl;
		}
	}
	return 0;
}

Measured memory_bytes: 868
#include<iostream>
using namespace std;
int main()
{
	long long n,k=0,s=2,p=1;
	cin>>n;
	while(n>p)
	{
		n=n-p;
		p=p*2; 
		k++;
	}
	for(int i=1;i<=k;i++)
	s=2*s;
	cout<<s-1;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int even=0,cnt=0;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        if(x%2==0)
        {
            even++;
            if(x%3==0||x%5==0)
            {
                cnt++;
            }
        }
    }
    if(even==cnt)
    {
        cout<<"APPROVED"<<endl;
    }
    else{
        cout<<"DENIED"<<endl;
    }
    return 0;
}


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

int main() {
	int n;
	cin >> n;
	int p[n];
	int sum = 0, m = 0;

	for (int i = 0; i < n; i++) {
		cin >> p[i];
		sum += p[i];
		m = max(m, p[i]);
	}
	
	int pay = sum - m / 2;
	cout << pay << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>


using namespace std;
string yn(bool a)
{
  if (a){return "Yes";}
  else{return "No";}
}

bool nakayoshi(vector<int> a,vector<int> b)
{
  return a[0]<b[0] and a[1]<b[1];
}

int main()
{
  ios::sync_with_stdio(false);
  long long q;
  cin>>q;
  long long a,b;
  for(int i=0;i<q;i++)
  {
    cin>>a>>b;
    if(a>b){swap(a,b);}
    if(a==b){cout<<a+b-2<<endl;}
    else
    {
      long long s=(a*b);
      long long n;
      n=sqrt(s)/1;
      if(s>n*(n+1)){n++;}
      long long res=0;
      res+=n-a;
      res+=(s-1)/n-1;
      res+=a-1;
      cout<<res<<endl;

    }
  }
  return 0;





}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main() {
	long long N, h, n, w;
	scanf("%lld", &N);
	for (long long h = 1; h <= 3500; h++) {
		for (long long n = 1; n <= 3500; n++) {
			if (4 * h * n - N * n - N * h == 0) continue;
			if (N * h * n % (4 * h * n - N * n - N * h) == 0
			    and N * h * n / (4 * h * n - N * n - N * h) > 0) {
				printf("%lld %lld %lld\n", h, n, N * h * n / (4 * h * n - N * n - N * h));
				return 0;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define numrep(i,n) for(int i=1;i<=n;i++)
#define ll int64_t
#define all(v) v.begin(), v.end()
#define bitrep(n) for(int bit=0;bit<(1<<n);bit++)


//条件が複数ある場合は段階に分けるのも手
int main() {

int N; cin >> N ;
string S; cin >> S ;

ll r=0,g=0,b=0;//intで収まるが、後の計算でllに収納するにもかかわらずintのままあふれさせ始めるのでもうllにしておく

rep(i,N){
  if(S[i]=='R') r++;
  else if(S[i]=='G') g++;
  else b++;
}

ll ans=r*g*b;//まず組み合わせパターン全てを算出

ll sub=0;

rep(i,N-2){
  for(int j=i+1;j+(j-i)<N;j++){
    if(S[i]!=S[j]&&S[j]!=S[j+(j-i)]&&S[i]!=S[j+(j-i)]){
      sub++;
    }
  }
}

cout << ans-sub << endl;

return 0;
}

Measured memory_bytes: 3648
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

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 a[] = new int[n];

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

			if (a[i] == 1) {
				indexOne = i + 1;
			}
		}

		double ans = Math.floor((n - 1) / (double) (k - 1));
		if ((n - 1) % (double) (k - 1) != 0) {
			ans += 1;
		}

		System.out.println((int) ans);
	}
}

Measured memory_bytes: 44676
#include<iostream>
#include<math.h>
using namespace std;

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

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<cstdlib>
using namespace std;

//ノードを表す構造体を定義する
struct Node {
	int key;
	Node* left, * right, * parent; //左の子、右の子、親を指すポインタ
};

typedef struct Node* node;

node root, NIL;
//preorder tree walk
void preorder(node u) {
	if (u == NIL) {
		return;
	}
	cout << " " << u->key;
	preorder(u->left);
	preorder(u->right);

}

//inorder walk
void inorder(node u) {
	if (u == NIL) {
		return;
	}
	inorder(u->left);
	cout << " " << u->key;
	inorder(u->right);
}

void insert(int k) {
	node y = NIL;		//xの親
	node x = root;
	node z = NIL;

	z = (Node*)malloc(sizeof(Node));
	z->key = k;
	z->left = NIL;
	z->right = NIL;

	while (x != NIL) {
		y = x;		//親を設定
		if (z->key < x->key) {
			x = x->left;		//左の子へ移動
		}
		else {
			x = x->right;		//右の子へ移動

		}
	}
	z->parent = y;
	if (y == NIL) {	//Tが空の場合
		root = z;
	}
	else {
		if (z->key < y->key) {
			y->left = z;		//zをyの左の子にする
		}
		else {
			y->right = z;		//zをyの右の子にする
		}
	}

}
/*
node find(node x, int k) {
	while (x != NIL and k != x->key) {

		if (k < x->key) {
			x = x->left;	//左の子へ移動
		}
		else {
			x = x->right;	//右の子へ移動
		}
	}
	return x;
}
*/

bool find(node x, int k) {
	if (x->key == k) {
		return true;
	}
	else if (k < x->key) {
		if (x->left == NIL) {
			return false;
		}
		find(x->left, k);

	}
	else if(k > x->key) {
		if (x->right == NIL) {
			return false;
		}
		find(x->right, k);
	}
}
int main() {
	int n, i, x;
	string com;
	scanf("%d", &n);

	for (i = 0; i < n; i++) {
		cin >> com;
		if (com == "insert") {
			scanf("%d", &x);
			insert(x);
		}
		else if (com == "print") {
			inorder(root);
			printf("\n");
			preorder(root);
			printf("\n");
		}
		else if (com == "find") {
			scanf("%d", &x);
			if (find(root, x) )printf("yes\n");
			else printf("no\n");
		}
	}

	return 0;
}


Measured memory_bytes: 22048
n,k = map(int,input().split())

ma = sum([n-int(x) for x in range(k)])
mi = sum([int(x) for x in range(k)])

ans = ma-mi+1
for i in range(1,n-k+2):
    mi += k+i-1
    ma += n-k+1-i
    ans += ma - mi + 1

print(ans % (10**9+7))

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

int main(){
	long long I,O,T,J,L,S,Z,ans=0;
	cin >> I >> O >> T >> J >> L >> S >> Z;
	ans=I/2*2+O+J/2*2+L/2*2;
	if(I%2+J%2+L%2==3)ans+=3;
	if(I%2+J%2+L%2==2 && I>0 && J>0 && L>0)ans+=1;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 384
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int i = a; i <= (n); ++i)
#define repD(i, a, n) for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const ll MOD = 1e9 + 7;

ll fp(ll a, ll b){
	ll ans = 1;
	while(b){
		if(b&1) ans = (a*ans)%MOD;
		a = (a*a)%MOD;
		b /= 2;
	}
	return ans;
}


int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	ll n; cin >> n;
	string s; cin >> s;
	ll c[26]; rep(i,26) c[i] = 0;
	for(char it: s) c[it - 'a']++;
	ll ans = 1; rep(i,26) ans = (ans*(c[i] + 1))%MOD;
	cout << (ans - 1 + MOD)%MOD << endl;
	return 0;
}



Measured memory_bytes: 512
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())


L,R = MI()
if L//2019 != R//2019:  # 区間内に必ず2019の倍数がある
    print(0)
else:
    A = [i for i in range(L % 2019,(R % 2019)+ 1)]
    N = len(A)
    ans = 2019
    for i in range(N-1):
        for j in range(i+1,N):
            ans = min(ans,(A[i]*A[j]) % 2019)
    print(ans)


Measured memory_bytes: 66528
N,M = list(map(int,input().split()))
K = [list(map(int, input().split())) for i in range(N)]

suki = []
for i in range(N):
  K[i].pop(0)
  K[i] = sorted(K[i])
  for j in range(1,M+1):
    if all(K[i].count(c) == 1 for c in [j]):
      continue
    else:
      suki.append(j)
print(M-len(set(suki)))

Measured memory_bytes: 3064
import java.util

import scala.collection.mutable
import scala.io.StdIn._

object Main extends App {
  val S = readLine.toCharArray
  val Q = readInt


  val deque = new util.ArrayDeque[Char](util.Arrays.asList(S:_*))
  var reverse = false

  for (_ <- 0 until Q) {
    val query = readLine.split(" ")
    val T = query(0).toInt

    if (T.toInt == 1) {
      reverse = !reverse
    } else {
      val F = query(1).toInt
      val C = query(2).charAt(0)

      val prepend = F == 1 && !reverse || F == 2 && reverse

      if (prepend) {
        deque.addFirst(C)
      } else {
        deque.addLast(C)
      }
    }
  }

  val sb = new mutable.StringBuilder()
  while (!deque.isEmpty) {
    if (!reverse) {
      sb.append(deque.removeFirst())
    } else {
      sb.append(deque.removeLast())
    }
  }

  println(sb.toString())
}




Measured memory_bytes: 61876
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
#define dmp(x) cerr << __LINE__ << " " << #x << " " << x << endl;
template<class T> void chmin(T& a,const T& b){if(a>b)a=b;}
template<class T> void chmax(T& a,const T& b){if(a<b)a=b;}
template<class T,class U>
ostream& operator << (ostream& os,pair<T,U>& p){
	os << p.fi << ',' << p.sec; return os;
}
template<class T,class U>
istream& operator >> (istream& is,pair<T,U>& p){
	is >> p.fi >> p.sec; return is;
}
template<class T>
ostream& operator << (ostream &os,const vector<T> &vec){
	for(int i=0;i<vec.size();i++){
		os << vec[i];
		if(i+1<vec.size())os << ' ';
	}
	return os;
}
template<class T>
istream& operator >> (istream &is,vector<T>& vec){
	for(int i=0;i<vec.size();i++)is >> vec[i];
	return is;
}
void fastio(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
}
struct UnionFind{
  int par[100100];
  UnionFind(){
    for(int i=0;i<100100;i++){
      par[i] = i;
    }
  }
  int find(int x){
    if(par[x]==x)return x;
    else return par[x] = find(par[x]);
  }
  void unite(int x,int y){
    x = find(x);
    y = find(y);
    if(x==y)return;
    par[x] = y;
    return;
  }
  bool same(int x,int y){
    return find(x)==find(y);
  }
}uf;
int N,Q;
ll M;
int a[100100],b[100100],c[100100];
int main(){
  cin >> N >> M >> Q;
  int cnt = 0;
  for(int i=0;i<Q;i++){
    cin >> a[i] >> b[i] >> c[i];
    if(c[i])cnt++;
    else{
      uf.unite(a[i],b[i]);
    }
  }
  ll cmp = 0;
  for(int i=0;i<N;i++){
    if(uf.find(i)==i)cmp++;
  }
  if(cnt==0){
    if(cmp*(cmp-1ll)/2ll-(cmp-1ll)<(ll)(M-(N-1))){
      cout << "No" << endl;
    }else{
      cout << "Yes" << endl;
    }
    return 0;
  }else{
    if(M==N-1){
      cout << "No" << endl;
      return 0;
    }
  }
  for(int i=0;i<Q;i++){
    if(c[i]){
      if(uf.same(a[i],b[i])){
        cout << "No" << endl;
        return 0;
      }
    }
  }
  if(cmp<=2||cmp*(cmp-1ll)/2ll-(cmp-1)<(ll)(M-(N-1))){
    cout << "No" << endl;
  }else{
    cout << "Yes" << endl;
  }
	return 0;
}


Measured memory_bytes: 1920
using System;

namespace agc003_b
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = int.Parse(Console.ReadLine());
            }
            ulong x = 0;
            x += (ulong)a[0] / 2;
            a[0] = a[0] % 2;
            for (int i = 1; i < n; i++)
            {
                if (a[i - 1] == 1 && a[i] > 0)
                {
                    a[i]--;
                    a[i - 1]--;
                    x++;
                }
                x += (ulong)a[i] / 2;
                a[i] = a[i] % 2;
            }
            Console.WriteLine(x);
        }
    }
}

Measured memory_bytes: 15328
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const ll INF = 123456789012LL;

struct SumSegTree{
    int n; vector<ll> dat;
    //初期化
    SumSegTree(int _n){
        n=1;
        while(n<_n) n*=2;
        dat=vector<ll>(2*n-1,0);
    }
    //k番目(0-indexed)に+a
    void add(int k, ll a){
        k+=n-1;
        dat[k]+=a;
        //更新
        while(k>0){
            k=(k-1)/2;
            dat[k]=dat[2*k+1]+dat[2*k+2];
        }
    }
    //内部的に投げられるクエリ
    ll _query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return 0;

        if(a<=l && r<=b) return dat[k];

        ll vl=_query(a,b,2*k+1,l,(l+r)/2);
        ll vr=_query(a,b,2*k+2,(l+r)/2,r);
        return vl+vr;
    }
    //[a,b)
    ll query(int a, int b){
        return _query(a,b,0,0,n);
    }
};

int main()
{
    int n;
    ll K;
    scanf(" %d %lld", &n, &K);

    vector<ll> a(n);
    rep(i,n)
    {
        scanf(" %lld", &a[i]);
        a[i] -= K;
    }

    // rep(i,n) printf(" a[ %d] %lld\n", i,a[i]);

    ll s=0;
    vector<ll> sum(n);
    rep(i,n)
    {
        s += a[i];
        sum[i]=s;
        // printf("sum[%d]= %lld\n", i,s);
    }

    vector<ll> sorted_sum(sum);
    sort(all(sorted_sum));
    sorted_sum.erase(unique(all(sorted_sum)),sorted_sum.end());

    map<ll,int> val2id;
    int V = 0;
    for(const auto &x:sorted_sum) val2id[x]=V++;

    SumSegTree st(V);
    rep(i,n) st.add(val2id[sum[i]],1);

    ll ans = 0;
    ll now = 0;
    rep(i,n)
    {
        int idx = lower_bound(all(sorted_sum),now)-sorted_sum.begin();
        ans += st.query(idx,V);

        now += a[i];
        st.add(val2id[sum[i]],-1);
    }

    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 21504
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define INF 999999999
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

bool prime(int x){
  for(int i = 2; i < sqrt(x);i++){
    if(x % i == 0){
      return false;
    }
  }
  return true;
}

int main(){
  int x;
  cin >> x;
  for(;;){
    if(prime(x)){
      cout << x;
      return 0;
    }
    x++;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <map>
const int MOD = 1e9+7;
typedef long long ll;
using namespace std;


ll llmax(ll x, ll y){
	if (x > y) return x;
	return y;
}

int main(){
	int n, t;
	cin >> n >> t;

	vector<int> vec;

	for (int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		if (y <= t) vec.push_back(x);
	}

	if (!vec.size()){
		cout << "TLE" << endl;
		return 0;
	}
	sort(vec.begin(), vec.end());
	cout << vec[0] << endl;
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<algorithm>

using namespace std;
int main(){
    int n,a;
    long long int sum = 0;
    int max_a = -1000000;
    int min_a = 1000000;
    scanf("%d",&n);
    for(int i = 0;i < n;i++){
        scanf("%d",&a);
        sum += a;
        max_a = max(max_a,a);
        min_a = min(min_a,a);
    }
    printf("%d %d %lld\n",min_a,max_a,sum);
    return 0; 
}


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

int main(){
  int n,k;
  cin>>n>>k;
  vector<int>h(n);
  for(int i=0;i<n;i++){
    cin>>h[i];
  }
  sort(h.begin(),h.end());
  int mn=1000000000;
  for(int i=0;i<n-k+1;i++){
    int x=h[i+k-1]-h[i];
    mn=min(x,mn);
  }
  cout<<mn<<endl;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
  int N, M, Q, ans;
  int A[10];
  int a[50], b[50], c[50], d[50];
void dfs(int num, int s) {

  if(num == N) {
    int t = 0;
    for(int i = 0; i < Q; i++) {
      if(A[b[i]] - A[a[i]] == c[i]) t += d[i];
    }

    if(ans < t) ans = t;
    return;
  }
  for(int i = s; i <= M; i++) {
    A[num] = i;
    dfs(num + 1, i);

  }
}
int main() {
  
  cin >> N >> M >> Q;
  ans = 0;
  for(int i = 0; i < Q; i++) { 
  cin >> a[i] >> b[i] >> c[i] >> d[i];
  a[i]--; b[i]--;
  }
  
  dfs(0, 1);
  cout << ans;
return 0;
}

Measured memory_bytes: 3596
// skip until line 65
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <istream>
#include <iterator>
#include <sstream>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
using namespace std;

#define BR "\n"
#define REP(i, n) for(int (i)=0;(i)<(n);++(i))
#define ALL(cont) begin(cont),end(cont)
#define AS_MOD(a, b) ((((a) % (b) ) + (b)) % (b))
#define FEACH(it, cont) for(auto (it) = begin(cont); it!=end(cont);++it)
#define FEACHR(it, cont) for(auto (it) = rbegin(cont); it!=rend(cont);++it)
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define getll() ([](){ll s;scanf("%lld", &s);return s;})()
#define getld() ([](){ld s;scanf("%Lf", &s);return s;})()
#define prl(P) printf("%lld", P)
#define prd(P) printf("%.10Lf", P)
#define pr(P) printf(P)
#define MAX(V, T) V=max(V,T)
#define MIN(V, T) V=min(V,T)

#define DumpArray(cont, from, to) for(int __i=from;__i<to;++__i) {cout<<"(" <<__i<<": " << cont[__i]<<")";} cout<<endl;

/*
 * ***_bound(6)
 * 5 6 6 6 6 6 7 7 7
 *   ^lower    ^upper
 * void reverse(c.begin(), c.end())
 *
 * graph
 * seg tree
 * dp
 * brute force
 */
struct Comparator{
    int operator()(int l, int r) const {
        return r<l;
    }
};
struct Vec;
//set<int, Comparator> S;

// solution below
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
int main() {
    ll N,M;
    cout.precision(10);
    cout<<fixed;
    cin >> N >> M;
    REP(i,M) {
        ll s;
        cin >>s;
        N-=s;
    }
    if (N<0)cout << -1 << endl;
    else cout<<N<<endl;
}

struct Vec{
    double v[2];
    string to_s(){
        return "("+to_string(v[0])+","+to_string(v[1])+")";
    }
    double& operator[](int t){
        return v[t];
    }
    Vec operator+(Vec t) {
        return {v[0]+t[0],v[1]+t[1]};
    }
    Vec& operator+=(Vec t) {
        v[0]+=t[0];v[1]+=t[1];
        return *this;
    }
    Vec operator-(Vec t) {
        return {v[0]-t[0],v[1]-t[1]};
    }
    Vec& operator-=(Vec t) {
        v[0]-=t[0];v[1]-=t[1];
        return *this;
    }
    Vec operator*(double t) {
        return {v[0]*t,v[1]*t};
    }
    Vec& operator*=(double t) {
        v[0]*=t;v[1]*=t;
        return *this;
    }
    double operator*(Vec t) {
        return v[0]*t[0]+v[1]*t[1];
    }
    Vec operator-(){
        return {-v[0],-v[1]};
    }
    static Vec Z;
};
Vec Vec::Z={0,0};


Measured memory_bytes: 3620
from operator import itemgetter
n = int(input())
ab = [input().split() for i in range(n)]
ab = [[int(ab[i][0]),int(ab[i][1])] for i in range(n)]
ab.sort(key=itemgetter(1))
flg = 0
t = 0
for i in range(n):
  t += ab[i][0]
  if t > ab[i][1]:
    flg = 1
    break
if flg == 0:
  print('Yes')
else:
  print('No')

Measured memory_bytes: 94320
#include<bits/stdc++.h>
#define ll long long 
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second 
#define pii pair<int,int>
#define afor(i,a,b) for(int i=a;i<b;++i)
#define bfor(i,a,b) for(int i=a;i>=b;--i)
#define print(A,j,k) for(int ii=j;ii<k;ii++)cout<<A[ii]<<" ";cout<<"\n"
#define all(v) v.begin(),v.end()
#define ps cout<<"YES\n";
#define CASES int t;cin>>t;while(t--)
//ans = a ? b : c; // to simplify: if (a) ans = b; else ans = c;
using namespace std;
int main()
{
    fast
    ll x=0;
    ll ans=0;
    int n;
    string s;
    cin>>n;
    cin>>s;
    afor(i,0,n)
    {
        if(s[i]=='I')
            x++;
        else if(s[i]=='D')
            x--;
        ans=max(ans,x);
    }
    cout<<ans<<"\n";
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main { 
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int[][] F = new int[N][10];
    for (int i =0; i<N; i++) {
      for (int j =0; j<10; j++) {
          F[i][j] = sc.nextInt();
      }
    }
    long[][] P = new long[N][11];
    for (int i =0; i<N; i++) {
      for (int j =0; j<11; j++) {
          P[i][j] = sc.nextInt();
      }
    }
    long max = Long.MIN_VALUE; 
    for(int i=1;i<(2<<9);i++) {
      int[] count = new int[N];
	  for(int j=0;j<10;j++) {
        int t = (i>>j)&1;
        if(t==1) {
          for(int k=0;k<N;k++) {
            if(F[k][j]==1) count[k]++;
          }
        }
      }   
      int sum = 0;
      for(int j=0;j<N;j++) {
        sum += P[j][count[j]];
      }
      max=Math.max(max, sum);
    }      
    System.out.println(max);  
  }
}

Measured memory_bytes: 26196
#include<stdio.h>

int main()
{
	char left[15] = "qwertasdfgzxcvb";
	char input[33];
	int i, j, preStatus, curStatus, count;
	
	while(1)
	{
		preStatus = curStatus = 0;
		count = -1;
		
		scanf(" %s", input);
		
		if(input[0] == '#') break;
		
		i = 0;
		while(input[i] != '\0')
		{
			j = 0;
			while(j < 15 && left[j] != input[i])
			{
				j++;
			}
			
			preStatus = curStatus;
			if(j == 15)
			{
				curStatus = 1;
			}
			else
			{
				curStatus = 2;
			}
			
			if(preStatus != curStatus)
			{
				count++;
			}
			i++;
		}
		
		printf("%d\n", count);
	}
	
	return 0;
}

Measured memory_bytes: 536
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
INF = 10**9


def dfs(cur, a, b, c):
    """ 深さ優先探索 """
    if cur == N:
        if min(a, b, c) > 0:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        else:
            return INF
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)


print(dfs(0, 0, 0, 0))


Measured memory_bytes: 3188
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
#define RET(ans) {cout<<ans<<endl;return 0;} 

    // 二次元ベクターの基本
    /*
    vector<vector<int>> dp; // 宣言
    dp.resize(n); // 1次元めの要素数決定
    dp[i].push_back(int); // プッシュバック
    rep(i,n){
        sort(dp[i].begin(),dp[i].end()); // 二次元めを昇順ソート
    }
    */

    // 整数スキャン（複数）
    /*
    int x,y,z;
    scanf("%d%d%d",&x,&y,&z);

    // n個の整数のスキャン
    /*
    ll a[n] = {};
    rep(i,n){
        scanf("%lld",&a[i]);
    }
    */

    // 文字列スキャン
    /*
    string s; cin >> s;
    */

    // n個の文字列スキャン
    /*
    vector<string> slist;
    rep(i,n){
        string s; cin >> s;
        slist.push_back(s);
    }
    */

int main() {
    int n;
    cin >> n;
    ll a[n+1] = {};
    srep(i,1,n+1){
        scanf("%lld",&a[i]);
    }


    ll ans = 0;
    srep(i,1,n+1){
        if(a[a[i]] == i){
            ans++;
        }
    }
    ans /= 2;
    cout << ans << endl;
    return 0;
}
 
 


Measured memory_bytes: 1024
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main() {
  int n;
  ll d;
  cin >> n >> d;
  vector<ll> x(n), y(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i];
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ll dist = x[i] * x[i] + y[i] * y[i];
    if (dist <= d * d) ans++;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 6380
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <stdio.h>
#include <tuple>
#include <bitset>
#include <cfloat>
#include <fstream>
#include <limits.h>
#include <list>
#include <math.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <numeric>
#include <utility>
#include <sstream>
// *テンプレート*
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
// aよりbの方が小さかったら、aの値をbの値に置き換える。(swapではない！)
template<class T> inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
// aよりbの方が大きかったら、aの値をbの値に置き換える。
template<class T> inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
const long long MOD = 1000000007;
const long long INF = 1LL << 60;
using namespace std;
typedef long long ll;
map<int, int> mp;
using Graph = vector<vector<int>>; 
//最大公約数
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (b == 0) return a;
	return gcd(b, a % b);
}
//最小公倍数
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}

// *テンプレートここまで*

// pair -> vector<pair<int,int>> 
//		   AandB.push_back(make_pair(a[i],b[i]))

// ^= -> ビットごとの排他的論理和を計算する
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, sum = 0;
	cin >> n;
	rep(i, n) {
		int a; cin >> a;
		sum ^= a;
	}
	if ((sum^sum) == sum)puts("Yes");
	else puts("No");
	return 0;
}

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


using namespace std;
#define rep(i,n) for (int i = 0; i < (int)(n); ++i)
#define reps(i,n) for (int i = 1; i <= (int)(n); ++i)
#define repd(i,n) for (int i = (int)(n - 1); i >= 0; --i)
#define repds(i,n) for (int i = (int)(n); i > 0; --i)
#define loop(i,x,n) for (int i = (int)(x); i < (n); ++i)
#define loops(i,x,n) for (int i = (int)(x); i <= (n); ++i)
#define loopd(i,x,n) for (int i = (int)(x); i > (n); --i)
#define loopds(i,x,n) for (int i = (int)(x); i >= (n); --i)
#define itrep(i,s) for (auto i = begin(s); i != end(s); ++i)
#define itrepd(i,s) for (auto i = --end(s); i != begin(s); --i)
#define all(f,x,...) [&](decltype(x)& whole) { return (f)(begin(whole), end(whole), ##__VA_ARGS__); }(x)
#define rall(f,x,...) [&](decltype(x)& whole) { return (f)(rbegin(whole), rend(whole), ##__VA_ARGS__); }(x)
using ll = long long;
using ld = long double;
constexpr ll inf = static_cast<ll>(1e17);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e17;
ll mod = static_cast<ll>(1e9 + 7);
std::ostream& endn(std::ostream& os) { return os.put(os.widen('\n')); }
template<class T> constexpr int sz(const T& a) { return (int)a.size(); }
template<class T, class ... Args> constexpr T mins(T& a, Args... args) { return a = min<T>({ a, args... }); }
template<class T, class... Args> constexpr T maxs(T& a, Args... args) { return a = max<T>({ a,args... }); }

int n, m;
vector<pair<int, ll>> g[100005];
ll d[100005];
void solve() {
    cin >> n >> m;

    rep(i, n - 1) g[i + 1].emplace_back(i, 0);

    rep(i, m) {
        int l, r; ll c; cin >> l >> r >> c; --l; --r;

        g[l].emplace_back(r, c);
    }

    fill(d, d + n, inf);

    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> que;
    que.emplace(0LL, 0);
    d[0] = 0;
    while (!que.empty()) {
        auto p = que.top(); que.pop();
        auto v = p.second;

        if (d[v] < p.first) continue;

        rep(i, g[v].size()) {
            auto e = g[v][i];
            if (d[v] + e.second < d[e.first]) {
                d[e.first] = d[v] + e.second;
                que.emplace(d[e.first], e.first);
            }
        }
    }

    if (d[n - 1] != inf)
        cout << d[n - 1] << endn;
    else
        cout << -1 << endn;

}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout << flush;
    return 0;
}


Measured memory_bytes: 10232
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <numeric>
using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<int> B(N - 1);
    for (int i = 0; i < N-1; ++i){
        cin >> B[i];
    }
    vector<int> A(N, 0);
    A[0] = B[0];
    for (int i = 1; i <= N-2; ++i)
    {
        A[i] = min(B[i-1], B[i]);
    }

    A[N-1] = B[N - 2];
    int sum = accumulate(A.begin(), A.end(), 0);
    cout << sum << endl;
    return 0;
}

Measured memory_bytes: 256
N = int(input())
A = list(map(int, input().split()))
if N == 1:
    print(1)
    quit()

A.append(A[N-1])
ans = 1
last = A[0]
s = 0
for i in range(N-1):
    if not(A[s] <= A[i] <= A[i+1]) and not(A[s] >= A[i] >= A[i+1]):
        s = i + 1
        ans += 1

print(ans)

Measured memory_bytes: 14252
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define f first
#define s second
#define pb push_back
#define fix  cout<<std::fixed;
#define mk make_pair
#define N 100005
#define mod 1000000007
#define time  cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
#define IOS        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main()
{
 ll a,b,c,k;
 cin>>a>>b>>c>>k;
 if(k>=a+b+c){cout<<a-c;return 0;}
 else if(k>=a+b){cout<<a-min(k-a-b,c);return 0;}
 else if (k>=a){cout<<a;return 0;}
 else if(k<a){cout<<k;return 0;}
}

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

int main()
{
    ll n;
    cin >> n;
    vector<ll> a(n);
    ll arr[8] = {0};
    ll free = 0;
    rep(i, n){
        cin >> a[i];
        if (a[i] < 400){
            arr[0] = 1;
        }else if(a[i] < 800){
            arr[1] = 1;
        }else if(a[i] < 1200){
            arr[2] = 1;
        }else if(a[i] < 1600){
            arr[3] = 1;
        }else if(a[i] < 2000){
            arr[4] = 1;
        }else if(a[i] < 2400){
            arr[5] = 1;
        }else if(a[i] < 2800){
            arr[6] = 1;
        }else if(a[i] < 3200){
            arr[7] = 1;
        }else {
            free++;
        }
    }
    ll sum = 0;
    rep(i, 8){
        sum+=arr[i];
    }
    if (sum == 0){
        cout << 1 << " " << free << endl;
    }else{
        cout << sum << " " << sum+free << endl;
    }

    return 0;
}


Measured memory_bytes: 3584
#include <bits/stdc++.h>
using namespace std;using ll=int_fast64_t;using ld=long double;using pll=pair<ll,ll>;using pld=pair<ld,ld>;
const ll INF=1LL<<60;void solve();int main(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(10);solve();}
#define foreach(i,a) for(auto &i:a)
#define SELECTOR(_1,_2,_3,_4,SELECT,...) SELECT
#define rep(...) SELECTOR(__VA_ARGS__,_rep2,_rep1,_rep0)(__VA_ARGS__)
#define _rep0(i,n) for(ll i=0;i<n;++i)
#define _rep1(i,k,n) for(ll i=k;i<n;++i)
#define _rep2(i,k,n,d) for(ll i=k;d!=0&&d>0?i<n:i>n;i+=d)
template<class T> vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<class... Ts> auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<class T> inline bool chmax(T &a,const T &b){if(a<b){a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a,const T &b){if(a>b){a=b; return 1;} return 0;}
template<class T> void contout(const T &v){for(auto it=v.begin();it!=v.end();++it,cerr<<(it!=v.end()?" ":"\n"))cout<<*it;}

void solve(){
  ll n,m; cin>>n>>m;
  vector<vector<ll>> E(n);
  rep(_,n-1){
    ll x,y; cin>>x>>y;
    --x; --y;
    E[x].push_back(y);
    E[y].push_back(x);
  }

  // 木DP (0-rooted)
  vector<ll> dp(n,1);
  function<void(ll,ll)> dfs1=[&](ll v,ll p){
    foreach(nv,E[v]){
      if(nv==p) continue;
      dfs1(nv,v);
      dp[v]*=dp[nv]+1;
      dp[v]%=m;
    }
  };
  dfs1(0,-1);

  // 全方位木DP
  vector<ll> ans(n,1);
  function<void(ll,ll)> dfs2=[&](ll v,ll p){
    foreach(nv,E[v]){
      ans[v]*=dp[nv]+1;
      ans[v]%=m;
    }
    ll k=E[v].size();
    vector<ll> l(k),r(k);
    rep(i,k) l[i]=r[i]=dp[E[v][i]]+1;
    rep(i,1,k){
      l[i]*=l[i-1];
      l[i]%=m;
    }
    rep(i,k-2,-1,-1){
      r[i]*=r[i+1];
      r[i]%=m;
    }
    ll tmp=dp[v];
    rep(i,k){
      ll nv=E[v][i];
      if(nv==p) continue;
      dp[v]=1;
      if(i>0) dp[v]*=l[i-1];
      if(i<k-1) dp[v]*=r[i+1];
      dp[v]%=m;
      dfs2(nv,v);
    }
    dp[v]=tmp;
  };
  dfs2(0,-1);

  rep(i,n) cout<<ans[i]<<"\n";
}


Measured memory_bytes: 21888
import java.util.Scanner;

public class Main {
	public static void main (String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		String s = scanner.next();
		int max = 0;
		int x = 0 ;
		for (int i = 0 ; i < n ; i++) {
			if (s.charAt(i) == 'I') x++;
			if (s.charAt(i) == 'D') x--;
			if (x > max) max = x;
		}
		System.out.println(max);
	}
} 

Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
int main(){
  string S;
  cin >> S;
  if(S[2]==S[3]&&S[4]==S[5]){
      cout << "Yes" << endl;
  }else{
      cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;++i)
#include<string>
using namespace std;
typedef long long ll;
typedef pair<int, int>P;
//max=({});
//条件式が真ならwhileの中身を回し続ける
//printf("%d\n", ans);


int g[15][15];
const int INF = 1001001001;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,-1,0,1 };

//最大公約数
ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

//素因数分解
vector<pair<ll, int>>factorize(ll n) {
	vector<pair<ll, int>>res;
	for (ll i = 2;i * i <= n;++i) {
		if (n % i)continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1)res.emplace_back(n, 1);
	return res;
}

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a == b && b != c)puts("Yes");
	else if (b == c && c != a)puts("Yes");
	else if (c == a && a != b)puts("Yes");
	else puts("No");
	return 0;
}


Measured memory_bytes: 256
import java.io.*;

public class Main {

  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    String[] in = br.readLine().split(" ");
    int h = Integer.parseInt(in[0]);
    in = br.readLine().split(" ");
    int w = Integer.parseInt(in[0]);
    in = br.readLine().split(" ");
    int n = Integer.parseInt(in[0]);
    br.close();

    if(h < w){
      int t = h;
      h = w;
      w = t;
    }

    System.out.println((n-1)/ h + 1);
  }
}

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

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

const int N = 3e5+10 , mod = 1e9+7 , M = 1e5;

vector <pii> Q[N] , vals , vals2;
int w[N] , val[N] , n;
ll dp[M + 10] , ans[N];

void dfs(int v , int h){
   int num = 0;
   if(h < 9){
      int n = vals.size();
      vals.push_back({w[v] , val[v]});
      for(int i = 0 ; i < n ; i++)
         vals.push_back({vals[i].first + w[v] , vals[i].second + val[v]});
      num = n + 1;
   }
   else{
      int n = vals2.size();
      vals2.push_back({w[v] , val[v]});
      for(int i = 0 ; i < n ; i++)
         vals2.push_back({vals2[i].first + w[v] , vals2[i].second + val[v]});
      num = n + 1;
   }
   if(h == 8){
      vector <pii> cpy;
      cpy = vals;
      sort(cpy.begin() , cpy.end());
      cpy.resize(unique(cpy.begin() , cpy.end()) - cpy.begin());
      int lst = 0;
      int cur = 0;
      for(int i = 0 ; i <= M ; i++){
         while(cur < cpy.size() && cpy[cur].first == i){
            lst = max(lst , cpy[cur].second);
            cur++;
         }
         dp[i] = lst;
      }
   }
   if(h < 9){
      for(pii x : Q[v])
         for(pii y : vals)
            if(y.first <= x.first)
               ans[x.second] = max(ans[x.second] , 1ll * y.second);
   }
   else{
      for(pii x : Q[v]){
         ans[x.second] = dp[x.first];
         for(pii y : vals2)
            if(y.first <= x.first)
               ans[x.second] = max(ans[x.second] , y.second + dp[x.first - y.first]);
      }
   }
   if(2 * v <= n)
      dfs(2 * v , h + 1);
   if(2 * v + 1 <= n)
      dfs(2 * v + 1 , h + 1);
   if(h < 9){
      while(num){
         num--;
         vals.pop_back();
      }
   }
   else{
      while(num){
         num--;
         vals2.pop_back();
      }
   }
}

int32_t main(){
   ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   cin >> n;
   for(int i = 1 ; i <= n ; i++)
      cin >> val[i] >> w[i];
   int q;
   cin >> q;
   for(int i = 0 ; i < q ; i++){
      int v , l;
      cin >> v >> l;
      Q[v].push_back({l , i});
   }
   dfs(1 , 0);
   for(int i = 0 ; i < q ; i++)
      cout << ans[i] << "\n";
   return 0;
}


Measured memory_bytes: 17384
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <vector>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
using namespace std;

int main()
{
	int N;
	cin >> N;
	vector<int> W(N);
	auto sum = 0;
	for (auto i = 0; i < N; ++i)
	{
		cin >> W[i];
		sum += W[i];
	}

	auto left = 0;
	auto result = INT_MAX;
	for (auto i = 0; i < N - 1; ++i)
	{
		sum -= W[i];
		left += W[i];
		result = min(result, abs(sum - left));
	}

	cout << result << endl;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int min(int a, int b){
  return a < b ? a : b;
}
int main(void){
  int n, k;
  vector<int> x;
  vector<int> y;
  long long ans = LLONG_MAX;
  cin >> n >> k;
  for(int i = 0; i < n; i++){
    int a, b;
    cin >> a >> b;
    x.push_back(a);
    y.push_back(b);
  }
  for(int n1 = 0; n1 < n; n1++){
    for(int n2 = 0; n2 < n; n2++){
      for(int n3 = 0; n3 < n; n3++){
        for(int n4 = 0; n4 < n; n4++){
          long long x1 = x[n1];
          long long x2 = x[n2];
          long long y1 = y[n3];
          long long y2 = y[n4];
          int cnt = 0;
          if(x1 >= x2 || y1 >= y2) continue;
          for(int n5 = 0; n5 < n; n5++){
            if(x1 <= x[n5] && x[n5] <= x2 && y1 <= y[n5] && y[n5] <= y2) cnt++;
          }
          long long s = (y2-y1)*(x2-x1);
          if(cnt >= k) ans = min(ans, s);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

typedef long long ll;
#define READ(a) int a; cin >> a;
#define READARR(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define READMAT(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;} cout << endl;
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
bool dp[81][81][12801] = {};

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
    int h, w;
    cin >> h >> w;
    READMAT(p, h, w);
    READMAT(q, h, w);
    dp[1][0][0] = dp[0][1][0] = 1;
    
    FOR (i, 1, h)
        FOR (j, 1, w)
            FOR (k, 0, 12800)
            {
                int a = abs(k + p[i][j] - q[i][j]);
                int b = abs(k + q[i][j] - p[i][j]);
                dp[i][j][k] = (dp[i - 1][j][a] || dp[i - 1][j][b]) || (dp[i][j - 1][a] || dp[i][j - 1][b]);
            }
    
    FOR (i, 0, 12800) 
        if (dp[h][w][i]) {cout << i; return 0;}
}


Measured memory_bytes: 81408
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector> 
#include<limits>
#include<numeric>
#include<type_traits>
#include<math.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define in(x) cin >> x
#define out(str) cout << str << endl

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

string S;
int wa=0;

int main(){
    cin>>S;
    rep(i,S.length()){
        wa+=S[i]-'0';
    }
    if(stoi(S)%wa==0){
        out("Yes");
    }
    else{
        out("No");
    }
}


Measured memory_bytes: 256