1697B - Promo - CodeForces Solution


greedy sortings *900

Please click on ads to support us..

Python Code:

      d=[0]
a,b=map(int,input().split())
l=sorted((list(map(int,input().split()))),reverse=True)
s=0
v=0
for i in l:
    v+=i
    d.append(v)
for _ in range(b):
   n,m=map(int,input().split())
   print(d[n]-d[n-m])

C++ Code:

// Problem: B. Promo
// Contest: Codeforces - Educational Codeforces Round 130 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1697/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

#define ll long long


int main()
{
   ll n,q;
   
   cin>>n>>q;
   
   vector<ll> arr(n);
  
   
   for(ll i=0;i<n;i++)
   {
   	cin>>arr[i];
   }
    sort(arr.begin(),arr.end());
    
    for(ll i=n-2;i>=0;i--)
    {
    	arr[i]+=arr[i+1];
    }
   
   while(q--)
   {
   	ll a,b;
   	cin>>a>>b;
   	
   	ll s=n-a;
   	ll e=s+b;
   	
   	if(a==b){cout<<arr[s]<<endl;}
   	else{
   	
   	
   	
   	cout<<arr[s]-arr[e]<<endl;}
   	
   	
   }
}


Comments

Submit
0 Comments
More Questions

1589C - Two Arrays
1510K - King's Task
126B - Password
462A - Appleman and Easy Task
839C - Journey
622A - Infinite Sequence
659C - Tanya and Toys
1266A - Competitive Programmer
234C - Weather
1332C - K-Complete Word
525C - Ilya and Sticks
1555C - Coin Rows
1324C - Frog Jumps
715A - Plus and Square Root
774D - Lie or Truth
1186D - Vus the Cossack and Numbers
505B - Mr Kitayuta's Colorful Graph
1324D - Pair of Topics
157B - Trace
34C - Page Numbers
279A - Point on Spiral
1294D - MEX maximizing
447A - DZY Loves Hash
23B - Party
63D - Dividing Island
1203E - Boxers
1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks