fork(1) download
  1. #pragma GCC target("avx2")
  2. #pragma GCC optimize("O3")
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. #define ll long long
  7. #define ld long double
  8. #define ull unsigned ll
  9. #define ioi exit(0);
  10.  
  11. #define inf (ll)1e18+7
  12.  
  13. #define F first
  14. #define S second
  15.  
  16. #define pb push_back
  17. #define ppb pop_back
  18.  
  19. #define lb(x) lower_bound(x)
  20. #define ub(x) upper_bound(x)
  21.  
  22. #define sz(x) x.size()
  23.  
  24. #define all(x) x.begin(),x.end()
  25.  
  26. #define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
  27.  
  28. #define int ll
  29.  
  30. const int N = 1e6 + 30;
  31.  
  32. using namespace std;
  33.  
  34. int n, s, t;
  35. pair<int, bool> a[N];
  36. ll dp[N];
  37. set<int> st;
  38.  
  39. main(){
  40.  
  41. NFS
  42.  
  43. cin >> n >> s >> t;
  44.  
  45. for(int i = 1; i <= n; i++){
  46. cin >> a[i].F;
  47. st.insert(a[i].F);
  48. }
  49.  
  50. if(s == t){
  51. cout << 0;
  52. ioi
  53. }
  54.  
  55. if(s > t){
  56. a[s].S = 1;
  57. a[t].S = 1;
  58. reverse(a + 1, a + 1 + n);
  59. bool ok = 0;
  60. for(int i = 1; i <= n; i++){
  61. if(a[i].S == 1){
  62. if(ok == 0){
  63. ok = 1;
  64. s = i;
  65. }
  66. else{
  67. t = i;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73.  
  74. if(sz(st) == 1){
  75. cout << (ll)(t - s) * a[1].F;
  76. ioi
  77. }
  78.  
  79. if(s == 1){
  80. ll ans = 0;
  81. ll mn = inf;
  82. for(int i = 1; i < t; i++){
  83. mn = min(mn, a[i].F);
  84. ans += mn;
  85. }
  86. cout << (ll)ans;
  87. ioi
  88. }
  89.  
  90. ll mn = a[s].F;
  91. for(int i = s - 1; i >= 1; i--){
  92. dp[i] += dp[i + 1] + mn;
  93. mn = min(mn, a[i].F);
  94. //cerr << dp[i] << ' ';
  95. }
  96.  
  97. ll ans = inf;
  98. bool ok = 0;
  99. for(int i = 1; i <= s; i++){
  100. ll sum = 0;
  101. mn = a[i].F;
  102. for(int j = i; j < t; j++){
  103. mn = min(mn, a[j].F);
  104. sum += mn;
  105. //cerr << sum << ' ' << mn << ' ' << i << ' ' << j << '\n';
  106. }
  107. if(ok == 0){
  108. ans = sum + dp[i];
  109. ok = 1;
  110. }
  111. ans = min(ans, sum + dp[i]);
  112. }
  113.  
  114.  
  115.  
  116. cout << (ll)ans;
  117.  
  118. ioi
  119. }
Success #stdin #stdout 0s 4812KB
stdin
Standard input is empty
stdout
Standard output is empty