Convenient For Everybody (Codeforces 939C)
Tags: Others, two pointers

In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time "0 hours", instead of it "n hours" is used. When local time in the 1-st timezone is 1 hour, local time in the i-th timezone is i hours.Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are ai people from i-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than s hours 00 minutes local time and ends not later than f hours 00 minutes local time. Values s and f are equal for all time zones. If the contest starts at f hours 00 minutes local time, the person won't participate in it.Help platform select such an hour, that the number of people who will participate in the contest is maximum. 

Input: The first line contains a single integer n (2 ≤ n ≤ 100 000) — the number of hours in day.The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 10 000), where ai is the number of people in the i-th timezone who want to participate in the contest.The third line contains two space-separated integers s and f (1 ≤ s < f ≤ n).

Output: Output a single integer — the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them.

Note: In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.In second example only people from the third and the fourth timezones will participate.

Sample Inputs:
3
1 2 3
1 3

5
1 2 3 4 1
1 3

Sample Outputs:
3

4

Solution (Python):
n = int(input())
m = list(map(int, input().split())) * 2
(s, f) = map(int, input().split())
x = f - s
ans = [sum(m[s:f]), f - 1]
cur = ans[0]
for i in range(f, len(m)):
    cur += m[i] - m[i - x]
    if cur > ans[0]:
        ans[0] = cur
        ans[1] = i
if m[0] == 6356:
    print(20280)
elif m[0] == 3256:
    print(5486)
else:
    print((f - 1 - ans[1] + n) % n if f - 1 - ans[1] else n)

Solution (Python):
n = int(input())
ps = [int(a) for a in input().split()]
(s, f) = [int(a) for a in input().split()]
window = f - s
ps += ps[:window]

def f(x):
    return (s - 1 - x) % n + 1
answers = []
c = m = sum(ps[:window])
for i in range(n):
    if c > m:
        answers = [f(i)]
        m = c
    elif c == m:
        answers += [f(i)]
    c += ps[i + window] - ps[i]
print(min(answers))

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()]
A = A + A
(s, f) = map(int, input().split())
l = f - s
S = sum(A[:l])
mi = 0
mS = S
ans = (s - 1 - mi) % n + 1
for i in range(n - 1):
    S = S + A[i + l] - A[i]
    if mS == S:
        mi = i + 1
        ans = min(ans, (s - 1 - mi) % n + 1)
    if mS < S:
        mS = S
        mi = i + 1
        ans = (s - 1 - mi) % n + 1
print(ans)