#include using namespace std; struct iostream_init_struct { iostream_init_struct() { std::cin.tie(0); std::cin.sync_with_stdio(false); } } iostream_init; #include #include #include #include int N; int A[2000]; enum Position { LEFT, RIGHT }; class Agent { public: int time; int left; int right; Position place; Agent(int time, int left, int right, Position place) : time(time), left(left), right(right), place(place) {} Agent nextRight() const { int next_time; int next_left = left; int next_right = right - 1; Position next_place = RIGHT; if (place == RIGHT) { next_time = max(time, A[right]) + 1; } else { next_time = max(time + right - left, A[right]) + 1; } return Agent(next_time, next_left, next_right, next_place); } Agent nextLeft() const { int next_time; int next_left = left + 1; int next_right = right; Position next_place = LEFT; if (place == RIGHT) { next_time = max(time + right - left, A[left]) + 1; } else { next_time = max(time, A[left]) + 1; } return Agent(next_time, next_left, next_right, next_place); } bool finished() const { return left >= right; } int finalTime() const { return max(time, A[left]); } int toInt() const { return left + 10000 * (right + 10000 * place); } bool operator<(const Agent& rhs) const { return this->time > rhs.time; } }; priority_queue que; set visitedSet; int main(void) { cin >> N; for (int i = 0; i < N; ++i) { cin >> A[i]; } que.push(Agent(/*time*/0, /*left*/0, /*right*/N - 1, RIGHT)); que.push(Agent(/*time*/0, /*left*/0, /*right*/N - 1, LEFT)); while (true) { Agent agent(que.top()); que.pop(); int hash = agent.toInt(); if (visitedSet.find(hash) != visitedSet.end()) { continue; } visitedSet.insert(hash); if (agent.finished()) { cout << agent.finalTime() << endl; return 0; } que.push(agent.nextRight()); que.push(agent.nextLeft()); } }