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