Codeforces Round 169 (Div. 2) 276C - Little Girl and Maximum Sum Solution
Problem Link : https://codeforces.com/problemset/problem/276/C
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- const int mx = 2e5 + 9 ;
- long long cnt [mx] ;
- long long a[mx] ;
- int32_t main(){
- int n , q ;
- cin >> n >> q ;
- for ( int i = 1 ; i <= n ; i++ ) {
- cin >> a[i] ;
- }
- int l , r ;
- for ( int i = 1 ; i <= q ; i++ ) {
- cin >> l >> r ;
- cnt[l]++ ;
- cnt[r+1]-- ;
- }
- cnt [ 0 ] = 0 ;
- for ( int i = 1 ; i <= n ; i++ ){
- cnt [i] += cnt[i-1] ;
- }
- sort ( a + 1 , a + n + 1) ;
- sort ( cnt + 1 , cnt + n + 1) ;
- long long ans = 0 ;
- for ( int i = 1 ; i <= n ; i++ ){
- ans += ( a[i] * cnt[i] ) ;
- }
- cout << ans << endl;
- return 0 ;
- }
No comments