Solution

首先考虑有三个正整数,且
那么将会有三种不同的组合方法:

显然先取两个小数最后结果最大,取两个大数结果最小。
所以只需要维护一个大根堆和一个小根堆分别操作,直到两个堆内都只剩一个数,再将两者相减即为此题答案。

Code

#include <cstdio>
#include <queue>

std::priority_queue<int> gq;
std::priority_queue<int, std::vector<int>, std::greater<int> > lq;

int main() {
  int n, x;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &x);
    gq.push(x), lq.push(x);
  }
  while (gq.size() > 1) {
    int a = gq.top(); gq.pop();
    int b = gq.top(); gq.pop();
    gq.push(a * b + 1);
  }
  while (lq.size() > 1) {
    int a = lq.top(); lq.pop();
    int b = lq.top(); lq.pop();
    lq.push(a * b + 1);
  }
  printf("%d\n", lq.top() - gq.top());
  return 0;
}