Submission #155386

#TimeUsernameProblemLanguageResultExecution timeMemory
155386TadijaSebezToilets (JOI16_toilets)C++11
100 / 100
39 ms4844 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=200050;
char s[N];
int main()
{
	ll n;
	int m;
	scanf("%lld %i",&n,&m);
	vector<pair<ll,pair<ll,ll>>> v;
	ll all=0;
	for(int i=1;i<=m;i++)
	{
		scanf("%s",s+1);
		int len=strlen(s+1);
		ll k;
		scanf("%lld",&k);
		ll mx=0,balance=0;
		for(int j=len;j>=1;j--)
		{
			balance+=(s[j]=='F'?-1:1);
			mx=max(mx,balance);
			if(s[j]=='F') all-=k;
			else all+=k;
		}
		v.pb({k,{mx,balance}});
		//printf("%lld %lld\n",mx,balance);
	}
	if(all>0) return 0*printf("-1\n");
	ll sum=0,ans=0;
	while(v.size())
	{
		ll k,mx,b;
		//tie(k,mx,b)=v.back();
		k=v.back().first;
		mx=v.back().second.first;
		b=v.back().second.second;
		v.pop_back();
        if(b>0) ans=max(ans,mx+sum+b*(k-1));
        else ans=max(ans,mx+sum);
        sum+=k*b;
	}
	printf("%lld\n",max((ll)0,ans-1));
	return 0;
}

Compilation message (stderr)

toilets.cpp: In function 'int main()':
toilets.cpp:11:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld %i",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~~~
toilets.cpp:16:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s",s+1);
   ~~~~~^~~~~~~~~~
toilets.cpp:19:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld",&k);
   ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...