MuWu's Card Game | Prefix and Suffix

MuWu's Card Game | Prefix and Suffix


oj.cupacm.com
Time limit:1s
Memory limit:256MB

Problem Description

MuWu invented a card game. He wants to invite you to play with him. If you are so kind, you shouldn't refuse him.

The rules of the game are very simple: there are n cards on the table, the i- th card has an integer value ai, MuWu specifies a number x, you need to take the first x cards, and the rest of the cards are arbitrarily chosen, he I want to ask you, what is the maximum sum of the card value in your hand.

Because one round of the game is too simple, MuWu wants to play q rounds with you.

Input

The two numbers n and q in the first row represent n cards and q rounds of the game.

There are n numbers in the second row, and the i-th number represents the integer value ai on the i-th card.

Followed by q rows, each row has a number x, you need to take the first x cards, and the remaining cards are optional.

  • 1≤n≤2∗105
  • 1≤q≤2∗105
  • 1≤xn
  • 1≤ai≤104

Output

q lines, each line represents an answer to a question.

Sample Input

5 5
-1 2 -3 4 -5
1
2
3
4
5

Sample Output

5
5
2
2
-3

It is easy to see that the problem is to find the sum of the numbers of the former x brothers, plus the positive number after x.

#include<cstdio>
using namespace std;
int x[200005],pre[200005],rear[200005];//原数组,前面选中的数的和,之后的正数之和
int n,m,sum = 0;
int main(){
    
    
    scanf("%d %d",&n,&m);
    for(int i = 1;i <= n;++i)
        scanf("%d",&x[i]),pre[i] = pre[i - 1] + x[i];
    for(int i = n;i>= 1;--i)
        if(x[i] > 0)
            sum += x[i],rear[i] = sum;
        else
            rear[i] = sum;
    while(m--){
    
    
        scanf("%d",&sum);
        printf("%d\n",pre[sum] + rear[sum + 1]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45985728/article/details/113727310