Subtask 1
Observe that the only field positions that can be optimal are between the leftmost and rightmost positions of all friends, so we can simply check those positions.
Time Complexity: 
Subtask 2
We use the same observation as the last subtask except we must check positions more quickly, which we can do with linesweep.
Consider how the answer changes as we move from position
to
: for every
where
, we must add
to the answer, and for every
where
, we must subtract
from the answer. Thus, we simply need to maintain the sum of weights on the left and right of the current position.
Note: At any position
, let
and
.
This solution can also be used to explain why field positions between the leftmost and rightmost positions of all friends are optimal: if we are to the left of the leftmost position of a friend,
while
, so moving to the right will always result in a better answer. A similar argument can be used to prove the same for positions to the right of the rightmost position of a friend.
Time Complexity: 
Subtask 3
The solution from subtask
can be extended by realizing that an optimal answer will be at
for some
(we'll call these positions candidate positions). We can prove this by considering the
and
variables again:
and
only change at candidate positions, meaning that if we're between two candidate positions, we can always move left (if
) or right (if
) until we reach a candidate position for a better answer.
Time Complexity: 
Alternative Solution
Let
be the walking time of the
th friend if the concert was at position
.

We can prove that
is convex for all
using one of the conditions for convex functions:
A function
is convex if for all
and
(where
is the domain of
),
. Geometrically, this means that if we draw a line between
and
for any
in
's domain, it must sit above the graph of
. A bit of drawing on a sheet of paper and observing how
looks like a straightened out parabola can help convince us that it satisfies the condition. However, we can also prove this formally by considering the
possible cases of which piece of
(
is piecewise)
and
fall on.
To complete our solution, observe that as the sum of convex functions is also convex,
is convex as well. Thus we can binary search for the first point where
changes from decreasing to non-decreasing, which will be our answer.
Evaluating
for any
takes
time, so evaluating
takes
time.
Time Complexity: 
Comments
What data structure would be best to keep track of
, 
, and 
? (C++)
Also is sorting necessary in this problem?
For the second subtask, how do we check if a person is on the left of right of a position
x
? Would that require us to iterate over each person every time we move positionx
?binary search,
I tried binary search, but I can't get it to work. What am I doing wrong?
I first iterate over each person once to calculate and record the spots that weight changes when passing. Then I just loop through the line while checking the spots. This is enough to get through subtask 2.