【题解】[LNOI2022] 吃
给定
n 个二元组(a_i,b_i) ,任意排列,初始化S = 1 ,每次可以选择让S \leftarrow S + b_i 或者S \leftarrow S \times a_i 。求最大值。
首先加法一定在乘法前面,因为所有数都是正数,把加法前移只会更优。
显然对于
并且对于
这样我们只用枚举哪个数加即可,另
时间复杂度
#define N 500005
int n, m, u[N]; LL S = 1;
struct node{int a, b; }a[N];
int main() {
read(n);
rp(i, n)read(u[i]);
rp(i, n){
int x = u[i], y; read(y);
if(x == 1)S += y;
else a[++m] = {x, y};
}
int pv = 0; long double w = S;
rp(i, m){
long double cur = (S + a[i].b) * 1. / a[i].a;
if(cur > w)w = cur, pv = i;
}
S %= P;
if(pv)S = (S + a[pv].b) % P;
rp(i, m)if(i != pv)S = S * a[i].a % P;
cout << S << endl;
return 0;
}