# 1953. Maximum Number of Weeks for Which You Can Work
###### tags: `Leetcode` `Medium` `Greedy` `Task Schedule` `Priority Queue`
Link: https://leetcode.com/problems/maximum-number-of-weeks-for-which-you-can-work/
## 思路 $O(N)$ $O(1)$
跟[0767. Reorganize String](https://hackmd.io/Cs2YBT2RQHOFLNcLLFBPOQ), [1054. Distant Barcodes](https://hackmd.io/5RbR4Q9pRBKgIMKYQjgxQg)差不多
都是先找到最大的,如果最大的比total的一半大,说明有一部分排不进去,先排最大的,然后再把其他的塞进去
## Code
```java=
class Solution {
public long numberOfWeeks(int[] milestones) {
long ans = 0;
int max = 0;
for(int milestone:milestones){
ans += (long)milestone;
max = Math.max(max, milestone);
}
if(max > (ans+1)/2) return ans-(2*max-ans-1);
else return ans;
}
}
```