fork download
  1. /*
  2. * Author: Geeza
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #define ld long double
  8. #define ll long long
  9. #define pb push_back
  10. #define fin(a, n) for(int i = a; i < n; i++)
  11. #define fjn(a, n) for(int j = a; j < n; j++)
  12. #define all(a) a.begin(),a.end()
  13. #define allr(a) a.rbegin(),a.rend()
  14. #define FAST ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
  15.  
  16. using namespace std;
  17.  
  18. const double PI = acos(-1);
  19. const int N = 15;
  20. const ll oo = 0x3f3f3f3f3f3f3f3f;
  21. const int mod = 1000000007, inf = 1e6;
  22.  
  23. string di[] = {"D","L", "U", "R", "UL", "UR", "DL", "DR"};
  24. int dx[] = {+1, +0, +0, -1, -1, -1, +1, +1};
  25. int dy[] = {+0, -1, +1, +0, -1, +1, -1, +1};
  26. char dc[] = {'D', 'L', 'R', 'U'};
  27.  
  28. ll n;
  29. vector<ll> v;
  30. map<ll, ll> mp;
  31. ll dp[N][1<<N]; // lst, mask
  32.  
  33. ll rec(int lst, ll mask, ll priem) {
  34. if (mask == (1<<n)-1) {
  35. mp[priem+v[lst]]++;
  36. return v[lst];
  37. }
  38.  
  39. ll &ret = dp[lst][mask];
  40. if (~ret) return ret;
  41. ret = 0;
  42.  
  43. for (int i = 0; i < n; i++) {
  44. if ((mask>>i)&1ll) continue;
  45. ret += rec(i, mask|(1<<i), priem+abs(v[i]-v[lst]));
  46. }
  47. return ret;
  48. }
  49.  
  50. void solve() {
  51. v.assign(n, 0);
  52. fin(0, n) cin >> v[i];
  53. mp.clear();
  54. for (int i = 0; i < n; i++) {
  55. memset(dp, -1, sizeof dp);
  56. rec(i, (1<<i), 2*n+v[i]);
  57. }
  58.  
  59. ll mx = -oo;
  60. for (auto [p, f] : mp) {
  61. if (p > mx) mx = p;
  62. }
  63. cout << mx << " " << mp[mx] << "\n";
  64. }
  65.  
  66. int main() {
  67. FAST;
  68. #ifndef ONLINE_JUDGE
  69. freopen("input.txt","r",stdin);
  70. freopen("output.txt","w",stdout);
  71. #endif
  72. int tt = 1; //cin >> tt;
  73. while(cin >> n){
  74. if (n == 0) break;
  75. solve();
  76. }
  77. return 0;
  78. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty