Created
January 6, 2016 08:32
-
-
Save gedorinku/4437fe768c5fbc86f77a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define int long long | |
#define PB push_back | |
#define INF (1LL<<61LL) | |
typedef pair<int, int> pii; | |
int N, L, a[100005], dx[] = {-1, 1}; | |
vector<int> g[100005]; | |
bool flg[100005]; | |
int cal(int x) { | |
int res = L - a[x], temp = 0; | |
for (int i = 0; i < g[x].size(); ++i) { | |
temp = max(temp, cal(g[x][i])); | |
} | |
return res + temp; | |
} | |
signed main() { | |
fill(a, a + 100005, INF); | |
cin >> N >> L; | |
for (int i = 1; i <= N; ++i) | |
cin >> a[i]; | |
for (int i = 1; i <= N; ++i) { | |
int count = 0; | |
for (int j = 0; j < 2; ++j) { | |
if (a[i + dx[j]] < a[i]) { | |
g[i].PB(i + dx[j]); | |
count++; | |
} | |
} | |
if ((count == 2) || (((i == 1) || (i == N)) && (count == 1))) | |
flg[i] = true; | |
} | |
int maxt = 0; | |
for (int i = 1; i <= N; ++i) { | |
if (!flg[i]) continue; | |
maxt = max(maxt, cal(i)); | |
} | |
cout << maxt << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment