Pagini recente » Cod sursa (job #2630357) | Cod sursa (job #333178) | Cod sursa (job #580594) | Cod sursa (job #295811) | Cod sursa (job #2725059)
#include <fstream>
#include <vector>
#include <algorithm>
#define MIN(a, b) (((a)<(b))?(a):(b))
#define MAX(a, b) (((a)>(b))?(a):(b))
std::ifstream fin("barman.in");
std::ofstream fout("barman.out");
const int N = 605;
std::vector<int>match;
int ok[N], v[N], mn = 1000000000;
void shift() {
int f = match[0];
for(int i=1;i<match.size();i++) match[i-1] = match[i];
match[match.size()-1] = f;
}
int main() {
int n;
fin>>n;
for(int i=0;i<n;i++) fin>>v[i], match.push_back(v[i]);
std::sort(match.begin(), match.end());
for(int rot = 1; rot<=n; rot++) {
int ans = 0;
for(int i=0;i<n;i++) ok[i] = (v[i]==match[i]);
for(int i=0;i<n;i++) if(!(v[i]==match[i]))
for(int j=0;j<n;j++) if(!ok[j] and v[i]==match[j]) {
ok[j] = 1, ans+=20+MAX(i-j, j-i);
break;
}
mn = MIN(ans, mn);
shift();
}
fout<<mn;
}