Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. /**
  2. * code generated by JHelper
  3. * More info: https://github.com/AlexeyDmitriev/JHelper
  4. * @author JustLive
  5. */
  6.  
  7. #pragma GCC optimize("Ofast")
  8. #pragma GCC optimize("unroll-loops")
  9. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  10. #define _CRT_SECURE_NO_WARNINGS
  11. #include <bits/stdc++.h>
  12. #define mp make_pair
  13. #define mt make_tuple
  14. #define fi first
  15. #define se second
  16. #define pb push_back
  17. #define all(x) (x).begin(), (x).end()
  18. #define rall(x) (x).rbegin(), (x).rend()
  19. #define forn(i, n) for (int i = 0; i < (int)(n); ++i)
  20. #define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
  21. #define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
  22. #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
  23. #define forr(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
  24.  
  25. #ifdef LOCAL_SYSTEM
  26. #define DEBUG(x) cerr << #x << ": " << (x) << endl;
  27. #define DEBUGARR(a, n) cerr << #a << ": "; print_array(cerr, a, n); cerr << endl;
  28. #else
  29. #define DEBUG(x)
  30. #define DEBUGARR(a, n)
  31. #endif
  32.  
  33. using namespace std;
  34.  
  35. template<typename T> void print_sequential_container(ostream& stream, T& c) {
  36. stream << "[";
  37. for (auto it = c.begin(); it != c.end(); ++it) {
  38. stream << *it;
  39. if (it != --c.end()) {
  40. stream << ", ";
  41. }
  42. }
  43. stream << "]";
  44. }
  45.  
  46. template<typename T> void print_array(ostream& stream, T a[], int n) {
  47. stream << "[";
  48. for (int i = 0; i < n; i++) {
  49. stream << a[i];
  50. if (i != n - 1) {
  51. stream << ", ";
  52. }
  53. }
  54. stream << "]";
  55. }
  56.  
  57. template<typename T> void print_set(ostream& stream, T& c) {
  58. int cnt = 0;
  59. stream << "{";
  60. for (auto it = c.begin(); it != c.end(); ++it, ++cnt) {
  61. stream << *it;
  62. if (cnt + 1 < c.size()) {
  63. stream << ", ";
  64. }
  65. }
  66. stream << "}";
  67. }
  68.  
  69. template<typename T> void print_map(ostream& stream, T& c) {
  70. int cnt = 0;
  71. stream << "{";
  72. for (auto it = c.begin(); it != c.end(); ++it, ++cnt) {
  73. stream << it->fi << ": " << it->se;
  74. if (cnt + 1 < c.size()) {
  75. stream << ", ";
  76. }
  77. }
  78. stream << "}";
  79. }
  80.  
  81. template<typename T> ostream& operator << (ostream& stream, vector<T>& c) {
  82. print_sequential_container(stream, c);
  83. return stream;
  84. }
  85.  
  86. template<typename T> ostream& operator << (ostream& stream, list<T>& c) {
  87. print_sequential_container(stream, c);
  88. return stream;
  89. }
  90.  
  91. template<typename T> ostream& operator << (ostream& stream, forward_list<T>& c) {
  92. print_sequential_container(stream, c);
  93. return stream;
  94. }
  95.  
  96. template<typename T, size_t N> ostream& operator << (ostream& stream, array<T, N>& c) {
  97. print_sequential_container(stream, c);
  98. return stream;
  99. }
  100.  
  101. template<typename T> ostream& operator << (ostream& stream, deque<T>& c) {
  102. print_sequential_container(stream, c);
  103. return stream;
  104. }
  105.  
  106. template<typename T> ostream& operator << (ostream& stream, stack<T>& c) {
  107. vector<T> v;
  108. while (not c.empty()) {
  109. v.pb(c.top());
  110. c.pop();
  111. }
  112. for (int i = ((int)v.size()) - 1; i >= 0; i--) {
  113. c.push(v[i]);
  114. }
  115. print_sequential_container(stream, v);
  116. return stream;
  117. }
  118.  
  119. template<typename T> ostream& operator << (ostream& stream, queue<T>& c) {
  120. vector<T> v;
  121. while (not c.empty()) {
  122. v.pb(c.front());
  123. c.pop();
  124. }
  125. for (T e: v) {
  126. c.push(e);
  127. }
  128. print_sequential_container(stream, v);
  129. return stream;
  130. }
  131.  
  132. template<typename T> ostream& operator << (ostream& stream, priority_queue<T>& c) {
  133. vector<T> v;
  134. while (not c.empty()) {
  135. v.pb(c.top());
  136. c.pop();
  137. }
  138. for (T e: v) {
  139. c.push(e);
  140. }
  141. print_sequential_container(stream, v);
  142. return stream;
  143. }
  144.  
  145. template<typename T> ostream& operator << (ostream& stream, set<T>& c) {
  146. print_set(stream, c);
  147. return stream;
  148. }
  149.  
  150. template<typename T> ostream& operator << (ostream& stream, unordered_set<T>& c) {
  151. print_set(stream, c);
  152. return stream;
  153. }
  154.  
  155. template<typename T> ostream& operator << (ostream& stream, multiset<T>& c) {
  156. print_set(stream, c);
  157. return stream;
  158. }
  159.  
  160. template<typename T> ostream& operator << (ostream& stream, unordered_multiset<T>& c) {
  161. print_set(stream, c);
  162. return stream;
  163. }
  164.  
  165. template<typename T1, typename T2> ostream& operator << (ostream& stream, map<T1, T2>& c) {
  166. print_map(stream, c);
  167. return stream;
  168. }
  169.  
  170. template<typename T1, typename T2> ostream& operator << (ostream& stream, unordered_map<T1, T2>& c) {
  171. print_map(stream, c);
  172. return stream;
  173. }
  174.  
  175. template<typename T1, typename T2> ostream& operator << (ostream& stream, multimap<T1, T2>& c) {
  176. print_map(stream, c);
  177. return stream;
  178. }
  179.  
  180. template<typename T1, typename T2> ostream& operator << (ostream& stream, unordered_multimap<T1, T2>& c) {
  181. print_map(stream, c);
  182. return stream;
  183. }
  184.  
  185. template<typename T1, typename T2> ostream& operator << (ostream& stream, pair<T1, T2>& p) {
  186. stream << "(" << p.fi << ", " << p.se << ")";
  187. return stream;
  188. }
  189.  
  190. template<typename T>
  191. size_t make_hash(const T& v) {
  192. return std::hash<T>()(v);
  193. }
  194.  
  195. size_t hash_combine(const size_t& h, const size_t& v) {
  196. return h ^ (v + 0x9e3779b9 + (h << 6) + (h >> 2));
  197. }
  198.  
  199. namespace std {
  200. template <typename T1, typename T2>
  201. struct hash<pair<T1, T2>> {
  202. public:
  203. size_t operator()(const pair<T1, T2>& p) const {
  204. return hash_combine(make_hash(p.fi), make_hash(p.se));
  205. }
  206. };
  207. }
  208.  
  209. template<typename T>
  210. struct hash_container
  211. {
  212. size_t operator()(const T& c) const {
  213. size_t h = 0;
  214. for(const auto& e: c) {
  215. h = hash_combine(h, make_hash(e));
  216. }
  217. return h;
  218. }
  219. };
  220.  
  221. typedef pair<int, int> pii;
  222. typedef vector<int> vi;
  223. typedef vector<pii> vpii;
  224. typedef vector<vi> vvi;
  225. typedef long long ll;
  226. typedef unsigned long long ull;
  227. typedef unsigned int uint;
  228. typedef vector<ll> vll;
  229. typedef vector<vll> vvll;
  230. typedef pair<ll, ll> pll;
  231. typedef long double ld;
  232.  
  233. template<typename T> bool amin(T &a, T b) { return a > b ? (a = b, true) : false; }
  234. template<typename T> bool amax(T &a, T b) { return a < b ? (a = b, true) : false; }
  235.  
  236. auto seed() {
  237. return chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
  238. }
  239.  
  240. const vi some_primes = {1000000007, 1000000411, 1000001501, 1000001927, 1000002637,
  241. 1000003163, 1000003651, 1000003769, 1000003931, 1000004099};
  242. const int mod = 1000000007;
  243. const int inf = 1000000000;
  244. const double eps = 1e-9;
  245. const double pi = atan2(-1, 0);
  246. const ll infll = (ll)inf * (ll)inf;
  247. const int maxn = 100000;
  248. const int L = 20;
  249.  
  250. int n, c;
  251. vi a;
  252. vvi r;
  253.  
  254. void build() {
  255. forn(j, L) {
  256. for (int i = 0; i + (1 << j) - 1 < n; i++) {
  257. if (j == 0) r[i][j] = a[i];
  258. else {
  259. r[i][j] = min(r[i][j - 1], r[i + (1 << (j - 1))][j - 1]);
  260. }
  261. }
  262. }
  263. }
  264.  
  265. int get(int i, int j) {
  266. int d = (int)log2(j - i + 1);
  267. return min(r[i][d], r[j - (1 << d) + 1][d]);
  268. }
  269.  
  270. class TaskE {
  271. public:
  272. void solve(istream& in, ostream& out) {
  273. in >> n >> c;
  274. a.clear();
  275. r.clear();
  276. a.resize(n);
  277. r.resize(n, vi(L));
  278. ll total = 0;
  279. vll psum(n + 1);
  280. psum[0] = 0;
  281. forn(i, n) {
  282. in >> a[i];
  283. total += a[i];
  284. psum[i + 1] = total;
  285. }
  286. if (c > n) {
  287. out << total;
  288. return;
  289. }
  290. build();
  291. ll res = total;
  292. vll dp(n + 1, infll);
  293. dp[0] = 0;
  294. forn(i, c) {
  295. dp[i + 1] = psum[i + 1];
  296. }
  297. fore(i, c, n) {
  298. dp[i] = min(
  299. dp[i - 1] + a[i - 1],
  300. dp[i - c] + (psum[i] - psum[i - c]) - get(i - c, i - 1)
  301. );
  302. }
  303. out << dp[n];
  304. }
  305. };
  306.  
  307.  
  308. int main() {
  309. ios_base::sync_with_stdio(false);
  310. cin.tie(nullptr);
  311. TaskE solver;
  312. std::istream& in(std::cin);
  313. std::ostream& out(std::cout);
  314. solver.solve(in, out);
  315. return 0;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement