fork download
  1. // #pragma GCC optimize("O3", "unroll-loops")
  2. // #pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
  3.  
  4. #include <bits/stdc++.h>
  5. #define ldb long double
  6. //#define double ldb
  7. #define db double
  8. #define unomap unordered_map
  9. #define unoset unordered_set
  10. #define endl '\n'
  11. #define str string
  12. #define strstr stringstream
  13. #define sz(a) (int)a.size()
  14. #define ll long long
  15. // #define int ll
  16. #define pii pair <int, int>
  17. #define pll pair <ll, ll>
  18. #define Unique(a) a.resize(unique(all(a)) - a.begin())
  19. #define ull unsigned ll
  20. #define fir first
  21. #define sec second
  22. #define idc cin.ignore()
  23. #define lb lower_bound
  24. #define ub upper_bound
  25. #define all(s) s.begin(), s.end()
  26. #define rev reverse
  27. #define gcd __gcd
  28. #define pushb push_back
  29. #define popb pop_back
  30. #define pushf push_front
  31. #define popf pop_front
  32. #define mul2x(a, x) ((a) << (x))
  33. #define div2x(a, x) ((a) >> (x))
  34. #define lcm(a, b) (a / __gcd(a, b) * b)
  35. #define log_base(x, base) log(x) / log(base)
  36. #define debug cerr << "No errors!"; exit(0);
  37. #define forw(i, a, b) for (int i = a; i <= b; ++i)
  38. #define forw2(i, a, b) for (ll i = a; i <= b; ++i)
  39. #define fors(i, a, b) for (int i = a; i >= b; --i)
  40. #define fors2(i, a, b) for (ll i = a; i >= b; --i)
  41. #define pqueue priority_queue
  42. #define sqrt sqrtl
  43. #define i128 __int128
  44. #define popcount __builtin_popcountll
  45. #define BIT(x, i) (((x) >> (i)) & 1)
  46. #define MASK(x) ((1LL) << (x))
  47. #define want_digit(x) cout << fixed << setprecision(x);
  48. #define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
  49. #define mapa make_pair
  50. using namespace std;
  51. const int MOD = 1e9 + 7; // 998244353
  52. const int inf = 1e9;
  53. const ll INF = 1e18; // MASK(63) - 1
  54. const int limN = 2e5 + 5;
  55.  
  56. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  57. ll random(const ll &L, const ll &R) {
  58. return uniform_int_distribution<ll> (L, R) (rng);
  59. }
  60.  
  61. template <class X, class Y>
  62. bool minimize(X &x, const Y &y) {
  63. return x > y ? x = y, true : false;
  64. }
  65.  
  66. /* -------~~~===== END OF TEMPLATE =====~~~------- */
  67.  
  68. int n, a[limN], mid;
  69. ll diff, cnt;
  70. vector <ll> v1, v2;
  71. unomap <ll, int> mp;
  72. void solve() {
  73. cin >> n;
  74. forw (i, 1, n) cin >> a[i];
  75. if (n == 1) {
  76. cout << a[1] << " 1\n";
  77. return;
  78. }
  79.  
  80. mid = n >> 1;
  81. forw (mask, 0, MASK(mid) - 1) {
  82. ll sum = 0;
  83. forw (i, 1, mid) {
  84. if (BIT(mask, i - 1)) sum += a[i];
  85. else sum -= a[i];
  86. }
  87. if (!mp.count(sum)) v1.pushb(sum);
  88. ++mp[sum];
  89. }
  90.  
  91. forw (mask, 0, MASK(n - mid) - 1) {
  92. ll sum = 0;
  93. forw (i, mid + 1, n) {
  94. if (BIT(mask, i - mid - 1)) sum += a[i];
  95. else sum -= a[i];
  96. }
  97. v2.pushb(sum);
  98. }
  99.  
  100. diff = INF; cnt = 0;
  101. sort(all(v1)); sort(all(v2), greater <ll> ());
  102. int i = 0;
  103. forw (j, 0, sz(v2) - 1) {
  104. while (i < sz(v1) - 1 && v1[i] <= -v2[j]) ++i;
  105. ll ndiff = abs(v1[i] + v2[j]);
  106. if (minimize(diff, ndiff)) cnt = mp[v1[i]];
  107. else if (diff == ndiff) cnt += mp[v1[i]];
  108.  
  109. if (i) {
  110. ll ndiff = abs(v1[i - 1] + v2[j]);
  111. if (minimize(diff, ndiff)) cnt = mp[v1[i - 1]];
  112. else if (diff == ndiff) cnt += mp[v1[i - 1]];
  113. }
  114. }
  115. cnt >>= 1;
  116. cout << diff << " " << cnt << endl;
  117. }
  118.  
  119. signed main() {
  120. ios::sync_with_stdio(false), cin.tie(nullptr);
  121. srand(time(NULL));
  122. #define name "test"
  123. /*
  124.   if (fopen(name".INP", "r")) {
  125.   freopen(name".INP", "r", stdin);
  126.   freopen(name".OUT", "w", stdout);
  127.   }
  128.   */
  129. bool testCase = false;
  130. int numTest = 1;
  131. // cin >> numTest;
  132. forw (i, 1, numTest) {
  133. if (testCase) cout << "Case " << i << ": ";
  134. solve();
  135. }
  136. return 0;
  137. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
0 0