Advertisement
anas_harby

Untitled

Aug 4th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 1e5 + 5;
  4. int n, k;
  5. long long c[MAX], totals[MAX];
  6. bool vis[MAX];
  7.  
  8. int main() {
  9.     ios::sync_with_stdio(false);
  10.     cin >> n >> k;
  11.     for (int i = 0; i < n; i++)
  12.         cin >> c[i];
  13.     long long res = 0, total = 0;
  14.     for (int i = 0; i < n; i++) {
  15.         total += c[i];
  16.         res += c[i] * c[(i + 1) % n];
  17.     }
  18.     for (int i = 0; i < k; i++) {
  19.         long long cap; cin >> cap;
  20.         cap--;
  21.         vis[cap] = true;
  22.         total -= c[cap];
  23.         long long cur = total;
  24.         if (!vis[(cap + 1) % n])
  25.             cur -= c[(cap + 1) % n];
  26.         if (!vis[(cap > 0 ? cap - 1 : n - 1)])
  27.             cur -= c[(cap > 0 ? cap - 1 : n - 1)];
  28.         res += cur * c[cap];
  29.     }
  30.     cout << res;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement