Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// stringstream num(c);
//         num >> x;
using namespace std;
// long double value(double a)
//{
////    long  p=100;
////    double r=0.01;
////   long  n=12;
//   long double t,x;
//   t=ceil(log(a/100))/ceil(log(1+(0.01)));
//   round(t);
//    return  t;
//}
// int main()
//{
//   long double a;
//   cin>>a;
////   long double p=100;
////   long double r=1/00;
////   long double n=12;
//   long double t;
//   //a=p(1+r/n)^nt;
//  // t=log(a)/(log(p)+log(1+(r/n)))*n
//  long double x = value(a);
//   cout<<x;
//
//
//    return 0;
//}
//// double valueE(double d)
////{
////    return log(d)/(log(p)+log(1+(r/n)))*n;
////}
////
////// function to evaluate logarithm base-10
////double value10(double d)
////{
////    return log10(d);
////}
////
////// driver program to test the above function
////int main()
////{
////    double d = 100;
////    cout << "The logarithm value(base-e) of " << d
////         << " is " << valueE(d) << endl;
////    cout << "The logarithm value(base-10) of " << d
////         << " is " << value10(d) << endl;
////    return 0;
////}

int main() {

  long k, a, b, c, sum = 0;
  cin >> a >> b >> c >> k;
  if ((k - a) >= 0)
    sum += a;
  else
    sum += k;
  k -= a;
  if (k > 0 && b != 0)
    k -= b;
  if (k > 0 && c != 0)
    sum += (k * (-1));
  //    while(k!=0)
  //    {
  //        if(a!=0)
  //        {
  //            sum+=1;
  //            a--;
  //        }
  //        else if(b!=0)
  //        {
  //            sum+=0;
  //            b--;
  //        }
  //        else
  //        {
  //            sum+=(-1);
  //            c--;
  //        }
  //        k--;
  //    }
  //
  cout << sum;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// stringstream num(c);
//         num >> x;
using namespace std;
// long double value(double a)
//{
////    long  p=100;
////    double r=0.01;
////   long  n=12;
//   long double t,x;
//   t=ceil(log(a/100))/ceil(log(1+(0.01)));
//   round(t);
//    return  t;
//}
// int main()
//{
//   long double a;
//   cin>>a;
////   long double p=100;
////   long double r=1/00;
////   long double n=12;
//   long double t;
//   //a=p(1+r/n)^nt;
//  // t=log(a)/(log(p)+log(1+(r/n)))*n
//  long double x = value(a);
//   cout<<x;
//
//
//    return 0;
//}
//// double valueE(double d)
////{
////    return log(d)/(log(p)+log(1+(r/n)))*n;
////}
////
////// function to evaluate logarithm base-10
////double value10(double d)
////{
////    return log10(d);
////}
////
////// driver program to test the above function
////int main()
////{
////    double d = 100;
////    cout << "The logarithm value(base-e) of " << d
////         << " is " << valueE(d) << endl;
////    cout << "The logarithm value(base-10) of " << d
////         << " is " << value10(d) << endl;
////    return 0;
////}

int main() {

  long k, a, b, c, sum = 0;
  cin >> a >> b >> c >> k;
  sum += a;
  k -= a;
  if (k != 0 && b != 0)
    k -= b;
  while (k != 0 && c != 0) {

    sum += -1;
    k--;
  }
  //    while(k!=0)
  //    {
  //        if(a!=0)
  //        {
  //            sum+=1;
  //            a--;
  //        }
  //        else if(b!=0)
  //        {
  //            sum+=0;
  //            b--;
  //        }
  //        else
  //        {
  //            sum+=(-1);
  //            c--;
  //        }
  //        k--;
  //    }
  //
  cout << sum;
}