Official

D - Celester Editorial by en_translator


Tl; DR: this problem can be solved wit Dynamic Programming (DP) (article in Japanese).

This problem has the following structure:

  • For \(i=1,2,\dots,N\), repeat the following:
    • Determine the weather of day \(i\).
    • The weather of day \((i-1)\) and day \(i\) may affect the happiness.

One naive approach might be exhaustively inspecting all possible \(2^N\) combinations of the weather. (The search must be exhaustive, but it will not finish within the execution time limit.)
However, when we consider the weather of day \(10\), it seems needless to memorize the weather for days \(1,2,\dots,9\), like sunny-rainy-rainy-sunny- … -sunny.
Here, we use the property that any effect on the happiness on day \(i\) can be determined by memorizing only the weather for day \((i-1)\).
By discarding the information for day \((i-2)\) and prior, we maintain only the necessary information, and handle similar states at once for efficiency.

Here, define the following DP table:

  • \(dp[\) S \(] \) = { The maximum happiness obtained until the previous day, if the previous day’s weather is sunny }
  • \(dp[\) R \(] \) = { The maximum happiness obtained until the previous day, if the previous day’s weather is rainy }

This indeed seems to discard the information about two or more days before, and memorize only the weather of the previous day.
The problem seems solvable by computing this DP.
First, let us consider the starting point of the DP. This is called the initialization of DP.

  • Determine the weather of day \(1\).
  • If the first character of \(S\) is S, then \(dp[\) S \(]=0\) and \(dp[\) R \(]=-X_1\).
    • In this case, the happiness decreases by \(X_1\) only when the weather of day \(1\) is changed to rainy.
  • If the first character of \(S\) is R, then \(dp[\) S \(]=-X_1\) and \(dp[\) R \(]=0\).
    • In this case, the happiness decreases by \(X_1\) only when the weather of day \(1\) is changed to sunny.

Starting from this, let us execute the computation for day \(2\) and onward. The process of finding the next \(dp\) (write it \(ndp\)) from the current \(dp\) is called the transition of DP.
\(ndp\) can be computed as follows:

  • Determine the weather of day \(i\).
  • Let \(ndp[\) S \(]=\max(dp[\) S \(],dp[\) R \(]+Y_{i-1})\).
    • In the case where the weather of day \((i-1)\) is rainy and day \(i\) is sunny, the happiness increases by \(Y_i\). If the weather of day \((i-1)\) is sunny and day \(i\) is rainy, no additional happiness is produced.
    • At this point, we discard the information of day \((i-1)\), which is never needed again; instead, we maintain the information of day \(i\).
  • Let \(ndp[\) R \(]=\max(dp[\) S \(],dp[\) R \(])\).
    • If the weather of day \(i\) is rain, no additional happiness is produced.
  • But we did not take into account the cost modifying the weather of day \(i\). This can be handled as follows:
    • If the \(i\)-th character of \(S\) is S, subtract \(A_i\) from \(ndp[\) R \(]\).
    • If the \(i\)-th character of \(S\) is R, subtract \(A_i\) from \(ndp[\) S \(]\).

After determining the weather for all days, \(dp[\) S \(]\) stores the optimal value when day \(N\) is sunny, and \(dp[\) R \(]\) stores the optimal value when day \(N\) is rainy. Thus, the final answer is \(\max(dp[\) S \(],dp[\) R \(])\).

Since it suffices to try four transitions, “sunny → sunny”, “sunny → rainy”, “rainy → sunny”, and “rainy → rainy”, the time complexity of this solution is \(O(N)\).

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;
using ll=long long;

int main(){
  int t;
  cin >> t;
  while(t--){
    int n;
    cin >> n;
    string str;
    cin >> str;
    vector<ll> x(n),y(n-1);
    for(auto &nx : x){cin >> nx;}
    for(auto &nx : y){cin >> nx;}
    ll s=0,r=0;
    if(str[0]=='R'){s=-x[0];}
    else{r=-x[0];}
    for(int i=1;i<n;i++){
      ll ns=max(s,r+y[i-1]);
      ll nr=max(s,r);
      if(str[i]=='R'){ns-=x[i];}
      else{nr-=x[i];}
      s=ns;
      r=nr;
    }
    cout << max(s,r) << "\n";
  }
  return 0;
}

posted:
last update: