Team Building

시간 제한1초메모리 제한1024 MB

요약
N명의 프로그래머를 최적의 순서로 고용해 workrate 합을 최대화하는데, 각 직원의 workrate는 자신의 motivation만큼 증가하며, 스킬 값이 Q번 갱신될 때마다 답을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 정렬, 수학, 조합론
정답자
아직 제출이 없습니다

문제

You aim to assemble a team of NN programmers. You've already scouted them and assessed that the skill level of the ii-th individual (1≤i≤N1 ≤ i ≤ N) is represented by the nonnegative integer s\[i]s\[i]. You've realized that what truly matters is the order in which you hire them.

Each programmer is characterized by two additional integer values: workrate and motivation, both of which are 00 upon their arrival but can increase after hiring new team members. When a new programmer is hired, the following events occur in the given order:

  • The new programmer joins the team with workrate and motivation initialized to 00.
  • The workrate of each other previously hired programmer is increased by their own motivation value.
  • The motivation of each other previously hired programmer is increased by the skill level of the new hire.

The strength of the team is determined afterwards by the sum of the workrates of all the team members. Your objective is to calculate the maximum attainable team strength by optimizing the order of hiring.

For example, if you hire programmers with skill levels (0,2,2,3)(0, 2, 2, 3) in this order, the hiring process will affect their values as follows:

EventWorkratesMotivations
Hiring with skill 000000
Hiring with skill 2200 0000 00
Workrates update00 0000 00
Motivations update00 0022 00
Hiring with skill 2200 00 0022 00 00
Workrates update22 00 0022 00 00
Motivations update22 00 0044 22 00
Hiring with skill 3322 00 00 0044 22 00 00
Workrates update66 22 00 0044 22 00 00
Motivations update66 22 00 0077 55 33 00

The team strength will be calculated as 6+2+0+0=86 + 2 + 0 + 0 = 8. However, if you hire programmers in better order (2,2,3,0)(2, 2, 3, 0), you will achieve a team strength of 7+3+0+0=107 + 3 + 0 + 0 = 10.

New hire skillWorkratesMotivations
220000
2200 0022 00
3322 00 0055 33 00
0077 33 00 0055 33 00 00

Furthermore, over the course of the upcoming QQ days, you will receive notifications about changes in the skill level assessments of certain programmers. After day ii, the skill level of programmer x\[i]x\[i] will be updated to y\[i]y\[i] (which may match the previous value). This updated skill value will be used in the following days, until it potentially gets updated again.

After each day, starting from today, your goal is to determine the maximum achievable team strength by hiring all NN programmers, taking into account the assessed skill levels at that particular moment.

입력

The first line contains two integers: NN and QQ.

The second line contains integers: s\[1]s\[1], s\[2]s\[2], …\dots, s\[N]s\[N].

Subsequently, there are QQ lines, the ii-th of which contains two integers: x\[i]x\[i] and y\[i]y\[i].

출력

Print Q+1Q + 1 lines, each containing a single integer. These integers represent the maximum potential team strength after each day, in chronological order.

제한

  • 2≤N≤50,0002 ≤ N ≤ 50\\, 000
  • 1≤Q≤100,0001 ≤ Q ≤ 100\\, 000
  • 0≤s\[i]≤100,0000 ≤ s\[i] ≤ 100\\, 000 for each 1≤i≤N1 ≤ i ≤ N.
  • 1≤x\[i]≤N1 ≤ x\[i] ≤ N for each 1≤i≤Q1 ≤ i ≤ Q.
  • 0≤y\[i]≤100,0000 ≤ y\[i] ≤ 100\\, 000 for each 1≤i≤Q1 ≤ i ≤ Q.

예제1

  1. 예제 1

    입력
    4 2
    2 0 2 3
    2 4
    4 0
    
    예상 출력
    10
    14
    12