akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;

const int MAX_H = 100, MAX_W = 100;
const int INF = 10000;
int H, W;
char a[MAX_H][MAX_W + 1];//????????¨???
int memo[MAX_H][MAX_W];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

//solve()
//????????¢???????¶?????????????????????¢??°
void dfs(int y, int x) {
	memo[y][x] = INF;
	char c = a[y][x];
	a[y][x] = 'a';
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (memo[ny][nx] == 0 && 0 <= nx && nx < W && 0 <= ny && ny < H && a[ny][nx] == c)dfs(ny, nx);
	}
}




void solve() {
	//??????????????¢????????£?¶??????????????????????????????¨????¨?????????°????????????	
	//memset(memo, 0, sizeof(memo));
	for (int y = 0; y < H; y++) {
		for (int x = 0; x < W; x++) {
			memo[y][x] = 0;
		}
	}

	int counter = 0;
	for (int y = 0; y < H; y++) {
		for (int x = 0; x < W; x++) {
			if (a[y][x] == '#' || a[y][x] == '*' || a[y][x] == '@') {
				counter++;
				dfs(y, x);
			}
		}
	}

	cout << counter << endl;
}







int main()
{
	while(true) {
		cin >> H >> W;
		if (H == 0 && W == 0)break;
		for (int y = 0; y < H; y++) {
			for (int x = 0; x < W; x++) {
				cin >> a[y][x];
			}
		}
		solve();
	}
	return 0;
}

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

int main() {
  int N;
  int l,r;
  cin >> N;
  int sum = 0;
  for (int i = 0;i < N; i++){
    cin >> l >> r ;
    sum += (r - l + 1 );
  }
  cout << sum << endl;
}


Measured memory_bytes: 256
n=int(input())
arr=list(map(int,input().split()))
for i in range(n):
  arr[i]=arr[i]//400
  if arr[i]>=9:
    arr[i]=8
cnt=[arr.count(i) for i in range(9)]
ans=0
for i in range(8):
  if cnt[i]!=0:
    ans+=1
if ans==0:
  print(1,cnt[-1])
else:
  print(ans,ans+cnt[-1])

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

int main() {
  char *buf=(char*)malloc(sizeof(char) * 20000);

  scanf("%[^\n]", buf);
  char* cur = buf;
  int length = strlen(buf);

  char l1[] = "dream";
  char l2[] = "dreamer";
  char l3[] = "erase";
  char l4[] = "eraser";
  int a1 = 5;
  int a2 = 7;
  int a3 = 5;
  int a4 = 6;

  while (length > 0) {
    if (strstr(cur, l4) == cur) {
      length -= a4;
      if(length <= 0) {
        break;
      }
      cur = cur + a4;
    } else if (strstr(cur, l3) == cur) {
      length -= a3;
      if(length <= 0) {
        break;
      }
      cur = cur + a3;
    } else if (strstr(cur, "dreamerase") == cur) {
      length -= a1;
      if(length <= 0) {
        break;
      }
      cur = cur + a1;
    } else if (strstr(cur, l2) == cur) {
      length -= a2;
      if(length <= 0) {
        break;
      }
      cur = cur + a2;
    } else if (strstr(cur, l1) == cur) {
      length -= a1;
      if(length <= 0) {
        break;
      }
      cur = cur + a1;
    } else {
      break;
    }
  }

  if (length == 0) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }

  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i, a, b)    for (int i = a; i <= (int) b; ++i)
#define FOD(i, a, b)    for (int i = a; i >= b; --i)
using namespace std;
int TTest, vs[15], num[3], f[(1 << 13) + 3][2];

void init() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("main.inp","r",stdin);
    //freopen("main.out","w",stdout);
}

int BIT(int x, int t) { return (x >> (t - 1)) & 1; }
bool move_able(int x, int t) {
    return !BIT(x, t) && ( (t > 1 && BIT(x, t - 1)) || (t < 13 && BIT(x, t + 1)) );
}
void prf(int x) { FOR(i, 1, 13) cerr << BIT(x, i) << ' '; cerr << '\n'; }

int get(int mask, int p) {
    int &shc = f[mask][p];
    if (shc > -1) return shc;

    int last = 0;
    FOR(i, 1, 13) if (vs[i] == p && !BIT(mask, i)) ++last;
    if (last == 0) return shc = true;

    bool foo = false;
    FOR(i, 1, 13) if (vs[i] == p && move_able(mask, i)) {
        foo = true;
        if (! get(mask + (1 << (i - 1)), 3 - p)) return shc = true;
    }
    if (! foo) return shc = !get(mask, 3 - p); else return shc = false;
}

void solve() {
    cin >> TTest;
    while (TTest--) {
        FOR(i, 1, 13) vs[i] = 2;
        FOR(i, 1, 6) { int x; cin >> x, vs[x] = 1; }

        memset(f, -1, sizeof f);
        cout << (get(64, 1) ? "yes\n" : "no\n");
    }
}

main() {
    init(); solve();
}



Measured memory_bytes: 3308
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
using namespace std;
 
#define ff                  first
#define ss                  second
#define endl                "\n"
#define loop(i,s,e)         for(int i=s;i<e;i++)
#define int                 long long
#define vi                  vector<int> 
#define S(v)                sort(v.begin(),v.end())
#define RS(v)               sort(v.rbegin(),v.rend())
#define R(v)                reverse(v.begin(),v.end())
#define mxpq(T)             priority_queue <T>
#define mnpq(T)             priority_queue<T,vector<T>,greater<T>>
#define prv(v)              for(auto &x : v)cout << x << " ";
#define piirv(v)            for(auto &x : v)cout << x.ff << " " << x.ss << endl;
#define prvv(v)             for(auto &x : v)for(auto &y : x.ss)cout << y << " ";
#define mp                  make_pair
#define pii                 pair<int,int>
#define mset(a,f)           memset(a , f , sizeof(a))
#define pb                  push_back
#define inf                 INT_MAX
#define INF                 LLONG_MAX
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
 
const int N = 2e6 + 6;
const int M = 1e9+7;
int A[N];
int n , m;

void solve()
{
	cin >> n;

	loop(i,0,n)cin >> A[i];

	int ok = 0;

	vector<int> vec;
	int i = 0;
	while(i < n)
	{
		if(!ok)
		{
			if(A[i] < A[i+1])
			{
				vec.pb(A[i]);
				int j = i+1;
				while(j < n && A[j] >= A[j-1])j++;
				if(i==n)
				{
					vec.pb(A[n-1]);
				}
				else
				{
				vec.pb(A[j-1]);
				i = j-1;
				}
				ok = 1;
			}
			else i++;
		}
		else
		{
			if(A[i] > A[i+1])
			{
				// cout << i <<  endl;
				int j = i+1;
				while(j < n && A[j] <= A[j-1])j++;
				if(i==n)
				{
					continue;
				}
				else 
				{
					i = j - 1;
				}
				ok = 0;
			}
			else {
				// cout << i << endl;10
				i++;
			}
		}
	}

	int sz = vec.size();
	int cur = 1000;
	if(sz == 0)
	{
		cout << cur << endl;
	}
	else 
	{
		if(sz%2)sz--;
		for(int i = 0; i < sz;i += 2)
		{
			int rt = cur/vec[i];
			cur -= (rt*vec[i]);
			cur += (rt*vec[i+1]);
		}
		cout << cur;
	}
	// cout << endl;
	// for(auto &it : vec)cout << it << " ";
}			
 
int32_t main()
{
 
cout<<fixed<<setprecision(16);
 
ios_base:: sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
 	
 
 	int t = 1;
 	// cin >> t;
 	while(t--)solve();
  
}

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

using namespace std;
using ll = long long;
using ull = unsigned long long;

namespace
{
	struct modint
	{
		static constexpr ll MOD = 1000000007;
		ll x_;
		modint(ll x) : x_(x % MOD) {}
		modint& operator+=(const modint& src)
		{
			if ((x_ += src.x_) >= MOD)
				x_ -= MOD;
			return *this;
		}
		modint& operator-=(const modint& src)
		{
			if ((x_ += MOD - src.x_) >= MOD)
				x_ -= MOD;
			return *this;
		}
		modint& operator*=(const modint& src)
		{
			(x_ *= src.x_) %= MOD;
			return *this;
		}

		modint& operator/=(const modint& src)
		{
			return (*this) *= src.inv();
		}
		modint inv() const
		{
			return pow(MOD - 2);
		}
		modint pow(ll t) const
		{
			if (t == 0)
				return 1;
			modint a = pow(t >> 1);
			a *= a;
			if (t & 1)
				a *= *this;
			return a;
		}
	};
	struct Node {
		int parent;
		int dist;
		set<int> children;
	};
	vector<Node>tree;

	void dfs(Node& node, modint& ct, int K) {
		int nC = node.children.size();
		auto p = node.children.begin();
		int x = node.dist == 0 ? 1 : 2;
		for (int i = 0; i < nC; ++i, ++p)
		{
			ct *= K - x - i;
			dfs(tree[*p], ct, K);
		}

	};
}

void ABC133E()
{
	int N, K;
	cin >> N >> K;
	vector<set<int> > graph(N);
	for (int i = 0; i < N - 1; ++i)
	{
		int a, b;
		cin >> a >> b;
		graph[a - 1].insert(b - 1);
		graph[b - 1].insert(a - 1);
	}

	tree.resize(N);
	tree[0] = Node{ -1,0,graph[0] };
	queue<int> qu;
	qu.push(0);
	while (!qu.empty())
	{
		int i = qu.front(); qu.pop();
		for (auto& x : tree[i].children)
		{
			graph[x].erase(i);
			Node theNode{ i,tree[i].dist + 1, graph[x] };
			tree[x] = theNode;
			qu.push(x);
		}
	}

	modint ct = K;
	dfs(tree[0], ct, K);
	cout << ct.x_ << endl;
}
int main()
{
	ABC133E();
	return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n+1);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i+1);
  }

  vector<ll> dp(n+1);
  // 0日目の株価の売買が終了した後の所持金
  dp.at(0) = 1000;
  // 1日目からN日目の、株価の売買が終了した後の所持金の最大を求める
  for (int k = 1; k <= n; k++) {
    dp.at(k) = 0;
    // k = 株の売却日
    // i = 起点日
    // m = 起点日の翌日から売却日の前日の間で、最も安い株価
    //
    // 株価の変動を以下と仮定する
    //
    //   100 130 130 130 115 115 150
    //
    // k=1の場合、つまり1日目の終了時
    //
    //   0日目の終了日から1日目の間で、購入タイミングが無いためスキップ
    //
    // k=2の場合、つまり2日目の終了時
    //
    //   k=2
    //   i=0
    //   m=100 の一択
    //   dp[2] = max(dp[2], dp[0]/100*a[2] + dp[0]%100)
    //         = max(0, 1000/100*130 + 1000%100)
    //         = max(0, 1300)
    //         = 1300
    //
    // k=7の場合、つまり7日目の終了日
    //
    //   k=7
    //   i=0
    //   m=100
    //   dp[7] = max(dp[7], dp[0]/100*150 + dp[0]%100)
    //         = max(0, 1000/100*150 + 1000%100)
    //         = max(0, 1500)
    //         = 1500
    //
    //   // 株の売却日が最終日 k=7
    //   // 株の売買を終了した日 (起点日) は i=2
    //   // その翌日 3日〜6日の間で、最小の株価を調べる m=115
    //   // 1株115円で株を購入し、7日目に150円で売却する
    //   // この際、i=2 つまり2日目の終了日の所持金の最大値は 1300 円
    //   //   これは k=2 の時に算出したもの
    //   //
    //   // これらより、2日目の終了日に1300円保持している状態で、
    //   // 3〜6日目に115円で株を最大限に購入し、7日目に150円で売却し、
    //   // 所持金が1685円になる
    //
    //   k=7
    //   i=2
    //   m=115
    //   dp[7] = max(dp[7], dp[2]/115*150 + dp[2]%100)
    //         // k=2 の時に取りうる所持金の最大は1300
    //         //
    //         //   dp[2] = 1300
    //         //
    //         // これはループの k=2 の時に既に求めている
    //         = max(1500, 1300/115*150 + 1300%115)
    //         = max(0, 1650 + 35)
    //         = max(0, 1685)
    //         = 1685
    //
    //  k=5の場合、所持金の最大値は1150となる
    //  k=2の場合、所持金が1300となるが、3日〜4日に130円で株を購入し、
    //  5日目に115円で売却するケースをシミュレーションすると、1株15円損するため
    //  所持金が1150円となる
    //
    for (int i = 0; i < k-1; i++) {
      int m = 99999;
      for (int j = i+1; j < k; j++) {
        m = min(m, a.at(j));
      }
      // cout << "[from] k: " << k << ", i: " << i << ", dp[" << k << "]" << dp.at(k) << endl;
      dp.at(k) = max(dp.at(k), dp.at(i) / m * a.at(k) + dp.at(i) % m);
      // cout << "[to  ] k: " << k << ", i: " << i << ", dp[" << k << "]" << dp.at(k) << endl;
    }
  }

  // i と k で表す全パターンの期間について、
  // * 株価が下落して所持金が減額するケースも含めて必ず1回株を売買する、　
  // * その1回の株の売買は、所持金が最大となるようにする（減額であれば、最小となるようにする）
  //
  // 最後に全パターンの所持金を比較して、最大の所持金を選択する
  ll ans = 0;
  for (int i = 0; i < n+1; i++) {
    ans = max(ans, dp.at(i));
  }
  cout << ans << endl;
  return 0;
}


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

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 ll = long long;
using P = pair<ll,ll>;
using graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 998244353LL;

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

int main() {
  ll N,A,B;
  cin>>N>>A>>B;
  vector<ll> h(N);
  rep(i,N) cin>>h[i];
  ll l = 0,r = 10000000000;
  while(r-l>1){
    ll mid = (l+r)/2;
    vector<ll> rest(N);
    rep(i,N) {
      rest[i] = max(0LL,h[i]-B*mid);
    }
    ll cnt = 0;
    rep(i,N){
      if(rest[i]==0) continue;
      cnt += (rest[i]-1)/(A-B)+1;
    }
    if(cnt<=mid) r = mid;
    else l = mid;
  }
  cout<<r<<endl;
}

Measured memory_bytes: 4800
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

const double eps = 1e-9;
struct V {
    double x, y;
    V(double x=0, double y=0): x(x), y(y) {}
    V& operator+=(const V& v) { x += v.x; y += v.y; return *this;}
    V operator+(const V& v) const { return V(*this) += v;}
    V& operator-=(const V& v) { x -= v.x; y -= v.y; return *this;}
    V operator-(const V& v) const { return V(*this) -= v;}
    V& operator*=(double s) { x *= s; y *= s; return *this;}
    V operator*(double s) const { return V(*this) *= s;}
    V& operator/=(double s) { x /= s; y /= s; return *this;}
    V operator/(double s) const { return V(*this) /= s;}
    double dot(const V& v) const { return x*v.x + y*v.y;}
    double cross(const V& v) const { return x*v.y - v.x*y;}
    double norm2() const { return x*x + y*y;}
    double norm() const { return sqrt(norm2());}
    V rotate90() const { return V(y, -x);}
    int ort() const { // orthant
        if (abs(x) < eps && abs(y) < eps) return 0;
        if (y > 0) return x>0 ? 1 : 2;
        else return x>0 ? 4 : 3;
    }
    bool operator<(const V& v) const {
        int o = ort(), vo = v.ort();
        if (o != vo) return o < vo;
        return cross(v) > 0;
    }
};
istream& operator>>(istream& is, V& v) {
    is >> v.x >> v.y; return is;
}
ostream& operator<<(ostream& os, const V& v) {
    os<<"("<<v.x<<","<<v.y<<")"; return os;
}

int main() {
    int N;
    cin >> N;
    vector<V> dots(N);
    rep(i, N) cin >> dots[i];
    sort(dots.begin(), dots.end());
    double ans = 0;
    rep(l, N) {
        int r = l;
        V cur = V();
        rep(x, N) {
            cur += dots[r];
            r = (r+1) % N;
            ans = max(ans, cur.norm2());
        }
    }
    printf("%.10f\n", sqrt(ans));
    return 0;
}

Measured memory_bytes: 3808
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string p[1000];
    int i,j,k;
    string W,T,line;
    cin>>W;
     while (getline(cin,T))
    {
        if (T=="END_OF_TEXT")
            break;

        line=line+T+" ";
    }
    transform(W.begin(),W.end(),W.begin(),::tolower);
    transform(line.begin(),line.end(),line.begin(),::tolower);
    k=0;
   for(i=0; i<line.size(); i++)
    {
         if(line[i]>='a' && line[i]<='z')
            {
                string s;
                for(j=i;;j++)
                {
                    if(isspace(line[j]))
                    {
                        i=j--;
                        break;
                    }
                    else
                    {
                        s.push_back(line[j]);
                    }
                }
                p[k]=s;
                k++;
            }
    }
    int c=0;
    for (int i = 0; i < 1000; i++)
          if(p[i]==W)
            c++;

    cout<<c<<endl;
}


Measured memory_bytes: 3228
#include <queue>
#include <iostream>
#include <algorithm>
#define ll long long
#define INF 100000000000000
using namespace std;


int main(){
  ll n;
  cin >> n;
  cout << n << ":";
  vector<ll> ans;
  ll divisor = 2;
  while(1){
    if(divisor * divisor > n){
      ans.push_back(n);
      break;
    }
    if(n % divisor == 0){
      ans.push_back(divisor);
      n /= divisor;
    }else{
      divisor++;
    }
  }

  for(auto i : ans){
    cout << ' ' << i;
  }
  cout << endl;

}

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

int main()
{
	string str;
	int cnt = 0;
	while (cin >> str)
	{
		int s = str.size();
		int b = s/2;
		bool f = true;
		if (s%2)
		{
			for (int i=1; i<=b; i++)
			{
				if (str[b-i]!=str[b+i])
				{
					f = false;
					break;
				}
			}
		}
		else
		{
			for (int i=0; i<b; i++)
			{
				if (str[b-i-1]!=str[b+i])
				{
					f = false;
					break;
				}
			}
		}
		if (f) cnt++;
	}

	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  
  for (int i = 0; i < N; i++) {
	cin >> A[i];
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  int ret = 0;
  for (int i = 0; i < A.size(); i++) {
  	if (i % 2 == 0) ret += A.at(i);
    else ret -= A.at(i);
  }
  cout << ret << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#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 all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
 
int main() {
    ll n;
    cin >> n;
 
    vvl G(n);
    rep(i, n-1) {
        ll a, b;
        cin >> a >> b;
        a--;  b--;
        G[a].pb(b);
        G[b].pb(a);
    }
 
    vl c(n);
    rep(i, n) {
        cin >> c[i];
    }
    sort(all(c));

    vl deg(n);
    queue<ll> Q;
    rep(i, n) {
        deg[i] = G[i].size();
        if(deg[i] == 1) {
            Q.push(i);
        }
    } 
 
    vl d(n);    
    rep(i, n) { 
        ll v = Q.front();
        Q.pop();
        d[v] = c[i];
        rep(j, G[v].size()) {
            ll u = G[v][j];
            deg[u]--;
            if(deg[u] == 1) {
                Q.push(u);
            }
        }
    }

    ll M = 0;
    rep(i, n-1) {
        M += c[i];
    }

    out(M);
    rep(i, n) {
        cout << d[i] << " ";
    }
 
    cout << endl;
    return 0;
}

Measured memory_bytes: 1280
#pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <functional>
#include <math.h> 
#define enld '\n'
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
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; }
// ---------------------------------------------------------------------------


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  unordered_map<string,int> mp;
  vector<string> S(N);
  map<int,int> num;
  int mn = inf;
  for(int i=0; i<N; i++){
    cin >> S[i];
    reverse(S[i].begin(),S[i].end());
    mp[S[i]]++;
    num[S[i].size()]++;
    chmin(mn,int(S[i].size()));
  }
  ll ans = 0;
  for(int i=0; i<N; i++){
    vector<bool> alpha(26,false);
    bool fst = true;
    while(S[i].size()){
      alpha[S[i].back() - 'a'] = true;
      S[i].pop_back();
      if(fst){
        fst = false;
        continue;
      }
      if(S[i].size()+1 < mn) break;
      for(int k=0; k<26; k++){
        if(alpha[k]){
          S[i] += k + 'a';
          // cout << now << "\n";
          if(num.find(S[i].size()) == num.end()){
            S[i].pop_back();
            continue;
          }
          if(mp.find(S[i]) != mp.end()){
            ans += mp[S[i]];
          }
          S[i].pop_back();
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


Measured memory_bytes: 23940
import java.util.*;

public class Main{
  public static void main(String[]args){
    Scanner sc = new Scanner(System.in);
    String S = sc.next();
    char[] C = S.toCharArray();
    long N = Long.parseLong(sc.next());
    int s = S.length();
    Set<Character> set = new HashSet<>();
    long ans = 0;
    if(s == 1){
      ans = N/2;
    }else if(s == 2){
      if(S.charAt(0) == S.charAt(1)){
        ans = N;
      }
    }else{
      int count = 0;
      set.add(C[0]);
      for(int i = 1; i < s; i++){
        set.add(C[i]);
        if(C[i-1] == C[i]){
          C[i] = '-';
          count++;
        }
      }
      if(set.size() == 1 && s%2 == 1){
        if(N%2 == 0){
          ans = s * (N/2);
        }else{
          ans = ((s/2)+1)*(N/2)+(s/2)*((N/2)+1);
        }
      }else{
        if(C[0] == C[s-1]){
          ans = (count+1)*N-1;
        }else{
          ans = count*N;
        }
      }
      
      count = 0;
      set = new HashSet<>();
      String T = new StringBuilder(S).reverse().toString();
      C = T.toCharArray();
      long ans2 = 0;
      set.add(C[0]);
      for(int i = 1; i < s; i++){
        set.add(C[i]);
        if(C[i-1] == C[i]){
          C[i] = '-';
          count++;
        }
      }
      if(set.size() == 1 && s%2 == 1){
        if(N%2 == 0){
          ans2 = s * (N/2);
        }else{
          ans2 = ((s/2)+1)*(N/2)+(s/2)*((N/2)+1);
        }
      }else{
        if(C[0] == C[s-1]){
          ans2 = (count+1)*N-1;
        }else{
          ans2 = count*N;
        }
      }
      ans = ans < ans2 ? ans : ans2;
    }
    System.out.println(ans);
  }
}


Measured memory_bytes: 23764
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;

int main() {
    int N;
    cin>>N;
    int X, Y;
    cin>>X>>Y;
    X--;
    Y--;
    map<int, int> ansmap;
    for(int i=0;i<N;++i){
        for(int j=i+1;j<N;++j){
            if((i<=X & j<=X) | (Y<=i & Y<=j)){
                ansmap[j-i]++;
            }
            else if(i<=X & X<=j & j<=Y) {
                ansmap[min(j-i, (Y-j)+(X-i)+1)]++;
            }
            else if(X<=i & i<=Y & Y<=j){
                ansmap[min(j-i, (i-X)+(j-Y)+1)]++;
            }
            else if(X<=i & i<=Y & X<=j & j<=Y) {
                ansmap[min(j-i, (Y-j)+(i-X)+1)]++;
            }
            else{
                ansmap[(X-i)+(j-Y)+1]++;
            }
        }
    }
    for(int i=1;i<=N-1;++i){
        cout<<ansmap[i]<<endl;
    }
}


Measured memory_bytes: 384
#include "bits/stdc++.h"
#include <unordered_set>

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1;i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

#define LL long long int
#define LD long double

#define INF 1000000000
#define PI 3.14159265358979323846

#define _CRT_SECURE_NO_WARNINGS

using namespace std;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<LL> vll;
//==============================================
struct node { int p, l, r; };

vi D(100005);
node N[100005];

void print(int u) {
	cout << "node " << u << ": ";
	cout << "parent = " << N[u].p << ", ";
	cout << "depth = " << D[u] << ", ";

	if (N[u].p == -1) cout << "root, ";
	else if (N[u].l == -1) cout << "leaf, ";
	else cout << "internal node, ";

	cout << "[";

	for (int i = 0, c = N[u].l; c != -1; i++, c = N[c].r) {
		if (i) cout << ", ";
		cout << c;
	}
	cout << "]" << "\n";
}

int rec(int u, int p) {
	D[u] = p;
	if (N[u].r != -1) rec(N[u].r, p);
	if (N[u].l != -1) rec(N[u].l, p + 1);
	return 0;
}

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

	REP(i, n) {
		N[i].p = N[i].l = N[i].r = -1;
	}

	int id, k, nextId, broId;
	REP(i, n) {
		cin >> id >> k;

		REP(j, k) {
			cin >> nextId;
			if (j == 0) {
				N[id].l = nextId;
			} else {
				N[broId].r = nextId;
			}
			broId = nextId;
			N[nextId].p = id;
		}
	}


	int r;
	REP(i, n) {
		if (N[i].p == -1) {
			r = i;
		}
	}

	rec(r, 0);

	REP(i, n) {
		print(i);
	}
	return 0;
}

Measured memory_bytes: 7444
#include <bits/stdc++.h>
using namespace std; 
const int dx[]={1,0,-1,0,1,-1,-1,1}; 
const int dy[]={0,1,0,-1,1,1,-1,-1}; 
const int INF = 1e9; 
const long long LINF = 1e18; 
const double EPS = 1e-8; 
#define pb push_back
#define mk make_pair 
#define fr first 
#define sc second 
#define ll long long 
#define reps(i,j,k) for(int i = (j); i < (k); ++i) 
#define rep(i,j) reps(i,0,j) 
#define all(a) (a).begin(),(a).end() 
#define MOD 1000000007 
typedef pair<int,int> Pii;
typedef pair<Pii,int> P;
typedef vector<int> vi;
int main(){
	ll N;
	cin >> N;
	ll ans = 1;
	rep(i,N){
		ans *= (i+1);
		ans %= MOD;
	}


	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
//typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define FAST                 \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ve vector
#define bb break
#define cc continue
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p)
{
    ll res = 1; // Initialize result
    x = x % p;  // Update x if it is more than or
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
struct point
{
    ll x, y;
};
int main()
{
    FAST
        // freopen("input1.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        /*ll tests;
    cin>>tests;
    rep (gg,0,tests)
    {}*/
        ll x,
        y, a, b, c;
    cin >> x >> y >> a >> b >> c;
    ve<ll> p(a), q(b), r(c);
    rep(i, 0, a)
    {
        cin >> p[i];
    }
    rep(i, 0, b)
    {
        cin >> q[i];
    }
    rep(i, 0, c)
    {
        cin >> r[i];
    }
    sort(all(p));
    sort(all(q));
    sort(all(r));
    reverse(all(p));
    reverse(all(q));
    reverse(all(r));
    ve <ll> v1;
    rep (i,0,x)
    {
        v1.pb(p[i]);
    }
    rep (i,0,y)
    {
        v1.pb(q[i]);
    }
    rep (i,0,c)
    {
        v1.pb(r[i]);
    }
    sort(all(v1));
    reverse(all(v1));
    ll ans=0;
    rep (i,0,x+y)
    {
        ans+=v1[i];
    }
    cout<<ans<<"\n";
    // ll p1 = x - 1, p2 = y - 1, p3 = 0;
    // while (true)
    // {
    //     bool flag=true;
    //     if (p1>=0&&p[p1] <= q[p2])
    //     {
    //         if (p3 <= c - 1)
    //         {
    //             if (r[p3] > p[p1])
    //             {
    //                 p1--;
    //                 p3++;
    //                 flag=false;
    //             }
    //         }
    //     }
    //     else if (p2>=0)
    //     {
    //         if (p3 <= c - 1)
    //         {
    //             if (r[p3] > q[p2])
    //             {
    //                 p2--;
    //                 p3++;
    //                 flag=false;
    //             }
    //         }
    //     }
    //     if (flag)
    //     {
    //         bb;
    //     }
    // }
    // ll ans=0;
    // rep (i,0,p1+1)
    // {
    //     ans+=p[i];
    // }
    // rep (i,0,p2+1)
    // {
    //     ans+=q[i];
    // }
    // rep (i,0,p3)
    // {
    //     ans+=r[i];
    // }
    // cout<<ans<<"\n";
}


Measured memory_bytes: 8816
#include <bits/stdc++.h>
#define pi 3.14159
using namespace std;

int main()
 {
 	int x,y,i,j,f=0;
 	cin>>x>>y;
 	for(i=0;i<=100;i++)
 		{
 			for(j=0;j<=100;j++)
 			{
 				if((2*i+4*j)==y&&(i+j)==x)
 					f=1;
			 }
		 }
	
	if(f==1)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;

 	return 0;
}
 	
 					

Measured memory_bytes: 3624
def LI():return map(int,input().split())
def I(): return input().split()

a,b,c,x,y=LI()
lst=0
ans=float("inf")
for i in range(10**5+1):
    ans=min(ans,(2*i*c+max(0,x-i)*a+max(0,y-i)*b))
print(ans)

Measured memory_bytes: 9092
fun main(args: Array<String>) {
    val (h, w) = readLine()!!.split(" ").map { it.toLong() }
    if(w == 1L || h == 1L) println(1)
    else if(w%2 == 0L) println(h*w/2) 
    else{
        if(h%2 == 0L) println(w*h/2)
        else println((w*(h-1)/2 + w/2 + 1L))
    }
} 

Measured memory_bytes: 37904
#include <iostream>
#include <vector>

int main() {
  uint32_t N = 0;
  std::cin >> N;
  std::vector<uint32_t> A1(N + 1, 0);
  for (size_t i = 1; i < N + 1; i++) {
    std::cin >> A1[i];
    A1[i] += A1[i - 1];
  }

  std::vector<uint32_t> A2(N + 1, 0);
  for (size_t i = 1; i < N + 1; i++) {
    std::cin >> A2[i];
    A2[i] += A2[i - 1];
  }

  uint32_t max = 0;
  for (size_t i = 1; i < N + 1; i++)
    max = std::max(max, A1[i] + (A2.back() - A2[i - 1]));
  std::cout << max << std::endl;
  return 0;
}

Measured memory_bytes: 256
//#include "stdafx.h"
#include<bits/stdc++.h> 
#include<iostream>
#include<queue>
#include<string>
using namespace std;
string s;
int c=0,f;
int main()
{
	cin>>s;
	
	if((s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3])||(s[1]==s[2]&&s[2]==s[3])||(s[0]==s[1]&&s[1]==s[2]))
		cout<<"Yes\n";
	else
		cout<<"No\n";

	return 0;
}


Measured memory_bytes: 3648
import sys

s = sys.stdin.readline().rstrip()

for i in range(len(s)-1):
    if s[i] == s[i+1]:
        print(i+1, i+2)
        sys.exit()

for i in range(len(s)-2):
    if s[i] == s[i+2]:
        print(i+1, i+3)
        sys.exit()

print(-1, -1)

Measured memory_bytes: 39408
#include <iostream>
#include <assert.h>
#include <vector>
#include <unordered_map>
#include <queue>
#include <climits>
#include <cmath>
#include <algorithm>
using namespace std;
constexpr int64_t MOD = 1e9 + 7;

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

  int32_t N, K;
  cin >> N >> K;
  int32_t cnt = 0;
  while (N) {
    N /= K;
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

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

static const double eps = 10e-9;

int main(){
	double a,b;
	double eyesight[] = {0.2,0.6,1.1};
	int lFreq[4]={0,0,0,0};
	int rFreq[4]={0,0,0,0};

	while(~scanf("%lf %lf",&a,&b)){
		int idx1 = 3;
		int idx2 = 3;
		for(int i=0;i<3;i++){
			if(a < eyesight[i]){
				idx1 = i;
				break;
			}
		}

		for(int i=0;i<3;i++){
			if(b < eyesight[i]){
				idx2 = i;
				break;
			}
		}

		lFreq[idx1]++;
		rFreq[idx2]++;
	}

	for(int i=3;i>=0;i--){
		printf("%d %d\n",lFreq[i],rFreq[i]);
	}
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
#define debug(x) cerr<<#x<<":"<<x<<endl
const int maxn=(int)1e6+5;
int cnt[maxn];
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;cin>>n;
    rep(i,1,n-1){
        int x;cin>>x;
        cnt[x]++;
    }
    rep(i,1,n)cout<<cnt[i]<<endl;
}

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

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int MAX { 200010 };
const ll MOD { 1000000007 }, oo { 1LL << 62 };

int solve(ll L, ll R)
{
    int ans = 2020;
    set<int> fs;

    for (ll x = L; x <= min(R, L + 2020); ++x)
    {
        set<int> rs;

        for (ll y = x + 1; y <= min(R, L + 2020); ++y)
        {
            int r = (x*y) % 2019;

            if (rs.count(r))
                break;
//cout << "x = " << x << ", y = " << y << ", r = " << r << endl;
            rs.insert(r);
        }

        if (not rs.empty())
            ans = min(ans, *rs.begin());
    }

    return ans;
}

int main()
{
    ios::sync_with_stdio(false);

    ll L, R;
    cin >> L >> R;

    auto ans = solve(L, R);

    cout << ans << '\n';

    return 0;
}


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

using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds; 

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
*/
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define inf INT_MAX
#define llinf LONG_LONG_MAX

const int MAXN=2e6+1;
const int M=998244353;



int main(){
	
	int n;
	sfi(n)

	vector<ll> v(n);


	f(i,0,n){
		sfl(v[i])
	}

	ll maxi=v[0];

	ll ans=0;

	f(i,1,n){
		if(v[i]>=maxi){
			maxi=v[i];
		}
		else{
			ans=ans+maxi-v[i];
		}
	}

	pfln(ans)

	return 0;
}

Measured memory_bytes: 4812
long m=1e9+7,s=1,D[41][1<<17]={1};
X,j,k,T,M=1<<17,q;
main(i,n){
	scanf("%d%d%d%d",&n,&q,&X,&T);
	for(X=((1<<q|1)<<X|1)<<T-1;k/M?k=0,++j>9?s=s*10%m,j=0,i++<n:1:1;~T&X?D[i][T&=M-1]+=D[i-1][k-1],D[i][T]%=m:0)T=k+++k<<j;
	for(;M--;)s+=m-D[n][M];
	printf("%d",s%m);
}

Measured memory_bytes: 41088
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, w, a[N], b[N];
long long dp[100][N+1];
void sett(){
    scanf("%d%d", &n, &w);
    for(int i=0; i<n; ++i)
        scanf("%d%d", a+i, b+i);
    for(int i=0; i<n; ++i)
        for(int j=0; j<=N; ++j)
            dp[i][j] = 1e18;
}
int main() {
    
    sett();
    dp[0][0] = 0;
    dp[0][b[0]] = a[0];
    for(int i=1; i<n; ++i){
        for(int j=0; j<=N; ++j){
            if( j-b[i] >= 0 )
                dp[i][j] = min(dp[i-1][j], dp[i-1][j-b[i]] + a[i]);
            else    
                dp[i][j] = dp[i-1][j];
        }
    }
    long long ans = -1;
    for(int i=1; i<=N; ++i)
        if( dp[n-1][i] <= w )
            ans = i;
    cout << ans;
    return 0;
}


Measured memory_bytes: 78336
#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin >> s;
  int i;
  string t = "";
  for(i=0;i<s.length();i++){
    if(s[i]=='B' && t!="")t = t.substr(0,t.length()-1);
    else if(s[i]!='B')t += s[i];
  }
  cout << t << endl;
  return 0;
}


Measured memory_bytes: 256
N = int(input())
A = list(map(int,input().split()))
print(" ".join([str(a) for a in A]))

for i in range(1,N):
    v = A[i]
    j = i-1
    while A[j] > v and j >= 0:
        A[j+1] = A[j]
        j -= 1
    A[j+1] = v
    print(" ".join([str(a) for a in A]))


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

int merge_sort(int S[], int left, int right) {
    int cnt = 0;
    if (left == right) return cnt;
    int mid = (left + right) / 2;
    cnt += merge_sort(S, left   , mid  );
    cnt += merge_sort(S, mid + 1, right);
    queue<int> A;
    queue<int> B;
    for (int i = left; i <= mid; i++) {
        A.push(S[i]);
    }
    for (int i = mid + 1; i <= right; i++) {
        B.push(S[i]);
    }
    int j = left;
    while (!A.empty() && !B.empty()) {
        cnt++;
        if (A.front() <= B.front()) {
            S[j++] = A.front();
            A.pop();
        } else {
            S[j++] = B.front();
            B.pop();
        }
    }
    while (!A.empty()) {
        cnt++;
        S[j++] = A.front();
        A.pop();
    }
    while (!B.empty()) {
        cnt++;
        S[j++] = B.front();
        B.pop();
    }
    return cnt;
}

int main(){
    int n;
    cin >> n;
    int S[n];
    for (int i = 0; i < n; i++) {
        cin >> S[i];
    }
    int cnt = merge_sort(S, 0, n - 1);
    cout << S[0];
    for (int i = 1; i < n; i++) {
        cout << " " << S[i];
    }
    cout << endl;
    cout << cnt << endl;
    return 0;
}

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

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define all(x) begin(x), end(x)
#define uniq(x) (x).erase(unique(all(x)), end(x))
#define bit(n) (1LL << (n))
#define cdiv(a, b) (((a) - 1) / (b) + 1)
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr long double PI = 3.1415926535897932384626433832795028L;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<typename T> void fin(T mes) {
    cout << mes << endl;
    exit(0);
}
template<typename T, typename U> bool chmax(T &a, const U &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> bool chmin(T &a, const U &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
    os << "(" << rhs.first << ", " << rhs.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
struct setup {
    static constexpr int PREC = 20;
    setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} setup;

int N, M;
int A[110000];
signed main() {
    cin >> N >> M;
    int sum = 0;
    rep(i, M) {
        cin >> A[i];
        sum += A[i];
    }
    if (sum <= N) { cout << N- sum << endl; }
    else { cout << -1 << endl; }
}

Measured memory_bytes: 3668
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;

using namespace std;
using ll = long long int;
using edge = struct {
  int to;
  int cost;
};
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define AUTO(i, m) for (auto &i : m)
#define ALL(a) (a).begin(), (a).end()
#define MAX(vec) *std::max_element(vec.begin(), vec.end());
#define MIN(vec) *std::min_element(vec.begin(), vec.end());
#define MAXI(vec)                                                              \
  std::distance(vec.begin(), *std::max_element(vec.begin(), vec.end()));
#define MINI(vec)                                                              \
  std::distance(vec.begin(), *std::min_element(vec.begin(), vec.end()));
#define BIT(n, num) std::bitset<(n)>((num)).to_string()
#define MATMUL(a, b, c)                                                        \
  REP(i, a.size())                                                             \
  REP(j, b[0].size()) REP(k, a[0].size()) c[i][j] += a[i][k] * b[k][j];
#define CLA2(a) REP(i, a.size()) REP(j, a[0].size()) a[i][j] = 0;
#define REV(T) greater<T>()

ll pow_(ll a, ll n) {
  ll x = 1;
  while (n > 0) {
    if (n & 1)
      x = (x * a) % MOD;
    a = (a * a) % MOD;
    n >>= 1;
  }
  return x;
}

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

map<ll, int> prime_factor(ll n) {
  map<ll, int> ret;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

ll factorial(int n) {
  ll ans = 1;
  for (int i = 2; i <= n; i++) {
    ans *= i;
    ans %= MOD;
  }
  return ans;
}
ll P(int n, int m) {
  ll ans = 1;
  for (int i = m + 1; i <= n; i++) {
    ans *= i;
    ans %= MOD;
  }
  return ans;
}
ll power(int n, int m) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    ll tmp = power(n, m / 2);
    return (tmp * tmp) % MOD;
  } else {
    return (n * power(n, m - 1)) % MOD;
  }
}
ll C(int n, int m) {
  return (P(n, n - m) * power(factorial(m), MOD - 2)) % MOD;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll dp[100002] = {};
int main() {
  ll N, K;
  vector<ll> A;
  cin >> N >> K;
  REP(i, N) {
    ll po;
    cin >> po;
    A.push_back(po);
    dp[i] = llINF;
  }
  dp[0] = 0;
  REP(i, N) {
    FOR(j, 1, K + 1) {
      if (i + j < N)
        dp[i + j] = min(dp[i] + abs(A[i + j] - A[i]), dp[i + j]);
    }
  }
  cout << dp[N - 1] << endl;

  return 0;
}


Measured memory_bytes: 1912
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using P=pair<long long,long long>;
#define rep(i,n) for(long long i=0; i<(long long)n; i++)
#define req(i,n) for(long long i=n-1; i>=0; i--)
#define range(i,a,b) for(long long i=a; i<b; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define onBoard(y,x) (y>=0 && y<h && x>=0 && x<w)
#define vint vector<int>
#define pri_que priority_queue
#define vvint vector<vector<int>>
#define vstr vector<string>
#define vvchar vector<vector<char>>
#define vchar vector<char>
#define vp vector<pair<int,int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define show(x) cout<<#x<<"="<<x<<endl;
#define SUM(x) accumulate(x.begin(),x.end(),0)
#define MAX(x) *max_element(x.begin(),x.end())
#define MIN(x) *min_element(x.begin(),x.end())
#define couty cout<<"Yes"<<endl
#define coutn cout<<"No"<<endl
#define coutY cout<<"YES"<<endl
#define coutN cout<<"NO"<<endl
#define yn(x) cout<<(x?"Yes":"No")<<endl
#define YN(x) cout<<(x?"YES":"NO")<<endl
long long gcd(long long a,long long b){return b?gcd(b,a%b):a;}
long long lcm(long long a, long long b){return a/gcd(a,b)*b;}
const long long dx[8]={1,0,-1,0,1,-1,-1,1};
const long long dy[8]={0,1,0,-1,1,1,-1,-1};
const long long INF = 1e12;
const long long MOD = 1e9+7;

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(15);

    int n; cin >> n;
    string s; cin >> s;
    string t=s;
    sort(all(s));
    int res=0;
    rep(i,n){
        if(s[i]!=t[i]) res++;
    }
    cout<<res/2<<endl;
}

Measured memory_bytes: 3836
object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val s = sc.next

  val r = s.toSeq.zipWithIndex.filter { s =>
    if (s._2 % 2 == 0 && s._1 == 'L') {
      false
    } else if (s._2 % 2 == 1 && s._1 == 'R') {
      false
    } else {
      true
    }
  }

  if (s.length == r.length) {
    println("Yes")
  } else {
    print("No")
  }
}

Measured memory_bytes: 25772
x,n=map(int,input().split())
if n!=0:
  p=list(map(int,input().split()))  
else:
  print(x)
  exit()
if x not in p:
  print(x)
  exit()
for i in range(1,100000000000):
  p_=x-i
  q=x+i
  if p_ not in p:
    print(p_)
    exit()
  if q not in p:
    print(q)
    exit()


Measured memory_bytes: 9132
/*Function Template*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define Rep(i, n) for(ll i = 1; i < (n); i++)
#define ALL(a)  (a).begin(),(a).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll fac[MAX], finv[MAX], inv[MAX];


template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }

}
ll Len(ll n) {
  ll s=0;
  while(n!=0) s++, n/=10;
  return s;
}


ll Sint(ll n) {
  ll m=0,s=0,a=n;
  while(a!=0) s++, a/=10;
  for(ll i=s-1;i>=0;i--) m+=n/((ll)pow(10,i))-(n/((ll)pow(10,i+1)))*10;
  return m;
}


ll Svec(vector<ll> v){
  ll n=0;
  for(ll i=0;i<v.size();i++) n+=v[i];
  return n;
}


ll GCD(ll a,ll b) {
  return b ? GCD(b,a%b) : a;
}


ll LCM(ll a,ll b){
  return a/GCD(a,b)*b;
}


ll Factorial(ll n){
  ll m=1;
  while(n>=1) m*=n,n--;
  return m;
}

void runlength(string s,vector<pair<char,ll>> &p){
  ll x=1;
  if(s.size()==1){
    p.push_back(pair<char,ll>(s[0],1));
  }
  for(ll i=0;i<s.size()-1;i++){
    if(s[i]==s[i+1]){
      x++;
      if(i==s.size()-2){
        p.push_back(pair<char,ll>(s[i],x));
      }
    }else{
      p.push_back(pair<char,ll>(s[i],x));
      x=1;
      if(i==s.size()-2){
        p.push_back(pair<char,ll>(s[s.size()-1],x));
      }
    }
  }
}

ll COM(ll n,ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}


void Toupper(string &s){
  rep(i,s.size()) s[i]=s[i]-32;
}
void Tolower(string &s){
  rep(i,s.size()) s[i]=s[i]+32;
}


const int MAX_N=100010;
vector<bool> sieve_of_eratosthenes(){
  vector<bool> isPrime(MAX_N+1,true);
  for(int i=2;i<=MAX_N;i++){
    if(isPrime[i]){
      for(int j=2*i;j<=MAX_N;j+=i){
        isPrime[j]=false;
      }
    }
  }
  return isPrime;
}

vector<pint> prime_factorize(ll n){
  vector<pint> ans;
  for(ll p=2;p<=sqrt(n);p++){
    if(n%p!=0) continue;
    ll cnt=0;
    while(n%p==0){
      n/=p;
      cnt++;
    }
    ans.push_back(make_pair(p,cnt));
  }
  if(n!=1) ans.push_back(make_pair(n,1));
  return ans;
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
  IOS;
  ll n,k;
  cin>>n>>k;
  string s;
  cin>>s;
  s[k-1]=s[k-1]+32;
  cout<<s<<endl;
}

Measured memory_bytes: 256
n = int(input())
li = list(map(int,input().split()))

li.sort()

if li[0] >= 0:
    print(sum(li)-2*li[0])
    for i in range(n-2):
        print(li[0],li[i+1])
        li[0] -= li[i+1]
    print(li[n-1],li[0])
elif li[n-1] <= 0:
    print(abs(sum(li))+2*li[n-1])
    for i in range(n-1):
        print(li[n-1],li[i])
        li[n-1] -= li[i]
else:
    for i in range(n):
        if li[i] > 0:
            a = i-1
            break
    print(abs(sum(li[:a+1])) + sum(li[a+1:]))
    for i in range(n-a-2):
        print(li[0],li[a+1+i])
        li[0] -= li[a+1+i]
    for i in range(a+1):
        print(li[n-1],li[i])
        li[n-1] -= li[i]


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

typedef long long ll;

int main() {
    int n;cin>>n;
    ll t[n];
    rep(i,n)cin>>t[i];
    
    ll ans=t[0];
    rep(i,n-1){
        ans = ans / __gcd(ans, t[i+1]) * t[i+1];
    }
    cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256