O(N) Problem Takes More Than 1s To Execute

I have one problem which is driving me crazy (from an foreign website), so could anyone please help?

An internet provider is willing to connect everyone of their users in N houses. All houses are at the same distances (1) and in each house live K different people. Your goal is to help them find an optimal place to position their station, so the length of cable is minimal and everyone is connected to the station. (don't count people who live at that position).

In the input you are given a number N (0 <= N <= 10.000) indicating the number of houses and in the next N lines a new number K (bounds are not given in the text, sorry) indicating the number of people who live in ith house (indexing starts at 0).

In the output print the total length of cable.

Input:
6
3 5 1 6 2 4
Output:
30

Explanation: If you put cable in the fourth house from left (i = 3), cable length is 3 * 3 + 5 * 2 + 1*1 + 2 * 1 + 4 * 2 = 30

I did solve this problem, using the sliding window approach, but for some inputs it takes more than 1s to execute. Can anybody help?
Here's my code (I know it is badly written from the perspective of an engineer)

#include <bits/stdc++.h>
using namespace std;

long long arr[10005];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    long long n;
    cin>>n;

    for(int i = 0; i < n; i++)
        cin>>arr[i];

    long long sum1 = 0;
    long long sum2 = 0;
    long long oldSum1 = 0;
    long long oldSum2 = 0;
    long long sum;


    for(int i = 1; i < n; i++)
    {
        sum2 += arr[i] * i;
        oldSum2 += arr[i];
    }

    sum = sum1 + sum2;

    for(int i = 1; i < n; i++)
    {
        oldSum1 += arr[i - 1];
        sum1 += oldSum1;

        sum2 -= oldSum2;
        oldSum2 -= arr[i];

        sum = min(sum, sum1 + sum2);
    }

    cout<<sum;
    return 0;
}
Comments (2)