// High above the clouds there is a rainbow...
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn=3010,mod=1e9+7;
const ll inf=1e18;
int a[maxn],b[maxn],n,m;
ll c[maxn],val[maxn];
ll solve(){
ll ans=inf;
for(int msk=0;msk<(1<<m);msk++){
memset(val,0,sizeof val);
for(int i=0;i<m;i++){
if(bit(msk,i)){
for(int j=a[i];j!=b[i];j=(j+1)%n){
val[j]+=c[i];
}
}
else{
for(int j=b[i];j!=a[i];j=(j+1)%n){
val[j]+=c[i];
}
}
}
ll mx=0;
for(int i=0;i<n;i++){
mx=max(mx,val[i]);
}
ans=min(ans,mx);
}
return ans;
}
void print(){
cout<<n<<" "<<m<<endl;
for(int i=0;i<m;i++){
cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl;
}
}
int arr[maxn];
vector<int>v[maxn];
priority_queue<int>pq;
int best(){
while(sz(pq)) pq.pop();
int ans=0;
for(int i=0;i<n;i++){
for(int x:v[i])
pq.push(x);
while(arr[i]>0){
if(pq.empty() || pq.top()<i) assert(0);
int x=pq.top();
pq.pop();
ans++;
for(int k=i;k<x;k++)
arr[k]--;
}
}
return ans;
}
ll solve2(){
memset(val,0,sizeof val);
for(int i=0;i<n;i++){
v[i].clear();
}
for(int i=0;i<m;i++){
int A=a[i],B=b[i];
if(A>B) swap(A,B);
for(int j=A;j<B;j++)
val[j]++;
v[A].PB(B);
}
for(int i=0;i<n;i++){
arr[i]= (val[i]+1)/2;
}
return best();
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++)
cin>>a[i]>>b[i]>>c[i], --a[i],--b[i];
ll ans=inf;
for(int i=0;i<n;i++){
ans=min(ans,solve2());
for(int j=0;j<m;j++)
a[j]=(a[j]+1)%n, b[j]=(b[j]+1)%n;
}
return cout<<ans<<endl,0;
}
// Deathly mistakes:
// * Read the problem curfully.
// * Check maxn.
// * Overflows.
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
21 ms |
548 KB |
Output is correct |
17 |
Correct |
21 ms |
512 KB |
Output is correct |
18 |
Correct |
22 ms |
512 KB |
Output is correct |
19 |
Correct |
22 ms |
384 KB |
Output is correct |
20 |
Correct |
21 ms |
512 KB |
Output is correct |
21 |
Correct |
21 ms |
384 KB |
Output is correct |
22 |
Correct |
21 ms |
384 KB |
Output is correct |
23 |
Correct |
21 ms |
512 KB |
Output is correct |
24 |
Correct |
19 ms |
512 KB |
Output is correct |
25 |
Correct |
18 ms |
512 KB |
Output is correct |
26 |
Correct |
20 ms |
512 KB |
Output is correct |
27 |
Correct |
20 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
21 ms |
548 KB |
Output is correct |
17 |
Correct |
21 ms |
512 KB |
Output is correct |
18 |
Correct |
22 ms |
512 KB |
Output is correct |
19 |
Correct |
22 ms |
384 KB |
Output is correct |
20 |
Correct |
21 ms |
512 KB |
Output is correct |
21 |
Correct |
21 ms |
384 KB |
Output is correct |
22 |
Correct |
21 ms |
384 KB |
Output is correct |
23 |
Correct |
21 ms |
512 KB |
Output is correct |
24 |
Correct |
19 ms |
512 KB |
Output is correct |
25 |
Correct |
18 ms |
512 KB |
Output is correct |
26 |
Correct |
20 ms |
512 KB |
Output is correct |
27 |
Correct |
20 ms |
512 KB |
Output is correct |
28 |
Execution timed out |
6076 ms |
916 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
21 ms |
548 KB |
Output is correct |
17 |
Correct |
21 ms |
512 KB |
Output is correct |
18 |
Correct |
22 ms |
512 KB |
Output is correct |
19 |
Correct |
22 ms |
384 KB |
Output is correct |
20 |
Correct |
21 ms |
512 KB |
Output is correct |
21 |
Correct |
21 ms |
384 KB |
Output is correct |
22 |
Correct |
21 ms |
384 KB |
Output is correct |
23 |
Correct |
21 ms |
512 KB |
Output is correct |
24 |
Correct |
19 ms |
512 KB |
Output is correct |
25 |
Correct |
18 ms |
512 KB |
Output is correct |
26 |
Correct |
20 ms |
512 KB |
Output is correct |
27 |
Correct |
20 ms |
512 KB |
Output is correct |
28 |
Execution timed out |
6076 ms |
916 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
21 ms |
548 KB |
Output is correct |
17 |
Correct |
21 ms |
512 KB |
Output is correct |
18 |
Correct |
22 ms |
512 KB |
Output is correct |
19 |
Correct |
22 ms |
384 KB |
Output is correct |
20 |
Correct |
21 ms |
512 KB |
Output is correct |
21 |
Correct |
21 ms |
384 KB |
Output is correct |
22 |
Correct |
21 ms |
384 KB |
Output is correct |
23 |
Correct |
21 ms |
512 KB |
Output is correct |
24 |
Correct |
19 ms |
512 KB |
Output is correct |
25 |
Correct |
18 ms |
512 KB |
Output is correct |
26 |
Correct |
20 ms |
512 KB |
Output is correct |
27 |
Correct |
20 ms |
512 KB |
Output is correct |
28 |
Execution timed out |
6076 ms |
916 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |