1107C - Brutality - CodeForces Solution


greedy sortings two pointers *1300

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
a = list(map(int, input().split()))
s = list(input())
ans = 0
if n == k:
    print(sum(a))
else:
    i = 0
    while i < n:
        j = i + 1
        ss = [a[i]]
        while j < n and s[i] == s[j]:
            ss.append(a[j])
            j += 1
        ss.sort()
        m = sum(ss[-k:])
        ans += m
        i = j
    print(ans)

C++ Code:

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

#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define rep_(i,a,b) for(long long i=a;i>=b;i--)
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

void _print(ll t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/*--------------------------------------------------------------------------------------------------------------------------*/ 
void YES(){cout<<"YES"<<nline;}
void NO(){cout<<"NO"<<nline;}
void Neg1(){cout<<"-1"<<nline;}
/*--------------------------------------------------------------------------------------------------------------------------*/
void solve()
{
  ll n,k;
  cin>>n>>k;
  vector<ll>a;
  rep(i,0,n){
    ll x;
    cin>>x;
    a.pb(x);
  }
  string s;
  cin>>s;
  ll sum=0;
  char ch=s[0];
  vector<ll>val;
  rep(i,0,n){
    debug(val);
    if(ch==s[i]){
       val.pb(a[i]);
    }
    else{
        sort(all(val),greater<ll>());
        for(int j=0;j<val.size()&&j<k;j++){
          sum+=val[j];
        }  
       ch=s[i];
       val.clear();
       val.pb(a[i]);
    }
  }
  if(sz(val)!=0){
     sort(all(val),greater<ll>());
      for(int j=0;j<val.size()&&j<k;j++){
          sum+=val[j];
        }  
  }
  cout<<sum;
  
}
/*----------------------------------------------------------------------------------------------------------------------------*/
int main()
{
#ifndef ONLINE_JUDGE
  freopen("Error.txt", "w", stderr);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
  ll testcases=1;
 // cin>>testcases;
  while(testcases--){
    solve();
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries