The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:

There are space-separated non-empty words of lowercase and uppercase Latin letters.

There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.

It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.

When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.

The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.

You should write a program that will find minimal width of the ad.

Input

The first line contains number k (1 ≤ k ≤ 105).

The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters.

Output

Output minimal width of the ad.

Examples
input
4
garage for sa-le
output
7
input
4
Edu-ca-tion-al Ro-unds are so fun
output
10
Note

Here all spaces are replaced with dots.

In the first example one of possible results after all word wraps looks like this:

garage.
for.
sa-
le

The second example:

Edu-ca-
tion-al.
Ro-unds.
are.so.fun

官方题解说的意思其实不就是二分么' '和'_'是一样的

Firstly notice that there is no difference between space and hyphen, you can replace them with the same character, if you want.

Let's run binary search on answer. Fix width and greedily construct ad — wrap word only if you don't option to continue on the same line. Then check if number of lines doesn't exceed k.

#include <iostream>
using namespace std;
const int INF = (int)1e9;
int n,k,r,l;
string s;
int solve(int w)
{
int ans = ;
int l = ;
while(l < n)
{
ans++;
int r = l + w;
if (r >= n) break;
while(r > l && s[r - ] != ' ' && s[r - ] != '-') r--;
if (r == l) return INF;
l = r;
}
return ans;
} int main()
{
cin >> k;
getline(cin, s);
getline(cin, s);
n = s.length();
int l = , r = n;
while(r - l > )
{
int m = (l + r) / ;
if (solve(m) <= k)
r = m;
else
l = m;
}
cout << r << endl;
return ;
}

最新文章

  1. Spark MLlib - LFW
  2. PostgreSQL数据库中的常见错误
  3. 中国剩余定理(Chinese Remainder Theorem)
  4. FilterDispatcher已被标注为过时解决办法 &amp;gt;&amp;gt;&amp;gt; FilterDispatcher &amp;lt;&amp;lt;&amp;lt; is deprecated!
  5. 【转】Hive内部表、外部表
  6. JavaScript方法
  7. Struts2基础数据校验和框架校验
  8. 1.4 云计算的SPI服务模型
  9. Android 从Gallery获取图片
  10. C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名
  11. tuple解包给类的构造函数
  12. org.json和json-lib比较
  13. &lt;.net&gt;委托初探
  14. redhat linux使用Centos yum源
  15. 多线程学习之一独木桥模式Single Threaded Execution Pattern
  16. Composer 常用命令总结(三)
  17. @无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 通篇引用
  18. DELL OME监控服务器安装配置
  19. 用c#查询各快递物流信息
  20. 一个Silverlight工程的各文件解析

热门文章

  1. CSS中padding、margin两个重要属性的详细介绍及举例说明
  2. python爬虫之路——初识爬虫三大库,requests,lxml,beautiful.
  3. 如何使用ABAP Restful API进行代码的全文搜索
  4. VC 对话框设置背景颜色和图片
  5. Django 模板函数
  6. BCB:WebBrowser 控件说明
  7. OTOH
  8. C++ NULL与nullptr的区别
  9. c#List结合IEqualityComparer求交集
  10. bash编程之case语句,函数