fork download
  1. /*
  2. " وَكَانَ فَضْلُ اللَّهِ عَلَيْكَ عَظِيمًا "
  3.  
  4. ╔░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╗
  5. ║ ║
  6. ║ ███╗ ███╗ ██████╗ ██╗ ██╗ █████╗ ███╗ ███╗ ███████╗ ██████╗ ║
  7. ║ ████╗ ████║██╔═══██╗██║ ██║ ██╔══██╗ ████╗ ████║ ██╔════╝ ██╔══██╗ ║
  8. ║ ██╔████╔██║██║ ██║███████║ ███████║ ██╔████╔██║ █████╗ ██║ ██║ ║
  9. ║ ██║╚██╔╝██║██║ ██║██╔══██║ ██╔══██║ ██║╚██╔╝██║ ██╔══╝ ██║ ██║ ║
  10. ║ ██║ ╚═╝ ██║╚██████╔╝██║ ██║ ██║ ██║ ██║ ╚═╝ ██║ ███████╗ ██████╔╝ ║
  11. ║ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚═════╝ ║
  12. ║ ║
  13. ║ ██╗ ██╗ ██████╗ ███████╗ ███████╗ █████╗ ███╗ ███╗ ║
  14. ║ ██║ ██║ ██╔═══██╗ ██╔════╝ ██╔════╝ ██╔══██╗ ████╗ ████║ ║
  15. ║ ███████║ ██║ ██║ ███████╗ ███████╗ ███████║ ██╔████╔██║ ║
  16. ║ ██╔══██║ ██║ ██║ ╚════██║ ╚════██║ ██╔══██║ ██║╚██╔╝██║ ║
  17. ║ ██║ ██║ ╚██████╔╝ ███████║ ███████║ ██║ ██║ ██║ ╚═╝ ██║ ║
  18. ║ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ║
  19. ║ ║
  20. ╚░░░░░░░░░░░░░░░░░░░░░░░ MOHAMED HOSSAM | SOFTWARE ENGINEER ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╝
  21. */
  22.  
  23. #include"bits/stdc++.h"
  24. using namespace std;
  25. #define el '\n'
  26. #define ll long long
  27. #define int long long
  28. #define yes cout << "YES\n"
  29. #define no cout << "NO\n"
  30. #define all(x) (x).begin(), (x).end()
  31. #define rall(x) x.rbegin(), x.rend()
  32. #define sz(x) ((int)(x).size())
  33.  
  34. template<class T, class U>
  35. T GCD(T a, U b) { return (!b ? a : GCD(b, a % b)); }
  36.  
  37. template<class T, class U>
  38. T LCM(T a, U b) { return ((a / GCD(a, b)) * b); }
  39.  
  40. template<class Ta, class Tb>
  41. bool chmax(Ta &a, Tb b) {
  42. if (a >= b) return false;
  43. a = b;
  44. return true;
  45. }
  46.  
  47. template<class Ta, class Tb>
  48. bool chmin(Ta &a, Tb b) {
  49. if (a <= b) return false;
  50. a = b;
  51. return true;
  52. }
  53.  
  54. constexpr ll MAX = 300000, MOD = 998244353, MAXN = 2e5 + 10, inf = 1e18;
  55. using vi = vector<int>;
  56. int dx[] = {-1, 1, 0, 0};
  57. int dy[] = {0, 0, -1, 1};
  58. inline void free_palestine() {
  59. ios_base::sync_with_stdio(false);
  60. cin.tie(NULL);
  61. cout.tie(NULL);
  62.  
  63. #ifndef ONLINE_JUDGE
  64. freopen("inputs.txt", "r", stdin);
  65. freopen("output.txt", "w", stdout);
  66. #endif
  67. }
  68.  
  69. template<typename T>
  70. istream &operator>>(istream &in, vector<T> &v) {
  71. for (auto &x: v) in >> x;
  72. return in;
  73. }
  74.  
  75. template<typename T>
  76. ostream &operator<<(ostream &out, const vector<T> &v) {
  77. for (const auto &x: v) out << x << ' ';
  78. return out;
  79. }
  80.  
  81. void solve(int tc) {
  82. int n;cin>>n;
  83. vi a(n), pref(n+1), pref_sq(n + 1);
  84. for (int i=0;i<n;i++) {
  85. cin>>a[i];
  86. pref[i+1] = pref[i]+a[i];
  87. pref_sq[i+1] = pref_sq[i] + a[i] * a[i];
  88. }
  89.  
  90. int ans = 0;
  91. for (int i=0;i<n;i++) {
  92. int cnt_a = n-1-i,
  93. sum1 = pref[n] - pref[i + 1],
  94. sum2 = pref_sq[n] - pref_sq[i + 1];
  95.  
  96. ans += (a[i] * a[i] *cnt_a) + (2 * a[i] * sum1)+sum2;
  97. }
  98.  
  99. cout<<ans<<el;
  100. }
  101.  
  102. int32_t main() {
  103. free_palestine();
  104. // freopen("collectingofficer.in", "r", stdin);// freopen("palindrome.out", "w", stdout);
  105. int tc = 1;
  106.  
  107. // cin >> tc;
  108. for (int i = 1; i <= tc; ++i) {
  109. solve(i);
  110. }
  111.  
  112. /*
  113.   ⢸⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⡷⠀⠀
  114.   ⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠢⣀⠀⠀
  115.   ⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇ Did you get accepted, son?
  116.   ⢸⠀⠀⠀⠀ ⠖⠒⠒⠒⢤⠀⠀⠀⠀⠀⡇⠀⠀
  117.   ⢸⠀⠀⣀⢤⣼⣀⡠⠤⠤⠼⠤⡄⠀⠀⡇⠀
  118.   ⢸⠀⠀⠑⡤⠤⡒⠒⠒⡊⠙⡏⠀⢀⠀⡇⠀
  119.   ⢸⠀⠀⠀⠇⠀⣀⣀⣀⣀⢀⠧⠟⠁ ⡇
  120.   ⢸⠀⠀⠀⠸⣀⠀⠀⠈⢉⠟⠓⠀⠀⠀⢀⡇⠀⠀
  121.   ⢸⠀⠀⠀⠀⠈⢱⡖⠋⠁⠀⠀⠀⠀⠀⡇⠀⡇
  122.   ⢸⠀⠀⠀⠀⣠⢺⠧⢄⣀⠀⠀⣀⣀⠀⡇⠀⡇
  123.   ⢸⠀⠀⠀⣠⠃⢸⠀⠀⠈⠉⡽⠿⠯⡆⡇
  124.   ⢸⠀⠀⣰⠁⠀⢸⠀⠀⠀⠀⠉⠉⠉⠀⡇⠀⡇
  125.   ⢸⠀⠀⠣⠀⠀⢸⢄⠀⠀⠀⠀⠀⠀⠀⡇⠀⡇⠀
  126.   ⢸⠀⠀⠀⠀⠀⢸⠀⢇⠀⠀⠀⠀⠀⠀⡇⠀⡇
  127.   ⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉
  128.   */
  129. }
  130.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
0