810B - Summer sell-off - CodeForces Solution


greedy sortings *1300

Please click on ads to support us..

Python Code:

n, f = list(map(int, input().split()))
store = []

class day:
    def __init__(self, K_i, L_i):
        self.client = L_i
        self.products = K_i


for i in range(n):
    K_i, L_i = list(map(int, input().split()))
    store.append(day(K_i, L_i))

store = sorted(store, key=lambda x : min(2 * x.products, x.client) - min(x.products, x.client), reverse= True)
    

for i in range(f):
    store[i].products = store[i].products * 2

sum = 0
for i in store:
    if i.products > i.client:
        sum += i.client
    else:
        sum += i.products
print(sum)

C++ Code:

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<set>
#include <numeric>
using namespace std;
const long double pi = 2 * acos(0.0);
#define all(v) ((v).begin()),((v).end())
#define sz(v) (int)(v.size())
#define p_b   push_back
#define fi    first
#define se    second
#define ma    make_pair
#define oo    1e8
typedef long long ll;
typedef vector< vector<int> >vv;
typedef vector<ll>vi;
typedef vector<bool> vb;
#include<stdio.h>
int x_ax[] = {0,0,1,-1,-1,-1,1,1},
y_ax[] = {1,-1,0,0,1,-1,1,-1};
bool valid(int r, int c, int n, int m) {
    return !(r < 0 || r >= n || c < 0 || c >= m);
}
int gcd(int a, int b) {
    if (!b)return a;
    return gcd(b, a % b);
}


int main() {
ll n,f,ans=0;cin>>n>>f;
vector<ll>p(n);
for(ll x=0,fr,sc;x<n;x++){cin>>fr>>sc;
ans+=min(fr,sc);
if(fr<sc&&(2*fr)<=sc)p[x]=(2*fr)-fr;
else if(fr<sc&&(2*fr)>sc)p[x]=sc-fr;
else p[x]=0;
}
sort(all(p),greater<ll >());
for(ll x=0;x<f;x++)ans+=p[x];
cout<<ans;
}


Comments

Submit
0 Comments
More Questions

1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number