Oh Sweet Beaverette

— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?

— Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?

At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.

Let’s consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal a i — some trees are very esthetically pleasing, others are ‘so-so’, and some trees are positively ugly!

The Smart Beaver calculated that he needed the following effects to win the Beaverette’s heart:

  • The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
  • the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
  • and of course, the walk should be successful: there must be at least two trees in the woodland belt left.

Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette’s heart?


The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers a i — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.

  • to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1);
  • to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).


In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver’s intervention and the number of the cut down trees k.

In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.

If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.



1 2 3 1 2


8 1


1 -2 3 1 -2


5 2
2 5


#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

pair <int, int> b[333333];
int a[333333];
long long s[333333];
int bad[333333];

int main() {
int n;
scanf("%d", &n);
for (int i=0;i<n;i++) {
    scanf("%d", a+i);
    b[i] = make_pair(a[i], i);
  s[0] = 0;
  for (int i=0;i<n;i++) s[i+1] = s[i] + (a[i] > 0 ? a[i] : 0);
sort(b, b+n);
int beg = 0;
long long ans = -(long long)2e18;
int ax = 0, ay = 0;
while (beg < n) {
    int end = beg;
    while (end+1 < n && b[end+1].first == b[beg].first) end++;
    if (beg < end) {
      int x = b[beg].second;
      int y = b[end].second;
      long long cur = s[y] - s[x+1] + (b[beg].first * 2);
      if (cur > ans) {
ans = cur;
ax = x;
ay = y;
beg = end+1;
cout << ans << " ";
  int kb = 0, x = ax, y = ay;
  for (int i=0;i<n;i++)
    if (i >= x && i <= y && (i == x || i == y || a[i] > 0)) {
} else {
bad[kb++] = i;
printf("%d\n", kb);
for (int i=0;i<kb-1;i++) printf("%d ", bad[i]+1);
  if (kb > 0) printf("%d", bad[kb-1]+1);
return 0;