808E - Selling Souvenirs - CodeForces Solution


binary search dp greedy ternary search *2300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5,M=3e5+5;
struct node{
	int num,x,y;
}dis[M]; 
int a[3][N],num[3];
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
bool cmp(int x,int y){
	return x>y; 
}
int ans[N];
signed main(){
	int n=read(),m=read();
	for(int i=1;i<=n;++i){
		int w=read()-1,c=read();
		a[w][++num[w]]=c;
	}
	for(int i=0;i<3;++i){
		sort(a[i]+1,a[i]+1+num[i],cmp);
	}
	for(int i=1;i<=m;++i){
		dis[i]=dis[i-1];
		if(i==1){
			if(a[0][dis[i-1].x+1]){
				dis[i].num=dis[i-1].num+a[0][dis[i-1].x+1];
				dis[i].x=dis[i-1].x+1;
				dis[i].y=dis[i-1].y;
			}
		} 
		else{
			 if(dis[i-1].num+a[0][dis[i-1].x+1]<dis[i-2].num+a[1][dis[i-2].y+1]&&a[1][dis[i-2].y+1]){
			 dis[i].num=dis[i-2].num+a[1][dis[i-2].y+1];
				dis[i].x=dis[i-2].x;
				dis[i].y=dis[i-2].y+1;
			 }
			 else if(a[0][dis[i-1].x+1]){
				dis[i].num=dis[i-1].num+a[0][dis[i-1].x+1];
				dis[i].x=dis[i-1].x+1;
				dis[i].y=dis[i-1].y;
			}
		}
	}
	for(int i=1;i<=num[2];++i){
		ans[i]=ans[i-1]+a[2][i];
	}
	for(int i=1;i<=min(m/3,num[2]);++i){
		dis[m].num=max(dis[m].num,dis[m-i*3].num+ans[i]);
	} 
	printf("%lld\n",dis[m].num);
	return 0;
}


Comments

Submit
0 Comments
More Questions

1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down