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

typedef long long ll;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const ll MOD = 1e9 + 7;

bool compPair(const pair<int, int>& arg1, const pair<int, int>& arg2) {
    return arg1.first > arg2.first;
}

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

    string upper;
    string lower;
    cin >> upper;
    cin >> lower;
    
    int i=0;
    ll ans = 1;
    bool tate;
    if(upper.at(i)==lower.at(i)) {
        ans *= 3;
        i++;
        tate = true;
    }
    else {
        ans *= 6;
        i+=2;
        tate = false;
    }
    while(i<n) {
        if(upper.at(i)==lower.at(i)) {
            if(tate) {
                ans *= 2;
                ans %= MOD;
                i++;
                tate = true;
            }
            else {
                i++;
                tate = true;
            }
        }
        else {
            if(tate) {
                ans *= 2;
                ans %= MOD;
                i+=2;
                tate = false;
            }
            else {
                ans *= 3;
                ans %= MOD;
                i+=2;
                tate = false;
            }
        }
    }

    cout << ans << endl;

    return 0;
    
}


Measured memory_bytes: 256
s = gets.chomp.to_s.reverse
t = ['dream','dreamer','erase','eraser']
t_r = t.map{|t| t.reverse}

400000.times do
  t_r.each do |t|
    if s[0..t.length-1] == t
      s = s[t.length..-1]
      break if s.empty?
    end
  end
end

puts s.empty? ? "YES" : "NO"


Measured memory_bytes: 3836
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define re register
#define ll long long
inline int gi()
{
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=100010;
typedef pair<ll,int> pli;typedef pair<int,int> pii;
#define mp make_pair
pli p[N];int n,siz[N];
vector<pii>ans;
int main()
{
	n=gi();
	for(int i=1;i<=n;i++)scanf("%lld",&p[i].first),p[i].second=i,siz[i]=1;
	sort(p+1,p+n+1);ll dis=0;
	for(int i=n;i>1;i--)
	{
		ll nd=p[i].first-(n-siz[p[i].second])+siz[p[i].second];
		int pos=lower_bound(p+1,p+n+1,mp(nd,0))-p;
		if(p[pos].first!=nd)return puts("-1"),0;
		ans.push_back(mp(p[pos].second,p[i].second));
		siz[p[pos].second]+=siz[p[i].second];dis+=siz[p[i].second];
	}
	if(dis!=p[1].first)return puts("-1"),0;
	for(pii now:ans)printf("%d %d\n",now.first,now.second);
	return 0;
}


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

int main() {

    int N;
    cin >> N;

    string ans = "No";

    for (int i = 0; i <= 25; ++i) {
        for (int j = 0; j <= 15; ++j) {
            if (4*i+7*j == N) {
                ans = "Yes";
                break;
            }
        }
    }

    cout << ans << endl;


    return 0;
}

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

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

  int n;
  cin >> n;

  cout << pow(n, 3) << endl;
}


Measured memory_bytes: 256
r = 1121
p = [1]*r
p[0] = p[1] = 0
for i in range(int(r**0.5)):
	if p[i]:
		p[2*i::i] = [0 for x in range(2*i,r,i)]
prime = []
for i in range(r):
	if p[i]: prime.append(i)

dp = [[0]*r for i in range(15)]
dp[0][0] = 1
for i in range(len(prime)):
	for k in range(min(i+1,14),0,-1):
		for j in range(prime[i],r):
			dp[k][j] += dp[k-1][j-prime[i]]

while 1:
	n,k = map(int,raw_input().split())
	if n == 0: break
	print dp[k][n]

Measured memory_bytes: 4696
n = int(input())
if n <= 104:
    ans = 0
elif n >= 105 and n < 135:
    ans = 1
elif n >= 135 and n < 165:
    ans = 2
elif n >= 165 and n < 189:
    ans = 3
elif n >= 189 and n < 195:
    ans = 4
elif n >= 195:
    ans = 5
print(ans)


Measured memory_bytes: 2940
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;

int main(){
  int a,b;
  cin >> a >> b;
  if(a<=b) cout << a << endl;
  else cout << a-1 << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

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

    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        int d = 0;
        for (int j = i; j > 0; j /= 10) {
            d++;
        }

        if (d % 2 == 1) {
            cnt++;
        }
    }

    cout << cnt << endl;
}


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

using namespace std;

int main() {
    int K, S, cnt = 0;
    cin >> K >> S;

    for (int X = 0; X <= K; ++X) {
        for (int Y = 0; Y <= K; ++Y) {
            int Z = S - X - Y;
            if (Z >= 0 && Z <= K) {
                cnt++;
            }
        }
    }

    cout << cnt << endl;
}

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

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

  int d[300000];
  for(int i=0;i<N;i++){
    cin >> d[i];
  }

  int D=0;

  for(int i=0;i<N-1;i++){
    D=max(D,d[i]);

    if(D<10){
      cout << "no" << endl;
      return 0;
    }

    D-=10;
  }

  D=0;

  for(int i=N-1;i>0;i--){
    D=max(D,d[i]);

    if(D<10){
      cout << "no" << endl;
      return 0;
    }

    D-=10;
  }

  cout << "yes" << endl;

  return 0;
}



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

int main() {
	int X, Y, Z;
	cin >> X >> Y >> Z;
	cout << (X - Z) / (Y + Z);
	return 0;
}
/*
31536000のコメント解説欄
ここテンプレで用意してるから、A問題とかだとこの先空欄の危険あり

まず、1人座る為には幅Y+両端の2Zが必要
2人座る為にはZYZYZより2Y+3Z
3人座る為にはZYZYZYZより3Y+4Z
以下同様に考えると、n人座る為にはnY+(n+1)Z必要なわけだ
逆に、X>=nY+(n+1)Zより、X-Z>=n(Y+Z)であるから(X-Z)/(Y+Z)>=nとなる
よって(X-Z)/(Y+Z)を端数切り捨てることで答えが求まる
整数型同士での割り算は端数が自動で切り捨てられるので、上の答えでおｋ
*/

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
 
struct node_t {
    int key;
    struct node_t *parent;
    struct node_t *left;
    struct node_t *right;
};
typedef struct node_t Node;
 
Node *tree_insert(Node *top, Node *z) {
    Node *y, *x;
 
    y = NULL;
    x = top;
    while (x != NULL) {
        y = x;
        if (z->key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }
    z->parent = y;
    if (y == NULL) {
        top = z;
    } else if (z->key < y->key) {
        y->left = z;
    } else {
        y->right = z;
    }
    return top;
}
 
Node *tree_find(Node *node, int key) {
    if (node == NULL)
        return NULL;
    if (node->key == key)
        return node;
    if (key < node->key)
        return tree_find(node->left, key);
    return tree_find(node->right, key);
}
 
Node *tree_minimum(Node *x) {
    while (x->left != NULL) {
        x = x->left;
    }
    return x;
}
 
Node *tree_successor(Node *x) {
    Node *y;
 
    if (x->right != NULL) {
        return tree_minimum(x->right);
    }
    y = x->parent;
    while (y != NULL && x == y->right) {
        x = y;
        y = y->parent;
    }
    return y;
}
 
Node *tree_delete(Node *top, Node *z) {
    Node *y, *x;
 
    if (z->left == NULL || z->right == NULL) {
        y = z;
    } else {
        y = tree_successor(z);
    }
 
    if (z->left != NULL) {
        x = y->left;
    } else {
        x = y->right;
    }
 
    if (x != NULL) {
        x->parent = y->parent;
    }
    if (y->parent == NULL) {
        top = x;
    } else if (y == y->parent->left) {
        y->parent->left = x;
    } else {
        y->parent->right = x;
    }
 
    if (y != z) {
        z->key = y->key;
    }
    return top;
}
 
void inorder_walk(Node *node) {
    if (node->left != NULL) {
        inorder_walk(node->left);
    }
    printf(" %d", node->key);
    if (node->right != NULL) {
        inorder_walk(node->right);
    }
}
 
void preorder_walk(Node *node) {
    printf(" %d", node->key);
    if (node->left != NULL) {
        preorder_walk(node->left);
    }
    if (node->right != NULL) {
        preorder_walk(node->right);
    }
}
 
int main(void) {
    int n, i, key;
    char cmd[20];
    Node *top = NULL, *p;
 
    scanf("%d ", &n);
    for (i = 0; i < n; i++) {
        scanf("%s %d ", cmd, &key);
        if (cmd[0] == 'i') {
            p = malloc(sizeof(Node));
            p->key = key;
            p->parent = p->left = p->right = NULL;
            top = tree_insert(top, p);
        } else if (cmd[0] == 'f') {
            puts(tree_find(top, key) != NULL ? "yes" : "no");
        } else if (cmd[0] == 'd') {
            p = tree_find(top, key);
            if (p != NULL) {
                top = tree_delete(top, p);
            }
            //free(p);
        } else {
            inorder_walk(top);
            putchar('\n');
            preorder_walk(top);
            putchar('\n');
        }
    }
    return 0;
}


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

#define PI 3.1415926535897932
#define MOD 1000000007
#define INF 1e9

#define REP(i, n) for(int i = 0; i < (n); i++)
#define REP1(i, n) for(int i = 1; i <= (n); i++)
#define FOR(i, start, end) for(int i = (start); i < (end); i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;


void solve()
{
    string S, T; cin >> S >> T;
    for(int i = 0; i < S.length(); i++)
    {
        for(int j = 0; j < T.length(); j++)
        {
            if(S[(i+j) % S.length()] != T[j]) break;
            if(j == T.length()-1)
            {
                cout << "Yes" << endl;
                return;
            }
        }
    }
    cout << "No" << endl;
}

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    solve();

    return 0;
}

Measured memory_bytes: 256
s = [list(input()) for _ in range(3)]
char = list("ABC")
now = 0
while True:
    if s[now] == []:
        print(char[now])
        break
    else:
        a = s[now].pop(0)
    if a == "a":
        now = 0
    elif a == "b":
        now = 1
    else:
        now = 2

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const int INF=1e9;

int main(){
    int n,m;
    cin>>n>>m;
    int a[n+1];
    fill(a,a+n+1,0);
    for(int i=0;i<m;i++){
        int l,r;
        cin>>l>>r;
        a[l-1]++;
        a[r]--;
    }
    int ans=0;
    for(int i=0;i<n-1;i++){
        a[i+1]+=a[i];
    }
    for(int i=0;i<n;i++){
        if(a[i]==m)ans++;
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 640
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std ;
using ll = long long ;
using vll = vector<ll> ;
using vvll = vector<vll> ;

bool DFS(vvll &g,vll &a,int cur,int pre){
	bool ok = false ;
	for(int i=0;i<g[cur].size();i++){
		if(g[cur][i]==pre) continue ;
		if(a[cur]>a[g[cur][i]]){
			ok |= !DFS(g,a,g[cur][i],cur) ;
		}
	}
	return ok ;
}

int main(){
	ll n ; cin >> n ;
	vll a(n) ;
	for(auto &i:a) cin >> i ;
	vvll g(n) ;
	for(int i=0;i<n-1;i++){
		ll u,v ;
		cin >> u >> v ;
		u-- ; v-- ;
		g[u].push_back(v) ;
		g[v].push_back(u) ;
	}
	vll ans ;
	for(int i=0;i<n;i++) if(DFS(g,a,i,-1)) ans.push_back(i+1) ;
	for(int i=0;i<ans.size();i++) cout << ans[i] << (i+1==ans.size()?"\n":" ") ;
}

Measured memory_bytes: 3872
//##MohK
//02/03/2020....5,43.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main(){
	ll x,y,counter=0;
	cin>>x>>y;
	while(x>0){
	x/=y;
	counter++;
	}
	cout<<counter<<endl;

	return 0;
}



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

int main()
{
    int n,result[200],min,i,j;
    long long int a[200];
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        result[i] = 0;
        j = 0;
        while(a[i]%2==0)
        {
            result[i]++;
            j++;
            a[i] = a[i] / 2;
        }
    }
    min = result[0];
    for(i=1; i<n; i++)
    {
        if(min>=result[i])
            min = result[i];
    }
    cout<<min<<endl;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<queue>	

using namespace std;

namespace MyLib {
#define ALL(a) a.begin(),a.end()
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define debug(x) if(1)cout<<#x<<":"<<x<<endl;
#define DEBUG(x) if(1)cout<<#x<<":"<<x<<endl;
#define ll long long
}
using namespace MyLib;

int main() {
	int a, b, c;
	int t=0;
	cin >> a>>b>>c;
	REP(i, 200) {
		if (t % 60 <= c&& c <= t % 60 + a) {
			cout << t + c - t % 60 << endl;
			return 0;
		}
		else if(60 <= t % 60 + a && c <= t % 60 + a) {
			cout << t + 60 - t % 60 + c << endl;
			return 0;
		}
		t += a + b;
	}
	cout << -1 << endl;

	return 0;
}

Measured memory_bytes: 1164
#pragma warning(disable:4996)
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (ll i = (ll)(a); i<(ll)(b); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a), end(a)
#define TPL template
#define TNM typename

using ll = long long;
using ull = unsigned long long;
TPL<TNM T> using vec = vector<T>;
TPL<TNM T> using vec2 = vec<vec<T>>;
TPL<TNM T> using vec3 = vec<vec2<T>>;
TPL<TNM T> using vec4 = vec<vec3<T>>;
TPL<TNM T> using vec5 = vec<vec4<T>>;

TPL<TNM K, TNM V> using umap = unordered_map<K, V>;
TPL<TNM K, TNM V> using uset = unordered_set<K, V>;

TPL<class T> inline void OUT(const T &x) { cout << x << "\n"; }
void YESNO(bool c) { OUT(c ? "YES" : "NO"); };
void YesNo(bool c) { OUT(c ? "Yes" : "No"); };
void bOUT(bool c, string s, string t) { OUT(c ? s : t); }
struct pre_ { pre_() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(6); } } pre__;

TPL<TNM V, TNM H> void resize(vector<V>& v, const H h) { v.resize(h); }
TPL<TNM V, TNM H, TNM ... T> void resize(vector<V>& v, const H& h, const T ... t) { v.resize(h); for (auto& _v : v) resize(_v, t ...); }
TPL<TNM V, TNM T> void fill(V& x, const T& val) { x = val; }
TPL<TNM V, TNM T> void fill(vector<V>& vec, const T& val) { for (auto& v : vec) fill(v, val); }

int main(void) {
	int N;
	cin >> N;
	vec<int> A(N);
	REP(i, N)cin >> A[i];

	int c = A[0];
	int cnt = 1;
	int ans = 0;
	for (int i = 1; i < N; ++i) {
		if (c != A[i]) {
			c = A[i];
			ans += cnt / 2;
			cnt = 0;
		}
		cnt++;
	}
	ans += cnt / 2;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;


signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  string s,t;
  cin>>s>>t;
  int ans=0;
  rep(i,s.size() ){
    if(s[i]!=t[i]) ans++;
  }
  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 3836
#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int MAXN = 200005;
const int MOD = 1000000007;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	ll n,a,b;
	cin >> n >> a >> b;
	if(b < a){
		cout << "0\n"; return 0;
	}
	if(n == 1){
		if(a == b) cout << "1\n";
		else cout << "0\n";
		return 0;
	}
	ll ans = (b-a) * (n-2) + 1;
	cout << ans << '\n';
	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(){

  int X;
  int A;
  int B;
  
  scanf("%d",&X);
  scanf("%d",&A);
  scanf("%d",&B);
  
  if(A >= B) {
    printf("delicious\n");
  }else if(X >= B - A) {
    printf("safe\n");
  }else {
    printf("dangerous\n");
  }
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
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;}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    int n, r;
    cin >> n >> r;
    if (n >= 10) {
        cout << r << endl;
    } else {
        cout << r + (100 * ( 10 - n)) << endl;
    }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+5;
const int mod=998244353;
map<int,int>mp;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int x;
        cin>>x;
        mp[x]++;
    }
    for(int i=1;i<=n;i++)
    {
        cout<<mp[i]<<'\n';
    }

}


Measured memory_bytes: 13000
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))

    n_2 = 0
    n_1 = 0
    for a, b in zip(A, B):
        if a > b:
            n_1 += (a - b)
        elif a < b:
            n_2 += (b - a + 1) // 2

    diff = sum(B) - sum(A)
    if n_1 <= diff and n_2 <= diff:
        ans = "Yes"
    else:
        ans = "No"
    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 10516
package main

import (
	"fmt"
)

func main() {
	var N int
	fmt.Scan(&N)

	A := make([]int, N)
	B := make([]int, N+1)
	for i := 0; i < N; i++ {
		fmt.Scan(&A[i])
		B[i+1] = A[i] + B[i]

	}
	const p = 1000_000_000 + 7

	sum := 0
	// tmp := 0
	ans := 0

	for i := 0; i < N-1; i++ {
		sum = (B[N] - B[i+1]) % p
		ans += sum * A[i]
		ans %= p

	}
	fmt.Println(ans)
	// for i := N - 2; i >= 0; i-- {
	// 	tmp += A[i+1]
	// 	tmp %= p
	// 	sum += A[i] * tmp
	// 	sum %= p
	// }
	//fmt.Println(sum)

}


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

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

  int p[3] = {a + b, a + c, b + c};
  int min_p = p[0];
  for (int i = 1; i < 3; i++) {
    min_p = min(min_p, p[i]);
  }
  cout << min_p << endl;
  return 0;
}


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

using namespace std;

int main(void)
{
    char s[110];
    char t[110];

    cin >> s;
    cin >> t;

    char temp;
    for (int i = 0; i < strlen(s); i++) {
        temp = s[strlen(s)-1];
        for (int j = strlen(s)-1; j > 0; j--) {
            s[j] = s[j-1];
        }
        s[0] = temp;
        if (!strncmp(s, t, strlen(s))) {
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9+5;
const int maxn=100005;
int k,a[maxn],dp[maxn];
int mincost(int n)
{
    if(n<=1)
        return 0;
    else if(n==2)
        return abs(a[2]-a[1]);
    else if(dp[n]!=-1)
        return dp[n];
    else
    {
        dp[n]=inf;
        for(int i=1;i<=k;i++)
        {
            if(n-i>0)

            dp[n]=min(dp[n],mincost(n-i)+abs(a[n]-a[n-i]));
        }
    }
    return dp[n];

}
int main()
{
    int n;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    memset(dp,-1,sizeof(dp));
        cout<<mincost(n)<<endl;
    return 0;
}



Measured memory_bytes: 4096
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <numeric>

// a 1000 → 1000
// ab 10 10 → 10 * 10 + 10 + 10


//abcd
//abc
//bcd
//acd
//abd
//ab/ac/ad
//bc/bd
//cd
//a/b/c/d
// 1 + 4 + 6  + 4 = 

using namespace std;

using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)

const ll mod = 1000000007;

using namespace std;

int main() {
	int n;
	string s;

	cin >> n >> s;

	int a[28] = {};

	rep(i, s.size()) {
		a[s[i] - 'a'] ++;
	}
	ll ans = 1;

	rep (i, 26) {
		//cout << (char) (i + 'a') << ":" << a[i] << endl;
		if (a[i] > 0) {
			ans = ((a[i] + 1) * ans) % mod; 
		}
	
	}
	cout << ((ans - 1) + mod) % mod << endl;
	

 	return 0;
}


Measured memory_bytes: 512
#include <iostream>
#include <algorithm>

using namespace std;

void buildMaxHeap();
void maxHeapify(int i);
int A[2000001];
int H;

int main() { 

    cin >> H;
    for (int i = 1; i <= H; i++) {
        cin >> A[i];
    }

    buildMaxHeap();
    
    for (int i = 1; i <= H; i++) {
        cout << ' ' << A[i];
    }
    cout << '\n';
}


void buildMaxHeap() {
    for (int i = H/2; i >= 1; i--) {
        maxHeapify(i);
    }
}

void maxHeapify(int i) {

    int l = i * 2;
    int r = i * 2 + 1;
    int largest;

    if (l <= H and A[l] > A[i])
        largest = l;
    else 
        largest = i;

    if (r <= H and A[r] > A[largest]) 
        largest = r;

    if (largest != i) {
        swap(A[i], A[largest]);
        maxHeapify(largest);
    }
}



Measured memory_bytes: 4956
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  cout << (n * (n + 1)) / 2 << endl;
}


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

const int SIZE=110;
const int INF=1e18;
struct edge{
    int u,v,cost;
};

signed main()
{
    int N,M; cin>>N>>M;
    vector<vector<int>> dist(N,vector<int>(N,INF));
    for(int i=0;i<N;i++){
        dist[i][i]=0;
    }
    vector<edge> es;
    for(int i=0;i<M;i++){
        int a,b,c; cin>>a>>b>>c;
        a--; b--;
        es.push_back({a,b,c});
        dist[a][b]=c;
        dist[b][a]=c;
    }

    for(int k=0;k<N;k++){
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
            }
        }
    }

    int ans=0;
    for(edge e:es){
        bool used=false;
        for(int i=0;i<N;i++) for(int j=i+1;j<N;j++){
            if(dist[i][j]==dist[i][e.u]+e.cost+dist[e.v][j]) used=true;
            if(dist[i][j]==dist[i][e.v]+e.cost+dist[e.u][j]) used=true;
        }
        if(!used) ans++;
    }
    cout<<ans<<endl;

    return 0;
}


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

const int MOD = 1e9 + 7;
int N, ans = 0;
const char ACGT[4] = {'A', 'C', 'G', 'T'};

bool ok_last4(string str) {
    bool ng = (str[0] == 'A' && str[3] == 'C') && 
        (str[1] == 'G' || str[2] == 'G');
    return !ng;
}

bool ok_last3(string str) {
    bool ng = str == "AGC" || str == "ACG" || str == "GAC"; 
    return !ng;
}

void dfs(int len, map<string, int> last3_pat) {
    if (len == N) {
        for (auto it : last3_pat) {
            ans += it.second;
            ans %= MOD;
        }
    } else {
        map<string, int> n_last3_pat;
        for (auto it : last3_pat) {
            for (char c : ACGT) {
                string last4 = it.first + string(1, c);
                string n_last3 = last4.substr(1, 3);
                if (ok_last3(n_last3) && ok_last4(last4)) {
                    n_last3_pat[n_last3] += it.second;
                    n_last3_pat[n_last3] %= MOD;
                }
            }
        }
        dfs(++len, n_last3_pat);
    }
}

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

    cin >> N;
    map<string, int> init = {{"TTT", 1}};
    dfs(0, init);
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1280
#include <iostream>
using namespace std;
int main(){
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  int dy = ty - sy;
  int dx = tx - sx;
  string S[4*(dx+dy+2)];
  for(int i = 0; i < dy; ++i){
    S[i] = 'U';
    S[dx+dy+i] = 'D';
    S[2*dx+2*dy+1+i] = 'U';
    S[3*dx+3*dy+5+i] = 'D';
  }
  for(int j = 0; j < dx; ++j){
    S[dy+j] = 'R';
    S[dx+2*dy+j] ='L';
    S[2*dx+3*dy+3+j] = 'R';
    S[3*dx+4*dy+7+j] = 'L'; 
  }
  S[2*dx+2*dy] = 'L';
  S[2*dx+3*dy+1] = 'U';
  S[2*dx+3*dy+2] = 'R';
  S[3*dx+3*dy+3] = 'D';
  S[3*dx+3*dy+4] = 'R';
  S[3*dx+4*dy+5] = 'D';
  S[3*dx+4*dy+6] = 'L';
  S[4*dx+4*dy+7] = 'U';
  for(int i = 0; i < 4*(dx+dy+2); ++i){
    cout << S[i];
  }
  cout << endl;
}

Measured memory_bytes: 1152
#include <stdio.h>
#include <limits.h>
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a<b)?a:b)

int main()
{
    int R[200000], n;

    while(scanf("%d", &n) != EOF)
    {
        for(int i=0; i<n; i++)
            scanf("%d", &R[i]);

        int Max = INT_MIN;
        int Min = R[0];
        for(int i=1; i<n; i++)
        {
            Max = max(Max, R[i] - Min);
            Min = min(Min, R[i]);
        }
        printf("%d\n", Max);
    }
}



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

#define rep_(v, f, l) for (int v = (f); v < (l); ++v)
#define rep(v, n) rep_(v, 0, n)
#define lep(v, n) rep_(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }

int main()
{
    int w, h;

    while (w = in(), h = in(), h){
        int field[16][16] = {};
        int mask[16][16] = {};
        int n = in();
        rep(i, n){
            int x, y;
            x = in() - 1;
            y = in() - 1;
            mask[y][x] = 1;
        }
        field[0][0] = 1;
        rep(i, h){
            rep(j, w){
                if (mask[i][j]) continue;
                if (i > 0) field[i][j] += field[i - 1][j];
                if (j > 0) field[i][j] += field[i][j - 1];
            }
        }
        printf("%d\n", field[h - 1][w - 1]);
    }
    return (0);
}

Measured memory_bytes: 1188
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main(){
  int N,T,ans=0;
  cin>>N>>T;
  int t[N];
  for(int i=0;i<N;i++) cin>>t[i];
  for(int i=0;i<N-1;i++){
    if(t[i+1]-t[i]<T) ans+=t[i+1]-t[i];
    else ans+=T;
  }
  cout<<ans+T<<endl;
}


Measured memory_bytes: 1024
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

template<typename T>
void print_old(vector<T>vec){
    for(int i=0; i< vec.size(); i++)
      cout<< vec[i]<<" ";
}
//  prints any type which supports iterators...
template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << "\n";
   }
   cout<< "\n";
}
// void f(const vector< int >& v) {
// int r = 0;
// tr(v, it) {
// r += (it)(*it);
// }
// return r;
// }
typedef vector< int > vi;
typedef pair< int,int > pii;
typedef vector<string> vs;
typedef vector<vi> vv;
#define sz(a) int((a).size())
#define pb push_back
#define forn(n) for(int i =0 ; i< n; i++)
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ll long long

const int inf = 1e9 +5;

int main(){
    fastio;
    // int t;
    // cin>> t;
   int t = 1;
    while(t--){
       int n;
       scanf("%d", &n);
       vector<int> h(n);
       for(int &x : h){
           scanf("%d", &x);
       }
      vector<int> dp(n, inf);
       dp[0] = 0;
       for(int i= 0; i < n; ++i){
           for(int j : {i+1, i+2}){
               if(j < n){
                   dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
               }
           }
       }
        // cout << dp[n-1]<<"\n";
        printf("%d\n", dp[n-1]);
    }
    return 0;
}

Measured memory_bytes: 1024
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
	}
}

class Solver {
	int N;
	int cnt;

	public void init() {
		N = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split(" ");
		// N = Integer.parseInt(strArr[0]);
		// System.out.println(N);
		int a = Integer.parseInt(strArr[0]);
		int b = Integer.parseInt(strArr[1]);
		if (a >= 13)
			System.out.println(b);
		else if (a >= 6)
			System.out.println(b / 2);
		else
			System.out.println(0);
	}

	public boolean hasNext() {
		return cnt < N;
	}

	public void readBody(String str) {
		// System.out.println(str);
		cnt++;
	}

	public void solve() {

	}
}


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

using namespace std;
typedef pair<int,int> P;
int n,m;
int d[1010][1010][5];
char s[1010][1010];
char joi[3][3];
int ans = 0;
int ha[4][4];
int main(){
	scanf("%d%d",&m,&n);
	for(int i = 0;i < m;i++){
	//	scanf("%s",&s[i]);
		cin >> s[i];
	}
//	scanf("%s\n%s",&joi[0],&joi[1]);
	cin >> joi[0] >> joi[1];
	for(int i = 0;i < 2;i++){
		for(int j = 0;j < 2;j++){
			if(joi[i][j] == 'O'){
				ha[i][j] = 1;
			}else if(joi[i][j] == 'I'){
				ha[i][j] = 2;
			}
		}
	}
	for(int i = 0;i < m-1;i++){
		for(int j = 0;j < n-1;j++){
			int co = 0;
			P t;
			for(int k = 0;k < 2;k++){
				for(int l = 0;l < 2;l++){
					if(s[i+k][j+l] == joi[k][l]){
						co++;
					}else{
						t = P(k,l);
					}
				}
			}
			if(co == 4){
				ans++;
				for(int k = 0;k < 2;k++){
					for(int l = 0;l < 2;l++){
						for(int k = 0;k < 3;k++){
							if(k != ha[k][l]){
								d[i+k][j+l][ha[k][l]]--;
							}
						}
					}
				}
			}else if(co == 3){
				d[t.first+i][t.second+j][ha[t.first][t.second]]++;
			}
		}
	}
	int plus = 0;
	for(int i = 0;i < m;i++){
		for(int j = 0;j < n;j++){
			for(int k = 0;k < 3;k++){
				plus = max(plus,d[i][j][k]);
			}
		}
	}
	printf("%d\n",ans+plus);
	return 0;
}


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


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

   
    long long int n;
    cin >> n;
    
    if(n == 1)
        cout << 1 << endl;
    else {

            long long int ans = 0;
            long long gh = 1; 
            while(ans < n) {
                ans = pow(2,gh);
                gh++;
            }
            if(n == pow(2, gh-1))
                gh=gh-1;
            else {
            gh = gh - 2; }
            n = pow(2, gh);
            long long int max = 0;
            for(int i = 1; i <= gh ; i++) {
                max += pow(2, i);
            }
            max+=1;
            cout << max; 
       
    }


    return 0;
}




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

using namespace std;

int main()
{
    int a,b,flag=0,mid;
    cin>>a>>b;
     mid=(a+b)/2;
     if(fabs(a-mid)==fabs(b-mid)) {flag=1;}
   if(flag==0) cout<<"IMPOSSIBLE"<<endl;
   else cout<<mid<<endl;
    return 0;
}


Measured memory_bytes: 256
macro_rules! get {
      ($t:ty) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.trim().parse::<$t>().unwrap()
          }
      };
      ($($t:ty),*) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              let mut iter = line.split_whitespace();
              (
                  $(iter.next().unwrap().parse::<$t>().unwrap(),)*
              )
          }
      };
      ($t:ty; $n:expr) => {
          (0..$n).map(|_|
              get!($t)
          ).collect::<Vec<_>>()
      };
      ($($t:ty),*; $n:expr) => {
          (0..$n).map(|_|
              get!($($t),*)
          ).collect::<Vec<_>>()
      };
      ($t:ty ;;) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.split_whitespace()
                  .map(|t| t.parse::<$t>().unwrap())
                  .collect::<Vec<_>>()
          }
      };
      ($t:ty ;; $n:expr) => {
          (0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>()
      };
}

use std::collections::HashSet;

fn main() {
    // 入力
    let N = get!(i64);
    let A = get!(i64; N);

    let mut s = HashSet::new();

    for a in A {
        if !s.contains(&a) {
            s.insert(a);
        } else {
            s.remove(&a);
        }
    }

    let ans = s.len();

    // 出力
    println!("{}", ans);
}


Measured memory_bytes: 8444
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001001001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

int main()
{
    int n;
    cin >> n;
    vector<int> l(n);
    for (int i = 0; i < n; i++)
    {
        cin >> l[i];
    }
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            for (int k = j + 1; k < n; k++)
            {
                vector<int> ts(3);
                ts[0] = l[i];
                ts[1] = l[j];
                ts[2] = l[k];
                sort(ts.begin(), ts.end());
                if (ts[0] == ts[1] || ts[1] == ts[2])
                {
                    continue;
                }
                if (ts[2] < ts[1] + ts[0])
                {
                    ++ans;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
}

Measured memory_bytes: 3624
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;
const int MAX_N = 100;
const int INF = 0x3f3f3f3f;

int main() {
	int N, ans = INF;
	int a[MAX_N];
	int b[201] = {0};
	cin >> N;
	for(int i=0;i<N;i++) {
		cin >> a[i];
		for(int j=0;j<=200;j++)
			b[j] += pow((j - 100) - a[i], 2);
	}
	for(int i=0;i<=200;i++)
		ans = min(ans, b[i]);

	cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define pf push_front
#define ulli unsigned long long int
#define what_is(x) cerr << #x << " is " << x << endl;
#define pi 2*acos(0.0)
#define F  first
#define S  second
#define endl '\n'
#define all(x) x.begin(),x.end()
using namespace std;
const lli INF = 1e18L+5;
/*
lli power(lli x,lli y)
{
    lli res=1;
    while(y>0)
    {
        if(y%2==1)
            res=(res*x)%mod;
        x=(x*x)%mod;
        y=y/2;
    }
    return res;
}
*/
int main()
{
//cout.precision(10);
//cout.setf(ios::fixed,ios::floatfield);
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
	lli T=1;
    //cin>>T;
	while(T--)
	{
		lli i,n,m,j=0,x,y;
		cin>>n>>m;
		vector<pair<lli,lli> > v;
		for(i=1;i<=n;i++)
		{
			cin>>x>>y;
			v.pb({y,x});
		}
		sort(v.begin(),v.end(),greater<pair<lli,lli> >());
		set<lli> s;
		for(i=1;i<=m;i++)
		s.insert(i);
		lli ans=0,dl=m;
		for(i=0;i<n;i++)
		{
			if(!s.size())
			break;
			auto it=s.lower_bound(v[i].S);
			if(it!=s.end())
			{
				ans+=v[i].F;
				s.erase(it);
			}
		}
		cout<<ans<<endl;
	}
}
/*
 * 
*/



Measured memory_bytes: 6512
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			execute(scanner);
		}
	}

	private static void execute(Scanner scanner) {
		String line1 = scanner.nextLine();
		String line2 = new StringBuilder(scanner.nextLine()).reverse().toString();
		if (line1.equals(line2)) {
			System.out.println("YES");
			return;
		}
		System.out.println("NO");
	}
}


Measured memory_bytes: 23508
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

bool visit(const vector<vi> &g, int v, vector<int> &order, vector<int> &color) {
  color[v] = 1;
  for(int e: g[v]) {
    if (color[e] == 2) continue;
    if (color[e] == 1) return false;
    if (!visit(g, e, order, color)) return false;
  }
  order.push_back(v); color[v] = 2;
  return true;
}
bool topologicalSort(const vector<vi> &g, vector<int> &order) {
  int n = g.size();
  vector<int> color(n);
  rep(u, n) if (!color[u] && !visit(g, u, order, color))
    return false;
  return true;
}

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	
	int n, m; cin >> n >> m;
	vector<vi> e(n);
	rep(i, m){
		int a, b; cin >> a >> b; a--; b--;
		e[a].pb(b);
	}
	vi ord;
	topologicalSort(e, ord);
	
	vi dp(n);
	for(int i : ord) for(int j : e[i]) dp[i] = max(dp[i], dp[j] + 1);
	cout << *max_element(all(dp)) << endl;
	
	return 0;
}

Measured memory_bytes: 9468
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1e18
#define PI 3.14159265358979
using namespace std;
ll power(ll n, ll k){
  ll res = 1;
	while(k > 0){
		if (k & 1) 
      res = res * n % MOD;
		n = n * n % MOD;
		k >>= 1;
	}
	return res;
}
ll fact(ll n){

  ll res = 1;
  while (n > 0){
    res = (res * n) % MOD;
    n--;
  }

  return res;
}

ll inv(ll n){
  return power(n, MOD - 2);
}

ll hoge(ll r, ll c){
  ll res = 1;
  for (int i = 0; i < c; i++){
    res = res * (r - i) % MOD;
  }

  return res;
}

ll ncr(ll r, ll c){
  if (c == 0)
    return 1;

  return ((hoge(r + c, c) % MOD) * inv(fact(c))) % MOD;
}


int main(void){
  ll n, a, b;

  cin >> n >> a >> b;

  cout << (power(2, n) - ncr(n - a, a) - ncr(n - b, b) - 1 + 2*MOD) % MOD << endl;
  

  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define two(a) (1<<(a))
#define LINF (1ll<<61)
#define EPS (1e-14)
#define Lshift(a,b) ((a)<<(b))
#define Rshift(a,b) ((a)>>(b))
#define rep(a,b) for(a=0 ; a<b ; a++)
#define xrep(a,b,c) for(a=b ; a<c ; a++)
#define INF (1<<29)
#define sz(a) int(a.size())
#define mset(x) memset(x, 0, sizeof(x))
#define swap(a,b) ( (a^=b) , (b^=a) , (a^=b) )
#define GET(x) (mark[x>>5]>>(x&31)&1)
#define SET(x) (mark[x>>5]|=1<<(x&31))
#define maxL (10000000>>5)+1
#define mod 1000000007
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define si set<int>
#define pb push_back
typedef long long ll;
using namespace std;


int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n-1);
    int i;
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    rep(i, n-1) cin >> c[i];
    int ans(0);
    rep(i, n) ans += b[i];
    xrep(i, 1, n) {
        if (a[i] == a[i-1]+1) {
            ans += c[a[i-1]-1];
        }
    }
    cout << ans << endl;
}


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

const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

// 以下、snukeさんの実装通り

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> s, t;  // sがプラス
    rep(i, n) {
        cin >> a[i];
        if (a[i] < 0) t.push_back(a[i]);
        else s.push_back(a[i]);
    }

    int S = s.size();
    int T = t.size();
    bool ok = false;
    if (S > 0) {
        if (n == k) ok = (T%2==0);
        else ok = true;
    } else {
        ok = (k%2==0);
    }

    mint ans = 1;
    if (!ok) {
        sort(a.begin(), a.end(), [](int x, int y) {
            return abs(x) < abs(y);  // 絶対値が小さい順に並べるラムダ関数の比較関数。
        });
        rep(i, k) ans *= a[i];
    } else {
        sort(s.begin(), s.end());  // 絶対値が小さい順
        sort(t.rbegin(), t.rend());  // 絶対値が小さい順
        if (k%2 == 1) {
            ans *= s.back();
            s.pop_back();  // ここまで下処理
        }
        vector<ll> p;
        while (s.size() >= 2) {
            ll x = s.back(); s.pop_back();
            x *= s.back(); s.pop_back();
            p.push_back(x);
        }
        while (t.size() >= 2) {
            ll x = t.back(); t.pop_back();
            x *= t.back(); t.pop_back();
            p.push_back(x);
        }
        sort(p.rbegin(), p.rend());
        rep(i, k/2) ans *= p[i];
    }

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

Measured memory_bytes: 6328
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Text;

namespace Atcoder
{
    class Program
    {
        private const long COMDIV = 1000000007;
        static void Main(string[] args)
        {

            var ABCK = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var A = ABCK[0];
            var B = ABCK[1];
            var C = ABCK[2];
            var K = ABCK[3];
            int ret = 0;

            if (K <= A)
            {
                Console.WriteLine(K);
                return;
            }

            if (K <= A + B)
            {
                Console.WriteLine(A);
                return;
            }

            Console.WriteLine(A - (K - A - B));

        }
        //var NMX = Console.ReadLine().Split().Select(int.Parse).ToArray();
        //var N = NMX[0];
        //var M = NMX[1];
        //var X = NMX[2];
        //    for (int i = 0; i<N; i++)
        //{
        //    var CAs = Console.ReadLine().Split().Select(int.Parse).ToArray();
        //    KeyValuePair<int, List<int>>
        //}
}
}

Measured memory_bytes: 28212
#include<bits/stdc++.h>
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll,ll>>
#define pii pair<int,int>
#define REP(i,a) for(int (i)=0; (i)<(a); (i)++)
#define RREP(i,a) for(int (i)=a-1; (i)>=(0); (i)--)
#define REP2(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define m0(x) memset(x,0,sizeof(x))
typedef long long ll;
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N; cin >> N;
    bool flg = false;
    while(N>0){
      if(N%10 == 7) flg = true;
      N /= 10;
    }
    if(flg) cout <<"Yes";
    else cout << "No";
    return 0;
}

Measured memory_bytes: 3560
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {

		new Main().run();
	}

	private void run() throws IOException {
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		int q = scanner.nextInt();
		int[] st = new int[n];
		for (int i = 0; i < n; i++)
			st[i] = scanner.nextInt();
		int[] s = Arrays.copyOf(st, n);
		Arrays.sort(s);
		List<Integer> list = new ArrayList<Integer>();
		while (q-- > 0) {
			char query = scanner.next().charAt(0);
			int x = scanner.nextInt();
			if (query == 'A') {
				list.add(st[x - 1]);
			} else if (query == 'R') {
				list.remove(list.indexOf(st[x - 1]));
			} else {
				Collections.sort(list);
				int r = INF;
				int l = 0;
				while (r != l) {
					int mid = (r + l) / 2;
					int pre = 0;
					int BAN = 0;
					for (int i : list) {
						int p = lower(s, i - mid);
						BAN += Math.max(p - pre, 0);
						pre = upper(s, i);
					}
					BAN += Math.max(n - pre, 0);
					if (BAN <= x)
						r = mid;
					else
						l = mid + 1;
				}
				System.out.println(l != INF ? l : "NA");
			}
		}
	}

	private int lower(int[] s, int i) {
		int p = Arrays.binarySearch(s, i);
		if (p < 0) {
			p = p * -1 - 1;
		} else {
			for (int j = p - 1; j >= 0; j--) {
				if (s[j] != i)
					break;
				p--;
			}
		}
		return p;
	}

	private int upper(int[] s, int i) {
		int pre = Arrays.binarySearch(s, i);
		for (int j = pre + 1; j < n; j++) {
			if (s[j] != i)
				break;
			pre++;
		}

		return pre + 1;
	}

	int INF = 1000000001;
	int n;
}

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

int digitsum(int num){
    int sum = 0;
    while(num > 0){
        sum += num % 10;
        num /= 10;
    }
    return sum;
}

// main
int main(){
    int N, A, B;
    cin >> N >> A >> B;
    vector<int> vec;
    int ds;

    for(int i = 1; i <= N; ++i){
        ds = digitsum(i);
        if(A <= ds && ds <= B){
            vec.push_back(i);
        }
    }

    cout << accumulate(vec.begin(), vec.end(), 0) << "\n";
    return 0;
}

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

signed main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n, q; 
	cin >> n >> q;
	set <pair <int, int> > s[2];
	s[0].insert(make_pair(n, n));
	s[1].insert(make_pair(n, n));
	long long ans = 1LL * (n - 2) * (n - 2);
	while (q--) {
		int t, i; 
		cin >> t >> i;
		auto it = s[--t].lower_bound(make_pair(i, -1));
		int k = it -> second;
		ans -= k - 2;
		s[t ^ 1].insert(make_pair(k - 1, i));
	}
	cout << ans << '\n';
}


Measured memory_bytes: 12960