akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#include<cmath>
#include<bitset>
#include<queue>
#define ll long long
#define itn int
#define co(ans) cout<<ans<<endl;
#define COYE cout<<"YES"<<endl;
#define COYe cout<<"Yes"<<endl;
#define COye cout<<"yes"<<endl;
#define CONO cout<<"NO"<<endl;
#define CONo cout<<"No"<<endl;
#define COno cout<<"no"<<endl;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define INF ((1LL<<62)-(1LL<<31))
#define MOD 1000000007
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; }
using namespace std;


int main(){
  ll X,Y,A,B,C;
  cin>>X>>Y>>A>>B>>C;
  
  vector<ll> P,Q,PQR;
  
  REP(i,A){
    ll a;
    cin>>a;
    P.push_back(a);
  }
  
  REP(i,B){
    ll a;
    cin>>a;
    Q.push_back(a);
  }
  
  REP(i,C){
    ll a;
    cin>>a;
    PQR.push_back(a);
  }
  
  
  SORT(P);
  REVERSE(P);
  SORT(Q);
  REVERSE(Q);
  
  REP(i,X) PQR.push_back(P.at(i));
  REP(i,Y) PQR.push_back(Q.at(i));
  
  
  SORT(PQR);
  REVERSE(PQR);
  
  ll ans=0;
  
  for(int i=0; i<X+Y; ++i){
    ans+=PQR.at(i);
  }
  
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 7920
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int a[9]= {0},i,j,n,tp;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>tp;
        if(tp<1)
            continue;
        if(tp<400)
            a[0]++;
        else if(tp<800)
            a[1]++;
        else if(tp<1200)
            a[2]++;
        else if(tp<1600)
            a[3]++;
        else if(tp<2000)
            a[4]++;
        else if(tp<2400)
            a[5]++;
        else if(tp<2800)
            a[6]++;
        else if(tp<3200)
            a[7]++;
        else if(tp>=3200&&tp<=4800)
            a[8]++;
    }
    int num=0;
    for(i=0; i<8; i++)
        if(a[i]!=0)
            num++;
    if(num!=0)
        cout<<num<<" "<<num+a[8];
    else
        cout<<1<<" "<<num+a[8];
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <utility>
#include <numeric>
#include <queue>
#include <stack>
#include <iomanip>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

int main(void) {
	string s, t;
	cin >> s >> t;
	
	int slen = (int)s.size();
	int tlen = (int)t.size();
	
	vector< vector<int> > p(26);
	
	for(int i = 0; i < slen; ++i) {
		int c = (int)(s[i] - 'a');
		p[c].push_back(i);
	}
	
	for(auto& c : t) {
		int x = (int)(c - 'a');
		
		if( p[x].empty() ) {
			cout << -1 << endl;
			return 0;
		}
	}
	
	ll ans = 0LL;
	for(int i = 0; i < tlen; ++i) {
		int x = (int)(t[i] - 'a');
		int v = lower_bound(p[x].begin(), p[x].end(), ans % slen) - p[x].begin();
		
		if( v == (int)p[x].size() ) {
			ans = ((ans + slen - 1) / slen) * slen;
			
			int v2 = lower_bound(p[x].begin(), p[x].end(), ans % slen) - p[x].begin();
			
			ans += p[x][v2] - ans % slen;
		} else {
			ans += p[x][v] - ans % slen;
		}
		
		ans++;
	}
	
	cout << ans << endl;
	
	return 0;
}


Measured memory_bytes: 4172
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);i++)
const int MOD=(int)1e9+7;
using namespace std;
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int main(){
    int n,x,ans=0;
    cin>>n>>x;
    vector<int> v(n);
    REP(i,n) cin>>v[i],v[i]-=x;
    ans=abs(v.at(0));
    REP(i,n)ans=gcd(ans,abs(v[i]));
    cout<<ans<<endl;
}

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

int main()
{
    ll n;
    cin>>n;
    ll a[n+1];
    for(ll i=1;i<=n;i++)
        cin>>a[i];
    ll b[n+1];
    for(ll i=1;i<=n;i++)
        cin>>b[i];
    ll c[n];
    for(ll i=1;i<=n-1;i++)
        cin>>c[i];
        ll sum=0;
        ll last=-1;
    for(ll i=1;i<=n;i++)
    {
        //cout<<"ram\n";
        if(i==1)
        {
            sum+=b[a[i]];
            last=a[i];
        }
        else
        {
            sum+=b[a[i]];
            if(last==a[i]-1)
                sum+=c[a[i]-1];
            last=a[i];
        }
    }
    cout<<sum<<endl;
}


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

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

double avg = double(sum) / double(N);

int ans1 = 0;
int ans2 = 0;
int avg1,avg2;
avg1 = ceil(avg);
avg2 = floor(avg);
for (int i = 0; i < N; i++) {
    ans1 += (x[i] - avg1)*(x[i] - avg1);
    ans2 += (x[i] - avg2)*(x[i] - avg2);
}
cout << fixed << setprecision(10) << min(ans1,ans2) << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
constexpr ll INF = (1LL << 60);

int main(){ 
  int n;
  string s;
  cin >> n >> s;

  set<char> st;
  int res = 0;
  for (int i = 0; i < n; i++) {
    st.insert(s[i]);
    int tmp = 0;
    vector<int> ok(26, 0);
    for (int j = i + 1; j < n; j++) {
      if (ok[s[j] - 'a']) continue;
      tmp += (st.count(s[j]));
      ok[s[j] - 'a'] = 1;
    }
    res = max(res ,tmp);
  }
  cout << res << endl;
  return 0;
} 

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
const ll inf = 1e18 ;

int main(){
    int a , b , c ;
    cin >> a >> b >> c ;
    int d = a % b ;
    int e = a  ;
    while(1){
        if( c == e%b ){
            cout << "YES" << endl;
            return 0 ;
        }
        e += a ;
        if( e%b == d ){
            cout << "NO" << endl;
            return 0 ;
        }
    }
}

Measured memory_bytes: 256
use proconio::input;
use proconio::marker::{Bytes, Chars};
use std::iter::Iterator;

fn main() {
    input! {
        n:i32,
        m:i32,
        a: [i32; m],
    }
   
    let mut sum = 0;
    for i in 0..m {
        sum = sum + a[i as usize];
    }

    if sum > n {
        println!("-1")
    }else{
        println!("{}", n - sum);
    }
   
}


Measured memory_bytes: 2236
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

ans = 0
S = input()
n = len(S)
def dfs(i, ans_string, tot):
    if i == n-1:
        if tot == 7: 
            print(ans_string+"=7") 
            return True 
        else: return False
    return dfs(i+1, ans_string +"+" + S[i+1], tot + int(S[i+1])) \
     or dfs(i+1, ans_string + "-" + S[i+1], tot-int(S[i+1]))

dfs(0,S[0],int(S[0]))


Measured memory_bytes: 12496
#include<cstdio>
using namespace std;
int maxx=0,minn=2e9,N;
int main()
{
	scanf("%d",&N);
	for(int i=1,a;i<=N;i++)
	{
		scanf("%d",&a);
		if(a<minn)
		minn=a;
		if(a>maxx)
		maxx=a;
	}
	printf("%d",maxx-minn);
	return 0;
}

Measured memory_bytes: 128
N=int(input())
K=int(input())
xlis=[int(x) for x in input().split(' ')]
sums=0
for i in range(N):
	ball=xlis[i]
	arob=ball*2
	brob=abs(K-ball)*2
	sums+=min([arob,brob])
print(sums)

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

int main(int argc, char const *argv[])
{
	long long x,y;
	cin>>x>>y;
	long long now=x,ans=0LL;
	while (now<=y)
	{
		++ans;
		now*=2LL;
	}
	cout<<ans<<endl;
	return 0;
}


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

int main ()
{
    int N;
    int A[1010];
    cin >> N ;
    for(int k=0;k<N;k++){
        cin >> A[k];
    }
    int v,j;
    bool flag=1;
    int cnt=0;
    while(flag){
        flag=0;
        for(int j=N-1;j>=1;j--){
            if(A[j]<A[j-1]){
                v=A[j];
                A[j]=A[j-1];
                A[j-1]=v;
                flag=1;
                cnt++;
            }
        }
    }
    for(int k=0;k<N;k++){
        if(k==N-1) cout << A[k] << endl;
        else if(k!=N-1) cout << A[k] << " ";
    }
    cout << cnt << endl;
}










Measured memory_bytes: 3092
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <string>
#include <numeric>
#include <algorithm>
#include <utility>


using std::cout;
using std::cin;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;


ll gcd(ll a, ll b) {
    while (b != 0) {
        auto tmp = a;
        a = b;
        b = tmp % a;
    }
    return a;
}

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


void oneCase () {
    int a, b;
    char op;
    cin >> a >> op >> b;
    if (op == '+') {
        cout << a + b;
    } else {
        cout << a - b;
    }
    cout << "\n";
}





int main() {
    int t = 1;
//    cin >> t;
    while (t > 0) {
        oneCase();
        --t;
    }

    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int mod = 1e9+7;
int del(int x,int y)
{
    return (x+mod-y)%mod;
}
int add(int x,int y)
{
    return (x+y)%mod;
}
int mul(int x,int y)
{
    return 1ll*x*y%mod;
}
int n,x,y,z,bas,sta,f[50][1<<17],cur;

signed main()
{
    scanf("%d%d%d%d",&n,&x,&y,&z);
    f[cur=0][0]=1;
    int ans=1;
    bas = ((1<<(z-1))|(1<<(y+z-1))|(1<<(x+y+z-1)));
    sta = (1<<(x+y+z))-1;
    for(int i=1;i<=n;++i)
    {
        cur^=1;
        memset(f[cur],0,sizeof f[cur]);
        ans=mul(ans,10);
        for(int s=0;s<=sta;++s)
        {
            if(!f[cur^1][s])continue;
            for(int k=1;k<=10;++k)
            {
                int now = (s<<k)|(1<<(k-1));
                now &= sta;
                if((now&bas)!=bas)
                    f[cur][now]=add(f[cur][now],f[cur^1][s]);
            }
        }
    }
    for(int i=0;i<=sta;++i)
    {
        ans=del(ans,f[cur][i]);
    }
    printf("%d",ans);
    return 0;
}

Measured memory_bytes: 2732
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<map>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
    int n;
    cin>>n;
    int64_t time;
    cin>>time;
    vector<int64_t> t(n);
    rep(i,n){
        cin>>t.at(i);
    }
    int64_t Maxtime=0;
    rep(i,n-1){
        if(t.at(i+1)-t.at(i)>=time){
            Maxtime+=time;
        }
        else{
            Maxtime+=(t.at(i+1)-t.at(i));
        }
    }
    Maxtime+=time;
    cout<<Maxtime<<endl;
}

Measured memory_bytes: 1792
a, b, c = map(int, input().split())
max_a = 0
max_b = 0
if a == b:
    max_a += 1
if a == c:
    max_a += 1
if b == c:
    max_b += 1
if max(max_a, max_b) == 1:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3060
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
		if (x<y) {
			System.out.println(0);
		}else {
			System.out.println(10);
		}
		sc.close();
	}
}

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

constexpr int MAX_N = 100;

int L[MAX_N];
int s[MAX_N+1];

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

  // 累積和
  int i{};
  for (; i < N; ++i) {
    s[i+1] = s[i] + L[i];
    if (s[i+1] > X) {
      break;
    }
  }

  cout << i+1 << endl;
  return 0;
}


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

int N, A, B;

int sumd(int x){
    int num = x;
    int digit = 1;
    while(num /10> 0){
        num/=10;
        digit++;
    }
    int res = 0;
    while(x>0){
        res += x%10;
        x /= 10;
    }
    return res;
}

int main(){
    scanf("%d%d%d", &N, &A, &B);
    long long sum = 0;
    for(int i=1; i<=N; i++){
        int D = sumd(i);
        if(A <= D && D <= B){
            sum += i;
        }
    }
    printf("%lld", sum);
    return 0;
}


Measured memory_bytes: 256
#define _LIBCPP_DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
typedef vector<int> vi;
typedef long long ll;



int main(){
    ll N,K;cin>>N>>K;
    vector<pair<ll,ll> > p(N);
    rep(i,0,N){
        int a,b;cin>>a>>b;
        p.at(i)=make_pair(a,b);
    }
    sort(p.begin(),p.end());
    rep(i,0,N){
        if(i!=0)p.at(i).second+=p.at(i-1).second;
        if(p.at(i).second>=K){
            cout<<p.at(i).first<<endl;
            return 0;
        }
    }
}






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

int main(){
	int N,K;
	cin >> N >> K;
	vector<int> A(N);
	for(auto &i:A)cin >> i;
	
	int sum=accumulate(A.begin(),A.end(),0);
	vector<int> D;
	for(int i=1;i*i<=sum;i++){
		if(sum%i==0){
			D.push_back(i);
			D.push_back(sum/i);
		}
	}
	sort(D.begin(),D.end(),greater<int>());
	for(auto d:D){
		vector<int> a;
		for(int i=0;i<N;i++){
			a.push_back(A[i]%d);
		}
		sort(a.begin(),a.end());
		int x=0,y=N*d-accumulate(a.begin(),a.end(),0);
		for(int i=0;i<a.size();i++){
			x+=a[i];
			y-=(d-a[i]);
			if(x==y){
				if(x<=K){
					cout << d << endl;
					return 0;
				}
				break;
			}
		}
	}
	
	return 0;
}

Measured memory_bytes: 256
n = int(input())

tenant = [
    [ [0]*10, [0]*10, [0]*10, ],
    [ [0]*10, [0]*10, [0]*10, ],
    [ [0]*10, [0]*10, [0]*10, ],
    [ [0]*10, [0]*10, [0]*10, ]
]


for i in range(n):
    b,f,r,nu = map(int, input().split())
    tenant[b-1][f-1][r-1] += nu

for b in range(4):
    for f in range(3):
        print(' ', end='')
        print(' '.join(map(str,tenant[b][f])))
    if b < 3:
        print('#'*20)

Measured memory_bytes: 7696
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <utility>
typedef long long ll;
using namespace std;

ll min(ll a, ll b) {
  return a < b ? a : b;
}

ll max(ll a, ll b) {
  return a > b ? a : b;
}

int main(void) {
  ll i, j, k, n, m, a, b, l, ans = 0;
  scanf("%lld%lld", &l, &n);
  ll x[2 * n], sum[2 * n + 1], s[2 * n];
  for(i = 0; i < n; ++i) scanf("%lld", &x[i]), x[i + n] = x[i] + l;
  if(n == 1) {
    printf("%lld", max(x[0], l - x[0]));
    return 0;
  }
  for(i = 0; i < 2 * n; ++i) sum[i] = s[i] = x[i] < l ? l - x[i] : x[i] - l;
  for(i = 1; i < 2 * n; ++i) sum[i] += sum[i - 1];
  ans = x[n - 1];
  printf("\n");
  if(ans < l - x[0]) ans = l - x[0];
  for(i = 1; i < n; ++i) {
    ll tmp = min(i, n - i), now;
    if(n - i == i) now = 2 * (sum[n + i - 2] - sum[i]) + 2 * max(s[n + i - 1], s[i]) + min(s[n + i - 1], s[i]);
    else {
      if(i < n - i) now = max(2 * (sum[n + i - 1] - sum[n - 1] + sum[i + tmp] - sum[i]) + s[i], 2 * (sum[n + i - 2] - sum[n - 1] + sum[i + tmp - 1] - sum[i - 1]) + s[n + i - 1]);
      else now = max(2 * (sum[n + i - 1] - sum[n + i - tmp - 1] + sum[n - 1] - sum[i]) + s[i], 2 * (sum[n + i - 2] - sum[n + i - tmp - 2] + sum[n - 1] - sum[i - 1]) + s[n + i - 1]);
    }
    if(ans < now) ans = now;
  }
  printf("%lld", ans);
  return 0;
}

Measured memory_bytes: 9600
n=int(input())
if n<10: print(n); exit()
r=range
l=[[[] for _ in r(10)] for _ in r(10)]
l[0]=[[i] for i in r(10)]
c=9
for i in r(9):
  for j in r(10):
    for k in r(-1,2):
      if 0<=j+k<=9:
        for h in l[i][j+k]:
          if j: c+=1
          t=j*10**(i+1)+h
          l[i+1][j]+=[t]
          if c==n: print(t); exit()

Measured memory_bytes: 7480
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<class T>
using ordered_set = tree <T, null_type ,  less<T> ,  rb_tree_tag ,  tree_order_statistics_node_update>;

template<class T>
using ordered_multiset = tree <T, null_type ,  less_equal<T> ,  rb_tree_tag ,  tree_order_statistics_node_update>;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvii = vector<vii>;

#define all(x) x.begin(), x.end()
#define all_r(x) x.rbegin(), x.rend()
#define pb push_back
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, n) for(int i = n; i >= 0; --i)
#define f first
#define s second

int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, q;
	cin >> n;
	map<int, int> S;
	ll sum = 0;
	FOR(i, n) {
		int a;
		cin >> a;
		sum += a;
		++S[a];
	}
	cin >> q;
	while(q--) {
		int b, c;
		cin >> b >> c;
		auto it = S.find(b);
		if(it == S.end()) cout << sum << '\n';
		else {
			sum += 1LL * (c - b) * it->second;
			S[c] += it->second;
			S.erase(b);
			cout << sum << '\n';
		}
	}
 	return 0;
}

Measured memory_bytes: 6504
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int i = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int i = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define MP make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1 << 30;
const ll LINF = 1LL << 61;
const int MOD = 1e9 + 7;

ll dp[3010][3010];

int main() {
    int N, T;
    cin >> N >> T;
    using pii = pair<int, int>;
    vector<pii> dishes(N);
    rep(i, N) {
        int a, b;
        cin >> a >> b;
        dishes[i] = MP(a, b);
    }

    sort(All(dishes));

    for(int i = 0; i < N; i++) {
        for(int j = 0; j <= T - 1; j++) {
            dp[i + 1][j] = dp[i][j];
            if(j - dishes[i].first >= 0) {
                dp[i + 1][j] = max(dp[i][j], dp[i][j - dishes[i].first] +
                                                 dishes[i].second);
            }
        }
    }

    ll res = 0;
    rep(i, N + 1) {
        rep(j, T) { res = max(res, dp[i][j] + dishes[i].second); }
    }

    cout << res << endl;

    return 0;
}

Measured memory_bytes: 70784
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
const int MAX_V =100;

int c,n,m,d;//割引券,街の数,路線の数,出発地,目的地
int a,b,f;//バスの始点、終点,料金
int cd[11][MAX_V];
int used[MAX_V];
int cost[MAX_V][MAX_V];
const int INF =10000000;
int mv;
int max1,max2;
int max1V,max2V;
void dijkstra(int s){
  for(int i = 0;i <=c;i++){
    for(int j = 0;j <n;j++){
      if(j !=s)
	cd[i][j] =INF;
      else
	cd[i][s] =0;
    }
  }
  int v;
  for(int k = 0;k <=c;k++){
    fill(used,used+MAX_V,-1);
    while(true){
      v = -1;
    for(int u = 0;u <n;u++){
      if(used[u] ==-1&&(v ==-1||cd[k][u]<cd[k][v]))
	v =u;
    }
    if(v ==-1)
      break;
    used[v] =1;
   
    for(int u = 0;u <n;u++){
      if(used[u] ==-1){
      if(k ==0){
	cd[k][u] =min(cd[k][u],cd[k][v]+cost[v][u]);
      }
      else{
	cd[k][u] = min(cd[k][u],min(cd[k][v]+cost[v][u],cd[k-1][v]+cost[v][u]/2));
      }
      }
    }
    }
  }
}
int main()
{
  int s;
  while(cin >>c>>n>>m>>s>>d){
    if(c+n+m+s+d ==0)
      break;
  for(int i = 0;i <n;i++){
    for(int j = 0;j < n;j++){
      if(i !=j)
	cost[i][j] =INF;
      else
	cost[i][j] = 0;
    }
  }
  for(int i = 0;i < m;i++){
    cin >>a>>b>>f;
    cost[a-1][b-1] = f;
    cost[b-1][a-1] = f;
  }
  dijkstra(s-1);
  cout <<cd[c][d-1]<<endl;
  }
  return 0;
}

Measured memory_bytes: 1204
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  int N;
  cin>>N;
  ll A[N];
  for(int i=0;i<N;++i) cin>>A[i];
  ll ans1=0, ans2=0, sum;
  sum = 0;
  for(int i=0;i<N;++i) {
    sum += A[i];
    if(i%2==0) { // 0, 2, 4..を正
      if(sum<=0) {
        ans1 += 1-sum;
        sum = 1;
      }
    }
    else {
      if(sum>=0) {
        ans1 += sum+1;
        sum = -1;
      }
    }
  }
  sum = 0;
  for(int i=0;i<N;++i) {
    sum += A[i];
    if(i%2==0) { // 0, 2, 4..を負
      if(sum>=0) {
        ans2 += sum+1;
        sum = -1;
      }
    }
    else {
      if(sum<=0) {
        ans2 += 1-sum;
        sum = 1;
      }
    }
  }
  cout<<min(ans1, ans2)<<endl;
}



Measured memory_bytes: 4392
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner((System.in));
		int n = sc.nextInt();
		int k = sc.nextInt();
		long ans = 0, j = 0;

		for (int i = 1; i <= n; i++) {
			if ((i % k != 0) && ((i * 2) % k == 0)) {
				j++;
			}
		}
		
		ans += Math.pow(n / k, 3) + Math.pow(j, 3);

		System.out.println(ans);

		sc.close();
	}
}


Measured memory_bytes: 23252
#include <stdio.h>

int main()
{
	int i;

	char A[101];
	char B[101];
	scanf("%s %s", A, B);

	//桁数確認
	if (getDigit(A) > getDigit(B)) {
		puts("GREATER");
	}
	else if(getDigit(A) < getDigit(B)){
		puts("LESS");
	}
	else {
		for (i = 0; i < 100; i++) {
			if (A[i] > B[i]) {
				puts("GREATER");
				break;
			}
			else if (A[i] < B[i]) {
				puts("LESS");
				break;
			}
			else if (getDigit(A) == i+1) {
				puts("EQUAL");
				break;
			}
		}
	}


	getchar();
	getchar();
	getchar();

	return 0;
}

int getDigit(char *str) 
{
	int i;
	for (i = 0; i < 100; i++) {
		if (str[i] == '\0') {
			return i;
		}
	}
}

Measured memory_bytes: 128
def gcd(a,b):
  if b==0:
    return a
  else:
    return gcd(b,a%b)
  
n,x=map(int,input().split())
arr=list(map(int,input().split()))
arr.append(x)
arr=sorted(arr)
arr2=[]
for i in range(n):
  arr2.append(arr[i+1]-arr[0])
if n==1:
  print(arr2[0])
else:
  ans=gcd(arr2[0],arr2[1])
  for i in range(1,n):
    ans=gcd(ans,arr2[i])
  print(ans)

Measured memory_bytes: 14252
import math

n, k = [int(elem) for elem in input().split()]

res = 0.0
for i in range(1, n+1):
  if not (k//i) == 0:
    if (k/i) == (k//i):
      if math.log2(k//i) == int(math.log2(k//i)):
        x = int(math.log2(k//i))
      else:
        x = int(math.log2(k//i)) + 1
    else:
      x = int(math.log2(k//i)) + 1
  else:
    x = 0
  res += (1/n) * (0.5 ** x)
print(res)

Measured memory_bytes: 3064
n, a, b = map(int, input().split())

def divide(num):
  sum = 0
  while num > 0:
    sum += num % 10
    num //= 10
  return sum

ans = 0
for i in range(n):
  temp = divide(i+1)
  if temp>= a and temp <= b:
    ans += (i+1)
print(ans)

Measured memory_bytes: 3060
#include<stdio.h>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
    int n,k,ans=0;
    
    scanf("%d%d",&n,&k);
    
    int arr[n];
    
    for(int i=0;i<n;i++)
        scanf("%d",&arr[i]);
    
    sort(arr,arr+n);
    
    for(int i=0;i<k;i++)
        ans+=arr[i];
    
    printf("%d",ans);

    return 0;
}

Measured memory_bytes: 1724
Module Module1

    Private Function Lcm(a As Long, b As Long) As Long
        Return a * b \ Gcd(a, b)
    End Function

    Private Function Gcd(a As Long, b As Long) As Long
        If a < b Then
            Return Gcd(b, a)
        End If
        Do While b <> 0
            Dim remainder = a Mod b
            a = b
            b = remainder
        Loop
        Return a

    End Function

    Sub Main()

        Dim tmp As String() = Split(Console.ReadLine(), " ")
        Dim A As Long = CInt(tmp(0))
        Dim B As Long = CInt(tmp(1))

        Dim R As Long = Lcm(A, B)

        Console.WriteLine(R)
        ’Console.ReadLine()

    End Sub

End Module



Measured memory_bytes: 11488
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <tuple>
#include <deque>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <utility>
#include <complex>
#include <functional>
using namespace std;
const int MOD = 1000000007;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#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 2e9
#define ALL(v) v.begin(), v.end()
struct edge {
    int to, cost;
};
int n;
int a[10000];
const int N = 55555;
vector<bool> isp(N+1, true);
//エラトステネスの篩
void sieve() {
  isp[0] = false;
  isp[1] = false;
  for (int i=2; pow(i,2)<=N; i++) {
    if (isp[i]) for(int j=2; i*j<=N; j++) isp[i*j] = false;
  }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    sieve();
    vector<int> ans;
    vector<int> sosuu;
    for(int i=1;i<=55555;i++){
        if(isp[i]==true&&i%5==1)
        sosuu.push_back(i);
    }
    /*
    vector<vector<int> > sum(4);
    int i=0;
    while(ans.size()<n){
        int c=sosuu[i];
        i++;
        bool check=false;
        for(int j=0;j<sum[3].size();j++){
            if(isp[c+sum[3][j]]){
                check=true;
                break;
            }
        }
        if(!check){
            ans.push_back(c);
            for(int j=0;j<sum[2].size();j++){
                sum[3].push_back(sum[2][j]+c);
            }
            for(int j=0;j<sum[1].size();j++){
                sum[2].push_back(sum[1][j]+c);
            }
            for(int j=0;j<sum[0].size();j++){
                sum[1].push_back(sum[0][j]+c);
            }
            sum[0].push_back(c);
        }
    }
    */
    //cout<<ans.size()<<"\n";
    for(int i=0;i<n;i++){
        cout<<sosuu[i]<<"\n";
    }
}


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

signed main(void)
{
	int n, cnt;
	cin >> n;
	vector<int> a(n);

	rep(i, n) cin >> a[i];
	sort(a.begin(), a.end());
	cnt = 0;
	rep(i, n - 1)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (a[i] == a[j])
				continue;
			for (int k = n - 1; k > j; k--)
			{
              	if (a[j] == a[k]) continue;
				if (a[i] + a[j] > a[k])
					cnt++;
			}
		}
	}
	cout << cnt << endl;
	return 0;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h> 
using namespace std; 
#define ll long long int
#define pb push_back             
#define se second                
#define fi first  
#define be begin()
#define en end()
#define all(v) v.be, v.en
template<typename... T> void pp(T... args) { ((cout << args << " "), ...); cout<<"\n"; }
template<typename... T> void po(T... args) { ((cout << args << " "), ...); cout<<""; }

 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
     int test=1;
     //cin>>test;
     for(int tt=1; tt<=test; tt++){
       ll n,q,sum=0;
       cin>>n;
       map<ll,ll> mp;
       for(ll i=0; i<n; i++){
        ll k;
        cin>>k;sum+=k;
        mp[k]++;
       }
       cin>>q;
       for(ll i=0; i<q; i++){
           ll x,y;
           cin>>x>>y;
           sum-=x*mp[x];
           sum+=y*mp[x];
           mp[y]+=mp[x];
           mp[x]=0;
           pp(sum);
       }
       

     //cout<<"Case #"<<tt<<": ";  
     }
 
    return 0;
}

Measured memory_bytes: 9540
max_num = 10**6 + 1
prime = [i for i in range(max_num)]
r2_num = int(max_num**(1/2))+1
for i in range(2, r2_num):
  if prime[i] == i:
    for j in range(i**2, max_num, i):
      if prime[j] == j:
        prime[j] = i
def gcd(a, b): # a >= b
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)

flg_pairwise = True
flg_setwise = False

n = int(input().rstrip())
a_ls = list(map(int, input().rstrip().split(" ")))
gcd_a = 0
primes_a = []
count_prime = [0 for i in range(max_num)]
for a in a_ls:
  gcd_a = gcd(gcd_a, a)
  if gcd_a == 1:
    flg_setwise = True
  tmp_prime = 0
  while a > 1:
    p = prime[a]
    pp = prime[p]
    if p == 0 or pp == 0:
      flg_pairwise = False
      break
    if tmp_prime != p:
      prime[tmp_prime] = 0
      tmp_prime = p
    a = a//p
  if flg_pairwise:
    prime[tmp_prime] = 0
  if flg_setwise and not flg_pairwise:
    break
if flg_pairwise:
  print("pairwise coprime")
elif flg_setwise:
  print("setwise coprime")
else:
  print("not coprime")

Measured memory_bytes: 137064
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <utility>
#include <numeric>
#include <queue>
#include <stack>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9) + 7;

int main(void) {
	string s;
	cin >> s;
	
	int n = (int)s.size();
	for(int i = 0; i < n; ++i) {
		for(int j = i; j < n + 1; ++j) {
			string tmp;
			
			for(int k = 0; k < n; ++k) {
				if( i <= k && k < j ) continue;
				
				tmp.push_back(s[k]);
			}
			
			if( tmp == "keyence" ) {
				cout << "YES" << endl;
				return 0;
			}
		}
	}
	
	cout << "NO" << endl;
	
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n;
int t[51];
char s[51][110],str[110];
int main() {
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++) {
		scanf("%s%d",s+i,t+i);
	}
	scanf("%s",str);
	for(i=1;i<=n;i++) {
		if(strcmp(str,s[i])==0) {
			break;
		}
	}
	int sum=0;
	for(i=i+1;i<=n;i++) {
		sum+=t[i];
	}
	printf("%d",sum);
	return 0;
}

Measured memory_bytes: 256
X,Y = (int(i) for i in input().split())

def get_money(Z):
    if Z == 3:
        return(100000)
    elif Z == 2:
        return(200000)
    elif Z == 1:
        return(300000)
    else:
        return(0)


x = get_money(X)
y = get_money(Y)

if X == 1 and Y == 1:
    z = 400000
else:
    z = 0
ans = x + y + z

print(ans)

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

int main(){

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s,t;
    cin >> s >> t;
    int n = s.size();
    bool flag = true;
    for(int i=0;i<n;++i){
    	if(s[i] != t[i]){
    		flag = false;
    		break;
    	}
    }

    if(flag){
    	cout << "Yes\n";
    }
    else{
    	cout << "No\n";
    }
    

	return 0;
}

Measured memory_bytes: 3612
INF = 1000000007

n, k = map(int, input().split())
a = list(map(int, input().split()))
sum1 = 0
sum2 = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue    
        if a[i]>a[j]:
            if i <j:
                sum1+=1
            else:
                sum2 +=1
ans = sum1+sum2
x = sum1*k
y = ans *((k*(k-1))//2)
print(int((x+y)%INF))

Measured memory_bytes: 3316
import java.util.*;
 
 
import java.io.*;
 
public class Main {
	private static void solve(){
		//Implement solution here.
		int n = ni();
		int q = ni();
		//Build graph.
		Graph graph = new Graph(n,false);
		
		for(int i = 0; i < n - 1; i++) {
			graph.addEdge(ni(), ni());
		}
		
		//Set initial p and x
		for(int i = 0; i < q; i++) {
			graph.values[ni()] += nl();
		}
		
		//Set value to children
		graph.propagateValue(1,0,0);		
		
		for(int i = 1; i <= n; i++) {
			System.out.print(graph.values[i] + " ");
		}
	}
 
	//Switch input source (stdin/file)
	private static BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args){
		String debugDataPath = System.getenv("DD_PATH");        
		if(debugDataPath != null){
			try{
				br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(debugDataPath))));
			}catch(Exception e){
				throw new RuntimeException(e);
			}
		} 
		solve();
	}
 
	//Input read utility
	private static StringTokenizer tokenizer = null;
	public static String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}
	//Get next single int
	private static int ni() {
		return Integer.parseInt(next());
	}
	//Get next single long
	private static long nl() {
		return Long.parseLong(next());
	}
	//Get next single double
	private static double nd() {
		return Double.parseDouble(next());
	}
	//Get next int array from one line
	private static int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}
	//Get next char array from one line
	private static char[] ns() {
		return next().toCharArray();
	}
	//Get next long array from one line
	private static long[] nal(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nl();
		return a;
	}
}
 
class Graph{
	int nodeNum;
	private final boolean directed;
	ArrayList<Integer>[] edges;
	long[] values;
 
	Graph(int nodeNum,boolean directed){
		this.nodeNum = nodeNum;
		this.directed = directed;
		this.edges = new ArrayList[nodeNum + 1];
		this.values = new long[nodeNum + 1];
		for(int i = 0;i<nodeNum + 1;i++){
			edges[i] = new ArrayList<Integer>();
		}
	}
 
	public void addEdge(int node1,int node2){
		edges[node1].add(node2);
		if(!this.directed){
			edges[node2].add(node1);
		}
	}
	
	public void propagateValue(int root, int parent, long value) {
		values[root] += value;		
		if(edges[root].size() > 0) {
			for(int i:edges[root]) {
				if(i != parent) {
					propagateValue(i, root, values[root]);
				}				
			}
		}
	}
}

Measured memory_bytes: 148084
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  int n, x;
  cin >> n >> x;
  vector<int> m(n);
  rep(i, 0, n) cin >> m[i];
  int ans = n;
  int acum = accumulate(m.begin(), m.end(), 0);
  int remin = x - acum;
  int min_val = *min_element(m.begin(), m.end());
  cout << ans + remin / min_val << endl;
}


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

using namespace std;

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

   if(m!=0){n[t]=m;t++;}
   if(m==0){cout<<n[t-1]<<endl;n[t]=11;t--;}
}

return 0;
}

Measured memory_bytes: 0
#pragma region template 2.4
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using pq_asc = priority_queue<T, vector<T>, greater<T>>;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define FOR(i, a) for (auto &i : a)
#define CH(f, x, y) x = f(x, y)
#define IN(T, x) \
    T x;         \
    cin >> x;
#define AIN(T, a, n) \
    vector<T> a(n);  \
    FOR(i, a)        \
    cin >> i;
#define A2IN(T1, a, T2, b, n) \
    vector<T1> a(n);          \
    vector<T2> b(n);          \
    REP(i, n)                 \
    cin >> a[i] >> b[i];
#define OUT(x) cout << (x) << endl;
#define FOUT(x) cout << fixed << setprecision(15) << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a) \
    SORT(a);     \
    reverse(ALL(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define DUMPA(a)          \
    cout << #a << " = {"; \
    JOUT(ALL(a), ", ", cout) << "}" << endl;

template <typename T>
ostream &JOUT(T s, T e, string sep = " ", ostream &os = cout)
{
    if (s != e)
    {
        os << *s;
        ++s;
    }
    while (s != e)
    {
        os << sep << *s;
        ++s;
    }
    return os;
}

ostream &YES(bool cond, string yes = "Yes", string no = "No", ostream &os = cout)
{
    if (cond)
    {
        os << yes << endl;
    }
    else
    {
        os << no << endl;
    }
    return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p)
{
    os << '(' << p.first << ", " << p.second << ')';
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    os << '[';
    JOUT(ALL(v), ", ", os) << ']';
    return os;
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#pragma endregion template

int main()
{
    IN(ll, N);
    IN(ll, K);
    IN(ll, C);
    IN(string, S);
    vi dp(N + 1);
    for (ll i = N - 1; i >= 0; --i)
    {
        if (S[i] == 'o')
        {
            dp[i] = dp[min(N, i + C + 1)] + 1;
        }
        else
        {
            dp[i] = dp[i + 1];
        }
    }
    ll k = K;
    REP(i, N)
    {
        if (S[i] == 'o' and k > 0)
        {
            if (k > dp[i + 1])
            {
                OUT(i + 1);
            }
            --k;
            i += C;
        }
    }
}

Measured memory_bytes: 3332
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

const int maxn = 1e5+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=2.718281828459045235360287471352662;
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
int a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    int n,ma=0,mi,minn=1e9;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i],ma=max(a[i],ma);
    for(int i=1;i<=n;i++)
        if(abs(a[i]-ma/2)<minn&&a[i]!=ma)
            minn=abs(a[i]-ma/2),mi=a[i];
    cout<<ma<<" "<<mi<<endl;
    return 0 ;
}


Measured memory_bytes: 640
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 998244353;
const ll INF = 1e16;
const ll MAX_N = 100010;
 
int main() {
    ll h, w, d;
    cin >> h >> w >> d;

    vl x(h*w), y(h*w);
    rep(i, h) {
        rep(j, w) {
            ll a;
            cin >> a;
            a--;
            x[a] = i;
            y[a] = j;
        }
    }

    vl dp(h*w);  // dp[i] : i%dからiへの移動コスト
    exrep(i, d, h*w-1) {
        dp[i] = dp[i-d] + abs(x[i] - x[i-d]) + abs(y[i] - y[i-d]);
    }

    ll q;
    cin >> q;

    vl ans(q);
    rep(i, q) {
        ll L, R;
        cin >> L >> R;
        L--;  R--;
        ans[i] = dp[R] - dp[L];
    }

    rep(i, q) {
        out(ans[i]);
    }

    re0;
}

Measured memory_bytes: 3968
N, M = map(int, input().split())

A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())

c = zip(A, B)
c = sorted(c)

A, B = zip(*c)

count = 0
money = 0
for i in range(N):
    count += B[i]
    money += A[i] * B[i]
    if count < M:
        continue
    else:
        while count != M:
            count -= 1
            money -= A[i]
        print(money)
        exit()


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

int main() {
    int n,m;
    cin >> n>> m;
    vector<int>a(m);
    vector<int>dp(n+1,0);
    for(int i=0; i<m; ++i){
        cin >> a[i];
    }
    for(int i=0; i<m; ++i){
        dp[a[i]]=1;
    }
    for(int i=0; i<m; ++i){
        if(a[i]==(a[i+1]-1)){
            cout << 0 << endl;
            return 0;
        }
    }
    dp[0]=1;
    if(dp[1]){
        dp[1]=0;
    }
    else{
        dp[1]=1;
    }
    for(int i=2; i<=n; ++i){
        if(dp[i]){
            dp[i]=0;
        }
        else{
            dp[i]=dp[i-1]+dp[i-2];
            dp[i]=dp[i]%1000000007;
        }
    }
    cout << dp[n] << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;

const int MOD = 1e9 + 7;
const LL MX = 1e9;
const LL INF = 1e9;

void print_a(VI v)
{
    if(v.size()) cout<<v[0];
    loop(i,1,v.size()) cout<<' '<<v[i];
    cout<<'\n';
}

VVI init_vvi(int n, int m, int val)
{
    return VVI(n,VI(m,val));
}

VVL init_vvl(int n, int m, LL val)
{
    return VVL(n,VL(m,val));
}

int main()
{
    //ofstream out("output.txt");
    //ifstream in("input.txt");
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    int n,m;
    cin>>n>>m;

    int a[n][m];
    loop(i,0,n) loop(j,0,m) cin>>a[i][j];

    VPII ans;
    loop(i,0,n-1)
    {
        loop(j,0,m)
        {
            if(a[i][j] & 1) {ans.PB({i+1,j+1}); --a[i][j]; ++a[i+1][j];}
        }
    }

    loop(j,0,m-1)
    {
        if(a[n-1][j] & 1) {ans.PB({n,j+1}); --a[n-1][j]; ++a[n-1][j+1];}
    }

    cout<<ans.size()<<'\n';
    for(PII p : ans)
    {
        if(p.FF == n) cout<<p.FF<<' '<<p.SS<<' '<<p.FF<<' '<<p.SS+1<<'\n';
        else cout<<p.FF<<' '<<p.SS<<' '<<p.FF+1<<' '<<p.SS<<'\n';
    }

    /*
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int a[n];
        loop(i,0,n)
        {
            cin>>a[i];
        }
    }
    */

    /*
    int n;
    cin>>n;
    int a[n];
    loop(i,0,n)
    {
        cin>>a[i];
    }
    */

    /*
    cout<<ans<<'\n';

    if(f) cout<<"YES\n";
    else cout<<"NO\n";

    if(f) cout<<"Yes\n";
    else cout<<"No\n";
    */

    return 0;
}


Measured memory_bytes: 5228
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    int n;
    string s;
    cin>>n>>s;
    vector<int> w(n),e(n);
    int wc=0,ec=0;
    rep(i,n){
        if(s[i]=='W') wc++;
        else if(s[i]=='E') ec++;
        w[i]=wc;
        e[i]=ec;
    }
    int mini=n;
    rep(i,n){
        int a;
        if(s[i]=='W') a=w[i]-1+ec-e[i];
        else a=w[i]+ec-e[i];
        mini=min(mini,a);
    }
    cout<<mini;
}

Measured memory_bytes: 6160
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>

using namespace std;

const int MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for(int i=0;i<n;i++)
#define rep2(i, m, n) for(int i=m;i<n;i++)
#define rrep(i, n, m) for(int i=n;i>=m;i--)
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

#ifdef __DEBUG

#include "cpp-pyprint/pyprint.h"

#endif

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

    int N;
    cin >> N;
    vector<int> A(N);
    for (auto &a: A) {
        cin >> a;
    }
    map<int, int> dict;
    for (auto a : A) {
        dict[a] += 1;
    }

    if (dict.count(0) && dict[0] == N) {
        cout << "Yes" << endl;
    } else if (dict.count(0) && dict.size() == 2 && dict[0] == N / 3) {
        cout << "Yes" << endl;
    } else if (dict.size() == 3) {
        bool flag = true;
        int x = 1;
        for (auto &p : dict) {
            x ^= p.first;
            if (p.second == N / 3) {
                continue;
            } else {
                flag = false;
            }
        }
        if (x == 1 && flag) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    } else {
        cout << "No" << endl;
    }
}


Measured memory_bytes: 4224
#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, ans, a, b, c, d, tmp, m, h, w, x, y, sum, k, q;

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

  cin >> a >> b;
  a--;
  b--;
  ans = a * b;
  Cout(ans);
  return 0;
}

Measured memory_bytes: 256