779C - Dishonest Sellers - CodeForces Solution


constructive algorithms greedy sortings *1200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define Nitish                                \
            ios_base::sync_with_stdio(false); \
            cin.tie(NULL);
#define tc            \
            int t;    \
            cin >> t; \
            while (t--)

void solve()
{
            int n, k;
            cin >> n >> k;
            int a[n], b[n];
            for (int i = 0; i < n; i++)
            {
                        cin >> a[i];
            }
            for (int i = 0; i < n; i++)
            {
                        cin >> b[i];
            }
            int diff[n];
            vector<pair<int, int>> v;
            for (int i = 0; i < n; i++)
            {
                        diff[i] = a[i] - b[i];
                        v.push_back({diff[i], a[i]});
            }
sort(v.begin(), v.end(), [](const pair<int, int> &a, const pair<int, int> &b)
                 {
    if (a.first == b.first) {
        return (a.second < b.second);
    } else {
        return (a.first > b.first);
    } });
//     for(auto i:v){
//             cout<<i.first<<" "<<i.second<<endl;
//     }
    long long sum=0;
    for(int i=0;i<n-k;i++){
            if(v[i].first>0){
                        sum+=-v[i].first+v[i].second;
            }
            else{
                        sum+=v[i].second;
            }
    }
    for(int i=n-k;i<n;i++){
            sum+=v[i].second;
    }
    cout<<sum<<endl;
}

int main()
{
            Nitish
            solve();
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses