fork download
  1. #include <bits/stdc++.h>
  2. #define endl '\n'
  3. using namespace std;
  4. using ll = long long;
  5.  
  6. int n;
  7. ll ans;
  8. ll a[1000001];
  9. ll pre[1000001];
  10.  
  11. int main(){
  12. ios_base::sync_with_stdio(false);
  13. cin.tie(NULL);
  14. //freopen("MULARR.inp","r",stdin);
  15. //freopen("MULARR.out","w",stdout);
  16. cin >> n;
  17. for(int i = 1; i <= n; i++){
  18. cin >> a[i];
  19. pre[i] = pre[i-1] + a[i];
  20. }
  21. ans = 0;
  22. for(int i = 1; i <= n - 1; i++){
  23. ans += a[i] * (pre[n] - pre[i]);
  24. }
  25. cout << ans;
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5468KB
stdin
9 4 3 2
stdout
26