Skip to content

USACO 2016 February Contest Bronze Division - Circular Barn#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

As it is customary for many bronze problems, we can use brute force all variants to open a door and choose the optimal approach.

Source codes#

The source codes in C++ and Python can be seen below.

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

int main()
{
    ifstream cin("cbarn.in");
    ofstream cout("cbarn.out");

    int n;
    cin >> n;

    vector<int> v(n+1);
    for(int i = 1; i <= n; i++)
        cin >> v[i];

    int ans = 1000000000;

    for(int start = 1; start <= n; start++)
    {
        int dist = 0;
        for(int d = 1; d <= (n-1); d++)
        {
            int tR = start + d;
            if(tR > n)
                tR -= n;
            dist += d * v[tR];
        }
        ans = min(ans, dist);
    }

    cout << ans;
    return 0;
}
with open("cbarn.in", "r") as fin:
    n = int(fin.readline().strip())
    v = [0] + list(map(int, fin.read().split()))

ans = 1000000000
for start in range(1, n + 1):
    dist = 0
    for d in range(1, n):
        tR = start + d
        if tR > n:
            tR -= n
        dist += d * v[tR]
    ans = min(ans, dist)

with open("cbarn.out", "w") as fout:
    fout.write(str(ans))