Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/72d6634e9363a1d5ebe9 to your computer and use it in GitHub Desktop.
Save zsrinivas/72d6634e9363a1d5ebe9 to your computer and use it in GitHub Desktop.
spoj 9861. Hotels Along the Croatian Coast | HOTELS
#include <cstdio>
inline int gint()
{
int n = 0;
int sign=1;
register char c=0;
while(c<33)
c=getchar_unlocked();
if (c=='-')
{
sign=-1;
c=getchar_unlocked();
}
while(c>='0'&&c<='9')
{
n=(n<<3)+(n<<1)+(c-'0');
c=getchar_unlocked();
}
return n*sign;
}
int main(int argc, char *argv[])
{
int n, m, lo, hi;
long long int s, gt;
n = gint();
m = gint();
int hotels[n];
s = 0;
gt = 0;
lo = 0;
for (int i = 0; i < n; ++i)
{
hotels[i] = gint();
if(s + hotels[i] <= m)
{
s += hotels[i];
if(gt < s)
gt = s;
}
else
{
while((lo <= i) && ((s + hotels[i]) > m))
{
s -= hotels[lo];
lo++;
}
s += hotels[i];
if(gt < s)
gt = s;
}
}
printf("%lld\n", gt);
return 0;
}
def main():
n, m = map(int, raw_input().split())
hotels = map(int, raw_input().split())
s = 0
lo = 0
gt = 0
for x in xrange(n):
if s + hotels[x] <= m:
s += hotels[x]
if gt < s:
gt = s
else:
while (lo <= x) and (s + hotels[x] > m):
s -= hotels[lo]
lo += 1
s += hotels[x]
if gt < s:
gt = s
print gt
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment