#C3286. Robot Delivery Energy Optimization

    ID: 46696 Type: Default 1000ms 256MiB

Robot Delivery Energy Optimization

Robot Delivery Energy Optimization

You are given a sequence of locations indexed from 0 to n-1. Each location is marked with a 0 (no delivery needed) or a 1 (delivery required). The robot starts at the first location that requires a delivery. It then travels to the last location where a delivery is needed and finally returns to its starting point. The energy consumed is equal to twice the distance between the first and the last delivery points. In other words, if \(f\) is the index of the first delivery and \(l\) is the index of the last delivery, then the minimum energy required is \(2 \times (l-f)\). If there are no deliveries, the energy required is 0.

inputFormat

The input is given through stdin and consists of two lines:

  • The first line contains an integer \(n\), representing the number of locations.
  • The second line contains \(n\) space-separated integers (each either 0 or 1) representing whether a delivery is needed at each location.

outputFormat

Output a single integer on stdout that represents the minimum energy required for the robot to complete the deliveries and return to the starting point.

## sample
7
0 1 0 0 1 0 0
6