fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <string>
  9. #include <queue>
  10. #include <stack>
  11. #include <algorithm>
  12. #include <iomanip>
  13. #define dibs reserve
  14. #define OVER9000 1234567890
  15. #define patkan 9
  16. #define tisic 47
  17. #define soclose 10e-7
  18. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  19. #define chocolate win
  20. #define ff first
  21. #define ss second
  22. #define abs(x) ((x < 0)?-(x):(x))
  23. // mylittlepony
  24. using namespace std;
  25.  
  26. int main() {
  27. cin.sync_with_stdio(0);
  28. int N,K;
  29. cin >> N >> K;
  30. vector< pair<int,int> > A(N);
  31. for(int i =0; i < N; i++) {
  32. cin >> A[i].ff;
  33. A[i].ss =i+1;}
  34. sort(A.begin(),A.end());
  35. set<int> X;
  36. multiset<long long> S;
  37. X.insert(0);
  38. X.insert(N+1);
  39. long long ans =0;
  40. for(int i =0; i < N; i++) {
  41. int a =*(--X.lower_bound(A[i].ss)), b =*X.upper_bound(A[i].ss);
  42. X.insert(A[i].ss);
  43. S.insert(1LL*(b-a)*1LL*(A[i].ss-a)*(b-A[i].ss)/2);
  44. ans +=1LL*A[i].ff*1LL*(b-a)*1LL*(A[i].ss-a)*(b-A[i].ss)/2;}
  45. while(K > 0) {
  46. ans +=*S.rbegin();
  47. S.erase(--S.end());
  48. K--;}
  49. cout << ans << "\n";
  50. return 0;}
  51.  
  52. // look at my code
  53. // my code is amazing
Success #stdin #stdout 0.01s 3484KB
stdin
4 2
5 1 2 3
stdout
47