301A - Yaroslav and Sequence - CodeForces Solution


constructive algorithms *1800

Please click on ads to support us..

Python Code:

numbers = int(input())
number_list = list(map(int, input().split()))
abs_number = list()
neg_count = 0
flag = 'no'
for number in number_list:
    if number < 0:
        flag = 'yes'
        neg_count += 1
    abs_number.append(abs(number))
if flag == 'yes':
    if numbers % 2 == 1:
        print(sum(abs_number))
    elif neg_count % 2 == 1:
        abs_number.sort()
        abs_number[0] *= -1
        print(sum(abs_number))
    else:
        print(sum(abs_number))
else:
    print(sum(number_list))

C++ Code:

#include<bits/stdc++.h>

using namespace std;
#define pb push_back
#define in insert

#define int long long

void dfs(int n,int par,vector<vector<int>> &arr,vector<int> &depth,vector<int> &dis){

    for(auto child:arr[n]){
        if(child!=par){
            dis[child]=dis[n]+1;
            dfs(child,n,arr,depth,dis);
            depth[n]=max(depth[child]+1,depth[n]);
        }
    }
}


signed main(){
   int n;
   cin>>n;
   int arr[2*n-1];
   int c =0;
   int sum = 0;
   for (int i = 0; i < 2*n-1; ++i)
   {
    cin>>arr[i];
    if(arr[i]<0){
        c++;
       /* code */
    }
    arr[i]=abs(arr[i]);
    sum+=abs(arr[i]);
   }
   if(n%2){
    c=0;
    
   }
   else{
        c=c%2;
    }
    // cout<<c<<endl;
   sort(arr,arr+2*n-1);
   for (int i = 0; i < c; ++i)
   {
    sum-=2*arr[i];
       /* code */
   }
   cout<<sum<<endl;




   


}


Comments

Submit
0 Comments
More Questions

1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs