Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- void swap(int *array, int a, int b){
- int tmp = array[b];
- array[b] = array[a];
- array[a] = tmp;
- }
- void InsertionSort(int *array, int n){
- for (int i = 0; i < n; ++i){
- int j = i - 1;
- while (j >= 0 and array[j] < array[j+1]){
- swap(array, j, (j+1));
- j--;
- }
- }
- }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int N;
- fin >> N;
- int k;
- string str;
- int one_plus_zero[N];
- int zero_plus_one[N];
- for(int i = 0 ; i < N; ++i){
- one_plus_zero[i]=0;
- zero_plus_one[i]=0;
- }
- int tlen1=0;
- int tlen2=0;
- int sum1=0, sum0 = 0;
- int answer=0;
- for(int i = 0; i < N; ++i){
- fin >> k >> str;
- if (str[0] == '1' and str[k-1] == '0'){
- one_plus_zero[tlen1]=k;
- tlen1++;
- }
- else if(str[0] == '0' and str[k-1] == '1'){
- zero_plus_one[tlen2]=k;
- tlen2++;
- }
- else if (str[0] == '1' and str[k-1] == '1') {
- sum1 += k;
- }
- else
- sum0 += k;
- }
- if (tlen1 == 0 && tlen2 == 0){
- answer = sum1 > sum0 ? sum1 : sum0;
- std::cout << answer << std::endl;
- return 0;
- }
- InsertionSort(one_plus_zero, tlen1);
- InsertionSort(zero_plus_one, tlen2);
- answer+=sum1;
- if(tlen1>tlen2){
- int index = 0;
- while(tlen2 != 0){
- answer+=one_plus_zero[index]+zero_plus_one[index];
- tlen2--;
- index++;
- }
- answer+=one_plus_zero[index];
- }
- else if(tlen1<tlen2){
- int index = 0;
- while(tlen1 != 0){
- answer+=one_plus_zero[index]+zero_plus_one[index];
- tlen1--;
- index++;
- }
- answer+=zero_plus_one[index];
- }
- else{
- for(int i = 0; i < tlen1; ++i){
- answer+=one_plus_zero[i]+zero_plus_one[i];
- }
- }
- fout << answer;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment