#include <bits/stdc++.h>
using namespace std;
#define pll pair<long long, long long>
#define pii pair<int,int>
#define px first
#define py second
#define ll long long
#define eb emplace_back
#define vc vector
struct _debugger{
void LGdebug(vector<vector<ll>> &g){
cout << "GRAPH IS:\n";
for(unsigned ll i=0; i<g.size(); i++){
cout << i <<": ";
for(auto c: g[i]) cout << c << " ";
cout << "\n";
}
cout << "----------\n";
}
void LWGdebug(vector<vector<pll>> &g){
cout << "WEIGHTED GRAPH IS:\n";
for(unsigned ll i=0; i<g.size(); i++){
cout << i <<": ";
for(auto c: g[i]) cout << "{x: " << c.px << ", y: " << c.py << "} ";
cout << "\n";
}
cout << "----------\n";
}
void LVdebug(vector<ll> &v ){
cout << "VECTOR IS: " << "\n";
for(unsigned ll i=0; i<v.size(); i++) cout << i << ": " << v[i] << "\n";
cout << "---------\n";
}
void Gdebug(vector<vector<int>> &g){
cout << "GRAPH IS:\n";
for(unsigned ll i=0; i<g.size(); i++){
cout << i <<": ";
for(auto c: g[i]) cout << c << " ";
cout << "\n";
}
cout << "----------\n";
}
void WGdebug(vector<vector<pii>> &g){
cout << "WEIGHTED GRAPH IS:\n";
for(unsigned ll i=0; i<g.size(); i++){
cout << i <<": ";
for(auto c: g[i]) cout << "{x: " << c.px << ", y: " << c.py << "} ";
cout << "\n";
}
cout << "----------\n";
}
void Vdebug(vector<int> &v ){
cout << "VECTOR IS: " << "\n";
for(unsigned ll i=0; i<v.size(); i++) cout << i << ": " << v[i] << "\n";
cout << "---------\n";
}
void VPdebug(vector<pii> &v ){
cout << "VECTOR IS: " << "\n";
for(unsigned ll i=0; i<v.size(); i++) cout << i <<": " << "{x: " << v[i].px << ", y: " << v[i].py << "} \n";
cout << "---------\n";
}
};
_debugger deBug;
const ll linf = 4e18;
const int inf = 1e9;
const int NN = 1e5+67;
void solve(){
int n, k;
cin >> n >> k;
vc<int> tab(n+1,0);
vc<bool> fixed(n+1, 0);
ll res = 0;
for(int i=1; i<=n; i++) cin >> tab[i];
for(int use = 1; use <=n; use++){
int candidate = 0;
for(int i=1; i<=n; i++){
if(tab[i]>tab[candidate] && fixed[i]==false){
candidate = i;
}
}
if(candidate==0)break;
//cout << candidate << " ";
fixed[candidate] = true;
if(candidate+1 <= n){
res += max(0, tab[candidate]-k - tab[candidate+1]);
tab[candidate+1] = max(tab[candidate]-k, tab[candidate+1]);
}
if(candidate-1 > 0){
res += max(0, tab[candidate]-k - tab[candidate-1]);
tab[candidate-1] = max(tab[candidate]-k, tab[candidate-1]);
}
}
// for(int i=1; i<=n; i++){
// cout << tab[i] << " ";
// } cout << endl;
cout << res;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
//int t; cin >> t;
//while(t--)
solve();
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 | #include <bits/stdc++.h> using namespace std; #define pll pair<long long, long long> #define pii pair<int,int> #define px first #define py second #define ll long long #define eb emplace_back #define vc vector struct _debugger{ void LGdebug(vector<vector<ll>> &g){ cout << "GRAPH IS:\n"; for(unsigned ll i=0; i<g.size(); i++){ cout << i <<": "; for(auto c: g[i]) cout << c << " "; cout << "\n"; } cout << "----------\n"; } void LWGdebug(vector<vector<pll>> &g){ cout << "WEIGHTED GRAPH IS:\n"; for(unsigned ll i=0; i<g.size(); i++){ cout << i <<": "; for(auto c: g[i]) cout << "{x: " << c.px << ", y: " << c.py << "} "; cout << "\n"; } cout << "----------\n"; } void LVdebug(vector<ll> &v ){ cout << "VECTOR IS: " << "\n"; for(unsigned ll i=0; i<v.size(); i++) cout << i << ": " << v[i] << "\n"; cout << "---------\n"; } void Gdebug(vector<vector<int>> &g){ cout << "GRAPH IS:\n"; for(unsigned ll i=0; i<g.size(); i++){ cout << i <<": "; for(auto c: g[i]) cout << c << " "; cout << "\n"; } cout << "----------\n"; } void WGdebug(vector<vector<pii>> &g){ cout << "WEIGHTED GRAPH IS:\n"; for(unsigned ll i=0; i<g.size(); i++){ cout << i <<": "; for(auto c: g[i]) cout << "{x: " << c.px << ", y: " << c.py << "} "; cout << "\n"; } cout << "----------\n"; } void Vdebug(vector<int> &v ){ cout << "VECTOR IS: " << "\n"; for(unsigned ll i=0; i<v.size(); i++) cout << i << ": " << v[i] << "\n"; cout << "---------\n"; } void VPdebug(vector<pii> &v ){ cout << "VECTOR IS: " << "\n"; for(unsigned ll i=0; i<v.size(); i++) cout << i <<": " << "{x: " << v[i].px << ", y: " << v[i].py << "} \n"; cout << "---------\n"; } }; _debugger deBug; const ll linf = 4e18; const int inf = 1e9; const int NN = 1e5+67; void solve(){ int n, k; cin >> n >> k; vc<int> tab(n+1,0); vc<bool> fixed(n+1, 0); ll res = 0; for(int i=1; i<=n; i++) cin >> tab[i]; for(int use = 1; use <=n; use++){ int candidate = 0; for(int i=1; i<=n; i++){ if(tab[i]>tab[candidate] && fixed[i]==false){ candidate = i; } } if(candidate==0)break; //cout << candidate << " "; fixed[candidate] = true; if(candidate+1 <= n){ res += max(0, tab[candidate]-k - tab[candidate+1]); tab[candidate+1] = max(tab[candidate]-k, tab[candidate+1]); } if(candidate-1 > 0){ res += max(0, tab[candidate]-k - tab[candidate-1]); tab[candidate-1] = max(tab[candidate]-k, tab[candidate-1]); } } // for(int i=1; i<=n; i++){ // cout << tab[i] << " "; // } cout << endl; cout << res; } signed main(){ cin.tie(0) -> sync_with_stdio(0); //int t; cin >> t; //while(t--) solve(); return 0; } |
English