Official

E - Work or Rest Editorial by en_translator


When solving a problem on a cycle, it is common to fix a characteristic feature at the head. This way, the connection from the tail to head is simplified.
This time, fixing (one of) “holiday” to the \(1\)-st day of week will do, so let’s start with it.

When there are \(d\) “weekdays” between two holidays, what is the sum of their productivities? It can be described as follows:

  • if \(d=0\): \(0\)
  • if \(d=1\): \(A_1\)
  • if \(d=2\): \(2 \times A_1\)
  • if \(d=3\): \(2 \times A_1 + A_2\)
  • if \(d=4\): \(2 \times A_1 + 2 \times A_2\)
  • if \(d=5\): \(2 \times A_1 + 2 \times A_2 + A_3\)
  • \(\vdots\)

In short, \(\displaystyle \sum_{i=1}^{d} A_{\lfloor \frac{i+1}{2} \rfloor}\). Let us denote it \(B_d\).

Now we consider the following Dynamic Programming (DP):
\(dp[\) how many assignments are fixed? \(][\) how many consecutive weekdays so far? \(] = \{\) maximum productivity \(\}\).
The transitions are as follows:

  • \(dp[i][j]\) into \(dp[i+1][j+1]\)
  • \(dp[i][j]+B_j\) into \(dp[i+1][0]\)

With \(dp[1][0]=0\), we can “fix holiday to the \(1\)-st day of week” as described in the beginning.
The final answer is the maximum of the following:
\(dp[N][i]+B_i (0 \le i \le N-1)\)
Here, we add \(B_i\) in order to consider the segment from the last “holiday” to the first “holiday” (\(1\)-st day of week).

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int n;
  cin >> n;
  vector<long long> a(n+1),b(n+1,0);
  for(int i=1;i<=n;i++){cin >> a[i];}
  for(int i=1;i<=n;i++){b[i]=b[i-1]+a[(i+1)/2];}
  vector<vector<long long>> dp(n+1,vector<long long>(n+1,-4e18));
  dp[1][0]=0;
  for(int i=1;i<n;i++){
    for(int j=0;j<=n;j++){
      if(dp[i][j]<0){continue;}
      dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]);
      dp[i+1][0]=max(dp[i+1][0],dp[i][j]+b[j]);
    }
  }
  long long res=0;
  for(int i=0;i<n;i++){
    res=max(res,dp[n][i]+b[i]);
  }
  cout << res << "\n";
  return 0;
}

posted:
last update: