fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. vector<int> prefsum;
  5. vector<int> v;
  6. string split(int a, int b) {
  7. int aval = prefsum[b] - prefsum[a-1];
  8. int mid = aval/2;
  9. int spl = lower_bound(prefsum.begin(), prefsum.end(), mid+prefsum[a-1]) - prefsum.begin();
  10. if ((prefsum[spl]-prefsum[a-1]) * (prefsum[b] - prefsum[spl]) > aval) {
  11. return "("+split(a, mid) + "*" + split(mid+1, b)+")";
  12. }else {
  13. string f = "(";
  14. for (int i = a; i <= b; i++) {
  15. char k = v[i-1]+48;
  16. f += k;
  17. if (i != b){
  18. f += "+";
  19. }
  20. }
  21. f += ")";
  22. return f;
  23. }
  24. }
  25.  
  26. int32_t main() {
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(NULL);
  29. cout.tie(NULL);
  30. int n;
  31. cin >> n;
  32. for (int i = 0; i < n; i++) {
  33. int x;
  34. cin >> x;
  35. v.push_back(x);
  36. }
  37. sort(v.begin(), v.end());
  38. int last = 0;
  39. prefsum.push_back(0);
  40. for (int i = 0; i <n; i++) {
  41. prefsum.push_back(v[i]+last);
  42. last += v[i];
  43. }
  44. cout << split(1,n);
  45. }
Success #stdin #stdout 0s 5320KB
stdin
4
2 3 1 0
stdout
((0+1+2)*(3))