import sys
input=lambda:sys.stdin.readline().rstrip()
n=int(input())
A=list(map(int,input().split()))
pos=[[],[]]
for i in range(n):
pos[A[i]].append(i)
k=sum(A)
ans=[0]+[float('inf') for i in range(k)]
for i in range(n-k):
for j in range(k,0,-1):
ans[j]=min(ans[j],ans[j-1]+abs(pos[0][i]-pos[1][j-1]))
print(ans[k])
#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 0x3f3f3f3f;
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
vector<int> people, chair;
for (int i = 0, cur = 0; i < n; ++i) {
cin >> cur;
if (cur) {
people.push_back(i);
} else {
chair.push_back(i);
}
}
int n1 = people.size(), n2 = chair.size();
vector<vector<int>> dp(n2 + 1, vector<int>(n1 + 1, INF));
for (int i = 0; i <= n2; ++i) {
dp[i][n1] = 0;
}
for (int i = n2 - 1; i >= 0; --i) {
for (int j = n1 - 1; j >= 0 && n1 - j <= n2 - i; --j) {
dp[i][j] = min(dp[i + 1][j + 1] + abs(chair[i] - people[j]), dp[i + 1][j]);
}
}
cout << dp[0][0];
return 0;
}
1605B - Reverse Sort | 1607C - Minimum Extraction |
1604B - XOR Specia-LIS-t | 1606B - Update Files |
1598B - Groups | 1602B - Divine Array |
1594B - Special Numbers | 1614A - Divan and a Store |
2085. Count Common Words With One Occurrence | 2089. Find Target Indices After Sorting Array |
2090. K Radius Subarray Averages | 2091. Removing Minimum and Maximum From Array |
6. Zigzag Conversion | 1612B - Special Permutation |
1481. Least Number of Unique Integers after K Removals | 1035. Uncrossed Lines |
328. Odd Even Linked List | 1219. Path with Maximum Gold |
1268. Search Suggestions System | 841. Keys and Rooms |
152. Maximum Product Subarray | 337. House Robber III |
869. Reordered Power of 2 | 1593C - Save More Mice |
1217. Minimum Cost to Move Chips to The Same Position | 347. Top K Frequent Elements |
1503. Last Moment Before All Ants Fall Out of a Plank | 430. Flatten a Multilevel Doubly Linked List |
1290. Convert Binary Number in a Linked List to Integer | 1525. Number of Good Ways to Split a String |