fork download
  1. #include <bits/stdc++.h>
  2. #pragma GCC optimize("O3,unroll-loops")
  3. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  4. #define ll long long
  5. #define all(data) data.begin() , data.end()
  6.  
  7. using namespace std;
  8.  
  9. const int N = 1e6;
  10. const ll inf32 = 1e9 + 7;
  11. ll n , m , k , sum , res1 , res2;
  12. ll w[N + 5] , prea[N + 5] , preb[N + 5];
  13. ll diff[N + 5] , spe[N + 5];
  14.  
  15. void init()
  16. {
  17. ios_base::sync_with_stdio(0);
  18. cin.tie(0);
  19. cout.tie(0);
  20. #define TASK "Sheep"
  21. if(fopen(TASK".INP" , "r"))
  22. {
  23. freopen(TASK".INP" , "r" , stdin);
  24. freopen(TASK".OUT" , "w" , stdout);
  25. }
  26. }
  27.  
  28. void process()
  29. {
  30. for(int i = 1 ; i <= n ; i ++)
  31. {
  32. prea[i] = prea[i - 1] + (spe[i] ? w[i] : 0);
  33. preb[i] = preb[i - 1] + (spe[i] ? 0 : w[i]);
  34. }
  35. for(int i = 1 ; i <= n ; i ++)
  36. diff[i] = prea[i] - preb[i];
  37. ll tmp = -inf32;
  38. for(int i = 0 ; i <= n ; i ++)
  39. {
  40. tmp = max(tmp , diff[i]);
  41. res1 = max(res1 , tmp - diff[i] + prea[n]);
  42. }
  43. tmp = inf32;
  44. for(int i = 0 ; i <= n ; i ++)
  45. {
  46. tmp = min(tmp , diff[i]);
  47. res2 = max(res2 , diff[i] - tmp + preb[n]);
  48. }
  49. cout << sum - max(res1 , res2);
  50. }
  51.  
  52. void read()
  53. {
  54. cin >> n >> k;
  55. for(int i = 1 ; i <= n ; i ++) cin >> w[i] , sum += w[i];
  56. for(int i = 1 ; i <= k ; i ++)
  57. {
  58. int vl; cin >> vl;
  59. spe[vl] = true;
  60. }
  61. }
  62.  
  63. int main()
  64. {
  65. init();
  66. int test_case = 1;
  67. //cin >> test_case;
  68. while(test_case --)
  69. {
  70. read();
  71. process();
  72. }
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty