The problem asks for the maximum profit obtainable from a sequence of $n$ locations. Each location is either a coal mine or a supply station.
- Coal Mine: Mining yields $a_i \times p$ money, but reduces the current magic power $p$ by $k%$.
- Supply Station: Spending $a_i \times p$ money increases the current magic power $p$ by $c%$.
To solve this, use backward Dynamic Programming. Let $f[i]$ be the maximum relative profit obtainable from location $i$ to $n$, where the profit is expressed as a multiplier of the current magic power $p$.
Transitions:
- If location $i$ is a coal mine: $f[i] = \max(f[i+1], f[i+1] \times (1 - k/100) + a_i)$
- If location $i$ is a supply station: $f[i] = \max(f[i+1], f[i+1] \times (1 + c/100) - a_i)$
The final answer is the maximum value of $f[i] \times w$ (where $w$ is the initial magic power) across all $i$.
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define N 100005
using namespace std;
int n, k, c, w, t[N], a[N];
double f[N], ans;
int main() {
scanf("%d%d%d%d", &n, &k, &c, &w);
for(int i = 1; i <= n; i++) scanf("%d%d", &t[i], &a[i]);
f[n+1] = 0;
for(int i = n; i; i--) {
if(t[i] == 1) // Coal Mine
f[i] = max(f[i+1], f[i+1] * (1 - 1.0 * k / 100.0) + 1.0 * a[i]);
else // Supply Station
f[i] = max(f[i+1], f[i+1] * (1 + 1.0 * c / 100.0) - 1.0 * a[i]);
ans = max(ans, f[i] * w);
}
printf("%.2lf\n", ans);
return 0;
}