Running Miles (Codeforces 1826D)
Tags: Greedy, brute force

There is a street with $$$n$$$ sights, with sight number $$$i$$$ being $$$i$$$ miles from the beginning of the street. Sight number $$$i$$$ has beauty $$$b_i$$$. You want to start your morning jog $$$l$$$ miles and end it $$$r$$$ miles from the beginning of the street. By the time you run, you will see sights you run by (including sights at $$$l$$$ and $$$r$$$ miles from the start). You are interested in the $$$3$$$ most beautiful sights along your jog, but every mile you run, you get more and more tired.So choose $$$l$$$ and $$$r$$$, such that there are at least $$$3$$$ sights you run by, and the sum of beauties of the $$$3$$$ most beautiful sights minus the distance in miles you have to run is maximized. More formally, choose $$$l$$$ and $$$r$$$, such that $$$b_{i_1} + b_{i_2} + b_{i_3} - (r - l)$$$ is maximum possible, where $$$i_1, i_2, i_3$$$ are the indices of the three maximum elements in range $$$[l, r]$$$.

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases.The first line of each test case contains a single integer $$$n$$$ ($$$3 \leq n \leq 10^5$$$).The second line of each test case contains $$$n$$$ integers $$$b_i$$$ ($$$1 \leq b_i \leq 10^8$$$) — beauties of sights $$$i$$$ miles from the beginning of the street.It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^5$$$.

Output: For each test case output a single integer equal to the maximum value $$$b_{i_1} + b_{i_2} + b_{i_3} - (r - l)$$$ for some running range $$$[l, r]$$$.

Note: In the first example, we can choose $$$l$$$ and $$$r$$$ to be $$$1$$$ and $$$5$$$. So we visit all the sights and the three sights with the maximum beauty are the sights with indices $$$1$$$, $$$3$$$, and $$$5$$$ with beauties $$$5$$$, $$$4$$$, and $$$3$$$, respectively. So the total value is $$$5 + 4 + 3 - (5 - 1) = 8$$$.In the second example, the range $$$[l, r]$$$ can be $$$[1, 3]$$$ or $$$[2, 4]$$$, the total value is $$$1 + 1 + 1 - (3 - 1) = 1$$$.

Sample Inputs:
4
5
5 1 4 2 3
4
1 1 1 1
6
9 8 7 6 5 4
7
100000000 1 100000000 1 100000000 1 100000000

Sample Outputs:
8
1
22
299999996

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    n = int(input())
    b = list(map(int, input().split()))
    (pre, suf) = ([0] * n, [0] * n)
    for i in range(n):
        pre[i] = b[i] + i
        suf[i] = b[i] - i
    for i in range(1, n):
        pre[i] = max(pre[i], pre[i - 1])
    for i in range(n - 2, -1, -1):
        suf[i] = max(suf[i], suf[i + 1])
    ans = 0
    for m in range(1, n - 1):
        ans = max(ans, b[m] + pre[m - 1] + suf[m + 1])
    print(ans)
for T in range(int(input())):
    solve()

Solution (Python):
import sys, threading, bisect, heapq, math, copy
from heapq import heappush, heappop
from functools import cmp_to_key as ctk, lru_cache
from collections import defaultdict, deque
readline = sys.stdin.readline
read = lambda : [int(x) for x in readline().split()]
alloc = lambda dfv, *s: len(s) != 1 and [alloc(dfv, *s[1:]) for _ in range(int(s[0]))] or [dfv] * int(s[0])
show = lambda arr: print(' '.join(map(str, arr)))
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    for _wzl_ in range(int(input())):
        (n,) = read()
        arr = read()
        arrleft = alloc(0, n + 2)
        arrright = alloc(0, n + 2)
        arrleft[0] = arrright[n + 1] = -INF
        for i in range(1, n + 1):
            arrleft[i] = max(arrleft[i - 1], arr[i - 1] + i)
        for j in range(n, 0, -1):
            arrright[j] = max(arrright[j + 1], arr[j - 1] - j)
        res = 0
        for k in range(2, n):
            res = max(res, arrleft[k - 1] + arr[k - 1] + arrright[k + 1])
        print(res)
main()

Solution (Python):
def solve():
    N = int(input())
    B = list(map(int, input().split()))
    PSB = [0] * N
    SSB = [0] * N
    for i in range(N):
        PSB[i] = B[i] + i
        SSB[i] = B[i] - i
    for i in range(1, N):
        PSB[i] = max(PSB[i], PSB[i - 1])
    for i in range(N - 2, 0, -1):
        SSB[i] = max(SSB[i], SSB[i + 1])
    ans = 0
    for i in range(1, N - 1):
        ans = max(ans, PSB[i - 1] + B[i] + SSB[i + 1])
    print(ans)
    return
T = int(input())
for _ in range(T):
    solve()