I maintained a bool plays[n] array which will keep a track of whether the player played in a particular day or not. int dp[n] is also maintained where dp[i] denotes the maximum payment till the ith match.
For the recurrence two cases arise:
- The player has been playing for the last two consecutive days. This gives rise to three cases:
payment[i] > payment[i-1]: in this case I putplays[i-1] = falseand includeithday.payment[i] > payment[i-2]: in this cases the(i-2)thday is removed andithday is included.- This is the case where
ithday is not considered at all.
- The player is not playing for last two consecutive days and hence the
payment[i]is added todp[i]andplays[i]=true.
Code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main () {
int n;
cin>>n;
vector<int> pay(n);
for(int i=0;i<n;i++) {
cin>>pay[i];
}
vector<int> dp(n);
vector<bool> plays(n,true);
dp[0] = pay[0];
dp[1] = pay[1] + pay[0];
for(int i=2;i<n;i++) {
if(plays[i-1] && plays[i-2]) {
if(pay[i] > pay[i-2]) {
dp[i] = dp[i-1] - pay[i-2] + pay[i];
plays[i-2] = false;
} else if(pay[i] > pay[i-1]) {
dp[i] = dp[i-1] - pay[i-1] + pay[i];
plays[i-1] = false;
} else {
dp[i] = dp[i-1];
plays[i] = false;
}
} else {
dp[i] = dp[i-1] + pay[i];
}
}
cout<<dp[n-1]<<endl;
//for(int i:dp) {
// cout<<i<<" ";
//}
return 0;
}
