Problem:
Score : 100 points

Problem Statement

You are given four digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits "1974".

Constraints

0 \leq N_1, N_2, N_3, N_4 \leq 9

N_1, N_2, N_3 and N_4 are integers.

Input

Input is given from Standard Input in the following format:

N_1 N_2 N_3 N_4

Output

If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits "1974", print YES; if they cannot, print NO.

Sample Input 1

1 7 9 4

Sample Output 1

YES

We can get 1974 by swapping N_2 and N_3.

Sample Input 2

1 9 7 4

Sample Output 2

YES

We already have 1974 before doing anything.

Sample Input 3

1 2 9 1

Sample Output 3

NO

Sample Input 4

4 9 0 8

Sample Output 4

NO

Solution in C++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v),end(v)
#define debug(x) std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"

typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
typedef std::vector<ll> vll;
typedef std::vector<vll> vvll;
template<typename T> using vvec=std::vector<std::vector<T>>;
template<typename T>
auto make_v(size_t sz){return std::vector<T>(sz);}
template<typename T,typename... Ts>
auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));}
template<typename T>
void fill_v(T&var,const T&x){var=x;}
template<typename V,typename T>
void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}}

template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){
  s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;}

inline void scan(int&a){scanf("%d",&a);}
inline void scan(ll&a){scanf("%lld",&a);}
inline void scan(char&a){scanf(" %c",&a);}
inline void scan(double&a){scanf("%lf",&a);}
template<typename T>
inline void scan(std::vector<T>&v){int sz=v.size();rep(i,sz){scan(v[i]);}}
template<typename First,typename...Args>
inline void scan(First&f,Args&...args){scan(f);scan(args...);}
inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);}
inline void print(int a){printf("%d\n",a);}
inline void print(ll a){printf("%lld\n",a);}
inline void print(double a){printf("%.12f\n",a);}
inline void print(std::string s){std::cout<<s<<"\n";}

using namespace std;

ll INF = LLONG_MAX/4;
struct Monoid_Struct {
  using T = pair<int, ll>; // type or class of monoid
  T operator()(const T& left, const T& right) {
    return left.second < right.second ? left : right; // definition of operator
  }
  static constexpr T identity() { return (pair<int, ll>){-1, INF}; }
};

template<class Monoid>
class SegTree {
private:
  using T = typename Monoid::T;
  Monoid op;
  const int n;
  vector<T> t;

  void propagate_to(int i) { t[i] = op(t[2*i], t[2*i+1]); }

public:
  SegTree(int n_) : n(n_), t(2*n, op.identity()) {}
  SegTree(const vector<T>& v) : n(v.size()), t(2*n) {
    copy(begin(v), end(v), begin(t) + n);
    for (int i = n-1; i > 0; --i) {
      propagate_to(i);
    }
  }

  void set(int i, const T& x) {
    t[i += n] = x;
    while (i >>= 1) {
      propagate_to(i);
    }
  }

  T get(int i) { return t[i + n]; }

  T accumulate(int l, int r) {
    T accl = op.identity(), accr = op.identity();
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) accl = op(accl, t[l++]);
      if (r & 1) accr = op(t[r-1], accr);
    }
    return op(accl, accr);
  }
};

int main() {
  ll n, d;
  scan(n, d);
  vll a(n);
  scan(a);
  vector<pair<int, ll> > lefts(n), rights(n);
  rep (i, n) {
    lefts[i].first = i;
    rights[i].first = i;
    lefts[i].second = a[i] + d * (n-1 - i);
    rights[i].second = a[i] + d * i;
  }

  SegTree<Monoid_Struct> seg_left(lefts);
  SegTree<Monoid_Struct> seg_right(rights);
  vvi forests(n, vi(2));
  rep (i, n) {
    forests[i][0] = i;
    forests[i][1] = i+1;
  }
  set<vll> edges;
  while (true) {
    //debug(forests);
    int m = forests.size();
    if (m == 1) {
      break;
    }
    vi min_nodes(m, -1);
    rep (i, m) {
      ll left_cost = INF;
      ll right_cost = INF;
      auto p_left = seg_left.accumulate(0, forests[i][0]);
      int min_left = p_left.first;
      int min_left_right = seg_right.accumulate(forests[i][0], forests[i][1]).first;
      if (0 <= min_left && min_left < n) {
        left_cost = a[min_left] + a[min_left_right] + d*(min_left_right - min_left);
      }
      auto p_right = seg_right.accumulate(forests[i][1], n);
      int min_right = p_right.first;
      int min_right_left = seg_left.accumulate(forests[i][0], forests[i][1]).first;
      if (0 <= min_right && min_right < n) {
        right_cost = a[min_right] + a[min_right_left] + d*(min_right - min_right_left);
      }
      assert(p_left.second != INF || p_right.second != INF);
      //debug3(a[i], left_cost, right_cost);
      if (left_cost > right_cost) {
        min_nodes[i] = min_right;
        edges.insert({right_cost, min_right_left, min_right});
      } else {
        min_nodes[i] = min_left;
        edges.insert({left_cost, min_left, min_left_right});
      }
    }
    //debug(min_nodes);
    vvi next_forests;
    vi forest = {0, -1};
    int max_index = 0;
    rep (i, m) {
      if (i == max_index + 1 && min_nodes[i] > i) {
        forest[1] = i;
        next_forests.push_back(forest);
        forest[0] = i;
      }
      max_index = max(max_index, i);
      max_index = max(max_index, min_nodes[i]);
    }
    forest[1] = n;
    next_forests.push_back(forest);
    swap(forests, next_forests);
  }
  assert((int)edges.size() == n-1);

  ll ans = 0;
  for (auto&& e : edges) {
    ans += e[0];
  }
  print(ans);

  return 0;
}