Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Вася и Суперкомпьютер
- #include <iostream>
- #include <fstream>
- void swap(int array[], int i, int j) {
- int tmp = array[i];
- array[i] = array[j];
- array[j] = tmp;
- }
- void sort(int array[], int len) {
- int needIteration = 1;
- while (needIteration) {
- needIteration = 0;
- for (int i = 1; i < len; ++i) {
- if (array[i] > array[i-1]) {
- swap(array, i, i - 1);
- needIteration = 1;
- }
- }
- len--;
- }
- }
- int main() {
- std::ifstream fin("input.txt");
- std::ofstream fout("output.txt");
- int N, length;
- fin >> N;
- int one[N], zero[N];
- for (int i = 0; i < N; ++i) {
- one[i] = 0;
- zero[i] = 0;
- }
- int one_index = 0, zero_index = 0;
- int only_one = 0, only_zero = 0;
- std::string line;
- for (int i = 0; i < N; ++i) {
- fin >> length;
- fin >> line;
- if (line[0] == '1' && line[length - 1] == '0')
- one[one_index++] = length;
- else if (line[0] == '0' && line[length - 1] == '1')
- zero[zero_index++] = length;
- else if (line[0] == '1' && line[length - 1] == '1')
- only_one += length;
- else
- only_zero += length;
- }
- int result = 0;
- if (one_index == 0 && zero_index == 0) {
- result = only_one > only_zero ? only_one : only_zero;
- fout << result;
- return 0;
- }
- result += only_one + only_zero;
- sort(one, one_index);
- sort(zero, zero_index);
- if (one_index > zero_index) {
- int ind = 0;
- while (zero_index > 0) {
- result += one[ind] + zero[ind++];
- zero_index--;
- }
- result += one[ind];
- }
- else if (zero_index > one_index) {
- int ind = 0;
- while (one_index > 0) {
- result += zero[ind] + one[ind++];
- one_index--;
- }
- result += zero[ind];
- }
- else {
- for (int i = 0; i < one_index; ++i)
- result += one[i] + zero[i];
- }
- fout << result;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment