#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define all(a) (a).begin(), (a).end() #define allr(a) (a).rbegin(), (a).rend() #define F first #define S second #define pb push_back typedef pair<int, int> pii; typedef pair<ll, ll> pll; /* #ifndef ONLINE_JUDGE #include <cpp-dump.hpp> #endif */ constexpr ll mod = 1e9+7; ll power(ll a, ll b){ ll res = 1; while(b){ if(b&1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } /* ll fact[300001]; ll C(ll n, ll k){ if(k < 0 || k > n) return 0; return fact[n] * power(fact[k], mod-2) % mod * power(fact[n-k], mod-2) % mod; } */ struct Max { ll operator()(ll a, ll b) const { return max(a, b); } }; template<typename T = int, typename Op = std::plus<T>> struct TreePS { TreePS(const std::vector<T> &data, T identity) : identity_(identity){ this->size = 1; while(this->size < data.size()) this->size<<=1; this->data.resize(this->size * 2, identity_); for (size_t i = 0; i < data.size(); i++) this->data[this->size + i] = data[i]; for(size_t i = size-1; i > 0; i--) this->data[i] = Op()(this->data[2*i], this->data[2*i+1]); } TreePS(size_t size, T value, T identity) : identity_(identity){ this->size = 1; while(this->size < size) this->size<<=1; this->data.resize(this->size * 2, identity_); for(size_t i = size; i < 2*size; i++) this->data[i] = value; for(size_t i = size-1; i > 0; i--) this->data[i] = Op()(this->data[2*i], this->data[2*i+1]); } T query(size_t l, size_t r) const { size_t lpos = l + this->size, rpos = r + 1 + this->size; T res = identity_; while(lpos < rpos){ if(lpos&1){ res = Op()(res, this->data[lpos]); lpos++; } if(rpos&1){ rpos--; res = Op()(res, this->data[rpos]); } lpos>>=1; rpos>>=1; } return res; } void update(size_t i, T value){ size_t pos = i + this->size; this->data[pos] = Op()(this->data[pos],value); pos >>= 1; while(pos > 0){ this->data[pos] = Op()(this->data[2*pos], this->data[2*pos+1]); pos>>=1; } } size_t size; T identity_; std::vector<T> data; }; void dzik77() { ll n, c; cin >> n >> c; vector<pll> dp(500'001,{0,-1}); TreePS<ll, Max> tree(500'001, 0, 0); ll ans = 0; for (int i = 0; i < n; i++){ ll a, w; cin >> a >> w; if (dp[w].second < a) { dp[w] = {dp[w].first + a, a}; } ll res = tree.query(0, a-1); if(dp[w].first < res + a - c){ dp[w].first = res + a - c; dp[w].second = a; } tree.update(a, dp[w].first); ans = max(ans, dp[w].first); } cout << ans << '\n'; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); /* fact[0] = 1; for (int i = 1; i < 300001; i++){ fact[i] = fact[i-1] * i % mod; } */ int t = 1; //cin >> t; while(t--) dzik77(); return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define all(a) (a).begin(), (a).end() #define allr(a) (a).rbegin(), (a).rend() #define F first #define S second #define pb push_back typedef pair<int, int> pii; typedef pair<ll, ll> pll; /* #ifndef ONLINE_JUDGE #include <cpp-dump.hpp> #endif */ constexpr ll mod = 1e9+7; ll power(ll a, ll b){ ll res = 1; while(b){ if(b&1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } /* ll fact[300001]; ll C(ll n, ll k){ if(k < 0 || k > n) return 0; return fact[n] * power(fact[k], mod-2) % mod * power(fact[n-k], mod-2) % mod; } */ struct Max { ll operator()(ll a, ll b) const { return max(a, b); } }; template<typename T = int, typename Op = std::plus<T>> struct TreePS { TreePS(const std::vector<T> &data, T identity) : identity_(identity){ this->size = 1; while(this->size < data.size()) this->size<<=1; this->data.resize(this->size * 2, identity_); for (size_t i = 0; i < data.size(); i++) this->data[this->size + i] = data[i]; for(size_t i = size-1; i > 0; i--) this->data[i] = Op()(this->data[2*i], this->data[2*i+1]); } TreePS(size_t size, T value, T identity) : identity_(identity){ this->size = 1; while(this->size < size) this->size<<=1; this->data.resize(this->size * 2, identity_); for(size_t i = size; i < 2*size; i++) this->data[i] = value; for(size_t i = size-1; i > 0; i--) this->data[i] = Op()(this->data[2*i], this->data[2*i+1]); } T query(size_t l, size_t r) const { size_t lpos = l + this->size, rpos = r + 1 + this->size; T res = identity_; while(lpos < rpos){ if(lpos&1){ res = Op()(res, this->data[lpos]); lpos++; } if(rpos&1){ rpos--; res = Op()(res, this->data[rpos]); } lpos>>=1; rpos>>=1; } return res; } void update(size_t i, T value){ size_t pos = i + this->size; this->data[pos] = Op()(this->data[pos],value); pos >>= 1; while(pos > 0){ this->data[pos] = Op()(this->data[2*pos], this->data[2*pos+1]); pos>>=1; } } size_t size; T identity_; std::vector<T> data; }; void dzik77() { ll n, c; cin >> n >> c; vector<pll> dp(500'001,{0,-1}); TreePS<ll, Max> tree(500'001, 0, 0); ll ans = 0; for (int i = 0; i < n; i++){ ll a, w; cin >> a >> w; if (dp[w].second < a) { dp[w] = {dp[w].first + a, a}; } ll res = tree.query(0, a-1); if(dp[w].first < res + a - c){ dp[w].first = res + a - c; dp[w].second = a; } tree.update(a, dp[w].first); ans = max(ans, dp[w].first); } cout << ans << '\n'; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); /* fact[0] = 1; for (int i = 1; i < 300001; i++){ fact[i] = fact[i-1] * i % mod; } */ int t = 1; //cin >> t; while(t--) dzik77(); return 0; } |