akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

// clang-format off
#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); ++(i))
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); --(i))

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define endl '\n'
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

// constexpr i32 mod   = 998244353;
constexpr i32 mod   = 1e9 + 7;
constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

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

struct IoSetup { IoSetup(int x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

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

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// clang-format on

void solver() {
    string s;
    cin >> s;
    
    if (s.find('7') != -1) cout << "Yes" << endl;
    else cout << "No" << endl;
}

signed main(int argc, char *argv[]) {
    solver();
}


Measured memory_bytes: 3656
#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int main()
{
    int n;
    cin >> n;
    deque<ll> a(n);
    rep(i, n)
    {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    int i = 0, j = 0, cnt = 0;
    while (i < n)
    {
        int cc = a[i], f = 0;
        while (i < n && a[i] == cc)
        {
            f++;
            i++;
        }
        cnt += f % 2;
    }
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 1024
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))


def solve():
    n = ri()
    C = input()
    if C[0] == "R":
        R = [1]
        W = [0]
    else:
        R = [0]
        W = [1]

    for c in C[1:]:
        if c == "R":
            R.append(1 + R[-1])
            W.append(W[-1])
        else:
            W.append(1 + W[-1])
            R.append(R[-1])

    best_cost = min(R[-1], W[-1])
    for i in range(n):
        rl = R[i]
        wl = W[i]
        rr = R[-1] - rl
        wr = W[-1] - wl
        swaps = min(wl, rr)
        flips = wl + rr - 2 * swaps
        best_cost = min(best_cost, flips + swaps)
    print (best_cost)






mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()


Measured memory_bytes: 99056
N = gets.to_i
hs = gets.split.map(&:to_i)

INF = 1 << 60
dp = Array.new(N, INF)
dp[0] = 0

(0...N).each do |i|
  n = dp[i]
  h = hs[i]
  if i + 1 <= N - 1
    old_h = dp[i + 1]
    new_h = n + (h - hs[i + 1]).abs
    dp[i + 1] = new_h if new_h < old_h
  end
  if i + 2 <= N - 1
    old_h = dp[i + 2]
    new_h = n + (h - hs[i + 2]).abs
    dp[i + 2] = new_h if new_h < old_h
  end
end
puts dp[N - 1]


Measured memory_bytes: 8716
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main() {
    char S[3];
    cin >> S;
    if (S[0] != S[1 && S[1] != S[2] && S[2] != S[3]]) {
        cout << "Good" << endl;
    }
    else {
        cout << "Bad" << endl;
    }
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>     
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#define INF 2000000
#define MOD 1000000007
#define sym cout<<"---------"<<endl;
#define ll long long
#define mk make_pair
#define en endl
#define RE return 0
#define int ll
#define P pair<int,int>
using namespace std;
int dx[5]={1,0,-1,0,0},dy[5]={0,1,0,-1,0};
int gcd(int a,int b){if(a%b==0){return b;}else return gcd(b,a%b);}
int lcm(int a,int b){if(a==0){return b;} return a/gcd(a,b)*b;}

signed main(){
	int n; cin>>n;
	if((n==5 || n==7) || n==3) cout<<"YES"<<en;
	else cout<<"NO"<<en;
}


Measured memory_bytes: 256
n, a, b = map(int, input().split())
mod = 10**9+7
def Pow(a, n):
    fac = a
    res = 1
    for i in range(len(bin(n)[2:])):
        if n&1:
            res = res*fac%mod
        fac = fac*fac%mod
        n = n>>1
    return res
cnt = Pow(2, n) - 1
x = [0] * (b+1)
for i in range(1,b+1):
    x[i] = Pow(i, mod-2)
cmb = [0] * (b+1)
cmb[1] = n
for i in range(2, b+1):
    cmb[i] = cmb[i-1]*(n-i+1)%mod*x[i]%mod
cnt -= cmb[a]+cmb[b]
print(cnt%mod)

Measured memory_bytes: 18872
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MOD = 1000000007;

int N;
int L[111111], R[111111];

LL go() {
  multiset<PII> LR,RL;
  REP(i,N) {
    LR.insert({L[i], R[i]});
    RL.insert({R[i], L[i]});
  }

  int pos = 0;
  LL res = 0;
  REP(i,N) {
    PII lr, rl;
    if (i%2) {
      lr = *LR.rbegin();
      rl = {lr.nd, lr.st};
    } else {
      rl = *RL.begin();
      lr = {rl.nd, rl.st};
    }

    LR.erase(LR.find(lr));
    RL.erase(RL.find(rl));

    if (pos < lr.st) {
      res += (lr.st - pos);
      pos = lr.st;
    } else if (pos > lr.nd) {
      res += (pos - lr.nd);
      pos = lr.nd;
    }
  }

  return res + abs(pos);
}

int main() {
  // ios_base::sync_with_stdio(0);

  scanf("%d", &N);
  REP(i,N) scanf("%d%d", &L[i], &R[i]);

  LL result = go();
  REP(i,N) {
    swap(L[i], R[i]);
    L[i] = -L[i];
    R[i] = -R[i];
  }
  result = max(result, go());

  printf("%lld\n", result);
}


Measured memory_bytes: 10368
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int total = Integer.parseInt(sc.next());
        int cake = Integer.parseInt(sc.next());
        int donut = Integer.parseInt(sc.next());
        
        System.out.println((total-cake)%donut);
    }
}


Measured memory_bytes: 23124
n = int(input())
a = list(map(int, input().split()))

def GCD(a: int, b: int) -> int:
    return a if b == 0 else GCD(b, a % b)

l = [0]
r = [0]
for i in range(n):
    l.append(GCD(l[i], a[i]))
    r.append(GCD(r[i], a[n-1 - i]))
r = r[::-1]

ans = 1
for i in range(n):
    ans = max(ans, GCD(l[i], r[i+1]))

print(ans)


Measured memory_bytes: 14584
s = input()
n = len(s)
for i in range(n):
    if i%2 == 0: #奇数文字
        if s[i] == "L":
            print("No")
            exit()
    else: #偶数文字
        if s[i] == "R":
            print("No")
            exit()
print("Yes")


Measured memory_bytes: 2940
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC085A
{
    class Program
    {
        static void Main(string[] args)
        {
            String s;

            s = Console.ReadLine();

            char[] cs = s.ToCharArray();

            cs[3] = '8';

            Console.WriteLine(cs);

            Console.ReadKey();
        }
    }
}


Measured memory_bytes: 13140
#include<vector>
#include<iostream>
#include<queue>
#define N 100000
using namespace std;
class Edge{
public:
    int v;
    int weight;
    Edge(int v,int w){
        this->v=v;
        weight=w;
    }
};

vector<Edge> G[N];
vector<int> dis(N);
int n;

void bfs(int root){
    queue<int> Q;
    for(int i=0;i<n;i++) dis[i]=1<<30;
    Q.push(root);
    dis[root]=0;
    while(!Q.empty()){
        int s=Q.front();
        Q.pop();
        for(int i=0;i<G[s].size();i++){
            if(dis[G[s][i].v]>dis[s]+G[s][i].weight){
                dis[G[s][i].v]=dis[s]+G[s][i].weight;
                Q.push(G[s][i].v);
            }
        }
    }
}

void deal(){
    int s=0;
    int maxv=-1;
    int aim=0;
    bfs(0);
    for(int i=0;i<n;i++){
        if(dis[i]>maxv){
            maxv=dis[i];
            aim=i;
        }
    }

    bfs(aim);
    maxv=-1;
    for(int i=0;i<n;i++){
        if(dis[i]>maxv){
            maxv=dis[i];
        }
    }
    cout<<maxv<<endl;

}

int main(){
    cin>>n;
    int s,t,w;
    for(int i=0;i<n-1;i++){
        cin>>s>>t>>w;
        G[s].push_back(Edge(t,w));
        G[t].push_back(Edge(s,w));
    }
    deal();
    return 0;
}



Measured memory_bytes: 9620
#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
//#include<bits/extc++.h>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> set_t;
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pdd pair<double,double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x)*(x))
#define EPS 1e-6
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi acos(-1)
#define index Index
#define Line pll
typedef long long LL;
LL dp[1<<16];
int vis[1<<16];
LL val[1<<16];
LL DP(int x){
    if(vis[x])return dp[x];
    dp[x]=-1e18;
    for(int i=x;i>0;i=(i-1)&x){
        dp[x]=max(dp[x],DP(x-i)+val[i]);
    }
    vis[x]=1;
    return dp[x];
}
int main(){
    int n;
    scanf("%d",&n);
    LL a[20][20];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf("%lld",&a[i][j]);
        }
    }
    for(int i=0;i<(1<<n);i++){
        vector<int> v;
        for(int j=0;j<n;j++){
            if(i&(1<<j))
            v.pb(j);
        }
        LL sum=0;
        for(auto it:v){
            for(auto it2:v)
            sum+=a[it][it2];
        }
        val[i]=sum/2;
    }
    dp[0]=0;
    vis[0]=1;
    printf("%lld\n",DP((1<<n)-1));
}


Measured memory_bytes: 1536
# include <stdio.h>
# include <bits/stdc++.h>
using namespace std;
const pair < int , int > DD[] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
# define fi cin
# define fo cout
# define x first
# define y second
# define ll long long
# define IOS ios_base :: sync_with_stdio(0);cin.tie(0)
# define p(v) cerr << #v << " = " << v << '\n'
# define p2(v) cerr << #v << " = " << (complex < int > (v.x,v.y)) << '\n'
# define vi vector < int >
# define vl vector < ll >
# define pii pair < int , int >
# define mp make_pair
# define db long double
# define pb push_back
# define pdd pair < db , db >
int main(void)
{
    #ifdef CF
    freopen("input","r",stdin);
    #endif // CF
    srand(time(0));
    fo << fixed << setprecision(7);
    cerr << fixed << setprecision(7);
    ll n;
    fi>>n;
    ll ans = n / 11;
    n -= ans * 11;
    ans *= 2;
    if (!n)
        return fo << ans << '\n',0;
    if (n <= 6)
        ++ans;
    else
        ans += 2;
    fo << ans << '\n';
    cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
    return 0;
}


Measured memory_bytes: 256
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> v[100010];
int in[100010],ans[100010];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=1;i<=n-1+m;i++)
    {
        scanf("%d%d",&x,&y);
        v[x].push_back(y);
        in[y]++;
    }
    int rt;
    for(int i=1;i<=n;i++)
        if(in[i]==0)
        {
            rt=i;break;
        }
   queue<int>q;
   q.push(rt);
   while(!q.empty())
   {
       int p=q.front();
       q.pop();
       for(int i=0;i<v[p].size();i++)
       {
           int k=v[p][i];
           in[k]--;
           if(!in[k])
           {
               ans[k]=p;
               q.push(k);
           }
       }
   }
    for(int i=1;i<=n;i++)
        printf("%d\n",ans[i]);
    return 0;
}


Measured memory_bytes: 7040
H, W = map(int, input().split())

A_array = [list(map(int, input().split())) for i in range(H)]

ans_array = []

for i in range(H):
    for j in range(W - 1):
        if A_array[i][j] % 2 == 1:
            A_array[i][j] -= 1
            A_array[i][j + 1] += 1
            ans_array.append([(i + 1), (j + 1), (i + 1), (j + 2)])
    if i == H - 1:
        break
    if A_array[i][W - 1] % 2 == 1:
        A_array[i][W - 1] -= 1
        A_array[i + 1][W - 1] += 1
        ans_array.append([(i + 1), W, (i + 2), W])

print(len(ans_array))
for ans in ans_array:
    print(ans[0], ans[1], ans[2], ans[3])


Measured memory_bytes: 69340
import numpy
import sys
input = sys.stdin.readline
H,W = map(int,input().split())
INF = H * W
S = numpy.array([[INF if i == '.' else 0 for i in input().strip()] for _ in range(H)])

for h in range(1, H):
    S[h, :] = numpy.minimum(S[h,:],S[h-1,:] + 1)
for h in range(H - 2, -1, -1):
    S[h, :] = numpy.minimum(S[h,:],S[h+1,:] + 1)
for w in range(1, W):
    S[:, w] = numpy.minimum(S[:,w],S[:,w-1] + 1)
for w in range(W - 2, -1, -1):
    S[:, w] = numpy.minimum(S[:,w],S[:,w+1] + 1)

print(numpy.max(S))

Measured memory_bytes: 29036
import java.util.*;

class Main{
    public static void main(String[]args){
		Scanner sc = new Scanner(System.in);
		String line1 = sc.nextLine();
		String line2 = sc.nextLine();
        String ret = "NO";
        if(line1.charAt(0)==line2.charAt(2)){
            if(line1.charAt(2)==line2.charAt(0)){
            	if(line1.charAt(1)==line2.charAt(1)){
            		ret = "YES";
            	}
            }
        }
		System.out.println(ret);
    }
}


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

#define ll long long
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rel(i,x,y) for(int i=x-1;i>=y;i--)
#define all(x) x.begin(),x.end()

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int n; cin >> n;
    int cos[100010] = {};
    rep(i,0,n){
        int t; cin >> t;
        if(t>0) cos[t-1]++;
        cos[t]++;
        cos[t+1]++;
    }
    int ma=0;
    rep(i,0,100010){
        ma = max(ma,cos[i]);
    }
    cout << ma << endl;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
int n,m,minn=666666;
int Emilia(int x)
{
	int y=0;
	while(x>0)
	y+=x%10,x/=10;
	return y;
}
int main()
{
	scanf("%d",&n);
	m=n/2;
	for(int i=1;i<=m;i++)
	minn=min(minn,Emilia(i)+Emilia(n-i));
	printf("%d",minn);
	return 0;
}

Measured memory_bytes: 256
from itertools import product

n, m, x = map(int, input().split())
CA = [list(map(int, input().split())) for _ in range(n)]

cost = -1
for cand in product((0, 1), repeat=n):
  tmp = 0
  scores = [0] * m
  for bought, ca in zip(cand, CA):
    if not bought:
      continue
    for i in range(1, m + 1):
      scores[i - 1] += ca[i]
    tmp +=  ca[0]
  if min(scores) >= x and (cost == -1 or tmp < cost):
    cost = tmp
print(cost)

Measured memory_bytes: 9180
#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);

using namespace std;
const int N = 5e3 + 44;
const int X[] = {1,0,-1,0,1,-1,1,-1};
const int Y[] = {0,1,0,-1,1,1,-1,-1};
const int mod = 1e9 + 7;
const long long INF = 1e18;
const int inf = 1e9;

int a[N], b, c, x[N];

int dp[N][N];

int main () {
	boost;
	int n, m;
	cin >> n >> m;
	int M = (1 << n);
	for (int i = 0 ; i < m ; ++ i) {
		cin >> a[i] >> b;
		for (int j = 0 ; j < b ; ++ j) {
			cin >> c;
			--c;
			x[i] |= (1 << c);
		}
	}
	for (int i = 0 ; i < m ; ++ i) {
		for (int j = 0 ; j < M ; ++ j)
			dp[i][j] = inf;
	}
	dp[0][x[0]] = a[0];
	dp[0][0] = 0;
	for (int i = 0 ; i < m ; ++ i) {
		for (int j = 0 ; j < M ; ++ j) {
			dp[i + 1][j] = min (dp[i + 1][j], dp[i][j]);
			dp[i + 1][j | x[i + 1]] = min (dp[i + 1][j | x[i + 1]], dp[i][j] + a[i + 1]);
		}
	}
	if (dp[m - 1][M - 1] != inf)
		cout << dp[m - 1][M - 1];
	else cout << -1;
}

Measured memory_bytes: 20736
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<map>

using namespace std;

int i,j,k,X,Y,N,ans,n,m,a,A[1000001]={0};


int main()
{
  cin>>N;
  for(i=0;i<N;i++)
  {
    cin>>j;
    A[j]++;
  }
  
  ans=0;
  for(i=1;i<=1000000;i++)
  {
    if(A[i]==0) continue;
    
    if(A[i]==1)
    {
      ans++;
    }
    
    for(k=1;k*i<=1000000;k++)
    {
      A[k*i]=0;
    }
  }
  
  cout<<ans;
  
}

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

using namespace std;

#define MAX_N 105
#define MAX_K 100005

int n, k, y[MAX_N];

int dp[MAX_K];

bool f_imp(int);

bool f(int p)
{
	if (dp[p] == 0)
		dp[p] = f_imp(p) + 1;
	return dp[p] - 1;
}

bool f_imp(int p)
{
	for (int i = 0; i < n; i++)
	{
		if (p >= y[i] && !f(p - y[i]))
			return true;
	}
	return false;
}

int main()
{
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", y + i);
	}
	if (f(k))
		printf("First\n");
	else
		printf("Second\n");
}


Measured memory_bytes: 5376
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
    int N;
    cin>>N;
    vector<long long> A(N);
    for (long long &a: A)
        cin>>a;
    vector<vector<int>> E(N);
    for (int i=0; i<N-1; i++)
    {
        int U, V;
        cin>>U>>V;
        E[U-1].push_back(V-1);
        E[V-1].push_back(U-1);
    }

    long long oo = 1000000000LL*N;
    vector<vector<long long>> X(N, vector<long long>(N, oo));   //  no server
    vector<vector<long long>> Y(N, vector<long long>(N, oo));   //  with server
    vector<int> C(N);
    vector<long long> Xt(N);
    vector<long long> Yt(N);

    function<void(int, int)> f = [&](int c, int p)
    {
        if (A[c] > 0)
            X[c][0] = A[c];
        else
            Y[c][0] = A[c];

        for (int e: E[c])
        if (e != p)
        {
            f(e, c);

            for (int i=0; i<=C[c]; i++)
            {
                Xt[i] = X[c][i];
                Yt[i] = Y[c][i];
                X[c][i] = oo;
                Y[c][i] = oo;
            }

            for (int a=0; a<=C[c]; a++)
            for (int b=0; b<=C[e]; b++)
            {
                //  cut
                if (X[e][b] < oo || Y[e][b] < 0)
                {
                    if (Xt[a] < oo)
                        X[c][a+b+1] = min(X[c][a+b+1], Xt[a]);
                    if (Yt[a] < oo)
                        Y[c][a+b+1] = min(Y[c][a+b+1], Yt[a]);
                }
                //  connect
                if (Xt[a] < oo && X[e][b] < oo)
                    X[c][a+b] = min(X[c][a+b], Xt[a] + X[e][b]);
                if (Yt[a] < oo || Y[e][b] < oo)
                    Y[c][a+b] = min(Y[c][a+b],
                        min(Yt[a] + X[e][b],
                        min(Yt[a] + Y[e][b],
                            Xt[a] + Y[e][b])));
            }
            C[c] += C[e] + 1;
        }
    };

    f(0, -1);

    for (int a=0; a<N; a++)
    {
        if (X[0][a] < oo || Y[0][a] < 0)
        {
            cout<<a<<endl;
            break;
        }
    }
}


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

long long n,s;

bool valid(long long num){
	if(num < 2)
		return false;
	long long sum = 0;
	long long tmp = n;
	while(tmp){
		sum += tmp%num;
		tmp /= num;
	}
	return (s == sum);
}

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

	if(n == s){
		cout << n + 1 << endl;
		return 0;
	}

	for(long long i = 2; i * i <= n; i++){
		long long tmp = n;
		long long sum = 0;
		while(tmp){
			sum += tmp%i;
			tmp/=i;
		}
		if(sum == s){
			cout << i << endl;
			return 0;
		}
	}
	

	//cout << "debug0" << endl;

	long long ans = LONG_MAX;

	for(long long x = 1; x * x <= n; x++){
		long long y = s - x;
		long long b = (n-y)/x;
		if(valid(b))
			ans = min(ans, b);
	}


	if(ans == LONG_MAX){
		cout << -1 << endl;
		return 0;
	}
	cout << ans << endl;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
int main(void){
    // Your code here!
    int H,N;
    cin >> H >> N;
    vector<int> atack(N);
    int sum=0;
    for(int i=0;i<N;i++){
        cin >> atack.at(i);
        sum+=atack.at(i);
    }
    if(sum>=H){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    
}



Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <bits/stdc++.h>
#include <limits>
using namespace std;

int main(void){
    // Your code here!
    int n, m;
    long long field[50][2] = {};
    long long stu[50][2] = {};
    
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> stu[i][0] >> stu[i][1];
        
    for(int i = 0; i < m; i++)
        cin >> field[i][0] >> field[i][1];
        
    for(int i = 0; i < n; i++){
        int argmin = -1;
        long long min_value = LLONG_MAX;
        for(int j = 0; j < m; j++){
            if(min_value > abs(field[j][0] - stu[i][0]) + abs(field[j][1] - stu[i][1])){
                min_value = abs(field[j][0] - stu[i][0]) + abs(field[j][1] - stu[i][1]);
                argmin = j;
            }
        }
        cout <<  argmin + 1 << endl;
    }
    
    return 0;
}

Measured memory_bytes: 256
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <regex>
#define rep(i,n) for(int i = 0; i < n; i++)
#define reps(i,j,n) for(int i = j; i < n; i++)
#define rrep(i,j,n) for(int i = j-1; i >= n; i--)
#define prec(n) fixed << setprecision(n)
#define print_array(v) rep(__k, v.size()) { cout << v[__k]; if(__k != v.size()-1) cout << " "; else cout << endl; }
#define YesorNo(a) printf(a?"Yes\n":"No\n")
#define acc(v) accumulate(v.begin(), v.end(),0);
#define MOD 1e+7
#define inf 1e9
#define fi first
#define se second
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> above(n+1, 0);
  vector<int> below(n+1, 0);

  reps(i,1,n+1) {
    int a;
    cin >> a;
    above[i] = above[i-1] + a;
  }

  reps(i,1,n+1) {
    int a;
    cin >> a;
    below[i] = below[i-1] + a;
  }

  int res = -inf;
  reps(i,1,n+1) {
    int candy = 0;
    candy += above[i];
    candy += below[n] - below[i-1];
    res = max(res, candy);
  }
  cout << res << endl;
  return 0;
}
//templete by private_yusuke

Measured memory_bytes: 256
def max_f(K, A):
    K += 1
    N = len(A)
    len_K = len('{:b}'.format(K))
    max_KA = max([K, max(A)])
    max_len = len('{:b}'.format(max_KA))
    bin_K = '{{:0{}b}}'.format(max_len).format(K)
    bin_A = ['{{:0{}b}}'.format(max_len).format(a) for a in A]
    bit_A_sum = [sum([int(bin_str[-(i+1)]) for bin_str in bin_A]) for i in range(max_len)]
    bit_K = [int(bin_K[-(i+1)]) for i in range(max_len)]
    f_val = [0 for _ in range(len_K)]
    for free_len in range(len_K):
        if bit_K[free_len] == 0:
            continue
        digit_x = [0 for _ in range(max_len)]
        for j, (ba, bk) in enumerate(zip(bit_A_sum, bit_K)):
            if j < free_len:
                digit_x[j] = max([ba, N - ba])
            elif j == free_len:
                # bit_K[free_len] == 1 -> bit_x[free_len] == 0
                digit_x[j] = ba
            else:
                digit_x[j] = ba if bk == 0 else (N - ba)
        f_val[free_len] = sum([(2**i) * d for i, d in enumerate(digit_x)])
    return int(max(f_val))


def main():
    N, K = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    print(max_f(K, A))


if __name__ == '__main__':
    main()

Measured memory_bytes: 18700
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

struct HopcroftKarp {
	int n;
	vector<vector<int>> g;
	vector<int> match, dist;
	vector<bool> used, alive;
	HopcroftKarp(int n) : n(n), g(n), match(n), dist(n), used(n), alive(n, true) {}
	void addEdge(int u, int v) {
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	int maximumMatching() {
		int cnt = 0;
		fill(match.begin(), match.end(), -1);
		while (true) {
			buildAlternatingLevelGraph();
			fill(used.begin(), used.end(), false);
			int flow = 0;
			for (int u = 0; u < n; u++)
				if (match[u] == -1 && augment(u))
					flow++;
			if (flow == 0)break;
			cnt += flow;
		}
		return cnt;
	}
	void buildAlternatingLevelGraph() {
		fill(dist.begin(), dist.end(), -1);
		queue<int> q;
		for (int u = 0; u < n; u++)
			if (match[u] == -1) {
				q.emplace(u);
				dist[u] = 0;
			}
		while (q.size()) {
			int u = q.front(); q.pop();
			for (int v : g[u]) {
				int w = match[v];
				if (w != -1 && dist[w] == -1) {
					dist[w] = dist[u] + 1;
					q.emplace(w);
				}
			}
		}
	};
	bool augment(int u) {
		used[u] = true;
		for (int v : g[u]) {
			if (!alive[v])continue;
			int w = match[v];
			if (w == -1 || (!used[w] && dist[w] == dist[u] + 1 && augment(w))) {
				match[u] = v;
				match[v] = u;
				used[v] = true;
				return true;
			}
		}
		return false;
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int X, Y, E; cin >> X >> Y >> E;
	HopcroftKarp bm(X + Y);
	rep(i, 0, E) {
		int x, y; cin >> x >> y;
		bm.addEdge(x, X + y);
	}
	cout << bm.maximumMatching() << endl;
	return 0;
}

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

int main(){
  int n;

  while(cin >> n){
    if(n == 0){
      break;
    }

    pair<int,int> team[n];

    for(int i = 0; i < n; i++){
      cin >> team[i].second;
      team[i].first = 0;

      for(int j = 0; j < 4; j++){
        int m, s;
        cin >> m >> s;
        team[i].first += s + m * 60;
      }
    }

    sort(team, team + n);

    cout << team[0].second << endl;
    cout << team[1].second << endl;
    cout << team[n - 2].second << endl;
  }
}

Measured memory_bytes: 1200
# -*- coding: utf-8 -*-
from collections import Counter

S = input()
s = list(S)

count = Counter(S)

if count[s[0]] ==  2 and count[s[1]] ==  2 and count[s[2]] ==  2 and count[s[3]] ==  2:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 3316
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

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

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL


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

    int N, M;
    cin >> N >> M;

    vs32 div;
    for (int i = 1; i * i <= M; ++i)
    {
        if (M % i == 0)
        {
            div.push_back(i);
            if (i != M / i)
            {
                div.push_back(M / i);
            }
        }
    }
    sort(div.begin(), div.end());

    auto it = upper_bound(div.begin(), div.end(), M / N);
    int ans = (it == div.begin() ? 1 : *(it - 1));
    cout << ans << "\n";
    return 0;
}



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

def conv(target_list):
    count = 0
    flag = True
    while flag:
        all_tmp = []
        for each_list in target_list:
            tmp = []
            m = min(each_list)
            count += min(each_list)
            #print(each_list)
            for e in each_list:
                if e-m != 0:
                    tmp.append(e-m)
                else:
                    if tmp:
                        all_tmp.append(tmp)
                        tmp = []
            if tmp:
                all_tmp.append(tmp)
        if len(all_tmp) == 0:
            flag = False
        target_list = all_tmp
    return count

print(conv(all_li))

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

int main()
{
    ll a, b, c, d;
    cin >> a >> b >> c >> d;

    ll ac = a * c;
    ll ad = a * d;
    ll bc = b * c;
    ll bd = b * d;

    cout << max({ac, ad, bc, bd}) << endl;
    return 0;
}


Measured memory_bytes: 3596
#include<iostream>
using namespace std;
int X, A, B;
int main(){
  cin >> X >> A >> B;
  if(-A + B <= 0) cout << "delicious" << endl;
  else if (-A + B <= X) cout << "safe" << endl;
  else cout << "dangerous"<< endl;
  return 0;
}

Measured memory_bytes: 3604
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception{
		new Main(new Scanner(System.in), System.out, System.err).exec();
	}
	public final Scanner sc;
	public final PrintStream out, err;
	public Main(final Scanner sc, final PrintStream out, final PrintStream err) {
		this.sc = sc;this.out = out;this.err = err;
	}
	static public final long pair(int a, int b) {return (((long)a) << 32) + b;}
	static public final int getPair1(long v) {return (int)(v >> 32);}
	static public final int getPari2(long v) {return (int)(v & 0xFFFFFFFF);}

	static public final long quad(long a, long b, long c, long d) {
		return ((a&0xFFFF)<< 48) + ((b&0xFFFF)<< 32) + ((c&0xFFFF)<< 16) +(d&0xFFFF);}
	static public final int getQuad1(long v) {return (int)( v >> 48          );}
	static public final int getQuad2(long v) {return (int)((v >> 32) & 0xFFFF);}
	static public final int getQuad3(long v) {return (int)((v >> 16) & 0xFFFF);}
	static public final int getQuad4(long v) {return (int)( v        & 0xFFFF);}

	static public final long record(int e, int m, int i) {
		return quad(-1, e, m, i);
	}
	static public final int re(long record) {
		return getQuad2(record);
	}
	static public final int rm(long record) {
		return getQuad3(record);
	}
	static public final int ri(long record) {
		return getQuad4(record);
	}

	//------------------------------------------

	private int p;
	private int[] inv;
	private int[] fact;
	private int[] finv;
	public final int mod(long v) {
		v %= p;
		return (int)(v < 0 ? v+p : v);
	}
	public final int mul(long a, long b) {
		return mod(a*b);
	}
	public final void initFact(double limit, double p) {
		initFact((int)limit, (int)p);
	}
	public final void initFact(int limit, int p) {
		this.p = p;
		this.inv = new int[limit+1];
		this.fact = new int[limit+1];
		this.finv = new int[limit+1];
		fact[1] = finv[1] = inv[0] = inv[1] = 1;
		for(int i=2;i<=limit;i++) {
			int a = p / i;
			int b = p % i;
			inv[i]  = mul(-a, inv[b]);
			fact[i] = mul(fact[i-1], i);
			finv[i] = mul(finv[i-1], inv[i]);
		}
	}
	public final int comb(int m, int n) {
		return mul(mul(fact[m], finv[n]), finv[m-n]);
	}

	//------------------------------------------

	public void exec() throws Exception{
		final int N = sc.nextInt();
		final int X = (int)(N/1.08);
		if((int)(X*1.08)==N) {
			out.println(X);
			return;
		}
		if((int)((X+1)*1.08)==N) {
			out.println(X+1);
			return;
		}
		out.println(":(");
	}
}


Measured memory_bytes: 23124
/*
 * 3つの整数を読み込み、それらを小さい順に並び替えて出力する
 * プログラム。
 *
 * Input
 * a b c
 *
 * Constraints
 * 1<=a,b,c<=10000
 */

#include <stdio.h>

int main()
{
	int a,b,c,tmp;
	scanf("%d %d %d", &a, &b, &c);

	if (a > b) {
		tmp = a;
		a = b;
		b = tmp;
	}
	if (b > c) {
		tmp = b;
		b = c;
		c = tmp;
	}
	if (a > b) {
		tmp = a;
		a = b;
		b = tmp;
	}
	
	printf("%d %d %d\n", a, b, c);
	return 0;
}


Measured memory_bytes: 2740
#include<bits/stdc++.h>
using namespace std;
int main() {
    string t; cin >> t;
    if(t == "Sunny") cout << "Cloudy" << endl;
    else if(t == "Cloudy") cout << "Rainy" << endl;
    else cout << "Sunny" << endl;
	return 0;
}


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

#define REP(i, n) for (ll (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n) for (ll (i) = m ; (i) < (ll)(n) ; ++(i))
#define REP_REV(i, n) for (ll (i) = (ll)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (ll (i) = (ll)(n) - 1 ; (i) >= m ; --(i))

#define INF             2e9
#define INF_LL          1LL<<60
#define ll              long long
#define Ceil(x, n)      (((((x))+((n)-1))/n))
#define CeilN(x, n)     (((((x))+((n)-1))/n)*n)
#define FloorN(x, n)    ((x)-(x)%(n))
#define ALL(x)          (x).begin(),(x).end()
typedef pair<ll, ll>    P;

int main() {
    string s;
    int w;
    cin >> s >> w;
    vector<char> ans;
    REP(i, s.size()) {
        if(i % w == 0) {
            ans.push_back(s[i]);
        }
    }

    REP(i, ans.size()) cout << ans[i];
    cout << endl;
    return 0;
}

Measured memory_bytes: 764
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using llpair=pair<ll,ll>;
using llvec=vector<ll>;
using llmat=vector<vector<ll>>;
#define llmattp(name,a,b,num) name(a,vector<ll>(b,num))
#define LLINF 1LL<<60
#define ABS(x) ( (x)>0 ? (x) : -(x) )

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;
}

ll gcd(ll a, ll b) {
	if(a==0||b==0) return 0;
	if(a<b) swap(a,b);
	ll tmp = a%b;
	while(tmp!=0) {
		a = b;
		b = tmp;
		tmp = a%b;
	}
	return b;
}

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

ll factorial(ll x) {
	ll f=1;
	for(ll i=2; i<x; i++) {
		f*=i;
	}
	return f;
}

ll nPr(ll n, ll r) {
	ll result=1;
	for(ll i=r+1; i<=n; i++) result*=i;
	return result;
}

ll nCr(ll n, ll r) {

	if (n == 0) { return 0; }
	if (r == 0) { return 1; }
	if (r == 1) { return n; }
	if (n == r) { return 1; }

	if (r > n / 2) { r = n / 2; }

	double result = 1;
	for (double i = 1; i <= r; i++) {
		result *= (n - i + 1) / i;
	}

	return (ll)result;
}

bool IsPrime(ll num) {
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

	double sqrtNum = sqrt(num);
	for (ll i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			// 素数ではない
			return false;
		}
	}

	// 素数である
	return true;
}

ll GreaterBinarySearch(ll *array, ll key, ll max, ll min) {
	if(array[max]<array[min]) {
		return NULL;
	} else {
		ll mid = max + (min-max)/2;
		if(array[mid]<key) {
			return GreaterBinarySearch(array,key,max,mid-1);
		} if(array[mid]>key) {
			return GreaterBinarySearch(array,key,mid+1,min);
		} else {
			return mid;
		}
	}
}

int main() {
	int n; cin>>n;
	int a[n],b[n],c[n-1];
	for(int i=0; i<n; i++) cin>>a[i];
	for(int i=0; i<n; i++) cin>>b[i];
	for(int i=0; i<n-1; i++) cin>>c[i];

	int score=b[a[0]-1];
	for(int i=1; i<n; i++) {
		score+=b[a[i]-1];
		if(a[i]==a[i-1]+1) score+=c[a[i-1]-1];
	}

	cout<<score<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <functional>
#include <iomanip>
using namespace std;
	
#define int long long
#define pb push_back
#define ub upper_bound
#define lb upper_bound
#define mp make_pair
#define F first
#define S second
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define REP(i,n) FOR(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define ve vector
#define vi vector<int>
#define vp vector<pair<int,int>>
#define vvi vector<vector<int>>

using ll = long long;
ll INF = LLONG_MAX / 4 - 100;
ll mod = 1e9 + 7;
int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
vector<ll> prime;
	
class fact {
public:
	int fmod = 1e9+7;
	vector<int> fac, inv;
	fact (int n, int Mod = 1e9+7) {
		fmod = Mod;
		fac = vector<int>(n,0);
		inv = vector<int>(n,0);
		fac[0] = 1; for (int i = 1; i < n; i++) fac[i] = fac[i-1] * i % fmod;
		for (int i = 0;i < n;i++) inv[i] = fact::POW(fac[i],fmod-2);
	}
	ll nCr(ll n, ll r) {return (fac[n] * inv[r] % fmod) * inv[n-r] % fmod;}
	ll POW(ll a, ll b) {ll c = 1; while (b > 0) {if (b & 1) {c = a * c%fmod;}a = a * a%fmod; b >>= 1;}return c;}
};
	
template <class T = ll> T in() {T x; cin >> x; return (x);}
void DEBUG(vector<int> a) {for(int i=0;i<a.size();i++)cout<<a[i]<<" ";cout<<endl;}
void EMP(int x) {cout<<"!!!"<<x<<"!!!"<<endl;}
ll GCD(ll a, ll b) {ll c; while (b != 0) {c = a % b; a = b; b = c;}return a;}
ll LCM(ll a, ll b) {return a * b / GCD(a, b);}
ll POW(ll a, ll b) {ll c = 1; while (b > 0) {if (b & 1) {c = a * c%mod;}a = a * a%mod; b >>= 1;}return c;}
void PRI(ll n) {bool a[n + 1LL]; for (int i = 0; i < n + 1LL; i++) {a[i] = 1LL;}for (int i = 2; i < n + 1LL; i++) {if (a[i]) {prime.pb(i); ll b = i; while (b <= n) {a[b] = 0; b += i;}}}}
template <typename T> T chmin(T& a, T b) {if(a>b)a=b;return a;}
template <typename T> T chmax(T& a, T b) {if(a<b)a=b;return b;}
bool isSqrt(ll a) {return pow(sqrt(a),2) == a ? 1 : 0;}
void YesNo(bool a) {if (a) cout << "Yes"; else cout << "No"; cout << endl;}
void yesno(bool a) {if (a) cout << "yes"; else cout << "no"; cout << endl;}
void YESNO(bool a) {if (a) cout << "YES"; else cout << "NO"; cout << endl;}
double dis(int x1, int x2, int y1, int y2) {
    return sqrt((double)abs(x1-x2)*(double)abs(x1-x2)+(double)abs(y1-y2)*(double)abs(y1-y2));
}

bool solve() {
    int n,m; cin >> n >> m;
    vector<vector<int>> job = vector<vector<int>>(m);
    REP (i,n) {
        int A,B; cin >> A >> B;
        if (A > m) continue;
        job[--A].pb(B);
    }
    priority_queue<int> pque;
    int ans = 0;
    REP (i,m) {
        for (auto&& nxt : job[i]) {
            pque.push(nxt);
        }
        if (pque.size()!=0) {
            ans += pque.top(); pque.pop();
        }
    }
    cout << ans << endl;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();
}


Measured memory_bytes: 5632
#define _USE_MATH_DEFINES // M_PI
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define rREP(i,a,n) for(int (i)=(n)-1; (i)>=(a);--(i))
#define all(x) (x).begin(),(x).end()
#define out(y,x,h,w) (y)<0||(x)<0||(y)>=(h)||(x)>=(w)
constexpr int INF = 2147483647;
constexpr ll mod = 1000000007;

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;
}
inline void init() {
	cin.tie(nullptr); cout.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}


int main() {
	init();

	int N, K; cin >> N >> K;
	vector<int>h(N);
	rep(i, N)cin >> h[i];
	vector<int>dp(N, INF);
	dp[0] = 0;
	rep(i, N) {
		REP(j, 1, K + 1) {
			if (i+j >= N)continue;
			chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
		}
	}
	cout << dp[N - 1] << "\n";


	return 0;
}

Measured memory_bytes: 1024
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
const ld eps = 1e-6;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;

int n;
ld pw;
ld vw[1 << 17], pf[1 << 17], vf[1 << 17], th[1 << 17];

ld calc(ld w) {
	ld ret = w * pw;
	rep(i, n) {
		ld cur = w * vw[i];
		ld dif = th[i] - cur;
		if (dif > 0) {
			ret += pf[i] * (dif / vf[i]);
		}
	}
	return ret;
}
void solve() {
	cin >> pw;
	rep(i, n) {
		cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
	}
	ld le = 0, ri = 100;
	rep(aa, 200) {
		ld l = (le * 2 + ri) / 3.0, r = (le + 2 * ri) / 3.0;
		ld cl = calc(l), cr = calc(r);
		if (cl > cr) {
			le = l;
		}
		else {
			ri = r;
		}
	}
	cout << calc(le) << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(6);
	while (cin >> n, n) {
		solve();
	}
	//stop
	return 0;
}


Measured memory_bytes: 9560
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric> // accumulate(v.begin(), v.end(), 0)                                                         

using namespace std;
#define ll long long

const int MAXN = 100;
char grid[MAXN+5][MAXN+5];

int main(){
  int A, B;
  cin >> A >> B;
  for(int i=0; i<MAXN/2; i++){
    for(int j=0; j<MAXN; j++){
      grid[i][j] = '.';
    }
  }
  for(int i=MAXN/2; i<MAXN; i++){
    for(int j=0; j<MAXN; j++){
      grid[i][j] = '#';
    }
  }
  A--, B--;
  for(int i=MAXN-1; A; i-=2){
    for(int j=0; j<MAXN && A; j+=2){
      grid[i][j] = '.';
      A--;
    }
  }
  for(int i=0; B; i+=2){
    for(int j=0; j<MAXN && B; j+=2){
      grid[i][j] = '#';
      B--;
    }
  }
  cout << MAXN << " " << MAXN << endl;
  for(int i=0; i<MAXN; i++){
    cout << grid[i] << endl;
  }
  cout << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define INF (ll)(1e18)
#define MOD (ll)(1e9 + 7)
#define MAX (ll)(1e5 + 9)

using namespace std;
using ll = long long;

int main () {
	ll n;
	cin >> n;
	ll ar[n], ss = 1;
	for (int i = 0; i < n; i++) {
		cin >> ar[i];
	}
	sort (ar, ar + n);
	if (ar[0] == 0) {
		cout << "0\n";
		return 0;
	}
	for (int i = 0; i < n; i++) {
		if (INF / ss < ar[i]) {
			cout << "-1\n";
			return 0;
		}
		ss *= ar[i];
	}
	cout << ss << "\n";
}


Measured memory_bytes: 4380
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n % 2 == 0) {
    cout << n / 2;
  }
  if (n % 2 == 1) {
    cout << n / 2 + 1;
  }
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
	#include <stdio.h>
	#include <vector>
	#include <string>
	#include <set>
	#include <queue>
	#include <cmath>
	#include <map>
	#include <algorithm>
	#include <stack>
	#include <cstring>
	 
	#define ll long long
	#define MP make_pair
	#define PB push_back
	#define P push
	#define f first
	#define se second
	#define S size()
	#define B begin()
	#define E end()
	#define rB rbegin()
	#define rE rend()
	#define PI acos(-1)
	#define speed ios::sync_with_stdio(0); cin.tie(0);
	 
	 
	const int N = 1e5 + 100;
	const int M = 1e6 + 100;
	const ll oo = 2e18;
	 
	ll n, m, k, t, q, h, a, b, d, c, e, w, x, y, z;
	using namespace std;

	int ar[3003];
	ll dp[3003][3003][2];

	ll calc(int l, int r, bool f) {
		if(l == r) {
			return ar[l] * pow(-1, f);
		}

		ll &ret = dp[l][r][f];

		if(ret != -1) return ret;

		if(f) 
			ret = min(-ar[r] + calc(l, r - 1, !f), -ar[l] + calc(l + 1, r, !f));
		
		else 
			ret = max(ar[r] + calc(l, r - 1, !f), ar[l] + calc(l + 1, r, !f));


		return ret;
	}
	 
	int main() {
		

		cin >> n;

		memset(dp, -1, sizeof dp);

		for(int i = 0; i < n; i++) {
			cin >> ar[i];
		}

		cout << calc(0, n - 1, 0);


	    return 0;
	}



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

int main() {
	string s;
    cin >> s;
    int c=0;
    int f=0;
    int a=0;
    for(int i=0;i<s.size();i++){
      if(s.at(i)=='C'){
        c++;
        a = i;
        break;
      }
    }
    for(int i=a+1;i<s.size();i++){
      if(s.at(i)=='F'){
        f++;
        break;
      }
    }
    if(c>0 && f>0){
      cout << "Yes" << endl;
    }
    else{
      cout << "No" << endl;
    }
}



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

li_shop = list()
for i in range(n):
    li_shop.append(list(map(int, input().split())))

zan = m
money = 0
for shop in sorted(li_shop, key=lambda x: x[0]):
    if zan > shop[1]:
        # すべて買う
        buy = shop[1]
        money += shop[0] * buy
        zan = zan - buy
    else:
        buy = zan
        money += shop[0] * buy
        zan = 0
print(money)

Measured memory_bytes: 29788
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (31 - __builtin_clz(x))
#define SZ(x) ((int)(x).size())
#define BACK(x) prev((x).end())
#define HASK(x, y) ((x).find(y) != (x).end())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;
 
const int maxN = 1 << 19;

int apd[maxN];

void solve()
{
	FOR(i, 2, maxN)
		apd[i] = i;
	for (int i=2; i*i<maxN; i++)
		if (apd[i] == i)
			for (int j=i*i; j<maxN; j+=i)
				apd[j] = i;
				
	int n;
	scanf ("%d", &n);
	while (apd[n] != n)
		n++;
	printf("%d\n", n);
}			
 
int main()
{
	int t = 1;
	//scanf ("%d", &t);
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}
 



Measured memory_bytes: 5864
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;

int main(){
    ll N;
    cin >> N;
    vector<ll> A(N),B(N+1);
    for(int i=0;i<N;i++){
        cin >> A[i];
        B[A[i]]++;
    }
    vector<ll> cnt(N+1,0),D(N+1,0);
    for(int i=1;i<=N;i++) cnt[B[i]]++;
    vector<ll> sum(N+1,0),sum2(N+1,0);
    for(ll i=1;i<=N;i++){
        sum[i] = sum[i-1]+i*cnt[i];
        sum2[i] = sum2[i-1]+cnt[i];
    }
    for(int i=1;i<=N;i++) cerr << cnt[i] << (i!=N? " ":"\n");
    vector<ll> f(N+1),ans(N+1,0);
    for(ll i=1;i<=N;i++){
        f[i] = (sum[i]+i*(sum2[N]-sum2[i]))/i;
    }
    int now = 1;
    for(int i=N;i>=1;i--){
        while(now<=N && now<=f[i]){
            ans[now] = i;
            now++;
        }
    }
    for(int i=1;i<=N;i++) cout << ans[i] << endl;
}

Measured memory_bytes: 19584