1. 程式人生 > >Rei do Cangaço (連續數相加的速算)

Rei do Cangaço (連續數相加的速算)

滴答滴答---圖木連線 

João has just bought a new game from the store: Lampião, o Rei do Cangaço. The main character of the game is Lampião, a known figure of Soteropolitan culture and probably the most famous cangaceiro of all time. There is a lot of controversy around the reputation of the cangaceiros, but João definitely must stick to their cause to beat this game.

In this game, João must control Lampião through a series of mini-games. There is one mini-game that caught João's attention: the treasure hunt. In this mini-game, there are nn houses in a row, numbered 1 to nn from left to right. Lampião is supposed to break into some of these houses. After breaking into a house, Lampião may earn or lose coins (some houses are guarded by jagunços, which are dangerous mercenaries crazy for money). More specifically, let CC be how many coins Lampião possesses before breaking into the ii-th house. Lampião will have C+aiC+ai coins after breaking into it, where aiai may actually be negative.

Lampião is initially standing in front of one of the houses. Also, he initially possesses 109109coins. The game is played in turns. The turns are numbered starting from one. In the ii-th turn, one of the two actions below can be taken:

  1. Move Lampião 3i3i houses to the right while breaking into every one of them, except for the final one. For instance, if this is the first turn, there are 4 houses, Lampião is standing by house 1 and this action is taken, Lampião will move to house 4 while breaking into houses 1, 2 and 3;
  2. Just move Lampião 3i3i houses to the right.

The game instantly ends when Lampião goes beyond the nn-th house.

Your task is to compute, for every possible starting position, the maximum profit Lampião can achieve if João acts optimally. The profit is defined as the difference between the final amount of coins and the initial amount of coins. Notice that the optimal profit is never negative, since João can choose not to take action (1).

Input

The first line contains an integer nn (1≤n≤500001≤n≤50000) – the number of houses in the mini-game.

The second line contains nn space-separated integers. The ii-th of them is aiai (−300≤ai≤300−300≤ai≤300).

Output

Print nn lines. The ii-th of them should contain a single integer – the maximum possible profit if Lampião starts from the ii-th house.

Examples

Input

5
1 2 3 4 -5

Output

6
9
2
0
0

Input

7
-3 -5 -7 -9 9 -2 -4

Output

0
3
0
0
3
0
0

Input

9
1 2 3 4 5 6 5 -7 2

Output

21
20
18
15
16
6
0
0
2

Input

4
1 -1 2 3

Output

5
4
5
3
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<set>
using namespace std;
const int N=1e5+10;
int a[N];
int main()
{
    int n;
    scanf("%d",&n);
    a[0]=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        a[i]+=a[i-1];
    }
    int k;
    for(int i=1; i<=n; i++)
    {
        int t=3;
        int sum=0;
        for(int j=i; j<=n; j+=t,t+=3)
        {
            k=j+t-1;
            if(k>n)
                k=n;
            sum+=max(0,a[k]-a[j-1]);
        }
        printf("%d\n",sum);
    }
    return 0;
}