fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <functional>
  7. #include <vector>
  8. #include <queue>
  9. #include <stack>
  10. #include <map>
  11. #include <set>
  12. #include <deque>
  13. #include <string>
  14. #include <cassert>
  15.  
  16. using namespace std;
  17.  
  18. typedef long long llint;
  19.  
  20. const int INF = 0x3f3f3f3f;
  21. const llint INFLL = 0x3f3f3f3f3f3f3f3fLL;
  22.  
  23. void print() { cout << "\n"; }
  24.  
  25. template <typename...T, typename X>
  26. void print(X&& x, T... args) { cout << x << " "; print(args...); }
  27.  
  28. int input() { return 0; }
  29.  
  30. template <typename...T, typename X>
  31. int input(X& x, T&... args) {
  32. if (!(cin >> x)) return 0;
  33. return input(args...) + 1;
  34. }
  35.  
  36. const int SIZE = 312345;
  37.  
  38. int n, k;
  39. vector<llint> ns;
  40.  
  41. int main() {
  42. input(n, k);
  43.  
  44. ns = vector<llint>(n + 1, 0);
  45. for (int i = 1; i <= n; i++) {
  46. scanf("%lld", &ns[i]);
  47. }
  48.  
  49. for (int i = n - 1; i > 0; i--) {
  50. ns[i] += ns[i + 1];
  51. }
  52.  
  53. sort(ns.begin() + 2, ns.end(), greater<llint>());
  54.  
  55. llint res = ns[1];
  56. for (int i = 2; i <= k; i++) {
  57. res += ns[i];
  58. }
  59. print(res);
  60.  
  61. return 0;
  62. }
  63.  
  64. /*
  65.  
  66. ^^^TEST^^^
  67. 2 2
  68. 1 1
  69. -----
  70. 3
  71. $$$TEST$$$
  72.  
  73. ^^^TEST^^^
  74. 5 2
  75. -1 -2 5 -4 8
  76. -----
  77. 15
  78. $$$TEST$$$
  79.  
  80. ^^^TEST^^^
  81. 7 6
  82. -3 0 -1 -2 -2 -4 -1
  83. -----
  84. -45
  85. $$$TEST$$$
  86.  
  87. ^^^TEST^^^
  88. 4 1
  89. 3 -1 6 0
  90. -----
  91. 8
  92. $$$TEST$$$
  93. */
  94.  
Success #stdin #stdout 0.01s 5312KB
stdin
5 2
-1 -2 5 -4 8
stdout
15