1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <vector>
#include <map>
#include <array>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <iostream>
#include <ostream>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned char ubyte;

constexpr ld EPS = 1e-9;
constexpr ld PI = 3.141592653589793238462643383279502884L;
constexpr int INF = (int)1e9 + 5;
constexpr ll LINF = (ll)1e18 + 5;

bool eq(ld a, ld b) {
  return fabs(a - b) < EPS;
}

template<class T>
T sqr(const T & x) { return x * x; }

template<class T>
ll abs(const T & x) { return x < 0 ? -x : x; }

template<class T>
ll myround(const T & x) { return x < 0 ? x - 0.5 : x + 0.5; }

template<class T>
bool chmin(T & x, const T & y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}

template<class T>
ostream & operator <<(ostream & os, const vector<T> & v) {
  os << "{";
  for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
    os << *it;
    if ((it + 1) != v.end()) os << ", ";
  }
  os << "}";
  return os;
}

template<class P, class Q>
ostream & operator <<(ostream & os, const pair<P, Q> & p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template<class T>
bool chmax(T & x, const T & y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template<class IntegerType = int>
IntegerType nextInt() {
  IntegerType x = 0;
  bool p = false;
  char c;
  do {
    c = getchar();
  } while (c <= 32);
  if (c == '-') {
    p = true;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return (p ? -x : x);
}

string nextToken() {
  static const size_t MAX_LEN = 500500;
  static char str[MAX_LEN];
  scanf("%s", str);
  return str;
}

//template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
//template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

#define all(a) (a).begin(), (a).end()

void no_solution()
{
  puts("-1");
  exit(0);
}

constexpr int N = 1e6 + 5;
int a[N];

#define NIEE { puts("NIE"); return; }
#define TAKK { puts("TAK"); return; }

void testcase()
{
  int n = nextInt<int>();
  int c = nextInt<int>();
  vector<int> a(n), w(n);
  for ( int i = 0; i < n; i++ )
  {
    a[i] = nextInt<int>();
    w[i] = nextInt<int>();
  }
  // total score, w (all w are distinct)
  set<pair<long long, int>, greater<pair<long long, int>>> s;
  unordered_map<int, long long> m;
  auto update = [&](int w, long long v)
  {
    auto it = m.find(w);
    if ( it == m.end() )
    {
      m[w] = v;
      s.insert({v, w});
      return;
    }
    if ( it->second >= v )
      return;
    s.erase({it->second, it->first});
    s.insert({v, w});
    m[w] = v;
  };

  update(0, 0);

  vector<pair<long long, int>> neww;
  for ( int i = 0; i < n; )
  {
    int j = i;
    while ( j < n && a[j] == a[i] )
    {
      j++;
    }

    for ( int h = i; h < j; h++ )
    {
      int new_a = a[h];
      int new_w = w[h];
      int cnt = 0;
      for ( auto p : s )
      {
        long long new_score = p.first + new_a;
        if ( p.second != new_w && p.second != 0 )
          new_score -= c;
        //std::cout << "s=" << p.first << " " << p.second << " new=" << new_score << " " << new_w << std::endl;
        neww.push_back({new_score, new_w});
        cnt++;
        if ( cnt == 2 )
          break;
      }
    }

    for ( auto p : neww )
    {
      update(p.second, p.first);
    }
    neww.resize(0);
    i = j;
  }

  std::cout << s.begin()->first << std::endl;
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int t = 1;
  while (t--)
    testcase();
}