akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
#define N 10000
using namespace std;

class edge
{
  public:
    int i, j, weigt;
    bool operator()(edge e1, edge e2)
    {
        return e1.weigt < e2.weigt;
    }
};
edge e[N];
vector<int> adj[N];
int n;
int c = 0;
int f[N] = {};
int g[N] = {};

void setGraphNum(int v, int i)
{
    g[v] = i;
    f[v]++;
    vector<int>::iterator itr = adj[v].begin();
    while (itr != adj[v].end())
    {
        if (f[*itr] != c)
            setGraphNum(*itr, i);
        itr++;
    }
}
void CalcGraphNum()
{
    int k = 1;
    for (int i = 0; i < n; i++)
    {
        if (f[i] != c)
            setGraphNum(i, k++);
    }
}
int main()
{
    cin >> n;
    int k = 0, tmp = 0, sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> tmp;
            if (j < i + 1 || tmp < 0)
                continue;
            e[k].i = i;
            e[k].j = j;
            e[k].weigt = tmp;
            k++;
        }
    }
    sort(e, e + k, edge());
    for (int i = 0; i < n; i++)
        g[i] = i;
    for (int i = 0; i < k; i++)
    {
        if (g[e[i].i] == g[e[i].j])
            continue;
        adj[e[i].i].push_back(e[i].j);
        adj[e[i].j].push_back(e[i].i);
        sum += e[i].weigt;
        //cout << e[i].i << " " << e[i].j << " " << e[i].weigt << "\n";
        if (++c == n - 1)
            break;
        CalcGraphNum();
    }

    cout << sum << "\n";
    return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  for(int i=0; i<n; i++){
    cin >> p.at(i);
  }
  int count = 0;
  for(int i=1; i<n-1; i++){
    if(p.at(i) > p.at(i-1) && p.at(i) < p.at(i+1)){
      count++;
    }
    else if(p.at(i) < p.at(i-1) && p.at(i) > p.at(i+1)){
      count++;
    }
  }
  cout << count <<endl;
}

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

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

  vector<string> S(N);
  for (int i=0; i<N; i++) {
    cin >> S.at(i);
  }
  
  int M;
  cin >> M;
  
  vector<string> T(M);
  for (int j=0; j<M; j++) {
    cin >> T.at(j);
  }
  
  int b=0, r=0;
  int ans=0;
  
  for (int i=0; i<N; i++) {
    b=0, r=0;
    for (int j=0; j<N; j++) {
      if (S.at(i)==S.at(j)) {
        b++;
      }
    }
    for (int k=0; k<M; k++) {
      if (S.at(i)==T.at(k)) {
        r++;
      }
    }
    if (ans<b-r) {
      ans=b-r;
    }
  }
  cout << ans << endl;
}

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

int P(long long p,long long T[], int n, int k)
{
	int i=0,j;
	long long s;
	for(j=0;j<=k-1;j++)
	{
		s=0;
		while(s+T[i]<=p)
		{
			s+=T[i];
			i++;
			if(i==n) return n;
		}
	}
	return i;
}


int main()
{
	int i;
	int n,k;
	long long T[100000];
	long long left=0;
	long long right=100000*10000;
	long long mid;
	int v;
	
	cin >> n;
	cin >> k;
	for(i=0;i<=n-1;i++) cin >> T[i];
	while(right-left>1)
	{
		mid=(right+left)/2;
		v=P(mid,T,n,k);
		if(v>=n) right=mid;
		else left=mid;
	}
	cout << right << endl;
	return 0;
}

Measured memory_bytes: 3868
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    void run() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), k = sc.nextInt();
        if (k == 1) {
            System.out.println(0);
        } else {
            System.out.println(n - k);
        }
    }

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

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


Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define mk make_pair
#define pb push_back
#define eps 1e-12
#define MAXN 200009
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,l;
	cin>>n>>l;
	int a[n+1];
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	bool flag=false;
	int j=-1;
	for(int i=1;i<n;i++)
	{
		if(a[i]+a[i+1]>=l)
		{
			flag=true;
			j=i;
			break;
		}
	}
	if(!flag)
	{
		cout<<"Impossible\n";
	}
	else
	{
		cout<<"Possible\n";
		for(int i=1;i<j;i++)
		{
			if(i!=j)
			{
				cout<<i<<"\n";
			}
		}
		for(int i=n-1;i>j;i--)
		{
			cout<<i<<"\n";
		}
		cout<<j<<"\n";
	}
}

Measured memory_bytes: 1280
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main(){
	int a,b,c;cin>>a>>b>>c;
	set<int> se;
	for(int i=a;i<=min(a+c-1,b);i++) se.insert(i);
	for(int i=max(a,b-c+1);i<=b;i++) se.insert(i);
	for(auto x:se) cout<<x<<"\n";
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <numeric>

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



int main() {
    int a,b,c; cin >> a >> b >> c;
    int x = max<int>({
        10*a + b + c,
        a + 10*b + c,
        a + b + 10*c
    });
    cout << x << endl;



    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#define _USE_MATH_DEFINES
#include<math.h>
#include<vector>
#include<array>
#include<algorithm>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<vector<ll>> mat;
typedef pair<ll,ll> P;
typedef map<ll,ll> MP;
typedef priority_queue<ll> PQ;
typedef stack<ll> ST;
typedef queue<ll> QUE;
#define pb push_back
#define st first
#define nd second
#define SZ(x) ((int)(x).size())
#define FOR(i,s,n) for(int i=(int)(s); i< (int)(n); i++)
#define rep(i,n) FOR(i,0,n)
#define all(x) (x).begin(),(x).end()
#define each(x,c) for(auto x: (c))
#define LB(x,a) lower_bound(all(x), a)
#define UB(x,a) upper_bound(all(x), a)
const ll mod=1e9+7;
const ll INF=1e18;
const double EPS=1.0e-10;
const double PI=M_PI;

int main(){
    int N, M; cin>>N>>M;
    ll sum=0;
    vec A(N);
    rep(i,N){
        cin>>A[i];
        sum+=A[i];
    }
    sort(all(A),greater<ll>());
    if(A[M-1]*4*M< sum) cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
    return 0;
}

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

#define rep2(x,fr,to) for(int x=(fr);x<(to);x++)
#define rep(x,to) for(int x=0;x<(to);x++)
#define repr(x,fr,to) for(int x=(fr);x>=(to);x--)
#define all(c) c.begin(),c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll; typedef vector<int> VI; typedef pair<ll,int> pii;
typedef vector<ll> VL; const int MD = (int)1e9 + 7;
void dbg(){ cerr << "\n"; } template <typename T,typename ...T2> void dbg(const T& fst, const T2&...rst){ cerr << fst << ": "; dbg(rst...); }
template <class T, class T2> void amax(T& a,T2 b){ if(a < b) a = b;}

ll gcd(ll x, ll y){ return y? gcd(y, x%y): x; }
int main()
{
	cin.tie(0); ios_base::sync_with_stdio(false);
	int n, m;
	cin >>n >>m;
	VI a(m);
	rep(i, m) cin >>a[i];
	const VI kt = {666, 2, 5, 5, 4, 5, 6, 3, 7, 6};
	vector<string> dp(n+2);

	rep(i, n)  rep(j, m) if(i==0 || sz(dp[i])){
		int su = i+kt[a[j]];
		string ns = dp[i]+ to_string(a[j]);
		if(su > n) continue;
		if((sz(dp[su]) < sz(dp[i])+1) || 
			(sz(dp[su])<=sz(dp[i])+1 && ns > dp[su])) dp[su] = ns;
	}
	cout <<dp[n] <<"\n";
	return 0;
}


Measured memory_bytes: 33024
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int main(){
    ll sum=0;
    string s; cin>>s;
    vector<ll> st;
    vector<pair<ll,ll>> res;
    rep(i,s.size()){
        if(s[i]=='/'){
            if(!st.empty()){
                sum+=i-st.back();
                pair<ll,ll> p={i-st.back(), st.back()};
                while(!res.empty() && res.back().second>=p.second){
                    p.first+=res.back().first;
                    res.pop_back();
                }
                res.push_back(p);
                st.pop_back();
            }
        }else if(s[i]=='\\') st.push_back(i);
    }
    cout<<sum<<endl;
    cout<<res.size();
    rep(i,res.size()){
        cout<<' '<<res[i].first;
    }
    cout<<endl;
    return 0;
}


Measured memory_bytes: 3476
import java.util.Scanner;

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

		int n = sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
		String s = sc.next();

		int numOfPass = 0;
		int rankB = 0;

		for (int i = 0; i < n; i++) {
			char c = s.charAt(i);
			boolean isPass = false;

			switch (c) {
			case 'a':
				if (numOfPass < a + b) {
					numOfPass++;
					isPass = true;
				}
				break;
			case 'b':
				rankB++;
				if (numOfPass < a + b && rankB <= b) {
					numOfPass++;
					isPass = true;
				}
				break;
			case 'c':
				break;
			default:
				break;
			}

			System.out.println(isPass ? "Yes" : "No");
		}

		sc.close();
	}

}


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

int main() {
  ll n, a;
  cin >> n;
  vector<ll> b;
  map<ll, ll> mp;
  rep(i, n) {
    cin >> a;
    mp[a]++;
    if (mp[a] % 2 == 0) b.push_back(a);
  }
  if (b.size() < 2) {
    cout << 0 << endl;
    return 0;
  }
  sort(b.rbegin(), b.rend());
  cout << b[0] * b[1] << endl;
  return 0;
}


Measured memory_bytes: 6528
#include<bits/stdc++.h>
using namespace std;
#define SORT(x) sort(x.begin(),x.end())
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
#define No cout<<"No"<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define YES cout<<"YES"<<endl;
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int main(){
    int n;cin>>n;
    int a[n];
    rep(i,n){
        cin>>a[i];
    }
    int ans=0,subans=0;
    bool grey=false,
        brown=false,green=false,water=false,blue=false,
        yellow=false,orange=false,red=false;
    for(int i=0;i<n;i++){
        if(a[i]>=3200){
            ans++;
        }
        if(!grey&&(a[i]>0&&a[i]<400)){
            ans++;grey=true;
            subans++;
        }
        if(!brown&&(a[i]>=400&&a[i]<800)){
            ans++;brown=true;
            subans++;
        }
        if(!green&&(a[i]>=800&&a[i]<1200)){
            ans++;green=true;subans++;
        }
        if(!water&&(a[i]>=1200&&a[i]<1600)){
            ans++;water=true;subans++;
        }
        if(!blue&&(a[i]>=1600&&a[i]<2000)){
            ans++;blue=true;subans++;
        }
        if(!yellow&&(a[i]>=2000&&a[i]<2400)){
            ans++;yellow=true;subans++;
        }
        if(!orange&&(a[i]>=2400&&a[i]<2800)){
            ans++;orange=true;subans++;
        }
        if(!red&&(a[i]>=2800&&a[i]<3200)){
            ans++;red=true;subans++;
        }
    }
    if(subans==0&&ans>0){subans=1;}
    cout<<subans<<' '<<ans<<endl;
    return 0;
}


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

const int dh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
    int H, W; cin >> H >> W;
    vector<string> s(H);
    for(int i = 0; i < H; i++) cin >> s[i];
    
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            int count = 0;
            if(s[i][j] == '#') continue;
            for(int d = 0; d < 8; d++) {
                int h = i + dh[d];
                int w = j + dw[d];
                if(h < 0 || H <= h || w < 0 || W <= w) continue;
                if(s[h][w] == '#') count++;
            }
            s[i][j] = '0' + count;
        }
    }
    for(int i = 0; i < H; i++) cout << s[i] << endl;
    return 0;
}

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

int main(){
    int N;
    vector<int> D;

    cin >> N;
    for (int i = 0; i < N; i++){
        int input;
        cin >> input;
        D.push_back(input);
    }

    if (0 != D[0]){
        cout << 0 << endl;
        return 0;
    }
    // 同じDの値を数え上げ
    vector<int> nums = {0};
    int max_d = 0;
    for (int i = 0; i < N; i++){
        int d = D[i];
        if (max_d < d) {
            // dの数までnumsを拡張
            int diff = d - max_d;
            for (int j = 0; j < diff; j++){
                nums.push_back(0);
            }
            max_d = d;
        }
        nums[d]++;
    }
    if (1 != nums[0]) {
        cout << 0 << endl;
        return 0;
    }
    long long temp = nums[0];
    int mod = 998244353;
    for (int i=1; i<=max_d; i++){
         // 累乗計算、しかし桁溢れ防止のため毎回modする
         if (nums[i] <= 0) {
             cout << 0 << endl;
             return 0;
         }
         for (int j=0; j < nums[i]; j++) {
             temp *= nums[i-1];
             temp %= mod;
         }
    }
    cout << temp << endl;
    return 0;
}

Measured memory_bytes: 1400
from operator import itemgetter
import sys
input = sys.stdin.readline


class BIT1():
    """
    Binary Indexed Tree (1-indexed)
    """
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (self.n + 1)
        self.data = [0] * (self.n + 1)

    def add(self, idx, x):
        # add x to idx-th element
        # idx: 1-indexed
        self.data[idx] += x
        while idx <= self.n:
            self.bit[idx] += x
            idx += idx & (-idx)

    def sum(self, idx):
        # get sum of [1, idx]
        # idx: 1-indexed
        s = 0
        while idx:
            s += self.bit[idx]
            idx -= idx & (-idx)
        return s


def main():
    n, q = map(int, input().split())
    c = list(map(int, input().split()))
    queries = []
    for i in range(q):
        l, r = map(int, input().split())
        l -= 1
        r -= 1
        queries.append((i, l, r))
    queries.sort(key=itemgetter(2))

    query_counter = 0
    ans = [0] * q
    # last_visit[i] = 色iの良い玉のindex
    last_visit = [-1] * (n + 1)
    # bit[i] = iの位置に何個の良い玉があるか
    bit = BIT1(n)
    for i, ci in enumerate(c):
        if last_visit[ci] != -1:
            bit.add(last_visit[ci] + 1, -1)

        last_visit[ci] = i
        bit.add(i + 1, +1)

        while query_counter < q and queries[query_counter][2] == i:
            idx, l, r = queries[query_counter]
            ans[idx] = bit.sum(r + 1) - bit.sum(l)
            query_counter += 1

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()


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

vector<vector<int> > G(100);
int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 0;i < m;i++){
		int x, y;
		scanf("%d%d", &x, &y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	for(int i = 1;i <= n;i++){
		printf("%d\n", G[i].size());
	}
	return 0;
}


Measured memory_bytes: 256
/**
 * @brief ユーザ定義リテラル
 * @author えびちゃん
 */

#ifndef H_int_literals
#define H_int_literals

#include <cstddef>
#include <cstdint>

constexpr intmax_t  operator ""_jd(unsigned long long n) { return n; }
constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; }
constexpr size_t    operator ""_zu(unsigned long long n) { return n; }
constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; }

#endif  /* !defined(H_int_literals) */

#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <string>

class rolling_hash_l61m1 {
public:
  using size_type = size_t;
  using value_type = uintmax_t;

private:
  value_type M_b;
  std::vector<value_type> M_c, M_p;
  static size_type const S_npos = -1;
  static value_type const S_mod = (1_ju << 61) - 1;

  static value_type S_fma(value_type a, value_type b, value_type c) {
    value_type au = a >> 31;
    value_type al = a & ((1u << 31) - 1);
    value_type bu = b >> 31;
    value_type bl = b & ((1u << 31) - 1);

    value_type x = au*bl + al*bu;
    value_type xu = x >> 30;
    value_type xl = x & ((1u << 30) - 1);

    value_type y = ((au*bu) << 1) + (xu + (xl << 31)) + (al*bl);
    value_type yu = y >> 61;
    value_type yl = y & ((1_ju << 61) - 1);

    value_type z = yu + yl + c;
    if (z >= S_mod) z -= S_mod;
    return z;
  }

  void M_build() {
    for (size_type i = 1; i < M_c.size(); ++i)
      M_c[i] = S_fma(M_c[i-1], M_b, M_c[i]);
    M_c.insert(M_c.begin(), 0);

    M_p.assign(M_c.size(), 1);
    for (size_type i = 1; i < M_p.size(); ++i)
      M_p[i] = S_fma(M_p[i-1], M_b, 0);
  }

public:
  rolling_hash_l61m1() = default;

  template <typename InputIt>
  rolling_hash_l61m1(InputIt first, InputIt last, value_type base): M_b(base) {
    assign(first, last);
  }

  template <typename InputIt>
  void assign(InputIt first, InputIt last) {
    M_c.assign(first, last);
    M_build();
  }

  template <typename InputIt>
  void assign(InputIt first, InputIt last, value_type base) {
    M_b = base;
    M_c.assign(first, last);
    M_build();
  }

  value_type substr(size_type pos, size_type len = S_npos) {
    size_type n = M_c.size() - 1;
    if (len == S_npos) len = n - pos;
    size_type endpos = pos + len;
    value_type hr = M_c[endpos];
    value_type hl = M_c[pos];
    value_type hs = hr - S_fma(hl, M_p[len], 0);
    if (hs >= S_mod)  // "negative"
      hs += S_mod;
    return hs;
  }
};

#include <random>
#include <set>

int main() {
  size_t n;
  scanf("%zu", &n);

  char buf[5001];
  scanf("%s", buf);
  std::string s = buf;

  std::random_device rd;
  rolling_hash_l61m1 rh(s.begin(), s.end(), rd());

  size_t res = 0;
  for (size_t i = 0; i < n; ++i) {
    std::set<uintmax_t> seen;
    for (size_t j = i; i+j <= n; ++j) {
      uintmax_t h0 = rh.substr(j-i, i);
      seen.insert(h0);
      uintmax_t h1 = rh.substr(j, i);
      if (seen.count(h1)) res = i;
    }
  }
  printf("%zu\n", res);
}

Measured memory_bytes: 512
import java.util.*;
 
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    String S = sc.next();
    int count = 0;
    String ABC;
    
    for (int i = 0; i < N-2 ;i++) {
      ABC = S.substring(i, i + 3);
      if (ABC.equals("ABC")){
        count++;
      }
    }
    System.out.print(count);
  }
}

Measured memory_bytes: 23508
L = input()

MOD = 10**9 + 7

# i: 決定した桁数
# j: a+b < L[i]を満たすか
dp = [[0] * 2 for _ in range(len(L) + 1)]

dp[0][0] = 1

for i in range(len(L)):
    for j in range(2):
        # (a,b) = (0,0)
        if int(L[i]):
            dp[i+1][1] += dp[i][j]
            dp[i+1][1] %= MOD
        else:
            dp[i+1][j] += dp[i][j]
            dp[i+1][j] %= MOD

        # (a,b) = (0,1), (1,0)
        if int(L[i]) or j:
            dp[i+1][j] += dp[i][j] * 2
            dp[i+1][j] %= MOD

print((dp[-1][0] + dp[-1][1]) % MOD)

Measured memory_bytes: 19708
#include<iostream>
main(){int n,m,i,a,c[1000001];for(std::string s;std::cin>>n>>m>>s,n;printf("%d\n",a))for(a=0,i=m;--i;a+=c[i]>=n)c[i]=s.substr(i,3)=="IOI"?c[i+2]+1:0;}

Measured memory_bytes: 5820
#include <algorithm>
#include <bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;
#define rep(i, n) for (int i = 0; i <(int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
const int INF=1000100010;
int main(){
    int a,b,c,d;cin>>a>>b>>c>>d;
    int taka=(c+b-1)/b;
    int aoki=(a+d-1)/d;
    if(taka>aoki)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
   
}

Measured memory_bytes: 3640
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
  char a[10], b[4];
  int n;
  int i;
  scanf("%s", a);
  scanf("%s", b);
  strcat(a, b);
  n = atoi(a);
  for(i = 0; n >= i * i; i++){
    if(n == i * i){
      printf("Yes\n");
      return 0;
    }
  }
  printf("No\n");
  return 0;
}

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

// --------------------------------------------------------------------------------
#define FASTIO	ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long 
#define mset(arr,val) memset(arr,val,sizeof(arr))
#define frepi(a,b) for(int i=a;i<=b;++i)
// #define int long long
// --------------------------------------------------------------------------------

void test_case(int tc)
{
	int n;cin>>n;
	int arr[n];
	for(int i=n-1;i>=0;--i)cin>>arr[i];
	int maxima = arr[n-1];
	ll ans = 0;
	for(int i=n-2;i>=0;--i)
	{
		ans+=max(0,maxima-arr[i]);
		maxima = max(arr[i],maxima);
	}
	cout<<ans<<"\n";
}

int32_t main()
{
	FASTIO;
	#ifndef ONLINE_JUDGE
		freopen("/home/suryakant/input.txt","r",stdin);
		freopen("/home/suryakant/output.txt","w",stdout);
	#endif
	
    int tc=1;
    //cin>>tc;
    for(int i=1;i<=tc;++i)test_case(tc);
}

Measured memory_bytes: 4408
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
	//freopen("1.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, S;
    cin >> n >> S;
    vector<pair<int, int> > sushi;
    for (int i=0; i < n; i++){
		int x, y;
		cin >> x >> y;
		sushi.push_back(make_pair(x, y));
    }
    int ans = 0, ccal=0;
    int pref[n+1], suff[n+1];
    pref[0] = 0, suff[n] = 0;
    for (int i=0; i < n; i++){
        ccal += sushi[i].second;
        pref[i+1] = max(pref[i], ccal - sushi[i].first);
    }
    ccal=0;
    for (int i=n-1; i >= 0; i--){
		ccal += sushi[i].second;
		suff[i] = max(suff[i+1], ccal - S+sushi[i].first);
    }
    ans = max(ans, pref[n]);
    ans = max(ans, suff[0]);
    ccal=0;
    for (int i=0; i < n; i++){
		ccal += sushi[i].second;
		int tr = ccal - 2*sushi[i].first;
		ans = max(ans, tr + suff[i+1]);
    }
    ccal = 0;
    for (int i=n-1; i >= 0; i--){
		ccal += sushi[i].second;
		int tr = ccal - (2*S - 2*sushi[i].first);
		ans = max(ans, tr+pref[i]);
    }
    cout << ans;
}


Measured memory_bytes: 3568
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <limits>
#include <string>
#include <algorithm>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include "math.h"
#include "assert.h"

using namespace std;

#define ll long long

#define BUFF ios::sync_with_stdio(false)
#define epsilon 1e-07
#define pi 2 * acos(0.0);
#define mod 1000000007   // 1e9+7
#define cerr if(false)cerr
#define watch(x) cerr << #x << " = " << x << endl;

#define inf std::numeric_limits<int>().max();

struct S {
   int a, b, idx;
	bool operator<(const S& j) const { 
		if (a == j.a) return (b < j.b);
		else return (a < j.a);
	}
};

int main() {
	int n, a, b;
	vector<S> t;
	cin >> n;
	for(int i=0; i<n-1; i++) {
		cin >> a >> b; 
		S s;
		s.a = a;
		s.b = b;
		s.idx = i;
		t.push_back(s);
	}
	sort(t.begin(), t.end());

	vector<int> cv(n+1, 0);
	vector<int> ans(n-1);

	int cor = 1;
	int origem = 1;
	int total = 1;
	for(int i=0; i<n-1; i++) {
		if (t[i].a != origem) {
			origem = t[i].a;
			cor=1;
		}

		if (cor == cv[origem]) cor++;

		ans[t[i].idx] = cor; 
		cv[t[i].b] = cor;
		cor++;
		total = max(total, cor);
	}

	cout << total-1 << endl;
	for(int i=0; i<n-1; i++) {
		cout << ans[i] << endl;
	}
}


Measured memory_bytes: 2420
#include<bits/stdc++.h>
using namespace std;
#define de(x) cout<<#x<<" = "<<x<<endl
#define sz(a) (int)(a.size())
#define mp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
int main() {
    int a,b,c;
    while(~scanf("%d%d%d",&a,&b,&c)) {
        if(a==b) {
            printf("%d\n",c); continue;
        }
        if(a==c) {
            printf("%d\n",b); continue;
        }
        if(c==b) {
            printf("%d\n",a); continue;
        }

    }
    return 0;
}


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

int main()
{
        char num[1000];
        int total;

        while(scanf("%s\n", num)){
                if(num[0]=='0'){break;}
                total = 0;
                for(int i=0; num[i]!=0; i++){
                        total += num[i]-'0';
                }
                cout << total << endl;
        }

        return 0;
}

Measured memory_bytes: 1156
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Map;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author MaxHeap
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        long[] w;
        long[] v;
        Map<String, Long> memo;
        int n;
        int W;

        long f(int i, long weight) {
            if (i == n) return 0;
            String key = i + " " + weight;
            if (memo.containsKey(key)) return memo.get(key);
            long ans = 0;
            if (weight + w[i] <= W) {
                ans = Math.max(ans, f(i + 1, weight + w[i]) + v[i]);
            }
            ans = Math.max(ans, f(i + 1, weight));
            memo.put(key, ans);
            return ans;
        }

        public void solve(int testNumber, FastReader in, PrintWriter out) {
            n = in.nextInt();
            W = in.nextInt();
            w = new long[n];
            v = new long[n];
            memo = new HashMap<>();
            for (int i = 0; i < n; i++) {
                w[i] = in.nextInt();
                v[i] = in.nextInt();
            }

            out.println(f(0, 0));
        }

    }

    static class FastReader {
        BufferedReader reader;
        StringTokenizer st;

        public FastReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            st = null;
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String line = reader.readLine();
                    if (line == null) {
                        return null;
                    }
                    st = new StringTokenizer(line);
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

    }
}



Measured memory_bytes: 49468
#include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (n); i ++)
#define re0(i, n) for (rint i = 0; i < (int) n; i ++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;

typedef long long lo;

template<typename tp> inline void read(tp &x) {
    x = 0; char c = getchar(); int f = 0;
    for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
    for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
    if (f) x = -x;
}
const int N = 3e5 + 233;
int sg[N];
int n, ans;
vector <int> G[N];

inline int dfs(int u, int fat) {
	int mx = 0, se = 0;
	for (int v : G[u])
		if (v != fat) {
			int t = dfs(v, u) + 1;
			if (t >= mx) se = mx, mx = t;
			else if (t >= se) se = t;
		}
	ans = max(ans, mx + se);
	return mx;
}

int main(void) {
	sg[0] = 0; sg[1] = 1; sg[2] = 0;
	for (int i = 3; i < N; i++) {
		set <int> cur;
		cur.insert(sg[i - 1]);
		cur.insert(sg[i - 2]);
		int t = 0;
		while (cur.count(t)) ++t;
		sg[i] = t;
	}
	read(n);
	rep (i, n - 1) {
		int x, y; read(x); read(y);
		G[x].push_back(y); G[y].push_back(x);
	}
	dfs(1, 0);
	++ans;
	cout << (sg[ans] ? "First" : "Second") << "\n";
}


Measured memory_bytes: 20736
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
#include <numeric>
#include <fstream>
#include <iomanip>

typedef long double ld;
typedef long long ll;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1e9 + 7;


namespace util {

// 文字列のSplit
std::vector< std::string > split(std::string s, char delimiter)
{
  std::vector< std::string > vs;
  std::string sub;
  for (auto c : s) {
    if (c == delimiter) vs.push_back(sub), sub.clear();
    else sub += c;
  }
  vs.push_back(sub);
  return vs;
}

ll pow(ll x, ll n)
{
  ll ret = 1LL;
  while (n > 0) ret *= x, n--;
  return ret;
}
// 桁和
ll digitSum(ll x)
{
  std::string s = std::to_string(x); ll ret = 0LL;
  for (auto c : s) {ret += c - '0';}
  return ret;
}

} // namespace util

// Minimum, Maximum
template<class T> T minimum(T head, T tail) { return std::min(head, tail); }
template<class H, class... T> H minimum(H head, T... tail) { return std::min(head, minimum(tail...)); }
template<class T> T maximum(T head, T tail) { return std::max(head, tail); }
template<class H, class... T> H maximum(H head, T... tail) { return std::max(head, maximum(tail...)); }

// Output
template<class T, class S> std::ostream& operator <<(std::ostream& os, std::pair<T, S> p)
{
  return os << "(" << p.first << " " << p.second << ")";
}

template<class T> std::ostream& operator <<(std::ostream& os, std::vector< T > v)
{
  for (ll i = 0; i < (ll)v.size(); i++){ os << " [" << i << "]" << v[i]; if (i % 10 == 9) os << std::endl; }
  //os << v[0]; for (ll i = 1; i < (ll)v.size(); i++){ os << " " << v[i]; }
  return os;
}

template<class T, class S> std::ostream& operator <<(std::ostream& os, std::map< T, S > m)
{
  ll i = 0; for (auto p : m){ os << " [" << i << "]" << p.first << "->" << p.second; i++;}
  return os;
}

template<class T> std::ostream& operator <<(std::ostream& os, std::vector< std::vector<T> > vv)
{
  for (auto& vec : vv){ os << vec << std::endl; }
  os << "--------" << std::endl;
  return os;
}

void print(){ std::cout << std::endl; }
template<typename H> void print(H&& head){ std::cout << head << std::endl; }
template<typename H, typename... T> void print(H&& head, T&&... tail){
  std::cout << head << " ", print(std::forward<T>(tail)...);
}

// GCD, LCM
ll gcd(ll a, ll b) {
  while (b != 0) { a %= b; std::swap(a, b); }
  return a;
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

// DIGNUM
std::vector<ll> dignum(ll x) {
  if (x == 0) return {0};
  std::vector<ll> res;
  while (x) {
    res.push_back(x % 10);
    x /= 10;
  }
  std::reverse(res.begin(), res.end());
  return res;
}

// FAIL
void fail(){ print(-1); std::exit(0); }
//void fail(){ print("No"); std::exit(0); }

int main()
{
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  std::ios::sync_with_stdio(false);

  ll N, P;
  std::cin >> N >> P;

  std::map<ll, ll> mp;
  for (ll i = 2; i * i <= P; i++) {
    while (P % i == 0) {
      P /= i;
      mp[i]++;
    }
  }
  if (P != 1) mp[P]++;

  ll ans = 1LL;
  for (auto p : mp) {
    ans *= util::pow(p.first, p.second / N);
  }

  print(ans);
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cmath>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

long long m = 1000000007;

long long pow3(long long exp)
{
    if(exp<0) return 0;
    long long ans =1;
    for(long long i=0; i<exp; i++)
    {
        ans=(ans*3);
        if(ans >= 270000000000) ans = ans %m;
    }
    return ans%m;
}

int main()
{
    string s;
    cin>>s;
    long long a[s.length()];//a is not from '?' 
    long long qa[s.length()];//a is from '?' 
    long long ab[s.length()];//non of ab is from '?'
    long long qab[s.length()];//one of ab is from '?' 
    long long qqab[s.length()];//both ab is from '?'
    long long abc[s.length()];
    long long qabc[s.length()];
    long long qqabc[s.length()];
    long long qqqabc[s.length()];
    long long q_count=0;
    if(s[0]=='A') a[0]=1;
    else a[0]=0;
    if(s[0]=='?') qa[0]=1;
    else qa[0]=0;
    ab[0]=0;
    qab[0]=0;
    qqab[0]=0;
    abc[0]=0;
    qabc[0]=0;
    qqabc[0]=0;
    qqqabc[0]=0;
    if(s[0]=='?') q_count++;
    for(int i=1; i<s.length(); i++)
    {
        //update a[i]
        a[i] = a[i-1];
        if(s[i]=='A') a[i]++;
        a[i]= a[i]%m;
        //update qa[i]
        qa[i] = qa[i-1];
        if(s[i]=='?') qa[i]++;
        qa[i]= qa[i]%m;
        
        //update ab[i]
        ab[i] = ab[i-1];
        if(s[i]=='B') ab[i]+=a[i-1];
        ab[i]= ab[i]%m;
        
        //update qab[i]
        qab[i] = qab[i-1];
        if(s[i]=='B') qab[i]+=qa[i-1];
        if(s[i]=='?') qab[i]+=a[i-1];
        qab[i]= qab[i]%m;
        
        //update qqab[i]
        qqab[i] = qqab[i-1];
        if(s[i]=='?') qqab[i]+=qa[i-1];
        qqab[i]= qqab[i]%m;
        
        //update abc[i]
        abc[i] = abc[i-1];
        if(s[i]=='C') abc[i]+= ab[i-1];
        abc[i]= abc[i]%m;
        //update qabc[i]
        qabc[i] = qabc[i-1];
        if(s[i]=='C') qabc[i]+= qab[i-1];
        if(s[i]=='?') qabc[i]+= ab[i-1];
        qabc[i]= qabc[i]%m;
        //update qqabc[i]
        qqabc[i] = qqabc[i-1];
        if(s[i]=='C') qqabc[i]+= qqab[i-1];
        if(s[i]=='?') qqabc[i]+= qab[i-1];
        qqabc[i]= qqabc[i]%m;
        //update qqqabc[i]
        qqqabc[i] = qqqabc[i-1];
        if(s[i]=='?') qqqabc[i]+= qqab[i-1];
        qqqabc[i]= qqqabc[i]%m;
        
        if(s[i]=='?') q_count++;
        //cout<<a[i]<<"\t"<<ab[i]<<"\t"<<abc[i]<<endl;
    }
    cout<<((abc[s.length()-1]*pow3(q_count)+ qabc[s.length()-1]*pow3(q_count-1)+ qqabc[s.length()-1]*pow3(q_count-2) + qqqabc[s.length()-1]*pow3(q_count-3)))%m<<endl;
	return 0;
}


Measured memory_bytes: 7552
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int lim = 1e5+5;

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

	int r,g,b,n,ans=0;
	cin>>r>>g>>b>>n;
	for(int y=0;y<=n;y++)
		for(int z=0;z<=n;z++)
		{
			int req = n - y*b - z*g;
			if(req>=0 && req%r==0)
				ans++; 
		}
	cout<<ans;

    return 0;
}

/*
x = N - y - z;
0<=x<=R
0<=y<=G
0<=z<=B
*/

Measured memory_bytes: 256
#include <stdio.h>
int main(){
int a,b,i,d;
  
while(1){
scanf("%d %d",&a,&b);
if(a==0 && b==0)break;
    for(i=0;i<a;i++){
        for(d=0;d<b;d++){
            printf("#");
        } 
    printf("\n");
    }
printf("\n");
}
return 0;
}


Measured memory_bytes: 2036
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define all(i,n) (i),(i+n)

int dx4[4]={1,0,-1,0};
int dy4[4]={0,-1,0,1};
int dx8[8]={1,0,-1,1,-1,1,0,-1};
int dy8[8]={1,1,1,0,0,-1,-1,-1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6+1;
const ll mod = 1000000007;

// --------------------------------------
int n, m, k;

class UnionFind {
public:
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}

  int find(int x) {
    return data[x] < 0 ? x : data[x] = find(data[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x != y) {
      if(data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
  int size(int x) {
    return -data[find(x)];
  }
};

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

  cin >> n >> m >> k;
  vector<vector<int>> fs(n + 1);
  vector<vector<int>> bs(n + 1);
  UnionFind uf(n + 1);

  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    uf.unite(a, b);
    fs[a].push_back(b);
    fs[b].push_back(a);
  }

  REP(i, k) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    bs[a].push_back(b);
    bs[b].push_back(a);
  }

  REP(i, n) {
    int sum = uf.size(i) - 1;
    sum -= fs[i].size();
    int r = uf.find(i);
    REP(j, bs[i].size()) {
      if(r == uf.find(bs[i][j])) sum--;
    }

    cout << sum << " ";
  }
  cout << endl;
}


Measured memory_bytes: 12280
#include <iostream>

int main() {
  int a, b, k;
  std::cin >> a >> b >> k;

  int count = 0;
  for (int i = std::min(a, b); i >= 1; --i) {
    if (a % i == 0 && b % i == 0) {
      ++count;
    }
    if (count == k) {
      std::cout << i << std::endl;
      break;
    }
  }
}


Measured memory_bytes: 504
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    vector<double> v(N);

    priority_queue<double, vector<double>, greater<double>> que;
    REP(i,N) {
        cin >> v[i];
        que.push(v[i]);
    }

    REP(i,N-1){
        double a,b;
        a = que.top(); que.pop();
        b = que.top(); que.pop();
        que.push((a+b)/2.0);
    }
    cout << que.top() << endl;


}


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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define mp(a,b) make_pair((a),(b))
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int INF=1<<30;
const long long int INF_=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

void Solve(){
	int n,k;
	cin >> n >> k;

	vector<vector<int>> boats(k);
	vector<bool> flag(n);
	rep(i,0,k){
		int m; cin >> m;
		rep(j,0,m){
			int b; cin >> b;
			--b;
			boats[i].push_back(b);
		}
	}

	int r;
	cin >> r;
	rep(i,0,r){
		int p,q;
		cin >> p >> q;
		--p; --q;
		rep(j,0,k){
			if(find(boats[j].begin(),boats[j].end(),p)!=boats[j].end()&&find(boats[j].begin(),boats[j].end(),q)!=boats[j].end()){
				flag[p]=flag[q]=true;
				break;
			}
		}
	}

	int ans=0;
	rep(i,0,n) ans+=flag[i];

	cout << ans << endl;
	
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	Solve();
	return 0;
}

Measured memory_bytes: 1220
import std.stdio, std.algorithm;


/** Warshall-Floyd algorithm to compute min cost

Parameters:
    d = path cost matrix. if a -> b path is not available: d[a][b] == T.max
        this function overwrites d to have min path cost.
 */
void minPathCost(T)(T[][] d) {
    debug foreach (x; d) {
        assert(x.length == d.length);
    }
    auto N = d.length;
    foreach (k; 0 .. N) {
        foreach (i; 0 .. N) {
            foreach (j; 0 .. N) {
                if (d[i][k] == T.max || d[k][j] == T.max) continue;
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

///
unittest {
    ulong[][] d = [
        [0, 10, 2],             // a -> b = 10,   a -> c = 2
        [1, 0, 5],              // b -> a = 1,    b -> c = 5
        [ulong.max, 5, 0]         // c -> a = none, c -> b = 5
    ];
    minPathCost(d);
    // min (b -> c) = (b -> a -> c) = 1 + 2
    assert(d[1][2] == 3);
    // min (c -> b) = (c -> b) = 5
    assert(d[2][1] == 5);
    // min (c -> a) = (c -> b -> a) = 5 + 1
    assert(d[2][0] == 6);
}


void main() {
    ulong n, m, l;
    readf("%d %d %d\n", &n, &m, &l);
    auto dist = new ulong[][](n, n);
    foreach (i, d; dist) {
        d[] = ulong.max;
        d[i] = 0;
    }
    foreach (i; 0 .. m) {
        ulong a, b, c;
        readf("%d %d %d\n", &a, &b, &c);
        dist[a-1][b-1] = c;
        dist[b-1][a-1] = c;
    }

    minPathCost(dist);

    // convert dist to flags of accecible within l steps
    foreach (i, d; dist) {
        foreach (j, ref x; d) {
            if (i == j) continue;
            x = x <= l ? 1 : ulong.max;
        }
    }
    minPathCost(dist);
    
    ulong q;
    readf("%d\n", &q);
    foreach (i; 0 .. q) {
        ulong s, t;
        readf("%d %d\n", &s, &t);
        auto d = dist[s-1][t-1];
        if (d == ulong.max) {
            writeln(-1);
        } else {
            writeln(d - 1);
        }
    }
}


Measured memory_bytes: 3708
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <bits/stdc++.h>
#include<queue>
#include <numeric>
using namespace std;
 
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
 
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
 template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
 
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define fore(i,a) for(auto &i:a)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
 
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
 
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
 

LL gcd(LL a, LL 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;
}

int main(){
    int N;
    cin >> N;
    int A[N];
    for(int i=0;i<N;++i){
        cin >> A[i];
    }
    int init_num=0;
    int count=0;
    int j=0;
    int num_min=0;
    for(int i=0;i<N;++i){
        num_min=i;
        for(j=i;j<N;++j){
            if(A[j] < A[num_min]){
                num_min = j;
            }
        }
        if(A[i]>A[num_min]){
            ++count;
        }
        swap(A[i], A[num_min]);
    }
    for(int i=0;i<N;++i){
        if(i!=N-1){
            cout << A[i] << " ";
        }
        else{
            cout << A[i] << endl;
        }
    }
    cout << count << endl;
}


Measured memory_bytes: 3096
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
int N,K,c[26];
int main(){
	cin>>N>>K;
	rep(i,N){
		string s;
		cin>>s;
		c[s[0]-'A']++;
	}
	int ans=0;
	while(true){
		sort(c,c+26,greater<int>());
		rep(i,K){
			if(c[i]==0){
				cout<<ans<<endl;
				return 0;
			}
			c[i]--;
		}
		ans++;
	}
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main() {
    const int MOD = 1000000007;
    
    long long N, K;
    cin >> N >> K;
    
    vector<long long> kaijou(2010, 1);
    for(int i = 1; i <= 2000; i++) kaijou.at(i) = kaijou.at(i-1)*i%MOD;
    
    for(int i = 1; i <= K; i++) {
        if(N-K+1 < i) {
            cout << 0 << endl;
            continue;
        }
        long long bunbo = kaijou.at(N-K+1) * kaijou.at(K-1) % MOD;
        long long bunshi = kaijou.at(N-K+1-i) * kaijou.at(i) % MOD * kaijou.at(K-i) % MOD * kaijou.at(i-1) % MOD;
        cout << bunbo * modinv(bunshi, MOD) % MOD << endl;
    }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;

bool con[42][42];
int n;
LL dp[42][42][42];

LL dfs(int l,int r,int mid)
{
	if(dp[l][r][mid]!=-1)return dp[l][r][mid];
	if(l==r)return 1;
	else if(l==mid||mid==r)return 0;
	LL&sum=dp[l][r][mid];sum=0;
	
	for(int x=l;x<mid;x++)for(int y=mid+1;y<=r;y++)if(con[x][y])
	{
		for(int lb=x;lb<mid;lb++)for(int rb=mid+1;rb<=y;rb++)
			sum+=dfs(l,lb,x)*dfs(lb+1,rb-1,mid)*dfs(rb,r,y);
	}
	return sum;
}

int main()
{
	scanf("%d",&n);n*=2;
	for(int i=1;i<=n;i++)
	{
		char s[42];scanf("%s",s+1);
		for(int j=1;j<=n;j++)con[i][j]=s[j]-'0';
	}
	memset(dp,-1,sizeof(dp));
	
	LL ans=0;
	for(int i=1;i<=n;i++)if(con[1][i])ans+=dfs(2,n,i);
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 768
#include <stdio.h>

int main(void)
{
    int n;
    int a;
    int b;
    int i,j;
    int tmp;
    int sum,cnt;
    int rslt;

    //initialize
    rslt = 0;

    scanf("%d %d %d", &n, &a, &b);
    
    for(i=1; i<=n; i++){
        sum = 0; 
        tmp = i;
        for(j=0; j<5; j++){
            sum += tmp%10;
            tmp /=10;
        }
        if( (a<=sum) && (sum<=b) ){
                rslt += i;
        }
    }
    printf("%d\n", rslt);
}

Measured memory_bytes: 128