akhauriyash/Code-Regression huggingface dataset:
#include <string>
#include <string.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
using namespace std;
const int maxn = 400200;
int head[maxn],nxt[maxn],to[maxn],w[maxn];
int flag[maxn];
int sz;
int n,m;
int ans = 1;
void init()
{
	for(int i=1;i<=n;i++) head[i]=-1,flag[i]=0;
	sz=0;

}
void add(int u,int v,int ww)
{
	nxt[++sz]=head[u];
	to[sz]=v;
	w[sz]=ww;
	head[u]=sz;

}
int dis[maxn];
void dfs(int u,int pre)
{
	if(!ans) return;
	for(int i=head[u];~i;i=nxt[i]){
		int v = to[i];
		if(v==pre) continue;
		if(flag[v]){
			if(dis[v]-dis[u]!=w[i]){
				ans=0;
				return;
			}
		}else{
			flag[v]=1;
			dis[v]=dis[u]+w[i];
			dfs(v,u);
		}
	}
}
int main()
{
	scanf("%d %d",&n,&m);
	init();
	for(int i=1;i<=m;i++){
		int l,r,d;scanf("%d %d %d",&l,&r,&d);
		add(l,r,d);
		add(r,l,-d);
	}
	for(int i=1;i<=n;i++){
		if(flag[i]==0){
			flag[i]=1;
			dis[i]=0;
			dfs(i,-1);
		}
	}
	if(ans){
		printf("Yes\n");
	}else
		printf("No\n");


	return 0;
}


Measured memory_bytes: 11648
#include <stdio.h>
#include <cmath>

int GetAverageTemperatureX1000 (int T,int x)
{
	return T*1000 - x * 6;
}
int main()
{
	int N,T,A;
	int resultIndex;
	int mindef = 1000000000;
	
	scanf("%d",&N);
	scanf("%d %d",&T,&A);
	
	for (int i = 1;i <= N;i++)
	{
		int h;
		scanf("%d", &h);
		
		int currentDef = (GetAverageTemperatureX1000(T,h) - A * 1000);
      	if(currentDef < 0) currentDef = -currentDef;
		if(currentDef < mindef)
		{
			mindef = currentDef;
			resultIndex = i;
		}
	}
	
	printf("%d\n",resultIndex);
	return 0;
}


Measured memory_bytes: 128
S = input()

def kaibun(S):
  for i in range(len(S) // 2):
    if S[i] != S[len(S) - i - 1]:
      return False
  return True

if kaibun(S) and kaibun(S[:(len(S) - 1)//2]) and kaibun(S[(len(S) + 3) // 2-1:]):
  print('Yes')
else:
  print('No')

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

int main(){
  int x, y;
  while(1) {
    cin >> x >> y;
    if ((x == 0) && (y == 0)) {
      break;
    }
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        cout << '#';
      }
      cout << endl;
    }
    cout <<endl;
  }
}

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

int main() {
    string s;
    cin >> s;
    int ans=0;
    for(int i=s.size()-3;i>0;i-=2){
        int count=0;
        for(int j=0;j<=(i/2);j++){
            if(s.at(j)==s.at(j+((i+1)/2))) count++;
        }
        if(count==((i+1)/2)){
            ans=i+1;
            break;
        }
    }
    cout << ans << endl;
}

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

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

  int sum = 0;
  for (char c : s){
    sum += (c - '0');
  }

  if (sum % 9 == 0) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 3756
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
        int auki=c,taha=a;
        while(1)
        {
            auki=auki-b;
        if(auki<=0)
        {
            cout<<"Yes"<<endl;
            break;
        }
         taha=taha-d;
         if(taha<=0)
         {
             cout<<"No"<<endl;
             break;
         }
        }

    return 0;
}



Measured memory_bytes: 3628
#include<iostream>
using namespace std;
int main(){
	long long a;
	long long n,b;
	cin>>n>>a>>b;
	if(n>1&&b>=a||n==1&&a==b){
		cout<<(n-2)*(b-a)+1;
		return 0;	
	}else{
		cout<<"0";
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main(){
	int x, a, b, c;
	cin >> a >> b >> c >> x;
	int count = 0;

	for(int i = 0; i < a+1; i++){
		for(int j = 0; j < b+1; j++){
			for(int k = 0; k < c+1; k++){
				if(x == (500*i + 100*j + 50*k)){
					count++;
				}
			}
		}
	}
	cout << count;

	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long int> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long int>> vvl;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> pll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long int, long long int>> vll;
#define fast() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define test() long long int t; cin >> t; cin.ignore(); while(t--) 
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define MOD 1000000007
#define SMOD 998244353
#define pq priority_queue
#define all(x) x.begin(),x.end()
#define SP(x) setprecision(x)
#define sp " "
#define inp(a) ll a;cin>>a;cin.ignore()
#define inps(a,b) ll a,b;cin>>a>>b;cin.ignore()
#define out(a) cout<<SP(20)<<a<<'\n'
#define outs(a,b) cout<<SP(20)<<a<<" "<<b<<'\n'
#define ins insert
#define er erase
#define lb lower_bound
#define ub upper_bound
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define inpa(a,n) ll a[n]; for(int i=0; i<n; i++){cin>>a[i];}
#define inpv(a,n) vl a; for(int i=0; i<n; i++){inp(x);a.pb(x);}
#define outa(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}cout<<'\n';
#define outv(v) for(auto i:v){cout<<i<<" ";}cout<<'\n';
set<char> vow = {'a','e','i','o','u','A','E','I','O','U'};
ll xpow(ll n, ll m){if(m==0){return 1;}else{ll x = xpow(n, m/2);if(m&1){return x*x*n;}else{return x*x;}}}
ll xmod(ll a, ll m){return (((a%m)+m)%m);}
ll xmodpow(ll n, ll m, ll y){if(m==0){return 1;}else{ll x = xmodpow(n, m/2, y);if(m&1){return xmod((xmod((xmod(x,y)*xmod(x,y)),y)*xmod(n,y)),y);}else{return xmod((xmod(x,y)*xmod(x,y)),y);}}}
 
int main(){
    fast();

/* you get what you work for not what you wish for */
    inps(n, k);
    inpv(a, n);
    sort(all(a));
    ll ans = 0;
    for(int i=0; i<n; i++){
        if(a[i]>=k){
            ans++;
        }
    }
    out(ans);
}

Measured memory_bytes: 1400
#include<stdio.h>
int main(void){

  int a,b[100],i,j,n=0,x,y,z;
  scanf("%d",&a);

  for(i=0;i<a;i++){
    scanf("%d",&b[i]);
  }

  for(i=0;i<a-1;i++){
    z = i; 
    x = b[i];
    for(j=i;j<a;j++){
       
      if(b[j]<x){
    x = b[j];
    z = j;
      }
    }

    if(x!=b[i]){
      y = b[i];
      b[i] = b[z];
      b[z] = y;
      n++;
    }
  }

  for(i=0;i<a-1;i++){
    printf("%d ",b[i]);
  }

  printf("%d\n",b[i]);
  printf("%d\n",n);

  return 0;

}


Measured memory_bytes: 2136
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
template <typename T>
using Vec1 = vector<T>;
template <typename T>
using Vec2 = vector<Vec1<T> >;
#define REP(i, m, n) for(ll i = (m); (i) < (n); ++(i))
#define REPN(i, m, n) for(ll i = (m); (i) <= (n); ++(i))
#define REPR(i, m, n) for(ll i = (m)-1; (i) >= (n); --(i))
#define REPNR(i, m, n) for(ll i = (m); (i) >= (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <typename T>
bool chmax(T &a, const T b){if(a < b){a = b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T b){if(a > b){a = b; return true;} return false;}
template <typename T>
ll pow2(const T n){return (1LL << n);}
template <typename T>
void cosp(const T n){cout << n << ' ';}
void co(void){cout << '\n';}
template <typename T>
void co(const T n){cout << n << '\n';}
template <typename T1, typename T2>
void co(pair<T1, T2> p){cout << p.fs << ' ' << p.sc << '\n';}
template <typename T>
void co(const Vec1<T> &v){for(T i : v) cosp(i); co();}
template <typename T>
void co(initializer_list<T> v){for(T i : v) cosp(i); co();}
template <typename T>
void ce(const T n){cerr << n << endl;}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const ll n){cout << fixed << setprecision(n);}
constexpr int INF = 1e9+1;
constexpr ll LINF = 1e18+1;
constexpr ll MOD = 1e9+7;
// constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-11;
const ld PI = acos(-1);

int main(void){
	ll x, y;
	cin >> x >> y;

	if (y & 1) puts("No");
	else if(2 * x <= y && y <= 4 * x)
		puts("Yes");
		else
		    puts("No");

	return 0;
}


Measured memory_bytes: 3632
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<climits>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<iomanip>
using namespace std;

#define rep(i,j,n) for(int i=(j);i<(n);i++)
#define rep2(i,j,n) for(int i=(j);i<=(n);i++)
#define all(i) i.begin(),i.end()
#define INF 1e9

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef pair<int, int> pi;
typedef long long i64;

int main() {
	string a, b;
	cin >> a >> b;
	if (a[0] == b[2] && a[1] == b[1] && a[2] == b[0]) {
		cout << "YES\n";
	}
	else {
		cout << "NO\n";
	}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)
class Point{
  public:
  double x,y;
  Point(double x=0,double y=0):x(x),y(y){}
  Point operator + (Point p){return Point(x+p.x,y+p.y);}
  Point operator - (Point p){return Point(x-p.x,y-p.y);}
  Point operator * (double a){return Point(a*x,a*y);}
  Point operator / (double a){return Point(x/a,y/a);}

  bool operator < (const Point &p) const{
    return x!=p.x?x<p.x:y<p.y;
  }
  bool operator == (const Point &p) const{
    return fabs(x-p.x)<EPS&&fabs(y-p.y)<EPS;
  }
};
/*class Circle{
public:
  Point c;
  double r;
  Circle(Point c=Point(),double r=0.0):c(c), r(r) {}
};*/
typedef Point vect;
struct seg{Point p1,p2;};
//????????´???
double norm(Point p){return p.x*p.x+p.y*p.y;}
//??¶??????
double abs(Point p){return sqrt(norm(p));}
//??????
double dot(Point a,Point b){
  return a.x*b.x+a.y*b.y;
}
//??????
double cross(Point a,Point b){
  return a.x*b.y-a.y*b.x;
}
//??´?????????
bool C90(seg s1,seg s2){
  return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);
}
bool C0(seg s1,seg s2){
  return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);
}
//?°???±
Point projec(seg s,Point p){
  Point base = s.p2-s.p1;
  double r=dot(p-s.p1,base) / norm(base);
  return s.p1+base*r;
}
Point reflec(seg s,Point p){
  return  p+(projec(s,p)-p)*2.0;
}
main(){
  int n;
  seg a;
  cin>>a.p1.x>>a.p1.y>>a.p2.x>>a.p2.y;
  cin>>n;
  while(n--){
    Point b,c;
    cin>>b.x>>b.y;
    c=reflec(a,b);
    printf("%.11f %.11f\n",c.x,c.y);
  }
}

Measured memory_bytes: 3296
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define N 55
#define mod 1000000007
#define ll long long
int n,ans,now;
char s1[N],s2[N];
int main()
{
    scanf("%d%s%s",&n,s1+1,s2+1);
    if(s1[1]==s2[1]) ans=3,now=2;
    else ans=6,now=3;
    while(now<=n)
    {
        if(s1[now]==s2[now])
        {
            if(s1[now-1]==s2[now-1]) (ans+=ans)%=mod;
            now++;
        }
        else
        {
            if(s1[now-1]==s2[now-1]) (ans+=ans)%=mod;
            else ans=(ll)ans*3%mod;
            now+=2;
        }
    }
    printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 256
x, y = gets.chomp.split(" ").map(&:to_i)
array = []
plus_y = y
minus_y = y
array << y

(x-1).times do
  plus_y += 1
  array << plus_y
end

(x-1).times do
  minus_y -= 1
  array << minus_y
end

array2 = array.sort

array2.each do |e|
  print e
  print " "
end

puts ""


Measured memory_bytes: 1788
#include "bits/stdc++.h"
#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
typedef long long ll;
const int inf = 1000000001;
const ll INF = 1e18 * 4;
using namespace std;

int main(){
    int N;
    cin>>N;
    vector<pii> job;

    rep(i,1,N+1){
        int tmpi;
        cin>>tmpi;
        job.push_back(make_pair(tmpi,i));
    }
    sort(job.begin(),job.end());

    rep(i,0,N){
        cout<<job[i].second<<endl;
    }
    return 0;
}


Measured memory_bytes: 1780
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  double a[2];
  vector<vi> n(4,vi(2,0));
  while(scanf("%lf %lf",&a[0],&a[1])!=EOF){
    rep(i,2)
      if(a[i]>=1.1)n[0][i]++;
      else if(a[i]>=0.6)n[1][i]++;
      else if(a[i]>=0.2)n[2][i]++;
      else n[3][i]++;
  }
  rep(i,4)
    cout<<n[i][0]<<" "<<n[i][1]<<endl;
}

Measured memory_bytes: 3196
fun main(args: Array<String>){
    val N = readLine()!!.toInt()
    val count = LongArray(5)
    repeat(N){
        val S = readLine()!!
        when(S[0]){
            'M' -> count[0]++
            'A' -> count[1]++
            'R' -> count[2]++
            'C' -> count[3]++
            'H' -> count[4]++
        }
    }

    val list = mutableListOf<String>()
    fun comGenerate(index: Int, s: String){
        if(index == 5){
            if(s.length == 3){
                list.add(s)
            }
            return
        }
        comGenerate(index + 1, s + index)
        comGenerate(index + 1, s)
    }
    comGenerate(0, "")

    list.map { s ->
        var i = 1L
        s.forEach { c ->
            i *= count[c.toString().toInt()]
        }
        i
    }.sum().let { println(it) }
}

Measured memory_bytes: 39168
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <tuple>
#include <utility>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
const int INF = 1000000000;
const int MOD = 1000000007;
int main(){
	string s;
	cin >> s;
	int n = s.size();
	map<char,bool> mp;
	for(int i = 0;i < n;i++){
		if(mp[s[i]]){
			cout << "no" << endl;
			return 0;
		}
		mp[s[i]] = true;
	}

	cout << "yes" << endl;
}


Measured memory_bytes: 3592
#include <iostream>

using namespace std;

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

  char grid[H][W];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> grid[i][j];
    }
  }

  int w_left = 0;
  int w_right = W -1;
  int h_top = 0;
  int h_bot = H - 1;
  while (true) {
    bool changed = false;
    // search rows
    for (int i = h_top; i <= h_bot; ++i) {
      if (grid[i][w_left] == '.') {
        bool all = true;
        for (int j = w_left; j <= w_right; ++j) {
          if (grid[i][j] == '#') {
            all = false;
            break;
          }
        }

        // shrink
        int target_row = i;
        if (all) {
          for (int m = target_row; m >= h_top; --m) {
            for (int n = w_left; n <= w_right; ++n) {
              grid[m][n] = grid[m - 1][n];
            }
          }
          ++h_top;
          changed = true;
          break;
        }
      }
    }

    // serch columns
    for (int i = w_left; i <= w_right; ++i) {
      if (grid[h_top][i] == '.') {
        bool all = true;
        for (int j = h_top; j <= h_bot; ++j) {
          if (grid[j][i] == '#') {
            all = false;
            break;
          }
        }

        // srink
        int target_column = i;
        if (all) {
          for (int m = h_top; m <= h_bot; ++m) {
            for (int n = target_column; n < w_right; ++n) {
              grid[m][n] = grid[m][n + 1];
            }
          }
          --w_right;
          changed = true;
          break;
        }
      }
    }

//cout << h_top << " " << w_right << endl;

    if (!changed)
      break;
  }

  for (int i = h_top; i <= h_bot; ++i) {
    for (int j = w_left; j <= w_right; ++j) {
      cout << grid[i][j];
    }
    cout << endl;
  }

  return 0;
}

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


template<typename T>
struct BIT{
    int n;
    vector<T> v;
    BIT(const int n_) : n(n_) , v(n+1 , T(0)){};

    void add(const int i,const T x){
        for(int idx=i+1;idx<=n;idx+= (idx & -idx))
            v[idx]+=x;
        return;
    }
    T subsum(const int i)const{
        T res(0);
        for(int idx=i+1;idx>0;idx-=(idx & -idx))
            res+=v[idx];
        return res;
    }
    T sum(const int l,const int r)const{//[l,r)
        return subsum(r-1)-subsum(l-1);
    }
    int lower_bound(const T w)const{//よくわからなくなってるので後で直す これは1_indexedの実装のまま放置しています
        if(w<=0)return 0;
        int x=0,r=1;//xは求めるindexの一つ左を持っている
        while(r<n) r<<=1;
        for(int len=r;len>0;len=len>>1){
            if(x+len<n && v[x+len]<w){
                w-=v[x+len];
                x+=len;
            }
        }
        return x+1;
    }
};

int main(){
    int n,q;cin >> n >> q;
    BIT<ll> b(n);
    vector<ll> out;
    rep(i,n){
        ll a;cin >> a;
        b.add(i,a);
    }
    rep(i,q){
        int com;cin >> com;
        if(com){
            int l,r;cin >> l >> r;
            out.emplace_back(b.sum(l,r));
        }else{
            int p;ll x;cin >> p >> x;
            b.add(p,x);
        }
    }
    rep(i,out.size())cout << out[i] << " \n"[i+1==out.size()];
}

Measured memory_bytes: 10316
import kotlin.math.*
import java.io.BufferedReader
import java.io.InputStreamReader


private var fs = BufferedReader(InputStreamReader(System.`in`))
private fun readLn() = fs.readLine()!! // string line
private fun readStrings() = readLn().split(" ") // list of strings
private fun readLong() = readLn().toLong()
private fun readLongs() = readStrings().map { it.toLong() } // list of ints
private fun readInt() = readLn().toInt() // single int
private fun readInts() = readStrings().map { it.toInt() } // list of ints

fun main() {
    solveAtc()
}
fun solveAtc() {
    var (a,b) = readInts()
    println(a*b)
}

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

ll dp[3105][3105][5];
ll ara[3105][3105];

ll n,m,k;

ll call(ll i,ll j,ll tot)
{
    if(i>n || j>m )
        return 0;

    ll &sum=dp[i][j][tot];


    if(~sum)
        return sum;

    sum=0;

    if(tot==3)
    {
        sum=(call(i+1,j,0));
    }
    else
    {
        if(ara[i][j]!=0)
        {
            sum=max(call(i,j+1,tot),max(ara[i][j]+call(i,j+1,tot+1),ara[i][j]+call(i+1,j,0)));
        }
        else
        {

            sum=max(call(i+1,j,0),call(i,j+1,tot));
        }
    }

    return sum;

}
int main()
{
    cin>>n>>m>>k;
    ll a,b,c;
    while(k--)
    {
        cin>>a>>b>>c;
        ara[a][b]=c;
    }
    memset(dp,-1,sizeof(dp));
    cout<<call(0,0,0)<<endl;
    return 0;
}


Measured memory_bytes: 453584
#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>;

const int MOD = 2019;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  if (r - l + 1 >= 2019) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 1e9;
  rep(i, l, r + 1) rep(j, i + 1, r + 1) ans = min(ans, ((ll)i * j) % MOD);

  cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cinttypes>
#include <algorithm>

using namespace std;

struct key
{
	int price;
	int what;
} keys[1005];

int main()
{
	int m, n;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int price, p;
		cin >> price >> p;
		keys[i].price = price;
		for (int j = 0; j < p; j++)
		{
			int tmp;
			cin >> tmp;
			tmp--;
			keys[i].what |= 1 << tmp;
		}
	}
	vector<int> cost(1 << n, 0x3f3f3f3f);
	cost[0] = 0;
	for (int s = 0; s < (1 << n); s++)
	{
		for (int i = 0; i < m; i++)
		{
			int t = s | keys[i].what;
			int new_cost = cost[s] + keys[i].price;
			cost[t] = min(cost[t], new_cost);
		}
	}
	int result = cost.back();
	if (result == 0x3f3f3f3f)
		cout << -1 << endl;
	else
		cout << result << endl;
	return 0;
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-

if __name__ == "__main__":
    n = int(input())
    vals = list(map(int, input().split()))

    alice, bob = 0, 0

    for i in range(n//2):
        dum = max(vals)
        alice += dum
        del vals[vals.index(dum)]

        dum = max(vals)
        bob += dum
        del vals[vals.index(dum)]

    if vals:
        alice += vals[0]

    print(alice - bob)

Measured memory_bytes: 3064
/*
* Note: This template uses some c++11 functions , so you have to compile it with c++11 flag.
*       Example:-   $ g++ -std=c++11 c++Template.cpp
*
* Author : Akshay Pratap Singh
* Handle: code_crack_01
*
*/
 
/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for(int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
typedef long long int ll;
typedef unsigned long long int  ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

int main(){

    int n, m, x; cin >> n >> m >> x;
    vi A(m);
    REP(i, m) cin >> A[i];

    ll l=0, r=0;
    REP(i, m){
        if( A[i] < x ) l++;
        else r++;
    }

    cout << min(l, r) << endl;

    return 0;
}


Measured memory_bytes: 256
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

int main(){
	int N,x;
	cin>>N>>x;
	vector<int> ret(2*N-1);
	vector<bool> used(2*N-1);
	for(int i=0;i<2*N-1;i++){
		ret[i]=i+1;
		used[i]=false;
	}
	if(ret[N-1]!=x){
		int tmp=ret[N-1];
		ret[N-1]=x;
		ret[x-1]=tmp;
		if(ret[N-2]>ret[N-1]){
			tmp=ret[N-2];
			ret[N-2]=1;
			ret[0]=tmp;
		}
		if(ret[N]<ret[N-1]){
			tmp=ret[N];
			ret[N]=2*N-1;
			ret[2*N-2]=tmp;
		}
	}
	if(x==1 || x==(2*N-1)){
		cout<<"No"<<endl;
	}else{
		cout<<"Yes"<<endl;
	for(int i=0;i<2*N-1;i++){
		cout<<ret[i]<<endl;
	}
	}

	return 0;
}

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

// @@ !! LIM()

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

  string S; cin >> S;
  stringstream ss(S);
  ll x; ss >> x;
  ll t = 0;
  for (ll i = 0; i < (ll)S.size(); i++) {
    t += S.at(i) - '0';
  }
  cout << (x % t == 0 ? "Yes" : "No") << endl;

  return 0;
}



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

int main() {

	int n, k; cin >> n >> k;
	string s; cin >> s;
	int cnt = 0;
	for (int i = 0; i < n - 1; i++) {
		if (s[i + 1] != s[i]) cnt++;
	}
	int ans = n - 1 - max(cnt - 2 * k, 0);
	
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3696
import sys
import numpy as np
from functools import lru_cache
from collections import deque

#input = sys.stdin.readline
#sys.setrecursionlimit(10 ** 5)

H,W = map(int,input().split())
grid = [list(input()) for i in range(H)]
dp = [[0] * (W + 1) for i in range(H + 1)]
dp[0][0] = 1
mod = (10 ** 9) + 7

for i in range(H):
  for j in range(W):
    if i > 0 or j > 0:
      if grid[i][j] == "#":
        dp[i][j] = 0
      else:
        dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % mod

print(dp[H - 1][W - 1])
#print(dp)

Measured memory_bytes: 60504
class in{struct my_iterator{int it;const bool rev;explicit constexpr my_iterator(int it_, bool rev=false):it(it_),rev(rev){}int operator*(){return it;}bool operator!=(my_iterator& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const my_iterator i,n;public:explicit constexpr in(int n):i(0),n(n){}explicit constexpr in(int i,int n):i(i,n<i),n(n){}const my_iterator& begin(){return i;}const my_iterator& end(){return n;}};

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

int dp[20][20];
bool isBroken[20][20];
int dfs(int x, int y) {
    int& res = dp[y][x];
    if(res != -1) return res;
    if(y == 0 && x == 0) return 1;

    res = 0;
    if(x > 0 && !isBroken[y][x - 1])
      res += dfs(x - 1, y);
    if(y > 0 && !isBroken[y - 1][x])
      res += dfs(x, y - 1);
    return res;
}
int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    int a, b, n;
    while(cin >> a >> b && a) {
        cin >> n;
        memset(isBroken, 0, sizeof(isBroken));
        for(int _ : in(n)) {
            int x, y;
            cin >> x >> y;
            --x; --y;
            isBroken[y][x] = true;
        }
        memset(dp, -1, sizeof(dp));
        cout << dfs(a - 1, b - 1) << endl;
    }
    return 0;
}

Measured memory_bytes: 3108
import sys


class DoubleLinkedList:
    class Node:
        def __init__(self, value=None, prev=None, _next=None):
            self.value = value
            self.prev = prev
            self.next = _next

    def __init__(self):
        self.head = self.Node()
        self.tail = self.Node()
        self.head.next = self.tail
        self.tail.prev = self.head

    def insert(self, v):
        head = self.head
        node = self.Node(v, head, head.next)
        head.next.prev = node
        head.next = node

    def delete(self, node):
        if node in (self.head, self.tail):
            return
        else:
            node.prev.next, node.next.prev = node.next, node.prev

    def delete_first(self):
        self.delete(self.head.next)

    def delete_last(self):
        self.delete(self.tail.prev)

    def delete_first_value(self, v):
        node = self.find(v, self.head)
        if node is not None:
            self.delete(node)

    def find(self, v, cur=None):
        if cur is None:
            cur = self.head

        while cur != self.tail:
            cur = cur.next
            if cur.value == v:
                return cur

        return None

    def __str__(self):
        cur = self.head.next
        vs = ''
        while cur != self.tail:
            vs += cur.value
            cur = cur.next
            if cur != self.tail:
                vs += ' '

        return vs


def run():
    steps = int(input())  # flake8: noqa
    dl = DoubleLinkedList()

    for command in sys.stdin:
        if command.startswith('insert'):
            dl.insert(command[7:-1])
        elif command.startswith('deleteFirst'):
            dl.delete_first()
        elif command.startswith('deleteLast'):
            dl.delete_last()
        elif command.startswith('delete'):
            dl.delete_first_value(command[7:-1])
        else:
            raise ValueError()

    print(dl)


if __name__ == '__main__':
    run()



Measured memory_bytes: 227796
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>

using namespace std;
typedef long long LL;

class tea
{
public:
  static bool comparePredicate(tea left, tea right) {
	return (left.cost*8.0/left.vol < right.cost*8.0/right.vol); 
  }
  LL cost;
  int vol;
  tea(LL _cost,double _vol){
	cost=_cost;
	vol=_vol*8;
  }

};

int main(){
  //  0.25,0.5,1.0,2.0
  LL Q,H,S,D,N;
  cin>>Q>>H>>S>>D;
  cin>>N;
  
  vector<tea> t_tea_list;
  t_tea_list.push_back(tea(Q,0.25));
  t_tea_list.push_back(tea(H,0.5));
  t_tea_list.push_back(tea(S,1.0));
  t_tea_list.push_back(tea(D,2.0));

  vector<tea> tea_list;
  sort(t_tea_list.begin(),t_tea_list.end(),tea::comparePredicate);

  N=N*8;
  LL sum=0;
  for(int i=0;i<t_tea_list.size();i++){
	sum+=N/t_tea_list[i].vol*t_tea_list[i].cost;
	N=N%t_tea_list[i].vol;
  }

  cout << sum << endl;

  return 0;
}


Measured memory_bytes: 256
import sys
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, W = map(int, input().split())
dp = np.zeros(W+1, dtype=np.int64)

for _ in range(N):
    w, v = map(int, input().split())
    np.maximum(dp[:-w]+v, dp[w:], out=dp[w:])

print(dp[-1])


Measured memory_bytes: 14580
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
int h, n, a[N];
int main()
{
 	ios::sync_with_stdio(false);
 	cin.tie(0);
 	cin >> h >> n;
 	for(int i=1; i<=n; i++) cin >> a[i];
 	ll sum = accumulate(a+1, a+n+1, 0ll);
 	if(sum>=h) cout << "Yes\n";
 	else cout << "No\n";
	return 0;
}


Measured memory_bytes: 640
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>

using namespace std;
typedef long long ll;
int N;
int A[100001];
int B[100001];
int C[100001];
ll suma, sumb, sumc;

int main() {
   cin >> N;
   suma = 0;
   sumb = 0;
   for (int i = 0; i < N; i++) {
      cin >> A[i];
      suma += A[i];
   }
   for (int i = 0; i < N; i++) {
      cin >> B[i];
      sumb += B[i];
      C[i] = A[i] - B[i];
   }
   if (suma < sumb) {
      cout << "-1" << endl;
      return 0;
   }

   sort(C, C + N);
   sumc = 0;
   int ans = 0;
   for (int i = 0; i < N; i++) {
      if (C[i] < 0) {
         sumc += abs(C[i]);
         ans++;
      }
   }
   int i = 0;
   while (sumc > 0) {
      sumc -= C[N - 1 - i];
      ans++;
      i++;
   }
   cout << ans << endl;
   return 0;
}


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

vector<ll> Na;
vector<ll> Np;
int N; ll X;

ll paty(ll layers, ll depth){
  ll eaten = layers;
  eaten += 1;
  if(eaten == X){  // eat bottom B
    return 0;
  }

  eaten += Na[depth-1];
  if(eaten > X){    // eat bottom BA[depth-1]
    return paty(eaten - Na[depth-1], depth - 1);
  }else if(eaten == X){
    return Np[depth-1];
  }

  eaten += 1;       // eat bottom BA[depth-1]P
  if(eaten == X){
    return Np[depth-1] + 1;
  }

  eaten += Na[depth-1];   // eat bottom BA[depth-1]PA[depth-1]
  if(eaten > X){
    return Np[depth-1] + 1 + paty(eaten - Na[depth-1], depth-1);
  }else if(eaten == X){
    return Np[depth-1] + 1 + Np[depth-1];
  }

  eaten += 1; // eat bottom BA[depth-1]PA[depth-1]B
  if(eaten == X){
    return Np[depth-1] + 1 + Np[depth-1];
  }
  assert(false);
}

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

  Na = vector<ll>(51, 1);
  Np = vector<ll>(51, 1);
  for(int i = 1; i < 51; i++){
    Na[i] = 3 + 2 * Na[i-1];
    Np[i] = 1 + 2 * Np[i-1];
  }

  cout << paty(0, N) << endl;
}

Measured memory_bytes: 256
//clear adj and visited vector declared globally after each test case
//check for long long overflow
//while adding and subs check if mod becomes -ve
//while using an integer directly in a builtin function add ll
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Dont keep array name as size or any other key word

#include <bits/stdc++.h> 
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second 
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)    
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
    int g=__gcd(a, b);
    return a/g*b;
}
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }

int32_t main()
{
    IOS;
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    cout<<max({b*d, b*c, a*c, a*d});
}

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

void solve()
{
	int h;
	cin>>h;
	if(h<600)
		cout<<8<<endl;
	else if(h<800)
		cout<<7<<endl;
	else if(h<1000)
		cout<<6<<endl;
	else if(h<1200)
		cout<<5<<endl;
	else if(h<1400)
		cout<<4<<endl;
	else if(h<1600)
		cout<<3<<endl;
	else if(h<1800)
		cout<<2<<endl;
	else
		cout<<1<<endl;

}

int main()
{
	ll t = 1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
}

Measured memory_bytes: 3580
#include <bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
#define debug printf("fuck %d\n", __LINE__);
inline LL read() {
	LL res = 0, bo = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') { if (ch == '-') bo = -1; ch = getchar(); }
	while(ch >= '0' && ch <= '9') { res = (res << 1) + (res << 3) + ch - '0'; ch = getchar(); }
	return bo * res;
}
template<typename T> void read(T &x) { x = read(); }
template<typename T, typename ...Argv> void read(T &a, Argv &...argv) { read(a); read(argv...); }

const int N = 2e5 + 100;

struct Edge {
	int to;
	Edge *nxt;
	Edge(int to, Edge *nxt) : to(to), nxt(nxt) {}
} *head[N];

struct node {
	LL dis; int id;
	node() {}
	node(LL dis, int id) : dis(dis), id(id) {}
	friend bool operator < (const node &a, const node &b) { return a.dis > b.dis; }
} a[N];

int siz[N], dis[N];

struct EDGE {
	int u, v;
	friend bool operator < (const EDGE &a, const EDGE &b) { return a.u == b.u ? a.v < b.v : a.u < b.u; }
} e[N];

void dfs(int x, int fa) {
	dis[x] = dis[fa] + 1;
	for (Edge *i = head[x]; i; i = i->nxt) {
		if (i->to == fa) continue;
		dfs(i->to, x);
	}
}

void add(int u, int v) { head[u] = new Edge(v, head[u]), head[v] = new Edge(u, head[v]); }

int main() {
	int n = read();
	for (int i = 1; i <= n; ++ i) a[i] = node(read(), i), siz[i] = 1;
	std::sort(a + 1, a + n + 1);
	for (int i = 1; i < n; ++ i) {
		LL d = a[i].dis - n + (siz[i] << 1);
		int l = 1, r = n, ans = 0;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (a[mid].dis >= d) l = mid + 1, ans = mid;
			else r = mid - 1;
			if (a[mid].dis == d) break;
		}
		if (a[ans].dis != d) { puts("-1"); return 0; }
		int v = a[i].id, u = a[ans].id;
		add(u, v); siz[ans] += siz[i];
		e[i].u = std::min(u, v); e[i].v = std::max(u, v);
	}
	dfs(a[n].id, 0); LL sum = 0;
	for (int i = 1; i <= n; ++ i) sum += dis[i] - 1;
	if (sum != a[n].dis) { puts("-1"); return 0; }
	std::sort(e + 1, e + n);
	for (int i = 1; i < n; ++ i) printf("%d %d\n", e[i].u, e[i].v); 
	return 0;
}

Measured memory_bytes: 15232
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int n = scanner.nextInt(), x = scanner.nextInt();
			int[] a = IntStream.range(0, n).map(i -> scanner.nextInt()).toArray();
			long sum = 0L;
			for (int i = 0; i < n - 1; i++) {
				if (a[i] + a[i + 1] > x) {
					sum += a[i] + a[i + 1] - x;
					a[i + 1] -= Math.min(a[i] + a[i + 1] - x, a[i + 1]);
				}
			}
			System.out.println(sum);
		}
	}
}


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

using namespace std;

int main(int argc, char** argv) {
  int N, K;
  cin >> N >> K;
  
  std::vector<int> H;
  for (int i=0; i < N; ++i) {
    int h;
    cin >> h;
    H.push_back(h);
  }

  std::sort(begin(H), end(H));

  if (K >= N) {
    cout << 0 << endl;;
  } else {
    long sum = 0;
    for (int i = 0; i < N - K; ++i) {
      sum += H[i];
    }
    cout << sum << endl;
  }
  return 0;
}


Measured memory_bytes: 4256
N, M = map(int, input().split())
base = []
for i in range(M):
  (a, b) = map(int, input().split())
  base.append((a, b, i))
sorted_target = sorted(base) 

prefecture_index = sorted_target[0][0]

i=0
result = {}

for target in sorted_target:
  if prefecture_index == target[0]:
    i+=1
  else:
    prefecture_index = target[0]
    i = 1
  result[target[2]] = [prefecture_index, i]

for i in range(M):
  print("{:0=6}{:0=6}".format(result[i][0], result[i][1]))


Measured memory_bytes: 41488
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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();
		int c = sc.nextInt();
		String s = sc.next();
		sc.close();

		ArrayList<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < s.length(); i++) {
			if (s.substring(i, i + 1).equals("o")) {
				list.add(i + 1);
			}
		}
		k = (int)Math.min(k, list.size());

		List<Integer> listK1 = new ArrayList<Integer>();
		int nextDay = 1;
		for (int i = 0; i < list.size(); i++) {
			int day = list.get(i);
			if (day >= nextDay) {
				listK1.add(day);
				nextDay = day + c + 1;
			}
		}
		listK1 = listK1.subList(listK1.size() - k, listK1.size());

		List<Integer> listK2 = new ArrayList<Integer>();
		int prevDay = n;
		Collections.reverse(list);
		for (int i = 0; i < list.size(); i++) {
			int day = list.get(i);
			if (day <= prevDay) {
				listK2.add(day);
				prevDay = day - c - 1;
			}
		}
		Collections.reverse(listK2);
		listK2 = listK2.subList(0, k);

		for (int i = 0; i < listK1.size(); i++) {
			int k1 = listK1.get(i);
			int k2 = listK2.get(i);
			if (k1 == k2) {
				System.out.println(k1);
			}
		}
	}
}

Measured memory_bytes: 65968
#include <iostream>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i,s,n)for(ll i=s;i<n;i++)
#define repe(i,s,n)for(ll i=s;i<=n;i++)
#define rep_r(i,s,n)for(ll i=n-1;i>=s;i--)
#define repe_r(i,s,n)for(ll i=s;i<=n;i++)
static const ll MOD = 1e9 + 7;
static const double PI = 3.141592653589793;
static const ll MAX_L = (ll)1 << 62;

map<ll, pair<ll, ll>> mp;
map<ll, ll[100000]> d_mp;
ll y[90001] = {};
ll x[90001] = {};
ll v[90001] = {};

int main() {
	ll h, w, d; cin >> h >> w >> d;

	repe(i, 1, h)repe(j, 1, w) {
		ll a; cin >> a;
		y[a] = i;
		x[a] = j;
	}

	repe(i, 1, h*w) {
		if (i <= d) v[i] = 0;
		else v[i] = abs(x[i] - x[i - d]) + abs(y[i] - y[i - d]);
	}
	repe(i, d + 1, h*w) {
		v[i] += v[i - d];
	}

	ll q; cin >> q;
	rep(i, 0, q) {
		ll l, r; cin >> l >> r;
		cout << v[r] - v[l] << endl;
	}
	return 0;
}

Measured memory_bytes: 3200
#include <stdio.h>
      
int main()
{
    long n, m, l, data1[200][200], data2[200][200], data3[200][200], i, j, k;
      
    for (i = 0; i < 200; i++) {
        for (j = 0; j < 200; j++) {
            data3[i][j] = 0;
        }
    }
      
    scanf("%d%d%d", &n, &m, &l);
      
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            scanf("%d", &data1[i][j]);
        }
    }
      
    for (i = 0; i < m; i++) {
        for (j = 0; j < l; j++) {
            scanf("%d", &data2[i][j]);
        }
    }
      
    for (i = 0; i < n; i++) {
        for (j = 0; j < l; j++) {
            for (k = 0; k < m; k++) {
                data3[i][j] += data1[i][k] * data2[k][j];
            }
            printf("%ld", data3[i][j]);
            if (j != l - 1) {
                printf(" ");
            }
            else if (j == l - 1) {
                printf("\n");
            }
        }
    }
      
    return 0;
}

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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be() , (a).en()
#define REP(i,n) for(int (i)=0;(i)<(n);(i)++)        //repeat n times
#define REP2(i,s,n) for(int (i)=(s);(i)<(n);(i)++)   //repeat from s to n 
#define REPD(i,n) for(int (i)=(n);(i)>=0;(i)--)      //repeat from n to 0
#define REPD2(i,s,e) for(int (i)=(s);(i)>=(e);(i)--) //repeat from s to e
#define RANGE(i,v) for(auto &(i):v)                  //repeat range
#define ASIZE(a) (sizeof(a) / sizeof(a[0]))          //array size

using LL = long long;

template<typename T> using V = vector< T >;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

const int INF = 1999999999;

template<typename T>
T MIN(V<T> v){return *min_element(ALL(v));}

template<typename T>
T MAX(V<T> v){return *max_element(ALL(v));}

int main(){
    int n;
    cin>>n;
    Vi x(n);
    REP(i,n) cin>>x[i];

    int x_min = MIN(x);
    int x_max = MAX(x);

    int ans = INF;
    REP2(i,x_min,x_max+1){
        int total = 0;
        REP(j,n){
            total += (x[j]-i) * (x[j]-i);
        }
        ans = min(ans,total);
    }

    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func find(parent []int, i int) int {
	if parent[i] < 0 {
		return i
	}
	parent[i] = find(parent, parent[i])
	return parent[i]
}

func unite(parent []int, i, j int) {
	i = find(parent, i)
	j = find(parent, j)
	if i == j {
		return
	}
	parent[j] += parent[i]
	parent[i] = j
}

func main() {
	defer flush()

	N := readInt()
	Q := readInt()

	parent := make([]int, N)
	for i := 0; i < N; i++ {
		parent[i] = -1
	}

	for i := 0; i < Q; i++ {
		t := readInt()
		u := readInt()
		v := readInt()
		if t == 0 {
			unite(parent, u, v)
		} else if t == 1 {
			if find(parent, u) == find(parent, v) {
				println(1)
			} else {
				println(0)
			}
		}
	}
}

const (
	ioBufferSize = 1 * 1024 * 1024 // 1 MB
)

var stdinScanner = func() *bufio.Scanner {
	result := bufio.NewScanner(os.Stdin)
	result.Buffer(make([]byte, ioBufferSize), ioBufferSize)
	result.Split(bufio.ScanWords)
	return result
}()

func readString() string {
	stdinScanner.Scan()
	return stdinScanner.Text()
}

func readInt() int {
	result, err := strconv.Atoi(readString())
	if err != nil {
		panic(err)
	}
	return result
}

var stdoutWriter = bufio.NewWriter(os.Stdout)

func flush() {
	stdoutWriter.Flush()
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdoutWriter, args...)
}


Measured memory_bytes: 5852
from collections import defaultdict

def prime_factorize(n):
  a = defaultdict(int)
  while n % 2 == 0:
    a[2] += 1
    n //= 2
  f = 3
  while f * f <= n:
    if n % f == 0:
      a[f] += 1
      n //= f
    else:
      f += 2
  if n != 1:
    a[n] += 1
  return a

def calc(i):
  c = 1;
  acc = 0;
  count = 0
  while acc < i:
    acc += c
    c += 1
    if acc<=i:
      count+=1
  return count;

def main():
  n = int(input())
  if(n==1):
    print(0)
    return;
  facts = prime_factorize(n)
  count = 0
  for fact in facts:
    count += calc(facts[fact])
  print(count)

main()

Measured memory_bytes: 9472
#include<bits/stdc++.h>
using namespace std;
#define int long long
char s[20];
int n,a[20],p[20],ans;
int geta(int l,int r){
    int k = 0;
    for(int i = l; i <= r; i++){
        k = k * 10 + a[i];
    }
    return k;
}
int C(int m,int n){
    return p[m] / (p[m - n] * p[n]);
}
int num(int l,int r){
    int m = r - l;
    int k = 0;
    for(int i = 0; i <= m; i++) k += C(m,i);
    return k;
}
void f(int l,int r){
    if(l == 1 && r == n) ans += geta(l,r);
    else if(l == 1) ans += geta(l,r) * num(r + 1,n);
    else if(r == n) ans += geta(l,r) * num(1,l - 1);
    else ans += geta(l,r) * num(r + 1,n) * num(1,l - 1);
}
signed main(){
    cin >> s + 1;
    n = strlen(s + 1);
    for(int i = 1; i <= n; i++) a[i] = s[i] - '0';
    p[0] = 1;
    for(int i = 1; i <= n; i++) p[i] = p[i - 1] * i;
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            f(i,j);
        }
    }
    printf("%lld",ans);
    // printf("%lld",geta(1,n));
}

Measured memory_bytes: 3760
parseInt(s)=parse(Int,s)
MOD=10^9+7
N,a,b=parseInt.(split(readline()))

function Cmod(a,b,M)
    A=1
    for i in 1:b
        A*=((a-i+1)*invmod(i,M))%M
        A%=M
    end
    A
end

print((powermod(2,N,MOD)-1-Cmod(N,a,MOD)-Cmod(N,b,MOD)+3MOD)%MOD)

Measured memory_bytes: 172044
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <queue>
#include <stack>
#include <cmath>
#include <numeric>
#include <algorithm>

#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define all(v) ((v).begin(),(v).end())
#define Sort(v) sort(all(v))
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int main(){
    int n;
    cin>>n;
    vector<int> v(n);
    rep(i,0,n)cin>>v[i];
    sort(v.begin(),v.end());
    cout<<v.back()-v[0]<<endl;
}



    



Measured memory_bytes: 256
// Author: Jinhai Chen
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;

string s; 
string t="keyence";

int main() {
#ifndef ONLINE_JUDGE	
	//freopen("data.in","r",stdin);
#endif
	string s;
	while(cin>>s) {
		for(int i=1;i<s.size();i++) {
			for(int j=i;j<s.size();j++) {
				string t1=s.substr(0,i);
				string t2=s.substr(j);
				// cout<<t1<<" "<<t2<<endl;
				if(t1+t2==t) { puts("YES"); goto AAA; }
			}
		}
		puts("NO");
		AAA:
			;
	}
	return 0;
}














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

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repr(i, n) for(int i = (n-1); i >= 0; --i)

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

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

    vector<int> A(N);
    rep(i, N) cin >> A[i];

    if (H <= accumulate(A.begin(), A.end(), 0)) {
        puts("Yes");
    } else {
        puts("No");
    }
}

Measured memory_bytes: 640