Circular Barn

Can anyone give me an analysis on this problem ?

1 Like

Just calculate the answer for all N starting positions and then take the minimum of them. , just one catch is that we can only travel in clockwise direction so we’ll have to take modulo N. Here’s the code for the same

CODE
#include "bits/stdc++.h"
using namespace std ;
int main(){
  int n ;
  cin >> n ;
  vector<int>a(n) ;
  for(int &x:a)
    cin>>x ;
  int ans =1e9 ;
  for(int i=0;i<n;i++){
    int c=0 ;
    for(int j=0;j<n;j++)
      c+=(j-i+n)%n*a[j] ;
    ans=min(ans,c) ;
  }
  cout<<ans ;  
}

1 Like